From 8a76568ea899394f7b28e5ec1596882fc81f7135 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 15 Jul 2014 19:27:05 +0100 Subject: [PATCH] core: Verify MD5 sums after each transfer --- fs/operations.go | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/fs/operations.go b/fs/operations.go index 7c838d635..0fd62d90e 100644 --- a/fs/operations.go +++ b/fs/operations.go @@ -97,6 +97,18 @@ func Equal(src, dst Object) bool { return true } +// Used to remove a failed copy +func removeFailedCopy(dst Object) { + if dst != nil { + Debug(dst, "Removing failed copy") + removeErr := dst.Remove() + if removeErr != nil { + Stats.Error() + Log(dst, "Failed to remove failed copy: %s", removeErr) + } + } +} + // Copy src object to dst or f if nil // // If dst is nil then the object must not exist already. If you do @@ -126,16 +138,29 @@ func Copy(f Fs, dst, src Object) { if err != nil { Stats.Error() Log(src, "Failed to copy: %s", err) - if dst != nil { - Debug(dst, "Removing failed copy") - removeErr := dst.Remove() - if removeErr != nil { - Stats.Error() - Log(dst, "Failed to remove failed copy: %s", removeErr) - } - } + removeFailedCopy(dst) return } + + // Verify md5sums are the same after transfer - ignoring blank md5sums + srcMd5sum, md5sumErr := src.Md5sum() + if md5sumErr != nil { + Stats.Error() + Log(src, "Failed to read md5sum: %s", md5sumErr) + } else if srcMd5sum != "" { + dstMd5sum, md5sumErr := dst.Md5sum() + if md5sumErr != nil { + Stats.Error() + Log(dst, "Failed to read md5sum: %s", md5sumErr) + } else if dstMd5sum != "" && srcMd5sum != dstMd5sum { + Stats.Error() + err = fmt.Errorf("Corrupted on transfer: md5sums differ %q vs %q", srcMd5sum, dstMd5sum) + Log(dst, "%s", err) + removeFailedCopy(dst) + return + } + } + Debug(src, actionTaken) }