| 1 |
// Copyright 2018 the u-root 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 cmp compares trees of files. |
| 6 |
// |
| 7 |
// cmp is an internal package for pkg/cp's and cmds/core/cp's tests. |
| 8 |
package cmp |
| 9 |
|
| 10 |
import ( |
| 11 |
"fmt" |
| 12 |
"os" |
| 13 |
"path/filepath" |
| 14 |
"reflect" |
| 15 |
|
| 16 |
"github.com/u-root/uio/cp" |
| 17 |
"github.com/u-root/uio/uio" |
| 18 |
) |
| 19 |
|
| 20 |
// isEqualFile compare two files by checksum. |
| 21 |
func isEqualFile(fpath1, fpath2 string) error { |
| 22 |
file1, err := os.Open(fpath1) |
| 23 |
if err != nil { |
| 24 |
return err |
| 25 |
} |
| 26 |
defer file1.Close() |
| 27 |
file2, err := os.Open(fpath2) |
| 28 |
if err != nil { |
| 29 |
return err |
| 30 |
} |
| 31 |
defer file2.Close() |
| 32 |
|
| 33 |
if !uio.ReaderAtEqual(file1, file2) { |
| 34 |
return fmt.Errorf("%q and %q do not have equal content", fpath1, fpath2) |
| 35 |
} |
| 36 |
return nil |
| 37 |
} |
| 38 |
|
| 39 |
func readDirNames(path string) ([]string, error) { |
| 40 |
entries, err := os.ReadDir(path) |
| 41 |
if err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
var basenames []string |
| 45 |
for _, entry := range entries { |
| 46 |
basenames = append(basenames, entry.Name()) |
| 47 |
} |
| 48 |
return basenames, nil |
| 49 |
} |
| 50 |
|
| 51 |
func stat(o cp.Options, path string) (os.FileInfo, error) { |
| 52 |
if o.NoFollowSymlinks { |
| 53 |
return os.Lstat(path) |
| 54 |
} |
| 55 |
return os.Stat(path) |
| 56 |
} |
| 57 |
|
| 58 |
// IsEqualTree compare the content in the file trees in src and dst paths. |
| 59 |
func IsEqualTree(o cp.Options, src, dst string) error { |
| 60 |
srcInfo, err := stat(o, src) |
| 61 |
if err != nil { |
| 62 |
return err |
| 63 |
} |
| 64 |
dstInfo, err := stat(o, dst) |
| 65 |
if err != nil { |
| 66 |
return err |
| 67 |
} |
| 68 |
if sm, dm := srcInfo.Mode()&os.ModeType, dstInfo.Mode()&os.ModeType; sm != dm { |
| 69 |
return fmt.Errorf("mismatched mode: %q has mode %s while %q has mode %s", src, sm, dst, dm) |
| 70 |
} |
| 71 |
|
| 72 |
switch { |
| 73 |
case srcInfo.Mode().IsDir(): |
| 74 |
srcEntries, err := readDirNames(src) |
| 75 |
if err != nil { |
| 76 |
return err |
| 77 |
} |
| 78 |
dstEntries, err := readDirNames(dst) |
| 79 |
if err != nil { |
| 80 |
return err |
| 81 |
} |
| 82 |
// os.ReadDir guarantees these are sorted. |
| 83 |
if !reflect.DeepEqual(srcEntries, dstEntries) { |
| 84 |
return fmt.Errorf("directory contents did not match:\n%q had %v\n%q had %v", src, srcEntries, dst, dstEntries) |
| 85 |
} |
| 86 |
for _, basename := range srcEntries { |
| 87 |
if err := IsEqualTree(o, filepath.Join(src, basename), filepath.Join(dst, basename)); err != nil { |
| 88 |
return err |
| 89 |
} |
| 90 |
} |
| 91 |
return nil |
| 92 |
|
| 93 |
case srcInfo.Mode().IsRegular(): |
| 94 |
return isEqualFile(src, dst) |
| 95 |
|
| 96 |
case srcInfo.Mode()&os.ModeSymlink == os.ModeSymlink: |
| 97 |
srcTarget, err := os.Readlink(src) |
| 98 |
if err != nil { |
| 99 |
return err |
| 100 |
} |
| 101 |
dstTarget, err := os.Readlink(dst) |
| 102 |
if err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
if srcTarget != dstTarget { |
| 106 |
return fmt.Errorf("target mismatch: symlink %q had target %q, while %q had target %q", src, srcTarget, dst, dstTarget) |
| 107 |
} |
| 108 |
return nil |
| 109 |
|
| 110 |
default: |
| 111 |
return fmt.Errorf("unsupported mode: %s", srcInfo.Mode()) |
| 112 |
} |
| 113 |
} |