From 70fbad662391334c285bf7b8623610face9df7ee Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Tue, 27 Aug 2024 14:25:35 +0200 Subject: [PATCH] archiver: minimize imports --- cmd/restic/cmd_backup.go | 8 +++++++- internal/archiver/exclude.go | 10 ++-------- internal/archiver/exclude_test.go | 5 +---- internal/archiver/scanner.go | 3 +-- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/cmd/restic/cmd_backup.go b/cmd/restic/cmd_backup.go index eaca150d9..107e8bbe0 100644 --- a/cmd/restic/cmd_backup.go +++ b/cmd/restic/cmd_backup.go @@ -25,6 +25,7 @@ import ( "github.com/restic/restic/internal/repository" "github.com/restic/restic/internal/restic" "github.com/restic/restic/internal/textfile" + "github.com/restic/restic/internal/ui" "github.com/restic/restic/internal/ui/backup" "github.com/restic/restic/internal/ui/termstatus" ) @@ -333,7 +334,12 @@ func collectRejectFuncs(opts BackupOptions, targets []string, fs fs.FS) (funcs [ } if len(opts.ExcludeLargerThan) != 0 && !opts.Stdin && !opts.StdinCommand { - f, err := archiver.RejectBySize(opts.ExcludeLargerThan) + maxSize, err := ui.ParseBytes(opts.ExcludeLargerThan) + if err != nil { + return nil, err + } + + f, err := archiver.RejectBySize(maxSize) if err != nil { return nil, err } diff --git a/internal/archiver/exclude.go b/internal/archiver/exclude.go index 280322f3c..1e855fc3a 100644 --- a/internal/archiver/exclude.go +++ b/internal/archiver/exclude.go @@ -11,7 +11,6 @@ import ( "github.com/restic/restic/internal/debug" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/fs" - "github.com/restic/restic/internal/ui" ) // RejectByNameFunc is a function that takes a filename of a @@ -139,7 +138,7 @@ func isExcludedByFile(filename, tagFilename, header string, rc *rejectionCache, func isDirExcludedByFile(dir, tagFilename, header string, fs fs.FS, warnf func(msg string, args ...interface{})) bool { tf := fs.Join(dir, tagFilename) _, err := fs.Lstat(tf) - if os.IsNotExist(err) { + if errors.Is(err, os.ErrNotExist) { return false } if err != nil { @@ -315,12 +314,7 @@ func RejectByDevice(samples []string, filesystem fs.FS) (RejectFunc, error) { }, nil } -func RejectBySize(maxSizeStr string) (RejectFunc, error) { - maxSize, err := ui.ParseBytes(maxSizeStr) - if err != nil { - return nil, err - } - +func RejectBySize(maxSize int64) (RejectFunc, error) { return func(item string, fi os.FileInfo, _ fs.FS) bool { // directory will be ignored if fi.IsDir() { diff --git a/internal/archiver/exclude_test.go b/internal/archiver/exclude_test.go index b9f1f8cdd..7eb24b08b 100644 --- a/internal/archiver/exclude_test.go +++ b/internal/archiver/exclude_test.go @@ -139,9 +139,6 @@ func TestMultipleIsExcludedByFile(t *testing.T) { func TestIsExcludedByFileSize(t *testing.T) { tempDir := test.TempDir(t) - // Max size of file is set to be 1k - maxSizeStr := "1k" - // Create some files in a temporary directory. // Files in UPPERCASE will be used as exclusion triggers later on. // We will test the inclusion later, so we add the expected value as @@ -185,7 +182,7 @@ func TestIsExcludedByFileSize(t *testing.T) { test.OKs(t, errs) // see if anything went wrong during the creation // create rejection function - sizeExclude, _ := RejectBySize(maxSizeStr) + sizeExclude, _ := RejectBySize(1024) // To mock the archiver scanning walk, we create filepath.WalkFn // that tests against the two rejection functions and stores diff --git a/internal/archiver/scanner.go b/internal/archiver/scanner.go index cb74a31d6..debd09aa3 100644 --- a/internal/archiver/scanner.go +++ b/internal/archiver/scanner.go @@ -3,7 +3,6 @@ package archiver import ( "context" "os" - "path/filepath" "sort" "github.com/restic/restic/internal/debug" @@ -131,7 +130,7 @@ func (s *Scanner) scan(ctx context.Context, stats ScanStats, target string) (Sca sort.Strings(names) for _, name := range names { - stats, err = s.scan(ctx, stats, filepath.Join(target, name)) + stats, err = s.scan(ctx, stats, s.FS.Join(target, name)) if err != nil { return stats, err }