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.
21 // A lazybuf is a lazily constructed path buffer.
22 // It supports append, reading previously appended bytes,
23 // and retrieving the final string. It does not allocate a buffer
24 // to hold the output until that output diverges from s.
33 func (b *lazybuf) index(i int) byte {
40 func (b *lazybuf) append(c byte) {
42 if b.w < len(b.path) && b.path[b.w] == c {
46 b.buf = make([]byte, len(b.path))
47 copy(b.buf, b.path[:b.w])
53 func (b *lazybuf) string() string {
55 return b.volAndPath[:b.volLen+b.w]
57 return b.volAndPath[:b.volLen] + string(b.buf[:b.w])
61 Separator = os.PathSeparator
62 ListSeparator = os.PathListSeparator
65 // Clean returns the shortest path name equivalent to path
66 // by purely lexical processing. It applies the following rules
67 // iteratively until no further processing can be done:
69 // 1. Replace multiple Separator elements with a single one.
70 // 2. Eliminate each . path name element (the current directory).
71 // 3. Eliminate each inner .. path name element (the parent directory)
72 // along with the non-.. element that precedes it.
73 // 4. Eliminate .. elements that begin a rooted path:
74 // that is, replace "/.." by "/" at the beginning of a path,
75 // assuming Separator is '/'.
77 // The returned path ends in a slash only if it represents a root directory,
78 // such as "/" on Unix or `C:\` on Windows.
80 // Finally, any occurrences of slash are replaced by Separator.
82 // If the result of this process is an empty string, Clean
83 // returns the string ".".
85 // See also Rob Pike, ``Lexical File Names in Plan 9 or
86 // Getting Dot-Dot Right,''
87 // https://9p.io/sys/doc/lexnames.html
88 func Clean(path string) string {
90 volLen := volumeNameLen(path)
93 if volLen > 1 && originalPath[1] != ':' {
95 return FromSlash(originalPath)
97 return originalPath + "."
99 rooted := os.IsPathSeparator(path[0])
102 // reading from path; r is index of next byte to process.
103 // writing to buf; w is index of next byte to write.
104 // dotdot is index in buf where .. must stop, either because
105 // it is the leading slash or it is a leading ../../.. prefix.
107 out := lazybuf{path: path, volAndPath: originalPath, volLen: volLen}
110 out.append(Separator)
116 case os.IsPathSeparator(path[r]):
117 // empty path element
119 case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])):
122 case path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])):
123 // .. element: remove to last separator
129 for out.w > dotdot && !os.IsPathSeparator(out.index(out.w)) {
133 // cannot backtrack, but not rooted, so append .. element.
135 out.append(Separator)
142 // real path element.
143 // add slash if needed
144 if rooted && out.w != 1 || !rooted && out.w != 0 {
145 out.append(Separator)
148 for ; r < n && !os.IsPathSeparator(path[r]); r++ {
154 // Turn empty string into "."
159 return FromSlash(out.string())
162 // ToSlash returns the result of replacing each separator character
163 // in path with a slash ('/') character. Multiple separators are
164 // replaced by multiple slashes.
165 func ToSlash(path string) string {
166 if Separator == '/' {
169 return strings.ReplaceAll(path, string(Separator), "/")
172 // FromSlash returns the result of replacing each slash ('/') character
173 // in path with a separator character. Multiple slashes are replaced
174 // by multiple separators.
175 func FromSlash(path string) string {
176 if Separator == '/' {
179 return strings.ReplaceAll(path, "/", string(Separator))
182 // SplitList splits a list of paths joined by the OS-specific ListSeparator,
183 // usually found in PATH or GOPATH environment variables.
184 // Unlike strings.Split, SplitList returns an empty slice when passed an empty
186 func SplitList(path string) []string {
187 return splitList(path)
190 // Split splits path immediately following the final Separator,
191 // separating it into a directory and file name component.
192 // If there is no Separator in path, Split returns an empty dir
193 // and file set to path.
194 // The returned values have the property that path = dir+file.
195 func Split(path string) (dir, file string) {
196 vol := VolumeName(path)
198 for i >= len(vol) && !os.IsPathSeparator(path[i]) {
201 return path[:i+1], path[i+1:]
204 // Join joins any number of path elements into a single path,
205 // separating them with an OS specific Separator. Empty elements
206 // are ignored. The result is Cleaned. However, if the argument
207 // list is empty or all its elements are empty, Join returns
209 // On Windows, the result will only be a UNC path if the first
210 // non-empty element is a UNC path.
211 func Join(elem ...string) string {
215 // Ext returns the file name extension used by path.
216 // The extension is the suffix beginning at the final dot
217 // in the final element of path; it is empty if there is
219 func Ext(path string) string {
220 for i := len(path) - 1; i >= 0 && !os.IsPathSeparator(path[i]); i-- {
228 // EvalSymlinks returns the path name after the evaluation of any symbolic
230 // If path is relative the result will be relative to the current directory,
231 // unless one of the components is an absolute symbolic link.
232 // EvalSymlinks calls Clean on the result.
233 func EvalSymlinks(path string) (string, error) {
234 return evalSymlinks(path)
237 // Abs returns an absolute representation of path.
238 // If the path is not absolute it will be joined with the current
239 // working directory to turn it into an absolute path. The absolute
240 // path name for a given file is not guaranteed to be unique.
241 // Abs calls Clean on the result.
242 func Abs(path string) (string, error) {
246 func unixAbs(path string) (string, error) {
248 return Clean(path), nil
250 wd, err := os.Getwd()
254 return Join(wd, path), nil
257 // Rel returns a relative path that is lexically equivalent to targpath when
258 // joined to basepath with an intervening separator. That is,
259 // Join(basepath, Rel(basepath, targpath)) is equivalent to targpath itself.
260 // On success, the returned path will always be relative to basepath,
261 // even if basepath and targpath share no elements.
262 // An error is returned if targpath can't be made relative to basepath or if
263 // knowing the current working directory would be necessary to compute it.
264 // Rel calls Clean on the result.
265 func Rel(basepath, targpath string) (string, error) {
266 baseVol := VolumeName(basepath)
267 targVol := VolumeName(targpath)
268 base := Clean(basepath)
269 targ := Clean(targpath)
270 if sameWord(targ, base) {
273 base = base[len(baseVol):]
274 targ = targ[len(targVol):]
278 // Can't use IsAbs - `\a` and `a` are both relative in Windows.
279 baseSlashed := len(base) > 0 && base[0] == Separator
280 targSlashed := len(targ) > 0 && targ[0] == Separator
281 if baseSlashed != targSlashed || !sameWord(baseVol, targVol) {
282 return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath)
284 // Position base[b0:bi] and targ[t0:ti] at the first differing elements.
287 var b0, bi, t0, ti int
289 for bi < bl && base[bi] != Separator {
292 for ti < tl && targ[ti] != Separator {
295 if !sameWord(targ[t0:ti], base[b0:bi]) {
307 if base[b0:bi] == ".." {
308 return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath)
311 // Base elements left. Must go up before going down.
312 seps := strings.Count(base[b0:bl], string(Separator))
317 buf := make([]byte, size)
319 for i := 0; i < seps; i++ {
321 copy(buf[n+1:], "..")
326 copy(buf[n+1:], targ[t0:])
328 return string(buf), nil
330 return targ[t0:], nil
333 // SkipDir is used as a return value from WalkFuncs to indicate that
334 // the directory named in the call is to be skipped. It is not returned
335 // as an error by any function.
336 var SkipDir = errors.New("skip this directory")
338 // WalkFunc is the type of the function called for each file or directory
339 // visited by Walk. The path argument contains the argument to Walk as a
340 // prefix; that is, if Walk is called with "dir", which is a directory
341 // containing the file "a", the walk function will be called with argument
342 // "dir/a". The info argument is the os.FileInfo for the named path.
344 // If there was a problem walking to the file or directory named by path, the
345 // incoming error will describe the problem and the function can decide how
346 // to handle that error (and Walk will not descend into that directory). In the
347 // case of an error, the info argument will be nil. If an error is returned,
348 // processing stops. The sole exception is when the function returns the special
349 // value SkipDir. If the function returns SkipDir when invoked on a directory,
350 // Walk skips the directory's contents entirely. If the function returns SkipDir
351 // when invoked on a non-directory file, Walk skips the remaining files in the
352 // containing directory.
353 type WalkFunc func(path string, info os.FileInfo, err error) error
355 var lstat = os.Lstat // for testing
357 // walk recursively descends path, calling walkFn.
358 func walk(path string, info os.FileInfo, walkFn WalkFunc) error {
360 return walkFn(path, info, nil)
363 names, err := readDirNames(path)
364 err1 := walkFn(path, info, err)
365 // If err != nil, walk can't walk into this directory.
366 // err1 != nil means walkFn want walk to skip this directory or stop walking.
367 // Therefore, if one of err and err1 isn't nil, walk will return.
368 if err != nil || err1 != nil {
369 // The caller's behavior is controlled by the return value, which is decided
370 // by walkFn. walkFn may ignore err and return nil.
371 // If walkFn returns SkipDir, it will be handled by the caller.
372 // So walk should return whatever walkFn returns.
376 for _, name := range names {
377 filename := Join(path, name)
378 fileInfo, err := lstat(filename)
380 if err := walkFn(filename, fileInfo, err); err != nil && err != SkipDir {
384 err = walk(filename, fileInfo, walkFn)
386 if !fileInfo.IsDir() || err != SkipDir {
395 // Walk walks the file tree rooted at root, calling walkFn for each file or
396 // directory in the tree, including root. All errors that arise visiting files
397 // and directories are filtered by walkFn. The files are walked in lexical
398 // order, which makes the output deterministic but means that for very
399 // large directories Walk can be inefficient.
400 // Walk does not follow symbolic links.
401 func Walk(root string, walkFn WalkFunc) error {
402 info, err := os.Lstat(root)
404 err = walkFn(root, nil, err)
406 err = walk(root, info, walkFn)
414 // readDirNames reads the directory named by dirname and returns
415 // a sorted list of directory entries.
416 func readDirNames(dirname string) ([]string, error) {
417 f, err := os.Open(dirname)
421 names, err := f.Readdirnames(-1)
430 // Base returns the last element of path.
431 // Trailing path separators are removed before extracting the last element.
432 // If the path is empty, Base returns ".".
433 // If the path consists entirely of separators, Base returns a single separator.
434 func Base(path string) string {
438 // Strip trailing slashes.
439 for len(path) > 0 && os.IsPathSeparator(path[len(path)-1]) {
440 path = path[0 : len(path)-1]
442 // Throw away volume name
443 path = path[len(VolumeName(path)):]
444 // Find the last element
446 for i >= 0 && !os.IsPathSeparator(path[i]) {
452 // If empty now, it had only slashes.
454 return string(Separator)
459 // Dir returns all but the last element of path, typically the path's directory.
460 // After dropping the final element, Dir calls Clean on the path and trailing
461 // slashes are removed.
462 // If the path is empty, Dir returns ".".
463 // If the path consists entirely of separators, Dir returns a single separator.
464 // The returned path does not end in a separator unless it is the root directory.
465 func Dir(path string) string {
466 vol := VolumeName(path)
468 for i >= len(vol) && !os.IsPathSeparator(path[i]) {
471 dir := Clean(path[len(vol) : i+1])
472 if dir == "." && len(vol) > 2 {
479 // VolumeName returns leading volume name.
480 // Given "C:\foo\bar" it returns "C:" on Windows.
481 // Given "\\host\share\foo" it returns "\\host\share".
482 // On other platforms it returns "".
483 func VolumeName(path string) string {
484 return path[:volumeNameLen(path)]