Encode checkpoint and debug path identities
This commit is contained in:
@@ -10,6 +10,38 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// EncodePathComponent returns a filesystem-safe, injective representation of
|
||||
// one logical path component.
|
||||
func EncodePathComponent(value string) string {
|
||||
if value == "" {
|
||||
return "%"
|
||||
}
|
||||
|
||||
const hexadecimal = "0123456789ABCDEF"
|
||||
var out strings.Builder
|
||||
for index := 0; index < len(value); index++ {
|
||||
byteValue := value[index]
|
||||
switch {
|
||||
case byteValue >= 'a' && byteValue <= 'z', byteValue >= 'A' && byteValue <= 'Z', byteValue >= '0' && byteValue <= '9', byteValue == '-', byteValue == '_':
|
||||
out.WriteByte(byteValue)
|
||||
case byteValue == '.' && safePathDot(value, index):
|
||||
out.WriteByte(byteValue)
|
||||
default:
|
||||
out.WriteByte('%')
|
||||
out.WriteByte(hexadecimal[byteValue>>4])
|
||||
out.WriteByte(hexadecimal[byteValue&0x0f])
|
||||
}
|
||||
}
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func safePathDot(value string, index int) bool {
|
||||
if value == "." || value == ".." {
|
||||
return false
|
||||
}
|
||||
return (index == 0 || value[index-1] != '.') && (index+1 == len(value) || value[index+1] != '.')
|
||||
}
|
||||
|
||||
func SafePath(root, name string) (string, error) {
|
||||
root = strings.TrimSpace(root)
|
||||
if root == "" {
|
||||
|
||||
Reference in New Issue
Block a user