|
| 1 | +// Copyright 2023 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 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "io/fs" |
| 9 | + "log" |
| 10 | + "os" |
| 11 | + "path" |
| 12 | + "path/filepath" |
| 13 | + "sort" |
| 14 | + "strings" |
| 15 | + "time" |
| 16 | +) |
| 17 | + |
| 18 | +// An Archive describes an archive to write: a collection of files. |
| 19 | +// Directories are implied by the files and not explicitly listed. |
| 20 | +type Archive struct { |
| 21 | + Files []File |
| 22 | +} |
| 23 | + |
| 24 | +// A File describes a single file to write to an archive. |
| 25 | +type File struct { |
| 26 | + Name string // name in archive |
| 27 | + Time time.Time // modification time |
| 28 | + Mode fs.FileMode |
| 29 | + Size int64 |
| 30 | + Src string // source file in OS file system |
| 31 | +} |
| 32 | + |
| 33 | +// Info returns a FileInfo about the file, for use with tar.FileInfoHeader |
| 34 | +// and zip.FileInfoHeader. |
| 35 | +func (f *File) Info() fs.FileInfo { |
| 36 | + return fileInfo{f} |
| 37 | +} |
| 38 | + |
| 39 | +// A fileInfo is an implementation of fs.FileInfo describing a File. |
| 40 | +type fileInfo struct { |
| 41 | + f *File |
| 42 | +} |
| 43 | + |
| 44 | +func (i fileInfo) Name() string { return path.Base(i.f.Name) } |
| 45 | +func (i fileInfo) ModTime() time.Time { return i.f.Time } |
| 46 | +func (i fileInfo) Mode() fs.FileMode { return i.f.Mode } |
| 47 | +func (i fileInfo) IsDir() bool { return false } |
| 48 | +func (i fileInfo) Size() int64 { return i.f.Size } |
| 49 | +func (i fileInfo) Sys() any { return nil } |
| 50 | + |
| 51 | +// NewArchive returns a new Archive containing all the files in the directory dir. |
| 52 | +// The archive can be amended afterward using methods like Add and Filter. |
| 53 | +func NewArchive(dir string) (*Archive, error) { |
| 54 | + a := new(Archive) |
| 55 | + err := fs.WalkDir(os.DirFS(dir), ".", func(name string, d fs.DirEntry, err error) error { |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + if d.IsDir() { |
| 60 | + return nil |
| 61 | + } |
| 62 | + info, err := d.Info() |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + a.Add(name, filepath.Join(dir, name), info) |
| 67 | + return nil |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + a.Sort() |
| 73 | + return a, nil |
| 74 | +} |
| 75 | + |
| 76 | +// Add adds a file with the given name and info to the archive. |
| 77 | +// The content of the file comes from the operating system file src. |
| 78 | +// After a sequence of one or more calls to Add, |
| 79 | +// the caller should invoke Sort to re-sort the archive's files. |
| 80 | +func (a *Archive) Add(name, src string, info fs.FileInfo) { |
| 81 | + a.Files = append(a.Files, File{ |
| 82 | + Name: name, |
| 83 | + Time: info.ModTime(), |
| 84 | + Mode: info.Mode(), |
| 85 | + Size: info.Size(), |
| 86 | + Src: src, |
| 87 | + }) |
| 88 | +} |
| 89 | + |
| 90 | +// Sort sorts the files in the archive. |
| 91 | +// It is only necessary to call Sort after calling Add. |
| 92 | +// ArchiveDir returns a sorted archive, and the other methods |
| 93 | +// preserve the sorting of the archive. |
| 94 | +func (a *Archive) Sort() { |
| 95 | + sort.Slice(a.Files, func(i, j int) bool { |
| 96 | + return a.Files[i].Name < a.Files[j].Name |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +// Clone returns a copy of the Archive. |
| 101 | +// Method calls like Add and Filter invoked on the copy do not affect the original, |
| 102 | +// nor do calls on the original affect the copy. |
| 103 | +func (a *Archive) Clone() *Archive { |
| 104 | + b := &Archive{ |
| 105 | + Files: make([]File, len(a.Files)), |
| 106 | + } |
| 107 | + copy(b.Files, a.Files) |
| 108 | + return b |
| 109 | +} |
| 110 | + |
| 111 | +// AddPrefix adds a prefix to all file names in the archive. |
| 112 | +func (a *Archive) AddPrefix(prefix string) { |
| 113 | + for i := range a.Files { |
| 114 | + a.Files[i].Name = path.Join(prefix, a.Files[i].Name) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// Filter removes files from the archive for which keep(name) returns false. |
| 119 | +func (a *Archive) Filter(keep func(name string) bool) { |
| 120 | + files := a.Files[:0] |
| 121 | + for _, f := range a.Files { |
| 122 | + if keep(f.Name) { |
| 123 | + files = append(files, f) |
| 124 | + } |
| 125 | + } |
| 126 | + a.Files = files |
| 127 | +} |
| 128 | + |
| 129 | +// SetMode changes the mode of every file in the archive |
| 130 | +// to be mode(name, m), where m is the file's current mode. |
| 131 | +func (a *Archive) SetMode(mode func(name string, m fs.FileMode) fs.FileMode) { |
| 132 | + for i := range a.Files { |
| 133 | + a.Files[i].Mode = mode(a.Files[i].Name, a.Files[i].Mode) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// Remove removes files matching any of the patterns from the archive. |
| 138 | +// The patterns use the syntax of path.Match, with an extension of allowing |
| 139 | +// a leading **/ or trailing /**, which match any number of path elements |
| 140 | +// (including no path elements) before or after the main match. |
| 141 | +func (a *Archive) Remove(patterns ...string) { |
| 142 | + a.Filter(func(name string) bool { |
| 143 | + for _, pattern := range patterns { |
| 144 | + match, err := amatch(pattern, name) |
| 145 | + if err != nil { |
| 146 | + log.Fatalf("archive remove: %v", err) |
| 147 | + } |
| 148 | + if match { |
| 149 | + return false |
| 150 | + } |
| 151 | + } |
| 152 | + return true |
| 153 | + }) |
| 154 | +} |
| 155 | + |
| 156 | +// SetTime sets the modification time of all files in the archive to t. |
| 157 | +func (a *Archive) SetTime(t time.Time) { |
| 158 | + for i := range a.Files { |
| 159 | + a.Files[i].Time = t |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func amatch(pattern, name string) (bool, error) { |
| 164 | + // firstN returns the prefix of name corresponding to the first n path elements. |
| 165 | + // If n <= 0, firstN returns the entire name. |
| 166 | + firstN := func(name string, n int) string { |
| 167 | + for i := 0; i < len(name); i++ { |
| 168 | + if name[i] == '/' { |
| 169 | + if n--; n == 0 { |
| 170 | + return name[:i] |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + return name |
| 175 | + } |
| 176 | + |
| 177 | + // lastN returns the suffix of name corresponding to the last n path elements. |
| 178 | + // If n <= 0, lastN returns the entire name. |
| 179 | + lastN := func(name string, n int) string { |
| 180 | + for i := len(name) - 1; i >= 0; i-- { |
| 181 | + if name[i] == '/' { |
| 182 | + if n--; n == 0 { |
| 183 | + return name[i+1:] |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + return name |
| 188 | + } |
| 189 | + |
| 190 | + if p, ok := strings.CutPrefix(pattern, "**/"); ok { |
| 191 | + return path.Match(p, lastN(name, 1+strings.Count(p, "/"))) |
| 192 | + } |
| 193 | + if p, ok := strings.CutSuffix(pattern, "/**"); ok { |
| 194 | + return path.Match(p, firstN(name, 1+strings.Count(p, "/"))) |
| 195 | + } |
| 196 | + return path.Match(pattern, name) |
| 197 | +} |
0 commit comments