Pull request 2298: 7314 Fix updater

Updates #7314.

Squashed commit of the following:

commit be214937fc391cf8c0c31e52a29b0adbdcd81f08
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Nov 5 18:42:25 2024 +0300

    updater: upd doc

commit bdb9df94eecb8b1845b1e65b39325f163541ee91
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Nov 5 18:41:26 2024 +0300

    all: rename const

commit 7fbabeb939ba37aba11852af3c4095363e4ce304
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Nov 5 18:34:37 2024 +0300

    all: add binary const

commit c9f1a9f747c9ec440a9ea5754eed6974eff4169d
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Nov 5 18:20:44 2024 +0300

    updater: fix binary perm
This commit is contained in:
Eugene Burkov 2024-11-05 19:21:29 +03:00
parent 47dfa44cf6
commit 80ec01dd49
2 changed files with 14 additions and 9 deletions

View file

@ -20,9 +20,10 @@ import (
"github.com/AdguardTeam/golibs/log" "github.com/AdguardTeam/golibs/log"
) )
// Default file and directory permissions. // Default file, binary, and directory permissions.
const ( const (
DefaultPermDir fs.FileMode = 0o700 DefaultPermDir fs.FileMode = 0o700
DefaultPermExe fs.FileMode = 0o700
DefaultPermFile fs.FileMode = 0o600 DefaultPermFile fs.FileMode = 0o600
) )

View file

@ -7,6 +7,7 @@ import (
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"io" "io"
"io/fs"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
@ -240,7 +241,7 @@ func (u *Updater) unpack() error {
func (u *Updater) check() (err error) { func (u *Updater) check() (err error) {
log.Debug("updater: checking configuration") log.Debug("updater: checking configuration")
err = copyFile(u.confName, filepath.Join(u.updateDir, "AdGuardHome.yaml")) err = copyFile(u.confName, filepath.Join(u.updateDir, "AdGuardHome.yaml"), aghos.DefaultPermFile)
if err != nil { if err != nil {
return fmt.Errorf("copyFile() failed: %w", err) return fmt.Errorf("copyFile() failed: %w", err)
} }
@ -266,7 +267,7 @@ func (u *Updater) backup(firstRun bool) (err error) {
log.Debug("updater: backing up current configuration") log.Debug("updater: backing up current configuration")
_ = aghos.Mkdir(u.backupDir, aghos.DefaultPermDir) _ = aghos.Mkdir(u.backupDir, aghos.DefaultPermDir)
if !firstRun { if !firstRun {
err = copyFile(u.confName, filepath.Join(u.backupDir, "AdGuardHome.yaml")) err = copyFile(u.confName, filepath.Join(u.backupDir, "AdGuardHome.yaml"), aghos.DefaultPermFile)
if err != nil { if err != nil {
return fmt.Errorf("copyFile() failed: %w", err) return fmt.Errorf("copyFile() failed: %w", err)
} }
@ -296,8 +297,8 @@ func (u *Updater) replace() error {
} }
if u.goos == "windows" { if u.goos == "windows" {
// rename fails with "File in use" error // Use copy, since renaming fails with "File in use" error.
err = copyFile(u.updateExeName, u.currentExeName) err = copyFile(u.updateExeName, u.currentExeName, aghos.DefaultPermExe)
} else { } else {
err = os.Rename(u.updateExeName, u.currentExeName) err = os.Rename(u.updateExeName, u.currentExeName)
} }
@ -521,15 +522,15 @@ func zipFileUnpack(zipfile, outDir string) (files []string, err error) {
return files, err return files, err
} }
// Copy file on disk // copyFile copies a file from src to dst with the specified permissions.
func copyFile(src, dst string) (err error) { func copyFile(src, dst string, perm fs.FileMode) (err error) {
d, err := os.ReadFile(src) d, err := os.ReadFile(src)
if err != nil { if err != nil {
// Don't wrap the error, since it's informative enough as is. // Don't wrap the error, since it's informative enough as is.
return err return err
} }
err = aghos.WriteFile(dst, d, aghos.DefaultPermFile) err = aghos.WriteFile(dst, d, perm)
if err != nil { if err != nil {
// Don't wrap the error, since it's informative enough as is. // Don't wrap the error, since it's informative enough as is.
return err return err
@ -538,6 +539,9 @@ func copyFile(src, dst string) (err error) {
return nil return nil
} }
// copySupportingFiles copies each file specified in files from srcdir to
// dstdir. If a file specified as a path, only the name of the file is used.
// It skips AdGuardHome, AdGuardHome.exe, and AdGuardHome.yaml.
func copySupportingFiles(files []string, srcdir, dstdir string) error { func copySupportingFiles(files []string, srcdir, dstdir string) error {
for _, f := range files { for _, f := range files {
_, name := filepath.Split(f) _, name := filepath.Split(f)
@ -548,7 +552,7 @@ func copySupportingFiles(files []string, srcdir, dstdir string) error {
src := filepath.Join(srcdir, name) src := filepath.Join(srcdir, name)
dst := filepath.Join(dstdir, name) dst := filepath.Join(dstdir, name)
err := copyFile(src, dst) err := copyFile(src, dst, aghos.DefaultPermFile)
if err != nil && !errors.Is(err, os.ErrNotExist) { if err != nil && !errors.Is(err, os.ErrNotExist) {
return err return err
} }