2014-03-28 18:56:04 +01:00
|
|
|
// Generic operations on filesystems and objects
|
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-08-01 18:58:39 +02:00
|
|
|
"io"
|
2015-03-01 13:38:31 +01:00
|
|
|
"mime"
|
|
|
|
"path"
|
2014-03-28 18:56:04 +01:00
|
|
|
"sync"
|
2015-10-02 20:48:48 +02:00
|
|
|
"sync/atomic"
|
2015-06-03 16:08:27 +02:00
|
|
|
"time"
|
2014-03-28 18:56:04 +01:00
|
|
|
)
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// CalculateModifyWindow works out modify window for Fses passed in -
|
|
|
|
// sets Config.ModifyWindow
|
2014-03-28 18:56:04 +01:00
|
|
|
//
|
|
|
|
// This is the largest modify window of all the fses in use, and the
|
|
|
|
// user configured value
|
|
|
|
func CalculateModifyWindow(fs ...Fs) {
|
|
|
|
for _, f := range fs {
|
|
|
|
if f != nil {
|
|
|
|
precision := f.Precision()
|
|
|
|
if precision > Config.ModifyWindow {
|
|
|
|
Config.ModifyWindow = precision
|
|
|
|
}
|
2015-08-20 21:48:58 +02:00
|
|
|
if precision == ModTimeNotSupported {
|
|
|
|
Debug(f, "Modify window not supported")
|
|
|
|
return
|
|
|
|
}
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
}
|
2015-08-20 21:48:58 +02:00
|
|
|
Debug(fs[0], "Modify window is %s", Config.ModifyWindow)
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
|
2015-08-17 00:24:34 +02:00
|
|
|
// Md5sumsEqual checks to see if src == dst, but ignores empty strings
|
|
|
|
func Md5sumsEqual(src, dst string) bool {
|
|
|
|
if src == "" || dst == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return src == dst
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// CheckMd5sums checks the two files to see if the MD5sums are the same
|
2014-03-28 18:56:04 +01:00
|
|
|
//
|
2015-08-20 21:48:58 +02:00
|
|
|
// Returns two bools, the first of which is equality and the second of
|
|
|
|
// which is true if either of the MD5SUMs were unset.
|
|
|
|
//
|
2014-03-28 18:56:04 +01:00
|
|
|
// May return an error which will already have been logged
|
|
|
|
//
|
2015-08-20 21:48:58 +02:00
|
|
|
// If an error is returned it will return equal as false
|
|
|
|
func CheckMd5sums(src, dst Object) (equal bool, unset bool, err error) {
|
2014-03-28 18:56:04 +01:00
|
|
|
srcMd5, err := src.Md5sum()
|
|
|
|
if err != nil {
|
|
|
|
Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(src, "Failed to calculate src md5: %s", err)
|
2015-08-20 21:48:58 +02:00
|
|
|
return false, false, err
|
|
|
|
}
|
|
|
|
if srcMd5 == "" {
|
|
|
|
return true, true, nil
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
dstMd5, err := dst.Md5sum()
|
|
|
|
if err != nil {
|
|
|
|
Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(dst, "Failed to calculate dst md5: %s", err)
|
2015-08-20 21:48:58 +02:00
|
|
|
return false, false, err
|
|
|
|
}
|
|
|
|
if dstMd5 == "" {
|
|
|
|
return true, true, nil
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
// Debug("Src MD5 %s", srcMd5)
|
|
|
|
// Debug("Dst MD5 %s", obj.Hash)
|
2015-08-20 21:48:58 +02:00
|
|
|
return Md5sumsEqual(srcMd5, dstMd5), false, nil
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Equal checks to see if the src and dst objects are equal by looking at
|
2014-03-28 18:56:04 +01:00
|
|
|
// size, mtime and MD5SUM
|
|
|
|
//
|
|
|
|
// If the src and dst size are different then it is considered to be
|
2015-06-06 09:38:45 +02:00
|
|
|
// not equal. If --size-only is in effect then this is the only check
|
|
|
|
// that is done.
|
2014-03-28 18:56:04 +01:00
|
|
|
//
|
|
|
|
// If the size is the same and the mtime is the same then it is
|
2015-06-06 09:38:45 +02:00
|
|
|
// considered to be equal. This check is skipped if using --checksum.
|
2014-03-28 18:56:04 +01:00
|
|
|
//
|
2015-06-06 09:38:45 +02:00
|
|
|
// If the size is the same and mtime is different, unreadable or
|
|
|
|
// --checksum is set and the MD5SUM is the same then the file is
|
|
|
|
// considered to be equal. In this case the mtime on the dst is
|
|
|
|
// updated if --checksum is not set.
|
2014-03-28 18:56:04 +01:00
|
|
|
//
|
|
|
|
// Otherwise the file is considered to be not equal including if there
|
|
|
|
// were errors reading info.
|
|
|
|
func Equal(src, dst Object) bool {
|
|
|
|
if src.Size() != dst.Size() {
|
|
|
|
Debug(src, "Sizes differ")
|
|
|
|
return false
|
|
|
|
}
|
2015-06-06 09:38:45 +02:00
|
|
|
if Config.SizeOnly {
|
|
|
|
Debug(src, "Sizes identical")
|
|
|
|
return true
|
|
|
|
}
|
2014-03-28 18:56:04 +01:00
|
|
|
|
2015-06-03 16:08:27 +02:00
|
|
|
var srcModTime time.Time
|
|
|
|
if !Config.CheckSum {
|
2015-08-20 21:48:58 +02:00
|
|
|
if Config.ModifyWindow == ModTimeNotSupported {
|
|
|
|
Debug(src, "Sizes identical")
|
|
|
|
return true
|
|
|
|
}
|
2015-06-03 16:08:27 +02:00
|
|
|
// Size the same so check the mtime
|
|
|
|
srcModTime = src.ModTime()
|
|
|
|
dstModTime := dst.ModTime()
|
|
|
|
dt := dstModTime.Sub(srcModTime)
|
|
|
|
ModifyWindow := Config.ModifyWindow
|
|
|
|
if dt >= ModifyWindow || dt <= -ModifyWindow {
|
|
|
|
Debug(src, "Modification times differ by %s: %v, %v", dt, srcModTime, dstModTime)
|
|
|
|
} else {
|
|
|
|
Debug(src, "Size and modification time the same (differ by %s, within tolerance %s)", dt, ModifyWindow)
|
|
|
|
return true
|
|
|
|
}
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// mtime is unreadable or different but size is the same so
|
|
|
|
// check the MD5SUM
|
2015-08-20 21:48:58 +02:00
|
|
|
same, md5unset, _ := CheckMd5sums(src, dst)
|
2014-03-28 18:56:04 +01:00
|
|
|
if !same {
|
|
|
|
Debug(src, "Md5sums differ")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-06-03 16:08:27 +02:00
|
|
|
if !Config.CheckSum {
|
|
|
|
// Size and MD5 the same but mtime different so update the
|
|
|
|
// mtime of the dst object here
|
|
|
|
dst.SetModTime(srcModTime)
|
|
|
|
}
|
2014-03-28 18:56:04 +01:00
|
|
|
|
2015-08-20 21:48:58 +02:00
|
|
|
if md5unset {
|
|
|
|
Debug(src, "Size of src and dst objects identical")
|
|
|
|
} else {
|
|
|
|
Debug(src, "Size and MD5SUM of src and dst objects identical")
|
|
|
|
}
|
2014-03-28 18:56:04 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// MimeType returns a guess at the mime type from the extension
|
2015-03-01 13:38:31 +01:00
|
|
|
func MimeType(o Object) string {
|
|
|
|
mimeType := mime.TypeByExtension(path.Ext(o.Remote()))
|
|
|
|
if mimeType == "" {
|
|
|
|
mimeType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
return mimeType
|
|
|
|
}
|
|
|
|
|
2014-07-15 20:27:05 +02:00
|
|
|
// Used to remove a failed copy
|
2015-03-14 18:54:41 +01:00
|
|
|
//
|
|
|
|
// Returns whether the file was succesfully removed or not
|
|
|
|
func removeFailedCopy(dst Object) bool {
|
|
|
|
if dst == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
Debug(dst, "Removing failed copy")
|
|
|
|
removeErr := dst.Remove()
|
|
|
|
if removeErr != nil {
|
|
|
|
Debug(dst, "Failed to remove failed copy: %s", removeErr)
|
|
|
|
return false
|
2014-07-15 20:27:05 +02:00
|
|
|
}
|
2015-03-14 18:54:41 +01:00
|
|
|
return true
|
2014-07-15 20:27:05 +02:00
|
|
|
}
|
|
|
|
|
2014-04-18 18:04:21 +02:00
|
|
|
// Copy src object to dst or f if nil
|
|
|
|
//
|
|
|
|
// If dst is nil then the object must not exist already. If you do
|
|
|
|
// call Copy() with dst nil on a pre-existing file then some filing
|
|
|
|
// systems (eg Drive) may duplicate the file.
|
|
|
|
func Copy(f Fs, dst, src Object) {
|
2015-02-02 18:29:08 +01:00
|
|
|
const maxTries = 10
|
|
|
|
tries := 0
|
|
|
|
doUpdate := dst != nil
|
2015-02-14 19:48:08 +01:00
|
|
|
var err, inErr error
|
2015-02-02 18:29:08 +01:00
|
|
|
tryAgain:
|
2015-02-14 19:48:08 +01:00
|
|
|
// Try server side copy first - if has optional interface and
|
|
|
|
// is same underlying remote
|
|
|
|
actionTaken := "Copied (server side copy)"
|
|
|
|
if fCopy, ok := f.(Copier); ok && src.Fs().Name() == f.Name() {
|
|
|
|
var newDst Object
|
|
|
|
newDst, err = fCopy.Copy(src, src.Remote())
|
|
|
|
if err == nil {
|
|
|
|
dst = newDst
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = ErrorCantCopy
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
2015-02-14 19:48:08 +01:00
|
|
|
// If can't server side copy, do it manually
|
|
|
|
if err == ErrorCantCopy {
|
|
|
|
var in0 io.ReadCloser
|
|
|
|
in0, err = src.Open()
|
|
|
|
if err != nil {
|
|
|
|
Stats.Error()
|
|
|
|
ErrorLog(src, "Failed to open: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2015-10-06 16:35:22 +02:00
|
|
|
|
|
|
|
// On big files add a buffer
|
|
|
|
if src.Size() > 10<<20 {
|
|
|
|
in0, _ = newAsyncReader(in0, 4, 4<<20)
|
|
|
|
}
|
|
|
|
|
2015-09-15 16:46:06 +02:00
|
|
|
in := NewAccount(in0, src) // account the transfer
|
2014-03-28 18:56:04 +01:00
|
|
|
|
2015-02-14 19:48:08 +01:00
|
|
|
if doUpdate {
|
|
|
|
actionTaken = "Copied (updated existing)"
|
|
|
|
err = dst.Update(in, src.ModTime(), src.Size())
|
|
|
|
} else {
|
|
|
|
actionTaken = "Copied (new)"
|
|
|
|
dst, err = f.Put(in, src.Remote(), src.ModTime(), src.Size())
|
|
|
|
}
|
|
|
|
inErr = in.Close()
|
2014-04-18 18:04:21 +02:00
|
|
|
}
|
2015-02-02 18:29:08 +01:00
|
|
|
// Retry if err returned a retry error
|
|
|
|
if r, ok := err.(Retry); ok && r.Retry() && tries < maxTries {
|
|
|
|
tries++
|
|
|
|
Log(src, "Received error: %v - retrying %d/%d", err, tries, maxTries)
|
2015-03-14 18:54:41 +01:00
|
|
|
if removeFailedCopy(dst) {
|
|
|
|
// If we removed dst, then nil it out and note we are not updating
|
|
|
|
dst = nil
|
|
|
|
doUpdate = false
|
|
|
|
}
|
2015-02-02 18:29:08 +01:00
|
|
|
goto tryAgain
|
|
|
|
}
|
2014-03-28 18:56:04 +01:00
|
|
|
if err == nil {
|
|
|
|
err = inErr
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(src, "Failed to copy: %s", err)
|
2014-07-15 20:27:05 +02:00
|
|
|
removeFailedCopy(dst)
|
2014-03-28 18:56:04 +01:00
|
|
|
return
|
|
|
|
}
|
2014-07-15 20:27:05 +02:00
|
|
|
|
2014-07-19 13:38:58 +02:00
|
|
|
// Verify sizes are the same after transfer
|
|
|
|
if src.Size() != dst.Size() {
|
|
|
|
Stats.Error()
|
|
|
|
err = fmt.Errorf("Corrupted on transfer: sizes differ %d vs %d", src.Size(), dst.Size())
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(dst, "%s", err)
|
2014-07-19 13:38:58 +02:00
|
|
|
removeFailedCopy(dst)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-15 20:27:05 +02:00
|
|
|
// Verify md5sums are the same after transfer - ignoring blank md5sums
|
2015-06-09 14:18:40 +02:00
|
|
|
if !Config.SizeOnly {
|
|
|
|
srcMd5sum, md5sumErr := src.Md5sum()
|
2014-07-15 20:27:05 +02:00
|
|
|
if md5sumErr != nil {
|
|
|
|
Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(src, "Failed to read md5sum: %s", md5sumErr)
|
2015-06-09 14:18:40 +02:00
|
|
|
} else if srcMd5sum != "" {
|
|
|
|
dstMd5sum, md5sumErr := dst.Md5sum()
|
|
|
|
if md5sumErr != nil {
|
|
|
|
Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(dst, "Failed to read md5sum: %s", md5sumErr)
|
2015-08-17 00:24:34 +02:00
|
|
|
} else if !Md5sumsEqual(srcMd5sum, dstMd5sum) {
|
2015-06-09 14:18:40 +02:00
|
|
|
Stats.Error()
|
|
|
|
err = fmt.Errorf("Corrupted on transfer: md5sums differ %q vs %q", srcMd5sum, dstMd5sum)
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(dst, "%s", err)
|
2015-06-09 14:18:40 +02:00
|
|
|
removeFailedCopy(dst)
|
|
|
|
return
|
|
|
|
}
|
2014-07-15 20:27:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-18 18:46:57 +02:00
|
|
|
Debug(src, actionTaken)
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if src needs to be copied to dst and if so puts it in out
|
2014-04-18 17:34:59 +02:00
|
|
|
func checkOne(pair ObjectPair, out ObjectPairChan) {
|
|
|
|
src, dst := pair.src, pair.dst
|
2014-03-28 18:56:04 +01:00
|
|
|
if dst == nil {
|
2014-04-18 17:34:59 +02:00
|
|
|
Debug(src, "Couldn't find file - need to transfer")
|
|
|
|
out <- pair
|
2014-03-28 18:56:04 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Check to see if can store this
|
|
|
|
if !src.Storable() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Check to see if changed or not
|
|
|
|
if Equal(src, dst) {
|
|
|
|
Debug(src, "Unchanged skipping")
|
|
|
|
return
|
|
|
|
}
|
2014-04-18 17:34:59 +02:00
|
|
|
out <- pair
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// PairChecker reads Objects~s on in send to out if they need transferring.
|
2014-03-28 18:56:04 +01:00
|
|
|
//
|
|
|
|
// FIXME potentially doing lots of MD5SUMS at once
|
2014-04-18 17:34:59 +02:00
|
|
|
func PairChecker(in ObjectPairChan, out ObjectPairChan, wg *sync.WaitGroup) {
|
2014-03-28 18:56:04 +01:00
|
|
|
defer wg.Done()
|
|
|
|
for pair := range in {
|
|
|
|
src := pair.src
|
|
|
|
Stats.Checking(src)
|
2014-04-18 17:34:59 +02:00
|
|
|
checkOne(pair, out)
|
2014-03-28 18:56:04 +01:00
|
|
|
Stats.DoneChecking(src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// PairCopier reads Objects on in and copies them.
|
2015-02-14 19:48:08 +01:00
|
|
|
func PairCopier(in ObjectPairChan, fdst Fs, wg *sync.WaitGroup) {
|
2014-03-28 18:56:04 +01:00
|
|
|
defer wg.Done()
|
2014-04-18 17:34:59 +02:00
|
|
|
for pair := range in {
|
|
|
|
src := pair.src
|
2014-03-28 18:56:04 +01:00
|
|
|
Stats.Transferring(src)
|
2014-06-26 16:33:06 +02:00
|
|
|
if Config.DryRun {
|
|
|
|
Debug(src, "Not copying as --dry-run")
|
|
|
|
} else {
|
|
|
|
Copy(fdst, pair.dst, src)
|
|
|
|
}
|
2014-03-28 18:56:04 +01:00
|
|
|
Stats.DoneTransferring(src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// PairMover reads Objects on in and moves them if possible, or copies
|
|
|
|
// them if not
|
2015-08-24 22:42:23 +02:00
|
|
|
func PairMover(in ObjectPairChan, fdst Fs, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
|
|
|
// See if we have Move available
|
|
|
|
fdstMover, haveMover := fdst.(Mover)
|
|
|
|
for pair := range in {
|
|
|
|
src := pair.src
|
|
|
|
dst := pair.dst
|
|
|
|
Stats.Transferring(src)
|
|
|
|
if Config.DryRun {
|
|
|
|
Debug(src, "Not moving as --dry-run")
|
|
|
|
} else if haveMover {
|
|
|
|
// Delete destination if it exists
|
|
|
|
if pair.dst != nil {
|
|
|
|
err := dst.Remove()
|
|
|
|
if err != nil {
|
|
|
|
Stats.Error()
|
2015-09-22 08:31:12 +02:00
|
|
|
ErrorLog(dst, "Couldn't delete: %v", err)
|
2015-08-24 22:42:23 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-22 08:31:12 +02:00
|
|
|
_, err := fdstMover.Move(src, src.Remote())
|
|
|
|
if err != nil {
|
|
|
|
Stats.Error()
|
|
|
|
ErrorLog(dst, "Couldn't move: %v", err)
|
|
|
|
} else {
|
|
|
|
Debug(src, "Moved")
|
|
|
|
}
|
2015-08-24 22:42:23 +02:00
|
|
|
} else {
|
|
|
|
Copy(fdst, pair.dst, src)
|
|
|
|
}
|
|
|
|
Stats.DoneTransferring(src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// DeleteFiles removes all the files passed in the channel
|
|
|
|
func DeleteFiles(toBeDeleted ObjectsChan) {
|
2014-03-28 18:56:04 +01:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(Config.Transfers)
|
|
|
|
for i := 0; i < Config.Transfers; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2015-09-22 19:47:16 +02:00
|
|
|
for dst := range toBeDeleted {
|
2014-03-28 18:56:04 +01:00
|
|
|
if Config.DryRun {
|
|
|
|
Debug(dst, "Not deleting as --dry-run")
|
|
|
|
} else {
|
|
|
|
Stats.Checking(dst)
|
|
|
|
err := dst.Remove()
|
|
|
|
Stats.DoneChecking(dst)
|
|
|
|
if err != nil {
|
|
|
|
Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(dst, "Couldn't delete: %s", err)
|
2014-03-28 18:56:04 +01:00
|
|
|
} else {
|
|
|
|
Debug(dst, "Deleted")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2014-07-23 00:03:14 +02:00
|
|
|
Log(nil, "Waiting for deletions to finish")
|
2014-03-28 18:56:04 +01:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2015-03-14 18:11:24 +01:00
|
|
|
// Read a map of Object.Remote to Object for the given Fs
|
|
|
|
func readFilesMap(fs Fs) map[string]Object {
|
|
|
|
files := make(map[string]Object)
|
|
|
|
for o := range fs.List() {
|
|
|
|
remote := o.Remote()
|
|
|
|
if _, ok := files[remote]; !ok {
|
|
|
|
files[remote] = o
|
|
|
|
} else {
|
|
|
|
Log(o, "Duplicate file detected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Same returns true if fdst and fsrc point to the same underlying Fs
|
|
|
|
func Same(fdst, fsrc Fs) bool {
|
2015-09-01 21:50:28 +02:00
|
|
|
return fdst.Name() == fsrc.Name() && fdst.Root() == fsrc.Root()
|
|
|
|
}
|
|
|
|
|
2014-03-28 18:56:04 +01:00
|
|
|
// Syncs fsrc into fdst
|
2014-04-18 17:34:59 +02:00
|
|
|
//
|
|
|
|
// If Delete is true then it deletes any files in fdst that aren't in fsrc
|
2015-08-24 22:42:23 +02:00
|
|
|
//
|
|
|
|
// If DoMove is true then files will be moved instead of copied
|
|
|
|
func syncCopyMove(fdst, fsrc Fs, Delete bool, DoMove bool) error {
|
2015-09-22 19:47:16 +02:00
|
|
|
if Same(fdst, fsrc) {
|
2015-09-01 21:50:28 +02:00
|
|
|
ErrorLog(fdst, "Nothing to do as source and destination are the same")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-03-28 18:56:04 +01:00
|
|
|
err := fdst.Mkdir()
|
|
|
|
if err != nil {
|
|
|
|
Stats.Error()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(fdst, "Building file list")
|
|
|
|
|
|
|
|
// Read the destination files first
|
|
|
|
// FIXME could do this in parallel and make it use less memory
|
2015-03-14 18:11:24 +01:00
|
|
|
delFiles := readFilesMap(fdst)
|
2014-03-28 18:56:04 +01:00
|
|
|
|
|
|
|
// Read source files checking them off against dest files
|
2015-09-22 19:47:16 +02:00
|
|
|
toBeChecked := make(ObjectPairChan, Config.Transfers)
|
|
|
|
toBeUploaded := make(ObjectPairChan, Config.Transfers)
|
2014-03-28 18:56:04 +01:00
|
|
|
|
|
|
|
var checkerWg sync.WaitGroup
|
|
|
|
checkerWg.Add(Config.Checkers)
|
|
|
|
for i := 0; i < Config.Checkers; i++ {
|
2015-09-22 19:47:16 +02:00
|
|
|
go PairChecker(toBeChecked, toBeUploaded, &checkerWg)
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var copierWg sync.WaitGroup
|
|
|
|
copierWg.Add(Config.Transfers)
|
|
|
|
for i := 0; i < Config.Transfers; i++ {
|
2015-08-24 22:42:23 +02:00
|
|
|
if DoMove {
|
2015-09-22 19:47:16 +02:00
|
|
|
go PairMover(toBeUploaded, fdst, &copierWg)
|
2015-08-24 22:42:23 +02:00
|
|
|
} else {
|
2015-09-22 19:47:16 +02:00
|
|
|
go PairCopier(toBeUploaded, fdst, &copierWg)
|
2015-08-24 22:42:23 +02:00
|
|
|
}
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for src := range fsrc.List() {
|
|
|
|
remote := src.Remote()
|
2015-09-27 17:13:20 +02:00
|
|
|
dst, dstFound := delFiles[remote]
|
|
|
|
if !Config.Filter.Include(remote, src.Size()) {
|
|
|
|
Debug(src, "Excluding from sync")
|
|
|
|
if dstFound && !Config.Filter.DeleteExcluded {
|
|
|
|
delete(delFiles, remote)
|
|
|
|
}
|
2014-03-28 18:56:04 +01:00
|
|
|
} else {
|
2015-09-27 17:13:20 +02:00
|
|
|
if dstFound {
|
|
|
|
delete(delFiles, remote)
|
|
|
|
toBeChecked <- ObjectPair{src, dst}
|
|
|
|
} else {
|
|
|
|
// No need to check since doesn't exist
|
|
|
|
toBeUploaded <- ObjectPair{src, nil}
|
|
|
|
}
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-22 19:47:16 +02:00
|
|
|
close(toBeChecked)
|
2014-03-28 18:56:04 +01:00
|
|
|
}()
|
|
|
|
|
|
|
|
Log(fdst, "Waiting for checks to finish")
|
|
|
|
checkerWg.Wait()
|
2015-09-22 19:47:16 +02:00
|
|
|
close(toBeUploaded)
|
2014-03-28 18:56:04 +01:00
|
|
|
Log(fdst, "Waiting for transfers to finish")
|
|
|
|
copierWg.Wait()
|
|
|
|
|
2014-04-18 17:34:59 +02:00
|
|
|
// Delete files if asked
|
|
|
|
if Delete {
|
|
|
|
if Stats.Errored() {
|
2015-08-24 22:42:23 +02:00
|
|
|
ErrorLog(fdst, "Not deleting files as there were IO errors")
|
2014-04-18 17:34:59 +02:00
|
|
|
return nil
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
2014-04-18 17:34:59 +02:00
|
|
|
|
|
|
|
// Delete the spare files
|
|
|
|
toDelete := make(ObjectsChan, Config.Transfers)
|
|
|
|
go func() {
|
|
|
|
for _, fs := range delFiles {
|
|
|
|
toDelete <- fs
|
|
|
|
}
|
|
|
|
close(toDelete)
|
|
|
|
}()
|
|
|
|
DeleteFiles(toDelete)
|
|
|
|
}
|
2014-03-28 18:56:04 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Sync fsrc into fdst
|
2015-08-24 22:42:23 +02:00
|
|
|
func Sync(fdst, fsrc Fs) error {
|
|
|
|
return syncCopyMove(fdst, fsrc, true, false)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// CopyDir copies fsrc into fdst
|
2015-08-24 22:42:23 +02:00
|
|
|
func CopyDir(fdst, fsrc Fs) error {
|
|
|
|
return syncCopyMove(fdst, fsrc, false, false)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// MoveDir moves fsrc into fdst
|
2015-08-24 22:42:23 +02:00
|
|
|
func MoveDir(fdst, fsrc Fs) error {
|
2015-09-22 19:47:16 +02:00
|
|
|
if Same(fdst, fsrc) {
|
2015-09-01 21:50:28 +02:00
|
|
|
ErrorLog(fdst, "Nothing to do as source and destination are the same")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-24 22:42:23 +02:00
|
|
|
// First attempt to use DirMover
|
|
|
|
if fdstDirMover, ok := fdst.(DirMover); ok && fsrc.Name() == fdst.Name() {
|
|
|
|
err := fdstDirMover.DirMove(fsrc)
|
|
|
|
Debug(fdst, "Using server side directory move")
|
|
|
|
switch err {
|
|
|
|
case ErrorCantDirMove, ErrorDirExists:
|
|
|
|
Debug(fdst, "Server side directory move failed - fallback to copy/delete: %v", err)
|
|
|
|
case nil:
|
|
|
|
Debug(fdst, "Server side directory move succeeded")
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
Stats.Error()
|
|
|
|
ErrorLog(fdst, "Server side directory move failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now move the files
|
|
|
|
err := syncCopyMove(fdst, fsrc, false, true)
|
|
|
|
if err != nil || Stats.Errored() {
|
|
|
|
ErrorLog(fdst, "Not deleting files as there were IO errors")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return Purge(fsrc)
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Check the files in fsrc and fdst according to Size and MD5SUM
|
2014-03-28 18:56:04 +01:00
|
|
|
func Check(fdst, fsrc Fs) error {
|
|
|
|
Log(fdst, "Building file list")
|
|
|
|
|
|
|
|
// Read the destination files first
|
|
|
|
// FIXME could do this in parallel and make it use less memory
|
2015-03-14 18:11:24 +01:00
|
|
|
dstFiles := readFilesMap(fdst)
|
2014-03-28 18:56:04 +01:00
|
|
|
|
|
|
|
// Read the source files checking them against dstFiles
|
|
|
|
// FIXME could do this in parallel and make it use less memory
|
2015-03-14 18:11:24 +01:00
|
|
|
srcFiles := readFilesMap(fsrc)
|
|
|
|
|
|
|
|
// Move all the common files into commonFiles and delete then
|
|
|
|
// from srcFiles and dstFiles
|
2014-03-28 18:56:04 +01:00
|
|
|
commonFiles := make(map[string][]Object)
|
2015-03-14 18:11:24 +01:00
|
|
|
for remote, src := range srcFiles {
|
2014-03-28 18:56:04 +01:00
|
|
|
if dst, ok := dstFiles[remote]; ok {
|
|
|
|
commonFiles[remote] = []Object{dst, src}
|
2015-03-14 18:11:24 +01:00
|
|
|
delete(srcFiles, remote)
|
2014-03-28 18:56:04 +01:00
|
|
|
delete(dstFiles, remote)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(fdst, "%d files not in %v", len(dstFiles), fsrc)
|
|
|
|
for _, dst := range dstFiles {
|
|
|
|
Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(dst, "File not in %v", fsrc)
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Log(fsrc, "%d files not in %s", len(srcFiles), fdst)
|
|
|
|
for _, src := range srcFiles {
|
|
|
|
Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(src, "File not in %v", fdst)
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
checks := make(chan []Object, Config.Transfers)
|
|
|
|
go func() {
|
|
|
|
for _, check := range commonFiles {
|
|
|
|
checks <- check
|
|
|
|
}
|
|
|
|
close(checks)
|
|
|
|
}()
|
|
|
|
|
|
|
|
var checkerWg sync.WaitGroup
|
|
|
|
checkerWg.Add(Config.Checkers)
|
|
|
|
for i := 0; i < Config.Checkers; i++ {
|
|
|
|
go func() {
|
|
|
|
defer checkerWg.Done()
|
|
|
|
for check := range checks {
|
|
|
|
dst, src := check[0], check[1]
|
|
|
|
Stats.Checking(src)
|
|
|
|
if src.Size() != dst.Size() {
|
|
|
|
Stats.DoneChecking(src)
|
|
|
|
Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(src, "Sizes differ")
|
2014-03-28 18:56:04 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-08-20 21:48:58 +02:00
|
|
|
same, _, err := CheckMd5sums(src, dst)
|
2014-03-28 18:56:04 +01:00
|
|
|
Stats.DoneChecking(src)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !same {
|
|
|
|
Stats.Error()
|
2015-08-08 21:10:31 +02:00
|
|
|
ErrorLog(src, "Md5sums differ")
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
Debug(src, "OK")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(fdst, "Waiting for checks to finish")
|
|
|
|
checkerWg.Wait()
|
|
|
|
Log(fdst, "%d differences found", Stats.GetErrors())
|
|
|
|
if Stats.GetErrors() > 0 {
|
|
|
|
return fmt.Errorf("%d differences found", Stats.GetErrors())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// ListFn lists the Fs to the supplied function
|
2014-03-28 18:56:04 +01:00
|
|
|
//
|
|
|
|
// Lists in parallel which may get them out of order
|
2014-07-12 13:09:20 +02:00
|
|
|
func ListFn(f Fs, fn func(Object)) error {
|
2014-03-28 18:56:04 +01:00
|
|
|
in := f.List()
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(Config.Checkers)
|
|
|
|
for i := 0; i < Config.Checkers; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for o := range in {
|
2014-07-12 13:09:20 +02:00
|
|
|
fn(o)
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-28 16:30:40 +01:00
|
|
|
// mutex for synchronized output
|
|
|
|
var outMutex sync.Mutex
|
|
|
|
|
|
|
|
// Synchronized fmt.Fprintf
|
2015-09-22 08:31:12 +02:00
|
|
|
//
|
|
|
|
// Ignores errors from Fprintf
|
|
|
|
func syncFprintf(w io.Writer, format string, a ...interface{}) {
|
2015-02-28 16:30:40 +01:00
|
|
|
outMutex.Lock()
|
|
|
|
defer outMutex.Unlock()
|
2015-09-22 08:31:12 +02:00
|
|
|
_, _ = fmt.Fprintf(w, format, a...)
|
2015-02-28 16:30:40 +01:00
|
|
|
}
|
|
|
|
|
2015-09-15 16:46:06 +02:00
|
|
|
// List the Fs to the supplied writer
|
2014-07-12 13:09:20 +02:00
|
|
|
//
|
|
|
|
// Shows size and path
|
|
|
|
//
|
|
|
|
// Lists in parallel which may get them out of order
|
2014-08-01 18:58:39 +02:00
|
|
|
func List(f Fs, w io.Writer) error {
|
2014-07-12 13:09:20 +02:00
|
|
|
return ListFn(f, func(o Object) {
|
2015-02-28 16:30:40 +01:00
|
|
|
syncFprintf(w, "%9d %s\n", o.Size(), o.Remote())
|
2014-07-12 13:09:20 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// ListLong lists the Fs to the supplied writer
|
2014-07-12 13:09:20 +02:00
|
|
|
//
|
|
|
|
// Shows size, mod time and path
|
|
|
|
//
|
|
|
|
// Lists in parallel which may get them out of order
|
2014-08-01 18:58:39 +02:00
|
|
|
func ListLong(f Fs, w io.Writer) error {
|
2014-07-12 13:09:20 +02:00
|
|
|
return ListFn(f, func(o Object) {
|
|
|
|
Stats.Checking(o)
|
|
|
|
modTime := o.ModTime()
|
|
|
|
Stats.DoneChecking(o)
|
2015-09-22 20:04:12 +02:00
|
|
|
syncFprintf(w, "%9d %s %s\n", o.Size(), modTime.Local().Format("2006-01-02 15:04:05.000000000"), o.Remote())
|
2014-07-12 13:09:20 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Md5sum list the Fs to the supplied writer
|
2014-07-12 13:09:20 +02:00
|
|
|
//
|
|
|
|
// Produces the same output as the md5sum command
|
|
|
|
//
|
|
|
|
// Lists in parallel which may get them out of order
|
2014-08-01 18:58:39 +02:00
|
|
|
func Md5sum(f Fs, w io.Writer) error {
|
2014-07-12 13:09:20 +02:00
|
|
|
return ListFn(f, func(o Object) {
|
|
|
|
Stats.Checking(o)
|
|
|
|
md5sum, err := o.Md5sum()
|
|
|
|
Stats.DoneChecking(o)
|
|
|
|
if err != nil {
|
|
|
|
Debug(o, "Failed to read MD5: %v", err)
|
2015-08-17 00:24:34 +02:00
|
|
|
md5sum = "ERROR"
|
2014-07-12 13:09:20 +02:00
|
|
|
}
|
2015-02-28 16:30:40 +01:00
|
|
|
syncFprintf(w, "%32s %s\n", md5sum, o.Remote())
|
2014-07-12 13:09:20 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-02 20:48:48 +02:00
|
|
|
// Count counts the objects and their sizes in the Fs
|
|
|
|
func Count(f Fs) (objects int64, size int64, err error) {
|
|
|
|
err = ListFn(f, func(o Object) {
|
|
|
|
atomic.AddInt64(&objects, 1)
|
|
|
|
atomic.AddInt64(&size, o.Size())
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// ListDir lists the directories/buckets/containers in the Fs to the supplied writer
|
2014-08-01 18:58:39 +02:00
|
|
|
func ListDir(f Fs, w io.Writer) error {
|
2014-03-28 18:56:04 +01:00
|
|
|
for dir := range f.ListDir() {
|
2015-02-28 16:30:40 +01:00
|
|
|
syncFprintf(w, "%12d %13s %9d %s\n", dir.Bytes, dir.When.Format("2006-01-02 15:04:05"), dir.Count, dir.Name)
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Mkdir makes a destination directory or container
|
2014-03-28 18:56:04 +01:00
|
|
|
func Mkdir(f Fs) error {
|
|
|
|
err := f.Mkdir()
|
|
|
|
if err != nil {
|
|
|
|
Stats.Error()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Rmdir removes a container but not if not empty
|
2014-03-28 18:56:04 +01:00
|
|
|
func Rmdir(f Fs) error {
|
|
|
|
if Config.DryRun {
|
|
|
|
Log(f, "Not deleting as dry run is set")
|
|
|
|
} else {
|
|
|
|
err := f.Rmdir()
|
|
|
|
if err != nil {
|
|
|
|
Stats.Error()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-22 19:47:16 +02:00
|
|
|
// Purge removes a container and all of its contents
|
2014-03-28 18:56:04 +01:00
|
|
|
//
|
|
|
|
// FIXME doesn't delete local directories
|
|
|
|
func Purge(f Fs) error {
|
2014-07-25 19:19:49 +02:00
|
|
|
var err error
|
2014-03-28 18:56:04 +01:00
|
|
|
if purger, ok := f.(Purger); ok {
|
2014-07-13 11:45:13 +02:00
|
|
|
if Config.DryRun {
|
|
|
|
Debug(f, "Not purging as --dry-run set")
|
|
|
|
} else {
|
2014-07-25 19:19:49 +02:00
|
|
|
err = purger.Purge()
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
} else {
|
2014-07-25 19:19:49 +02:00
|
|
|
// DeleteFiles and Rmdir observe --dry-run
|
2014-03-28 18:56:04 +01:00
|
|
|
DeleteFiles(f.List())
|
2014-07-25 19:19:49 +02:00
|
|
|
err = Rmdir(f)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
Stats.Error()
|
|
|
|
return err
|
2014-03-28 18:56:04 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|