| |
| |
| |
|
|
| package filepathlite |
|
|
| import ( |
| "internal/bytealg" |
| "internal/stringslite" |
| "internal/syscall/windows" |
| "syscall" |
| ) |
|
|
| const ( |
| Separator = '\\' |
| ListSeparator = ';' |
| ) |
|
|
| func IsPathSeparator(c uint8) bool { |
| return c == '\\' || c == '/' |
| } |
|
|
| func isLocal(path string) bool { |
| if path == "" { |
| return false |
| } |
| if IsPathSeparator(path[0]) { |
| |
| return false |
| } |
| if stringslite.IndexByte(path, ':') >= 0 { |
| |
| |
| return false |
| } |
| hasDots := false |
| for p := path; p != ""; { |
| var part string |
| part, p, _ = cutPath(p) |
| if part == "." || part == ".." { |
| hasDots = true |
| } |
| if isReservedName(part) { |
| return false |
| } |
| } |
| if hasDots { |
| path = Clean(path) |
| } |
| if path == ".." || stringslite.HasPrefix(path, `..\`) { |
| return false |
| } |
| return true |
| } |
|
|
| func localize(path string) (string, error) { |
| for i := 0; i < len(path); i++ { |
| switch path[i] { |
| case ':', '\\', 0: |
| return "", errInvalidPath |
| } |
| } |
| containsSlash := false |
| for p := path; p != ""; { |
| |
| var element string |
| i := bytealg.IndexByteString(p, '/') |
| if i < 0 { |
| element = p |
| p = "" |
| } else { |
| containsSlash = true |
| element = p[:i] |
| p = p[i+1:] |
| } |
| if isReservedName(element) { |
| return "", errInvalidPath |
| } |
| } |
| if containsSlash { |
| |
| buf := []byte(path) |
| for i, b := range buf { |
| if b == '/' { |
| buf[i] = '\\' |
| } |
| } |
| path = string(buf) |
| } |
| return path, nil |
| } |
|
|
| |
| |
| |
| |
| |
| func isReservedName(name string) bool { |
| |
| base := name |
| for i := 0; i < len(base); i++ { |
| switch base[i] { |
| case ':', '.': |
| base = base[:i] |
| } |
| } |
| |
| for len(base) > 0 && base[len(base)-1] == ' ' { |
| base = base[:len(base)-1] |
| } |
| if !isReservedBaseName(base) { |
| return false |
| } |
| if len(base) == len(name) { |
| return true |
| } |
| |
| |
| |
| |
| p, err := syscall.UTF16PtrFromString(name) |
| if err != nil { |
| return false |
| } |
| return windows.RtlIsDosDeviceName_U(p) > 0 |
| } |
|
|
| func isReservedBaseName(name string) bool { |
| if len(name) == 3 { |
| switch string([]byte{toUpper(name[0]), toUpper(name[1]), toUpper(name[2])}) { |
| case "CON", "PRN", "AUX", "NUL": |
| return true |
| } |
| } |
| if len(name) >= 4 { |
| switch string([]byte{toUpper(name[0]), toUpper(name[1]), toUpper(name[2])}) { |
| case "COM", "LPT": |
| if len(name) == 4 && '1' <= name[3] && name[3] <= '9' { |
| return true |
| } |
| |
| switch name[3:] { |
| case "\u00b2", "\u00b3", "\u00b9": |
| return true |
| } |
| return false |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| if len(name) == 6 && name[5] == '$' && equalFold(name, "CONIN$") { |
| return true |
| } |
| if len(name) == 7 && name[6] == '$' && equalFold(name, "CONOUT$") { |
| return true |
| } |
| return false |
| } |
|
|
| func equalFold(a, b string) bool { |
| if len(a) != len(b) { |
| return false |
| } |
| for i := 0; i < len(a); i++ { |
| if toUpper(a[i]) != toUpper(b[i]) { |
| return false |
| } |
| } |
| return true |
| } |
|
|
| func toUpper(c byte) byte { |
| if 'a' <= c && c <= 'z' { |
| return c - ('a' - 'A') |
| } |
| return c |
| } |
|
|
| |
| func IsAbs(path string) (b bool) { |
| l := volumeNameLen(path) |
| if l == 0 { |
| return false |
| } |
| |
| if IsPathSeparator(path[0]) && IsPathSeparator(path[1]) { |
| return true |
| } |
| path = path[l:] |
| if path == "" { |
| return false |
| } |
| return IsPathSeparator(path[0]) |
| } |
|
|
| |
| |
| |
| |
| |
| |
| func volumeNameLen(path string) int { |
| switch { |
| case len(path) >= 2 && path[1] == ':': |
| |
| |
| |
| |
| |
| |
| |
| return 2 |
|
|
| case len(path) == 0 || !IsPathSeparator(path[0]): |
| |
| return 0 |
|
|
| case pathHasPrefixFold(path, `\\.\UNC`): |
| |
| |
| |
| |
| |
| return uncLen(path, len(`\\.\UNC\`)) |
|
|
| case pathHasPrefixFold(path, `\\.`) || |
| pathHasPrefixFold(path, `\\?`) || pathHasPrefixFold(path, `\??`): |
| |
| |
| |
| |
| |
| |
| if len(path) == 3 { |
| return 3 |
| } |
| _, rest, ok := cutPath(path[4:]) |
| if !ok { |
| return len(path) |
| } |
| return len(path) - len(rest) - 1 |
|
|
| case len(path) >= 2 && IsPathSeparator(path[1]): |
| |
| return uncLen(path, 2) |
| } |
| return 0 |
| } |
|
|
| |
| |
| |
| func pathHasPrefixFold(s, prefix string) bool { |
| if len(s) < len(prefix) { |
| return false |
| } |
| for i := 0; i < len(prefix); i++ { |
| if IsPathSeparator(prefix[i]) { |
| if !IsPathSeparator(s[i]) { |
| return false |
| } |
| } else if toUpper(prefix[i]) != toUpper(s[i]) { |
| return false |
| } |
| } |
| if len(s) > len(prefix) && !IsPathSeparator(s[len(prefix)]) { |
| return false |
| } |
| return true |
| } |
|
|
| |
| |
| |
| func uncLen(path string, prefixLen int) int { |
| count := 0 |
| for i := prefixLen; i < len(path); i++ { |
| if IsPathSeparator(path[i]) { |
| count++ |
| if count == 2 { |
| return i |
| } |
| } |
| } |
| return len(path) |
| } |
|
|
| |
| func cutPath(path string) (before, after string, found bool) { |
| for i := range path { |
| if IsPathSeparator(path[i]) { |
| return path[:i], path[i+1:], true |
| } |
| } |
| return path, "", false |
| } |
|
|
| |
| |
| func postClean(out *lazybuf) { |
| if out.volLen != 0 || out.buf == nil { |
| return |
| } |
| |
| |
| |
| for _, c := range out.buf { |
| if IsPathSeparator(c) { |
| break |
| } |
| if c == ':' { |
| out.prepend('.', Separator) |
| return |
| } |
| } |
| |
| |
| |
| if len(out.buf) >= 3 && IsPathSeparator(out.buf[0]) && out.buf[1] == '?' && out.buf[2] == '?' { |
| out.prepend(Separator, '.') |
| } |
| } |
|
|