Add test that symlinking over a directory returns EEXIST.

Also remove comments in InodeOperations that required that implementation of
some Create* operations ensure that the name does not already exist, since
these checks are all centralized in the Dirent.

PiperOrigin-RevId: 241637335
Change-Id: Id098dc6063ff7c38347af29d1369075ad1e89a58
This commit is contained in:
Nicolas Lacasse 2019-04-02 17:27:30 -07:00 committed by Shentubot
parent f9431fb20f
commit 1776ab28f0
3 changed files with 11 additions and 23 deletions

View File

@ -104,15 +104,12 @@ type InodeOperations interface {
CreateLink(ctx context.Context, dir *Inode, oldname string, newname string) error
// CreateHardLink creates a hard link under dir between the target
// Inode and name. Implementations must ensure that name does not
// already exist.
// Inode and name.
//
// The caller must ensure this operation is permitted.
CreateHardLink(ctx context.Context, dir *Inode, target *Inode, name string) error
// CreateFifo creates a new named pipe under dir at name.
// Implementations must ensure that an Inode at name does not
// already exist.
//
// The caller must ensure that this operation is permitted.
CreateFifo(ctx context.Context, dir *Inode, name string, perm FilePermissions) error
@ -144,7 +141,6 @@ type InodeOperations interface {
Rename(ctx context.Context, oldParent *Inode, oldName string, newParent *Inode, newName string, replacement bool) error
// Bind binds a new socket under dir at the given name.
// Implementations must ensure that name does not already exist.
//
// The caller must ensure that this operation is permitted.
Bind(ctx context.Context, dir *Inode, name string, data transport.BoundEndpoint, perm FilePermissions) (*Dirent, error)

View File

@ -268,10 +268,6 @@ func (d *Dir) createInodeOperationsCommon(ctx context.Context, name string, make
d.mu.Lock()
defer d.mu.Unlock()
if _, ok := d.children[name]; ok {
return nil, syscall.EEXIST
}
inode, err := makeInodeOperations()
if err != nil {
return nil, err

View File

@ -113,23 +113,19 @@ TEST(SymlinkTest, CannotCreateSymlinkInReadOnlyDir) {
}
TEST(SymlinkTest, CannotSymlinkOverExistingFile) {
const std::string oldname = NewTempAbsPath();
const std::string newname = NewTempAbsPath();
const auto oldfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
const auto newfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
int oldfd;
int newfd;
ASSERT_THAT(oldfd = open(oldname.c_str(), O_CREAT | O_RDWR, 0666),
SyscallSucceeds());
EXPECT_THAT(close(oldfd), SyscallSucceeds());
ASSERT_THAT(newfd = open(newname.c_str(), O_CREAT | O_RDWR, 0666),
SyscallSucceeds());
EXPECT_THAT(close(newfd), SyscallSucceeds());
EXPECT_THAT(symlink(oldname.c_str(), newname.c_str()),
EXPECT_THAT(symlink(oldfile.path().c_str(), newfile.path().c_str()),
SyscallFailsWithErrno(EEXIST));
}
EXPECT_THAT(unlink(oldname.c_str()), SyscallSucceeds());
EXPECT_THAT(unlink(newname.c_str()), SyscallSucceeds());
TEST(SymlinkTest, CannotSymlinkOverExistingDir) {
const auto oldfile = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile());
const auto newdir = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
EXPECT_THAT(symlink(oldfile.path().c_str(), newdir.path().c_str()),
SyscallFailsWithErrno(EEXIST));
}
TEST(SymlinkTest, OldnameIsEmpty) {