Make ModTime fall back to LastModified header

Which means it no longer needs to report an error which simplifies the code
This commit is contained in:
Nick Craig-Wood 2013-01-02 15:21:55 +00:00
parent 90a2c86eb3
commit ecb863dd4f
5 changed files with 26 additions and 37 deletions

22
fs.go
View File

@ -26,7 +26,7 @@ type Fs interface {
type FsObject interface { type FsObject interface {
Remote() string Remote() string
Md5sum() (string, error) Md5sum() (string, error)
ModTime() (time.Time, error) ModTime() time.Time
SetModTime(time.Time) SetModTime(time.Time)
Size() int64 Size() int64
Open() (io.ReadCloser, error) Open() (io.ReadCloser, error)
@ -118,24 +118,18 @@ func Equal(src, dst FsObject) bool {
} }
// Size the same so check the mtime // Size the same so check the mtime
srcModTime, err := src.ModTime() srcModTime := src.ModTime()
if err != nil { dstModTime := dst.ModTime()
FsDebug(src, "Failed to read src mtime: %s", err) if !dstModTime.Equal(srcModTime) {
FsDebug(src, "Modification times differ")
} else { } else {
dstModTime, err := dst.ModTime() FsDebug(src, "Size and modification time the same")
if err != nil { return true
FsDebug(dst, "Failed to read dst mtime: %s", err)
} else if !dstModTime.Equal(srcModTime) {
FsDebug(src, "Modification times differ")
} else {
FsDebug(src, "Size and modification time the same")
return true
}
} }
// mtime is unreadable or different but size is the same so // mtime is unreadable or different but size is the same so
// check the MD5SUM // check the MD5SUM
same, err := CheckMd5sums(src, dst) same, _ := CheckMd5sums(src, dst)
if !same { if !same {
FsDebug(src, "Md5sums differ") FsDebug(src, "Md5sums differ")
return false return false

View File

@ -150,12 +150,7 @@ func (f *FsLocal) Put(src FsObject) {
} }
// Set the mtime // Set the mtime
modTime, err := src.ModTime() fs.SetModTime(src.ModTime())
if err != nil {
FsDebug(fs, "Failed to read mtime from object: %s", err)
} else {
fs.SetModTime(modTime)
}
} }
// Mkdir creates the directory if it doesn't exist // Mkdir creates the directory if it doesn't exist
@ -200,8 +195,8 @@ func (fs *FsObjectLocal) Size() int64 {
} }
// ModTime returns the modification time of the object // ModTime returns the modification time of the object
func (fs *FsObjectLocal) ModTime() (modTime time.Time, err error) { func (fs *FsObjectLocal) ModTime() time.Time {
return fs.info.ModTime(), nil return fs.info.ModTime()
} }
// Sets the modification time of the local fs object // Sets the modification time of the local fs object

View File

@ -183,12 +183,7 @@ func (f *FsSwift) Put(src FsObject) {
// Set the mtime // Set the mtime
m := swift.Metadata{} m := swift.Metadata{}
modTime, err := src.ModTime() m.SetModTime(src.ModTime())
if err != nil {
FsDebug(fs, "Failed to read mtime from object: %s", err)
} else {
m.SetModTime(modTime)
}
_, err = fs.swift.c.ObjectPut(fs.swift.container, fs.remote, in, true, "", "", m.ObjectHeaders()) _, err = fs.swift.c.ObjectPut(fs.swift.container, fs.remote, in, true, "", "", m.ObjectHeaders())
if err != nil { if err != nil {
@ -246,18 +241,22 @@ func (fs *FsObjectSwift) readMetaData() (err error) {
} }
// ModTime returns the modification time of the object // ModTime returns the modification time of the object
func (fs *FsObjectSwift) ModTime() (modTime time.Time, err error) { //
err = fs.readMetaData() //
// It attempts to read the objects mtime and if that isn't present the
// LastModified returned in the http headers
func (fs *FsObjectSwift) ModTime() time.Time {
err := fs.readMetaData()
if err != nil { if err != nil {
FsLog(fs, "Failed to read metadata: %s", err) // FsLog(fs, "Failed to read metadata: %s", err)
return return fs.info.LastModified
} }
modTime, err = fs.meta.GetModTime() modTime, err := fs.meta.GetModTime()
if err != nil { if err != nil {
FsLog(fs, "Failed to read mtime from object: %s", err) // FsLog(fs, "Failed to read mtime from object: %s", err)
return return fs.info.LastModified
} }
return return modTime
} }
// Sets the modification time of the local fs object // Sets the modification time of the local fs object

View File

@ -1,5 +1,6 @@
Todo Todo
* FIXME: More -dry-run checks for object transfer * FIXME: More -dry-run checks for object transfer
* FIXME ls is very slow also - need to run mtimes in parallel
* Check logging in various parts * Check logging in various parts
* Make logging controllable with flags (mostly done) * Make logging controllable with flags (mostly done)
* progress meter would be nice! Do this by wrapping the Reader with a progress bar * progress meter would be nice! Do this by wrapping the Reader with a progress bar

View File

@ -250,7 +250,7 @@ func List(f Fs) {
// fmt.Printf("%9s %19s %s\n", "Directory", "-", fs.Remote()) // fmt.Printf("%9s %19s %s\n", "Directory", "-", fs.Remote())
// } else { // } else {
// FIXME ModTime is expensive? // FIXME ModTime is expensive?
modTime, _ := fs.ModTime() modTime := fs.ModTime()
fmt.Printf("%9d %19s %s\n", fs.Size(), modTime.Format("2006-01-02 15:04:05"), fs.Remote()) fmt.Printf("%9d %19s %s\n", fs.Size(), modTime.Format("2006-01-02 15:04:05"), fs.Remote())
// fmt.Printf("%9d %19s %s\n", fs.Size(), object.LastModified.Format("2006-01-02 15:04:05"), fs.Remote()) // fmt.Printf("%9d %19s %s\n", fs.Size(), object.LastModified.Format("2006-01-02 15:04:05"), fs.Remote())
// } // }