mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
gvisor: return ENOTDIR from the unlink syscall
ENOTDIR has to be returned when a component used as a directory in pathname is not, in fact, a directory. PiperOrigin-RevId: 269037893
This commit is contained in:
@@ -1126,7 +1126,7 @@ func (d *Dirent) unmount(ctx context.Context, replacement *Dirent) error {
|
||||
|
||||
// Remove removes the given file or symlink. The root dirent is used to
|
||||
// resolve name, and must not be nil.
|
||||
func (d *Dirent) Remove(ctx context.Context, root *Dirent, name string) error {
|
||||
func (d *Dirent) Remove(ctx context.Context, root *Dirent, name string, dirPath bool) error {
|
||||
// Check the root.
|
||||
if root == nil {
|
||||
panic("Dirent.Remove: root must not be nil")
|
||||
@@ -1151,6 +1151,8 @@ func (d *Dirent) Remove(ctx context.Context, root *Dirent, name string) error {
|
||||
// Remove cannot remove directories.
|
||||
if IsDir(child.Inode.StableAttr) {
|
||||
return syscall.EISDIR
|
||||
} else if dirPath {
|
||||
return syscall.ENOTDIR
|
||||
}
|
||||
|
||||
// Remove cannot remove a mount point.
|
||||
|
||||
@@ -343,7 +343,7 @@ func TestRemoveExtraRefs(t *testing.T) {
|
||||
}
|
||||
d := f.Dirent
|
||||
|
||||
if err := test.root.Remove(contexttest.Context(t), test.root, name); err != nil {
|
||||
if err := test.root.Remove(contexttest.Context(t), test.root, name, false /* dirPath */); err != nil {
|
||||
t.Fatalf("root.Remove(root, %q) failed: %v", name, err)
|
||||
}
|
||||
|
||||
|
||||
@@ -1423,9 +1423,6 @@ func unlinkAt(t *kernel.Task, dirFD int32, addr usermem.Addr) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if dirPath {
|
||||
return syserror.ENOENT
|
||||
}
|
||||
|
||||
return fileOpAt(t, dirFD, path, func(root *fs.Dirent, d *fs.Dirent, name string, _ uint) error {
|
||||
if !fs.IsDir(d.Inode.StableAttr) {
|
||||
@@ -1436,7 +1433,7 @@ func unlinkAt(t *kernel.Task, dirFD int32, addr usermem.Addr) error {
|
||||
return err
|
||||
}
|
||||
|
||||
return d.Remove(t, root, name)
|
||||
return d.Remove(t, root, name, dirPath)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -123,6 +123,8 @@ TEST(UnlinkTest, AtBad) {
|
||||
SyscallSucceeds());
|
||||
EXPECT_THAT(unlinkat(dirfd, "UnlinkAtFile", AT_REMOVEDIR),
|
||||
SyscallFailsWithErrno(ENOTDIR));
|
||||
EXPECT_THAT(unlinkat(dirfd, "UnlinkAtFile/", 0),
|
||||
SyscallFailsWithErrno(ENOTDIR));
|
||||
ASSERT_THAT(close(fd), SyscallSucceeds());
|
||||
EXPECT_THAT(unlinkat(dirfd, "UnlinkAtFile", 0), SyscallSucceeds());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user