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.
25 common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git"
26 stconfig "github.com/syncthing/syncthing/lib/config"
27 "github.com/syncthing/syncthing/lib/protocol"
30 // FolderChangeArg argument structure used by FolderChange
31 type FolderChangeArg struct {
38 // FolderLoadFromStConfig Load/Retrieve folder config from syncthing database
40 func (s *SyncThing) FolderLoadFromStConfig(f *[]xsapiv1.FolderConfig) error {
42 defaultSdk := "" // cannot know which was the default sdk
44 stCfg, err := s.ConfigGet()
48 if len(stCfg.Devices) < 1 {
49 return fmt.Errorf("Cannot load syncthing config: no device defined")
51 devID := stCfg.Devices[0].DeviceID.String()
53 if len(stCfg.Devices) < 2 {
54 return fmt.Errorf("Cannot load syncthing config: no valid device found")
56 devID = stCfg.Devices[1].DeviceID.String()
59 for _, stFld := range stCfg.Folders {
60 *f = append(*f, xsapiv1.FolderConfig{
63 ClientPath: strings.TrimRight(stFld.Path, "/"),
64 Type: xsapiv1.TypeCloudSync,
65 Status: StatusDisable,
66 DefaultSdk: defaultSdk,
68 DataCloudSync: xsapiv1.CloudSyncConfig{SyncThingID: devID},
76 // FolderChange is called when configuration has changed
77 func (s *SyncThing) FolderChange(f FolderChangeArg) (string, error) {
80 stCfg, err := s.ConfigGet()
86 stClientID := f.SyncThingID
87 // Add new Device if needed
88 var devID protocol.DeviceID
89 if err := devID.UnmarshalText([]byte(stClientID)); err != nil {
90 s.log.Errorf("not a valid device id (err %v)", err)
94 newDevice := stconfig.DeviceConfiguration{
97 Addresses: []string{"dynamic"},
101 for _, device := range stCfg.Devices {
102 if device.DeviceID == devID {
108 stCfg.Devices = append(stCfg.Devices, newDevice)
111 // Add or update Folder settings
113 if label = f.Label; label == "" {
114 label = strings.Split(id, "/")[0]
116 if id = f.ID; id == "" {
117 id = stClientID[0:15] + "_" + label
120 // Resolve local path
121 pathCli, err := common.ResolveEnvVar(f.RelativePath)
123 pathCli = f.RelativePath
126 folder := stconfig.FolderConfiguration{
134 if s.conf.FileConf.SThgConf.RescanIntervalS > 0 {
135 folder.RescanIntervalS = s.conf.FileConf.SThgConf.RescanIntervalS
139 folder.Devices = append(folder.Devices, stconfig.FolderDeviceConfiguration{
140 DeviceID: newDevice.DeviceID,
144 var fld stconfig.FolderConfiguration
145 for _, fld = range stCfg.Folders {
146 if folder.ID == fld.ID {
153 stCfg.Folders = append(stCfg.Folders, folder)
154 fld = stCfg.Folders[0]
157 err = s.ConfigSet(stCfg)
162 // FolderDelete is called to delete a folder config
163 func (s *SyncThing) FolderDelete(id string) error {
164 // Get current config
165 stCfg, err := s.ConfigGet()
171 for i, fld := range stCfg.Folders {
173 stCfg.Folders = append(stCfg.Folders[:i], stCfg.Folders[i+1:]...)
174 err = s.ConfigSet(stCfg)
185 // FolderConfigGet Returns the configuration of a specific folder
186 func (s *SyncThing) FolderConfigGet(folderID string) (stconfig.FolderConfiguration, error) {
187 fc := stconfig.FolderConfiguration{}
189 return fc, fmt.Errorf("folderID not set")
191 cfg, err := s.ConfigGet()
195 for _, f := range cfg.Folders {
196 if f.ID == folderID {
201 return fc, fmt.Errorf("id not found")
204 // FolderStatus Returns all information about the current
205 func (s *SyncThing) FolderStatus(folderID string) (*FolderStatus, error) {
209 return nil, fmt.Errorf("folderID not set")
211 if err := s.client.HTTPGet("db/status?folder="+folderID, &data); err != nil {
214 if err := json.Unmarshal(data, &res); err != nil {
220 // IsFolderInSync Returns true when folder is in sync
221 func (s *SyncThing) IsFolderInSync(folderID string) (bool, error) {
222 sts, err := s.FolderStatus(folderID)
226 return sts.NeedBytes == 0 && sts.State == "idle", nil
229 // FolderScan Request immediate folder scan.
230 // Scan all folders if folderID param is empty
231 func (s *SyncThing) FolderScan(folderID string, subpath string) error {
234 url += "?folder=" + folderID
237 url += "&sub=" + subpath
240 return s.client.HTTPPost(url, "")