diff options
author | 2021-09-12 10:10:24 +0100 | |
---|---|---|
committer | 2021-09-12 10:10:24 +0100 | |
commit | f6492d12d948507021bbe934de94e87e20464c01 (patch) | |
tree | 6705d6ef6f3c4d70f3b3ebc77c2960d8e508cf37 /vendor/git.iim.gay/grufwub/go-store/util/fs.go | |
parent | Merge pull request #213 from superseriousbusiness/alpine+node_upstep (diff) | |
parent | fix keys used to access storage items (diff) | |
download | gotosocial-f6492d12d948507021bbe934de94e87e20464c01.tar.xz |
Merge pull request #214 from NyaaaWhatsUpDoc/improvement/update-storage-library
add git.iim.gay/grufwub/go-store for storage backend, replacing blob.Storage
Diffstat (limited to 'vendor/git.iim.gay/grufwub/go-store/util/fs.go')
-rw-r--r-- | vendor/git.iim.gay/grufwub/go-store/util/fs.go | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/vendor/git.iim.gay/grufwub/go-store/util/fs.go b/vendor/git.iim.gay/grufwub/go-store/util/fs.go new file mode 100644 index 000000000..20c0ab116 --- /dev/null +++ b/vendor/git.iim.gay/grufwub/go-store/util/fs.go @@ -0,0 +1,105 @@ +package util + +import ( + "io/fs" + "os" + "strings" + "syscall" + + "git.iim.gay/grufwub/fastpath" +) + +var dotdot = "../" + +// CountDotdots returns the number of "dot-dots" (../) in a cleaned filesystem path +func CountDotdots(path string) int { + if !strings.HasSuffix(path, dotdot) { + return 0 + } + return strings.Count(path, dotdot) +} + +// WalkDir traverses the dir tree of the supplied path, performing the supplied walkFn on each entry +func WalkDir(pb *fastpath.Builder, path string, walkFn func(string, fs.DirEntry)) error { + // Read supplied dir path + dirEntries, err := os.ReadDir(path) + if err != nil { + return err + } + + // Iter entries + for _, entry := range dirEntries { + // Pass to walk fn + walkFn(path, entry) + + // Recurse dir entries + if entry.IsDir() { + err = WalkDir(pb, pb.Join(path, entry.Name()), walkFn) + if err != nil { + return err + } + } + } + + return nil +} + +// CleanDirs traverses the dir tree of the supplied path, removing any folders with zero children +func CleanDirs(path string) error { + // Acquire builder + pb := AcquirePathBuilder() + defer ReleasePathBuilder(pb) + + // Get dir entries + entries, err := os.ReadDir(path) + if err != nil { + return err + } + + // Recurse dirs + for _, entry := range entries { + if entry.IsDir() { + err := cleanDirs(pb, pb.Join(path, entry.Name())) + if err != nil { + return err + } + } + } + return nil +} + +// cleanDirs performs the actual dir cleaning logic for the exported version +func cleanDirs(pb *fastpath.Builder, path string) error { + // Get dir entries + entries, err := os.ReadDir(path) + if err != nil { + return err + } + + // If no entries, delete + if len(entries) < 1 { + return os.Remove(path) + } + + // Recurse dirs + for _, entry := range entries { + if entry.IsDir() { + err := cleanDirs(pb, pb.Join(path, entry.Name())) + if err != nil { + return err + } + } + } + return nil +} + +// RetryOnEINTR is a low-level filesystem function for retrying syscalls on O_EINTR received +func RetryOnEINTR(do func() error) error { + for { + err := do() + if err == syscall.EINTR { + continue + } + return err + } +} |