Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Dominic Della Valle <ddvpublic@gmail.com>
  • Loading branch information
djdv committed Jun 14, 2018
1 parent b8e6087 commit f4f3ca7
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions core/commands/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,37 +253,51 @@ func (gw *getWriter) writeExtracted(r io.Reader, fpath string) error {
defer bar.Set64(gw.Size)

extractor := &tar.Extractor{Path: fpath, Progress: bar.Add64}
sanitizeExtractor(extractor, gw.Out)

return extractor.Extract(r)
}

func sanitizeExtractor(ex *tar.Extractor, logOut io.Writer) {
if osh.IsWindows() {
extractor.Sanitize(true)
builtinSanitizer := extractor.SanitizePathFunc
extractor.SanitizePathFunc = func(path string) (string, error) {
sanitizedPath, err := builtinSanitizer(path)
inBase, outBase := gopath.Base(path), filepath.Base(sanitizedPath)
if inBase != outBase {
modified = true
fmt.Fprintf(gw.Out, "%q extracted as %q\n", inBase, outBase)
}
return sanitizedPath, err
extractor.SanitizePathFunc = winSanitize(extractor, logOut)
extractor.LinkFunc = winLink
}
}

// winSanitize wraps the built in sanitizer, adding logging
func winSanitize(ex *tar.Extractor, logOut io.Writer) func(string) (string, err) {
ex.Sanitize(true)
if logOut == nil {
return ex.SanitizePathFunc
}
builtinSanitizer := ex.SanitizePathFunc
return func(path string) (string, error) {
sanitizedPath, err := builtinSanitizer(path)
inBase, outBase := gopath.Base(path), filepath.Base(sanitizedPath)
if inBase != outBase {
modified = true
fmt.Fprintf(gw.Out, "%q extracted as %q\n", inBase, outBase)
}
extractor.LinkFunc = func(l tar.Link) error {
if _, err := os.Lstat(l.Name); err == nil {
if err = os.Remove(l.Name); err != nil {
return err
}
}
return sanitizedPath, err
}

err := os.Symlink(l.Target, l.Name)
if err != nil {
if haveLinkCreatePriviledge { // non privilege errors
return err
}
return errors.Errorf("Symlink %q->%q cannot be created, user does not have symlink creation privileges (see:https://git.io/vpHKV)", l.Name, l.Target)
}
}

func winLink(l tar.Link) error {
if _, err := os.Lstat(l.Name); err == nil {
if err = os.Remove(l.Name); err != nil {
return err
}
}

err := extractor.Extract(r)
return err
err := os.Symlink(l.Target, l.Name)
if err != nil {
if haveLinkCreatePriviledge { // non privilege errors
return err
}
return errors.Errorf("Symlink %q->%q cannot be created, user does not have symlink creation privileges (see: https://git.io/vpHKV)", l.Name, l.Target)
}
}

func getCompressOptions(req *cmds.Request) (int, error) {
Expand Down

0 comments on commit f4f3ca7

Please sign in to comment.