zrok/drives/sync/synchronizer.go

60 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-11-27 22:53:02 +01:00
package sync
import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
2023-11-27 23:31:13 +01:00
"os"
2023-11-27 22:53:02 +01:00
)
func OneWay(src, dst Target, sync bool) error {
2023-11-27 22:53:02 +01:00
srcTree, err := src.Inventory()
if err != nil {
return errors.Wrap(err, "error creating source inventory")
}
var dstTree []*Object
if sync {
dstTree, err = dst.Inventory()
if err != nil {
return errors.Wrap(err, "error creating destination inventory")
}
2023-11-27 22:53:02 +01:00
}
dstIndex := make(map[string]*Object)
for _, f := range dstTree {
dstIndex[f.Path] = f
}
var copyList []*Object
for _, srcF := range srcTree {
if dstF, found := dstIndex[srcF.Path]; found {
if !srcF.IsDir && (dstF.Size != srcF.Size || dstF.Modified.Unix() != srcF.Modified.Unix()) {
2024-01-09 23:15:58 +01:00
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.Unix(), srcF.Modified.Unix())
2023-11-27 22:53:02 +01:00
copyList = append(copyList, srcF)
}
} else {
logrus.Debugf("%v <- !found", srcF.Path)
2023-11-27 22:53:02 +01:00
copyList = append(copyList, srcF)
}
}
for _, copyPath := range copyList {
if copyPath.IsDir {
if err := dst.Mkdir(copyPath.Path); err != nil {
return err
}
} else {
ss, err := src.ReadStream(copyPath.Path)
if err != nil {
return err
}
if err := dst.WriteStreamWithModTime(copyPath.Path, ss, os.ModePerm, copyPath.Modified); err != nil {
return err
}
2023-11-28 03:11:19 +01:00
}
logrus.Infof("=> %v", copyPath.Path)
2023-11-27 22:53:02 +01:00
}
return nil
}