summaryrefslogtreecommitdiffstats
path: root/lib/syncthing/folder-st.go
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-08-26 11:29:56 +0200
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-08-26 11:29:56 +0200
commit5e120c466686880c5bf6b94043dd01edc261fef9 (patch)
tree9d3071f226bd0b234540bd20bdab0c77d3bca164 /lib/syncthing/folder-st.go
parente113bbc75f88457d29f11823af0ff902e7c2ac8b (diff)
parent0367a6f9f2f868d785f197a052840ec621a681a6 (diff)
Merge remote-tracking branch 'origin/wip'
Diffstat (limited to 'lib/syncthing/folder-st.go')
-rw-r--r--lib/syncthing/folder-st.go170
1 files changed, 170 insertions, 0 deletions
diff --git a/lib/syncthing/folder-st.go b/lib/syncthing/folder-st.go
new file mode 100644
index 0000000..da27062
--- /dev/null
+++ b/lib/syncthing/folder-st.go
@@ -0,0 +1,170 @@
+package st
+
+import (
+ "fmt"
+ "path/filepath"
+
+ "github.com/iotbzh/xds-server/lib/folder"
+ "github.com/iotbzh/xds-server/lib/xdsconfig"
+ uuid "github.com/satori/go.uuid"
+ "github.com/syncthing/syncthing/lib/config"
+)
+
+// IFOLDER interface implementation for syncthing
+
+// STFolder .
+type STFolder struct {
+ globalConfig *xdsconfig.Config
+ st *SyncThing
+ fConfig folder.FolderConfig
+ stfConfig config.FolderConfiguration
+ eventIDs []int
+ eventChangeCB *folder.EventCB
+ eventChangeCBData *folder.EventCBData
+}
+
+// NewFolderST Create a new instance of STFolder
+func (s *SyncThing) NewFolderST(gc *xdsconfig.Config) *STFolder {
+ return &STFolder{
+ globalConfig: gc,
+ st: s,
+ }
+}
+
+// NewUID Get a UUID
+func (f *STFolder) NewUID(suffix string) string {
+ i := len(f.st.MyID)
+ if i > 15 {
+ i = 15
+ }
+ return uuid.NewV1().String()[:14] + f.st.MyID[:i] + "_" + suffix
+}
+
+// Add a new folder
+func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
+
+ // Sanity check
+ if cfg.DataCloudSync.SyncThingID == "" {
+ return nil, fmt.Errorf("device id not set (SyncThingID field)")
+ }
+
+ // rootPath should not be empty
+ if cfg.RootPath == "" {
+ cfg.RootPath = f.globalConfig.FileConf.ShareRootDir
+ }
+
+ f.fConfig = cfg
+
+ f.fConfig.DataCloudSync.BuilderSThgID = f.st.MyID // FIXME - should be removed after local ST config rework
+
+ // Update Syncthing folder
+ // (expect if status is ErrorConfig)
+ // TODO: add cache to avoid multiple requests on startup
+ if f.fConfig.Status != folder.StatusErrorConfig {
+ id, err := f.st.FolderChange(f.fConfig)
+ if err != nil {
+ return nil, err
+ }
+
+ f.stfConfig, err = f.st.FolderConfigGet(id)
+ if err != nil {
+ f.fConfig.Status = folder.StatusErrorConfig
+ return nil, err
+ }
+
+ // Register to events to update folder status
+ for _, evName := range []string{EventStateChanged, EventFolderPaused} {
+ evID, err := f.st.Events.Register(evName, f.cbEventState, id, nil)
+ if err != nil {
+ return nil, err
+ }
+ f.eventIDs = append(f.eventIDs, evID)
+ }
+
+ f.fConfig.IsInSync = false // will be updated later by events
+ f.fConfig.Status = folder.StatusEnable
+ }
+
+ return &f.fConfig, nil
+}
+
+// GetConfig Get public part of folder config
+func (f *STFolder) GetConfig() folder.FolderConfig {
+ return f.fConfig
+}
+
+// GetFullPath returns the full path
+func (f *STFolder) GetFullPath(dir string) string {
+ if &dir == nil {
+ dir = ""
+ }
+ if filepath.IsAbs(dir) {
+ return filepath.Join(f.fConfig.RootPath, dir)
+ }
+ return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
+}
+
+// Remove a folder
+func (f *STFolder) Remove() error {
+ return f.st.FolderDelete(f.stfConfig.ID)
+}
+
+// RegisterEventChange requests registration for folder event change
+func (f *STFolder) RegisterEventChange(cb *folder.EventCB, data *folder.EventCBData) error {
+ f.eventChangeCB = cb
+ f.eventChangeCBData = data
+ return nil
+}
+
+// UnRegisterEventChange remove registered callback
+func (f *STFolder) UnRegisterEventChange() error {
+ f.eventChangeCB = nil
+ f.eventChangeCBData = nil
+ return nil
+}
+
+// Sync Force folder files synchronization
+func (f *STFolder) Sync() error {
+ return f.st.FolderScan(f.stfConfig.ID, "")
+}
+
+// IsInSync Check if folder files are in-sync
+func (f *STFolder) IsInSync() (bool, error) {
+ sts, err := f.st.IsFolderInSync(f.stfConfig.ID)
+ if err != nil {
+ return false, err
+ }
+ f.fConfig.IsInSync = sts
+ return sts, nil
+}
+
+// callback use to update IsInSync status
+func (f *STFolder) cbEventState(ev Event, data *EventsCBData) {
+ prevSync := f.fConfig.IsInSync
+ prevStatus := f.fConfig.Status
+
+ switch ev.Type {
+
+ case EventStateChanged:
+ to := ev.Data["to"]
+ switch to {
+ case "scanning", "syncing":
+ f.fConfig.Status = folder.StatusSyncing
+ case "idle":
+ f.fConfig.Status = folder.StatusEnable
+ }
+ f.fConfig.IsInSync = (to == "idle")
+
+ case EventFolderPaused:
+ if f.fConfig.Status == folder.StatusEnable {
+ f.fConfig.Status = folder.StatusPause
+ }
+ f.fConfig.IsInSync = false
+ }
+
+ if f.eventChangeCB != nil &&
+ (prevSync != f.fConfig.IsInSync || prevStatus != f.fConfig.Status) {
+ cpConf := f.fConfig
+ (*f.eventChangeCB)(&cpConf, f.eventChangeCBData)
+ }
+}