X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=lib%2Fsyncthing%2Ffolder-st.go;h=27a43e6d087a4503379aa12f5f30a83df8576cbb;hb=8119c1641e88ee5020e380c08119f508c6e24222;hp=ffcd2840ff8c91b8d243a3ec9f2a1f79e850cfa4;hpb=dd6f08b10b1597f44e3dc25509ac9a45336b0914;p=src%2Fxds%2Fxds-server.git diff --git a/lib/syncthing/folder-st.go b/lib/syncthing/folder-st.go index ffcd284..27a43e6 100644 --- a/lib/syncthing/folder-st.go +++ b/lib/syncthing/folder-st.go @@ -2,10 +2,13 @@ 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" ) @@ -13,10 +16,13 @@ import ( // STFolder . type STFolder struct { - globalConfig *xdsconfig.Config - st *SyncThing - fConfig folder.FolderConfig - stfConfig config.FolderConfiguration + 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 @@ -27,6 +33,15 @@ func (s *SyncThing) NewFolderST(gc *xdsconfig.Config) *STFolder { } } +// 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) { @@ -42,10 +57,8 @@ func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) { 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) + // (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) @@ -59,6 +72,16 @@ func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) { 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 } @@ -70,7 +93,7 @@ func (f *STFolder) GetConfig() folder.FolderConfig { return f.fConfig } -// GetFullPath returns the full path +// GetFullPath returns the full path of a directory (from server POV) func (f *STFolder) GetFullPath(dir string) string { if &dir == nil { dir = "" @@ -81,9 +104,53 @@ func (f *STFolder) GetFullPath(dir string) string { 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 { - return f.st.FolderDelete(f.stfConfig.ID) + err := f.st.FolderDelete(f.stfConfig.ID) + + // Delete folder on server side + err2 := os.RemoveAll(f.GetFullPath("")) + + if err != nil { + return err + } + return err2 +} + +// 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 @@ -93,5 +160,41 @@ func (f *STFolder) Sync() error { // IsInSync Check if folder files are in-sync func (f *STFolder) IsInSync() (bool, error) { - return f.st.IsFolderInSync(f.stfConfig.ID) + 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) + } }