This breaks the expected copyFile semantics, which should truncate an existing file instead of throwing an error.
We could pro-actively remove the target:
func copyFile(target, source string) error {
+ if err := os.Remove(target); err != nil {
+ if !errors.Is(err, unix.ENOENT) {
+ return fmt.Errorf("removing copyFile target failed: %w", err)
+ }
+ }
if err := unix.Clonefile(source, target, unix.CLONE_NOFOLLOW); err != nil {
if !errors.Is(err, unix.ENOTSUP) {
return fmt.Errorf("clonefile failed: %w", err)
But I'm not sure if this could become a problem when clonefile is not supported, or when the copy operation is cross-device, and we still fall back to copyFile.
I'm happy to create a PR with the patch above, but would like confirmation first that this would indeed be the correct approach.
This breaks the expected
copyFilesemantics, which should truncate an existing file instead of throwing an error.We could pro-actively remove the target:
func copyFile(target, source string) error { + if err := os.Remove(target); err != nil { + if !errors.Is(err, unix.ENOENT) { + return fmt.Errorf("removing copyFile target failed: %w", err) + } + } if err := unix.Clonefile(source, target, unix.CLONE_NOFOLLOW); err != nil { if !errors.Is(err, unix.ENOTSUP) { return fmt.Errorf("clonefile failed: %w", err)But I'm not sure if this could become a problem when
clonefileis not supported, or when the copy operation is cross-device, and we still fall back tocopyFile.I'm happy to create a PR with the patch above, but would like confirmation first that this would indeed be the correct approach.