9 common "github.com/iotbzh/xds-common/golib"
10 "github.com/iotbzh/xds-server/lib/xdsconfig"
11 uuid "github.com/satori/go.uuid"
14 // IFOLDER interface implementation for native/path mapping folders
18 globalConfig *xdsconfig.Config
22 // NewFolderPathMap Create a new instance of PathMap
23 func NewFolderPathMap(gc *xdsconfig.Config) *PathMap {
31 func (f *PathMap) NewUID(suffix string) string {
32 return uuid.NewV1().String() + "_" + suffix
36 func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) {
37 if cfg.DataPathMap.ServerPath == "" {
38 return nil, fmt.Errorf("ServerPath must be set")
41 // Use shareRootDir if ServerPath is a relative path
42 dir := cfg.DataPathMap.ServerPath
43 if !filepath.IsAbs(dir) {
44 dir = filepath.Join(f.globalConfig.FileConf.ShareRootDir, dir)
48 if !common.Exists(dir) {
49 // try to create if not existing
50 if err := os.MkdirAll(dir, 0755); err != nil {
51 return nil, fmt.Errorf("Cannot create ServerPath directory: %s", dir)
54 if !common.Exists(dir) {
55 return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir)
57 file, err := ioutil.TempFile(dir, "xds_pathmap_check")
59 return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error())
61 defer os.Remove(file.Name())
63 msg := "sanity check PathMap Add folder"
64 n, err := file.Write([]byte(msg))
65 if err != nil || n != len(msg) {
66 return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error())
70 f.config.RootPath = dir
71 f.config.DataPathMap.ServerPath = dir
72 f.config.IsInSync = true
73 f.config.Status = StatusEnable
78 // GetConfig Get public part of folder config
79 func (f *PathMap) GetConfig() FolderConfig {
83 // GetFullPath returns the full path
84 func (f *PathMap) GetFullPath(dir string) string {
86 return f.config.DataPathMap.ServerPath
88 return filepath.Join(f.config.DataPathMap.ServerPath, dir)
92 func (f *PathMap) Remove() error {
97 // RegisterEventChange requests registration for folder change event
98 func (f *PathMap) RegisterEventChange(cb *EventCB, data *EventCBData) error {
102 // UnRegisterEventChange remove registered callback
103 func (f *PathMap) UnRegisterEventChange() error {
107 // Sync Force folder files synchronization
108 func (f *PathMap) Sync() error {
112 // IsInSync Check if folder files are in-sync
113 func (f *PathMap) IsInSync() (bool, error) {