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 {
32 func (f *PathMap) NewUID(suffix string) string {
33 return uuid.NewV1().String() + "_" + suffix
37 func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) {
38 if cfg.DataPathMap.ServerPath == "" {
39 return nil, fmt.Errorf("ServerPath must be set")
42 // Use shareRootDir if ServerPath is a relative path
43 dir := cfg.DataPathMap.ServerPath
44 if !filepath.IsAbs(dir) {
45 dir = filepath.Join(f.globalConfig.FileConf.ShareRootDir, dir)
49 if !common.Exists(dir) {
50 // try to create if not existing
51 if err := os.MkdirAll(dir, 0755); err != nil {
52 return nil, fmt.Errorf("Cannot create ServerPath directory: %s", dir)
55 if !common.Exists(dir) {
56 return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir)
58 file, err := ioutil.TempFile(dir, "xds_pathmap_check")
60 return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error())
62 defer os.Remove(file.Name())
64 msg := "sanity check PathMap Add folder"
65 n, err := file.Write([]byte(msg))
66 if err != nil || n != len(msg) {
67 return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error())
71 f.config.RootPath = dir
72 f.config.DataPathMap.ServerPath = dir
73 f.config.IsInSync = true
74 f.config.Status = StatusEnable
79 // GetConfig Get public part of folder config
80 func (f *PathMap) GetConfig() FolderConfig {
84 // GetFullPath returns the full path of a directory (from server POV)
85 func (f *PathMap) GetFullPath(dir string) string {
87 return f.config.DataPathMap.ServerPath
89 return filepath.Join(f.config.DataPathMap.ServerPath, dir)
92 // ConvPathCli2Svr Convert path from Client to Server
93 func (f *PathMap) ConvPathCli2Svr(s string) string {
94 if f.config.ClientPath != "" && f.config.DataPathMap.ServerPath != "" {
95 return strings.Replace(s,
97 f.config.DataPathMap.ServerPath,
103 // ConvPathSvr2Cli Convert path from Server to Client
104 func (f *PathMap) ConvPathSvr2Cli(s string) string {
105 if f.config.ClientPath != "" && f.config.DataPathMap.ServerPath != "" {
106 return strings.Replace(s,
107 f.config.DataPathMap.ServerPath,
115 func (f *PathMap) Remove() error {
120 // RegisterEventChange requests registration for folder change event
121 func (f *PathMap) RegisterEventChange(cb *EventCB, data *EventCBData) error {
125 // UnRegisterEventChange remove registered callback
126 func (f *PathMap) UnRegisterEventChange() error {
130 // Sync Force folder files synchronization
131 func (f *PathMap) Sync() error {
135 // IsInSync Check if folder files are in-sync
136 func (f *PathMap) IsInSync() (bool, error) {