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.
5 // Package filepath implements utility routines for manipulating filename paths
6 // in a way compatible with the target operating system-defined file paths.
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.
22 // A lazybuf is a lazily constructed path buffer.
23 // It supports append, reading previously appended bytes,
24 // and retrieving the final string. It does not allocate a buffer
25 // to hold the output until that output diverges from s.
34 func (b *lazybuf) index(i int) byte {
41 func (b *lazybuf) append(c byte) {
43 if b.w < len(b.path) && b.path[b.w] == c {
47 b.buf = make([]byte, len(b.path))
48 copy(b.buf, b.path[:b.w])
54 func (b *lazybuf) string() string {
56 return b.volAndPath[:b.volLen+b.w]
58 return b.volAndPath[:b.volLen] + string(b.buf[:b.w])
62 Separator = os.PathSeparator
63 ListSeparator = os.PathListSeparator
66 // Clean returns the shortest path name equivalent to path
67 // by purely lexical processing. It applies the following rules
68 // iteratively until no further processing can be done:
70 // 1. Replace multiple Separator elements with a single one.
71 // 2. Eliminate each . path name element (the current directory).
72 // 3. Eliminate each inner .. path name element (the parent directory)
73 // along with the non-.. element that precedes it.
74 // 4. Eliminate .. elements that begin a rooted path:
75 // that is, replace "/.." by "/" at the beginning of a path,
76 // assuming Separator is '/'.
78 // The returned path ends in a slash only if it represents a root directory,
79 // such as "/" on Unix or `C:\` on Windows.
81 // Finally, any occurrences of slash are replaced by Separator.
83 // If the result of this process is an empty string, Clean
84 // returns the string ".".
86 // See also Rob Pike, ``Lexical File Names in Plan 9 or
87 // Getting Dot-Dot Right,''
88 // https://9p.io/sys/doc/lexnames.html
89 func Clean(path string) string {
91 volLen := volumeNameLen(path)
94 if volLen > 1 && originalPath[1] != ':' {
96 return FromSlash(originalPath)
98 return originalPath + "."
100 rooted := os.IsPathSeparator(path[0])
103 // reading from path; r is index of next byte to process.
104 // writing to buf; w is index of next byte to write.
105 // dotdot is index in buf where .. must stop, either because
106 // it is the leading slash or it is a leading ../../.. prefix.
108 out := lazybuf{path: path, volAndPath: originalPath, volLen: volLen}
111 out.append(Separator)
117 case os.IsPathSeparator(path[r]):
118 // empty path element
120 case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])):
123 case path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])):
124 // .. element: remove to last separator
130 for out.w > dotdot && !os.IsPathSeparator(out.index(out.w)) {
134 // cannot backtrack, but not rooted, so append .. element.
136 out.append(Separator)
143 // real path element.
144 // add slash if needed
145 if rooted && out.w != 1 || !rooted && out.w != 0 {
146 out.append(Separator)
149 for ; r < n && !os.IsPathSeparator(path[r]); r++ {
155 // Turn empty string into "."
160 return FromSlash(out.string())
163 // ToSlash returns the result of replacing each separator character
164 // in path with a slash ('/') character. Multiple separators are
165 // replaced by multiple slashes.
166 func ToSlash(path string) string {
167 if Separator == '/' {
170 return strings.ReplaceAll(path, string(Separator), "/")
173 // FromSlash returns the result of replacing each slash ('/') character
174 // in path with a separator character. Multiple slashes are replaced
175 // by multiple separators.
176 func FromSlash(path string) string {
177 if Separator == '/' {
180 return strings.ReplaceAll(path, "/", string(Separator))
183 // SplitList splits a list of paths joined by the OS-specific ListSeparator,
184 // usually found in PATH or GOPATH environment variables.
185 // Unlike strings.Split, SplitList returns an empty slice when passed an empty
187 func SplitList(path string) []string {
188 return splitList(path)
191 // Split splits path immediately following the final Separator,
192 // separating it into a directory and file name component.
193 // If there is no Separator in path, Split returns an empty dir
194 // and file set to path.
195 // The returned values have the property that path = dir+file.
196 func Split(path string) (dir, file string) {
197 vol := VolumeName(path)
199 for i >= len(vol) && !os.IsPathSeparator(path[i]) {
202 return path[:i+1], path[i+1:]
205 // Join joins any number of path elements into a single path,
206 // separating them with an OS specific Separator. Empty elements
207 // are ignored. The result is Cleaned. However, if the argument
208 // list is empty or all its elements are empty, Join returns
210 // On Windows, the result will only be a UNC path if the first
211 // non-empty element is a UNC path.
212 func Join(elem ...string) string {
216 // Ext returns the file name extension used by path.
217 // The extension is the suffix beginning at the final dot
218 // in the final element of path; it is empty if there is
220 func Ext(path string) string {
221 for i := len(path) - 1; i >= 0 && !os.IsPathSeparator(path[i]); i-- {
229 // EvalSymlinks returns the path name after the evaluation of any symbolic
231 // If path is relative the result will be relative to the current directory,
232 // unless one of the components is an absolute symbolic link.
233 // EvalSymlinks calls Clean on the result.
234 func EvalSymlinks(path string) (string, error) {
235 return evalSymlinks(path)
238 // Abs returns an absolute representation of path.
239 // If the path is not absolute it will be joined with the current
240 // working directory to turn it into an absolute path. The absolute
241 // path name for a given file is not guaranteed to be unique.
242 // Abs calls Clean on the result.
243 func Abs(path string) (string, error) {
247 func unixAbs(path string) (string, error) {
249 return Clean(path), nil
251 wd, err := os.Getwd()
255 return Join(wd, path), nil
258 // Rel returns a relative path that is lexically equivalent to targpath when
259 // joined to basepath with an intervening separator. That is,
260 // Join(basepath, Rel(basepath, targpath)) is equivalent to targpath itself.
261 // On success, the returned path will always be relative to basepath,
262 // even if basepath and targpath share no elements.
263 // An error is returned if targpath can't be made relative to basepath or if
264 // knowing the current working directory would be necessary to compute it.
265 // Rel calls Clean on the result.
266 func Rel(basepath, targpath string) (string, error) {
267 baseVol := VolumeName(basepath)
268 targVol := VolumeName(targpath)
269 base := Clean(basepath)
270 targ := Clean(targpath)
271 if sameWord(targ, base) {
274 base = base[len(baseVol):]
275 targ = targ[len(targVol):]
279 // Can't use IsAbs - `\a` and `a` are both relative in Windows.
280 baseSlashed := len(base) > 0 && base[0] == Separator
281 targSlashed := len(targ) > 0 && targ[0] == Separator
282 if baseSlashed != targSlashed || !sameWord(baseVol, targVol) {
283 return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath)
285 // Position base[b0:bi] and targ[t0:ti] at the first differing elements.
288 var b0, bi, t0, ti int
290 for bi < bl && base[bi] != Separator {
293 for ti < tl && targ[ti] != Separator {
296 if !sameWord(targ[t0:ti], base[b0:bi]) {
308 if base[b0:bi] == ".." {
309 return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath)
312 // Base elements left. Must go up before going down.
313 seps := strings.Count(base[b0:bl], string(Separator))
318 buf := make([]byte, size)
320 for i := 0; i < seps; i++ {
322 copy(buf[n+1:], "..")
327 copy(buf[n+1:], targ[t0:])
329 return string(buf), nil
331 return targ[t0:], nil
334 // SkipDir is used as a return value from WalkFuncs to indicate that
335 // the directory named in the call is to be skipped. It is not returned
336 // as an error by any function.
337 var SkipDir = errors.New("skip this directory")
339 // WalkDirFunc is the type of the function called by WalkDir to visit
340 // each each file or directory.
342 // The path argument contains the argument to Walk as a prefix.
343 // That is, if Walk is called with root argument "dir" and finds a file
344 // named "a" in that directory, the walk function will be called with
347 // The directory and file are joined with Join, which may clean the
348 // directory name: if Walk is called with the root argument "x/../dir"
349 // and finds a file named "a" in that directory, the walk function will
350 // be called with argument "dir/a", not "x/../dir/a".
352 // The d argument is the fs.DirEntry for the named path.
354 // The error result returned by the function controls how WalkDir
355 // continues. If the function returns the special value SkipDir, WalkDir
356 // skips the current directory (path if d.IsDir() is true, otherwise
357 // path's parent directory). Otherwise, if the function returns a non-nil
358 // error, WalkDir stops entirely and returns that error.
360 // The err argument reports an error related to path, signaling that
361 // WalkDir will not walk into that directory. The function can decide how
362 // to handle that error; as described earlier, returning the error will
363 // cause WalkDir to stop walking the entire tree.
365 // WalkDir calls the function with a non-nil err argument in two cases.
367 // First, if the initial os.Lstat on the root directory fails, WalkDir
368 // calls the function with path set to root, d set to nil, and err set to
369 // the error from os.Lstat.
371 // Second, if a directory's ReadDir method fails, WalkDir calls the
372 // function with path set to the directory's path, d set to an
373 // fs.DirEntry describing the directory, and err set to the error from
374 // ReadDir. In this second case, the function is called twice with the
375 // path of the directory: the first call is before the directory read is
376 // attempted and has err set to nil, giving the function a chance to
377 // return SkipDir and avoid the ReadDir entirely. The second call is
378 // after a failed ReadDir and reports the error from ReadDir.
379 // (If ReadDir succeeds, there is no second call.)
381 // The differences between WalkDirFunc compared to WalkFunc are:
383 // - The second argument has type fs.DirEntry instead of fs.FileInfo.
384 // - The function is called before reading a directory, to allow SkipDir
385 // to bypass the directory read entirely.
386 // - If a directory read fails, the function is called a second time
387 // for that directory to report the error.
389 type WalkDirFunc func(path string, d fs.DirEntry, err error) error
391 // WalkFunc is the type of the function called by Walk to visit each each
392 // file or directory.
394 // The path argument contains the argument to Walk as a prefix.
395 // That is, if Walk is called with root argument "dir" and finds a file
396 // named "a" in that directory, the walk function will be called with
399 // The directory and file are joined with Join, which may clean the
400 // directory name: if Walk is called with the root argument "x/../dir"
401 // and finds a file named "a" in that directory, the walk function will
402 // be called with argument "dir/a", not "x/../dir/a".
404 // The info argument is the fs.FileInfo for the named path.
406 // The error result returned by the function controls how Walk continues.
407 // If the function returns the special value SkipDir, Walk skips the
408 // current directory (path if info.IsDir() is true, otherwise path's
409 // parent directory). Otherwise, if the function returns a non-nil error,
410 // Walk stops entirely and returns that error.
412 // The err argument reports an error related to path, signaling that Walk
413 // will not walk into that directory. The function can decide how to
414 // handle that error; as described earlier, returning the error will
415 // cause Walk to stop walking the entire tree.
417 // Walk calls the function with a non-nil err argument in two cases.
419 // First, if an os.Lstat on the root directory or any directory or file
420 // in the tree fails, Walk calls the function with path set to that
421 // directory or file's path, info set to nil, and err set to the error
424 // Second, if a directory's Readdirnames method fails, Walk calls the
425 // function with path set to the directory's path, info, set to an
426 // fs.FileInfo describing the directory, and err set to the error from
428 type WalkFunc func(path string, info fs.FileInfo, err error) error
430 var lstat = os.Lstat // for testing
432 // walkDir recursively descends path, calling walkDirFn.
433 func walkDir(path string, d fs.DirEntry, walkDirFn WalkDirFunc) error {
434 if err := walkDirFn(path, d, nil); err != nil || !d.IsDir() {
435 if err == SkipDir && d.IsDir() {
436 // Successfully skipped directory.
442 dirs, err := readDir(path)
444 // Second call, to report ReadDir error.
445 err = walkDirFn(path, d, err)
451 for _, d1 := range dirs {
452 path1 := Join(path, d1.Name())
453 if err := walkDir(path1, d1, walkDirFn); err != nil {
463 // walk recursively descends path, calling walkFn.
464 func walk(path string, info fs.FileInfo, walkFn WalkFunc) error {
466 return walkFn(path, info, nil)
469 names, err := readDirNames(path)
470 err1 := walkFn(path, info, err)
471 // If err != nil, walk can't walk into this directory.
472 // err1 != nil means walkFn want walk to skip this directory or stop walking.
473 // Therefore, if one of err and err1 isn't nil, walk will return.
474 if err != nil || err1 != nil {
475 // The caller's behavior is controlled by the return value, which is decided
476 // by walkFn. walkFn may ignore err and return nil.
477 // If walkFn returns SkipDir, it will be handled by the caller.
478 // So walk should return whatever walkFn returns.
482 for _, name := range names {
483 filename := Join(path, name)
484 fileInfo, err := lstat(filename)
486 if err := walkFn(filename, fileInfo, err); err != nil && err != SkipDir {
490 err = walk(filename, fileInfo, walkFn)
492 if !fileInfo.IsDir() || err != SkipDir {
501 // WalkDir walks the file tree rooted at root, calling fn for each file or
502 // directory in the tree, including root.
504 // All errors that arise visiting files and directories are filtered by fn:
505 // see the WalkDirFunc documentation for details.
507 // The files are walked in lexical order, which makes the output deterministic
508 // but requires WalkDir to read an entire directory into memory before proceeding
509 // to walk that directory.
511 // WalkDir does not follow symbolic links.
512 func WalkDir(root string, fn WalkDirFunc) error {
513 info, err := os.Lstat(root)
515 err = fn(root, nil, err)
517 err = walkDir(root, &dirEntryFromInfo{info}, fn)
525 type dirEntryFromInfo struct {
529 func (e *dirEntryFromInfo) Type() fs.FileMode {
530 return e.Mode().Type()
533 func (e *dirEntryFromInfo) Info() (fs.FileInfo, error) {
534 return e.FileInfo, nil
537 // Walk walks the file tree rooted at root, calling fn for each file or
538 // directory in the tree, including root.
540 // All errors that arise visiting files and directories are filtered by fn:
541 // see the WalkFunc documentation for details.
543 // The files are walked in lexical order, which makes the output deterministic
544 // but requires Walk to read an entire directory into memory before proceeding
545 // to walk that directory.
547 // Walk does not follow symbolic links.
549 // Walk is less efficient than WalkDir, introduced in Go 1.16,
550 // which avoids calling os.Lstat on every visited file or directory.
551 func Walk(root string, fn WalkFunc) error {
552 info, err := os.Lstat(root)
554 err = fn(root, nil, err)
556 err = walk(root, info, fn)
564 // readDir reads the directory named by dirname and returns
565 // a sorted list of directory entries.
566 func readDir(dirname string) ([]fs.DirEntry, error) {
567 f, err := os.Open(dirname)
571 dirs, err := f.ReadDir(-1)
576 sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() })
580 // readDirNames reads the directory named by dirname and returns
581 // a sorted list of directory entry names.
582 func readDirNames(dirname string) ([]string, error) {
583 f, err := os.Open(dirname)
587 names, err := f.Readdirnames(-1)
596 // Base returns the last element of path.
597 // Trailing path separators are removed before extracting the last element.
598 // If the path is empty, Base returns ".".
599 // If the path consists entirely of separators, Base returns a single separator.
600 func Base(path string) string {
604 // Strip trailing slashes.
605 for len(path) > 0 && os.IsPathSeparator(path[len(path)-1]) {
606 path = path[0 : len(path)-1]
608 // Throw away volume name
609 path = path[len(VolumeName(path)):]
610 // Find the last element
612 for i >= 0 && !os.IsPathSeparator(path[i]) {
618 // If empty now, it had only slashes.
620 return string(Separator)
625 // Dir returns all but the last element of path, typically the path's directory.
626 // After dropping the final element, Dir calls Clean on the path and trailing
627 // slashes are removed.
628 // If the path is empty, Dir returns ".".
629 // If the path consists entirely of separators, Dir returns a single separator.
630 // The returned path does not end in a separator unless it is the root directory.
631 func Dir(path string) string {
632 vol := VolumeName(path)
634 for i >= len(vol) && !os.IsPathSeparator(path[i]) {
637 dir := Clean(path[len(vol) : i+1])
638 if dir == "." && len(vol) > 2 {
645 // VolumeName returns leading volume name.
646 // Given "C:\foo\bar" it returns "C:" on Windows.
647 // Given "\\host\share\foo" it returns "\\host\share".
648 // On other platforms it returns "".
649 func VolumeName(path string) string {
650 return path[:volumeNameLen(path)]