|
| 1 | +package archiver |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +// Archiver represent a archive format |
| 14 | +type Archiver interface { |
| 15 | + // Match checks supported files |
| 16 | + Match(filename string) bool |
| 17 | + // Make makes an archive file on disk. |
| 18 | + Make(destination string, sources []string) error |
| 19 | + // Open extracts an archive file on disk. |
| 20 | + Open(source, destination string) error |
| 21 | + // Write writes an archive to a Writer. |
| 22 | + Write(output io.Writer, sources []string) error |
| 23 | + // Read reads an archive from a Reader. |
| 24 | + Read(input io.Reader, destination string) error |
| 25 | +} |
| 26 | + |
| 27 | +// SupportedFormats contains all supported archive formats |
| 28 | +var SupportedFormats = map[string]Archiver{} |
| 29 | + |
| 30 | +// RegisterFormat adds a supported archive format |
| 31 | +func RegisterFormat(name string, format Archiver) { |
| 32 | + if _, ok := SupportedFormats[name]; ok { |
| 33 | + log.Printf("Format %s already exists, skip!\n", name) |
| 34 | + return |
| 35 | + } |
| 36 | + SupportedFormats[name] = format |
| 37 | +} |
| 38 | + |
| 39 | +// MatchingFormat returns the first archive format that matches |
| 40 | +// the given file, or nil if there is no match |
| 41 | +func MatchingFormat(fpath string) Archiver { |
| 42 | + for _, fmt := range SupportedFormats { |
| 43 | + if fmt.Match(fpath) { |
| 44 | + return fmt |
| 45 | + } |
| 46 | + } |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +func writeNewFile(fpath string, in io.Reader, fm os.FileMode) error { |
| 51 | + err := os.MkdirAll(filepath.Dir(fpath), 0755) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("%s: making directory for file: %v", fpath, err) |
| 54 | + } |
| 55 | + |
| 56 | + out, err := os.Create(fpath) |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("%s: creating new file: %v", fpath, err) |
| 59 | + } |
| 60 | + defer out.Close() |
| 61 | + |
| 62 | + err = out.Chmod(fm) |
| 63 | + if err != nil && runtime.GOOS != "windows" { |
| 64 | + return fmt.Errorf("%s: changing file mode: %v", fpath, err) |
| 65 | + } |
| 66 | + |
| 67 | + _, err = io.Copy(out, in) |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("%s: writing file: %v", fpath, err) |
| 70 | + } |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func writeNewSymbolicLink(fpath string, target string) error { |
| 75 | + err := os.MkdirAll(filepath.Dir(fpath), 0755) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("%s: making directory for file: %v", fpath, err) |
| 78 | + } |
| 79 | + |
| 80 | + _, err = os.Lstat(fpath) |
| 81 | + if err == nil { |
| 82 | + err = os.Remove(fpath) |
| 83 | + if err != nil { |
| 84 | + return fmt.Errorf("%s: failed to unlink: %+v", fpath, err) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + err = os.Symlink(target, fpath) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("%s: making symbolic link for: %v", fpath, err) |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func writeNewHardLink(fpath string, target string) error { |
| 97 | + err := os.MkdirAll(filepath.Dir(fpath), 0755) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("%s: making directory for file: %v", fpath, err) |
| 100 | + } |
| 101 | + |
| 102 | + _, err = os.Lstat(fpath) |
| 103 | + if err == nil { |
| 104 | + err = os.Remove(fpath) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("%s: failed to unlink: %+v", fpath, err) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + err = os.Link(target, fpath) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("%s: making hard link for: %v", fpath, err) |
| 113 | + } |
| 114 | + |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +func mkdir(dirPath string) error { |
| 119 | + err := os.MkdirAll(dirPath, 0755) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("%s: making directory: %v", dirPath, err) |
| 122 | + } |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func sanitizeExtractPath(filePath string, destination string) error { |
| 127 | + // to avoid zip slip (writing outside of the destination), we resolve |
| 128 | + // the target path, and make sure it's nested in the intended |
| 129 | + // destination, or bail otherwise. |
| 130 | + destpath := filepath.Join(destination, filePath) |
| 131 | + if !strings.HasPrefix(destpath, filepath.Clean(destination)) { |
| 132 | + return fmt.Errorf("%s: illegal file path", filePath) |
| 133 | + } |
| 134 | + return nil |
| 135 | +} |
0 commit comments