Skip to content

Commit

Permalink
fix some egregious errors in mirror handling and workaround the doubl…
Browse files Browse the repository at this point in the history
…e-slash 404 bug on some mirrors
  • Loading branch information
neutralinsomniac committed Feb 3, 2021
1 parent fb1957d commit 40c11ed
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
34 changes: 17 additions & 17 deletions cmd/obsdpkgup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,20 +166,28 @@ func getSystemInfo() SysInfo {
return sysInfo
}

var protocolRe = regexp.MustCompile(`://`)
var repeatingSlashRe = regexp.MustCompile(`/+`)

func replaceMirrorVars(mirror string, sysInfo SysInfo) string {
if sysInfo.snapshot {
mirror = strings.ReplaceAll(mirror, "%m", "/pub/OpenBSD/%c/packages/%a/")
} else {
mirror = strings.ReplaceAll(mirror, "%m", "/pub/OpenBSD/%c/packages-stable/%a/")
}
mirror = strings.ReplaceAll(mirror, "%a", sysInfo.arch)
mirror = strings.ReplaceAll(mirror, "%v", sysInfo.version)
mirror = strings.ReplaceAll(mirror, "%m", "/pub/OpenBSD/%c/packages/%a/")

if sysInfo.snapshot {
mirror = strings.ReplaceAll(mirror, "%c", "snapshots")
} else {
mirror = strings.ReplaceAll(mirror, "%c/packages", "%c/packages-stable")
mirror = strings.ReplaceAll(mirror, "%c", sysInfo.version)
}

mirror = strings.ReplaceAll(mirror, "%a", sysInfo.arch)
mirror = strings.ReplaceAll(mirror, "%v", sysInfo.version)

// strip duplicate /'s to work around a bug on some of the mirrors
s := protocolRe.Split(mirror, -1)
s[1] = repeatingSlashRe.ReplaceAllString(s[1], "/")

mirror = strings.Join(s, "://")

return mirror
}

Expand All @@ -202,19 +210,11 @@ func getMirror() string {
installurlBytes, err := ioutil.ReadFile("/etc/installurl")
if err == nil {
installurl := strings.TrimSpace(string(installurlBytes))
if sysInfo.snapshot {
return replaceMirrorVars(fmt.Sprintf("%s/%%c/packages/%%a/", installurl), sysInfo)
} else {
return replaceMirrorVars(fmt.Sprintf("%s/%%c/packages-stable/%%a/", installurl), sysInfo)
}
return replaceMirrorVars(fmt.Sprintf("%s/%%c/packages/%%a/", installurl), sysInfo)
}

// finally, fall back to cdn
if sysInfo.snapshot {
return replaceMirrorVars("https://cdn.openbsd.org/pub/OpenBSD/%%c/packages/%%a/", sysInfo)
} else {
return replaceMirrorVars("https://cdn.openbsd.org/pub/OpenBSD/%%c/packages-stable/%%a/", sysInfo)
}
return replaceMirrorVars("https://cdn.openbsd.org/pub/OpenBSD/%c/packages/%a/", sysInfo)
}

var pkgpathVersionRE = regexp.MustCompile(`^.*/.*/([^ ,]+).*$`)
Expand Down
2 changes: 1 addition & 1 deletion openbsd/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func GetIndexTxt(mirror string) (string, error) {
indexUrl := fmt.Sprintf("%s/index.txt", mirror)
indexUrl := fmt.Sprintf("%sindex.txt", mirror)
resp, err := http.Get(indexUrl)
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions openbsd/quirks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func GetQuirksSignifyBlockFromIndex(baseUrl, index string) (string, error) {
s := strings.Fields(line)
pkgName := s[9]
if strings.HasPrefix(pkgName, "quirks-") {
url := fmt.Sprintf("%s/%s", baseUrl, pkgName)
url := fmt.Sprintf("%s%s", baseUrl, pkgName)
resp, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("error fetching quirks (%s): %s", url, err.Error())
Expand All @@ -32,7 +32,7 @@ func GetQuirksSignifyBlockFromIndex(baseUrl, index string) (string, error) {

return gz.Comment, nil
case 404:
return "", fmt.Errorf("404 while downloading quirks: %s\n", url)
return "", fmt.Errorf("404 while downloading quirks: \"%s\"\n", url)
default:
return "", fmt.Errorf("unexpected HTTP response (%d) while downloading quirks: %s\n", resp.StatusCode, url)
}
Expand Down

0 comments on commit 40c11ed

Please sign in to comment.