Repository navigation
Expand file tree
/
Copy pathpath.go
More file actions
283 lines (246 loc) · 7.11 KB
/
Copy pathpath.go
File metadata and controls
283 lines (246 loc) · 7.11 KB
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
package gomodfs
import (
"fmt"
"maps"
"path"
"reflect"
"strings"
"sync"
"sync/atomic"
"github.com/tailscale/gomodfs/store"
"golang.org/x/mod/module"
)
// gmPath is the gomodfs-specific path structure
// for paths received from WebDAV Stat or OpenFile calls.
type gmPath struct {
// WellKnown, if non-empty, indicates that the path is
// for a well-known gomodfs path. The only possible values
// so far are ".gomodfs-status", for the status file,
// and "tsgo.extracted" (see wkTSGoExtracted).
WellKnown string
// NotExist indicates that the path does not exist.
// This is used for paths that we know operating systems
// are just checking for, but don't exist in the gomodfs hierarchy.
NotExist bool
// CacheDownloadFileExt is one of "mod", "ziphash", "info"
// if the path is for "cache/download/<module>/@v/<version>.<CacheDownloadFileExt>".
// If non-empty, then ModVersion is also populated.
CacheDownloadFileExt string
// InZip, if true, means that the path is within the contents of a zip
// file (i.e. not in "cache/download/" and deep enough in the filesystem
// to have hit an a directory with an "@" sign, such as
// "github.com/!azure/azure-sdk-for-go@v16.2.1+incompatible".
// When true, ModVersion and Path are also populated.
InZip bool
// Path is the path within the zip file, if InZip is true.
// For the root directory in the zip, Path is empty.
Path string
// ModVersion is the module version for the path, if yet known.
// It's populated if CacheDownloadFileExt is non-empty or
// InZip is true.
ModVersion store.ModuleVersion
}
// String returns a debug representation of the wdPath for tests.
func (p gmPath) String() string {
var sb strings.Builder
sb.WriteByte('{')
rv := reflect.ValueOf(p)
for _, st := range reflect.VisibleFields(rv.Type()) {
f := rv.FieldByIndex(st.Index)
if f.IsZero() {
continue
}
if sb.Len() > 1 {
sb.WriteString(", ")
}
fmt.Fprintf(&sb, "%s:%v", st.Name, f.Interface())
}
sb.WriteByte('}')
return sb.String()
}
// parsePath parses a path as received for a WebDAV Stat or OpenFile call
// and returns its godmodfs-specific structure.
func parsePath(name string) (ret gmPath) {
if name == statusFile {
ret.WellKnown = statusFile
return
}
base := path.Base(name)
// isDotFile is whether the base file is for a dotfile such as
// .DS_Store, .Spotlight-V100, ._., .hidden,
// .metadata_never_index_unless_rootfs, ..metadata_never_index, etc
// These may exist inside a zip, but they never exist elsewhere.
isDotFile := name != "" && strings.HasPrefix(base, ".")
if strings.HasPrefix(name, "tsgo-") {
if isDotFile {
// TODO(bradfitz): as far as I know, this is true (no useful dot
// files in the goroot), and this is what we did in earlier versions
// of gomodfs, so keep it for now.
ret.NotExist = true
return
}
return parseTSGoPath(name)
}
if dlSuffix, ok := strings.CutPrefix(name, "cache/download/"); ok {
if isDotFile {
ret.NotExist = true
return
}
escMod, escVer, ok := strings.Cut(dlSuffix, "/@v/")
if !ok {
// Not enough.
return
}
ext := path.Ext(escVer)
if ext == "" {
return
}
switch ext {
case ".mod", ".ziphash", ".info":
ret.CacheDownloadFileExt = ext[1:]
default:
// Not a recognized cache/download file.
ret.NotExist = true
return
}
escVer = strings.TrimSuffix(escVer, ext)
ret.ModVersion, ok = parseModVersion(escMod, escVer)
if !ok {
ret.NotExist = true
}
return
}
// Zip path.
escMod, verAndPath, ok := strings.Cut(name, "@")
if !ok {
if isDotFile {
ret.NotExist = true
return
}
return
}
escVer, path, _ := strings.Cut(verAndPath, "/")
ret.ModVersion, ok = parseModVersion(escMod, escVer)
if ok {
ret.Path = strings.TrimSuffix(path, "/")
ret.InZip = true
} else {
ret.NotExist = true
}
return
}
func isHexChar(r rune) bool {
return r >= '0' && r <= '9' || r >= 'a' && r <= 'f' || r >= 'A' && r <= 'F'
}
func parseTSGoPath(name string) (ret gmPath) {
name, ok := strings.CutPrefix(name, "tsgo-")
if !ok {
return
}
osHyphenArch, rest, ok := strings.Cut(name, "/")
if !ok {
return
}
goos, goarch, ok := strings.Cut(osHyphenArch, "-")
if !ok {
return
}
if !validTSGoOSARCH(goos, goarch) {
ret.NotExist = true
return
}
firstSeg, rest, _ := strings.Cut(rest, "/")
hash, wantExtractedFile := strings.CutSuffix(firstSeg, ".extracted")
if len(hash) != 40 {
ret.NotExist = true
}
for _, b := range hash {
if !isHexChar(b) {
ret.NotExist = true
return
}
}
ret.ModVersion.Module = "github.com/tailscale/go"
ret.ModVersion.Version = tsGoVersion(tsgoTriple{OS: goos, Arch: goarch, Hash: hash})
if wantExtractedFile {
ret.WellKnown = "tsgo.extracted"
return
}
ret.Path = strings.TrimSuffix(rest, "/")
ret.InZip = true
return ret
}
type tsgoTriple struct {
OS string
Arch string
Hash string
}
var tsGoVersion = funcSmallSet(func(triple tsgoTriple) string {
return fmt.Sprintf("tsgo-%s-%s-%s", triple.OS, triple.Arch, triple.Hash)
})
var tsGoZipRoot = funcSmallSet(func(triple tsgoTriple) string {
return fmt.Sprintf("tsgo-%s-%s/%s", triple.OS, triple.Arch, triple.Hash)
})
type escPair struct {
EscMod string
EscVer string
}
// seePairs memoizes [parseModVersion].
// It grows unbounded, which in practice is fine.
var seenPairs sync.Map // escPair -> store.ModuleVersion
func parseModVersion(escMod, escVer string) (mv store.ModuleVersion, ok bool) {
pair := escPair{EscMod: escMod, EscVer: escVer}
if v, ok := seenPairs.Load(pair); ok {
return v.(store.ModuleVersion), true
}
var err error
mv.Module, err = module.UnescapePath(escMod)
if err != nil {
return mv, false
}
mv.Version, err = module.UnescapeVersion(escVer)
if err != nil {
return mv, false
}
seenPairs.Store(pair, mv)
return mv, true
}
// funcSmallSet returns a memoized version of f.
//
// The implementation assumption is that the the set of possible K values is
// small. The implementation re-clones the prior map of all previous keys
// and values whenever a new key is seen.
//
// f should be a pure function (free of side effects and deterministic).
func funcSmallSet[K comparable, V any](f func(K) V) func(K) V {
var latestMap atomic.Value
var mu sync.Mutex
// TODO(bradfitz): if Go 1.24's internal/sync.HashTrieMap is ever exported,
// perhaps via a new generic version of sync.Map (Go 1.24's sync.Map is just
// the any/any instantiation of internal/sync.HashTrieMap), then we should
// use that here instead. But tailscale.com's syncs.Map (mutex + normal map) and
// std sync.Map are both sad for concurrency + boxing-into-interface reasons,
// respectively. So do a lame non-scalable version (that only works for small sets
// of items) instead, naming the func appropriately to scare off users who might
// use it for tons of items)
return func(k K) V {
latest, _ := latestMap.Load().(map[K]V)
if v, ok := latest[k]; ok {
return v // common case
}
mu.Lock()
defer mu.Unlock()
latest, _ = latestMap.Load().(map[K]V)
if v, ok := latest[k]; ok {
return v // lost fill race
}
v := f(k)
clone := maps.Clone(latest)
if clone == nil {
clone = make(map[K]V)
}
clone[k] = v
latestMap.Store(clone)
return v
}
}