Skip to content

Commit

Permalink
Create the tarball in the download function
Browse files Browse the repository at this point in the history
Well, it might now be doing more than it should,
But WSL imposes the precondition that nobody else holds the rootfs file open when registering the distro instance.
So we must ensure the file is closed by the time we call import.
This is the most readable way to do so IMHO.

We could have wrapped the file creation and the call to download in a closure. It would be more compliant to SRP,
but less readable.
  • Loading branch information
CarlosNihelton committed Jul 18, 2024
1 parent ca2716b commit 195025e
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions windows-agent/internal/proservices/landscape/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,7 @@ func installFromURL(ctx context.Context, homeDir string, downloadDir string, dis

tarball := filepath.Join(tmpDir, distro.Name()+".tar.gz")

f, err := os.Create(tarball)
if err != nil {
return err
}
defer f.Close()

err = download(ctx, f, rootfsURL)
err = download(ctx, rootfsURL, tarball)
if err != nil {
return err
}
Expand All @@ -278,9 +272,9 @@ func installFromURL(ctx context.Context, homeDir string, downloadDir string, dis
return nil
}

// download downloads the rootfs from the given URL and writes it to the given writer while verifying its checksum.
// download downloads the rootfs from the given URL and writes it to the given destination while verifying its checksum.
// The checksum is read from the SHA256SUMS file found alongside the rootfs URL, as done in cloud-images.ubuntu.com.
func download(ctx context.Context, f io.Writer, u *url.URL) (err error) {
func download(ctx context.Context, u *url.URL, destination string) (err error) {
defer decorate.OnError(&err, "could not download %q", u)

checksum, err := wantRootfsChecksum(ctx, u)
Expand All @@ -297,6 +291,11 @@ func download(ctx context.Context, f io.Writer, u *url.URL) (err error) {
return fmt.Errorf("http request failed with code %d", resp.StatusCode)
}

f, err := os.Create(destination)
if err != nil {
return err
}
defer f.Close()
// Verify checksum and write file to disk
r := io.TeeReader(resp.Body, f)
if checksum != "" {
Expand Down

0 comments on commit 195025e

Please sign in to comment.