From 1369658a32a62053530c82df2deae3f733bcf7ce Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 29 Jun 2024 17:15:29 +0200 Subject: [PATCH] archiver: extract Readdirnames to fs package --- internal/archiver/archiver.go | 25 ++----------------------- internal/archiver/scanner.go | 2 +- internal/archiver/tree.go | 2 +- internal/fs/file.go | 22 ++++++++++++++++++++++ 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/internal/archiver/archiver.go b/internal/archiver/archiver.go index 9a31911b9..19ad12ab8 100644 --- a/internal/archiver/archiver.go +++ b/internal/archiver/archiver.go @@ -304,7 +304,7 @@ func (arch *Archiver) saveDir(ctx context.Context, snPath string, dir string, fi return FutureNode{}, err } - names, err := readdirnames(arch.FS, dir, fs.O_NOFOLLOW) + names, err := fs.Readdirnames(arch.FS, dir, fs.O_NOFOLLOW) if err != nil { return FutureNode{}, err } @@ -707,27 +707,6 @@ func (arch *Archiver) saveTree(ctx context.Context, snPath string, atree *Tree, return fn, len(nodes), nil } -// flags are passed to fs.OpenFile. O_RDONLY is implied. -func readdirnames(filesystem fs.FS, dir string, flags int) ([]string, error) { - f, err := filesystem.OpenFile(dir, fs.O_RDONLY|flags, 0) - if err != nil { - return nil, errors.WithStack(err) - } - - entries, err := f.Readdirnames(-1) - if err != nil { - _ = f.Close() - return nil, errors.Wrapf(err, "Readdirnames %v failed", dir) - } - - err = f.Close() - if err != nil { - return nil, err - } - - return entries, nil -} - // resolveRelativeTargets replaces targets that only contain relative // directories ("." or "../../") with the contents of the directory. Each // element of target is processed with fs.Clean(). @@ -743,7 +722,7 @@ func resolveRelativeTargets(filesys fs.FS, targets []string) ([]string, error) { } debug.Log("replacing %q with readdir(%q)", target, target) - entries, err := readdirnames(filesys, target, fs.O_NOFOLLOW) + entries, err := fs.Readdirnames(filesys, target, fs.O_NOFOLLOW) if err != nil { return nil, err } diff --git a/internal/archiver/scanner.go b/internal/archiver/scanner.go index cc419b19e..d61e5ce47 100644 --- a/internal/archiver/scanner.go +++ b/internal/archiver/scanner.go @@ -124,7 +124,7 @@ func (s *Scanner) scan(ctx context.Context, stats ScanStats, target string) (Sca stats.Files++ stats.Bytes += uint64(fi.Size()) case fi.Mode().IsDir(): - names, err := readdirnames(s.FS, target, fs.O_NOFOLLOW) + names, err := fs.Readdirnames(s.FS, target, fs.O_NOFOLLOW) if err != nil { return stats, s.Error(target, err) } diff --git a/internal/archiver/tree.go b/internal/archiver/tree.go index 16a78ee70..cd03ba521 100644 --- a/internal/archiver/tree.go +++ b/internal/archiver/tree.go @@ -233,7 +233,7 @@ func unrollTree(f fs.FS, t *Tree) error { // nodes, add the contents of Path to the nodes. if t.Path != "" && len(t.Nodes) > 0 { debug.Log("resolve path %v", t.Path) - entries, err := readdirnames(f, t.Path, 0) + entries, err := fs.Readdirnames(f, t.Path, 0) if err != nil { return err } diff --git a/internal/fs/file.go b/internal/fs/file.go index 4a236ea09..929195f1c 100644 --- a/internal/fs/file.go +++ b/internal/fs/file.go @@ -1,6 +1,7 @@ package fs import ( + "fmt" "os" "path/filepath" "time" @@ -138,3 +139,24 @@ func ResetPermissions(path string) error { } return nil } + +// Readdirnames returns a list of file in a directory. Flags are passed to fs.OpenFile. O_RDONLY is implied. +func Readdirnames(filesystem FS, dir string, flags int) ([]string, error) { + f, err := filesystem.OpenFile(dir, O_RDONLY|flags, 0) + if err != nil { + return nil, fmt.Errorf("openfile for readdirnames failed: %w", err) + } + + entries, err := f.Readdirnames(-1) + if err != nil { + _ = f.Close() + return nil, fmt.Errorf("readdirnames %v failed: %w", dir, err) + } + + err = f.Close() + if err != nil { + return nil, err + } + + return entries, nil +}