Use go module as dependency tool instead of glide
[src/xds/xds-server.git] / lib / xdsserver / folder-st.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
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
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package xdsserver
19
20 import (
21         "fmt"
22         "os"
23         "path/filepath"
24         "strings"
25
26         st "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/syncthing"
27         "gerrit.automotivelinux.org/gerrit/src/xds/xds-server.git/lib/xsapiv1"
28         uuid "github.com/satori/go.uuid"
29         "github.com/syncthing/syncthing/lib/config"
30 )
31
32 // IFOLDER interface implementation for syncthing
33
34 // STFolder .
35 type STFolder struct {
36         *Context
37         st        *st.SyncThing
38         fConfig   xsapiv1.FolderConfig
39         stfConfig config.FolderConfiguration
40         eventIDs  []string
41 }
42
43 var stEventMonitored = []string{st.EventStateChanged, st.EventFolderPaused}
44
45 // NewFolderST Create a new instance of STFolder
46 func NewFolderST(ctx *Context, sthg *st.SyncThing) *STFolder {
47         return &STFolder{
48                 Context: ctx,
49                 st:      sthg,
50         }
51 }
52
53 // NewUID Get a UUID
54 func (f *STFolder) NewUID(suffix string) string {
55         i := len(f.st.MyID)
56         if i > 15 {
57                 i = 15
58         }
59         uuid := uuid.NewV1().String()[:14] + f.st.MyID[:i]
60         if len(suffix) > 0 {
61                 uuid += "_" + suffix
62         }
63         return uuid
64 }
65
66 // Add a new folder
67 func (f *STFolder) Add(cfg xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
68
69         // Sanity check
70         if cfg.DataCloudSync.SyncThingID == "" {
71                 return nil, fmt.Errorf("device id not set (SyncThingID field)")
72         }
73
74         // rootPath should not be empty
75         if cfg.RootPath == "" {
76                 cfg.RootPath = f.Config.FileConf.ShareRootDir
77         }
78
79         f.fConfig = cfg
80
81         // Update Syncthing folder
82         _, err := f.st.FolderChange(f.fConfig)
83         if err != nil {
84                 return nil, err
85         }
86
87         // Use Setup function to setup remains fields
88         return f.Setup(f.fConfig)
89 }
90
91 // Setup Setup local project config
92 func (f *STFolder) Setup(fld xsapiv1.FolderConfig) (*xsapiv1.FolderConfig, error) {
93
94         var err error
95
96         // Update folder Config
97         f.fConfig = fld
98
99         // Retrieve Syncthing folder config
100         f.stfConfig, err = f.st.FolderConfigGet(f.fConfig.ID)
101         if err != nil {
102                 f.fConfig.Status = xsapiv1.StatusErrorConfig
103                 return nil, err
104         }
105
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)
109                 if err != nil {
110                         return nil, err
111                 }
112                 f.eventIDs = append(f.eventIDs, evID)
113         }
114
115         f.fConfig.IsInSync = false // will be updated later by events
116         f.fConfig.Status = xsapiv1.StatusEnable
117
118         return &f.fConfig, nil
119 }
120
121 // GetConfig Get public part of folder config
122 func (f *STFolder) GetConfig() xsapiv1.FolderConfig {
123         return f.fConfig
124 }
125
126 // GetFullPath returns the full path of a directory (from server POV)
127 func (f *STFolder) GetFullPath(dir string) string {
128         if &dir == nil {
129                 dir = ""
130         }
131         if filepath.IsAbs(dir) {
132                 return filepath.Join(f.fConfig.RootPath, dir)
133         }
134         return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
135 }
136
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,
143                         -1)
144         }
145         return s
146 }
147
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,
154                         -1)
155         }
156         return s
157 }
158
159 // Remove a folder
160 func (f *STFolder) Remove() error {
161         var err1 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
166                         err1 = err
167                 }
168         }
169
170         // Delete in Syncthing
171         err2 := f.st.FolderDelete(f.stfConfig.ID)
172
173         // Delete folder on server side
174         err3 := os.RemoveAll(f.GetFullPath(""))
175
176         if err1 != nil {
177                 return err1
178         } else if err2 != nil {
179                 return err2
180         }
181         return err3
182 }
183
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")
188         }
189         f.fConfig = cfg
190         return &f.fConfig, nil
191 }
192
193 // Sync Force folder files synchronization
194 func (f *STFolder) Sync() error {
195         return f.st.FolderScan(f.stfConfig.ID, "")
196 }
197
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)
201         if err != nil {
202                 return false, err
203         }
204         f.fConfig.IsInSync = sts
205         return sts, nil
206 }
207
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
212
213         switch ev.Type {
214
215         case st.EventStateChanged:
216                 to := ev.Data["to"]
217                 switch to {
218                 case "scanning", "syncing":
219                         f.fConfig.Status = xsapiv1.StatusSyncing
220                 case "idle":
221                         f.fConfig.Status = xsapiv1.StatusEnable
222                 }
223                 f.fConfig.IsInSync = (to == "idle")
224
225         case st.EventFolderPaused:
226                 if f.fConfig.Status == xsapiv1.StatusEnable {
227                         f.fConfig.Status = xsapiv1.StatusPause
228                 }
229                 f.fConfig.IsInSync = false
230         }
231
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)
236                 }
237         }
238 }