vfs: Update parent directory modtimes on vfs actions

This isn't written back to the storage so might change on remount but
makes the VFS just a little more POSIX compatible.
This commit is contained in:
David Pedersen 2023-05-15 21:52:03 +02:00 committed by Nick Craig-Wood
parent 7453b7d5f3
commit 071c3f28e5
2 changed files with 28 additions and 0 deletions

View File

@ -923,6 +923,10 @@ func (d *Dir) Create(name string, flags int) (*File, error) {
if d.vfs.Opt.ReadOnly { if d.vfs.Opt.ReadOnly {
return nil, EROFS return nil, EROFS
} }
if err = d.SetModTime(time.Now()); err != nil {
fs.Errorf(d, "Dir.Create failed to set modtime on parent dir: %v", err)
return nil, err
}
// This gets added to the directory when the file is opened for write // This gets added to the directory when the file is opened for write
return newFile(d, d.Path(), nil, name), nil return newFile(d, d.Path(), nil, name), nil
} }
@ -957,6 +961,10 @@ func (d *Dir) Mkdir(name string) (*Dir, error) {
fsDir := fs.NewDir(path, time.Now()) fsDir := fs.NewDir(path, time.Now())
dir := newDir(d.vfs, d.f, d, fsDir) dir := newDir(d.vfs, d.f, d, fsDir)
d.addObject(dir) d.addObject(dir)
if err = d.SetModTime(time.Now()); err != nil {
fs.Errorf(d, "Dir.Mkdir failed to set modtime on parent dir: %v", err)
return nil, err
}
// fs.Debugf(path, "Dir.Mkdir OK") // fs.Debugf(path, "Dir.Mkdir OK")
return dir, nil return dir, nil
} }
@ -1028,6 +1036,10 @@ func (d *Dir) RemoveName(name string) error {
fs.Errorf(d, "Dir.Remove error: %v", err) fs.Errorf(d, "Dir.Remove error: %v", err)
return err return err
} }
if err = d.SetModTime(time.Now()); err != nil {
fs.Errorf(d, "Dir.Remove failed to set modtime on parent dir: %v", err)
return err
}
return node.Remove() return node.Remove()
} }
@ -1098,6 +1110,10 @@ func (d *Dir) Rename(oldName, newName string, destDir *Dir) error {
// Show moved - delete from old dir and add to new // Show moved - delete from old dir and add to new
d.delObject(oldName) d.delObject(oldName)
destDir.addObject(oldNode) destDir.addObject(oldNode)
if err = d.SetModTime(time.Now()); err != nil {
fs.Errorf(d, "Dir.Rename failed to set modtime on parent dir: %v", err)
return err
}
// fs.Debugf(newPath, "Dir.Rename renamed from %q", oldPath) // fs.Debugf(newPath, "Dir.Rename renamed from %q", oldPath)
// fs.Debugf(d, "AFTER\n%s", d.dump()) // fs.Debugf(d, "AFTER\n%s", d.dump())

View File

@ -343,9 +343,12 @@ func TestDirOpen(t *testing.T) {
func TestDirCreate(t *testing.T) { func TestDirCreate(t *testing.T) {
_, vfs, dir, _ := dirCreate(t) _, vfs, dir, _ := dirCreate(t)
origModTime := dir.ModTime()
time.Sleep(100 * time.Millisecond) // for low rez Windows timers
file, err := dir.Create("potato", os.O_WRONLY|os.O_CREATE) file, err := dir.Create("potato", os.O_WRONLY|os.O_CREATE)
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, int64(0), file.Size()) assert.Equal(t, int64(0), file.Size())
assert.True(t, dir.ModTime().After(origModTime))
fd, err := file.Open(os.O_WRONLY | os.O_CREATE) fd, err := file.Open(os.O_WRONLY | os.O_CREATE)
require.NoError(t, err) require.NoError(t, err)
@ -385,8 +388,11 @@ func TestDirMkdir(t *testing.T) {
_, err := dir.Mkdir("file1") _, err := dir.Mkdir("file1")
assert.Error(t, err) assert.Error(t, err)
origModTime := dir.ModTime()
time.Sleep(100 * time.Millisecond) // for low rez Windows timers
sub, err := dir.Mkdir("sub") sub, err := dir.Mkdir("sub")
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, dir.ModTime().After(origModTime))
// check the vfs // check the vfs
checkListing(t, dir, []string{"file1,14,false", "sub,0,true"}) checkListing(t, dir, []string{"file1,14,false", "sub,0,true"})
@ -488,8 +494,11 @@ func TestDirRemoveAll(t *testing.T) {
func TestDirRemoveName(t *testing.T) { func TestDirRemoveName(t *testing.T) {
r, vfs, dir, _ := dirCreate(t) r, vfs, dir, _ := dirCreate(t)
origModTime := dir.ModTime()
time.Sleep(100 * time.Millisecond) // for low rez Windows timers
err := dir.RemoveName("file1") err := dir.RemoveName("file1")
require.NoError(t, err) require.NoError(t, err)
assert.True(t, dir.ModTime().After(origModTime))
checkListing(t, dir, []string(nil)) checkListing(t, dir, []string(nil))
root, err := vfs.Root() root, err := vfs.Root()
require.NoError(t, err) require.NoError(t, err)
@ -538,8 +547,11 @@ func TestDirRename(t *testing.T) {
dir = node.(*Dir) dir = node.(*Dir)
// Rename a file // Rename a file
origModTime := dir.ModTime()
time.Sleep(100 * time.Millisecond) // for low rez Windows timers
err = dir.Rename("file1", "file2", root) err = dir.Rename("file1", "file2", root)
assert.NoError(t, err) assert.NoError(t, err)
assert.True(t, dir.ModTime().After(origModTime))
checkListing(t, root, []string{"dir2,0,true", "file2,14,false"}) checkListing(t, root, []string{"dir2,0,true", "file2,14,false"})
checkListing(t, dir, []string{"file3,15,false"}) checkListing(t, dir, []string{"file3,15,false"})