aboutsummaryrefslogtreecommitdiffstats
path: root/lib/syncthing
diff options
context:
space:
mode:
authorSebastien Douheret <sebastien.douheret@iot.bzh>2017-11-29 08:54:00 +0100
committerSebastien Douheret <sebastien.douheret@iot.bzh>2017-11-29 11:10:30 +0100
commit2f7828d01f4c4ca2909f95f098627cd5475ed225 (patch)
treeb5e71920b813b95cae3e32044be08b99223348ec /lib/syncthing
parent5caebfb4b7c3b73988f067082b219ce5b7f409ba (diff)
Refit source files to have a public xs-apiv1 lib package.
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'lib/syncthing')
-rw-r--r--lib/syncthing/folder-st.go213
-rw-r--r--lib/syncthing/stfolder.go14
2 files changed, 7 insertions, 220 deletions
diff --git a/lib/syncthing/folder-st.go b/lib/syncthing/folder-st.go
deleted file mode 100644
index 74aa4bb..0000000
--- a/lib/syncthing/folder-st.go
+++ /dev/null
@@ -1,213 +0,0 @@
-package st
-
-import (
- "fmt"
- "os"
- "path/filepath"
- "strings"
-
- "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
- }
- uuid := uuid.NewV1().String()[:14] + f.st.MyID[:i]
- if len(suffix) > 0 {
- uuid += "_" + suffix
- }
- return uuid
-}
-
-// 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
-
- // Update Syncthing folder
- // (except 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 of a directory (from server POV)
-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)
-}
-
-// ConvPathCli2Svr Convert path from Client to Server
-func (f *STFolder) ConvPathCli2Svr(s string) string {
- if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
- return strings.Replace(s,
- f.fConfig.ClientPath,
- f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
- -1)
- }
- return s
-}
-
-// ConvPathSvr2Cli Convert path from Server to Client
-func (f *STFolder) ConvPathSvr2Cli(s string) string {
- if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
- return strings.Replace(s,
- f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
- f.fConfig.ClientPath,
- -1)
- }
- return s
-}
-
-// Remove a folder
-func (f *STFolder) Remove() error {
- err := f.st.FolderDelete(f.stfConfig.ID)
-
- // Delete folder on server side
- err2 := os.RemoveAll(f.GetFullPath(""))
-
- if err != nil {
- return err
- }
- return err2
-}
-
-// Update update some fields of a folder
-func (f *STFolder) Update(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
- if f.fConfig.ID != cfg.ID {
- return nil, fmt.Errorf("Invalid id")
- }
- f.fConfig = cfg
- return &f.fConfig, nil
-}
-
-// 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)
- }
-}
diff --git a/lib/syncthing/stfolder.go b/lib/syncthing/stfolder.go
index 503ba4b..1a5a7ec 100644
--- a/lib/syncthing/stfolder.go
+++ b/lib/syncthing/stfolder.go
@@ -6,13 +6,13 @@ import (
"path/filepath"
"strings"
- "github.com/iotbzh/xds-server/lib/folder"
+ "github.com/iotbzh/xds-server/lib/xsapiv1"
stconfig "github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/protocol"
)
// FolderLoadFromStConfig Load/Retrieve folder config from syncthing database
-func (s *SyncThing) FolderLoadFromStConfig(f *[]folder.FolderConfig) error {
+func (s *SyncThing) FolderLoadFromStConfig(f *[]xsapiv1.FolderConfig) error {
defaultSdk := "" // cannot know which was the default sdk
@@ -36,15 +36,15 @@ func (s *SyncThing) FolderLoadFromStConfig(f *[]folder.FolderConfig) error {
if cliPath == "" {
cliPath = stFld.Path
}
- *f = append(*f, folder.FolderConfig{
+ *f = append(*f, xsapiv1.FolderConfig{
ID: stFld.ID,
Label: stFld.Label,
ClientPath: strings.TrimRight(cliPath, "/"),
- Type: folder.TypeCloudSync,
- Status: folder.StatusDisable,
+ Type: xsapiv1.TypeCloudSync,
+ Status: xsapiv1.StatusDisable,
DefaultSdk: defaultSdk,
RootPath: s.conf.FileConf.ShareRootDir,
- DataCloudSync: folder.CloudSyncConfig{SyncThingID: devID},
+ DataCloudSync: xsapiv1.CloudSyncConfig{SyncThingID: devID},
})
}
@@ -52,7 +52,7 @@ func (s *SyncThing) FolderLoadFromStConfig(f *[]folder.FolderConfig) error {
}
// FolderChange is called when configuration has changed
-func (s *SyncThing) FolderChange(f folder.FolderConfig) (string, error) {
+func (s *SyncThing) FolderChange(f xsapiv1.FolderConfig) (string, error) {
// Get current config
stCfg, err := s.ConfigGet()