9 common "github.com/iotbzh/xds-common/golib"
10 "github.com/iotbzh/xds-server/lib/xdsconfig"
13 // IFOLDER interface implementation for native/path mapping folders
17 globalConfig *xdsconfig.Config
21 // NewFolderPathMap Create a new instance of PathMap
22 func NewFolderPathMap(gc *xdsconfig.Config) *PathMap {
30 func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) {
31 if cfg.DataPathMap.ServerPath == "" {
32 return nil, fmt.Errorf("ServerPath must be set")
35 // Use shareRootDir if ServerPath is a relative path
36 dir := cfg.DataPathMap.ServerPath
37 if !filepath.IsAbs(dir) {
38 dir = filepath.Join(f.globalConfig.FileConf.ShareRootDir, dir)
42 if !common.Exists(dir) {
43 // try to create if not existing
44 if err := os.MkdirAll(dir, 0755); err != nil {
45 return nil, fmt.Errorf("Cannot create ServerPath directory: %s", dir)
48 if !common.Exists(dir) {
49 return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir)
51 file, err := ioutil.TempFile(dir, "xds_pathmap_check")
53 return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error())
55 defer os.Remove(file.Name())
57 msg := "sanity check PathMap Add folder"
58 n, err := file.Write([]byte(msg))
59 if err != nil || n != len(msg) {
60 return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error())
64 f.config.RootPath = dir
65 f.config.DataPathMap.ServerPath = dir
66 f.config.Status = StatusEnable
71 // GetConfig Get public part of folder config
72 func (f *PathMap) GetConfig() FolderConfig {
76 // GetFullPath returns the full path
77 func (f *PathMap) GetFullPath(dir string) string {
79 return f.config.DataPathMap.ServerPath
81 return filepath.Join(f.config.DataPathMap.ServerPath, dir)
85 func (f *PathMap) Remove() error {
90 // Sync Force folder files synchronization
91 func (f *PathMap) Sync() error {
95 // IsInSync Check if folder files are in-sync
96 func (f *PathMap) IsInSync() (bool, error) {