Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom zip extraction method #9

Merged
merged 1 commit into from
Jun 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions src/nwjs-autoupdater/glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/nwjs-autoupdater/glide.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package: nwjs-autoupdater
import:
- package: github.com/ivaxer/go-xattr
- package: github.com/mholt/archiver
version: ^1.1.1
- package: github.com/skratchdot/open-golang
subpackages:
- open
82 changes: 82 additions & 0 deletions src/nwjs-autoupdater/unzip/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package unzip

import (
"archive/zip"
"io"
"os"
"path/filepath"
)

// Unzip a file to a destination and preserve the symlinks
// Based on http://stackoverflow.com/a/24792688/842097 with symlink additions
func Unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()

os.MkdirAll(dest, 0755)

// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()

path := filepath.Join(dest, f.Name)

if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else if f.FileInfo().Mode()&os.ModeSymlink != 0 {
buffer := make([]byte, f.FileInfo().Size())
size, err := rc.Read(buffer)
if err != nil {
return err
}

target := string(buffer[:size])

err = os.Symlink(target, path)
if err != nil {
return err
}
} else {
os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err = f.Close(); err != nil {
panic(err)
}
}()

_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
return nil
}

for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
}

return nil
}
4 changes: 2 additions & 2 deletions src/nwjs-autoupdater/updater/updater_darwin.go
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ import (
"io/ioutil"
"os"
"path/filepath"
"nwjs-autoupdater/unzip"

"github.com/mholt/archiver"
"github.com/ivaxer/go-xattr"
)

@@ -21,7 +21,7 @@ func Update(bundle, instDir, appName string) (error, string) {
}
defer os.RemoveAll(tempDir)

err = archiver.Zip.Open(bundle, tempDir)
err = unzip.Unzip(bundle, tempDir)
if err != nil {
return err, appExec
}
4 changes: 2 additions & 2 deletions src/nwjs-autoupdater/updater/updater_windows.go
Original file line number Diff line number Diff line change
@@ -3,14 +3,14 @@ package updater
import (
"path/filepath"

"github.com/mholt/archiver"
"nwjs-autoupdater/unzip"
)

func Update(bundle, instDir, appName string) (error, string) {
appExecName := appName + ".exe"
appExec := filepath.Join(instDir, appExecName)

err := archiver.Zip.Open(bundle, instDir)
err := unzip.Unzip(bundle, instDir)
if err != nil {
return err, appExec
}