mirror of
https://github.com/openziti/zrok.git
synced 2025-02-23 21:51:27 +01:00
updated synchronizer and target wrappers (#511)
This commit is contained in:
parent
562e4226b3
commit
a95476bbe7
@ -36,7 +36,7 @@ func (t *FilesystemTarget) Inventory() ([]*Object, error) {
|
||||
}
|
||||
|
||||
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
|
||||
@ -46,29 +46,39 @@ func (t *FilesystemTarget) Inventory() ([]*Object, error) {
|
||||
return t.tree, nil
|
||||
}
|
||||
|
||||
func (t *FilesystemTarget) IsDir() bool {
|
||||
return true
|
||||
func (t *FilesystemTarget) Mkdir(path string) error {
|
||||
return os.MkdirAll(filepath.Join(t.cfg.Root, path), os.ModePerm)
|
||||
}
|
||||
|
||||
func (t *FilesystemTarget) recurse(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !d.IsDir() {
|
||||
fi, err := d.Info()
|
||||
fi, err := d.Info()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
etag := ""
|
||||
if v, ok := fi.(webdav.ETager); ok {
|
||||
etag, err = v.ETag(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
etag := ""
|
||||
if v, ok := fi.(webdav.ETager); ok {
|
||||
etag, err = v.ETag(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
etag = fmt.Sprintf(`"%x%x"`, fi.ModTime().UTC().UnixNano(), fi.Size())
|
||||
} else {
|
||||
etag = fmt.Sprintf(`"%x%x"`, fi.ModTime().UTC().UnixNano(), fi.Size())
|
||||
}
|
||||
if path != "." {
|
||||
outPath := "/" + path
|
||||
if fi.IsDir() {
|
||||
outPath = outPath + "/"
|
||||
}
|
||||
t.tree = append(t.tree, &Object{path, fi.Size(), fi.ModTime(), etag})
|
||||
t.tree = append(t.tree, &Object{
|
||||
Path: outPath,
|
||||
IsDir: fi.IsDir(),
|
||||
Size: fi.Size(),
|
||||
Modified: fi.ModTime(),
|
||||
ETag: etag,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
|
||||
type Object struct {
|
||||
Path string
|
||||
IsDir bool
|
||||
Size int64
|
||||
Modified time.Time
|
||||
ETag string
|
||||
@ -15,6 +16,7 @@ type Object struct {
|
||||
|
||||
type Target interface {
|
||||
Inventory() ([]*Object, error)
|
||||
Mkdir(path string) error
|
||||
ReadStream(path string) (io.ReadCloser, error)
|
||||
WriteStream(path string, stream io.Reader, mode os.FileMode) error
|
||||
SetModificationTime(path string, mtime time.Time) error
|
||||
|
@ -25,24 +25,32 @@ func Synchronize(src, dst Target) error {
|
||||
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() {
|
||||
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)
|
||||
}
|
||||
} else {
|
||||
logrus.Debugf("%v <- !found", srcF.Path)
|
||||
copyList = append(copyList, srcF)
|
||||
}
|
||||
}
|
||||
|
||||
for _, copyPath := range copyList {
|
||||
ss, err := src.ReadStream(copyPath.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := dst.WriteStream(copyPath.Path, ss, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := dst.SetModificationTime(copyPath.Path, copyPath.Modified); err != nil {
|
||||
return err
|
||||
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.WriteStream(copyPath.Path, ss, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := dst.SetModificationTime(copyPath.Path, copyPath.Modified); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
logrus.Infof("=> %v", copyPath.Path)
|
||||
}
|
||||
|
@ -39,9 +39,10 @@ func (t *WebDAVTarget) Inventory() ([]*Object, error) {
|
||||
}
|
||||
var objects []*Object
|
||||
for _, fi := range fis {
|
||||
if !fi.IsDir {
|
||||
if fi.Path != "/" {
|
||||
objects = append(objects, &Object{
|
||||
Path: fi.Path,
|
||||
IsDir: fi.IsDir,
|
||||
Size: fi.Size,
|
||||
Modified: fi.ModTime,
|
||||
ETag: fi.ETag,
|
||||
@ -51,6 +52,10 @@ func (t *WebDAVTarget) Inventory() ([]*Object, error) {
|
||||
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) {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
defer ws.Close()
|
||||
defer func() { _ = ws.Close() }()
|
||||
_, err = io.Copy(ws, rs)
|
||||
if err != nil {
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user