bc714dde688668f3811924284e21de1c73aeafc8
[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         return uuid.NewV1().String() + "_" + suffix
37 }
38
39 // Add a new folder
40 func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) {
41         if cfg.DataPathMap.ServerPath == "" {
42                 return nil, fmt.Errorf("ServerPath must be set")
43         }
44
45         // Use shareRootDir if ServerPath is a relative path
46         dir := cfg.DataPathMap.ServerPath
47         if !filepath.IsAbs(dir) {
48                 dir = filepath.Join(f.globalConfig.FileConf.ShareRootDir, dir)
49         }
50
51         // Sanity check
52         if !common.Exists(dir) {
53                 // try to create if not existing
54                 if err := os.MkdirAll(dir, 0755); err != nil {
55                         return nil, fmt.Errorf("Cannot create ServerPath directory: %s", dir)
56                 }
57         }
58         if !common.Exists(dir) {
59                 return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir)
60         }
61
62         f.config = cfg
63         f.config.RootPath = dir
64         f.config.DataPathMap.ServerPath = dir
65         f.config.IsInSync = true
66
67         // Verify file created by XDS agent when needed
68         if cfg.DataPathMap.CheckFile != "" {
69                 errMsg := "ServerPath sanity check error (%d): %v"
70                 ckFile := f.ConvPathCli2Svr(cfg.DataPathMap.CheckFile)
71                 if !common.Exists(ckFile) {
72                         return nil, fmt.Errorf(errMsg, 1, "file not present")
73                 }
74                 if cfg.DataPathMap.CheckContent != "" {
75                         fd, err := os.OpenFile(ckFile, os.O_APPEND|os.O_RDWR, 0600)
76                         if err != nil {
77                                 return nil, fmt.Errorf(errMsg, 2, err)
78                         }
79                         defer fd.Close()
80
81                         // Check specific message written by agent
82                         content, err := ioutil.ReadAll(fd)
83                         if err != nil {
84                                 return nil, fmt.Errorf(errMsg, 3, err)
85                         }
86                         if string(content) != cfg.DataPathMap.CheckContent {
87                                 return nil, fmt.Errorf(errMsg, 4, "file content differ")
88                         }
89
90                         // Write a specific message that will be check back on agent side
91                         msg := "Pathmap checked message written by xds-server ID: " + f.globalConfig.ServerUID + "\n"
92                         if n, err := fd.WriteString(msg); n != len(msg) || err != nil {
93                                 return nil, fmt.Errorf(errMsg, 5, err)
94                         }
95                 }
96         }
97
98         f.config.Status = StatusEnable
99
100         return &f.config, nil
101 }
102
103 // GetConfig Get public part of folder config
104 func (f *PathMap) GetConfig() FolderConfig {
105         return f.config
106 }
107
108 // GetFullPath returns the full path of a directory (from server POV)
109 func (f *PathMap) GetFullPath(dir string) string {
110         if &dir == nil {
111                 return f.config.DataPathMap.ServerPath
112         }
113         return filepath.Join(f.config.DataPathMap.ServerPath, dir)
114 }
115
116 // ConvPathCli2Svr Convert path from Client to Server
117 func (f *PathMap) ConvPathCli2Svr(s string) string {
118         if f.config.ClientPath != "" && f.config.DataPathMap.ServerPath != "" {
119                 return strings.Replace(s,
120                         f.config.ClientPath,
121                         f.config.DataPathMap.ServerPath,
122                         -1)
123         }
124         return s
125 }
126
127 // ConvPathSvr2Cli Convert path from Server to Client
128 func (f *PathMap) ConvPathSvr2Cli(s string) string {
129         if f.config.ClientPath != "" && f.config.DataPathMap.ServerPath != "" {
130                 return strings.Replace(s,
131                         f.config.DataPathMap.ServerPath,
132                         f.config.ClientPath,
133                         -1)
134         }
135         return s
136 }
137
138 // Remove a folder
139 func (f *PathMap) Remove() error {
140         // nothing to do
141         return nil
142 }
143
144 // RegisterEventChange requests registration for folder change event
145 func (f *PathMap) RegisterEventChange(cb *EventCB, data *EventCBData) error {
146         return nil
147 }
148
149 // UnRegisterEventChange remove registered callback
150 func (f *PathMap) UnRegisterEventChange() error {
151         return nil
152 }
153
154 // Sync Force folder files synchronization
155 func (f *PathMap) Sync() error {
156         return nil
157 }
158
159 // IsInSync Check if folder files are in-sync
160 func (f *PathMap) IsInSync() (bool, error) {
161         return true, nil
162 }