Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion updater/download_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/hex"
"fmt"
"io"
"os"

"github.com/arduino/go-paths-helper"
"github.com/codeclysm/extract/v4"
Expand Down Expand Up @@ -108,7 +109,7 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
defer download.Close()

// Download the zip
temp, err := paths.MkTempDir("", "flasher-updater-")
temp, err := GetTempDir("download-")
if err != nil {
return nil, "", fmt.Errorf("could not create temporary download directory: %w", err)
}
Expand Down Expand Up @@ -159,3 +160,21 @@ func ExtractImage(archive, temp *paths.Path) error {
}
return nil
}

// GetTempDir returns a temporary directory inside the user's cache directory.
// The caller is responsible for removing the directory when no longer needed.
func GetTempDir(prefix string) (*paths.Path, error) {
userCacheDir, err := os.UserCacheDir()
if err != nil {
return nil, fmt.Errorf("could not get user's cache directory: %w", err)
}

cacheDir := paths.New(userCacheDir, "arduino-flasher-cli")
_ = cacheDir.MkdirAll()

temp, err := paths.MkTempDir(cacheDir.String(), prefix)
if err != nil {
return nil, fmt.Errorf("could not create .cache directory: %w", err)
}
return temp, nil
}
2 changes: 1 addition & 1 deletion updater/flasher.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Flash(ctx context.Context, imagePath *paths.Path, version string, forceYes

imagePath = tempImagePath
} else if !imagePath.IsDir() {
temp, err := paths.MkTempDir("", "debian-image-")
temp, err := GetTempDir("extract-")
if err != nil {
return fmt.Errorf("error creating a temporary directory to extract the archive: %v", err)
}
Expand Down