-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #322 from nkryuchkov/fix/move-files
Fix moving files between different drives and filesystems
- Loading branch information
Showing
3 changed files
with
82 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package rename | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"os" | ||
) | ||
|
||
const crossDeviceError = "invalid cross-device link" | ||
|
||
// Rename renames (moves) oldPath to newPath using os.Rename. | ||
// If paths are located on different drives or filesystems, os.Rename fails. | ||
// In that case, Rename uses a workaround by copying oldPath to newPath and removing oldPath thereafter. | ||
func Rename(oldPath, newPath string) error { | ||
if err := os.Rename(oldPath, newPath); err == nil || err.Error() != crossDeviceError { | ||
return err | ||
} | ||
|
||
stat, err := os.Stat(oldPath) | ||
if err != nil { | ||
return fmt.Errorf("stat: %w", err) | ||
} | ||
|
||
if !stat.Mode().IsRegular() { | ||
return fmt.Errorf("is regular: %w", err) | ||
} | ||
|
||
// Paths are located on different devices. | ||
if err := move(oldPath, newPath); err != nil { | ||
return fmt.Errorf("move: %w", err) | ||
} | ||
|
||
if err := os.Chmod(newPath, stat.Mode()); err != nil { | ||
return fmt.Errorf("chmod: %w", err) | ||
} | ||
|
||
if err := os.Remove(oldPath); err != nil { | ||
return fmt.Errorf("remove: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func move(oldPath string, newPath string) error { | ||
inputFile, err := os.Open(oldPath) // nolint:gosec | ||
if err != nil { | ||
return fmt.Errorf("open: %w", err) | ||
} | ||
|
||
defer func() { | ||
if err := inputFile.Close(); err != nil { | ||
log.Printf("Failed to close file %q: %v", inputFile.Name(), err) | ||
} | ||
}() | ||
|
||
outputFile, err := os.Create(newPath) | ||
if err != nil { | ||
return fmt.Errorf("create: %w", err) | ||
} | ||
|
||
defer func() { | ||
if err := outputFile.Close(); err != nil { | ||
log.Printf("Failed to close file %q: %v", outputFile.Name(), err) | ||
} | ||
}() | ||
|
||
if _, err = io.Copy(outputFile, inputFile); err != nil { | ||
return fmt.Errorf("copy: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters