Fix: Handle rename across volumes

This commit is contained in:
Neeraj Gupta 2023-10-18 13:55:29 +05:30
parent fd7aac9b44
commit 3e29d17625
2 changed files with 43 additions and 3 deletions

View file

@ -5,7 +5,9 @@ import (
"cli-go/pkg/model/export"
"encoding/json"
"errors"
"io"
"os"
"strings"
)
const (
@ -96,3 +98,41 @@ func readJSONFromFile(filePath string, data interface{}) error {
decoder := json.NewDecoder(file)
return decoder.Decode(data)
}
func Move(source, destination string) error {
err := os.Rename(source, destination)
if err != nil && strings.Contains(err.Error(), "cross-device link") {
return moveCrossDevice(source, destination)
}
return err
}
func moveCrossDevice(source, destination string) error {
src, err := os.Open(source)
if err != nil {
return err
}
dst, err := os.Create(destination)
if err != nil {
src.Close()
return err
}
_, err = io.Copy(dst, src)
src.Close()
dst.Close()
if err != nil {
return err
}
fi, err := os.Stat(source)
if err != nil {
os.Remove(destination)
return err
}
err = os.Chmod(destination, fi.Mode())
if err != nil {
os.Remove(destination)
return err
}
os.Remove(source)
return nil
}

View file

@ -145,11 +145,11 @@ func (c *ClICtrl) downloadEntry(ctx context.Context,
imageFilePath := filepath.Join(diskInfo.ExportRoot, diskInfo.AlbumMeta.FolderName, imageFileName)
videoFilePath := filepath.Join(diskInfo.ExportRoot, diskInfo.AlbumMeta.FolderName, videoFileName)
// move the decrypt file to filePath
err = os.Rename(imagePath, imageFilePath)
err = Move(imagePath, imageFilePath)
if err != nil {
return err
}
err = os.Rename(videoPath, videoFilePath)
err = Move(videoPath, videoFilePath)
if err != nil {
return err
}
@ -159,7 +159,7 @@ func (c *ClICtrl) downloadEntry(ctx context.Context,
fileName := baseFileName + extension
filePath := filepath.Join(diskInfo.ExportRoot, diskInfo.AlbumMeta.FolderName, fileName)
// move the decrypt file to filePath
err = os.Rename(*decrypt, filePath)
err = Move(*decrypt, filePath)
if err != nil {
return err
}