summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-storage/disk/fs.go
blob: 606d8fb0faf127b3b395681be448166ba686d3aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package disk

import (
	"errors"
	"fmt"
	"io/fs"
	"os"
	"syscall"

	"codeberg.org/gruf/go-fastpath/v2"
	"codeberg.org/gruf/go-storage/internal"
)

// NOTE:
// These functions are for opening storage files,
// not necessarily for e.g. initial setup (OpenFile)

// walkDir traverses the dir tree of the supplied path, performing the supplied walkFn on each entry
func walkDir(pb *fastpath.Builder, path string, args OpenArgs, walkFn func(string, fs.DirEntry) error) error {
	// Read directory entries at path.
	entries, err := readDir(path, args)
	if err != nil {
		return err
	}

	// frame represents a directory entry
	// walk-loop snapshot, taken when a sub
	// directory requiring iteration is found
	type frame struct {
		path    string
		entries []fs.DirEntry
	}

	// stack contains a list of held snapshot
	// frames, representing unfinished upper
	// layers of a directory structure yet to
	// be traversed.
	var stack []frame

outer:
	for {
		if len(entries) == 0 {
			if len(stack) == 0 {
				// Reached end
				break outer
			}

			// Pop frame from stack
			frame := stack[len(stack)-1]
			stack = stack[:len(stack)-1]

			// Update loop vars
			entries = frame.entries
			path = frame.path
		}

		for len(entries) > 0 {
			// Pop next entry from queue
			entry := entries[0]
			entries = entries[1:]

			// Pass to provided walk function
			if err := walkFn(path, entry); err != nil {
				return err
			}

			if entry.IsDir() {
				// Push current frame to stack
				stack = append(stack, frame{
					path:    path,
					entries: entries,
				})

				// Update current directory path
				path = pb.Join(path, entry.Name())

				// Read next directory entries
				next, err := readDir(path, args)
				if err != nil {
					return err
				}

				// Set next entries
				entries = next

				continue outer
			}
		}
	}

	return nil
}

// cleanDirs traverses the dir tree of the supplied path, removing any folders with zero children
func cleanDirs(path string, args OpenArgs) error {
	pb := internal.GetPathBuilder()
	err := cleanDir(pb, path, args, true)
	internal.PutPathBuilder(pb)
	return err
}

// cleanDir performs the actual dir cleaning logic for the above top-level version.
func cleanDir(pb *fastpath.Builder, path string, args OpenArgs, top bool) error {
	// Get directory entries at path.
	entries, err := readDir(path, args)
	if err != nil {
		return err
	}

	// If no entries, delete dir.
	if !top && len(entries) == 0 {
		return rmdir(path)
	}

	var errs []error

	// Iterate all directory entries.
	for _, entry := range entries {

		if entry.IsDir() {
			// Calculate directory path.
			dir := pb.Join(path, entry.Name())

			// Recursively clean sub-directory entries, adding errs.
			if err := cleanDir(pb, dir, args, false); err != nil {
				err = fmt.Errorf("error(s) cleaning subdir %s: %w", dir, err)
				errs = append(errs, err)
			}
		}
	}

	// Return combined errors.
	return errors.Join(errs...)
}

// readDir will open file at path, read the unsorted list of entries, then close.
func readDir(path string, args OpenArgs) ([]fs.DirEntry, error) {
	// Open directory at path.
	file, err := open(path, args)
	if err != nil {
		return nil, err
	}

	// Read ALL directory entries.
	entries, err := file.ReadDir(-1)

	// Done with file
	_ = file.Close()

	return entries, err
}

// open is a simple wrapper around syscall.Open().
func open(path string, args OpenArgs) (*os.File, error) {
	var fd int
	err := retryOnEINTR(func() (err error) {
		fd, err = syscall.Open(path, args.Flags, args.Perms)
		return
	})
	if err != nil {
		return nil, err
	}
	return os.NewFile(uintptr(fd), path), nil
}

// stat is a simple wrapper around syscall.Stat().
func stat(path string) (*syscall.Stat_t, error) {
	var stat syscall.Stat_t
	err := retryOnEINTR(func() error {
		return syscall.Stat(path, &stat)
	})
	if err != nil {
		if err == syscall.ENOENT {
			// not-found is no error
			err = nil
		}
		return nil, err
	}
	return &stat, nil
}

// unlink is a simple wrapper around syscall.Unlink().
func unlink(path string) error {
	return retryOnEINTR(func() error {
		return syscall.Unlink(path)
	})
}

// rmdir is a simple wrapper around syscall.Rmdir().
func rmdir(path string) error {
	return retryOnEINTR(func() error {
		return syscall.Rmdir(path)
	})
}

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