From c65dab4ab0256d66c75dc7e283c6e46568c8426c Mon Sep 17 00:00:00 2001 From: Radoslav Dimitrov Date: Wed, 7 Feb 2024 01:13:30 +0200 Subject: [PATCH] Use correct path separators for URL and target files that are formatted like name/target Signed-off-by: Radoslav Dimitrov --- metadata/fetcher/fetcher.go | 4 ++-- metadata/updater/updater.go | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/metadata/fetcher/fetcher.go b/metadata/fetcher/fetcher.go index 49234722..bffa799a 100644 --- a/metadata/fetcher/fetcher.go +++ b/metadata/fetcher/fetcher.go @@ -56,7 +56,7 @@ func (d *DefaultFetcher) DownloadFile(urlPath string, maxLength int64, timeout t } defer res.Body.Close() // Handle HTTP status codes. - if res.StatusCode == http.StatusNotFound || res.StatusCode == http.StatusForbidden || res.StatusCode != http.StatusOK { + if res.StatusCode != http.StatusOK { return nil, &metadata.ErrDownloadHTTP{StatusCode: res.StatusCode, URL: urlPath} } var length int64 @@ -74,7 +74,7 @@ func (d *DefaultFetcher) DownloadFile(urlPath string, maxLength int64, timeout t // Although the size has been checked above, use a LimitReader in case // the reported size is inaccurate, or size is -1 which indicates an // unknown length. We read maxLength + 1 in order to check if the read data - // surpased our set limit. + // surpassed our set limit. data, err := io.ReadAll(io.LimitReader(res.Body, maxLength+1)) if err != nil { return nil, err diff --git a/metadata/updater/updater.go b/metadata/updater/updater.go index 2ce43162..2f1e2749 100644 --- a/metadata/updater/updater.go +++ b/metadata/updater/updater.go @@ -239,7 +239,7 @@ func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath targetFilePath = fmt.Sprintf("%s.%s", hashes, dirName) } else { // /. - targetFilePath = filepath.Join(dirName, fmt.Sprintf("%s.%s", hashes, baseName)) + targetFilePath = fmt.Sprintf("%s/%s.%s", dirName, hashes, baseName) } } fullURL := fmt.Sprintf("%s%s", targetBaseURL, targetFilePath) @@ -676,8 +676,9 @@ func (update *Updater) generateTargetFilePath(tf *metadata.TargetFiles) (string, if update.cfg.LocalTargetsDir == "" && !update.cfg.DisableLocalCache { return "", &metadata.ErrValue{Msg: "LocalTargetsDir must be set if filepath is not given"} } - // Use URL encoded target path as filename - return url.JoinPath(update.cfg.LocalTargetsDir, url.QueryEscape(tf.Path)) + // Use URL encoded target path as filename (e.g. "foo/bar" -> "foo%2Fbar") + // Normalize the path separator to the OS specific one to avoid failures on Windows + return url.JoinPath(update.cfg.LocalTargetsDir, url.QueryEscape(filepath.FromSlash(tf.Path))) } // loadLocalMetadata reads a local .json file and returns its bytes