|
| 1 | +package filepathlite |
| 2 | + |
| 3 | +func IsPathSeparator(c uint8) bool { |
| 4 | + return c == '\\' || c == '/' |
| 5 | +} |
| 6 | + |
| 7 | +// IsAbs reports whether the path is absolute. |
| 8 | +func IsAbs(path string) (b bool) { |
| 9 | + l := volumeNameLen(path) |
| 10 | + if l == 0 { |
| 11 | + return false |
| 12 | + } |
| 13 | + // If the volume name starts with a double slash, this is an absolute path. |
| 14 | + if IsPathSeparator(path[0]) && IsPathSeparator(path[1]) { |
| 15 | + return true |
| 16 | + } |
| 17 | + path = path[l:] |
| 18 | + if path == "" { |
| 19 | + return false |
| 20 | + } |
| 21 | + return IsPathSeparator(path[0]) |
| 22 | +} |
| 23 | + |
| 24 | +// volumeNameLen returns length of the leading volume name on Windows. |
| 25 | +// It returns 0 elsewhere. |
| 26 | +// |
| 27 | +// See: |
| 28 | +// https://learn.microsoft.com/en-us/dotnet/standard/io/file-path-formats |
| 29 | +// https://googleprojectzero.blogspot.com/2016/02/the-definitive-guide-on-win32-to-nt.html |
| 30 | +func volumeNameLen(path string) int { |
| 31 | + switch { |
| 32 | + case len(path) >= 2 && path[1] == ':': |
| 33 | + // Path starts with a drive letter. |
| 34 | + // |
| 35 | + // Not all Windows functions necessarily enforce the requirement that |
| 36 | + // drive letters be in the set A-Z, and we don't try to here. |
| 37 | + // |
| 38 | + // We don't handle the case of a path starting with a non-ASCII character, |
| 39 | + // in which case the "drive letter" might be multiple bytes long. |
| 40 | + return 2 |
| 41 | + |
| 42 | + case len(path) == 0 || !IsPathSeparator(path[0]): |
| 43 | + // Path does not have a volume component. |
| 44 | + return 0 |
| 45 | + |
| 46 | + case pathHasPrefixFold(path, `\\.\UNC`): |
| 47 | + // We're going to treat the UNC host and share as part of the volume |
| 48 | + // prefix for historical reasons, but this isn't really principled; |
| 49 | + // Windows's own GetFullPathName will happily remove the first |
| 50 | + // component of the path in this space, converting |
| 51 | + // \\.\unc\a\b\..\c into \\.\unc\a\c. |
| 52 | + return uncLen(path, len(`\\.\UNC\`)) |
| 53 | + |
| 54 | + case pathHasPrefixFold(path, `\\.`) || |
| 55 | + pathHasPrefixFold(path, `\\?`) || pathHasPrefixFold(path, `\??`): |
| 56 | + // Path starts with \\.\, and is a Local Device path; or |
| 57 | + // path starts with \\?\ or \??\ and is a Root Local Device path. |
| 58 | + // |
| 59 | + // We treat the next component after the \\.\ prefix as |
| 60 | + // part of the volume name, which means Clean(`\\?\c:\`) |
| 61 | + // won't remove the trailing \. (See #64028.) |
| 62 | + if len(path) == 3 { |
| 63 | + return 3 // exactly \\. |
| 64 | + } |
| 65 | + _, rest, ok := cutPath(path[4:]) |
| 66 | + if !ok { |
| 67 | + return len(path) |
| 68 | + } |
| 69 | + return len(path) - len(rest) - 1 |
| 70 | + |
| 71 | + case len(path) >= 2 && IsPathSeparator(path[1]): |
| 72 | + // Path starts with \\, and is a UNC path. |
| 73 | + return uncLen(path, 2) |
| 74 | + } |
| 75 | + return 0 |
| 76 | +} |
| 77 | + |
| 78 | +// pathHasPrefixFold tests whether the path s begins with prefix, |
| 79 | +// ignoring case and treating all path separators as equivalent. |
| 80 | +// If s is longer than prefix, then s[len(prefix)] must be a path separator. |
| 81 | +func pathHasPrefixFold(s, prefix string) bool { |
| 82 | + if len(s) < len(prefix) { |
| 83 | + return false |
| 84 | + } |
| 85 | + for i := 0; i < len(prefix); i++ { |
| 86 | + if IsPathSeparator(prefix[i]) { |
| 87 | + if !IsPathSeparator(s[i]) { |
| 88 | + return false |
| 89 | + } |
| 90 | + } else if toUpper(prefix[i]) != toUpper(s[i]) { |
| 91 | + return false |
| 92 | + } |
| 93 | + } |
| 94 | + if len(s) > len(prefix) && !IsPathSeparator(s[len(prefix)]) { |
| 95 | + return false |
| 96 | + } |
| 97 | + return true |
| 98 | +} |
| 99 | + |
| 100 | +// uncLen returns the length of the volume prefix of a UNC path. |
| 101 | +// prefixLen is the prefix prior to the start of the UNC host; |
| 102 | +// for example, for "//host/share", the prefixLen is len("//")==2. |
| 103 | +func uncLen(path string, prefixLen int) int { |
| 104 | + count := 0 |
| 105 | + for i := prefixLen; i < len(path); i++ { |
| 106 | + if IsPathSeparator(path[i]) { |
| 107 | + count++ |
| 108 | + if count == 2 { |
| 109 | + return i |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return len(path) |
| 114 | +} |
| 115 | + |
| 116 | +// cutPath slices path around the first path separator. |
| 117 | +func cutPath(path string) (before, after string, found bool) { |
| 118 | + for i := range path { |
| 119 | + if IsPathSeparator(path[i]) { |
| 120 | + return path[:i], path[i+1:], true |
| 121 | + } |
| 122 | + } |
| 123 | + return path, "", false |
| 124 | +} |
| 125 | + |
| 126 | +func toUpper(c byte) byte { |
| 127 | + if 'a' <= c && c <= 'z' { |
| 128 | + return c - ('a' - 'A') |
| 129 | + } |
| 130 | + return c |
| 131 | +} |
0 commit comments