updated synchronizer and target wrappers (#511)

This commit is contained in:
Michael Quigley 2024-01-09 13:58:38 -05:00
parent 562e4226b3
commit a95476bbe7
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 51 additions and 26 deletions

View File

@ -36,7 +36,7 @@ func (t *FilesystemTarget) Inventory() ([]*Object, error) {
} }
if !fi.IsDir() { if !fi.IsDir() {
return []*Object{{Path: t.cfg.Root, Size: fi.Size(), Modified: fi.ModTime()}}, nil return []*Object{{Path: "/" + t.cfg.Root, Size: fi.Size(), Modified: fi.ModTime()}}, nil
} }
t.tree = nil t.tree = nil
@ -46,15 +46,14 @@ func (t *FilesystemTarget) Inventory() ([]*Object, error) {
return t.tree, nil return t.tree, nil
} }
func (t *FilesystemTarget) IsDir() bool { func (t *FilesystemTarget) Mkdir(path string) error {
return true return os.MkdirAll(filepath.Join(t.cfg.Root, path), os.ModePerm)
} }
func (t *FilesystemTarget) recurse(path string, d fs.DirEntry, err error) error { func (t *FilesystemTarget) recurse(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
return err return err
} }
if !d.IsDir() {
fi, err := d.Info() fi, err := d.Info()
if err != nil { if err != nil {
return err return err
@ -68,7 +67,18 @@ func (t *FilesystemTarget) recurse(path string, d fs.DirEntry, err error) error
} else { } else {
etag = fmt.Sprintf(`"%x%x"`, fi.ModTime().UTC().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}) if path != "." {
outPath := "/" + path
if fi.IsDir() {
outPath = outPath + "/"
}
t.tree = append(t.tree, &Object{
Path: outPath,
IsDir: fi.IsDir(),
Size: fi.Size(),
Modified: fi.ModTime(),
ETag: etag,
})
} }
return nil return nil
} }

View File

@ -8,6 +8,7 @@ import (
type Object struct { type Object struct {
Path string Path string
IsDir bool
Size int64 Size int64
Modified time.Time Modified time.Time
ETag string ETag string
@ -15,6 +16,7 @@ type Object struct {
type Target interface { type Target interface {
Inventory() ([]*Object, error) Inventory() ([]*Object, error)
Mkdir(path string) error
ReadStream(path string) (io.ReadCloser, error) ReadStream(path string) (io.ReadCloser, error)
WriteStream(path string, stream io.Reader, mode os.FileMode) error WriteStream(path string, stream io.Reader, mode os.FileMode) error
SetModificationTime(path string, mtime time.Time) error SetModificationTime(path string, mtime time.Time) error

View File

@ -25,15 +25,22 @@ func Synchronize(src, dst Target) error {
var copyList []*Object var copyList []*Object
for _, srcF := range srcTree { for _, srcF := range srcTree {
if dstF, found := dstIndex[srcF.Path]; found { if dstF, found := dstIndex[srcF.Path]; found {
if dstF.Size != srcF.Size || dstF.Modified.UTC() != srcF.Modified.UTC() { if !srcF.IsDir && (dstF.Size != srcF.Size || dstF.Modified.Unix() != srcF.Modified.Unix()) {
logrus.Debugf("%v <- dstF.Size = '%d', srcF.Size = '%d', dstF.Modified.UTC = '%d', srcF.Modified.UTC = '%d'", srcF.Path, dstF.Size, srcF.Size, dstF.Modified, srcF.Modified)
copyList = append(copyList, srcF) copyList = append(copyList, srcF)
} }
} else { } else {
logrus.Debugf("%v <- !found", srcF.Path)
copyList = append(copyList, srcF) copyList = append(copyList, srcF)
} }
} }
for _, copyPath := range copyList { for _, copyPath := range copyList {
if copyPath.IsDir {
if err := dst.Mkdir(copyPath.Path); err != nil {
return err
}
} else {
ss, err := src.ReadStream(copyPath.Path) ss, err := src.ReadStream(copyPath.Path)
if err != nil { if err != nil {
return err return err
@ -44,6 +51,7 @@ func Synchronize(src, dst Target) error {
if err := dst.SetModificationTime(copyPath.Path, copyPath.Modified); err != nil { if err := dst.SetModificationTime(copyPath.Path, copyPath.Modified); err != nil {
return err return err
} }
}
logrus.Infof("=> %v", copyPath.Path) logrus.Infof("=> %v", copyPath.Path)
} }

View File

@ -39,9 +39,10 @@ func (t *WebDAVTarget) Inventory() ([]*Object, error) {
} }
var objects []*Object var objects []*Object
for _, fi := range fis { for _, fi := range fis {
if !fi.IsDir { if fi.Path != "/" {
objects = append(objects, &Object{ objects = append(objects, &Object{
Path: fi.Path, Path: fi.Path,
IsDir: fi.IsDir,
Size: fi.Size, Size: fi.Size,
Modified: fi.ModTime, Modified: fi.ModTime,
ETag: fi.ETag, ETag: fi.ETag,
@ -51,6 +52,10 @@ func (t *WebDAVTarget) Inventory() ([]*Object, error) {
return objects, nil return objects, nil
} }
func (t *WebDAVTarget) Mkdir(path string) error {
return t.dc.Mkdir(context.Background(), path)
}
func (t *WebDAVTarget) ReadStream(path string) (io.ReadCloser, error) { func (t *WebDAVTarget) ReadStream(path string) (io.ReadCloser, error) {
return t.dc.Open(context.Background(), path) return t.dc.Open(context.Background(), path)
} }
@ -60,7 +65,7 @@ func (t *WebDAVTarget) WriteStream(path string, rs io.Reader, _ os.FileMode) err
if err != nil { if err != nil {
return err return err
} }
defer ws.Close() defer func() { _ = ws.Close() }()
_, err = io.Copy(ws, rs) _, err = io.Copy(ws, rs)
if err != nil { if err != nil {
return err return err