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 "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1"
27 stconfig "github.com/syncthing/syncthing/lib/config"
28 "github.com/syncthing/syncthing/lib/protocol"
31 // FolderLoadFromStConfig Load/Retrieve folder config from syncthing database
32 func (s *SyncThing) FolderLoadFromStConfig(f *[]xsapiv1.FolderConfig) error {
34 defaultSdk := "" // cannot know which was the default sdk
36 stCfg, err := s.ConfigGet()
40 if len(stCfg.Devices) < 1 {
41 return fmt.Errorf("Cannot load syncthing config: no device defined")
43 devID := stCfg.Devices[0].DeviceID.String()
45 if len(stCfg.Devices) < 2 {
46 return fmt.Errorf("Cannot load syncthing config: no valid device found")
48 devID = stCfg.Devices[1].DeviceID.String()
51 for _, stFld := range stCfg.Folders {
52 cliPath := strings.TrimPrefix(stFld.Path, s.conf.FileConf.ShareRootDir)
56 *f = append(*f, xsapiv1.FolderConfig{
59 ClientPath: strings.TrimRight(cliPath, "/"),
60 Type: xsapiv1.TypeCloudSync,
61 Status: xsapiv1.StatusDisable,
62 DefaultSdk: defaultSdk,
63 RootPath: s.conf.FileConf.ShareRootDir,
64 DataCloudSync: xsapiv1.CloudSyncConfig{SyncThingID: devID},
71 // FolderChange is called when configuration has changed
72 func (s *SyncThing) FolderChange(f xsapiv1.FolderConfig) (string, error) {
75 if id = f.ID; id == "" {
76 s.log.Errorln("Try to create Syncthing folder with null ID: %v", f)
77 return "", fmt.Errorf("Cannot create Syncthing folder (ID must be set")
81 stCfg, err := s.ConfigGet()
87 stClientID := f.DataCloudSync.SyncThingID
88 // Add new Device if needed
89 var devID protocol.DeviceID
90 if err := devID.UnmarshalText([]byte(stClientID)); err != nil {
91 s.log.Errorf("not a valid device id (err %v)", err)
95 newDevice := stconfig.DeviceConfiguration{
98 Addresses: []string{"dynamic"},
102 for _, device := range stCfg.Devices {
103 if device.DeviceID == devID {
109 stCfg.Devices = append(stCfg.Devices, newDevice)
112 // Add or update Folder settings
113 if label = f.Label; label == "" {
114 label = strings.Split(id, "/")[0]
117 folder := stconfig.FolderConfiguration{
120 Path: filepath.Join(s.conf.FileConf.ShareRootDir, f.ClientPath),
123 if s.conf.FileConf.SThgConf.RescanIntervalS > 0 {
124 folder.RescanIntervalS = s.conf.FileConf.SThgConf.RescanIntervalS
127 folder.Devices = append(folder.Devices, stconfig.FolderDeviceConfiguration{
128 DeviceID: newDevice.DeviceID,
132 var fld stconfig.FolderConfiguration
133 for _, fld = range stCfg.Folders {
134 if folder.ID == fld.ID {
141 stCfg.Folders = append(stCfg.Folders, folder)
142 fld = stCfg.Folders[0]
145 err = s.ConfigSet(stCfg)
153 // FolderDelete is called to delete a folder config
154 func (s *SyncThing) FolderDelete(id string) error {
155 // Get current config
156 stCfg, err := s.ConfigGet()
162 for i, fld := range stCfg.Folders {
164 stCfg.Folders = append(stCfg.Folders[:i], stCfg.Folders[i+1:]...)
165 err = s.ConfigSet(stCfg)
176 // FolderConfigGet Returns the configuration of a specific folder
177 func (s *SyncThing) FolderConfigGet(folderID string) (stconfig.FolderConfiguration, error) {
178 fc := stconfig.FolderConfiguration{}
180 return fc, fmt.Errorf("folderID not set")
182 cfg, err := s.ConfigGet()
186 for _, f := range cfg.Folders {
187 if f.ID == folderID {
192 return fc, fmt.Errorf("id not found")
195 // FolderStatus Returns all information about the current
196 func (s *SyncThing) FolderStatus(folderID string) (*FolderStatus, error) {
200 return nil, fmt.Errorf("folderID not set")
202 if err := s.client.HTTPGet("db/status?folder="+folderID, &data); err != nil {
205 if err := json.Unmarshal(data, &res); err != nil {
211 // IsFolderInSync Returns true when folder is in sync
212 func (s *SyncThing) IsFolderInSync(folderID string) (bool, error) {
213 sts, err := s.FolderStatus(folderID)
217 return sts.NeedBytes == 0 && sts.State == "idle", nil
220 // FolderScan Request immediate folder scan.
221 // Scan all folders if folderID param is empty
222 func (s *SyncThing) FolderScan(folderID string, subpath string) error {
225 url += "?folder=" + folderID
228 url += "&sub=" + subpath
231 return s.client.HTTPPost(url, "")