-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathresolvepath.go
More file actions
90 lines (82 loc) · 2.47 KB
/
Copy pathresolvepath.go
File metadata and controls
90 lines (82 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2009 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 of the Go project.
package main
import (
"path/filepath"
"runtime"
"strings"
)
// osPathToURLPath converts a native absolute filesystem path (as returned by
// filepath.Abs) into the "/"-rooted path convention used internally to
// identify documents, which resolvePath always produces and expects as input.
//
// On POSIX this is a no-op beyond slash conversion, since native absolute
// paths already start with "/". On Windows, native absolute paths start with
// a drive letter (e.g. "C:\foo\bar") with no leading separator, so one is
// added, matching the "file:///C:/foo/bar" URI convention.
func osPathToURLPath(pth string) string {
pth = filepath.ToSlash(pth)
if pth == "" || pth[0] != '/' {
pth = "/" + pth
}
return pth
}
// isWindowsDriveAbs reports whether pth is an internal "/"-rooted path
// wrapping a Windows drive-absolute path, e.g. "/C:/foo/bar".
func isWindowsDriveAbs(pth string) bool {
return len(pth) >= 3 &&
pth[0] == '/' &&
(('a' <= pth[1] && pth[1] <= 'z') || ('A' <= pth[1] && pth[1] <= 'Z')) &&
pth[2] == ':' &&
(len(pth) == 3 || pth[3] == '/')
}
// urlPathToOSPath is the reverse of osPathToURLPath: it turns an internal
// "/"-rooted document path back into a native OS path suitable for os.Open.
//
// On Windows, a path like "/C:/foo/bar" must have its leading "/" stripped
// before the drive letter, or filepath.FromSlash would produce the invalid
// "\C:\foo\bar".
func urlPathToOSPath(pth string) string {
if runtime.GOOS == "windows" && isWindowsDriveAbs(pth) {
pth = pth[1:]
}
return filepath.FromSlash(pth)
}
// resolvePath applies special path segments from refs and applies
// them to base, per RFC 3986.
//
// Copied from package net/url.
func resolvePath(base, ref string) string {
var full string
if ref == "" {
full = base
} else if ref[0] != '/' {
i := strings.LastIndex(base, "/")
full = base[:i+1] + ref
} else {
full = ref
}
if full == "" {
return ""
}
var dst []string
src := strings.Split(full, "/")
for _, elem := range src {
switch elem {
case ".":
// drop
case "..":
if len(dst) > 0 {
dst = dst[:len(dst)-1]
}
default:
dst = append(dst, elem)
}
}
if last := src[len(src)-1]; last == "." || last == ".." {
// Add final slash to the joined path.
dst = append(dst, "")
}
return "/" + strings.TrimPrefix(strings.Join(dst, "/"), "/")
}