diff --git a/diff_containerd.go b/diff_containerd.go index 3702715..d72f4e6 100644 --- a/diff_containerd.go +++ b/diff_containerd.go @@ -1,7 +1,9 @@ package fsutil import ( + "bytes" "context" + "io" "os" "strings" @@ -35,6 +37,8 @@ const ( // computed during a directory changes calculation. type ChangeFunc func(ChangeKind, string, os.FileInfo, error) error +const compareChunkSize = 32 * 1024 + type currentPath struct { path string stat *types.Stat @@ -42,7 +46,7 @@ type currentPath struct { } // doubleWalkDiff walks both directories to create a diff -func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b walkerFn, filter FilterFunc) (err error) { +func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b walkerFn, filter FilterFunc, differ DiffType) (err error) { g, ctx := errgroup.WithContext(ctx) var ( @@ -116,7 +120,7 @@ func doubleWalkDiff(ctx context.Context, changeFn ChangeFunc, a, b walkerFn, fil } f1 = nil case ChangeKindModify: - same, err := sameFile(f1, f2copy) + same, err := sameFile(f1, f2copy, differ) if err != nil { return err } @@ -165,7 +169,10 @@ func pathChange(lower, upper *currentPath) (ChangeKind, string) { } } -func sameFile(f1, f2 *currentPath) (same bool, retErr error) { +func sameFile(f1, f2 *currentPath, differ DiffType) (same bool, retErr error) { + if differ == DiffNone { + return false, nil + } // If not a directory also check size, modtime, and content if !f1.stat.IsDir() { if f1.stat.Size_ != f2.stat.Size_ { @@ -177,7 +184,43 @@ func sameFile(f1, f2 *currentPath) (same bool, retErr error) { } } - return compareStat(f1.stat, f2.stat) + same, err := compareStat(f1.stat, f2.stat) + if err != nil || !same || differ == DiffMetadata { + return same, err + } + return compareFileContent(f1.path, f2.path) +} + +func compareFileContent(p1, p2 string) (bool, error) { + f1, err := os.Open(p1) + if err != nil { + return false, err + } + defer f1.Close() + f2, err := os.Open(p2) + if err != nil { + return false, err + } + defer f2.Close() + + b1 := make([]byte, compareChunkSize) + b2 := make([]byte, compareChunkSize) + for { + n1, err1 := f1.Read(b1) + if err1 != nil && err1 != io.EOF { + return false, err1 + } + n2, err2 := f2.Read(b2) + if err2 != nil && err2 != io.EOF { + return false, err2 + } + if n1 != n2 || !bytes.Equal(b1[:n1], b2[:n2]) { + return false, nil + } + if err1 == io.EOF && err2 == io.EOF { + return true, nil + } + } } // compareStat returns whether the stats are equivalent, diff --git a/receive.go b/receive.go index 5c6a486..209d1d2 100644 --- a/receive.go +++ b/receive.go @@ -11,12 +11,21 @@ import ( "golang.org/x/sync/errgroup" ) +type DiffType int + +const ( + DiffMetadata DiffType = iota + DiffNone + DiffContent +) + type ReceiveOpt struct { NotifyHashed ChangeFunc ContentHasher ContentHasher ProgressCb func(int, bool) Merge bool Filter FilterFunc + Differ DiffType } func Receive(ctx context.Context, conn Stream, dest string, opt ReceiveOpt) error { @@ -33,6 +42,7 @@ func Receive(ctx context.Context, conn Stream, dest string, opt ReceiveOpt) erro progressCb: opt.ProgressCb, merge: opt.Merge, filter: opt.Filter, + differ: opt.Differ, } return r.run(ctx) } @@ -47,6 +57,7 @@ type receiver struct { progressCb func(int, bool) merge bool filter FilterFunc + differ DiffType notifyHashed ChangeFunc contentHasher ContentHasher @@ -132,7 +143,7 @@ func (r *receiver) run(ctx context.Context) error { if !r.merge { destWalker = getWalkerFn(r.dest) } - err := doubleWalkDiff(ctx, dw.HandleChange, destWalker, w.fill, r.filter) + err := doubleWalkDiff(ctx, dw.HandleChange, destWalker, w.fill, r.filter, r.differ) if err != nil { return err }