-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added some methods for sanitizing path on different platforms
Signed-off-by: zongz <[email protected]>
- Loading branch information
Showing
5 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package path | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestSanitizePath(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
{ | ||
name: "Path with null character", | ||
input: "test\x00file", | ||
expected: "test_file", | ||
}, | ||
{ | ||
name: "Path without invalid characters", | ||
input: "/usr/local/bin/test", | ||
expected: "/usr/local/bin/test", | ||
}, | ||
} | ||
|
||
if runtime.GOOS == "windows" { | ||
tests = append(tests, struct { | ||
name string | ||
input string | ||
expected string | ||
}{ | ||
name: "Windows style path", | ||
input: "C:\\Program Files\\Test<:>*|", | ||
expected: "C:\\Program Files\\Test______", | ||
}) | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
output := SanitizePath(tt.input) | ||
if output != tt.expected { | ||
t.Errorf("expected %s, got %s", tt.expected, output) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package path | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
var NeedToSanitize map[rune]bool | ||
|
||
func init() { | ||
NeedToSanitize = map[rune]bool{ | ||
'\x00': true, | ||
} | ||
} | ||
|
||
// sanitizePath cleans a path string by removing or replacing invalid Windows file name characters. | ||
func sanitizePath(path string, sanitize sanitizer, toSanitize map[rune]bool) string { | ||
// replace all slashes with backslashes | ||
path = filepath.FromSlash(path) | ||
|
||
// replace all invalid characters | ||
return strings.Map(func(r rune) rune { | ||
if _, isInvalid := toSanitize[r]; isInvalid { | ||
return sanitize(r, toSanitize) | ||
} | ||
return r | ||
}, path) | ||
} | ||
|
||
// sanitizer defined how to handle and replace invalid file name characters. | ||
type sanitizer func(rune, map[rune]bool) rune | ||
|
||
// SanitizePath replaces invalid characters in a Windows path with a placeholder. | ||
func SanitizePath(path string) string { | ||
return sanitizePath(path, func(r rune, invalidChars map[rune]bool) rune { | ||
if _, ok := invalidChars[r]; ok { | ||
return '_' | ||
} | ||
return r | ||
}, NeedToSanitize) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package path | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
var NeedToSanitize map[rune]bool | ||
|
||
func init() { | ||
NeedToSanitize = map[rune]bool{ | ||
'<': true, '>': true, ':': true, '"': true, | ||
'/': true, '\\': true, '|': true, '?': true, '*': true, | ||
} | ||
} | ||
|
||
// sanitizePath cleans a path string by removing or replacing invalid Windows file name characters. | ||
func sanitizePath(path string, sanitize sanitizer, toSanitize map[rune]bool) string { | ||
// replace all slashes with backslashes | ||
path = filepath.FromSlash(path) | ||
|
||
// replace all invalid characters | ||
return strings.Map(func(r rune) rune { | ||
if _, isInvalid := toSanitize[r]; isInvalid { | ||
return sanitize(r, toSanitize) | ||
} | ||
return r | ||
}, path) | ||
} | ||
|
||
// sanitizer defined how to handle and replace invalid file name characters. | ||
type sanitizer func(rune, map[rune]bool) rune | ||
|
||
// SanitizePath replaces invalid characters in a Windows path with a placeholder. | ||
func SanitizePath(path string) string { | ||
volumeName := filepath.VolumeName(path) | ||
// Only sanitize the part of the path after the volume name | ||
sanitized := sanitizePath(path[len(volumeName):], func(r rune, invalidChars map[rune]bool) rune { | ||
if _, ok := invalidChars[r]; ok { | ||
return '_' | ||
} | ||
return r | ||
}, NeedToSanitize) | ||
return volumeName + sanitized | ||
} |