GODT-1381 Treat readonly folder as failure for cache on disk.

This commit is contained in:
Jakub
2021-11-29 12:00:01 +01:00
parent d7c5ace8e4
commit f30269865d
2 changed files with 20 additions and 4 deletions

View File

@ -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()?

View File

@ -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
}