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
|
|
|
)
|
|
|
|
|
2024-01-11 17:36:05 +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")
|
|
|
|
}
|
|
|
|
|
2024-01-11 17:36:05 +01:00
|
|
|
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 {
|
2024-01-09 19:58:38 +01:00
|
|
|
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 {
|
2024-01-09 19:58:38 +01:00
|
|
|
logrus.Debugf("%v <- !found", srcF.Path)
|
2023-11-27 22:53:02 +01:00
|
|
|
copyList = append(copyList, srcF)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 18:10:35 +01:00
|
|
|
for _, copyPath := range copyList {
|
2024-01-09 19:58:38 +01:00
|
|
|
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
|
|
|
|
}
|
2024-01-16 19:54:01 +01:00
|
|
|
if err := dst.WriteStreamWithModTime(copyPath.Path, ss, os.ModePerm, copyPath.Modified); err != nil {
|
2024-01-09 19:58:38 +01:00
|
|
|
return err
|
|
|
|
}
|
2023-11-28 03:11:19 +01:00
|
|
|
}
|
2023-12-15 18:10:35 +01:00
|
|
|
logrus.Infof("=> %v", copyPath.Path)
|
2023-11-27 22:53:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|