types/model: make Name.Filepath substitute colons in host with ("%")

This makes the filepath legal on all supported platforms.

Fixes #4088
This commit is contained in:
Blake Mizerany
2024-05-02 14:36:46 -07:00
parent e9ae607ece
commit 61b287cf25
5 changed files with 112 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package server
import (
"os"
"path/filepath"
"runtime"
"strings"
)
@@ -24,3 +25,44 @@ func fixBlobs(dir string) error {
return nil
})
}
// fixManifests walks the provided dir and replaces (":") to ("%") for all
// manifest files on non-Windows systems.
func fixManifests(dir string) error {
if runtime.GOOS == "windows" {
return nil
}
return filepath.Walk(dir, func(oldPath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
var partNum int
newPath := []byte(oldPath)
for i := len(newPath) - 1; i >= 0; i-- {
if partNum > 3 {
break
}
if partNum == 3 {
if newPath[i] == ':' {
newPath[i] = '%'
break
}
continue
}
if newPath[i] == '/' {
partNum++
}
}
newDir, _ := filepath.Split(string(newPath))
if err := os.MkdirAll(newDir, 0o755); err != nil {
return err
}
return os.Rename(oldPath, string(newPath))
})
}