From 793194db67a9b5f8440f77863a170375d3c7612f Mon Sep 17 00:00:00 2001 From: Andrey Meshkov Date: Mon, 20 Jul 2020 12:32:14 +0300 Subject: [PATCH] Fix unpacking tar files --- home/control_update.go | 17 +++++++--- home/control_update_test.go | 68 ++++++++++++++++++++++--------------- 2 files changed, 52 insertions(+), 33 deletions(-) diff --git a/home/control_update.go b/home/control_update.go index 566b8327..cecadb33 100644 --- a/home/control_update.go +++ b/home/control_update.go @@ -11,6 +11,7 @@ import ( "net/http" "os" "os/exec" + "path" "path/filepath" "runtime" "strings" @@ -244,6 +245,7 @@ func getUpdateInfo(jsonData []byte) (*updateInfo, error) { if runtime.GOOS == "windows" { binName = "AdGuardHome.exe" } + u.curBinName = filepath.Join(workDir, binName) if !util.FileExists(u.curBinName) { return nil, fmt.Errorf("executable file %s doesn't exist", u.curBinName) @@ -347,7 +349,6 @@ func zipFileUnpack(zipfile, outdir string) ([]string, error) { // Existing files are overwritten // Return the list of files (not directories) written func targzFileUnpack(tarfile, outdir string) ([]string, error) { - f, err := os.Open(tarfile) if err != nil { return nil, fmt.Errorf("os.Open(): %s", err) @@ -376,7 +377,13 @@ func targzFileUnpack(tarfile, outdir string) ([]string, error) { continue } - fn := filepath.Join(outdir, header.Name) + dir := path.Dir(header.Name) + if dir != "" { + // Create dir if necessary + _ = os.Mkdir(dir, os.FileMode(header.Mode&0777)) + } + + fn := filepath.Join(outdir, path.Base(header.Name)) if header.Typeflag == tar.TypeDir { err = os.Mkdir(fn, os.FileMode(header.Mode&0777)) @@ -398,17 +405,17 @@ func targzFileUnpack(tarfile, outdir string) ([]string, error) { } _, err = io.Copy(f, tarReader) if err != nil { - f.Close() + _ = f.Close() err2 = fmt.Errorf("io.Copy(): %s", err) break } - f.Close() + _ = f.Close() log.Tracef("created file %s", fn) files = append(files, header.Name) } - gzReader.Close() + _ = gzReader.Close() return files, err2 } diff --git a/home/control_update_test.go b/home/control_update_test.go index 6ec4a186..1b1d254e 100644 --- a/home/control_update_test.go +++ b/home/control_update_test.go @@ -5,6 +5,8 @@ package home import ( "os" "testing" + + "github.com/stretchr/testify/assert" ) func TestDoUpdate(t *testing.T) { @@ -16,16 +18,28 @@ func TestDoUpdate(t *testing.T) { "version": "v0.96", "announcement": "AdGuard Home v0.96 is now available!", "announcement_url": "", - "download_windows_amd64": "", - "download_windows_386": "", - "download_darwin_amd64": "", - "download_linux_amd64": "https://github.com/AdguardTeam/AdGuardHome/releases/download/v0.96/AdGuardHome_linux_amd64.tar.gz", - "download_linux_386": "", - "download_linux_arm": "", - "download_linux_arm64": "", - "download_linux_mips": "", - "download_linux_mipsle": "", - "selfupdate_min_version": "v0.0" + "download_windows_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_windows_amd64.zip", + "download_windows_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_windows_386.zip", + "download_darwin_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_darwin_amd64.zip", + "download_darwin_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_darwin_386.zip", + "download_linux_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_amd64.tar.gz", + "download_linux_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_386.tar.gz", + "download_linux_arm": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz", + "download_linux_armv5": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv5.tar.gz", + "download_linux_armv6": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz", + "download_linux_armv7": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv7.tar.gz", + "download_linux_arm64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_arm64.tar.gz", + "download_linux_mips": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mips_softfloat.tar.gz", + "download_linux_mipsle": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mipsle_softfloat.tar.gz", + "download_linux_mips64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mips64_softfloat.tar.gz", + "download_linux_mips64le": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_mips64le_softfloat.tar.gz", + "download_freebsd_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_386.tar.gz", + "download_freebsd_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_amd64.tar.gz", + "download_freebsd_arm": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv6.tar.gz", + "download_freebsd_armv5": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv5.tar.gz", + "download_freebsd_armv6": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv6.tar.gz", + "download_freebsd_armv7": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_armv7.tar.gz", + "download_freebsd_arm64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_freebsd_arm64.tar.gz" }` uu, err := getUpdateInfo([]byte(data)) if err != nil { @@ -33,7 +47,7 @@ func TestDoUpdate(t *testing.T) { } u := updateInfo{ - pkgURL: "https://github.com/AdguardTeam/AdGuardHome/releases/download/" + newver + "/AdGuardHome_linux_amd64.tar.gz", + pkgURL: "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz", pkgName: Context.workDir + "/agh-update-" + newver + "/AdGuardHome_linux_amd64.tar.gz", newVer: newver, updateDir: Context.workDir + "/agh-update-" + newver, @@ -45,18 +59,16 @@ func TestDoUpdate(t *testing.T) { newBinName: Context.workDir + "/agh-update-" + newver + "/AdGuardHome/AdGuardHome", } - if uu.pkgURL != u.pkgURL || - uu.pkgName != u.pkgName || - uu.newVer != u.newVer || - uu.updateDir != u.updateDir || - uu.backupDir != u.backupDir || - uu.configName != u.configName || - uu.updateConfigName != u.updateConfigName || - uu.curBinName != u.curBinName || - uu.bkpBinName != u.bkpBinName || - uu.newBinName != u.newBinName { - t.Fatalf("getUpdateInfo: %v != %v", uu, u) - } + assert.Equal(t, uu.pkgURL, u.pkgURL) + assert.Equal(t, uu.pkgName, u.pkgName) + assert.Equal(t, uu.newVer, u.newVer) + assert.Equal(t, uu.updateDir, u.updateDir) + assert.Equal(t, uu.backupDir, u.backupDir) + assert.Equal(t, uu.configName, u.configName) + assert.Equal(t, uu.updateConfigName, u.updateConfigName) + assert.Equal(t, uu.curBinName, u.curBinName) + assert.Equal(t, uu.bkpBinName, u.bkpBinName) + assert.Equal(t, uu.newBinName, u.newBinName) e := doUpdate(&u) if e != nil { @@ -66,20 +78,20 @@ func TestDoUpdate(t *testing.T) { } func TestTargzFileUnpack(t *testing.T) { - fn := "./dist/AdGuardHome_v0.95_linux_amd64.tar.gz" - outdir := "./test-unpack" + fn := "../dist/AdGuardHome_linux_amd64.tar.gz" + outdir := "../test-unpack" + defer os.RemoveAll(outdir) _ = os.Mkdir(outdir, 0755) files, e := targzFileUnpack(fn, outdir) if e != nil { t.Fatalf("FAILED: %s", e) } t.Logf("%v", files) - os.RemoveAll(outdir) } func TestZipFileUnpack(t *testing.T) { - fn := "./dist/AdGuardHome_v0.95_Windows_amd64.zip" - outdir := "./test-unpack" + fn := "../dist/AdGuardHome_windows_amd64.zip" + outdir := "../test-unpack" _ = os.Mkdir(outdir, 0755) files, e := zipFileUnpack(fn, outdir) if e != nil {