2012-12-26 13:23:58 +01:00
|
|
|
// File system interface
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-12-31 17:40:34 +01:00
|
|
|
"fmt"
|
2012-12-26 13:23:58 +01:00
|
|
|
"io"
|
2012-12-31 17:40:34 +01:00
|
|
|
"log"
|
2012-12-26 13:23:58 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A Filesystem, describes the local filesystem and the remote object store
|
|
|
|
type Fs interface {
|
2012-12-31 17:40:34 +01:00
|
|
|
String() string
|
2012-12-26 13:23:58 +01:00
|
|
|
List() FsObjectsChan
|
|
|
|
NewFsObject(remote string) FsObject
|
2013-01-10 22:58:46 +01:00
|
|
|
Put(in io.Reader, remote string, modTime time.Time, size int64) (FsObject, error)
|
2012-12-26 13:23:58 +01:00
|
|
|
Mkdir() error
|
|
|
|
Rmdir() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME make f.Debugf...
|
|
|
|
|
|
|
|
// A filesystem like object which can either be a remote object or a
|
|
|
|
// local file/directory
|
|
|
|
type FsObject interface {
|
|
|
|
Remote() string
|
|
|
|
Md5sum() (string, error)
|
2013-01-02 16:21:55 +01:00
|
|
|
ModTime() time.Time
|
2012-12-26 13:23:58 +01:00
|
|
|
SetModTime(time.Time)
|
|
|
|
Size() int64
|
|
|
|
Open() (io.ReadCloser, error)
|
|
|
|
Storable() bool
|
|
|
|
// Exists() bool
|
|
|
|
Remove() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type FsObjectsChan chan FsObject
|
|
|
|
|
|
|
|
type FsObjects []FsObject
|
|
|
|
|
2012-12-29 12:35:41 +01:00
|
|
|
// NewFs makes a new Fs object from the path
|
|
|
|
//
|
|
|
|
// FIXME make more generic in future
|
|
|
|
func NewFs(path string) (Fs, error) {
|
|
|
|
if swiftMatch.MatchString(path) {
|
|
|
|
return NewFsSwift(path)
|
|
|
|
}
|
2013-01-08 19:53:35 +01:00
|
|
|
if s3Match.MatchString(path) {
|
|
|
|
return NewFsS3(path)
|
|
|
|
}
|
2013-01-15 00:38:18 +01:00
|
|
|
if driveMatch.MatchString(path) {
|
|
|
|
return NewFsDrive(path)
|
|
|
|
}
|
2012-12-29 12:35:41 +01:00
|
|
|
return NewFsLocal(path)
|
|
|
|
}
|
|
|
|
|
2012-12-31 17:40:34 +01:00
|
|
|
// Write debuging output for this FsObject
|
|
|
|
func FsDebug(fs FsObject, text string, args ...interface{}) {
|
|
|
|
if *verbose {
|
|
|
|
out := fmt.Sprintf(text, args...)
|
|
|
|
log.Printf("%s: %s", fs.Remote(), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write log output for this FsObject
|
|
|
|
func FsLog(fs FsObject, text string, args ...interface{}) {
|
|
|
|
if !*quiet {
|
|
|
|
out := fmt.Sprintf(text, args...)
|
|
|
|
log.Printf("%s: %s", fs.Remote(), out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// checkClose is a utility function used to check the return from
|
|
|
|
// Close in a defer statement.
|
|
|
|
func checkClose(c io.Closer, err *error) {
|
|
|
|
cerr := c.Close()
|
|
|
|
if *err == nil {
|
|
|
|
*err = cerr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-31 18:31:19 +01:00
|
|
|
// Check the two files to see if the MD5sums are the same
|
|
|
|
//
|
|
|
|
// May return an error which will already have been logged
|
|
|
|
//
|
|
|
|
// If an error is returned it will return false
|
|
|
|
func CheckMd5sums(src, dst FsObject) (bool, error) {
|
|
|
|
srcMd5, err := src.Md5sum()
|
|
|
|
if err != nil {
|
2013-01-03 23:50:00 +01:00
|
|
|
stats.Error()
|
2012-12-31 18:31:19 +01:00
|
|
|
FsLog(src, "Failed to calculate src md5: %s", err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
dstMd5, err := dst.Md5sum()
|
|
|
|
if err != nil {
|
2013-01-03 23:50:00 +01:00
|
|
|
stats.Error()
|
2012-12-31 18:31:19 +01:00
|
|
|
FsLog(dst, "Failed to calculate dst md5: %s", err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
// FsDebug("Src MD5 %s", srcMd5)
|
|
|
|
// FsDebug("Dst MD5 %s", obj.Hash)
|
|
|
|
return srcMd5 == dstMd5, nil
|
|
|
|
}
|
|
|
|
|
2012-12-26 13:23:58 +01:00
|
|
|
// Checks to see if the src and dst objects are equal by looking at
|
|
|
|
// size, mtime and MD5SUM
|
|
|
|
//
|
|
|
|
// If the src and dst size are different then it is considered to be
|
|
|
|
// not equal.
|
|
|
|
//
|
|
|
|
// If the size is the same and the mtime is the same then it is
|
|
|
|
// considered to be equal. This is the heuristic rsync uses when
|
|
|
|
// not using --checksum.
|
|
|
|
//
|
|
|
|
// If the size is the same and and mtime is different or unreadable
|
|
|
|
// and the MD5SUM is the same then the file is considered to be equal.
|
|
|
|
// In this case the mtime on the dst is updated.
|
|
|
|
//
|
|
|
|
// Otherwise the file is considered to be not equal including if there
|
|
|
|
// were errors reading info.
|
|
|
|
func Equal(src, dst FsObject) bool {
|
|
|
|
if src.Size() != dst.Size() {
|
2012-12-31 17:40:34 +01:00
|
|
|
FsDebug(src, "Sizes differ")
|
2012-12-26 13:23:58 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size the same so check the mtime
|
2013-01-02 16:21:55 +01:00
|
|
|
srcModTime := src.ModTime()
|
|
|
|
dstModTime := dst.ModTime()
|
|
|
|
if !dstModTime.Equal(srcModTime) {
|
2013-01-15 00:38:18 +01:00
|
|
|
FsDebug(src, "Modification times differ: %v, %v", srcModTime, dstModTime)
|
2012-12-26 13:23:58 +01:00
|
|
|
} else {
|
2013-01-02 16:21:55 +01:00
|
|
|
FsDebug(src, "Size and modification time the same")
|
|
|
|
return true
|
2012-12-26 13:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// mtime is unreadable or different but size is the same so
|
|
|
|
// check the MD5SUM
|
2013-01-02 16:21:55 +01:00
|
|
|
same, _ := CheckMd5sums(src, dst)
|
2012-12-31 18:31:19 +01:00
|
|
|
if !same {
|
2012-12-31 17:40:34 +01:00
|
|
|
FsDebug(src, "Md5sums differ")
|
2012-12-26 13:23:58 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size and MD5 the same but mtime different so update the
|
|
|
|
// mtime of the dst object here
|
|
|
|
dst.SetModTime(srcModTime)
|
|
|
|
|
2012-12-31 17:40:34 +01:00
|
|
|
FsDebug(src, "Size and MD5SUM of src and dst objects identical")
|
2012-12-26 13:23:58 +01:00
|
|
|
return true
|
|
|
|
}
|
2013-01-10 22:58:46 +01:00
|
|
|
|
|
|
|
// Copy src object to f
|
|
|
|
func Copy(f Fs, src FsObject) {
|
|
|
|
in0, err := src.Open()
|
|
|
|
if err != nil {
|
|
|
|
stats.Error()
|
|
|
|
FsLog(src, "Failed to open: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
in := NewAccount(in0) // account the transfer
|
|
|
|
|
|
|
|
dst, err := f.Put(in, src.Remote(), src.ModTime(), src.Size())
|
|
|
|
inErr := in.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = inErr
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
stats.Error()
|
|
|
|
FsLog(dst, "Failed to copy: %s", err)
|
|
|
|
FsDebug(dst, "Removing failed copy")
|
|
|
|
removeErr := dst.Remove()
|
|
|
|
if removeErr != nil {
|
|
|
|
stats.Error()
|
|
|
|
FsLog(dst, "Failed to remove failed copy: %s", removeErr)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
FsDebug(src, "Copied")
|
|
|
|
}
|