-
Notifications
You must be signed in to change notification settings - Fork 12
/
path_windows.go
64 lines (57 loc) · 1.94 KB
/
path_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
// Copyright (c) 2023 Tim van der Molen <tim@kariliq.nl>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package main
import (
"strings"
"unicode"
)
// sanitiseFilename sanitises a filename for Windows. The sanitation is based
// on https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file.
func sanitiseFilename(name string) string {
if name == "" {
return "_"
}
// If X is a reserved name, then transform "X" and "X.ext" into "X_"
// and "X_.ext", respectively
rnames := [...]string{
"AUX", "CON", "NUL", "PRN",
"COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
}
i := strings.IndexByte(name, '.')
if i <= 0 {
i = len(name)
}
base, ext := name[:i], name[i:]
for _, rname := range rnames {
if strings.EqualFold(base, rname) {
name = base + "_" + ext
break
}
}
// Handle filenames ending with a space or a dot (this also takes care
// of "." and "..")
if name[len(name)-1] == ' ' || name[len(name)-1] == '.' {
name += "_"
}
// Replace reserved characters
rchars := `"*/:<>?\|`
runes := []rune(name)
for i, r := range runes {
if strings.ContainsRune(rchars, r) || unicode.IsControl(r) {
runes[i] = '_'
}
}
return string(runes)
}