Debian Codegraph @

src/path/filepath/path.go

raw
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package filepath implements utility routines for manipulating filename paths
6 // in a way compatible with the target operating system-defined file paths.
7 //
8 // The filepath package uses either forward slashes or backslashes,
9 // depending on the operating system. To process paths such as URLs
10 // that always use forward slashes regardless of the operating
11 // system, see the [path] package.
12 package filepath
13
14 import (
15 "errors"
16 "internal/bytealg"
17 "internal/filepathlite"
18 "io/fs"
19 "os"
20 "slices"
21 )
22
23 const (
24 Separator = os.PathSeparator
25 ListSeparator = os.PathListSeparator
26 )
27
28 // Clean returns the shortest path name equivalent to path
29 // by purely lexical processing. It applies the following rules
30 // iteratively until no further processing can be done:
31 //
32 // 1. Replace multiple [Separator] elements with a single one.
33 // 2. Eliminate each . path name element (the current directory).
34 // 3. Eliminate each inner .. path name element (the parent directory)
35 // along with the non-.. element that precedes it.
36 // 4. Eliminate .. elements that begin a rooted path:
37 // that is, replace "/.." by "/" at the beginning of a path,
38 // assuming Separator is '/'.
39 //
40 // The returned path ends in a slash only if it represents a root directory,
41 // such as "/" on Unix or `C:\` on Windows.
42 //
43 // Finally, any occurrences of slash are replaced by Separator.
44 //
45 // If the result of this process is an empty string, Clean
46 // returns the string ".".
47 //
48 // On Windows, Clean does not modify the volume name other than to replace
49 // occurrences of "/" with `\`.
50 // For example, Clean("//host/share/../x") returns `\\host\share\x`.
51 //
52 // See also Rob Pike, “Lexical File Names in Plan 9 or
53 // Getting Dot-Dot Right,”
54 // https://9p.io/sys/doc/lexnames.html
55 func Clean(path string) string {
56 return filepathlite.Clean(path)
57 }
58
59 // IsLocal reports whether path, using lexical analysis only, has all of these properties:
60 //
61 // - is within the subtree rooted at the directory in which path is evaluated
62 // - is not an absolute path
63 // - is not empty
64 // - on Windows, is not a reserved name such as "NUL"
65 //
66 // If IsLocal(path) returns true, then
67 // Join(base, path) will always produce a path contained within base and
68 // Clean(path) will always produce an unrooted path with no ".." path elements.
69 //
70 // IsLocal is a purely lexical operation.
71 // In particular, it does not account for the effect of any symbolic links
72 // that may exist in the filesystem.
73 func IsLocal(path string) bool {
74 return filepathlite.IsLocal(path)
75 }
76
77 // Localize converts a slash-separated path into an operating system path.
78 // The input path must be a valid path as reported by [io/fs.ValidPath].
79 //
80 // Localize returns an error if the path cannot be represented by the operating system.
81 // For example, the path a\b is rejected on Windows, on which \ is a separator
82 // character and cannot be part of a filename.
83 //
84 // The path returned by Localize will always be local, as reported by IsLocal.
85 func Localize(path string) (string, error) {
86 return filepathlite.Localize(path)
87 }
88
89 // ToSlash returns the result of replacing each separator character
90 // in path with a slash ('/') character. Multiple separators are
91 // replaced by multiple slashes.
92 func ToSlash(path string) string {
93 return filepathlite.ToSlash(path)
94 }
95
96 // FromSlash returns the result of replacing each slash ('/') character
97 // in path with a separator character. Multiple slashes are replaced
98 // by multiple separators.
99 //
100 // See also the Localize function, which converts a slash-separated path
101 // as used by the io/fs package to an operating system path.
102 func FromSlash(path string) string {
103 return filepathlite.FromSlash(path)
104 }
105
106 // SplitList splits a list of paths joined by the OS-specific [ListSeparator],
107 // usually found in PATH or GOPATH environment variables.
108 // Unlike strings.Split, SplitList returns an empty slice when passed an empty
109 // string.
110 func SplitList(path string) []string {
111 return splitList(path)
112 }
113
114 // Split splits path immediately following the final [Separator],
115 // separating it into a directory and file name component.
116 // If there is no Separator in path, Split returns an empty dir
117 // and file set to path.
118 // The returned values have the property that path = dir+file.
119 func Split(path string) (dir, file string) {
120 return filepathlite.Split(path)
121 }
122
123 // Join joins any number of path elements into a single path,
124 // separating them with an OS specific [Separator]. Empty elements
125 // are ignored. The result is Cleaned. However, if the argument
126 // list is empty or all its elements are empty, Join returns
127 // an empty string.
128 // On Windows, the result will only be a UNC path if the first
129 // non-empty element is a UNC path.
130 func Join(elem ...string) string {
131 return join(elem)
132 }
133
134 // Ext returns the file name extension used by path.
135 // The extension is the suffix beginning at the final dot
136 // in the final element of path; it is empty if there is
137 // no dot.
138 func Ext(path string) string {
139 return filepathlite.Ext(path)
140 }
141
142 // EvalSymlinks returns the path name after the evaluation of any symbolic
143 // links.
144 // If path is relative the result will be relative to the current directory,
145 // unless one of the components is an absolute symbolic link.
146 // EvalSymlinks calls [Clean] on the result.
147 func EvalSymlinks(path string) (string, error) {
148 return evalSymlinks(path)
149 }
150
151 // IsAbs reports whether the path is absolute.
152 func IsAbs(path string) bool {
153 return filepathlite.IsAbs(path)
154 }
155
156 // Abs returns an absolute representation of path.
157 // If the path is not absolute it will be joined with the current
158 // working directory to turn it into an absolute path. The absolute
159 // path name for a given file is not guaranteed to be unique.
160 // Abs calls [Clean] on the result.
161 func Abs(path string) (string, error) {
162 return abs(path)
163 }
164
165 func unixAbs(path string) (string, error) {
166 if IsAbs(path) {
167 return Clean(path), nil
168 }
169 wd, err := os.Getwd()
170 if err != nil {
171 return "", err
172 }
173 return Join(wd, path), nil
174 }
175
176 // Rel returns a relative path that is lexically equivalent to targPath when
177 // joined to basePath with an intervening separator. That is,
178 // [Join](basePath, Rel(basePath, targPath)) is equivalent to targPath itself.
179 //
180 // The returned path will always be relative to basePath, even if basePath and
181 // targPath share no elements. Rel calls [Clean] on the result.
182 //
183 // An error is returned if targPath can't be made relative to basePath
184 // or if knowing the current working directory would be necessary to compute it.
185 func Rel(basePath, targPath string) (string, error) {
186 baseVol := VolumeName(basePath)
187 targVol := VolumeName(targPath)
188 base := Clean(basePath)
189 targ := Clean(targPath)
190 if sameWord(targ, base) {
191 return ".", nil
192 }
193 base = base[len(baseVol):]
194 targ = targ[len(targVol):]
195 if base == "." {
196 base = ""
197 } else if base == "" && filepathlite.VolumeNameLen(baseVol) > 2 /* isUNC */ {
198 // Treat any targetpath matching `\\host\share` basePath as absolute path.
199 base = string(Separator)
200 }
201
202 // Can't use IsAbs - `\a` and `a` are both relative in Windows.
203 baseSlashed := len(base) > 0 && base[0] == Separator
204 targSlashed := len(targ) > 0 && targ[0] == Separator
205 if baseSlashed != targSlashed || !sameWord(baseVol, targVol) {
206 return "", errors.New("Rel: can't make " + targPath + " relative to " + basePath)
207 }
208 // Position base[b0:bi] and targ[t0:ti] at the first differing elements.
209 bl := len(base)
210 tl := len(targ)
211 var b0, bi, t0, ti int
212 for {
213 for bi < bl && base[bi] != Separator {
214 bi++
215 }
216 for ti < tl && targ[ti] != Separator {
217 ti++
218 }
219 if !sameWord(targ[t0:ti], base[b0:bi]) {
220 break
221 }
222 if bi < bl {
223 bi++
224 }
225 if ti < tl {
226 ti++
227 }
228 b0 = bi
229 t0 = ti
230 }
231 if base[b0:bi] == ".." {
232 return "", errors.New("Rel: can't make " + targPath + " relative to " + basePath)
233 }
234 if b0 != bl {
235 // Base elements left. Must go up before going down.
236 seps := bytealg.CountString(base[b0:bl], Separator)
237 size := 2 + seps*3
238 if tl != t0 {
239 size += 1 + tl - t0
240 }
241 buf := make([]byte, size)
242 n := copy(buf, "..")
243 for i := 0; i < seps; i++ {
244 buf[n] = Separator
245 copy(buf[n+1:], "..")
246 n += 3
247 }
248 if t0 != tl {
249 buf[n] = Separator
250 copy(buf[n+1:], targ[t0:])
251 }
252 return Clean(string(buf)), nil
253 }
254 return targ[t0:], nil
255 }
256
257 // SkipDir is used as a return value from [WalkFunc] to indicate that
258 // the directory named in the call is to be skipped. It is not returned
259 // as an error by any function.
260 var SkipDir error = fs.SkipDir
261
262 // SkipAll is used as a return value from [WalkFunc] to indicate that
263 // all remaining files and directories are to be skipped. It is not returned
264 // as an error by any function.
265 var SkipAll error = fs.SkipAll
266
267 // WalkFunc is the type of the function called by [Walk] to visit each
268 // file or directory.
269 //
270 // The path argument contains the argument to Walk as a prefix.
271 // That is, if Walk is called with root argument "dir" and finds a file
272 // named "a" in that directory, the walk function will be called with
273 // argument "dir/a".
274 //
275 // The directory and file are joined with Join, which may clean the
276 // directory name: if Walk is called with the root argument "x/../dir"
277 // and finds a file named "a" in that directory, the walk function will
278 // be called with argument "dir/a", not "x/../dir/a".
279 //
280 // The info argument is the fs.FileInfo for the named path.
281 //
282 // The error result returned by the function controls how Walk continues.
283 // If the function returns the special value [SkipDir], Walk skips the
284 // current directory (path if info.IsDir() is true, otherwise path's
285 // parent directory). If the function returns the special value [SkipAll],
286 // Walk skips all remaining files and directories. Otherwise, if the function
287 // returns a non-nil error, Walk stops entirely and returns that error.
288 //
289 // The err argument reports an error related to path, signaling that Walk
290 // will not walk into that directory. The function can decide how to
291 // handle that error; as described earlier, returning the error will
292 // cause Walk to stop walking the entire tree.
293 //
294 // Walk calls the function with a non-nil err argument in two cases.
295 //
296 // First, if an [os.Lstat] on the root directory or any directory or file
297 // in the tree fails, Walk calls the function with path set to that
298 // directory or file's path, info set to nil, and err set to the error
299 // from os.Lstat.
300 //
301 // Second, if a directory's Readdirnames method fails, Walk calls the
302 // function with path set to the directory's path, info, set to an
303 // [fs.FileInfo] describing the directory, and err set to the error from
304 // Readdirnames.
305 type WalkFunc func(path string, info fs.FileInfo, err error) error
306
307 var lstat = os.Lstat // for testing
308
309 // walkDir recursively descends path, calling walkDirFn.
310 func walkDir(path string, d fs.DirEntry, walkDirFn fs.WalkDirFunc) error {
311 if err := walkDirFn(path, d, nil); err != nil || !d.IsDir() {
312 if err == SkipDir && d.IsDir() {
313 // Successfully skipped directory.
314 err = nil
315 }
316 return err
317 }
318
319 dirs, err := os.ReadDir(path)
320 if err != nil {
321 // Second call, to report ReadDir error.
322 err = walkDirFn(path, d, err)
323 if err != nil {
324 if err == SkipDir && d.IsDir() {
325 err = nil
326 }
327 return err
328 }
329 }
330
331 for _, d1 := range dirs {
332 path1 := Join(path, d1.Name())
333 if err := walkDir(path1, d1, walkDirFn); err != nil {
334 if err == SkipDir {
335 break
336 }
337 return err
338 }
339 }
340 return nil
341 }
342
343 // walk recursively descends path, calling walkFn.
344 func walk(path string, info fs.FileInfo, walkFn WalkFunc) error {
345 if !info.IsDir() {
346 return walkFn(path, info, nil)
347 }
348
349 names, err := readDirNames(path)
350 err1 := walkFn(path, info, err)
351 // If err != nil, walk can't walk into this directory.
352 // err1 != nil means walkFn want walk to skip this directory or stop walking.
353 // Therefore, if one of err and err1 isn't nil, walk will return.
354 if err != nil || err1 != nil {
355 // The caller's behavior is controlled by the return value, which is decided
356 // by walkFn. walkFn may ignore err and return nil.
357 // If walkFn returns SkipDir or SkipAll, it will be handled by the caller.
358 // So walk should return whatever walkFn returns.
359 return err1
360 }
361
362 for _, name := range names {
363 filename := Join(path, name)
364 fileInfo, err := lstat(filename)
365 if err != nil {
366 if err := walkFn(filename, fileInfo, err); err != nil && err != SkipDir {
367 return err
368 }
369 } else {
370 err = walk(filename, fileInfo, walkFn)
371 if err != nil {
372 if !fileInfo.IsDir() || err != SkipDir {
373 return err
374 }
375 }
376 }
377 }
378 return nil
379 }
380
381 // WalkDir walks the file tree rooted at root, calling fn for each file or
382 // directory in the tree, including root.
383 //
384 // All errors that arise visiting files and directories are filtered by fn:
385 // see the [fs.WalkDirFunc] documentation for details.
386 //
387 // The files are walked in lexical order, which makes the output deterministic
388 // but requires WalkDir to read an entire directory into memory before proceeding
389 // to walk that directory.
390 //
391 // WalkDir does not follow symbolic links.
392 //
393 // WalkDir calls fn with paths that use the separator character appropriate
394 // for the operating system. This is unlike [io/fs.WalkDir], which always
395 // uses slash separated paths.
396 func WalkDir(root string, fn fs.WalkDirFunc) error {
397 info, err := os.Lstat(root)
398 if err != nil {
399 err = fn(root, nil, err)
400 } else {
401 err = walkDir(root, fs.FileInfoToDirEntry(info), fn)
402 }
403 if err == SkipDir || err == SkipAll {
404 return nil
405 }
406 return err
407 }
408
409 // Walk walks the file tree rooted at root, calling fn for each file or
410 // directory in the tree, including root.
411 //
412 // All errors that arise visiting files and directories are filtered by fn:
413 // see the [WalkFunc] documentation for details.
414 //
415 // The files are walked in lexical order, which makes the output deterministic
416 // but requires Walk to read an entire directory into memory before proceeding
417 // to walk that directory.
418 //
419 // Walk does not follow symbolic links.
420 //
421 // Walk is less efficient than [WalkDir], introduced in Go 1.16,
422 // which avoids calling os.Lstat on every visited file or directory.
423 func Walk(root string, fn WalkFunc) error {
424 info, err := os.Lstat(root)
425 if err != nil {
426 err = fn(root, nil, err)
427 } else {
428 err = walk(root, info, fn)
429 }
430 if err == SkipDir || err == SkipAll {
431 return nil
432 }
433 return err
434 }
435
436 // readDirNames reads the directory named by dirname and returns
437 // a sorted list of directory entry names.
438 func readDirNames(dirname string) ([]string, error) {
439 f, err := os.Open(dirname)
440 if err != nil {
441 return nil, err
442 }
443 names, err := f.Readdirnames(-1)
444 f.Close()
445 if err != nil {
446 return nil, err
447 }
448 slices.Sort(names)
449 return names, nil
450 }
451
452 // Base returns the last element of path.
453 // Trailing path separators are removed before extracting the last element.
454 // If the path is empty, Base returns ".".
455 // If the path consists entirely of separators, Base returns a single separator.
456 func Base(path string) string {
457 return filepathlite.Base(path)
458 }
459
460 // Dir returns all but the last element of path, typically the path's directory.
461 // After dropping the final element, Dir calls [Clean] on the path and trailing
462 // slashes are removed.
463 // If the path is empty, Dir returns ".".
464 // If the path consists entirely of separators, Dir returns a single separator.
465 // The returned path does not end in a separator unless it is the root directory.
466 func Dir(path string) string {
467 return filepathlite.Dir(path)
468 }
469
470 // VolumeName returns leading volume name.
471 // Given "C:\foo\bar" it returns "C:" on Windows.
472 // Given "\\host\share\foo" it returns "\\host\share".
473 // On other platforms it returns "".
474 func VolumeName(path string) string {
475 return filepathlite.VolumeName(path)
476 }