rollback file unpack

This commit is contained in:
Andrey Meshkov 2020-07-20 20:15:05 +03:00
parent a409cdc2bb
commit 62ccd3fb41

View File

@ -11,7 +11,6 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@ -245,7 +244,6 @@ 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)
@ -349,6 +347,7 @@ 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)
@ -377,11 +376,15 @@ func targzFileUnpack(tarfile, outdir string) ([]string, error) {
continue continue
} }
filename := path.Base(header.Name) fn := filepath.Join(outdir, header.Name)
fn := filepath.Join(outdir, filename)
if header.Typeflag == tar.TypeDir { if header.Typeflag == tar.TypeDir {
log.Tracef("%s: ignoring directory", fn) err = os.Mkdir(fn, os.FileMode(header.Mode&0777))
if err != nil && !os.IsExist(err) {
err2 = fmt.Errorf("os.Mkdir(%s): %s", fn, err)
break
}
log.Tracef("created directory %s", fn)
continue continue
} else if header.Typeflag != tar.TypeReg { } else if header.Typeflag != tar.TypeReg {
log.Tracef("%s: unknown file type %d, skipping", header.Name, header.Typeflag) log.Tracef("%s: unknown file type %d, skipping", header.Name, header.Typeflag)
@ -395,17 +398,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, filename) files = append(files, header.Name)
} }
_ = gzReader.Close() gzReader.Close()
return files, err2 return files, err2
} }