From 57bed73721c35971bb1e141901aa23ab50b6269f Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Fri, 19 Jun 2026 03:34:27 -0400 Subject: [PATCH] better context management for dwarfs --- internal/appherder/appimage.go | 7 ++++--- internal/appherder/dwarfs.go | 4 ++-- internal/appherder/install.go | 8 ++++---- internal/appherder/sync.go | 4 ++-- internal/appherder/upgrade.go | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/internal/appherder/appimage.go b/internal/appherder/appimage.go index 34d2f66..a9323f7 100644 --- a/internal/appherder/appimage.go +++ b/internal/appherder/appimage.go @@ -1,6 +1,7 @@ package appherder import ( + "context" "encoding/binary" "errors" "fmt" @@ -17,7 +18,7 @@ import ( // openAppImage exposes a type-2 AppImage's filesystem as an fs.FS. Supports // SquashFS (in-process) and DwarFS (extracted via the runtime). Caller must // invoke the returned closer to release resources. -func openAppImage(path string) (fs.FS, func(), error) { +func openAppImage(ctx context.Context, path string) (fs.FS, func(), error) { file, err := os.Open(path) if err != nil { return nil, nil, fmt.Errorf("open AppImage %s: %w", path, err) @@ -31,7 +32,7 @@ func openAppImage(path string) (fs.FS, func(), error) { if isDwarFS(file, offset) { file.Close() - return openDwarFS(path) + return openDwarFS(ctx, path) } if isSquashFS(file, offset) { @@ -280,7 +281,7 @@ func (a App) pruneVersions(dir string, keep int) { // readAppImageVersion returns the embedded version string from the AppImage's // desktop file, falling back to the file's mtime. func readAppImageVersion(path string) string { - if fsys, closeFs, err := openAppImage(path); err == nil { + if fsys, closeFs, err := openAppImage(context.Background(), path); err == nil { defer closeFs() if desktop, _, err := findDesktopFile(fsys); err == nil && desktop != nil { if version, ok := desktop.Get(desktopEntrySection, "X-AppImage-Version"); ok && version != "" { diff --git a/internal/appherder/dwarfs.go b/internal/appherder/dwarfs.go index e3bcabe..b3609b7 100644 --- a/internal/appherder/dwarfs.go +++ b/internal/appherder/dwarfs.go @@ -13,14 +13,14 @@ import ( // openDwarFS extracts the DwarFS payload from an AppImage. Tries the system // dwarfsextract tool first; falls back to the AppImage's own --appimage-extract // as a last resort. Returns the extracted filesystem and a cleanup function. -func openDwarFS(appimagePath string) (fs.FS, func(), error) { +func openDwarFS(ctx context.Context, appimagePath string) (fs.FS, func(), error) { dir, err := os.MkdirTemp("", "appherder-dwarfs") if err != nil { return nil, nil, fmt.Errorf("create temp directory: %w", err) } cleanup := func() { os.RemoveAll(dir) } - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) + ctx, cancel := context.WithTimeout(ctx, 5*time.Minute) defer cancel() cmd := extractCommand(ctx, appimagePath, dir) diff --git a/internal/appherder/install.go b/internal/appherder/install.go index b8a8d25..0b3a933 100644 --- a/internal/appherder/install.go +++ b/internal/appherder/install.go @@ -11,10 +11,10 @@ import ( ) func (a App) Install(appimage string) (string, error) { - return a.install(appimage, expectedChecksum{}) + return a.install(context.Background(), appimage, expectedChecksum{}) } -func (a App) install(appimage string, want expectedChecksum) (appName string, err error) { +func (a App) install(ctx context.Context, appimage string, want expectedChecksum) (appName string, err error) { appimage, err = filepath.Abs(appimage) if err != nil { return "", fmt.Errorf("resolve AppImage path %q: %w", appimage, err) @@ -28,7 +28,7 @@ func (a App) install(appimage string, want expectedChecksum) (appName string, er return "", err } - fsys, closeAppImage, err := openAppImage(appimage) + fsys, closeAppImage, err := openAppImage(ctx, appimage) if err != nil { return "", err } @@ -124,7 +124,7 @@ func (a App) InstallFromURL(ctx context.Context, url string) (string, error) { return "", err } defer os.Remove(tmpName) - return a.install(tmpName, expectedChecksum{}) + return a.install(ctx, tmpName, expectedChecksum{}) } // deriveAppName picks the canonical install name: the desktop entry's Name diff --git a/internal/appherder/sync.go b/internal/appherder/sync.go index c025f24..5ededc2 100644 --- a/internal/appherder/sync.go +++ b/internal/appherder/sync.go @@ -57,8 +57,8 @@ func (a App) Sync(ctx context.Context, force bool) (SyncResult, error) { } // parallelMap preserves input order, so results stay deterministic. - installResults := parallelMap(ctx, files, installConcurrency, func(_ context.Context, f string) SyncInstall { - name, err := a.Install(f) + installResults := parallelMap(ctx, files, installConcurrency, func(ctx context.Context, f string) SyncInstall { + name, err := a.install(ctx, f, expectedChecksum{}) return SyncInstall{File: f, AppName: name, Err: err} }) diff --git a/internal/appherder/upgrade.go b/internal/appherder/upgrade.go index d48a772..8806069 100644 --- a/internal/appherder/upgrade.go +++ b/internal/appherder/upgrade.go @@ -124,7 +124,7 @@ func (a App) applyUpgrade(ctx context.Context, name string, rel Release) error { } defer os.Remove(tmpName) - _, err = a.install(tmpName, rel.expectedChecksum()) + _, err = a.install(ctx, tmpName, rel.expectedChecksum()) return err } -- 2.51.2