diff options
Diffstat (limited to 'vendor/git.iim.gay/grufwub/go-store/util')
-rw-r--r-- | vendor/git.iim.gay/grufwub/go-store/util/fs.go | 105 | ||||
-rw-r--r-- | vendor/git.iim.gay/grufwub/go-store/util/io.go | 42 | ||||
-rw-r--r-- | vendor/git.iim.gay/grufwub/go-store/util/nocopy.go | 6 | ||||
-rw-r--r-- | vendor/git.iim.gay/grufwub/go-store/util/pools.go | 44 |
4 files changed, 197 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 + } +} diff --git a/vendor/git.iim.gay/grufwub/go-store/util/io.go b/vendor/git.iim.gay/grufwub/go-store/util/io.go new file mode 100644 index 000000000..d034cf62b --- /dev/null +++ b/vendor/git.iim.gay/grufwub/go-store/util/io.go @@ -0,0 +1,42 @@ +package util + +import "io" + +// NopReadCloser turns a supplied io.Reader into io.ReadCloser with a nop Close() implementation +func NopReadCloser(r io.Reader) io.ReadCloser { + return &nopReadCloser{r} +} + +// NopWriteCloser turns a supplied io.Writer into io.WriteCloser with a nop Close() implementation +func NopWriteCloser(w io.Writer) io.WriteCloser { + return &nopWriteCloser{w} +} + +// ReadCloserWithCallback adds a customizable callback to be called upon Close() of a supplied io.ReadCloser +func ReadCloserWithCallback(rc io.ReadCloser, cb func()) io.ReadCloser { + return &callbackReadCloser{ + ReadCloser: rc, + callback: cb, + } +} + +// nopReadCloser turns an io.Reader -> io.ReadCloser with a nop Close() +type nopReadCloser struct{ io.Reader } + +func (r *nopReadCloser) Close() error { return nil } + +// nopWriteCloser turns an io.Writer -> io.WriteCloser with a nop Close() +type nopWriteCloser struct{ io.Writer } + +func (w nopWriteCloser) Close() error { return nil } + +// callbackReadCloser allows adding our own custom callback to an io.ReadCloser +type callbackReadCloser struct { + io.ReadCloser + callback func() +} + +func (c *callbackReadCloser) Close() error { + defer c.callback() + return c.ReadCloser.Close() +} diff --git a/vendor/git.iim.gay/grufwub/go-store/util/nocopy.go b/vendor/git.iim.gay/grufwub/go-store/util/nocopy.go new file mode 100644 index 000000000..e4dd071aa --- /dev/null +++ b/vendor/git.iim.gay/grufwub/go-store/util/nocopy.go @@ -0,0 +1,6 @@ +package util + +type NoCopy struct{} + +func (*NoCopy) Lock() {} +func (*NoCopy) Unlock() {} diff --git a/vendor/git.iim.gay/grufwub/go-store/util/pools.go b/vendor/git.iim.gay/grufwub/go-store/util/pools.go new file mode 100644 index 000000000..c02f2d25e --- /dev/null +++ b/vendor/git.iim.gay/grufwub/go-store/util/pools.go @@ -0,0 +1,44 @@ +package util + +import ( + "sync" + + "git.iim.gay/grufwub/fastpath" + "git.iim.gay/grufwub/go-bufpool" + "git.iim.gay/grufwub/go-bytes" +) + +// pathBuilderPool is the global fastpath.Builder pool, we implement +// our own here instead of using fastpath's default one because we +// don't want to deal with fastpath's sync.Once locks on every Acquire/Release +var pathBuilderPool = sync.Pool{ + New: func() interface{} { + pb := fastpath.NewBuilder(make([]byte, 0, 512)) + return &pb + }, +} + +// AcquirePathBuilder returns a reset fastpath.Builder instance +func AcquirePathBuilder() *fastpath.Builder { + return pathBuilderPool.Get().(*fastpath.Builder) +} + +// ReleasePathBuilder resets and releases provided fastpath.Builder instance to global pool +func ReleasePathBuilder(pb *fastpath.Builder) { + pb.Reset() + pathBuilderPool.Put(pb) +} + +// bufferPool is the global BufferPool, we implement this here +// so we can share allocations across whatever libaries need them. +var bufferPool = bufpool.BufferPool{} + +// AcquireBuffer returns a reset bytes.Buffer with at least requested capacity +func AcquireBuffer(cap int) *bytes.Buffer { + return bufferPool.Get(cap) +} + +// ReleaseBuffer resets and releases provided bytes.Buffer to global BufferPool +func ReleaseBuffer(buf *bytes.Buffer) { + bufferPool.Put(buf) +} |