github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/path/filepath/path.go (about) 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 "io/fs" 17 "os" 18 "sort" 19 "strings" 20 ) 21 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. 26 type lazybuf struct { 27 path string 28 buf []byte 29 w int 30 volAndPath string 31 volLen int 32 } 33 34 func (b *lazybuf) index(i int) byte { 35 if b.buf != nil { 36 return b.buf[i] 37 } 38 return b.path[i] 39 } 40 41 func (b *lazybuf) append(c byte) { 42 if b.buf == nil { 43 if b.w < len(b.path) && b.path[b.w] == c { 44 b.w++ 45 return 46 } 47 b.buf = make([]byte, len(b.path)) 48 copy(b.buf, b.path[:b.w]) 49 } 50 b.buf[b.w] = c 51 b.w++ 52 } 53 54 func (b *lazybuf) string() string { 55 if b.buf == nil { 56 return b.volAndPath[:b.volLen+b.w] 57 } 58 return b.volAndPath[:b.volLen] + string(b.buf[:b.w]) 59 } 60 61 const ( 62 Separator = os.PathSeparator 63 ListSeparator = os.PathListSeparator 64 ) 65 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: 69 // 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 '/'. 77 // 78 // The returned path ends in a slash only if it represents a root directory, 79 // such as "/" on Unix or `C:\` on Windows. 80 // 81 // Finally, any occurrences of slash are replaced by Separator. 82 // 83 // If the result of this process is an empty string, Clean 84 // returns the string ".". 85 // 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 { 90 originalPath := path 91 volLen := volumeNameLen(path) 92 path = path[volLen:] 93 if path == "" { 94 if volLen > 1 && originalPath[1] != ':' { 95 // should be UNC 96 return FromSlash(originalPath) 97 } 98 return originalPath + "." 99 } 100 rooted := os.IsPathSeparator(path[0]) 101 102 // Invariants: 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. 107 n := len(path) 108 out := lazybuf{path: path, volAndPath: originalPath, volLen: volLen} 109 r, dotdot := 0, 0 110 if rooted { 111 out.append(Separator) 112 r, dotdot = 1, 1 113 } 114 115 for r < n { 116 switch { 117 case os.IsPathSeparator(path[r]): 118 // empty path element 119 r++ 120 case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])): 121 // . element 122 r++ 123 case path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])): 124 // .. element: remove to last separator 125 r += 2 126 switch { 127 case out.w > dotdot: 128 // can backtrack 129 out.w-- 130 for out.w > dotdot && !os.IsPathSeparator(out.index(out.w)) { 131 out.w-- 132 } 133 case !rooted: 134 // cannot backtrack, but not rooted, so append .. element. 135 if out.w > 0 { 136 out.append(Separator) 137 } 138 out.append('.') 139 out.append('.') 140 dotdot = out.w 141 } 142 default: 143 // real path element. 144 // add slash if needed 145 if rooted && out.w != 1 || !rooted && out.w != 0 { 146 out.append(Separator) 147 } 148 // copy element 149 for ; r < n && !os.IsPathSeparator(path[r]); r++ { 150 out.append(path[r]) 151 } 152 } 153 } 154 155 // Turn empty string into "." 156 if out.w == 0 { 157 out.append('.') 158 } 159 160 return FromSlash(out.string()) 161 } 162 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 == '/' { 168 return path 169 } 170 return strings.ReplaceAll(path, string(Separator), "/") 171 } 172 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 == '/' { 178 return path 179 } 180 return strings.ReplaceAll(path, "/", string(Separator)) 181 } 182 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 186 // string. 187 func SplitList(path string) []string { 188 return splitList(path) 189 } 190 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) 198 i := len(path) - 1 199 for i >= len(vol) && !os.IsPathSeparator(path[i]) { 200 i-- 201 } 202 return path[:i+1], path[i+1:] 203 } 204 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 209 // an empty string. 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 { 213 return join(elem) 214 } 215 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 219 // no dot. 220 func Ext(path string) string { 221 for i := len(path) - 1; i >= 0 && !os.IsPathSeparator(path[i]); i-- { 222 if path[i] == '.' { 223 return path[i:] 224 } 225 } 226 return "" 227 } 228 229 // EvalSymlinks returns the path name after the evaluation of any symbolic 230 // links. 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) 236 } 237 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) { 244 return abs(path) 245 } 246 247 func unixAbs(path string) (string, error) { 248 if IsAbs(path) { 249 return Clean(path), nil 250 } 251 wd, err := os.Getwd() 252 if err != nil { 253 return "", err 254 } 255 return Join(wd, path), nil 256 } 257 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) { 272 return ".", nil 273 } 274 base = base[len(baseVol):] 275 targ = targ[len(targVol):] 276 if base == "." { 277 base = "" 278 } 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) 284 } 285 // Position base[b0:bi] and targ[t0:ti] at the first differing elements. 286 bl := len(base) 287 tl := len(targ) 288 var b0, bi, t0, ti int 289 for { 290 for bi < bl && base[bi] != Separator { 291 bi++ 292 } 293 for ti < tl && targ[ti] != Separator { 294 ti++ 295 } 296 if !sameWord(targ[t0:ti], base[b0:bi]) { 297 break 298 } 299 if bi < bl { 300 bi++ 301 } 302 if ti < tl { 303 ti++ 304 } 305 b0 = bi 306 t0 = ti 307 } 308 if base[b0:bi] == ".." { 309 return "", errors.New("Rel: can't make " + targpath + " relative to " + basepath) 310 } 311 if b0 != bl { 312 // Base elements left. Must go up before going down. 313 seps := strings.Count(base[b0:bl], string(Separator)) 314 size := 2 + seps*3 315 if tl != t0 { 316 size += 1 + tl - t0 317 } 318 buf := make([]byte, size) 319 n := copy(buf, "..") 320 for i := 0; i < seps; i++ { 321 buf[n] = Separator 322 copy(buf[n+1:], "..") 323 n += 3 324 } 325 if t0 != tl { 326 buf[n] = Separator 327 copy(buf[n+1:], targ[t0:]) 328 } 329 return string(buf), nil 330 } 331 return targ[t0:], nil 332 } 333 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 error = fs.SkipDir 338 339 // WalkFunc is the type of the function called by Walk to visit each each 340 // file or directory. 341 // 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 345 // argument "dir/a". 346 // 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". 351 // 352 // The info argument is the fs.FileInfo for the named path. 353 // 354 // The error result returned by the function controls how Walk continues. 355 // If the function returns the special value SkipDir, Walk skips the 356 // current directory (path if info.IsDir() is true, otherwise path's 357 // parent directory). Otherwise, if the function returns a non-nil error, 358 // Walk stops entirely and returns that error. 359 // 360 // The err argument reports an error related to path, signaling that Walk 361 // will not walk into that directory. The function can decide how to 362 // handle that error; as described earlier, returning the error will 363 // cause Walk to stop walking the entire tree. 364 // 365 // Walk calls the function with a non-nil err argument in two cases. 366 // 367 // First, if an os.Lstat on the root directory or any directory or file 368 // in the tree fails, Walk calls the function with path set to that 369 // directory or file's path, info set to nil, and err set to the error 370 // from os.Lstat. 371 // 372 // Second, if a directory's Readdirnames method fails, Walk calls the 373 // function with path set to the directory's path, info, set to an 374 // fs.FileInfo describing the directory, and err set to the error from 375 // Readdirnames. 376 type WalkFunc func(path string, info fs.FileInfo, err error) error 377 378 var lstat = os.Lstat // for testing 379 380 // walkDir recursively descends path, calling walkDirFn. 381 func walkDir(path string, d fs.DirEntry, walkDirFn fs.WalkDirFunc) error { 382 if err := walkDirFn(path, d, nil); err != nil || !d.IsDir() { 383 if err == SkipDir && d.IsDir() { 384 // Successfully skipped directory. 385 err = nil 386 } 387 return err 388 } 389 390 dirs, err := readDir(path) 391 if err != nil { 392 // Second call, to report ReadDir error. 393 err = walkDirFn(path, d, err) 394 if err != nil { 395 return err 396 } 397 } 398 399 for _, d1 := range dirs { 400 path1 := Join(path, d1.Name()) 401 if err := walkDir(path1, d1, walkDirFn); err != nil { 402 if err == SkipDir { 403 break 404 } 405 return err 406 } 407 } 408 return nil 409 } 410 411 // walk recursively descends path, calling walkFn. 412 func walk(path string, info fs.FileInfo, walkFn WalkFunc) error { 413 if !info.IsDir() { 414 return walkFn(path, info, nil) 415 } 416 417 names, err := readDirNames(path) 418 err1 := walkFn(path, info, err) 419 // If err != nil, walk can't walk into this directory. 420 // err1 != nil means walkFn want walk to skip this directory or stop walking. 421 // Therefore, if one of err and err1 isn't nil, walk will return. 422 if err != nil || err1 != nil { 423 // The caller's behavior is controlled by the return value, which is decided 424 // by walkFn. walkFn may ignore err and return nil. 425 // If walkFn returns SkipDir, it will be handled by the caller. 426 // So walk should return whatever walkFn returns. 427 return err1 428 } 429 430 for _, name := range names { 431 filename := Join(path, name) 432 fileInfo, err := lstat(filename) 433 if err != nil { 434 if err := walkFn(filename, fileInfo, err); err != nil && err != SkipDir { 435 return err 436 } 437 } else { 438 err = walk(filename, fileInfo, walkFn) 439 if err != nil { 440 if !fileInfo.IsDir() || err != SkipDir { 441 return err 442 } 443 } 444 } 445 } 446 return nil 447 } 448 449 // WalkDir walks the file tree rooted at root, calling fn for each file or 450 // directory in the tree, including root. 451 // 452 // All errors that arise visiting files and directories are filtered by fn: 453 // see the fs.WalkDirFunc documentation for details. 454 // 455 // The files are walked in lexical order, which makes the output deterministic 456 // but requires WalkDir to read an entire directory into memory before proceeding 457 // to walk that directory. 458 // 459 // WalkDir does not follow symbolic links. 460 func WalkDir(root string, fn fs.WalkDirFunc) error { 461 info, err := os.Lstat(root) 462 if err != nil { 463 err = fn(root, nil, err) 464 } else { 465 err = walkDir(root, &statDirEntry{info}, fn) 466 } 467 if err == SkipDir { 468 return nil 469 } 470 return err 471 } 472 473 type statDirEntry struct { 474 info fs.FileInfo 475 } 476 477 func (d *statDirEntry) Name() string { return d.info.Name() } 478 func (d *statDirEntry) IsDir() bool { return d.info.IsDir() } 479 func (d *statDirEntry) Type() fs.FileMode { return d.info.Mode().Type() } 480 func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil } 481 482 // Walk walks the file tree rooted at root, calling fn for each file or 483 // directory in the tree, including root. 484 // 485 // All errors that arise visiting files and directories are filtered by fn: 486 // see the WalkFunc documentation for details. 487 // 488 // The files are walked in lexical order, which makes the output deterministic 489 // but requires Walk to read an entire directory into memory before proceeding 490 // to walk that directory. 491 // 492 // Walk does not follow symbolic links. 493 // 494 // Walk is less efficient than WalkDir, introduced in Go 1.16, 495 // which avoids calling os.Lstat on every visited file or directory. 496 func Walk(root string, fn WalkFunc) error { 497 info, err := os.Lstat(root) 498 if err != nil { 499 err = fn(root, nil, err) 500 } else { 501 err = walk(root, info, fn) 502 } 503 if err == SkipDir { 504 return nil 505 } 506 return err 507 } 508 509 // readDir reads the directory named by dirname and returns 510 // a sorted list of directory entries. 511 func readDir(dirname string) ([]fs.DirEntry, error) { 512 f, err := os.Open(dirname) 513 if err != nil { 514 return nil, err 515 } 516 dirs, err := f.ReadDir(-1) 517 f.Close() 518 if err != nil { 519 return nil, err 520 } 521 sort.Slice(dirs, func(i, j int) bool { return dirs[i].Name() < dirs[j].Name() }) 522 return dirs, nil 523 } 524 525 // readDirNames reads the directory named by dirname and returns 526 // a sorted list of directory entry names. 527 func readDirNames(dirname string) ([]string, error) { 528 f, err := os.Open(dirname) 529 if err != nil { 530 return nil, err 531 } 532 names, err := f.Readdirnames(-1) 533 f.Close() 534 if err != nil { 535 return nil, err 536 } 537 sort.Strings(names) 538 return names, nil 539 } 540 541 // Base returns the last element of path. 542 // Trailing path separators are removed before extracting the last element. 543 // If the path is empty, Base returns ".". 544 // If the path consists entirely of separators, Base returns a single separator. 545 func Base(path string) string { 546 if path == "" { 547 return "." 548 } 549 // Strip trailing slashes. 550 for len(path) > 0 && os.IsPathSeparator(path[len(path)-1]) { 551 path = path[0 : len(path)-1] 552 } 553 // Throw away volume name 554 path = path[len(VolumeName(path)):] 555 // Find the last element 556 i := len(path) - 1 557 for i >= 0 && !os.IsPathSeparator(path[i]) { 558 i-- 559 } 560 if i >= 0 { 561 path = path[i+1:] 562 } 563 // If empty now, it had only slashes. 564 if path == "" { 565 return string(Separator) 566 } 567 return path 568 } 569 570 // Dir returns all but the last element of path, typically the path's directory. 571 // After dropping the final element, Dir calls Clean on the path and trailing 572 // slashes are removed. 573 // If the path is empty, Dir returns ".". 574 // If the path consists entirely of separators, Dir returns a single separator. 575 // The returned path does not end in a separator unless it is the root directory. 576 func Dir(path string) string { 577 vol := VolumeName(path) 578 i := len(path) - 1 579 for i >= len(vol) && !os.IsPathSeparator(path[i]) { 580 i-- 581 } 582 dir := Clean(path[len(vol) : i+1]) 583 if dir == "." && len(vol) > 2 { 584 // must be UNC 585 return vol 586 } 587 return vol + dir 588 } 589 590 // VolumeName returns leading volume name. 591 // Given "C:\foo\bar" it returns "C:" on Windows. 592 // Given "\\host\share\foo" it returns "\\host\share". 593 // On other platforms it returns "". 594 func VolumeName(path string) string { 595 return path[:volumeNameLen(path)] 596 }