Add folder update support and ClientData field.
[src/xds/xds-server.git] / lib / folder / folder-pathmap.go
1 package folder
2
3 import (
4         "fmt"
5         "io/ioutil"
6         "os"
7         "path/filepath"
8         "strings"
9
10         common "github.com/iotbzh/xds-common/golib"
11         "github.com/iotbzh/xds-server/lib/xdsconfig"
12         uuid "github.com/satori/go.uuid"
13 )
14
15 // IFOLDER interface implementation for native/path mapping folders
16
17 // PathMap .
18 type PathMap struct {
19         globalConfig *xdsconfig.Config
20         config       FolderConfig
21 }
22
23 // NewFolderPathMap Create a new instance of PathMap
24 func NewFolderPathMap(gc *xdsconfig.Config) *PathMap {
25         f := PathMap{
26                 globalConfig: gc,
27                 config: FolderConfig{
28                         Status: StatusDisable,
29                 },
30         }
31         return &f
32 }
33
34 // NewUID Get a UUID
35 func (f *PathMap) NewUID(suffix string) string {
36         uuid := uuid.NewV1().String()
37         if len(suffix) > 0 {
38                 uuid += "_" + suffix
39         }
40         return uuid
41 }
42
43 // Add a new folder
44 func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) {
45         if cfg.DataPathMap.ServerPath == "" {
46                 return nil, fmt.Errorf("ServerPath must be set")
47         }
48
49         // Use shareRootDir if ServerPath is a relative path
50         dir := cfg.DataPathMap.ServerPath
51         if !filepath.IsAbs(dir) {
52                 dir = filepath.Join(f.globalConfig.FileConf.ShareRootDir, dir)
53         }
54
55         // Sanity check
56         if !common.Exists(dir) {
57                 // try to create if not existing
58                 if err := os.MkdirAll(dir, 0755); err != nil {
59                         return nil, fmt.Errorf("Cannot create ServerPath directory: %s", dir)
60                 }
61         }
62         if !common.Exists(dir) {
63                 return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir)
64         }
65
66         f.config = cfg
67         f.config.RootPath = dir
68         f.config.DataPathMap.ServerPath = dir
69         f.config.IsInSync = true
70
71         // Verify file created by XDS agent when needed
72         if cfg.DataPathMap.CheckFile != "" {
73                 errMsg := "ServerPath sanity check error (%d): %v"
74                 ckFile := f.ConvPathCli2Svr(cfg.DataPathMap.CheckFile)
75                 if !common.Exists(ckFile) {
76                         return nil, fmt.Errorf(errMsg, 1, "file not present")
77                 }
78                 if cfg.DataPathMap.CheckContent != "" {
79                         fd, err := os.OpenFile(ckFile, os.O_APPEND|os.O_RDWR, 0600)
80                         if err != nil {
81                                 return nil, fmt.Errorf(errMsg, 2, err)
82                         }
83                         defer fd.Close()
84
85                         // Check specific message written by agent
86                         content, err := ioutil.ReadAll(fd)
87                         if err != nil {
88                                 return nil, fmt.Errorf(errMsg, 3, err)
89                         }
90                         if string(content) != cfg.DataPathMap.CheckContent {
91                                 return nil, fmt.Errorf(errMsg, 4, "file content differ")
92                         }
93
94                         // Write a specific message that will be check back on agent side
95                         msg := "Pathmap checked message written by xds-server ID: " + f.globalConfig.ServerUID + "\n"
96                         if n, err := fd.WriteString(msg); n != len(msg) || err != nil {
97                                 return nil, fmt.Errorf(errMsg, 5, err)
98                         }
99                 }
100         }
101
102         f.config.Status = StatusEnable
103
104         return &f.config, nil
105 }
106
107 // GetConfig Get public part of folder config
108 func (f *PathMap) GetConfig() FolderConfig {
109         return f.config
110 }
111
112 // GetFullPath returns the full path of a directory (from server POV)
113 func (f *PathMap) GetFullPath(dir string) string {
114         if &dir == nil {
115                 return f.config.DataPathMap.ServerPath
116         }
117         return filepath.Join(f.config.DataPathMap.ServerPath, dir)
118 }
119
120 // ConvPathCli2Svr Convert path from Client to Server
121 func (f *PathMap) ConvPathCli2Svr(s string) string {
122         if f.config.ClientPath != "" && f.config.DataPathMap.ServerPath != "" {
123                 return strings.Replace(s,
124                         f.config.ClientPath,
125                         f.config.DataPathMap.ServerPath,
126                         -1)
127         }
128         return s
129 }
130
131 // ConvPathSvr2Cli Convert path from Server to Client
132 func (f *PathMap) ConvPathSvr2Cli(s string) string {
133         if f.config.ClientPath != "" && f.config.DataPathMap.ServerPath != "" {
134                 return strings.Replace(s,
135                         f.config.DataPathMap.ServerPath,
136                         f.config.ClientPath,
137                         -1)
138         }
139         return s
140 }
141
142 // Remove a folder
143 func (f *PathMap) Remove() error {
144         // nothing to do
145         return nil
146 }
147
148 // Update update some fields of a folder
149 func (f *PathMap) Update(cfg FolderConfig) (*FolderConfig, error) {
150         if f.config.ID != cfg.ID {
151                 return nil, fmt.Errorf("Invalid id")
152         }
153         f.config = cfg
154         return &f.config, nil
155 }
156
157 // RegisterEventChange requests registration for folder change event
158 func (f *PathMap) RegisterEventChange(cb *EventCB, data *EventCBData) error {
159         return nil
160 }
161
162 // UnRegisterEventChange remove registered callback
163 func (f *PathMap) UnRegisterEventChange() error {
164         return nil
165 }
166
167 // Sync Force folder files synchronization
168 func (f *PathMap) Sync() error {
169         return nil
170 }
171
172 // IsInSync Check if folder files are in-sync
173 func (f *PathMap) IsInSync() (bool, error) {
174         return true, nil
175 }