fix: Block creating files with non-existing parent directories

This commit is contained in:
Felicitas Pojtinger
2022-01-12 22:02:17 +01:00
parent a83f28809f
commit dcc51ea88f
2 changed files with 20 additions and 6 deletions

View File

@@ -93,6 +93,21 @@ func (f *STFS) Create(name string) (afero.File, error) {
return nil, os.ErrInvalid
}
if _, err := inventory.Stat(
f.metadata,
filepath.Dir(name),
false,
f.onHeader,
); err != nil {
if err == sql.ErrNoRows {
return nil, os.ErrNotExist
}
return nil, err
}
return f.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
}

View File

@@ -653,12 +653,11 @@ var createTests = []struct {
createArgs{""},
true,
},
// FIXME: STFS can create file in non-existent directory, which should not be possible
// {
// "Can not create /nonexistent/test.txt",
// createArgs{"/nonexistent/test.txt"},
// true,
// },
{
"Can not create /nonexistent/test.txt",
createArgs{"/nonexistent/test.txt"},
true,
},
}
func TestSTFS_Create(t *testing.T) {