Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

mountlib: add Remove and RemoveAll methods to Node

Nick Craig-Wood3af9d63261ff
diff --git a/cmd/mountlib/dir.go b/cmd/mountlib/dir.go
index b35418aaf..996ec3711 100644
--- a/cmd/mountlib/dir.go
+++ b/cmd/mountlib/dir.go
@@ -362,6 +362,55 @@ func (d *Dir) Mkdir(name string) (*Dir, error) {
return dir, nil
}
+// Remove the directory
+func (d *Dir) Remove() error {
+ if d.fsys.readOnly {
+ return EROFS
+ }
+ // Check directory is empty first
+ empty, err := d.isEmpty()
+ if err != nil {
+ fs.Errorf(d.path, "Dir.Remove dir error: %v", err)
+ return err
+ }
+ if !empty {
+ fs.Errorf(d.path, "Dir.Remove not empty")
+ return ENOTEMPTY
+ }
+ // remove directory
+ err = d.f.Rmdir(d.path)
+ if err != nil {
+ fs.Errorf(d.path, "Dir.Remove failed to remove directory: %v", err)
+ return err
+ }
+ // Remove the item from the parent directory listing
+ if d.parent != nil {
+ d.parent.delObject(d.Name())
+ }
+ return nil
+}
+
+// RemoveAll removes the directory and any contents recursively
+func (d *Dir) RemoveAll() error {
+ if d.fsys.readOnly {
+ return EROFS
+ }
+ // Remove contents of the directory
+ items, err := d.ReadDirAll()
+ if err != nil {
+ fs.Errorf(d.path, "Dir.RemoveAll failed to read directory: %v", err)
+ return err
+ }
+ for _, item := range items {
+ err = item.Node.RemoveAll()
+ if err != nil {
+ fs.Errorf(item.Obj, "Dir.RemoveAll failed to remove: %v", err)
+ return err
+ }
+ }
+ return d.Remove()
+}
+
// RemoveName removes the entry with the given name from the receiver,
// which must be a directory. The entry to be removed may correspond
// to a file (unlink) or to a directory (rmdir).
@@ -376,39 +425,7 @@ func (d *Dir) RemoveName(name string) error {
fs.Errorf(path, "Dir.Remove error: %v", err)
return err
}
- switch x := item.Obj.(type) {
- case fs.Object:
- err = x.Remove()
- if err != nil {
- fs.Errorf(path, "Dir.Remove file error: %v", err)
- return err
- }
- case fs.Directory:
- // Check directory is empty first
- dir := item.Node.(*Dir)
- empty, err := dir.isEmpty()
- if err != nil {
- fs.Errorf(path, "Dir.Remove dir error: %v", err)
- return err
- }
- if !empty {
- fs.Errorf(path, "Dir.Remove not empty")
- return ENOTEMPTY
- }
- // remove directory
- err = d.f.Rmdir(path)
- if err != nil {
- fs.Errorf(path, "Dir.Remove failed to remove directory: %v", err)
- return err
- }
- default:
- fs.Errorf(path, "Dir.Remove unknown type %T", item)
- return errors.Errorf("unknown type %T", item)
- }
- // Remove the item from the directory listing
- d.delObject(name)
- // fs.Debugf(path, "Dir.Remove OK")
- return nil
+ return item.Node.Remove()
}
// Rename the file
diff --git a/cmd/mountlib/file.go b/cmd/mountlib/file.go
index a01282812..71a813d69 100644
--- a/cmd/mountlib/file.go
+++ b/cmd/mountlib/file.go
@@ -251,3 +251,23 @@ func (f *File) OpenWrite() (fh *WriteFileHandle, err error) {
func (f *File) Fsync() error {
return nil
}
+
+// Remove the file
+func (f *File) Remove() error {
+ if f.d.fsys.readOnly {
+ return EROFS
+ }
+ err := f.o.Remove()
+ if err != nil {
+ fs.Errorf(f.o, "File.Remove file error: %v", err)
+ return err
+ }
+ // Remove the item from the directory listing
+ f.d.delObject(f.Name())
+ return nil
+}
+
+// RemoveAll the file - same as remove for files
+func (f *File) RemoveAll() error {
+ return f.Remove()
+}
diff --git a/cmd/mountlib/fs.go b/cmd/mountlib/fs.go
index 125079241..e432f1990 100644
--- a/cmd/mountlib/fs.go
+++ b/cmd/mountlib/fs.go
@@ -17,6 +17,8 @@ type Node interface {
Inode() uint64
SetModTime(modTime time.Time) error
Fsync() error
+ Remove() error
+ RemoveAll() error
}
var (