From c397ae883b4ebc80ac41b92a4cfba65f645ba37b Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 27 Nov 2023 21:11:19 -0500 Subject: [PATCH] modification time and UTC (#438) --- util/sync/filesystem.go | 11 ++++++++++- util/sync/model.go | 1 + util/sync/synchronizer.go | 6 ++++-- util/sync/webdav.go | 5 +++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/util/sync/filesystem.go b/util/sync/filesystem.go index 49d4be8e..6877440c 100644 --- a/util/sync/filesystem.go +++ b/util/sync/filesystem.go @@ -8,6 +8,7 @@ import ( "io/fs" "os" "path/filepath" + "time" ) type FilesystemTargetConfig struct { @@ -52,7 +53,7 @@ func (t *FilesystemTarget) recurse(path string, d fs.DirEntry, err error) error return err } } else { - etag = fmt.Sprintf(`"%x%x"`, fi.ModTime().UnixNano(), fi.Size()) + etag = fmt.Sprintf(`"%x%x"`, fi.ModTime().UTC().UnixNano(), fi.Size()) } t.tree = append(t.tree, &Object{path, fi.Size(), fi.ModTime(), etag}) } @@ -79,3 +80,11 @@ func (t *FilesystemTarget) WriteStream(path string, stream io.Reader, mode os.Fi } return nil } + +func (t *FilesystemTarget) SetModificationTime(path string, mtime time.Time) error { + targetPath := filepath.Join(t.cfg.Root, path) + if err := os.Chtimes(targetPath, time.Now(), mtime); err != nil { + return err + } + return nil +} diff --git a/util/sync/model.go b/util/sync/model.go index 8ea6c1ed..8fca5ca0 100644 --- a/util/sync/model.go +++ b/util/sync/model.go @@ -17,4 +17,5 @@ type Target interface { Inventory() ([]*Object, error) ReadStream(path string) (io.ReadCloser, error) WriteStream(path string, stream io.Reader, mode os.FileMode) error + SetModificationTime(path string, mtime time.Time) error } diff --git a/util/sync/synchronizer.go b/util/sync/synchronizer.go index 23e63af4..36ba78fc 100644 --- a/util/sync/synchronizer.go +++ b/util/sync/synchronizer.go @@ -25,7 +25,7 @@ func Synchronize(src, dst Target) error { var copyList []*Object for _, srcF := range srcTree { if dstF, found := dstIndex[srcF.Path]; found { - if dstF.ETag != srcF.ETag { + if dstF.Size != srcF.Size || dstF.Modified.UTC() != srcF.Modified.UTC() { copyList = append(copyList, srcF) } } else { @@ -34,7 +34,6 @@ func Synchronize(src, dst Target) error { } for _, target := range copyList { - logrus.Infof("+> %v", target.Path) ss, err := src.ReadStream(target.Path) if err != nil { return err @@ -42,6 +41,9 @@ func Synchronize(src, dst Target) error { if err := dst.WriteStream(target.Path, ss, os.ModePerm); err != nil { return err } + if err := dst.SetModificationTime(target.Path, target.Modified); err != nil { + return err + } logrus.Infof("=> %v", target.Path) } diff --git a/util/sync/webdav.go b/util/sync/webdav.go index 6f63f21f..840a685b 100644 --- a/util/sync/webdav.go +++ b/util/sync/webdav.go @@ -6,6 +6,7 @@ import ( "io" "os" "path/filepath" + "time" ) type WebDAVTargetConfig struct { @@ -67,3 +68,7 @@ func (t *WebDAVTarget) ReadStream(path string) (io.ReadCloser, error) { func (t *WebDAVTarget) WriteStream(path string, stream io.Reader, mode os.FileMode) error { return t.c.WriteStream(path, stream, mode) } + +func (t *WebDAVTarget) SetModificationTime(path string, mtime time.Time) error { + return nil +}