backend: consistently use os package for filesystem access

The go std library should be good enough to manage the files in the
backend and cache folders.
This commit is contained in:
Michael Eischer committed 2024-08-31 18:20:40 +02:00
1 parent ca1e5e10b6
commit 24f4e780f1
9 files changed
+33 -50

No files matched your search

+6 -7
View File
@@ -12,7 +12,6 @@ import (
"github.com/pkg/errors"
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/restic"
)
@@ -54,7 +53,7 @@ const cachedirTagSignature = "Signature: 8a477f597d28d172789f06886806bc55\n"
func writeCachedirTag(dir string) error {
tagfile := filepath.Join(dir, "CACHEDIR.TAG")
f, err := fs.OpenFile(tagfile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, fileMode)
f, err := os.OpenFile(tagfile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, fileMode)
if err != nil {
if errors.Is(err, os.ErrExist) {
return nil
@@ -85,7 +84,7 @@ func New(id string, basedir string) (c *Cache, err error) {
}
}
err = fs.MkdirAll(basedir, dirMode)
err = os.MkdirAll(basedir, dirMode)
if err != nil {
return nil, errors.WithStack(err)
}
@@ -113,7 +112,7 @@ func New(id string, basedir string) (c *Cache, err error) {
case errors.Is(err, os.ErrNotExist):
// Create the repo cache dir. The parent exists, so Mkdir suffices.
err := fs.Mkdir(cachedir, dirMode)
err := os.Mkdir(cachedir, dirMode)
switch {
case err == nil:
created = true
@@ -134,7 +133,7 @@ func New(id string, basedir string) (c *Cache, err error) {
}
for _, p := range cacheLayoutPaths {
if err = fs.MkdirAll(filepath.Join(cachedir, p), dirMode); err != nil {
if err = os.MkdirAll(filepath.Join(cachedir, p), dirMode); err != nil {
return nil, errors.WithStack(err)
}
}
@@ -152,7 +151,7 @@ func New(id string, basedir string) (c *Cache, err error) {
// directory d to the current time.
func updateTimestamp(d string) error {
t := time.Now()
return fs.Chtimes(d, t, t)
return os.Chtimes(d, t, t)
}
// MaxCacheAge is the default age (30 days) after which cache directories are considered old.
@@ -165,7 +164,7 @@ func validCacheDirName(s string) bool {
// listCacheDirs returns the list of cache directories.
func listCacheDirs(basedir string) ([]os.FileInfo, error) {
f, err := fs.Open(basedir)
f, err := os.Open(basedir)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err = nil
+10 -11
View File
@@ -12,7 +12,6 @@ import (
"github.com/restic/restic/internal/backend/util"
"github.com/restic/restic/internal/crypto"
"github.com/restic/restic/internal/debug"
"github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/restic"
)
@@ -44,7 +43,7 @@ func (c *Cache) load(h backend.Handle, length int, offset int64) (io.ReadCloser,
return nil, false, errors.New("cannot be cached")
}
f, err := fs.Open(c.filename(h))
f, err := os.Open(c.filename(h))
if err != nil {
return nil, false, errors.WithStack(err)
}
@@ -91,7 +90,7 @@ func (c *Cache) save(h backend.Handle, rd io.Reader) error {
finalname := c.filename(h)
dir := filepath.Dir(finalname)
err := fs.Mkdir(dir, 0700)
err := os.Mkdir(dir, 0700)
if err != nil && !errors.Is(err, os.ErrExist) {
return err
}
@@ -106,26 +105,26 @@ func (c *Cache) save(h backend.Handle, rd io.Reader) error {
n, err := io.Copy(f, rd)
if err != nil {
_ = f.Close()
_ = fs.Remove(f.Name())
_ = os.Remove(f.Name())
return errors.Wrap(err, "Copy")
}
if n <= int64(crypto.CiphertextLength(0)) {
_ = f.Close()
_ = fs.Remove(f.Name())
_ = os.Remove(f.Name())
debug.Log("trying to cache truncated file %v, removing", h)
return nil
}
// Close, then rename. Windows doesn't like the reverse order.
if err = f.Close(); err != nil {
_ = fs.Remove(f.Name())
_ = os.Remove(f.Name())
return errors.WithStack(err)
}
err = fs.Rename(f.Name(), finalname)
err = os.Rename(f.Name(), finalname)
if err != nil {
_ = fs.Remove(f.Name())
_ = os.Remove(f.Name())
}
if runtime.GOOS == "windows" && errors.Is(err, os.ErrPermission) {
// On Windows, renaming over an existing file is ok
@@ -162,7 +161,7 @@ func (c *Cache) remove(h backend.Handle) (bool, error) {
return false, nil
}
err := fs.Remove(c.filename(h))
err := os.Remove(c.filename(h))
removed := err == nil
if errors.Is(err, os.ErrNotExist) {
err = nil
@@ -189,7 +188,7 @@ func (c *Cache) Clear(t restic.FileType, valid restic.IDSet) error {
}
// ignore ErrNotExist to gracefully handle multiple processes running Clear() concurrently
if err = fs.Remove(c.filename(backend.Handle{Type: t, Name: id.String()})); err != nil && !errors.Is(err, os.ErrNotExist) {
if err = os.Remove(c.filename(backend.Handle{Type: t, Name: id.String()})); err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
}
@@ -236,6 +235,6 @@ func (c *Cache) Has(h backend.Handle) bool {
return false
}
_, err := fs.Stat(c.filename(h))
_, err := os.Stat(c.filename(h))
return err == nil
}
+1 -2
View File
@@ -12,7 +12,6 @@ import (
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/errors"
"github.com/restic/restic/internal/fs"
"github.com/restic/restic/internal/restic"
rtest "github.com/restic/restic/internal/test"
@@ -278,7 +277,7 @@ func TestFileSaveConcurrent(t *testing.T) {
func TestFileSaveAfterDamage(t *testing.T) {
c := TestNewCache(t)
rtest.OK(t, fs.RemoveAll(c.path))
rtest.OK(t, os.RemoveAll(c.path))
// save a few bytes of data in the cache
data := rtest.Random(123456789, 42)