2021-01-13 13:18:51 +00:00
|
|
|
// Package updater provides an updater for AdGuardHome.
|
|
|
|
package updater
|
2020-07-20 18:14:07 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-21 15:50:35 +00:00
|
|
|
"archive/tar"
|
|
|
|
"archive/zip"
|
|
|
|
"compress/gzip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2021-01-13 13:18:51 +00:00
|
|
|
"net/url"
|
2020-07-20 18:14:07 +00:00
|
|
|
"os"
|
2020-07-21 15:50:35 +00:00
|
|
|
"os/exec"
|
2021-01-13 13:18:51 +00:00
|
|
|
"path"
|
2020-07-20 18:14:07 +00:00
|
|
|
"path/filepath"
|
2020-07-21 15:50:35 +00:00
|
|
|
"strings"
|
2021-01-13 13:18:51 +00:00
|
|
|
"sync"
|
2020-07-21 09:17:23 +00:00
|
|
|
"time"
|
2020-07-21 15:50:35 +00:00
|
|
|
|
2020-11-23 11:14:08 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghio"
|
2020-10-30 10:32:02 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/util"
|
2021-01-13 13:18:51 +00:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/version"
|
2020-07-21 15:50:35 +00:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2020-07-20 18:14:07 +00:00
|
|
|
)
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
// Updater is the AdGuard Home updater.
|
2020-07-20 18:14:07 +00:00
|
|
|
type Updater struct {
|
2021-01-13 13:18:51 +00:00
|
|
|
client *http.Client
|
2020-07-20 18:14:07 +00:00
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
version string
|
|
|
|
channel string
|
|
|
|
goarch string
|
|
|
|
goos string
|
|
|
|
goarm string
|
|
|
|
gomips string
|
|
|
|
|
|
|
|
workDir string
|
|
|
|
confName string
|
|
|
|
versionCheckURL string
|
|
|
|
|
|
|
|
// mu protects all fields below.
|
|
|
|
mu *sync.RWMutex
|
|
|
|
|
|
|
|
// TODO(a.garipov): See if all of these fields actually have to be in
|
|
|
|
// this struct.
|
2020-07-21 15:50:35 +00:00
|
|
|
currentExeName string // current binary executable
|
2021-01-13 13:18:51 +00:00
|
|
|
updateDir string // "workDir/agh-update-v0.103.0"
|
|
|
|
packageName string // "workDir/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]"
|
2020-07-21 15:50:35 +00:00
|
|
|
unpackedFiles []string
|
2020-07-21 09:17:23 +00:00
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
newVersion string
|
|
|
|
packageURL string
|
|
|
|
|
|
|
|
// Cached fields to prevent too many API requests.
|
|
|
|
prevCheckError error
|
|
|
|
prevCheckTime time.Time
|
|
|
|
prevCheckResult VersionInfo
|
2020-07-20 18:14:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
// Config is the AdGuard Home updater configuration.
|
2020-07-22 17:27:20 +00:00
|
|
|
type Config struct {
|
|
|
|
Client *http.Client
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
Version string
|
|
|
|
Channel string
|
|
|
|
GOARCH string
|
|
|
|
GOOS string
|
|
|
|
GOARM string
|
|
|
|
GOMIPS string
|
|
|
|
|
|
|
|
// ConfName is the name of the current configuration file. Typically,
|
|
|
|
// "AdGuardHome.yaml".
|
|
|
|
ConfName string
|
|
|
|
// WorkDir is the working directory that is used for temporary files.
|
|
|
|
WorkDir string
|
2020-07-22 17:27:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
// NewUpdater creates a new Updater.
|
|
|
|
func NewUpdater(conf *Config) *Updater {
|
|
|
|
u := &url.URL{
|
|
|
|
Scheme: "https",
|
|
|
|
Host: "static.adguard.com",
|
|
|
|
Path: path.Join("adguardhome", conf.Channel, "version.json"),
|
|
|
|
}
|
2020-07-22 17:27:20 +00:00
|
|
|
return &Updater{
|
2021-01-13 13:18:51 +00:00
|
|
|
client: conf.Client,
|
|
|
|
|
|
|
|
version: conf.Version,
|
|
|
|
channel: conf.Channel,
|
|
|
|
goarch: conf.GOARCH,
|
|
|
|
goos: conf.GOOS,
|
|
|
|
goarm: conf.GOARM,
|
|
|
|
gomips: conf.GOMIPS,
|
|
|
|
|
|
|
|
confName: conf.ConfName,
|
|
|
|
workDir: conf.WorkDir,
|
|
|
|
versionCheckURL: u.String(),
|
|
|
|
|
|
|
|
mu: &sync.RWMutex{},
|
2020-07-20 18:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
// Update performs the auto-update.
|
|
|
|
func (u *Updater) Update() error {
|
|
|
|
u.mu.Lock()
|
|
|
|
defer u.mu.Unlock()
|
|
|
|
|
2020-07-21 15:50:35 +00:00
|
|
|
err := u.prepare()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer u.clean()
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
err = u.downloadPackageFile(u.packageURL, u.packageName)
|
2020-07-21 15:50:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = u.unpack()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = u.check()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = u.backup()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = u.replace()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
// NewVersion returns the available new version.
|
|
|
|
func (u *Updater) NewVersion() (nv string) {
|
|
|
|
u.mu.RLock()
|
|
|
|
defer u.mu.RUnlock()
|
|
|
|
|
|
|
|
return u.newVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
// VersionCheckURL returns the version check URL.
|
|
|
|
func (u *Updater) VersionCheckURL() (vcu string) {
|
|
|
|
u.mu.RLock()
|
|
|
|
defer u.mu.RUnlock()
|
|
|
|
|
|
|
|
return u.versionCheckURL
|
|
|
|
}
|
|
|
|
|
2020-07-21 15:50:35 +00:00
|
|
|
func (u *Updater) prepare() error {
|
2021-01-13 13:18:51 +00:00
|
|
|
u.updateDir = filepath.Join(u.workDir, fmt.Sprintf("agh-update-%s", u.newVersion))
|
2020-07-21 15:50:35 +00:00
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
_, pkgNameOnly := filepath.Split(u.packageURL)
|
|
|
|
if pkgNameOnly == "" {
|
2020-07-21 15:50:35 +00:00
|
|
|
return fmt.Errorf("invalid PackageURL")
|
|
|
|
}
|
2021-01-13 13:18:51 +00:00
|
|
|
|
2020-07-21 15:50:35 +00:00
|
|
|
u.packageName = filepath.Join(u.updateDir, pkgNameOnly)
|
2021-01-13 13:18:51 +00:00
|
|
|
u.backupDir = filepath.Join(u.workDir, "agh-backup")
|
2020-07-21 15:50:35 +00:00
|
|
|
|
|
|
|
exeName := "AdGuardHome"
|
2021-01-13 13:18:51 +00:00
|
|
|
if u.goos == "windows" {
|
2020-07-21 15:50:35 +00:00
|
|
|
exeName = "AdGuardHome.exe"
|
|
|
|
}
|
|
|
|
|
|
|
|
u.backupExeName = filepath.Join(u.backupDir, exeName)
|
|
|
|
u.updateExeName = filepath.Join(u.updateDir, exeName)
|
2020-07-22 11:20:14 +00:00
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
log.Info("Updating from %s to %s. URL:%s", version.Version(), u.newVersion, u.packageURL)
|
2020-07-22 11:20:14 +00:00
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
// If the binary file isn't found in working directory, we won't be able
|
|
|
|
// to auto-update. Getting the full path to the current binary file on
|
|
|
|
// Unix and checking write permissions is more difficult.
|
|
|
|
u.currentExeName = filepath.Join(u.workDir, exeName)
|
2020-07-22 11:20:14 +00:00
|
|
|
if !util.FileExists(u.currentExeName) {
|
|
|
|
return fmt.Errorf("executable file %s doesn't exist", u.currentExeName)
|
|
|
|
}
|
2021-01-13 13:18:51 +00:00
|
|
|
|
2020-07-21 15:50:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Updater) unpack() error {
|
|
|
|
var err error
|
2021-01-13 13:18:51 +00:00
|
|
|
_, pkgNameOnly := filepath.Split(u.packageURL)
|
2020-07-21 15:50:35 +00:00
|
|
|
|
|
|
|
log.Debug("updater: unpacking the package")
|
|
|
|
if strings.HasSuffix(pkgNameOnly, ".zip") {
|
|
|
|
u.unpackedFiles, err = zipFileUnpack(u.packageName, u.updateDir)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return fmt.Errorf(".zip unpack failed: %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if strings.HasSuffix(pkgNameOnly, ".tar.gz") {
|
|
|
|
u.unpackedFiles, err = tarGzFileUnpack(u.packageName, u.updateDir)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return fmt.Errorf(".tar.gz unpack failed: %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unknown package extension")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Updater) check() error {
|
|
|
|
log.Debug("updater: checking configuration")
|
2021-01-13 13:18:51 +00:00
|
|
|
err := copyFile(u.confName, filepath.Join(u.updateDir, "AdGuardHome.yaml"))
|
2020-07-21 15:50:35 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return fmt.Errorf("copyFile() failed: %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
|
|
|
cmd := exec.Command(u.updateExeName, "--check-config")
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil || cmd.ProcessState.ExitCode() != 0 {
|
|
|
|
return fmt.Errorf("exec.Command(): %s %d", err, cmd.ProcessState.ExitCode())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Updater) backup() error {
|
|
|
|
log.Debug("updater: backing up the current configuration")
|
2020-11-05 12:20:57 +00:00
|
|
|
_ = os.Mkdir(u.backupDir, 0o755)
|
2021-01-13 13:18:51 +00:00
|
|
|
err := copyFile(u.confName, filepath.Join(u.backupDir, "AdGuardHome.yaml"))
|
2020-07-21 15:50:35 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return fmt.Errorf("copyFile() failed: %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
wd := u.workDir
|
|
|
|
err = copySupportingFiles(u.unpackedFiles, wd, u.backupDir)
|
2020-07-21 15:50:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("copySupportingFiles(%s, %s) failed: %s",
|
2021-01-13 13:18:51 +00:00
|
|
|
wd, u.backupDir, err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Updater) replace() error {
|
2021-01-13 13:18:51 +00:00
|
|
|
err := copySupportingFiles(u.unpackedFiles, u.updateDir, u.workDir)
|
2020-07-21 15:50:35 +00:00
|
|
|
if err != nil {
|
2021-01-13 13:18:51 +00:00
|
|
|
return fmt.Errorf("copySupportingFiles(%s, %s) failed: %s", u.updateDir, u.workDir, err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("updater: renaming: %s -> %s", u.currentExeName, u.backupExeName)
|
|
|
|
err = os.Rename(u.currentExeName, u.backupExeName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-13 13:18:51 +00:00
|
|
|
if u.goos == "windows" {
|
2020-07-21 15:50:35 +00:00
|
|
|
// rename fails with "File in use" error
|
|
|
|
err = copyFile(u.updateExeName, u.currentExeName)
|
|
|
|
} else {
|
|
|
|
err = os.Rename(u.updateExeName, u.currentExeName)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-13 13:18:51 +00:00
|
|
|
|
2020-07-21 15:50:35 +00:00
|
|
|
log.Debug("updater: renamed: %s -> %s", u.updateExeName, u.currentExeName)
|
2021-01-13 13:18:51 +00:00
|
|
|
|
2020-07-21 15:50:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *Updater) clean() {
|
|
|
|
_ = os.RemoveAll(u.updateDir)
|
|
|
|
}
|
|
|
|
|
2020-11-23 11:14:08 +00:00
|
|
|
// MaxPackageFileSize is a maximum package file length in bytes. The largest
|
|
|
|
// package whose size is limited by this constant currently has the size of
|
|
|
|
// approximately 9 MiB.
|
|
|
|
const MaxPackageFileSize = 32 * 1024 * 1024
|
|
|
|
|
2020-07-21 15:50:35 +00:00
|
|
|
// Download package file and save it to disk
|
2020-12-07 11:32:06 +00:00
|
|
|
func (u *Updater) downloadPackageFile(url, filename string) error {
|
2021-01-13 13:18:51 +00:00
|
|
|
resp, err := u.client.Get(url)
|
2020-07-21 15:50:35 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return fmt.Errorf("http request failed: %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
2020-11-23 11:14:08 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
resp.Body, err = aghio.LimitReadCloser(resp.Body, MaxPackageFileSize)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("http request failed: %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
2020-11-23 11:14:08 +00:00
|
|
|
defer resp.Body.Close()
|
2020-07-21 15:50:35 +00:00
|
|
|
|
|
|
|
log.Debug("updater: reading HTTP body")
|
2020-11-23 11:14:08 +00:00
|
|
|
// This use of ReadAll is now safe, because we limited body's Reader.
|
2020-07-21 15:50:35 +00:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return fmt.Errorf("ioutil.ReadAll() failed: %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
_ = os.Mkdir(u.updateDir, 0o755)
|
2020-07-21 15:50:35 +00:00
|
|
|
|
|
|
|
log.Debug("updater: saving package to file")
|
2020-11-05 12:20:57 +00:00
|
|
|
err = ioutil.WriteFile(filename, body, 0o644)
|
2020-07-21 15:50:35 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return fmt.Errorf("ioutil.WriteFile() failed: %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unpack all files from .tar.gz file to the specified directory
|
|
|
|
// Existing files are overwritten
|
|
|
|
// All files are created inside 'outdir', subdirectories are not created
|
|
|
|
// Return the list of files (not directories) written
|
|
|
|
func tarGzFileUnpack(tarfile, outdir string) ([]string, error) {
|
|
|
|
f, err := os.Open(tarfile)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return nil, fmt.Errorf("os.Open(): %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = f.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
gzReader, err := gzip.NewReader(f)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return nil, fmt.Errorf("gzip.NewReader(): %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var files []string
|
|
|
|
var err2 error
|
|
|
|
tarReader := tar.NewReader(gzReader)
|
|
|
|
for {
|
2021-03-11 12:38:39 +00:00
|
|
|
var header *tar.Header
|
|
|
|
header, err = tarReader.Next()
|
2020-07-21 15:50:35 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
err2 = nil
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
err2 = fmt.Errorf("tarReader.Next(): %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
_, inputNameOnly := filepath.Split(header.Name)
|
2021-01-13 13:18:51 +00:00
|
|
|
if inputNameOnly == "" {
|
2020-07-21 15:50:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
outputName := filepath.Join(outdir, inputNameOnly)
|
|
|
|
|
|
|
|
if header.Typeflag == tar.TypeDir {
|
2021-02-02 17:32:44 +00:00
|
|
|
if inputNameOnly == "AdGuardHome" {
|
|
|
|
// Top-level AdGuardHome/. Skip it.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): This whole package needs to
|
|
|
|
// be rewritten and covered in more integration
|
|
|
|
// tests. It has weird assumptions and file
|
|
|
|
// mode issues.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:20:57 +00:00
|
|
|
err = os.Mkdir(outputName, os.FileMode(header.Mode&0o777))
|
2020-07-21 15:50:35 +00:00
|
|
|
if err != nil && !os.IsExist(err) {
|
2020-11-05 12:20:57 +00:00
|
|
|
err2 = fmt.Errorf("os.Mkdir(%s): %w", outputName, err)
|
2020-07-21 15:50:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Debug("updater: created directory %s", outputName)
|
|
|
|
continue
|
|
|
|
} else if header.Typeflag != tar.TypeReg {
|
|
|
|
log.Debug("updater: %s: unknown file type %d, skipping", inputNameOnly, header.Typeflag)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-11 12:38:39 +00:00
|
|
|
var f io.WriteCloser
|
|
|
|
f, err = os.OpenFile(outputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(header.Mode&0o777))
|
2020-07-21 15:50:35 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
err2 = fmt.Errorf("os.OpenFile(%s): %w", outputName, err)
|
2020-07-21 15:50:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
_, err = io.Copy(f, tarReader)
|
|
|
|
if err != nil {
|
|
|
|
_ = f.Close()
|
2020-11-05 12:20:57 +00:00
|
|
|
err2 = fmt.Errorf("io.Copy(): %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
err2 = fmt.Errorf("f.Close(): %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("updater: created file %s", outputName)
|
|
|
|
files = append(files, header.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = gzReader.Close()
|
|
|
|
return files, err2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unpack all files from .zip file to the specified directory
|
|
|
|
// Existing files are overwritten
|
|
|
|
// All files are created inside 'outdir', subdirectories are not created
|
|
|
|
// Return the list of files (not directories) written
|
|
|
|
func zipFileUnpack(zipfile, outdir string) ([]string, error) {
|
|
|
|
r, err := zip.OpenReader(zipfile)
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
return nil, fmt.Errorf("zip.OpenReader(): %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
var files []string
|
|
|
|
var err2 error
|
|
|
|
var zr io.ReadCloser
|
|
|
|
for _, zf := range r.File {
|
|
|
|
zr, err = zf.Open()
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
err2 = fmt.Errorf("zip file Open(): %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
fi := zf.FileInfo()
|
|
|
|
inputNameOnly := fi.Name()
|
2021-01-13 13:18:51 +00:00
|
|
|
if inputNameOnly == "" {
|
2020-07-21 15:50:35 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
outputName := filepath.Join(outdir, inputNameOnly)
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
2021-02-02 17:32:44 +00:00
|
|
|
if inputNameOnly == "AdGuardHome" {
|
|
|
|
// Top-level AdGuardHome/. Skip it.
|
|
|
|
//
|
|
|
|
// TODO(a.garipov): See the similar todo in
|
|
|
|
// tarGzFileUnpack.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-07-21 15:50:35 +00:00
|
|
|
err = os.Mkdir(outputName, fi.Mode())
|
|
|
|
if err != nil && !os.IsExist(err) {
|
2020-11-05 12:20:57 +00:00
|
|
|
err2 = fmt.Errorf("os.Mkdir(): %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Tracef("created directory %s", outputName)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-03-11 12:38:39 +00:00
|
|
|
var f io.WriteCloser
|
|
|
|
f, err = os.OpenFile(outputName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fi.Mode())
|
2020-07-21 15:50:35 +00:00
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
err2 = fmt.Errorf("os.OpenFile(): %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
_, err = io.Copy(f, zr)
|
|
|
|
if err != nil {
|
|
|
|
_ = f.Close()
|
2020-11-05 12:20:57 +00:00
|
|
|
err2 = fmt.Errorf("io.Copy(): %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
2020-11-05 12:20:57 +00:00
|
|
|
err2 = fmt.Errorf("f.Close(): %w", err)
|
2020-07-21 15:50:35 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Tracef("created file %s", outputName)
|
|
|
|
files = append(files, inputNameOnly)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = zr.Close()
|
|
|
|
return files, err2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy file on disk
|
|
|
|
func copyFile(src, dst string) error {
|
|
|
|
d, e := ioutil.ReadFile(src)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
2020-11-05 12:20:57 +00:00
|
|
|
e = ioutil.WriteFile(dst, d, 0o644)
|
2020-07-21 15:50:35 +00:00
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copySupportingFiles(files []string, srcdir, dstdir string) error {
|
|
|
|
for _, f := range files {
|
|
|
|
_, name := filepath.Split(f)
|
|
|
|
if name == "AdGuardHome" || name == "AdGuardHome.exe" || name == "AdGuardHome.yaml" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
src := filepath.Join(srcdir, name)
|
|
|
|
dst := filepath.Join(dstdir, name)
|
|
|
|
|
|
|
|
err := copyFile(src, dst)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("updater: copied: %s -> %s", src, dst)
|
|
|
|
}
|
2020-07-20 18:14:07 +00:00
|
|
|
return nil
|
|
|
|
}
|