github.com/avfs/avfs@v0.33.1-0.20240303173310-c6ba67c33eb7/vfs_ostype_off.go (about) 1 // 2 // Copyright 2023 The AVFS authors 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 //go:build !avfs_setostype 18 19 package avfs 20 21 import ( 22 "os" 23 "path/filepath" 24 ) 25 26 const buildFeatSetOSType = 0 27 28 // Base returns the last element of path. 29 // Trailing path separators are removed before extracting the last element. 30 // If the path is empty, Base returns ".". 31 // If the path consists entirely of separators, Base returns a single separator. 32 func Base[T VFSBase](_ T, path string) string { 33 return filepath.Base(path) 34 } 35 36 // Clean returns the shortest path name equivalent to path 37 // by purely lexical processing. It applies the following rules 38 // iteratively until no further processing can be done: 39 // 40 // 1. Replace multiple Separator elements with a single one. 41 // 2. Eliminate each . path name element (the current directory). 42 // 3. Eliminate each inner .. path name element (the parent directory) 43 // along with the non-.. element that precedes it. 44 // 4. Eliminate .. elements that begin a rooted path: 45 // that is, replace "/.." by "/" at the beginning of a path, 46 // assuming Separator is '/'. 47 // 48 // The returned path ends in a slash only if it represents a root directory, 49 // such as "/" on Unix or `C:\` on Windows. 50 // 51 // Finally, any occurrences of slash are replaced by Separator. 52 // 53 // If the result of this process is an empty string, Clean 54 // returns the string ".". 55 // 56 // See also Rob Pike, “Lexical File Names in Plan 9 or 57 // Getting Dot-Dot Right,” 58 // https://9p.io/sys/doc/lexnames.html 59 func Clean[T VFSBase](_ T, path string) string { 60 return filepath.Clean(path) 61 } 62 63 // Dir returns all but the last element of path, typically the path's directory. 64 // After dropping the final element, Dir calls Clean on the path and trailing 65 // slashes are removed. 66 // If the path is empty, Dir returns ".". 67 // If the path consists entirely of separators, Dir returns a single separator. 68 // The returned path does not end in a separator unless it is the root directory. 69 func Dir[T VFSBase](_ T, path string) string { 70 return filepath.Dir(path) 71 } 72 73 // FromSlash returns the result of replacing each slash ('/') character 74 // in path with a separator character. Multiple slashes are replaced 75 // by multiple separators. 76 func FromSlash[T VFSBase](_ T, path string) string { 77 return filepath.FromSlash(path) 78 } 79 80 // IsAbs reports whether the path is absolute. 81 func IsAbs[T VFSBase](_ T, path string) bool { 82 return filepath.IsAbs(path) 83 } 84 85 // IsPathSeparator reports whether c is a directory separator character. 86 func IsPathSeparator[T VFSBase](_ T, c uint8) bool { 87 return os.IsPathSeparator(c) 88 } 89 90 // Join joins any number of path elements into a single path, 91 // separating them with an OS specific Separator. Empty elements 92 // are ignored. The result is Cleaned. However, if the argument 93 // list is empty or all its elements are empty, Join returns 94 // an empty string. 95 // On Windows, the result will only be a UNC path if the first 96 // non-empty element is a UNC path. 97 func Join[T VFSBase](_ T, elem ...string) string { 98 return filepath.Join(elem...) 99 } 100 101 // Match reports whether name matches the shell file name pattern. 102 // The pattern syntax is: 103 // 104 // pattern: 105 // { term } 106 // term: 107 // '*' matches any sequence of non-Separator characters 108 // '?' matches any single non-Separator character 109 // '[' [ '^' ] { character-range } ']' 110 // character class (must be non-empty) 111 // c matches character c (c != '*', '?', '\\', '[') 112 // '\\' c matches character c 113 // 114 // character-range: 115 // c matches character c (c != '\\', '-', ']') 116 // '\\' c matches character c 117 // lo '-' hi matches character c for lo <= c <= hi 118 // 119 // Match requires pattern to match all of name, not just a substring. 120 // The only possible returned error is ErrBadPattern, when pattern 121 // is malformed. 122 // 123 // On Windows, escaping is disabled. Instead, '\\' is treated as 124 // path separator. 125 func Match[T VFSBase](_ T, pattern, name string) (matched bool, err error) { 126 return filepath.Match(pattern, name) 127 } 128 129 // Rel returns a relative path that is lexically equivalent to targpath when 130 // joined to basepath with an intervening separator. That is, 131 // Join(basepath, Rel(basepath, targpath)) is equivalent to targpath itself. 132 // On success, the returned path will always be relative to basepath, 133 // even if basepath and targpath share no elements. 134 // An error is returned if targpath can't be made relative to basepath or if 135 // knowing the current working directory would be necessary to compute it. 136 // Rel calls Clean on the result. 137 func Rel[T VFSBase](_ T, basepath, targpath string) (string, error) { 138 return filepath.Rel(basepath, targpath) 139 } 140 141 // Split splits path immediately following the final Separator, 142 // separating it into a directory and file name component. 143 // If there is no Separator in path, Split returns an empty dir 144 // and file set to path. 145 // The returned values have the property that path = dir+file. 146 func Split[T VFSBase](_ T, path string) (dir, file string) { 147 return filepath.Split(path) 148 } 149 150 // ToSlash returns the result of replacing each separator character 151 // in path with a slash ('/') character. Multiple separators are 152 // replaced by multiple slashes. 153 func ToSlash[T VFSBase](_ T, path string) string { 154 return filepath.ToSlash(path) 155 } 156 157 // VolumeNameLen returns length of the leading volume name on Windows. 158 // It returns 0 elsewhere. 159 func VolumeNameLen[T VFSBase](_ T, path string) int { 160 return volumeNameLen(path) 161 }