|
| 1 | +// Copyright 2018 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// The txtar-x command extracts a txtar archive to a filesystem. |
| 6 | +// |
| 7 | +// Usage: |
| 8 | +// |
| 9 | +// txtar-x [-C root-dir] saved.txt |
| 10 | +// |
| 11 | +// See https://godoc.org/github.com/rogpeppe/go-internal/txtar for details of the format |
| 12 | +// and how to parse a txtar file. |
| 13 | +// |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "flag" |
| 18 | + "fmt" |
| 19 | + "io/ioutil" |
| 20 | + "log" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/rogpeppe/go-internal/txtar" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + extractDir = flag.String("C", ".", "directory to extract files into") |
| 30 | +) |
| 31 | + |
| 32 | +func usage() { |
| 33 | + fmt.Fprintf(os.Stderr, "usage: txtar-x [flags] [file]\n") |
| 34 | + flag.PrintDefaults() |
| 35 | +} |
| 36 | + |
| 37 | +func main() { |
| 38 | + os.Exit(main1()) |
| 39 | +} |
| 40 | + |
| 41 | +func main1() int { |
| 42 | + flag.Usage = usage |
| 43 | + flag.Parse() |
| 44 | + if flag.NArg() > 1 { |
| 45 | + usage() |
| 46 | + return 2 |
| 47 | + } |
| 48 | + log.SetPrefix("txtar-x: ") |
| 49 | + log.SetFlags(0) |
| 50 | + |
| 51 | + var a *txtar.Archive |
| 52 | + if flag.NArg() == 0 { |
| 53 | + data, err := ioutil.ReadAll(os.Stdin) |
| 54 | + if err != nil { |
| 55 | + log.Printf("cannot read stdin: %v", err) |
| 56 | + return 1 |
| 57 | + } |
| 58 | + a = txtar.Parse(data) |
| 59 | + } else { |
| 60 | + a1, err := txtar.ParseFile(flag.Arg(0)) |
| 61 | + if err != nil { |
| 62 | + log.Print(err) |
| 63 | + return 1 |
| 64 | + } |
| 65 | + a = a1 |
| 66 | + } |
| 67 | + if err := extract(a); err != nil { |
| 68 | + log.Print(err) |
| 69 | + return 1 |
| 70 | + } |
| 71 | + return 0 |
| 72 | +} |
| 73 | + |
| 74 | +func extract(a *txtar.Archive) error { |
| 75 | + for _, f := range a.Files { |
| 76 | + if err := extractFile(f); err != nil { |
| 77 | + return fmt.Errorf("cannot extract %q: %v", f.Name, err) |
| 78 | + } |
| 79 | + } |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func extractFile(f txtar.File) error { |
| 84 | + path := filepath.Clean(filepath.FromSlash(f.Name)) |
| 85 | + if isAbs(path) || strings.HasPrefix(path, ".."+string(filepath.Separator)) { |
| 86 | + return fmt.Errorf("outside parent directory") |
| 87 | + } |
| 88 | + path = filepath.Join(*extractDir, path) |
| 89 | + if err := os.MkdirAll(filepath.Dir(path), 0777); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + // Avoid overwriting existing files by using O_EXCL. |
| 93 | + out, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + defer out.Close() |
| 98 | + if _, err := out.Write(f.Data); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func isAbs(p string) bool { |
| 105 | + // Note: under Windows, filepath.IsAbs(`\foo`) returns false, |
| 106 | + // so we need to check for that case specifically. |
| 107 | + return filepath.IsAbs(p) || strings.HasPrefix(p, string(filepath.Separator)) |
| 108 | +} |
0 commit comments