From 0957d8d6ff0c9db29962bea4246b08e26a62392d Mon Sep 17 00:00:00 2001 From: Fredrik Skogman Date: Wed, 21 Feb 2024 15:26:28 +0100 Subject: [PATCH 1/4] Use filepath.Join when combining filesystem components Signed-off-by: Fredrik Skogman --- metadata/updater/updater.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metadata/updater/updater.go b/metadata/updater/updater.go index ac7b807b..678f31bc 100644 --- a/metadata/updater/updater.go +++ b/metadata/updater/updater.go @@ -223,6 +223,7 @@ func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath } else { targetBaseURL = ensureTrailingSlash(targetBaseURL) } + targetFilePath := targetFile.Path consistentSnapshot := update.trusted.Root.Signed.ConsistentSnapshot if consistentSnapshot && update.cfg.PrefixTargetsWithHash { @@ -250,7 +251,6 @@ func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath if err != nil { return "", nil, err } - // do not persist the target file if cache is disabled if !update.cfg.DisableLocalCache { err = os.WriteFile(filePath, data, 0644) @@ -674,7 +674,7 @@ func (update *Updater) generateTargetFilePath(tf *metadata.TargetFiles) (string, 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)) + return filepath.Join(update.cfg.LocalTargetsDir, url.QueryEscape(tf.Path)), nil } // loadLocalMetadata reads a local .json file and returns its bytes From 4389b5eb21aeb7d9323d3b7d10fdaf25cefd54f4 Mon Sep 17 00:00:00 2001 From: Fredrik Skogman Date: Wed, 21 Feb 2024 16:33:01 +0100 Subject: [PATCH 2/4] Added back empty line that was removed by mistake Signed-off-by: Fredrik Skogman --- metadata/updater/updater.go | 1 + 1 file changed, 1 insertion(+) diff --git a/metadata/updater/updater.go b/metadata/updater/updater.go index 678f31bc..192e5032 100644 --- a/metadata/updater/updater.go +++ b/metadata/updater/updater.go @@ -251,6 +251,7 @@ func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath if err != nil { return "", nil, err } + // do not persist the target file if cache is disabled if !update.cfg.DisableLocalCache { err = os.WriteFile(filePath, data, 0644) From 484e95dc84e3daf7ee6a0933e159a257126670d0 Mon Sep 17 00:00:00 2001 From: Fredrik Skogman Date: Wed, 21 Feb 2024 16:52:00 +0100 Subject: [PATCH 3/4] Always use slash (/) when fetching remote files Signed-off-by: Fredrik Skogman --- metadata/updater/updater.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/metadata/updater/updater.go b/metadata/updater/updater.go index 192e5032..d478ad32 100644 --- a/metadata/updater/updater.go +++ b/metadata/updater/updater.go @@ -225,6 +225,7 @@ func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath } targetFilePath := targetFile.Path + targetRemotePath := targetFilePath consistentSnapshot := update.trusted.Root.Signed.ConsistentSnapshot if consistentSnapshot && update.cfg.PrefixTargetsWithHash { hashes := "" @@ -237,12 +238,14 @@ func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath if !ok { // . targetFilePath = fmt.Sprintf("%s.%s", hashes, dirName) + targetRemotePath = targetFilePath } else { // /. targetFilePath = filepath.Join(dirName, fmt.Sprintf("%s.%s", hashes, baseName)) + targetRemotePath = fmt.Sprintf("%s/%s.%s", dirName, hashes, baseName) } } - fullURL := fmt.Sprintf("%s%s", targetBaseURL, targetFilePath) + fullURL := fmt.Sprintf("%s%s", targetBaseURL, targetRemotePath) data, err := update.cfg.Fetcher.DownloadFile(fullURL, targetFile.Length, time.Second*15) if err != nil { return "", nil, err From 208041c76a46ae8e79b63c0dc721c76a07f1eb75 Mon Sep 17 00:00:00 2001 From: Fredrik Skogman Date: Wed, 21 Feb 2024 16:56:53 +0100 Subject: [PATCH 4/4] Avoid uneccesary assignment Signed-off-by: Fredrik Skogman --- metadata/updater/updater.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/metadata/updater/updater.go b/metadata/updater/updater.go index d478ad32..918131c9 100644 --- a/metadata/updater/updater.go +++ b/metadata/updater/updater.go @@ -237,11 +237,9 @@ func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath dirName, baseName, ok := strings.Cut(targetFilePath, "/") if !ok { // . - targetFilePath = fmt.Sprintf("%s.%s", hashes, dirName) - targetRemotePath = targetFilePath + targetRemotePath = fmt.Sprintf("%s.%s", hashes, dirName) } else { // /. - targetFilePath = filepath.Join(dirName, fmt.Sprintf("%s.%s", hashes, baseName)) targetRemotePath = fmt.Sprintf("%s/%s.%s", dirName, hashes, baseName) } }