archiver: extract Readdirnames to fs package

This commit is contained in:
Michael Eischer
2024-06-29 17:15:29 +02:00
parent 83fdcf21fe
commit 1369658a32
4 changed files with 26 additions and 25 deletions

View File

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