vfs: read directory and check for a file before mkdir

Before this change when doing Mkdir the VFS layer could add the new
item to an unread directory which caused confusion.

It could also do mkdir on a file when run on a bucket based remote
which would temporarily overwrite the file with a directory.

Fixes #2993
This commit is contained in:
Nick Craig-Wood
2019-02-28 14:05:17 +00:00
parent e3bceb9083
commit 173dfbd051
2 changed files with 44 additions and 2 deletions
+17 -2
View File
@@ -458,8 +458,23 @@ func (d *Dir) Mkdir(name string) (*Dir, error) {
return nil, EROFS
}
path := path.Join(d.path, name)
node, err := d.stat(name)
switch err {
case ENOENT:
// not found, carry on
case nil:
// found so check what it is
if node.IsDir() {
return node.(*Dir), err
}
return nil, EEXIST
default:
// a different error - report
fs.Errorf(d, "Dir.Mkdir failed to read directory: %v", err)
return nil, err
}
// fs.Debugf(path, "Dir.Mkdir")
err := d.f.Mkdir(path)
err = d.f.Mkdir(path)
if err != nil {
fs.Errorf(d, "Dir.Mkdir failed to create directory: %v", err)
return nil, err
@@ -600,7 +615,7 @@ func (d *Dir) Rename(oldName, newName string, destDir *Dir) error {
}
default:
err = errors.Errorf("unknown type %T", oldNode)
fs.Errorf(d.path, "Dir.ReadDirAll error: %v", err)
fs.Errorf(d.path, "Dir.Rename error: %v", err)
return err
}