mirror of
https://github.com/openziti/zrok.git
synced 2025-06-20 09:48:07 +02: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() {
|
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,29 +46,39 @@ 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 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
etag := ""
|
||||||
|
if v, ok := fi.(webdav.ETager); ok {
|
||||||
|
etag, err = v.ETag(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
etag := ""
|
} else {
|
||||||
if v, ok := fi.(webdav.ETager); ok {
|
etag = fmt.Sprintf(`"%x%x"`, fi.ModTime().UTC().UnixNano(), fi.Size())
|
||||||
etag, err = v.ETag(context.Background())
|
}
|
||||||
if err != nil {
|
if path != "." {
|
||||||
return err
|
outPath := "/" + path
|
||||||
}
|
if fi.IsDir() {
|
||||||
} else {
|
outPath = outPath + "/"
|
||||||
etag = fmt.Sprintf(`"%x%x"`, fi.ModTime().UTC().UnixNano(), fi.Size())
|
|
||||||
}
|
}
|
||||||
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -25,24 +25,32 @@ 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 {
|
||||||
ss, err := src.ReadStream(copyPath.Path)
|
if copyPath.IsDir {
|
||||||
if err != nil {
|
if err := dst.Mkdir(copyPath.Path); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := dst.WriteStream(copyPath.Path, ss, os.ModePerm); err != nil {
|
} else {
|
||||||
return err
|
ss, err := src.ReadStream(copyPath.Path)
|
||||||
}
|
if err != nil {
|
||||||
if err := dst.SetModificationTime(copyPath.Path, copyPath.Modified); err != nil {
|
return err
|
||||||
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)
|
logrus.Infof("=> %v", copyPath.Path)
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user