GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — locks/config.go at master · asciimoth/locks

Expand file tree

/

config.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
239 lines (232 loc) · 6.17 KB
/

config.go

Copy path

File metadata and controls

239 lines (232 loc) · 6.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package locks
import (
"fmt"
"net/url"
"path/filepath"
"runtime"
"strings"
)
// Location defines independent read and write policies for a store.
type Location struct {
Address string
CheckOnly, ReadOnly bool
}
// Endpoint constructs the v1 endpoint while preserving encoded paths and query bytes.
func Endpoint(base string) (string, error) {
u, e := url.Parse(base)
if e != nil || u == nil {
return "", ErrFormat
}
u.Scheme = strings.ToLower(u.Scheme)
if (u.Scheme != "http" && u.Scheme != "https") || u.Hostname() == "" || u.User != nil || u.Opaque != "" {
return "", ErrFormat
}
keys, e := queryKeys(u.RawQuery)
if e != nil || len(keys) > 0 {
return "", ErrFormat
}
p := strings.TrimRight(u.EscapedPath(), "/") + "/lock"
// EscapedPath returns a valid encoding, including when RawPath is invalid.
u.Path, _ = url.PathUnescape(p)
u.RawPath = p
u.Fragment = ""
u.RawFragment = ""
return u.String(), nil
}
// queryKeys decodes once and treats '+' as a literal. Unknown query values are ignored.
func queryKeys(query string) ([]string, error) {
var keys []string
for _, f := range strings.Split(query, "&") {
k, v, _ := strings.Cut(f, "=")
name, e := url.PathUnescape(k)
if e != nil {
return nil, ErrFormat
}
if name == "key" {
s, e := url.PathUnescape(v)
if e != nil {
return nil, ErrFormat
}
keys = append(keys, s)
}
}
return keys, nil
}
// ParsePath parses LOCK_PATH without expansion or whitespace removal. Empty entries are ignored.
func ParsePath(s string) ([]Location, error) { return ParsePathForOS(s, runtime.GOOS) }
// ParsePathForOS permits deterministic parsing of another platform's native paths.
func ParsePathForOS(s, goos string) ([]Location, error) {
var result []Location
seen := map[Location]bool{}
for len(s) > 0 {
loc := Location{}
// A prefix precedes a location. An '=' inside an absolute location is data.
if s[0] != '"' && !locationStart(s, goos) {
flags, rest, ok := strings.Cut(s, "=")
if !ok {
return nil, ErrFormat
}
s = rest
for _, flag := range strings.Split(flags, ",") {
switch flag {
case "check":
if loc.CheckOnly {
return nil, ErrFormat
}
loc.CheckOnly = true
case "ro":
if loc.ReadOnly {
return nil, ErrFormat
}
loc.ReadOnly = true
default:
return nil, ErrFormat
}
}
}
quoted := strings.HasPrefix(s, "\"")
if quoted {
s = s[1:]
var b strings.Builder
closed := false
for len(s) > 0 {
if s[0] != '"' {
b.WriteByte(s[0])
s = s[1:]
continue
}
if len(s) > 1 && s[1] == '"' {
b.WriteByte('"')
s = s[2:]
continue
}
s = s[1:]
closed = true
break
}
if !closed || len(s) > 0 && s[0] != ';' {
return nil, ErrFormat
}
loc.Address = b.String()
if loc.Address == "" {
return nil, ErrFormat
}
} else {
loc.Address, s, _ = strings.Cut(s, ";")
if strings.ContainsRune(loc.Address, '"') {
return nil, ErrFormat
}
}
if quoted && strings.HasPrefix(s, ";") {
s = s[1:]
}
if loc.Address == "" {
if loc.ReadOnly || loc.CheckOnly {
return nil, ErrFormat
}
continue
}
if !validLocation(loc.Address, goos) {
return nil, fmt.Errorf("%w: location %q", ErrFormat, loc.Address)
}
if !seen[loc] {
result = append(result, loc)
seen[loc] = true
}
}
return result, nil
}
func httpLocation(s string) bool {
scheme, _, ok := strings.Cut(s, ":")
return ok && (strings.EqualFold(scheme, "http") || strings.EqualFold(scheme, "https"))
}
func locationStart(s, goos string) bool {
return strings.HasPrefix(s, "/") || httpLocation(s) || strings.HasPrefix(s, ";") || goos == "windows" && (strings.HasPrefix(s, `\`) || len(s) > 1 && s[1] == ':')
}
func validLocation(s, goos string) bool {
if httpLocation(s) {
_, e := Endpoint(s)
return e == nil
}
if strings.ContainsRune(s, 0) {
return false
}
if goos != "windows" {
return strings.HasPrefix(s, "/")
}
s = strings.ReplaceAll(s, "/", `\`)
if strings.HasPrefix(s, `\\?\`) || strings.HasPrefix(s, `\\.\`) {
return false
}
if len(s) >= 3 && ((s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z')) && s[1] == ':' && (s[2] == '\\' || s[2] == '/') {
return true
}
if !strings.HasPrefix(s, `\\`) {
return false
}
parts := strings.Split(strings.ReplaceAll(s[2:], "/", `\`), `\`)
return len(parts) >= 2 && parts[0] != "" && parts[1] != "" && parts[0] != "." && parts[0] != ".." && parts[1] != "." && parts[1] != ".."
}
// Discover uses LOCK_PATH when set, including when empty. Project precedes user and host defaults.
// getenv must distinguish an absent variable from an explicitly empty variable.
func Discover(project string, getenv func(string) (string, bool)) ([]Location, error) {
return discover(runtime.GOOS, project, getenv, knownFolders)
}
func discover(goos, project string, getenv func(string) (string, bool), folders func() (string, string)) ([]Location, error) {
if getenv == nil {
return nil, ErrFormat
}
if s, ok := getenv("LOCK_PATH"); ok {
return ParsePathForOS(s, goos)
}
var result []Location
add := func(base string, parts ...string) {
if validLocation(base, goos) {
path := filepath.Join(append([]string{base}, parts...)...)
result = append(result, Location{Address: path})
}
}
if project != "" {
if !filepath.IsAbs(project) || strings.ContainsRune(project, 0) {
return nil, ErrFormat
}
add(project, ".lock", "locks")
}
home, _ := getenv("HOME")
if strings.ContainsRune(home, 0) {
home = ""
}
switch goos {
case "windows":
user, host := folders()
if user != "" {
add(user, "Locks", "locks")
}
if host != "" {
add(host, "Locks", "locks")
}
case "darwin":
if filepath.IsAbs(home) {
add(home, "Library", "Caches", "Locks", "locks")
}
add("/Library/Application Support/Locks/locks")
default:
cache, _ := getenv("XDG_CACHE_HOME")
if strings.ContainsRune(cache, 0) {
cache = ""
}
if !filepath.IsAbs(cache) && filepath.IsAbs(home) {
cache = filepath.Join(home, ".cache")
}
if filepath.IsAbs(cache) {
add(cache, "locks")
}
switch goos {
case "freebsd", "openbsd", "netbsd", "dragonfly":
add("/var/db/locks/locks")
default:
add("/var/lib/locks/locks")
}
}
return result, nil
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.