forked from Silverfish/proton-bridge
GODT-1381 Treat readonly folder as failure for cache on disk.
This commit is contained in:
7
internal/store/cache/disk.go
vendored
7
internal/store/cache/disk.go
vendored
@ -23,6 +23,7 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -60,6 +61,12 @@ func NewOnDiskCache(path string, cmp Compressor, opts Options) (Cache, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file, err := ioutil.TempFile(path, "tmp")
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("cannot write to target: %w", err)
|
||||||
|
}
|
||||||
|
os.Remove(file.Name()) //nolint[errcheck]
|
||||||
|
|
||||||
usage := du.NewDiskUsage(path)
|
usage := du.NewDiskUsage(path)
|
||||||
|
|
||||||
// NOTE(GODT-1158): use Available() or Free()?
|
// NOTE(GODT-1158): use Available() or Free()?
|
||||||
|
|||||||
@ -202,7 +202,7 @@ func (u *Users) DisableCache() error {
|
|||||||
func (u *Users) MigrateCache(srcPath, dstPath string) error {
|
func (u *Users) MigrateCache(srcPath, dstPath string) error {
|
||||||
fiSrc, err := os.Stat(srcPath)
|
fiSrc, err := os.Stat(srcPath)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
logrus.WithError(err).Warn("Skipping migration: Unknown source for cache migration")
|
logrus.WithError(err).Warn("Skipping migration: unknown source for cache migration")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if !fiSrc.IsDir() {
|
if !fiSrc.IsDir() {
|
||||||
@ -225,10 +225,18 @@ func (u *Users) MigrateCache(srcPath, dstPath string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.Rename(srcPath, dstPath)
|
// GODT-1381 Edge case: read-only source migration: prevent re-naming
|
||||||
|
// (read-only is conserved). Do copy instead.
|
||||||
|
tmp, err := ioutil.TempFile(srcPath, "tmp")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
defer os.Remove(tmp.Name()) //nolint[errcheck]
|
||||||
|
|
||||||
|
if err := os.Rename(srcPath, dstPath); err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
logrus.WithError(err).Warn("Cannot write to source: do copy to new destination instead of rename")
|
||||||
|
}
|
||||||
|
|
||||||
// Rename failed let's try an actual copy/delete
|
// Rename failed let's try an actual copy/delete
|
||||||
if err = copyFolder(srcPath, dstPath); err != nil {
|
if err = copyFolder(srcPath, dstPath); err != nil {
|
||||||
@ -236,7 +244,8 @@ func (u *Users) MigrateCache(srcPath, dstPath string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err = os.RemoveAll(srcPath); err != nil { // we don't care much about error there.
|
if err = os.RemoveAll(srcPath); err != nil { // we don't care much about error there.
|
||||||
logrus.Info("Original cache folder could not be entirely removed")
|
logrus.WithError(err).Warn("Original cache folder could not be entirely removed")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user