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 return uuid.NewV1().String() + "_" + suffix
40 func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) {
41 if cfg.DataPathMap.ServerPath == "" {
42 return nil, fmt.Errorf("ServerPath must be set")
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)
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)
58 if !common.Exists(dir) {
59 return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir)
63 f.config.RootPath = dir
64 f.config.DataPathMap.ServerPath = dir
65 f.config.IsInSync = true
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")
74 if cfg.DataPathMap.CheckContent != "" {
75 fd, err := os.OpenFile(ckFile, os.O_APPEND|os.O_RDWR, 0600)
77 return nil, fmt.Errorf(errMsg, 2, err)
81 // Check specific message written by agent
82 content, err := ioutil.ReadAll(fd)
84 return nil, fmt.Errorf(errMsg, 3, err)
86 if string(content) != cfg.DataPathMap.CheckContent {
87 return nil, fmt.Errorf(errMsg, 4, "file content differ")
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)
98 f.config.Status = StatusEnable
100 return &f.config, nil
103 // GetConfig Get public part of folder config
104 func (f *PathMap) GetConfig() FolderConfig {
108 // GetFullPath returns the full path of a directory (from server POV)
109 func (f *PathMap) GetFullPath(dir string) string {
111 return f.config.DataPathMap.ServerPath
113 return filepath.Join(f.config.DataPathMap.ServerPath, dir)
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,
121 f.config.DataPathMap.ServerPath,
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,
139 func (f *PathMap) Remove() error {
144 // RegisterEventChange requests registration for folder change event
145 func (f *PathMap) RegisterEventChange(cb *EventCB, data *EventCBData) error {
149 // UnRegisterEventChange remove registered callback
150 func (f *PathMap) UnRegisterEventChange() error {
154 // Sync Force folder files synchronization
155 func (f *PathMap) Sync() error {
159 // IsInSync Check if folder files are in-sync
160 func (f *PathMap) IsInSync() (bool, error) {