2023-11-27 22:18:22 +01:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2023-11-30 18:37:17 +01:00
|
|
|
"github.com/openziti/zrok/endpoints/drive/webdav"
|
2023-11-27 23:31:13 +01:00
|
|
|
"io"
|
2023-11-27 22:18:22 +01:00
|
|
|
"io/fs"
|
|
|
|
"os"
|
2023-11-27 23:31:13 +01:00
|
|
|
"path/filepath"
|
2023-11-28 03:11:19 +01:00
|
|
|
"time"
|
2023-11-27 22:18:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type FilesystemTargetConfig struct {
|
|
|
|
Root string
|
|
|
|
}
|
|
|
|
|
|
|
|
type FilesystemTarget struct {
|
2023-11-27 23:31:13 +01:00
|
|
|
cfg *FilesystemTargetConfig
|
2023-11-27 22:18:22 +01:00
|
|
|
root fs.FS
|
|
|
|
tree []*Object
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFilesystemTarget(cfg *FilesystemTargetConfig) *FilesystemTarget {
|
|
|
|
root := os.DirFS(cfg.Root)
|
2023-11-27 23:31:13 +01:00
|
|
|
return &FilesystemTarget{cfg: cfg, root: root}
|
2023-11-27 22:18:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *FilesystemTarget) Inventory() ([]*Object, error) {
|
2023-12-15 18:10:35 +01:00
|
|
|
fi, err := os.Stat(t.cfg.Root)
|
|
|
|
if os.IsNotExist(err) {
|
2023-11-27 23:31:13 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
2023-12-15 18:10:35 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return []*Object{{Path: t.cfg.Root, Size: fi.Size(), Modified: fi.ModTime()}}, nil
|
|
|
|
}
|
|
|
|
|
2023-11-27 22:18:22 +01:00
|
|
|
t.tree = nil
|
|
|
|
if err := fs.WalkDir(t.root, ".", t.recurse); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t.tree, nil
|
|
|
|
}
|
|
|
|
|
2023-12-15 18:10:35 +01:00
|
|
|
func (t *FilesystemTarget) IsDir() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-11-27 22:18:22 +01:00
|
|
|
func (t *FilesystemTarget) recurse(path string, d fs.DirEntry, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !d.IsDir() {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
} else {
|
2023-11-28 03:11:19 +01:00
|
|
|
etag = fmt.Sprintf(`"%x%x"`, fi.ModTime().UTC().UnixNano(), fi.Size())
|
2023-11-27 22:18:22 +01:00
|
|
|
}
|
|
|
|
t.tree = append(t.tree, &Object{path, fi.Size(), fi.ModTime(), etag})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-11-27 23:31:13 +01:00
|
|
|
|
|
|
|
func (t *FilesystemTarget) ReadStream(path string) (io.ReadCloser, error) {
|
2023-12-01 18:36:06 +01:00
|
|
|
return os.Open(filepath.Join(t.cfg.Root, path))
|
2023-11-27 23:31:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *FilesystemTarget) WriteStream(path string, stream io.Reader, mode os.FileMode) error {
|
|
|
|
targetPath := filepath.Join(t.cfg.Root, path)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(filepath.Dir(targetPath), mode); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.Create(targetPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(f, stream)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-11-28 03:11:19 +01:00
|
|
|
|
|
|
|
func (t *FilesystemTarget) SetModificationTime(path string, mtime time.Time) error {
|
|
|
|
targetPath := filepath.Join(t.cfg.Root, path)
|
|
|
|
if err := os.Chtimes(targetPath, time.Now(), mtime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|