mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Implement MoveFile functionality in FileManager and corresponding tests
This commit is contained in:
@@ -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) }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user