Add folder interface and support native pathmap folder type.
[src/xds/xds-server.git] / lib / syncthing / folder-st.go
1 package st
2
3 import (
4         "fmt"
5         "path/filepath"
6
7         "github.com/iotbzh/xds-server/lib/folder"
8         "github.com/iotbzh/xds-server/lib/xdsconfig"
9         "github.com/syncthing/syncthing/lib/config"
10 )
11
12 // IFOLDER interface implementation for syncthing
13
14 // STFolder .
15 type STFolder struct {
16         globalConfig *xdsconfig.Config
17         st           *SyncThing
18         fConfig      folder.FolderConfig
19         stfConfig    config.FolderConfiguration
20 }
21
22 // NewFolderST Create a new instance of STFolder
23 func (s *SyncThing) NewFolderST(gc *xdsconfig.Config) *STFolder {
24         return &STFolder{
25                 globalConfig: gc,
26                 st:           s,
27         }
28 }
29
30 // Add a new folder
31 func (f *STFolder) Add(cfg folder.FolderConfig) (*folder.FolderConfig, error) {
32
33         // Sanity check
34         if cfg.DataCloudSync.SyncThingID == "" {
35                 return nil, fmt.Errorf("device id not set (SyncThingID field)")
36         }
37
38         // rootPath should not be empty
39         if cfg.RootPath == "" {
40                 cfg.RootPath = f.globalConfig.FileConf.ShareRootDir
41         }
42
43         f.fConfig = cfg
44
45         f.fConfig.DataCloudSync.BuilderSThgID = f.st.MyID // FIXME - should be removed after local ST config rework
46
47         // Update Syncthing folder
48         // (expect if status is ErrorConfig)
49         // TODO: add cache to avoid multiple requests on startup
50         if f.fConfig.Status != folder.StatusErrorConfig {
51                 id, err := f.st.FolderChange(f.fConfig)
52                 if err != nil {
53                         return nil, err
54                 }
55
56                 f.stfConfig, err = f.st.FolderConfigGet(id)
57                 if err != nil {
58                         f.fConfig.Status = folder.StatusErrorConfig
59                         return nil, err
60                 }
61
62                 f.fConfig.Status = folder.StatusEnable
63         }
64
65         return &f.fConfig, nil
66 }
67
68 // GetConfig Get public part of folder config
69 func (f *STFolder) GetConfig() folder.FolderConfig {
70         return f.fConfig
71 }
72
73 // GetFullPath returns the full path
74 func (f *STFolder) GetFullPath(dir string) string {
75         if &dir == nil {
76                 dir = ""
77         }
78         if filepath.IsAbs(dir) {
79                 return filepath.Join(f.fConfig.RootPath, dir)
80         }
81         return filepath.Join(f.fConfig.RootPath, f.fConfig.ClientPath, dir)
82 }
83
84 // Remove a folder
85 func (f *STFolder) Remove() error {
86         return f.st.FolderDelete(f.stfConfig.ID)
87 }
88
89 // Sync Force folder files synchronization
90 func (f *STFolder) Sync() error {
91         return f.st.FolderScan(f.stfConfig.ID, "")
92 }
93
94 // IsInSync Check if folder files are in-sync
95 func (f *STFolder) IsInSync() (bool, error) {
96         return f.st.IsFolderInSync(f.stfConfig.ID)
97 }