Implement MoveFile functionality in FileManager and corresponding tests

This commit is contained in:
2025-07-11 19:49:08 +02:00
parent 5a6895ecdc
commit 9bb95f603c
4 changed files with 158 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ import (
type fileSystem interface {
ReadFile(path string) ([]byte, error)
WriteFile(path string, data []byte, perm fs.FileMode) error
MoveFile(src, dst string) error
Remove(path string) error
MkdirAll(path string, perm fs.FileMode) error
RemoveAll(path string) error
@@ -38,6 +39,21 @@ func (f *osFS) WriteFile(path string, data []byte, perm fs.FileMode) error {
return os.WriteFile(path, data, perm)
}
// MoveFile moves the file from src to dst, overwriting if necessary.
func (f *osFS) MoveFile(src, dst string) error {
if err := os.Rename(src, dst); err != nil {
if os.IsExist(err) {
// If the destination exists, remove it and try again
if err := os.Remove(dst); err != nil && !os.IsNotExist(err) {
return err
}
return os.Rename(src, dst)
}
return err
}
return nil
}
// Remove deletes the file at the given path.
func (f *osFS) Remove(path string) error { return os.Remove(path) }