Fix machine volumes with long path and paths with dashes

AppleHV accepts a max 36 bytes for mount tags. Instead of using the fully qualified path for the mount tag, SHA256 the path, and truncate the shasum to 36 bytes.
Also correctly escape dashes in mounted paths.

Signed-off-by: Ashley Cui <acui@redhat.com>
This commit is contained in:
Ashley Cui
2024-04-05 17:53:54 -04:00
parent ba73c604c4
commit a140c74ba4
5 changed files with 40 additions and 23 deletions

View File

@ -428,13 +428,17 @@ func splitString(s string, separators string, flags SplitFlags) ([]string, error
return splitStringAppend(make([]string, 0), s, separators, flags)
}
func charNeedEscape(c rune) bool {
func charNeedEscape(c rune, isPath bool) bool {
if c > 128 {
return false /* unicode is ok */
}
pathRune := (isPath && c == '-') ||
(isPath && c == '/')
return unicode.IsSpace(c) ||
unicode.IsControl(c) ||
pathRune ||
c == '"' ||
c == '\'' ||
c == '\\'
@ -442,18 +446,22 @@ func charNeedEscape(c rune) bool {
func wordNeedEscape(word string) bool {
for _, c := range word {
if charNeedEscape(c) {
if charNeedEscape(c, false) {
return true
}
}
return false
}
func appendEscapeWord(escaped *strings.Builder, word string) {
escaped.WriteRune('"')
for _, c := range word {
if charNeedEscape(c) {
escapeString(escaped, word, false)
escaped.WriteRune('"')
}
func escapeString(escaped *strings.Builder, word string, isPath bool) {
for i, c := range word {
if charNeedEscape(c, isPath) {
switch c {
case '\a':
escaped.WriteString("\\a")
@ -477,6 +485,10 @@ func appendEscapeWord(escaped *strings.Builder, word string) {
escaped.WriteString("\\\"")
case '\'':
escaped.WriteString("'")
case '/':
if isPath && i != 0 {
escaped.WriteString("-")
}
default:
escaped.WriteString(fmt.Sprintf("\\x%.2x", c))
}
@ -484,7 +496,6 @@ func appendEscapeWord(escaped *strings.Builder, word string) {
escaped.WriteRune(c)
}
}
escaped.WriteRune('"')
}
func escapeWords(words []string) string {
@ -500,6 +511,5 @@ func escapeWords(words []string) string {
escaped.WriteString(word)
}
}
return escaped.String()
}

View File

@ -932,3 +932,9 @@ func (f *UnitFile) GetTemplateParts() (string, string) {
}
return parts[0] + "@" + ext, parts[1]
}
func PathEscape(path string) string {
var escaped strings.Builder
escapeString(&escaped, path, true)
return escaped.String()
}