Fix "invalid cross-device link" error when running on a different filesystem (#1769)

* fix cross-device rename errors

* fallback to copy instead of always using it

* use tmp dir in data folder

* recreate tmp dir at startup
This commit is contained in:
Lerk
2022-03-08 01:30:40 +01:00
committed by GitHub
parent 88ed026eae
commit a7687c861e
5 changed files with 51 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io"
"math/rand"
"net/url"
"os"
@ -60,9 +61,40 @@ func Copy(source, destination string) error {
return os.WriteFile(destination, input, 0600)
}
// Move moves the file to destination.
// Move moves the file at source to destination.
func Move(source, destination string) error {
return os.Rename(source, destination)
err := os.Rename(source, destination)
if err != nil {
log.Warnln("Moving with os.Rename failed, falling back to copy and delete!", err)
return moveFallback(source, destination)
}
return nil
}
// moveFallback moves a file using a copy followed by a delete, which works across file systems.
// source: https://gist.github.com/var23rav/23ae5d0d4d830aff886c3c970b8f6c6b
func moveFallback(source, destination string) error {
inputFile, err := os.Open(source)
if err != nil {
return fmt.Errorf("Couldn't open source file: %s", err)
}
outputFile, err := os.Create(destination)
if err != nil {
inputFile.Close()
return fmt.Errorf("Couldn't open dest file: %s", err)
}
defer outputFile.Close()
_, err = io.Copy(outputFile, inputFile)
inputFile.Close()
if err != nil {
return fmt.Errorf("Writing to output file failed: %s", err)
}
// The copy was successful, so now delete the original file
err = os.Remove(source)
if err != nil {
return fmt.Errorf("Failed removing original file: %s", err)
}
return nil
}
// IsUserAgentABot returns if a web client user-agent is seen as a bot.