-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpath.go
388 lines (352 loc) · 11 KB
/
path.go
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright 2024 The zb Authors
// SPDX-License-Identifier: MIT
package zbstore
import (
"crypto/sha256"
"fmt"
"io"
"os"
posixpath "path"
"path/filepath"
"runtime"
"strings"
"zb.256lights.llc/pkg/internal/storepath"
"zb.256lights.llc/pkg/internal/windowspath"
"zb.256lights.llc/pkg/sets"
"zombiezen.com/go/nix"
"zombiezen.com/go/nix/nixbase32"
)
// Directory is the absolute path of a zb store.
type Directory string
const (
// DefaultUnixDirectory is the default zb store directory
// on Unix-like systems.
DefaultUnixDirectory Directory = "/zb/store"
// DefaultWindowsDirectory is the default zb store directory
// on Windows systems.
DefaultWindowsDirectory Directory = `C:\zb\store`
)
// DefaultDirectory returns the default zb store directory for the running operating system.
// This will be one of [DefaultUnixDirectory] or [DefaultWindowsDirectory].
func DefaultDirectory() Directory {
switch localPathStyle() {
case posixPathStyle:
return DefaultUnixDirectory
case windowsPathStyle:
return DefaultWindowsDirectory
default:
panic("unreachable")
}
}
// CleanDirectory cleans an absolute POSIX-style or Windows-style path
// as a [Directory].
// It returns an error if the path is not absolute.
func CleanDirectory(path string) (Directory, error) {
switch detectPathStyle(path) {
case posixPathStyle:
if !posixpath.IsAbs(path) {
return "", fmt.Errorf("store directory %q is not absolute", path)
}
return Directory(posixpath.Clean(path)), nil
case windowsPathStyle:
if !windowspath.IsAbs(path) {
return "", fmt.Errorf("store directory %q is not absolute", path)
}
return Directory(windowspath.Clean(path)), nil
default:
return "", fmt.Errorf("store directory %q is not absolute", path)
}
}
// DirectoryFromEnvironment returns the zb store [Directory] in use
// based on the ZB_STORE_DIR environment variable,
// falling back to [DefaultDirectory] if not set.
func DirectoryFromEnvironment() (Directory, error) {
dir := os.Getenv("ZB_STORE_DIR")
if dir == "" {
return DefaultDirectory(), nil
}
if !filepath.IsAbs(dir) {
// The directory must be in the format of the local OS.
return "", fmt.Errorf("store directory %q is not absolute", dir)
}
return CleanDirectory(dir)
}
// Object returns the store path for the given store object name.
func (dir Directory) Object(name string) (Path, error) {
joined := dir.Join(name)
if name == "" || name == "." || name == ".." || strings.ContainsAny(name, `/\`) {
return "", fmt.Errorf("parse zb store path %s: invalid object name %q", joined, name)
}
storePath, err := ParsePath(joined)
if err != nil {
return "", err
}
return storePath, nil
}
// Join joins any number of path elements to the store directory
// separated by the store directory's separator type.
func (dir Directory) Join(elem ...string) string {
switch detectPathStyle(string(dir)) {
case windowsPathStyle:
return windowspath.Join(append([]string{string(dir)}, elem...)...)
default:
return posixpath.Join(append([]string{string(dir)}, elem...)...)
}
}
// ParsePath verifies that a given absolute path
// begins with the store directory
// and names either a store object or a file inside a store object.
// On success, it returns the store object's name
// and the slash-separated relative path inside the store object, if any.
func (dir Directory) ParsePath(path string) (storePath Path, sub string, err error) {
var cleaned, dirPrefix, tail string
var sep rune
style := detectPathStyle(string(dir))
switch style {
case posixPathStyle:
if !posixpath.IsAbs(path) {
return "", "", fmt.Errorf("parse zb store path %s: not absolute", path)
}
sep = '/'
cleaned = posixpath.Clean(path)
dirPrefix = posixpath.Clean(string(dir)) + string(sep)
var ok bool
tail, ok = strings.CutPrefix(cleaned, dirPrefix)
if !ok {
return "", "", fmt.Errorf("parse zb store path %s: outside %s", path, dir)
}
case windowsPathStyle:
if !windowspath.IsAbs(path) {
return "", "", fmt.Errorf("parse zb store path %s: not absolute", path)
}
sep = windowspath.Separator
cleaned = windowspath.Clean(path)
dirPrefix = windowspath.Clean(string(dir)) + string(sep)
var ok bool
tail, ok = strings.CutPrefix(cleaned, dirPrefix)
if !ok {
return "", "", fmt.Errorf("parse zb store path %s: outside %s", path, dir)
}
default:
return "", "", fmt.Errorf("parse zb store path %s: directory %s not absolute", path, dir)
}
childName, sub, _ := strings.Cut(tail, string(sep))
storePath, err = ParsePath(cleaned[:len(dirPrefix)+len(childName)])
if err != nil {
return "", "", err
}
if style == windowsPathStyle {
sub = windowspath.ToSlash(sub)
}
return storePath, sub, nil
}
// IsNative reports whether the directory uses the same path style
// as the running operating system.
func (dir Directory) IsNative() bool {
return detectPathStyle(string(dir)) == localPathStyle()
}
// Path is a zb store path:
// the absolute path of a zb store object in the filesystem.
// For example: "/zb/store/s66mzxpvicwk07gjbjfw9izjfa797vsw-hello-2.12.1"
// or "C:\zb\store\s66mzxpvicwk07gjbjfw9izjfa797vsw-hello-2.12.1".
type Path string
const (
objectNameDigestLength = 32
maxObjectNameLength = objectNameDigestLength + 1 + 211
)
// ParsePath parses an absolute path as a store path
// (i.e. an immediate child of a zb store directory).
func ParsePath(path string) (Path, error) {
var cleaned, base string
switch detectPathStyle(path) {
case posixPathStyle:
cleaned = posixpath.Clean(path)
_, base = posixpath.Split(cleaned)
case windowsPathStyle:
cleaned = windowspath.Clean(path)
_, base = windowspath.Split(cleaned)
default:
return "", fmt.Errorf("parse zb store path %s: not absolute", path)
}
if len(base) < objectNameDigestLength+len("-")+1 {
return "", fmt.Errorf("parse zb store path %s: %q is too short", path, base)
}
if len(base) > maxObjectNameLength {
return "", fmt.Errorf("parse zb store path %s: %q is too long", path, base)
}
for i := 0; i < len(base); i++ {
if !isNameChar(base[i]) {
return "", fmt.Errorf("parse zb store path %s: %q contains illegal character %q", path, base, base[i])
}
}
if err := nixbase32.ValidateString(base[:objectNameDigestLength]); err != nil {
return "", fmt.Errorf("parse zb store path %s: %v", path, err)
}
if base[objectNameDigestLength] != '-' {
return "", fmt.Errorf("parse zb store path %s: digest not separated by dash", path)
}
return Path(cleaned), nil
}
// Dir returns the path's directory.
func (path Path) Dir() Directory {
switch detectPathStyle(string(path)) {
case posixPathStyle:
return Directory(posixpath.Dir(string(path)))
case windowsPathStyle:
return Directory(windowspath.Dir(string(path)))
default:
return ""
}
}
// Base returns the last element of the path.
func (path Path) Base() string {
if path == "" {
return ""
}
switch detectPathStyle(string(path)) {
case posixPathStyle:
return posixpath.Base(string(path))
case windowsPathStyle:
return windowspath.Base(string(path))
default:
return ""
}
}
// IsDerivation reports whether the name ends in [DerivationExt].
func (path Path) IsDerivation() bool {
_, isDrv := path.DerivationName()
return isDrv
}
// Digest returns the digest part of the last element of the path.
func (path Path) Digest() string {
base := path.Base()
if len(base) < objectNameDigestLength {
return ""
}
return string(base[:objectNameDigestLength])
}
// Name returns the part of the last element of the path after the digest,
// excluding the separating dash.
func (path Path) Name() string {
base := path.Base()
if len(base) <= objectNameDigestLength+len("-") {
return ""
}
return string(base[objectNameDigestLength+len("-"):])
}
// DerivationName returns [Path.Name] with a suffix of [DerivationExt] stripped.
// If the path does not end in [DerivationExt],
// DerivationName returns ("", false).
func (path Path) DerivationName() (drvName string, isDrv bool) {
drvName, isDrv = strings.CutSuffix(path.Name(), DerivationExt)
if !isDrv {
return "", false
}
return drvName, true
}
// Join joins any number of path elements to the store path
// separated by the store path's separator type.
func (path Path) Join(elem ...string) string {
elem = append([]string{path.Base()}, elem...)
return path.Dir().Join(elem...)
}
// IsNative reports whether the path uses the same path style
// as the running operating system.
func (path Path) IsNative() bool {
return detectPathStyle(string(path)) == localPathStyle()
}
// MarshalText returns a byte slice of the path
// or an error if it's empty.
func (path Path) MarshalText() ([]byte, error) {
if path == "" {
return nil, fmt.Errorf("marshal zb store path: empty")
}
return []byte(path), nil
}
// UnmarshalText validates and cleans the path in the same way as [ParsePath]
// and stores it into *path.
func (path *Path) UnmarshalText(data []byte) error {
var err error
*path, err = ParsePath(string(data))
if err != nil {
return err
}
return nil
}
// makeStorePath computes a store path
// according to https://nixos.org/manual/nix/stable/protocols/store-path.
func makeStorePath(dir Directory, typ string, hash nix.Hash, name string, refs References) (Path, error) {
h := sha256.New()
io.WriteString(h, typ)
for _, ref := range refs.Others.All() {
io.WriteString(h, ":")
io.WriteString(h, string(ref))
}
if refs.Self {
io.WriteString(h, ":self")
}
digest := storepath.MakeDigest(h, string(dir), hash, name)
return dir.Object(digest + "-" + name)
}
// References represents a set of references to other store paths
// that a store object contains for the purpose of generating a [Path].
// The zero value is an empty set.
type References struct {
// Self is true if the store object contains one or more references to itself.
Self bool
// Others holds paths of other store objects that the store object references.
Others sets.Sorted[Path]
}
// MakeReferences converts a set of complete store paths into a [References] value.
func MakeReferences(self Path, refSet *sets.Sorted[Path]) References {
refs := References{
Self: refSet.Has(self),
Others: *refSet.Clone(),
}
if refs.Self {
refs.Others.Delete(self)
}
return refs
}
// IsEmpty reports whether refs represents the empty set.
func (refs References) IsEmpty() bool {
return !refs.Self && refs.Others.Len() == 0
}
// ToSet converts the references to a set of paths
// given the store object's own path.
func (refs References) ToSet(self Path) *sets.Sorted[Path] {
result := refs.Others.Clone()
if refs.Self {
result.Add(self)
}
return result
}
type pathStyle int8
const (
posixPathStyle pathStyle = 1 + iota
windowsPathStyle
)
func localPathStyle() pathStyle {
if runtime.GOOS == "windows" {
return windowsPathStyle
}
return posixPathStyle
}
// detectPathStyle returns the OS that the given absolute path uses,
// or zero if the path is not absolute.
func detectPathStyle(path string) pathStyle {
switch {
case posixpath.IsAbs(path):
return posixPathStyle
case windowspath.IsAbs(path):
return windowsPathStyle
default:
return 0
}
}
func isNameChar(c byte) bool {
return 'a' <= c && c <= 'z' ||
'A' <= c && c <= 'Z' ||
'0' <= c && c <= '9' ||
c == '+' || c == '-' || c == '.' || c == '_' || c == '='
}