On rename, rename in cache too if the file exists

Signed-off-by: Anagh Kumar Baranwal <6824881+darthShadow@users.noreply.github.com>
This commit is contained in:
Anagh Kumar Baranwal 2019-08-26 18:13:11 +05:30 committed by Nick Craig-Wood
parent 5ddfa9f7f6
commit 5928704e1b
3 changed files with 48 additions and 2 deletions

View File

@ -270,12 +270,50 @@ func (c *cache) exists(name string) bool {
if err != nil { if err != nil {
return false return false
} }
if fi.IsDir() { // checks for non-regular files (e.g. directories, symlinks, devices, etc.)
if !fi.Mode().IsRegular() {
return false return false
} }
return true return true
} }
// renames the file in cache
func (c *cache) rename(name string, newName string) (err error) {
osOldPath := c.toOSPath(name)
osNewPath := c.toOSPath(newName)
sfi, err := os.Stat(osOldPath)
if err != nil {
return errors.Wrapf(err, "Failed to stat source: %s", osOldPath)
}
if !sfi.Mode().IsRegular() {
// cannot copy non-regular files (e.g., directories, symlinks, devices, etc.)
return errors.Errorf("Non-regular source file: %s (%q)", sfi.Name(), sfi.Mode().String())
}
dfi, err := os.Stat(osNewPath)
if err != nil {
if !os.IsNotExist(err) {
return errors.Wrapf(err, "Failed to stat destination: %s", osNewPath)
}
parent := findParent(osNewPath)
err = os.MkdirAll(parent, 0700)
if err != nil {
return errors.Wrapf(err, "Failed to create parent dir: %s", parent)
}
} else {
if !(dfi.Mode().IsRegular()) {
return errors.Errorf("Non-regular destination file: %s (%q)", dfi.Name(), dfi.Mode().String())
}
if os.SameFile(sfi, dfi) {
return nil
}
}
if err = os.Rename(osOldPath, osNewPath); err != nil {
return errors.Wrapf(err, "Failed to rename in cache: %s to %s", osOldPath, osNewPath)
}
fs.Infof(name, "Renamed in cache")
return nil
}
// _close marks name as closed - must be called with the lock held // _close marks name as closed - must be called with the lock held
func (c *cache) _close(isFile bool, name string) { func (c *cache) _close(isFile bool, name string) {
for { for {

View File

@ -129,6 +129,14 @@ func (f *File) rename(ctx context.Context, destDir *Dir, newName string) error {
fs.Errorf(f.Path(), "File.Rename error: %v", err) fs.Errorf(f.Path(), "File.Rename error: %v", err)
return err return err
} }
// Rename in the cache too if it exists
if f.d.vfs.Opt.CacheMode >= CacheModeWrites && f.d.vfs.cache.exists(f.Path()) {
if err := f.d.vfs.cache.rename(f.Path(), newPath); err != nil {
fs.Infof(f.Path(), "File.Rename failed in Cache: %v", err)
}
}
// newObject can be nil here for example if --dry-run // newObject can be nil here for example if --dry-run
if newObject == nil { if newObject == nil {
err = errors.New("rename failed: nil object returned") err = errors.New("rename failed: nil object returned")

View File

@ -21,7 +21,7 @@ func fileCreate(t *testing.T, r *fstest.Run) (*VFS, *File, fstest.Item) {
node, err := vfs.Stat("dir/file1") node, err := vfs.Stat("dir/file1")
require.NoError(t, err) require.NoError(t, err)
require.True(t, node.IsFile()) require.True(t, node.Mode().IsRegular())
return vfs, node.(*File), file1 return vfs, node.(*File), file1
} }