1
0
mirror of https://github.com/hrfee/jfa-go.git synced 2024-12-22 17:10:10 +00:00

updater: immediately store executable

for some reason I kept the response body and downloaded file in memory,
which led to timeouts and failed updates.
This commit is contained in:
Harvey Tindall 2021-04-07 18:17:18 +01:00
parent d51a6abb02
commit 2687af31ca
Signed by: hrfee
GPG Key ID: BBC65952848FB1A2

View File

@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/fs"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -390,11 +391,13 @@ func (ud *Updater) pullInternal(url string) (applyUpdate ApplyUpdate, status int
if err != nil || resp.StatusCode != 200 { if err != nil || resp.StatusCode != 200 {
return return
} }
defer resp.Body.Close()
gz, err := gzip.NewReader(resp.Body) gz, err := gzip.NewReader(resp.Body)
if err != nil { if err != nil {
status = -1 status = -1
return return
} }
defer gz.Close()
tarReader := tar.NewReader(gz) tarReader := tar.NewReader(gz)
var header *tar.Header var header *tar.Header
for { for {
@ -410,31 +413,33 @@ func (ud *Updater) pullInternal(url string) (applyUpdate ApplyUpdate, status int
case tar.TypeReg: case tar.TypeReg:
// Search only for file named ud.binary // Search only for file named ud.binary
if header.Name == ud.binary { if header.Name == ud.binary {
applyUpdate = func() error { var file string
defer gz.Close() file, err = os.Executable()
defer resp.Body.Close()
file, err := os.Executable()
if err != nil { if err != nil {
return err return
} }
path, err := filepath.EvalSymlinks(file) var path string
path, err = filepath.EvalSymlinks(file)
if err != nil { if err != nil {
return err return
} }
info, err := os.Stat(path) var info fs.FileInfo
info, err = os.Stat(path)
if err != nil { if err != nil {
return err return
} }
mode := info.Mode() mode := info.Mode()
f, err := os.OpenFile(path+"_", os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode) var f *os.File
f, err = os.OpenFile(path+"_", os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode)
if err != nil { if err != nil {
return err return
} }
defer f.Close() defer f.Close()
_, err = io.Copy(f, tarReader) _, err = io.Copy(f, tarReader)
if err != nil { if err != nil {
return err return
} }
applyUpdate = func() error {
return os.Rename(path+"_", path) return os.Rename(path+"_", path)
} }
return return