-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentry_path_ext.go
More file actions
303 lines (263 loc) · 6.51 KB
/
Copy pathentry_path_ext.go
File metadata and controls
303 lines (263 loc) · 6.51 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package dt
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
)
// EntryStatusFlags controls optional classification behavior.
// The zero value is safe and means "follow symlinks" (os.Stat).
type EntryStatusFlags uint32
const (
// DontFollowSymlinks causes Status to inspect the entry itself
// (os.Lstat) instead of following symlinks.
DontFollowSymlinks EntryStatusFlags = 1 << iota
// (Reserved for future flags)
// TreatBrokenSymlinkAsMissing
// ClassifyBlockVsCharDevice
// ...
)
// Status classifies the filesystem entry referred to by fp.
//
// It returns IsMissingEntry when the entry does not exist (err == nil).
// It returns IsEntryError for all other filesystem errors (err != nil).
// By default it follows symlinks (like os.Stat). To inspect the entry
// itself, pass FlagDontFollowSymlinks.
//
// On platforms that don't support certain kinds (e.g., sockets/devices on
// Windows), those statuses will never be returned.
func (ep EntryPath) Status(flags ...EntryStatusFlags) (status EntryStatus, err error) {
var info os.FileInfo
switch {
case len(flags) == 0:
info, err = os.Stat(string(ep))
case flags[0]&DontFollowSymlinks != 0:
info, err = os.Lstat(string(ep))
default:
info, err = os.Stat(string(ep))
}
if errors.Is(err, fs.ErrNotExist) {
status = IsMissingEntry
err = nil
goto end
}
if err != nil {
status = IsEntryError
goto end
}
status = GetEntryStatus(info)
end:
return status, err
}
func (ep EntryPath) Join(elems ...any) EntryPath {
ss := make([]string, 0, len(elems)+1)
ss = append(ss, string(ep))
for _, part := range elems {
switch s := part.(type) {
case string:
ss = append(ss, s)
case EntryPath:
ss = append(ss, string(s))
case DirPath:
ss = append(ss, string(s))
case Filepath:
ss = append(ss, string(s))
case RelFilepath:
ss = append(ss, string(s))
case PathSegment:
ss = append(ss, string(s))
case PathSegments:
ss = append(ss, string(s))
default:
ss = append(ss, fmt.Sprintf("%s", part))
}
}
return EntryPath(filepath.Join(ss...))
}
// EnsureTrailSep returns ep with exactly one trailing path separator
// when appropriate for the platform. It does not modify an empty string.
func (ep EntryPath) EnsureTrailSep() EntryPath {
if ep == "" {
goto end
}
// Already has a trailing native separator?
if ep[len(ep)-1] == os.PathSeparator {
goto end
}
// On Windows, also consider '/' as a valid existing trailing separator.
if os.PathSeparator == '\\' && ep[len(ep)-1] == '/' {
//goland:noinspection GoAssignmentToReceiver
ep = ep[:len(ep)-1] + `\`
goto end
}
ep += EntryPath(os.PathSeparator)
end:
return ep
}
// HasDotDotPrefix reports whether p, interpreted as a relative path,
// starts with a ".." segment (e.g. ".." or "../foo" or "..\foo").
// It does NOT treat names like "..foo" as having a dot-dot prefix.
func (ep EntryPath) HasDotDotPrefix() bool {
return ep == ".." || strings.HasPrefix(string(ep), ".."+string(os.PathSeparator))
}
func (ep EntryPath) Expand() (out EntryPath, err error) {
var home DirPath
s := string(ep)
switch {
case len(s) == 0:
err = ErrEmpty
goto end
case s == ".":
var dp DirPath
// We are "current directory
dp, err = Getwd()
if err != nil {
goto end
}
out = EntryPath(dp)
goto end
case s == "~":
home, err = UserHomeDir()
if err != nil {
goto end
}
out = EntryPath(home)
goto end
case s[0] == '/':
// We are an absolute path already, works on Windows, macOS and Linux.
if runtime.GOOS == "windows" {
s = filepath.FromSlash(s)
}
out, err = EntryPath(s).Clean().Abs()
goto end
case s[0] == '\\' && runtime.GOOS == "windows":
// We are an absolute path on Windows already
s = filepath.FromSlash(s)
out, err = EntryPath(s).Clean().Abs()
goto end
case len(s) == 1:
out, err = EntryPath(s).Clean().Abs()
goto end
case s[:2] == "..":
// We are a parent path, just return it
out, err = EntryPath(filepath.Dir(s)).Clean().Abs()
goto end
case s[:2] == "~/":
// We start with ~/ so we are a tilde path; works on Windows or Linux/macOS
if runtime.GOOS == "windows" {
s = filepath.FromSlash(s)
}
// Go on to be handled by the tilde expansion
case s[:2] == "~\\" && runtime.GOOS == "windows":
// Go on to be handled by the tilde expansion
default:
// Not a special case, just a relative path
out, err = EntryPath(s).Clean().Abs()
goto end
}
home, err = UserHomeDir()
if err != nil {
goto end
}
if len(s) == 2 {
out = EntryPath(home)
goto end
}
out = EntryPathJoin(home, s[2:]).Clean()
end:
if err != nil {
err = WithErr(err, ErrFailedToExpandPath, ep.ErrKV())
}
return out, err
}
func (ep EntryPath) Exists() (exists bool, err error) {
var status EntryStatus
status, err = ep.Status()
if err != nil {
goto end
}
exists = status == IsDirEntry || status == IsFileEntry
end:
return exists, err
}
func (ep EntryPath) ToTilde(opt TildeOption) (tep TildeEntryPath) {
return ToTilde[EntryPath, TildeEntryPath](ep, opt)
}
func (ep EntryPath) TrimTilde() (tdp PathSegments) {
return TrimTilde[EntryPath](ep)
}
func (ep EntryPath) IsFile() (isFile bool) {
status, err := ep.Status()
if err != nil {
goto end
}
if status != IsFileEntry {
goto end
}
isFile = true
end:
return isFile
}
func (ep EntryPath) IsDir() (isDir bool) {
status, err := ep.Status()
if err != nil {
goto end
}
if status != IsDirEntry {
goto end
}
isDir = true
end:
return isDir
}
func (ep EntryPath) ErrKV() ErrKV {
return kv{k: "path", v: ep.ToTilde(OrFullPath)}
}
func (ep EntryPath) EnsureFilepath(defaultName Filename) (fp Filepath, err error) {
var exists bool
fp = Filepath(ep)
if ep.IsDir() {
fp = Filepath(ep.Join(defaultName))
}
exists, err = fp.Exists()
if !exists {
err = NewErr(ErrFileNotExist, fp.ErrKV(), err)
goto end
}
end:
return fp, err
}
func EnsureFilepath(path string, defaultName Filename) (fp Filepath, err error) {
var ep EntryPath
ep, err = ParseEntryPath(path)
if err != nil {
err = NewErr(ErrInvalidFilepath, err)
goto end
}
fp, err = ep.EnsureFilepath(defaultName)
end:
return fp, err
}
func GetEntryStatus(info os.FileInfo) (status EntryStatus) {
mode := info.Mode()
switch {
case mode.IsRegular():
return IsFileEntry
case mode.IsDir():
return IsDirEntry
case mode&fs.ModeSymlink != 0:
return IsSymlinkEntry
case mode&fs.ModeSocket != 0:
return IsSocketEntry
case mode&fs.ModeNamedPipe != 0:
return IsPipeEntry
case mode&fs.ModeDevice != 0:
return IsDeviceEntry
case uint32(mode) != 0:
return IsUnclassifiedEntryStatus
}
return IsInvalidEntryStatus
}