10 common "github.com/iotbzh/xds-common/golib"
11 "github.com/iotbzh/xds-server/lib/xdsconfig"
12 uuid "github.com/satori/go.uuid"
15 // IFOLDER interface implementation for native/path mapping folders
19 globalConfig *xdsconfig.Config
23 // NewFolderPathMap Create a new instance of PathMap
24 func NewFolderPathMap(gc *xdsconfig.Config) *PathMap {
28 Status: StatusDisable,
35 func (f *PathMap) NewUID(suffix string) string {
36 uuid := uuid.NewV1().String()
44 func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) {
45 if cfg.DataPathMap.ServerPath == "" {
46 return nil, fmt.Errorf("ServerPath must be set")
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)
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)
62 if !common.Exists(dir) {
63 return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir)
67 f.config.RootPath = dir
68 f.config.DataPathMap.ServerPath = dir
69 f.config.IsInSync = true
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")
78 if cfg.DataPathMap.CheckContent != "" {
79 fd, err := os.OpenFile(ckFile, os.O_APPEND|os.O_RDWR, 0600)
81 return nil, fmt.Errorf(errMsg, 2, err)
85 // Check specific message written by agent
86 content, err := ioutil.ReadAll(fd)
88 return nil, fmt.Errorf(errMsg, 3, err)
90 if string(content) != cfg.DataPathMap.CheckContent {
91 return nil, fmt.Errorf(errMsg, 4, "file content differ")
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)
102 f.config.Status = StatusEnable
104 return &f.config, nil
107 // GetConfig Get public part of folder config
108 func (f *PathMap) GetConfig() FolderConfig {
112 // GetFullPath returns the full path of a directory (from server POV)
113 func (f *PathMap) GetFullPath(dir string) string {
115 return f.config.DataPathMap.ServerPath
117 return filepath.Join(f.config.DataPathMap.ServerPath, dir)
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,
125 f.config.DataPathMap.ServerPath,
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,
143 func (f *PathMap) Remove() error {
148 // RegisterEventChange requests registration for folder change event
149 func (f *PathMap) RegisterEventChange(cb *EventCB, data *EventCBData) error {
153 // UnRegisterEventChange remove registered callback
154 func (f *PathMap) UnRegisterEventChange() error {
158 // Sync Force folder files synchronization
159 func (f *PathMap) Sync() error {
163 // IsInSync Check if folder files are in-sync
164 func (f *PathMap) IsInSync() (bool, error) {