/
windows.go
91 lines (84 loc) · 1.8 KB
/
windows.go
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
91
// Copyright 2020 cloudeng llc. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package cloudpath
import (
"strings"
)
func isWindowsDrive(p string) (string, bool) {
if len(p) == 0 {
return "", false
}
drive := p[0]
if drive >= 'A' && drive <= 'Z' || drive >= 'a' && drive <= 'z' {
if len(p) >= 2 && p[1] == ':' {
return string(drive), true
}
}
return "", false
}
func fileURIWindows(o, p string) Match {
if len(p) == 0 {
return Match{}
}
host, rest, drive := parseFileURI(p)
if len(drive) == 0 || len(rest) == 0 {
return Match{}
}
return Match{
Matched: o,
Scheme: WindowsFileSystem,
Separator: '/',
Host: host,
Volume: drive,
Path: rest,
Key: rest[2:],
Local: true,
}
}
// WindowsMatcher implements Matcher for Windows filenames. It returns
// WindowsFileSystem for its scheme result.
func WindowsMatcher(p string) Match {
if len(p) == 0 {
return Match{}
}
m := Match{
Matched: p,
Scheme: WindowsFileSystem,
Host: "",
Separator: '\\',
Local: true,
}
if len(p) >= 7 && p[:7] == "file://" {
return fileURIWindows(p, p[7:])
}
// extended length names
p = strings.TrimPrefix(p, `\\?`)
if drive, ok := isWindowsDrive(p); ok {
m.Volume = drive
m.Path = p
m.Key = p[2:]
return m
}
if len(p) < 2 || strings.Index(p, `\`) < -1 {
// no backslashes so there's no way to tell.
return Match{}
}
if !strings.HasPrefix(p, `\\`) {
return Match{}
}
// UNC format: \\server\share\path
parts := strings.Split(strings.TrimSuffix(p[2:], `\`), `\`)
switch len(parts) {
default:
m.Path = `\` + strings.Join(parts[2:], `\`)
fallthrough
case 2:
m.Volume = parts[1]
fallthrough
case 1:
m.Host = parts[0]
}
m.Key = m.Path
return m
}