Skip to content

Commit d26a643

Browse files
committed
fix: rename file fallback
1 parent d122a1e commit d26a643

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

drivers/local/driver.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,35 @@ func renameFile(srcPath, dstPath string, replace bool) error {
294294
if utils.Exists(dstPath) && !replace {
295295
return fmt.Errorf("destination file %s already exists", dstPath)
296296
}
297-
return atomic.ReplaceFile(srcPath, dstPath)
297+
err := atomic.ReplaceFile(srcPath, dstPath)
298+
if err != nil {
299+
println("atomic rename failed:", err, " src:", srcPath, " dst:", dstPath)
300+
// Fall back to copy + delete
301+
err2 := cp.Copy(srcPath, dstPath, cp.Options{
302+
Sync: true,
303+
PreserveTimes: true,
304+
PreserveOwner: true,
305+
})
306+
if err2 != nil {
307+
return fmt.Errorf("Rename failed: %s to %s: %w, failed to copy %s to %s: %w", srcPath, dstPath, err, srcPath, dstPath, err2)
308+
}
309+
// Copy succeeded, delete the source
310+
info, err2 := os.Lstat(srcPath)
311+
if err2 != nil {
312+
return fmt.Errorf("failed to stat source file %s after copy: %w", srcPath, err2)
313+
}
314+
if info.IsDir() {
315+
err2 = os.RemoveAll(srcPath)
316+
} else {
317+
err2 = os.Remove(srcPath)
318+
}
319+
if err2 != nil {
320+
return fmt.Errorf("failed to remove source file %s after copy: %w", srcPath, err2)
321+
}
322+
// Remove succeeded
323+
return nil
324+
}
325+
return nil
298326
}
299327
func (d *Local) Move(ctx context.Context, srcObj, dstDir model.Obj) error {
300328
srcPath := srcObj.GetPath()

0 commit comments

Comments
 (0)