| 1 | package fsutil |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "io" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | |
| 11 | "github.com/tonistiigi/fsutil/types" |
| 12 | "golang.org/x/sync/errgroup" |
| 13 | ) |
| 14 | |
| 15 | // Everything below is copied from containerd/fs. TODO: remove duplication @dmcgowan |
| 16 | |
| 17 | // Const redefined because containerd/fs doesn't build on !linux |
| 18 | |
| 19 | // ChangeKind is the type of modification that |
| 20 | // a change is making. |
| 21 | type ChangeKind int |
| 22 | |
| 23 | const ( |
| 24 | // ChangeKindAdd represents an addition of |
| 25 | // a file |
| 26 | ChangeKindAdd ChangeKind = iota |
| 27 | |
| 28 | // ChangeKindModify represents a change to |
| 29 | // an existing file |
| 30 | ChangeKindModify |
| 31 | |
| 32 | // ChangeKindDelete represents a delete of |
| 33 | // a file |
| 34 | ChangeKindDelete |
| 35 | ) |
| 36 | |
| 37 | func (k ChangeKind) String() string { |
| 38 | switch k { |
| 39 | case ChangeKindAdd: |
| 40 | return "add" |
| 41 | case ChangeKindModify: |
| 42 | return "modify" |
| 43 | case ChangeKindDelete: |
| 44 | return "delete" |
| 45 | default: |
| 46 | return "unknown" |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // ChangeFunc is the type of function called for each change |
| 51 | // computed during a directory changes calculation. |
| 52 | type ChangeFunc func(ChangeKind, string, os.FileInfo, error) error |
| 53 | |
| 54 | const compareChunkSize = 32 * 1024 |
| 55 | |
| 56 | type currentPath struct { |
| 57 | path string |
| 58 | stat *types.Stat |
| 59 | // fullPath string |
| 60 | } |
| 61 | |
| 62 | // doubleWalkDiff walks both directories to create a diff |
| 63 | func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b walkerFn, filter FilterFunc, differ DiffType) (err error) { |
| 64 | g, ctx := errgroup.WithContext(ctx) |
| 65 | |
| 66 | var ( |
| 67 | c1 = make(chan *currentPath, 128) |
| 68 | c2 = make(chan *currentPath, 128) |
| 69 | |
| 70 | f1, f2 *currentPath |
| 71 | rmdir string |
| 72 | ) |
| 73 | g.Go(func() error { |
| 74 | defer close(c1) |
| 75 | return a(ctx, c1) |
| 76 | }) |
| 77 | g.Go(func() error { |
| 78 | defer close(c2) |
| 79 | return b(ctx, c2) |
| 80 | }) |
| 81 | g.Go(func() error { |
| 82 | loop0: |
| 83 | for c1 != nil || c2 != nil { |
| 84 | if f1 == nil && c1 != nil { |
| 85 | f1, err = nextPath(ctx, c1) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | if f1 == nil { |
| 90 | c1 = nil |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if f2 == nil && c2 != nil { |
| 95 | f2, err = nextPath(ctx, c2) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | if f2 == nil { |
| 100 | c2 = nil |
| 101 | } |
| 102 | } |
| 103 | if f1 == nil && f2 == nil { |
| 104 | continue |
| 105 | } |
| 106 | |
| 107 | var f *types.Stat |
| 108 | var f2copy *currentPath |
| 109 | if f2 != nil { |
| 110 | statCopy := f2.stat.Clone() |
| 111 | if filter != nil { |
| 112 | filter(f2.path, statCopy) |
| 113 | } |
| 114 | f2copy = ¤tPath{path: f2.path, stat: statCopy} |
| 115 | } |
| 116 | k, p := pathChange(f1, f2copy) |
| 117 | switch k { |
| 118 | case ChangeKindAdd: |
| 119 | if rmdir != "" { |
| 120 | rmdir = "" |
| 121 | } |
| 122 | f = f2.stat |
| 123 | f2 = nil |
| 124 | case ChangeKindDelete: |
| 125 | // Check if this file is already removed by being |
| 126 | // under of a removed directory |
| 127 | if rmdir != "" && strings.HasPrefix(f1.path, rmdir) { |
| 128 | f1 = nil |
| 129 | continue |
| 130 | } else if rmdir == "" && f1.stat.IsDir() { |
| 131 | rmdir = f1.path + string(filepath.Separator) |
| 132 | } else if rmdir != "" { |
| 133 | rmdir = "" |
| 134 | } |
| 135 | f1 = nil |
| 136 | case ChangeKindModify: |
| 137 | same, err := sameFile(f1, f2copy, differ) |
| 138 | if err != nil { |
| 139 | return err |
| 140 | } |
| 141 | if f1.stat.IsDir() && !f2copy.stat.IsDir() { |
| 142 | rmdir = f1.path + string(filepath.Separator) |
| 143 | } else if rmdir != "" { |
| 144 | rmdir = "" |
| 145 | } |
| 146 | f = f2.stat |
| 147 | f1 = nil |
| 148 | f2 = nil |
| 149 | if same { |
| 150 | continue loop0 |
| 151 | } |
| 152 | } |
| 153 | if err := changeFn(k, p, &StatInfo{f}, nil); err != nil { |
| 154 | return err |
| 155 | } |
| 156 | } |
| 157 | return nil |
| 158 | }) |
| 159 | |
| 160 | return g.Wait() |
| 161 | } |
| 162 | |
| 163 | func pathChange(lower, upper *currentPath) (ChangeKind, string) { |
| 164 | if lower == nil { |
| 165 | if upper == nil { |
| 166 | panic("cannot compare nil paths") |
| 167 | } |
| 168 | return ChangeKindAdd, upper.path |
| 169 | } |
| 170 | if upper == nil { |
| 171 | return ChangeKindDelete, lower.path |
| 172 | } |
| 173 | |
| 174 | switch i := ComparePath(lower.path, upper.path); { |
| 175 | case i < 0: |
| 176 | // File in lower that is not in upper |
| 177 | return ChangeKindDelete, lower.path |
| 178 | case i > 0: |
| 179 | // File in upper that is not in lower |
| 180 | return ChangeKindAdd, upper.path |
| 181 | default: |
| 182 | return ChangeKindModify, upper.path |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | func sameFile(f1, f2 *currentPath, differ DiffType) (same bool, retErr error) { |
| 187 | if differ == DiffNone { |
| 188 | return false, nil |
| 189 | } |
| 190 | // If not a directory also check size, modtime, and content |
| 191 | if !f1.stat.IsDir() { |
| 192 | if f1.stat.Size != f2.stat.Size { |
| 193 | return false, nil |
| 194 | } |
| 195 | |
| 196 | if f1.stat.ModTime != f2.stat.ModTime { |
| 197 | return false, nil |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | same, err := compareStat(f1.stat, f2.stat) |
| 202 | if err != nil || !same || differ == DiffMetadata { |
| 203 | return same, err |
| 204 | } |
| 205 | return compareFileContent(f1.path, f2.path) |
| 206 | } |
| 207 | |
| 208 | func compareFileContent(p1, p2 string) (bool, error) { |
| 209 | f1, err := os.Open(p1) |
| 210 | if err != nil { |
| 211 | return false, err |
| 212 | } |
| 213 | defer f1.Close() |
| 214 | f2, err := os.Open(p2) |
| 215 | if err != nil { |
| 216 | return false, err |
| 217 | } |
| 218 | defer f2.Close() |
| 219 | |
| 220 | b1 := make([]byte, compareChunkSize) |
| 221 | b2 := make([]byte, compareChunkSize) |
| 222 | for { |
| 223 | n1, err1 := f1.Read(b1) |
| 224 | if err1 != nil && err1 != io.EOF { |
| 225 | return false, err1 |
| 226 | } |
| 227 | n2, err2 := f2.Read(b2) |
| 228 | if err2 != nil && err2 != io.EOF { |
| 229 | return false, err2 |
| 230 | } |
| 231 | if n1 != n2 || !bytes.Equal(b1[:n1], b2[:n2]) { |
| 232 | return false, nil |
| 233 | } |
| 234 | if err1 == io.EOF && err2 == io.EOF { |
| 235 | return true, nil |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // compareStat returns whether the stats are equivalent, |
| 241 | // whether the files are considered the same file, and |
| 242 | // an error |
| 243 | func compareStat(ls1, ls2 *types.Stat) (bool, error) { |
| 244 | return ls1.Mode == ls2.Mode && ls1.Uid == ls2.Uid && ls1.Gid == ls2.Gid && ls1.Devmajor == ls2.Devmajor && ls1.Devminor == ls2.Devminor && ls1.Linkname == ls2.Linkname, nil |
| 245 | } |
| 246 | |
| 247 | func nextPath(ctx context.Context, pathC <-chan *currentPath) (*currentPath, error) { |
| 248 | select { |
| 249 | case <-ctx.Done(): |
| 250 | return nil, ctx.Err() |
| 251 | case p := <-pathC: |
| 252 | return p, nil |
| 253 | } |
| 254 | } |