2 * Copyright (C) 2017-2018 "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 st "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/syncthing"
27 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
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
43 var stEventMonitored = []string{st.EventStateChanged, st.EventFolderPaused}
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 _, err := f.st.FolderChange(f.fConfig)
87 // Use Setup function to setup remains fields
88 return f.Setup(f.fConfig)
91 // Setup Setup local project config
92 func (f *STFolder) Setup(fld xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
96 // Update folder Config
99 // Retrieve Syncthing folder config
100 f.stfConfig, err = f.st.FolderConfigGet(f.fConfig.ID)
102 f.fConfig.Status = xsapiv1.StatusErrorConfig
106 // Register to events to update folder status
107 for _, evName := range stEventMonitored {
108 evID, err := f.st.Events.Register(evName, f.cbEventState, f.fConfig.ID, nil)
112 f.eventIDs = append(f.eventIDs, evID)
115 f.fConfig.IsInSync = false // will be updated later by events
116 f.fConfig.Status = xsapiv1.StatusEnable
118 return &f.fConfig, nil
121 // GetConfig Get public part of folder config
122 func (f *STFolder) GetConfig() xsapiv1.FolderConfig {
126 // GetFullPath returns the full path of a directory (from server POV)
127 func (f *STFolder) GetFullPath(dir string) string {
131 if filepath.IsAbs(dir) {
132 return filepath.Join(f.fConfig.RootPath, dir)
134 return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
137 // ConvPathCli2Svr Convert path from Client to Server
138 func (f *STFolder) ConvPathCli2Svr(s string) string {
139 if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
140 return strings.Replace(s,
141 f.fConfig.ClientPath,
142 f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
148 // ConvPathSvr2Cli Convert path from Server to Client
149 func (f *STFolder) ConvPathSvr2Cli(s string) string {
150 if f.fConfig.ClientPath != "" && f.fConfig.RootPath != "" {
151 return strings.Replace(s,
152 f.fConfig.RootPath+"/"+f.fConfig.ClientPath,
153 f.fConfig.ClientPath,
160 func (f *STFolder) Remove() error {
162 // Un-register events
163 for _, evID := range f.eventIDs {
164 if err := f.st.Events.UnRegister(evID); err != nil && err1 == nil {
165 // only report 1st error
170 // Delete in Syncthing
171 err2 := f.st.FolderDelete(f.stfConfig.ID)
173 // Delete folder on server side
174 err3 := os.RemoveAll(f.GetFullPath(""))
178 } else if err2 != nil {
184 // Update update some fields of a folder
185 func (f *STFolder) Update(cfg xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
186 if f.fConfig.ID != cfg.ID {
187 return nil, fmt.Errorf("Invalid id")
190 return &f.fConfig, nil
193 // Sync Force folder files synchronization
194 func (f *STFolder) Sync() error {
195 return f.st.FolderScan(f.stfConfig.ID, "")
198 // IsInSync Check if folder files are in-sync
199 func (f *STFolder) IsInSync() (bool, error) {
200 sts, err := f.st.IsFolderInSync(f.stfConfig.ID)
204 f.fConfig.IsInSync = sts
208 // callback use to update IsInSync status
209 func (f *STFolder) cbEventState(ev st.Event, data *st.EventsCBData) {
210 prevSync := f.fConfig.IsInSync
211 prevStatus := f.fConfig.Status
215 case st.EventStateChanged:
218 case "scanning", "syncing":
219 f.fConfig.Status = xsapiv1.StatusSyncing
221 f.fConfig.Status = xsapiv1.StatusEnable
223 f.fConfig.IsInSync = (to == "idle")
225 case st.EventFolderPaused:
226 if f.fConfig.Status == xsapiv1.StatusEnable {
227 f.fConfig.Status = xsapiv1.StatusPause
229 f.fConfig.IsInSync = false
232 if prevSync != f.fConfig.IsInSync || prevStatus != f.fConfig.Status {
233 // Emit Folder state change event
234 if err := f.events.Emit(xsapiv1.EVTFolderStateChange, &f.fConfig, ""); err != nil {
235 f.Log.Warningf("Cannot notify folder change: %v", err)