2 * Copyright (C) 2017 "IoT.bzh"
3 * Author Sebastien Douheret <sebastien@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
26 "github.com/iotbzh/xds-server/lib/xsapiv1"
27 st "github.com/iotbzh/xds-server/lib/syncthing"
28 uuid "github.com/satori/go.uuid"
29 "github.com/syncthing/syncthing/lib/config"
32 // IFOLDER interface implementation for syncthing
35 type STFolder struct {
38 fConfig xsapiv1.FolderConfig
39 stfConfig config.FolderConfiguration
41 eventChangeCB *FolderEventCB
42 eventChangeCBData *FolderEventCBData
45 // NewFolderST Create a new instance of STFolder
46 func NewFolderST(ctx *Context, sthg *st.SyncThing) *STFolder {
54 func (f *STFolder) NewUID(suffix string) string {
59 uuid := uuid.NewV1().String()[:14] + f.st.MyID[:i]
67 func (f *STFolder) Add(cfg xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
70 if cfg.DataCloudSync.SyncThingID == "" {
71 return nil, fmt.Errorf("device id not set (SyncThingID field)")
74 // rootPath should not be empty
75 if cfg.RootPath == "" {
76 cfg.RootPath = f.Config.FileConf.ShareRootDir
81 // Update Syncthing folder
82 // (except if status is ErrorConfig)
83 // TODO: add cache to avoid multiple requests on startup
84 if f.fConfig.Status != xsapiv1.StatusErrorConfig {
85 id, err := f.st.FolderChange(f.fConfig)
90 f.stfConfig, err = f.st.FolderConfigGet(id)
92 f.fConfig.Status = xsapiv1.StatusErrorConfig
96 // Register to events to update folder status
97 for _, evName := range []string{st.EventStateChanged, st.EventFolderPaused} {
98 evID, err := f.st.Events.Register(evName, f.cbEventState, id, nil)
102 f.eventIDs = append(f.eventIDs, evID)
105 f.fConfig.IsInSync = false // will be updated later by events
106 f.fConfig.Status = xsapiv1.StatusEnable
109 return &f.fConfig, nil
112 // GetConfig Get public part of folder config
113 func (f *STFolder) GetConfig() xsapiv1.FolderConfig {
117 // GetFullPath returns the full path of a directory (from server POV)
118 func (f *STFolder) GetFullPath(dir string) string {
122 if filepath.IsAbs(dir) {
123 return filepath.Join(f.fConfig.RootPath, dir)
125 return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
128 // ConvPathCli2Svr Convert path from Client to Server
129 func (f *STFolder) ConvPathCli2Svr(s string) string {
130 if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
131 return strings.Replace(s,
132 f.fConfig.ClientPath,
133 f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
139 // ConvPathSvr2Cli Convert path from Server to Client
140 func (f *STFolder) ConvPathSvr2Cli(s string) string {
141 if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
142 return strings.Replace(s,
143 f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
144 f.fConfig.ClientPath,
151 func (f *STFolder) Remove() error {
152 err := f.st.FolderDelete(f.stfConfig.ID)
154 // Delete folder on server side
155 err2 := os.RemoveAll(f.GetFullPath(""))
163 // Update update some fields of a folder
164 func (f *STFolder) Update(cfg xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
165 if f.fConfig.ID != cfg.ID {
166 return nil, fmt.Errorf("Invalid id")
169 return &f.fConfig, nil
172 // RegisterEventChange requests registration for folder event change
173 func (f *STFolder) RegisterEventChange(cb *FolderEventCB, data *FolderEventCBData) error {
175 f.eventChangeCBData = data
179 // UnRegisterEventChange remove registered callback
180 func (f *STFolder) UnRegisterEventChange() error {
181 f.eventChangeCB = nil
182 f.eventChangeCBData = nil
186 // Sync Force folder files synchronization
187 func (f *STFolder) Sync() error {
188 return f.st.FolderScan(f.stfConfig.ID, "")
191 // IsInSync Check if folder files are in-sync
192 func (f *STFolder) IsInSync() (bool, error) {
193 sts, err := f.st.IsFolderInSync(f.stfConfig.ID)
197 f.fConfig.IsInSync = sts
201 // callback use to update IsInSync status
202 func (f *STFolder) cbEventState(ev st.Event, data *st.EventsCBData) {
203 prevSync := f.fConfig.IsInSync
204 prevStatus := f.fConfig.Status
208 case st.EventStateChanged:
211 case "scanning", "syncing":
212 f.fConfig.Status = xsapiv1.StatusSyncing
214 f.fConfig.Status = xsapiv1.StatusEnable
216 f.fConfig.IsInSync = (to == "idle")
218 case st.EventFolderPaused:
219 if f.fConfig.Status == xsapiv1.StatusEnable {
220 f.fConfig.Status = xsapiv1.StatusPause
222 f.fConfig.IsInSync = false
225 if f.eventChangeCB != nil &&
226 (prevSync != f.fConfig.IsInSync || prevStatus != f.fConfig.Status) {
228 (*f.eventChangeCB)(&cpConf, f.eventChangeCBData)