From f30269865d2a34df811a0a2e51f42a2e574be141 Mon Sep 17 00:00:00 2001 From: Jakub Date: Mon, 29 Nov 2021 12:00:01 +0100 Subject: [PATCH] GODT-1381 Treat readonly folder as failure for cache on disk. --- internal/store/cache/disk.go | 7 +++++++ internal/users/cache.go | 17 +++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/internal/store/cache/disk.go b/internal/store/cache/disk.go index 307ae0f1..224eae2e 100644 --- a/internal/store/cache/disk.go +++ b/internal/store/cache/disk.go @@ -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()? diff --git a/internal/users/cache.go b/internal/users/cache.go index eb1e8a91..4be531b0 100644 --- a/internal/users/cache.go +++ b/internal/users/cache.go @@ -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 }