Skip to content
Open
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
22 changes: 17 additions & 5 deletions internal/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ type Updater struct {
// TODO(a.garipov): See if all of these fields actually have to be in
// this struct.
currentExeName string // current binary executable
updateDir string // "workDir/agh-update-v0.103.0"
packageName string // "workDir/agh-update-v0.103.0/pkg_name.tar.gz"
tempDir string // temporary update staging directory
updateDir string // "tempDir/agh-update-v0.103.0"
packageName string // "tempDir/agh-update-v0.103.0/pkg_name.tar.gz"
backupDir string // "workDir/agh-backup"
backupExeName string // "workDir/agh-backup/AdGuardHome[.exe]"
updateExeName string // "workDir/agh-update-v0.103.0/AdGuardHome[.exe]"
Expand Down Expand Up @@ -222,7 +223,18 @@ func (u *Updater) NewVersion() (nv string) {

// prepare fills all necessary fields in Updater object.
func (u *Updater) prepare(ctx context.Context) (err error) {
u.updateDir = filepath.Join(u.workDir, fmt.Sprintf("agh-update-%s", u.newVersion))
u.tempDir, err = os.MkdirTemp("", "agh-update-*")
if err != nil {
return fmt.Errorf("creating temporary update dir: %w", err)
}
Comment on lines +226 to +229

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tempDir is created but never cleaned up if prepare() returns an error after creating the temporary directory. Consider adding a defer cleanup in the prepare() function to ensure the temporary directory is removed if any subsequent steps fail.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks.

I added cleanup for the temporary directory if prepare() returns an error
after os.MkdirTemp() succeeds.


defer func() {
if err != nil {
_ = os.RemoveAll(u.tempDir)
}
}()

u.updateDir = filepath.Join(u.tempDir, fmt.Sprintf("agh-update-%s", u.newVersion))

_, pkgNameOnly := filepath.Split(u.packageURL)
if pkgNameOnly == "" {
Expand Down Expand Up @@ -383,9 +395,9 @@ func (u *Updater) replace(ctx context.Context) (err error) {

// clean removes the temporary directory itself and all it's contents.
func (u *Updater) clean(ctx context.Context) {
err := os.RemoveAll(u.updateDir)
err := os.RemoveAll(u.tempDir)
if err != nil {
u.logger.WarnContext(ctx, "removing update dir", slogutil.KeyError, err)
u.logger.WarnContext(ctx, "removing temp update dir", slogutil.KeyError, err)
}
}

Expand Down