1 // Copyright 2022 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.
12 func fromFS(path string) (string, error) {
13 if !utf8.ValidString(path) {
14 return "", errInvalidPath
16 for len(path) > 1 && path[0] == '/' && path[1] == '/' {
19 containsSlash := false
20 for p := path; p != ""; {
21 // Find the next path element.
23 for i < len(p) && p[i] != '/' {
26 return "", errInvalidPath
37 if IsReservedName(part) {
38 return "", errInvalidPath
42 // We can't depend on strings, so substitute \ for / manually.
44 for i, b := range buf {
54 // IsReservedName reports if name is a Windows reserved device name.
55 // It does not detect names with an extension, which are also reserved on some Windows versions.
57 // For details, search for PRN in
58 // https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file.
59 func IsReservedName(name string) bool {
60 // Device names can have arbitrary trailing characters following a dot or colon.
62 for i := 0; i < len(base); i++ {
68 // Trailing spaces in the last path element are ignored.
69 for len(base) > 0 && base[len(base)-1] == ' ' {
70 base = base[:len(base)-1]
72 if !isReservedBaseName(base) {
75 if len(base) == len(name) {
78 // The path element is a reserved name with an extension.
79 // Some Windows versions consider this a reserved name,
80 // while others do not. Use FullPath to see if the name is
82 if p, _ := syscall.FullPath(name); len(p) >= 4 && p[:4] == `\\.\` {
88 func isReservedBaseName(name string) bool {
90 switch string([]byte{toUpper(name[0]), toUpper(name[1]), toUpper(name[2])}) {
91 case "CON", "PRN", "AUX", "NUL":
96 switch string([]byte{toUpper(name[0]), toUpper(name[1]), toUpper(name[2])}) {
98 if len(name) == 4 && '1' <= name[3] && name[3] <= '9' {
101 // Superscript ¹, ², and ³ are considered numbers as well.
103 case "\u00b2", "\u00b3", "\u00b9":
110 // Passing CONIN$ or CONOUT$ to CreateFile opens a console handle.
111 // https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#consoles
113 // While CONIN$ and CONOUT$ aren't documented as being files,
114 // they behave the same as CON. For example, ./CONIN$ also opens the console input.
115 if len(name) == 6 && name[5] == '$' && equalFold(name, "CONIN$") {
118 if len(name) == 7 && name[6] == '$' && equalFold(name, "CONOUT$") {
124 func equalFold(a, b string) bool {
125 if len(a) != len(b) {
128 for i := 0; i < len(a); i++ {
129 if toUpper(a[i]) != toUpper(b[i]) {
136 func toUpper(c byte) byte {
137 if 'a' <= c && c <= 'z' {
138 return c - ('a' - 'A')