Fix unpacking tar files

This commit is contained in:
Andrey Meshkov 2020-07-20 12:32:14 +03:00
parent 4175d82279
commit 793194db67
2 changed files with 52 additions and 33 deletions

View File

@ -11,6 +11,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -244,6 +245,7 @@ func getUpdateInfo(jsonData []byte) (*updateInfo, error) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
binName = "AdGuardHome.exe" binName = "AdGuardHome.exe"
} }
u.curBinName = filepath.Join(workDir, binName) u.curBinName = filepath.Join(workDir, binName)
if !util.FileExists(u.curBinName) { if !util.FileExists(u.curBinName) {
return nil, fmt.Errorf("executable file %s doesn't exist", 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 // Existing files are overwritten
// Return the list of files (not directories) written // Return the list of files (not directories) written
func targzFileUnpack(tarfile, outdir string) ([]string, error) { func targzFileUnpack(tarfile, outdir string) ([]string, error) {
f, err := os.Open(tarfile) f, err := os.Open(tarfile)
if err != nil { if err != nil {
return nil, fmt.Errorf("os.Open(): %s", err) return nil, fmt.Errorf("os.Open(): %s", err)
@ -376,7 +377,13 @@ func targzFileUnpack(tarfile, outdir string) ([]string, error) {
continue 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 { if header.Typeflag == tar.TypeDir {
err = os.Mkdir(fn, os.FileMode(header.Mode&0777)) 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) _, err = io.Copy(f, tarReader)
if err != nil { if err != nil {
f.Close() _ = f.Close()
err2 = fmt.Errorf("io.Copy(): %s", err) err2 = fmt.Errorf("io.Copy(): %s", err)
break break
} }
f.Close() _ = f.Close()
log.Tracef("created file %s", fn) log.Tracef("created file %s", fn)
files = append(files, header.Name) files = append(files, header.Name)
} }
gzReader.Close() _ = gzReader.Close()
return files, err2 return files, err2
} }

View File

@ -5,6 +5,8 @@ package home
import ( import (
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert"
) )
func TestDoUpdate(t *testing.T) { func TestDoUpdate(t *testing.T) {
@ -16,16 +18,28 @@ func TestDoUpdate(t *testing.T) {
"version": "v0.96", "version": "v0.96",
"announcement": "AdGuard Home v0.96 is now available!", "announcement": "AdGuard Home v0.96 is now available!",
"announcement_url": "", "announcement_url": "",
"download_windows_amd64": "", "download_windows_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_windows_amd64.zip",
"download_windows_386": "", "download_windows_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_windows_386.zip",
"download_darwin_amd64": "", "download_darwin_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_darwin_amd64.zip",
"download_linux_amd64": "https://github.com/AdguardTeam/AdGuardHome/releases/download/v0.96/AdGuardHome_linux_amd64.tar.gz", "download_darwin_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_darwin_386.zip",
"download_linux_386": "", "download_linux_amd64": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_amd64.tar.gz",
"download_linux_arm": "", "download_linux_386": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_386.tar.gz",
"download_linux_arm64": "", "download_linux_arm": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz",
"download_linux_mips": "", "download_linux_armv5": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv5.tar.gz",
"download_linux_mipsle": "", "download_linux_armv6": "https://static.adguard.com/adguardhome/beta/AdGuardHome_linux_armv6.tar.gz",
"selfupdate_min_version": "v0.0" "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)) uu, err := getUpdateInfo([]byte(data))
if err != nil { if err != nil {
@ -33,7 +47,7 @@ func TestDoUpdate(t *testing.T) {
} }
u := updateInfo{ 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", pkgName: Context.workDir + "/agh-update-" + newver + "/AdGuardHome_linux_amd64.tar.gz",
newVer: newver, newVer: newver,
updateDir: Context.workDir + "/agh-update-" + newver, updateDir: Context.workDir + "/agh-update-" + newver,
@ -45,18 +59,16 @@ func TestDoUpdate(t *testing.T) {
newBinName: Context.workDir + "/agh-update-" + newver + "/AdGuardHome/AdGuardHome", newBinName: Context.workDir + "/agh-update-" + newver + "/AdGuardHome/AdGuardHome",
} }
if uu.pkgURL != u.pkgURL || assert.Equal(t, uu.pkgURL, u.pkgURL)
uu.pkgName != u.pkgName || assert.Equal(t, uu.pkgName, u.pkgName)
uu.newVer != u.newVer || assert.Equal(t, uu.newVer, u.newVer)
uu.updateDir != u.updateDir || assert.Equal(t, uu.updateDir, u.updateDir)
uu.backupDir != u.backupDir || assert.Equal(t, uu.backupDir, u.backupDir)
uu.configName != u.configName || assert.Equal(t, uu.configName, u.configName)
uu.updateConfigName != u.updateConfigName || assert.Equal(t, uu.updateConfigName, u.updateConfigName)
uu.curBinName != u.curBinName || assert.Equal(t, uu.curBinName, u.curBinName)
uu.bkpBinName != u.bkpBinName || assert.Equal(t, uu.bkpBinName, u.bkpBinName)
uu.newBinName != u.newBinName { assert.Equal(t, uu.newBinName, u.newBinName)
t.Fatalf("getUpdateInfo: %v != %v", uu, u)
}
e := doUpdate(&u) e := doUpdate(&u)
if e != nil { if e != nil {
@ -66,20 +78,20 @@ func TestDoUpdate(t *testing.T) {
} }
func TestTargzFileUnpack(t *testing.T) { func TestTargzFileUnpack(t *testing.T) {
fn := "./dist/AdGuardHome_v0.95_linux_amd64.tar.gz" fn := "../dist/AdGuardHome_linux_amd64.tar.gz"
outdir := "./test-unpack" outdir := "../test-unpack"
defer os.RemoveAll(outdir)
_ = os.Mkdir(outdir, 0755) _ = os.Mkdir(outdir, 0755)
files, e := targzFileUnpack(fn, outdir) files, e := targzFileUnpack(fn, outdir)
if e != nil { if e != nil {
t.Fatalf("FAILED: %s", e) t.Fatalf("FAILED: %s", e)
} }
t.Logf("%v", files) t.Logf("%v", files)
os.RemoveAll(outdir)
} }
func TestZipFileUnpack(t *testing.T) { func TestZipFileUnpack(t *testing.T) {
fn := "./dist/AdGuardHome_v0.95_Windows_amd64.zip" fn := "../dist/AdGuardHome_windows_amd64.zip"
outdir := "./test-unpack" outdir := "../test-unpack"
_ = os.Mkdir(outdir, 0755) _ = os.Mkdir(outdir, 0755)
files, e := zipFileUnpack(fn, outdir) files, e := zipFileUnpack(fn, outdir)
if e != nil { if e != nil {