2012-03-30 23:27:11 +02:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package filepath
|
|
|
|
|
|
|
|
import (
|
2016-07-22 20:15:38 +02:00
|
|
|
"strings"
|
2012-03-30 23:27:11 +02:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
// normVolumeName is like VolumeName, but makes drive letter upper case.
|
|
|
|
// result of EvalSymlinks must be unique, so we have
|
|
|
|
// EvalSymlinks(`c:\a`) == EvalSymlinks(`C:\a`).
|
|
|
|
func normVolumeName(path string) string {
|
|
|
|
volume := VolumeName(path)
|
|
|
|
|
|
|
|
if len(volume) > 2 { // isUNC
|
|
|
|
return volume
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.ToUpper(volume)
|
|
|
|
}
|
|
|
|
|
2016-09-10 15:14:00 +02:00
|
|
|
// normBase returns the last element of path with correct case.
|
2016-07-22 20:15:38 +02:00
|
|
|
func normBase(path string) (string, error) {
|
|
|
|
p, err := syscall.UTF16PtrFromString(path)
|
2012-10-23 06:31:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2012-03-31 00:36:44 +02:00
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
var data syscall.Win32finddata
|
|
|
|
|
|
|
|
h, err := syscall.FindFirstFile(p, &data)
|
2012-10-23 06:31:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
syscall.FindClose(h)
|
|
|
|
|
|
|
|
return syscall.UTF16ToString(data.FileName[:]), nil
|
|
|
|
}
|
|
|
|
|
2019-01-18 20:04:36 +01:00
|
|
|
// baseIsDotDot reports whether the last element of path is "..".
|
2016-09-10 15:14:00 +02:00
|
|
|
// The given path should be 'Clean'-ed in advance.
|
|
|
|
func baseIsDotDot(path string) bool {
|
|
|
|
i := strings.LastIndexByte(path, Separator)
|
|
|
|
return path[i+1:] == ".."
|
|
|
|
}
|
|
|
|
|
2017-01-14 01:05:42 +01:00
|
|
|
// toNorm returns the normalized path that is guaranteed to be unique.
|
2016-09-10 15:14:00 +02:00
|
|
|
// It should accept the following formats:
|
|
|
|
// * UNC paths (e.g \\server\share\foo\bar)
|
|
|
|
// * absolute paths (e.g C:\foo\bar)
|
|
|
|
// * relative paths begin with drive letter (e.g C:foo\bar, C:..\foo\bar, C:.., C:.)
|
|
|
|
// * relative paths begin with '\' (e.g \foo\bar)
|
|
|
|
// * relative paths begin without '\' (e.g foo\bar, ..\foo\bar, .., .)
|
|
|
|
// The returned normalized path will be in the same form (of 5 listed above) as the input path.
|
|
|
|
// If two paths A and B are indicating the same file with the same format, toNorm(A) should be equal to toNorm(B).
|
|
|
|
// The normBase parameter should be equal to the normBase func, except for in tests. See docs on the normBase func.
|
|
|
|
func toNorm(path string, normBase func(string) (string, error)) (string, error) {
|
2016-07-22 20:15:38 +02:00
|
|
|
if path == "" {
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
path = Clean(path)
|
|
|
|
|
|
|
|
volume := normVolumeName(path)
|
|
|
|
path = path[len(volume):]
|
|
|
|
|
|
|
|
// skip special cases
|
2020-12-23 18:57:37 +01:00
|
|
|
if path == "" || path == "." || path == `\` {
|
2016-07-22 20:15:38 +02:00
|
|
|
return volume + path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var normPath string
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
for {
|
2016-09-10 15:14:00 +02:00
|
|
|
if baseIsDotDot(path) {
|
|
|
|
normPath = path + `\` + normPath
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := normBase(volume + path)
|
2012-03-30 23:27:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
|
|
|
|
normPath = name + `\` + normPath
|
|
|
|
|
|
|
|
i := strings.LastIndexByte(path, Separator)
|
|
|
|
if i == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if i == 0 { // `\Go` or `C:\Go`
|
|
|
|
normPath = `\` + normPath
|
|
|
|
|
|
|
|
break
|
2015-10-31 01:59:47 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
|
|
|
|
path = path[:i]
|
2012-03-30 23:27:11 +02:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
|
|
|
|
normPath = normPath[:len(normPath)-1] // remove trailing '\'
|
|
|
|
|
|
|
|
return volume + normPath, nil
|
2012-03-31 00:36:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func evalSymlinks(path string) (string, error) {
|
2018-01-09 02:23:08 +01:00
|
|
|
newpath, err := walkSymlinks(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
newpath, err = toNorm(newpath, normBase)
|
2015-01-15 01:27:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-09-06 20:12:46 +02:00
|
|
|
return newpath, nil
|
2012-03-30 23:27:11 +02:00
|
|
|
}
|