-
Notifications
You must be signed in to change notification settings - Fork 12
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 #51 from babarot/babarot/move
Make moving files across different partitions possible by adding fallback to copy-and-delete
- Loading branch information
Showing
9 changed files
with
160 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
name: Go | ||
on: | ||
pull_request: | ||
paths: | ||
- '**/*.go' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
|
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
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
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,60 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"path/filepath" | ||
|
||
cp "github.com/otiai10/copy" | ||
) | ||
|
||
// copyAndDelete copies a file or directory (recursively) and then deletes the original. | ||
func copyAndDelete(src, dst string) error { | ||
slog.Debug("starting copy and delete operation", "from", src, "to", dst) | ||
if err := cp.Copy(src, dst); err != nil { | ||
return fmt.Errorf("failed to copy file: %w", err) | ||
} | ||
|
||
// If the copy is successful, remove the original file or directory | ||
if err := os.Remove(src); err != nil { | ||
// If removal of the source fails after copying, attempt to delete the copied file as well | ||
if rmErr := os.Remove(dst); rmErr != nil { | ||
return fmt.Errorf( | ||
"failed to remove both source and destination files: source error: %v, destination error: %v", | ||
err, rmErr) | ||
} | ||
return fmt.Errorf("failed to remove source file after successful copy: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// move attempts to rename a file or directory. If it's on different partitions, it falls back to copying and deleting. | ||
func move(src, dst string) error { | ||
dstDir := filepath.Dir(dst) | ||
|
||
// Ensure the destination directory exists before attempting to move | ||
if _, err := os.Stat(dstDir); os.IsNotExist(err) { | ||
slog.Debug("mkdir", "dir", dstDir) | ||
if err := os.MkdirAll(dstDir, 0777); err != nil { | ||
return fmt.Errorf("failed to create destination directory: %w", err) | ||
} | ||
} | ||
|
||
// Check if source and destination are on the same partition | ||
samePartition, err := isSamePartition(src, dstDir) | ||
if err != nil { | ||
return err | ||
} | ||
defer slog.Debug("file moved", "from", src, "to", dst) | ||
|
||
// If they are on the same partition, use os.Rename; otherwise, fallback to copy-and-delete | ||
if samePartition { | ||
slog.Debug("moving file with os.Rename") | ||
return os.Rename(src, dst) | ||
} | ||
|
||
slog.Debug("different partitions detected, falling back to copy-and-delete operation") | ||
return copyAndDelete(src, dst) | ||
} |
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,37 @@ | ||
//go:build !windows | ||
|
||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
// isSamePartition checks if the source and destination reside on the same filesystem partition. | ||
func isSamePartition(src, dst string) (bool, error) { | ||
srcStat, err := os.Stat(src) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to get source file stats: %w", err) | ||
} | ||
|
||
dstStat, err := os.Stat(dst) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to get destination file stats: %w", err) | ||
} | ||
|
||
srcSys := srcStat.Sys().(*syscall.Stat_t) | ||
dstSys := dstStat.Sys().(*syscall.Stat_t) | ||
|
||
// Compare the device identifiers (st_dev) of the source and destination | ||
// If the device IDs are the same, the files are on the same partition. | ||
samePartition := srcSys.Dev == dstSys.Dev | ||
|
||
slog.Debug("check src/dst file info", | ||
"samePartition", samePartition, | ||
"src st_dev", srcSys.Dev, | ||
"dst st_dev", dstSys.Dev) | ||
|
||
return samePartition, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//go:build windows | ||
|
||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"path/filepath" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
// isSamePartition checks if the source and destination reside on the same filesystem partition on Windows. | ||
func isSamePartition(src, dst string) (bool, error) { | ||
// Get the volume names (drive letters) for both source and destination paths | ||
srcVolume := filepath.VolumeName(src) | ||
dstVolume := filepath.VolumeName(dst) | ||
|
||
if srcVolume == "" || dstVolume == "" { | ||
return false, fmt.Errorf("failed to determine volume name from file paths") | ||
} | ||
|
||
// Get volume information for both source and destination volumes | ||
var srcVolID, dstVolID uint32 | ||
err := windows.GetVolumeInformation(windows.StringToUTF16Ptr(srcVolume), nil, 0, &srcVolID, nil, nil, nil, 0) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to get source volume information: %w", err) | ||
} | ||
|
||
err = windows.GetVolumeInformation(windows.StringToUTF16Ptr(dstVolume), nil, 0, &dstVolID, nil, nil, nil, 0) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to get destination volume information: %w", err) | ||
} | ||
|
||
// Compare the volume IDs to determine if they are on the same partition | ||
samePartition := srcVolID == dstVolID | ||
|
||
slog.Debug("check src/dst volume info", | ||
"samePartition", samePartition, | ||
"src volume", srcVolID, | ||
"dst volume", dstVolID) | ||
|
||
return samePartition, 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