mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 04:36:43 +00:00
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/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -60,6 +61,12 @@ func NewOnDiskCache(path string, cmp Compressor, opts Options) (Cache, error) {
|
||||
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)
|
||||
|
||||
// NOTE(GODT-1158): use Available() or Free()?
|
||||
|
||||
@ -202,7 +202,7 @@ func (u *Users) DisableCache() error {
|
||||
func (u *Users) MigrateCache(srcPath, dstPath string) error {
|
||||
fiSrc, err := os.Stat(srcPath)
|
||||
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
|
||||
}
|
||||
if !fiSrc.IsDir() {
|
||||
@ -225,9 +225,17 @@ 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 {
|
||||
return nil
|
||||
defer os.Remove(tmp.Name()) //nolint[errcheck]
|
||||
|
||||
if err := os.Rename(srcPath, dstPath); err == 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
|
||||
@ -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.
|
||||
logrus.Info("Original cache folder could not be entirely removed")
|
||||
logrus.WithError(err).Warn("Original cache folder could not be entirely removed")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user