1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-23 01:20:11 +00:00

Fix updater for zip files

Forgot to change this when I switched, oops.
This commit is contained in:
Harvey Tindall 2021-05-23 23:05:40 +01:00
parent e01144950b
commit e78b4882b3
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2

View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"archive/tar" "archive/zip"
"compress/gzip" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -392,27 +392,20 @@ func (ud *Updater) pullInternal(url string) (applyUpdate ApplyUpdate, status int
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
gz, err := gzip.NewReader(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
status = -1 status = -1
return return
} }
defer gz.Close() zp, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
tarReader := tar.NewReader(gz)
var header *tar.Header
for {
header, err = tarReader.Next()
if err == io.EOF {
break
}
if err != nil { if err != nil {
status = -1 status = -1
return return
} }
switch header.Typeflag { for _, zf := range zp.File {
case tar.TypeReg: if zf.Name != ud.binary {
// Search only for file named ud.binary continue
if header.Name == ud.binary { }
var file string var file string
file, err = os.Executable() file, err = os.Executable()
if err != nil { if err != nil {
@ -429,13 +422,19 @@ func (ud *Updater) pullInternal(url string) (applyUpdate ApplyUpdate, status int
return return
} }
mode := info.Mode() mode := info.Mode()
var unzippedFile io.ReadCloser
unzippedFile, err = zf.Open()
if err != nil {
return
}
defer unzippedFile.Close()
var f *os.File var f *os.File
f, err = os.OpenFile(path+"_", os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode) f, err = os.OpenFile(path+"_", os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
if err != nil { if err != nil {
return return
} }
defer f.Close() defer f.Close()
_, err = io.Copy(f, tarReader) _, err = io.Copy(f, unzippedFile)
if err != nil { if err != nil {
return return
} }
@ -453,8 +452,69 @@ func (ud *Updater) pullInternal(url string) (applyUpdate ApplyUpdate, status int
} }
return return
} }
} // gz, err := gzip.NewReader(resp.Body)
} // if err != nil {
// status = -1
// return
// }
// defer gz.Close()
// tarReader := tar.NewReader(gz)
// var header *tar.Header
// for {
// header, err = tarReader.Next()
// if err == io.EOF {
// break
// }
// if err != nil {
// status = -1
// return
// }
// switch header.Typeflag {
// case tar.TypeReg:
// // Search only for file named ud.binary
// if header.Name == ud.binary {
// var file string
// file, err = os.Executable()
// if err != nil {
// return
// }
// var path string
// path, err = filepath.EvalSymlinks(file)
// if err != nil {
// return
// }
// var info fs.FileInfo
// info, err = os.Stat(path)
// if err != nil {
// return
// }
// mode := info.Mode()
// var f *os.File
// f, err = os.OpenFile(path+"_", os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
// if err != nil {
// return
// }
// defer f.Close()
// _, err = io.Copy(f, tarReader)
// if err != nil {
// return
// }
// applyUpdate = func() error {
// oldName := path + "-" + version + "-" + commit
// err := os.Rename(path, oldName)
// if err != nil {
// return err
// }
// err = os.Rename(path+"_", path)
// if err != nil {
// return err
// }
// return os.Remove(oldName)
// }
// return
// }
// }
// }
err = errors.New("Couldn't find file: " + ud.binary) err = errors.New("Couldn't find file: " + ud.binary)
return return
} }