modification time and UTC (#438)

This commit is contained in:
Michael Quigley 2023-11-27 21:11:19 -05:00
parent 93277a191b
commit c397ae883b
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 20 additions and 3 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}