Skip to content

Commit

Permalink
fix: use custom zip extraction method
Browse files Browse the repository at this point in the history
mholt/archiver package can't currently handle symbolic links

This adds custom zip extraction based on
mholt/archiver#21 (comment)

Closes #8
  • Loading branch information
oaleynik committed Jun 14, 2017
1 parent 687812b commit 2fa6e97
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 19 deletions.
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
Expand Up @@ -4,8 +4,8 @@ import (
"io/ioutil"
"os"
"path/filepath"
"nwjs-autoupdater/unzip"

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

Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/nwjs-autoupdater/updater/updater_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 2fa6e97

Please sign in to comment.