Files
zrok/util/sync/synchronizer.go
2023-11-27 21:11:19 -05:00

52 lines
1.1 KiB
Go

package sync
import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"os"
)
func Synchronize(src, dst Target) error {
srcTree, err := src.Inventory()
if err != nil {
return errors.Wrap(err, "error creating source inventory")
}
dstTree, err := dst.Inventory()
if err != nil {
return errors.Wrap(err, "error creating destination inventory")
}
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 dstF.Size != srcF.Size || dstF.Modified.UTC() != srcF.Modified.UTC() {
copyList = append(copyList, srcF)
}
} else {
copyList = append(copyList, srcF)
}
}
for _, target := range copyList {
ss, err := src.ReadStream(target.Path)
if err != nil {
return err
}
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)
}
return nil
}