diff --git a/errors.go b/errors.go index 7ed27cb..860d7ec 100644 --- a/errors.go +++ b/errors.go @@ -3,12 +3,17 @@ package fuse -import bazilfuse "bazil.org/fuse" +import ( + "syscall" + + bazilfuse "bazil.org/fuse" +) const ( // Errors corresponding to kernel error numbers. These may be treated // specially when returned by a FileSystem method. - ENOSYS = bazilfuse.ENOSYS - ENOENT = bazilfuse.ENOENT - EIO = bazilfuse.EIO + EIO = bazilfuse.EIO + ENOENT = bazilfuse.ENOENT + ENOSYS = bazilfuse.ENOSYS + ENOTEMPTY = bazilfuse.Errno(syscall.ENOTEMPTY) ) diff --git a/samples/memfs/fs.go b/samples/memfs/fs.go index 6e8b216..31fe9f4 100644 --- a/samples/memfs/fs.go +++ b/samples/memfs/fs.go @@ -306,7 +306,22 @@ func (fs *memFS) RmDir( parent := fs.getInodeForModifyingOrDie(req.Parent) defer parent.mu.Unlock() - // TODO(jacobsa): Check for empty. (Make sure we have a failing test first.) + // Find the child within the parent. + childID, ok := parent.LookUpChild(req.Name) + if !ok { + err = fuse.ENOENT + return + } + + // Grab the child. + child := fs.getInodeForModifyingOrDie(childID) + defer child.mu.Unlock() + + // Make sure the child is empty. + if child.Len() != 0 { + err = fuse.ENOTEMPTY + return + } // Remove the entry within the parent. parent.RemoveChild(req.Name) diff --git a/samples/memfs/inode.go b/samples/memfs/inode.go index 2a3eb57..255dfe0 100644 --- a/samples/memfs/inode.go +++ b/samples/memfs/inode.go @@ -143,6 +143,20 @@ func (inode *inode) findChild(name string) (i int, ok bool) { // Public methods //////////////////////////////////////////////////////////////////////// +// Return the number of children of the directory. +// +// REQUIRES: inode.dir +// SHARED_LOCKS_REQUIRED(inode.mu) +func (inode *inode) Len() (n int) { + for _, e := range inode.entries { + if e.Type != fuseutil.DT_Unknown { + n++ + } + } + + return +} + // Find an entry for the given child name and return its inode ID. // // REQUIRES: inode.dir diff --git a/samples/memfs/memfs_test.go b/samples/memfs/memfs_test.go index 01afb60..456681b 100644 --- a/samples/memfs/memfs_test.go +++ b/samples/memfs/memfs_test.go @@ -334,7 +334,7 @@ func (t *MemFSTest) Rmdir_NonEmpty() { err = os.Remove(path.Join(t.mfs.Dir(), "foo")) AssertNe(nil, err) - ExpectThat(err, Error(HasSubstr("non-empty"))) + ExpectThat(err, Error(HasSubstr("not empty"))) } func (t *MemFSTest) Rmdir_Empty() {