Merge remote-tracking branch 'origin/wip'
[src/xds/xds-server.git] / lib / folder / folder-pathmap.go
1 package folder
2
3 import (
4         "fmt"
5         "io/ioutil"
6         "os"
7         "path/filepath"
8
9         common "github.com/iotbzh/xds-common/golib"
10         "github.com/iotbzh/xds-server/lib/xdsconfig"
11         uuid "github.com/satori/go.uuid"
12 )
13
14 // IFOLDER interface implementation for native/path mapping folders
15
16 // PathMap .
17 type PathMap struct {
18         globalConfig *xdsconfig.Config
19         config       FolderConfig
20 }
21
22 // NewFolderPathMap Create a new instance of PathMap
23 func NewFolderPathMap(gc *xdsconfig.Config) *PathMap {
24         f := PathMap{
25                 globalConfig: gc,
26         }
27         return &f
28 }
29
30 // NewUID Get a UUID
31 func (f *PathMap) NewUID(suffix string) string {
32         return uuid.NewV1().String() + "_" + suffix
33 }
34
35 // Add a new folder
36 func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) {
37         if cfg.DataPathMap.ServerPath == "" {
38                 return nil, fmt.Errorf("ServerPath must be set")
39         }
40
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)
45         }
46
47         // Sanity check
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)
52                 }
53         }
54         if !common.Exists(dir) {
55                 return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir)
56         }
57         file, err := ioutil.TempFile(dir, "xds_pathmap_check")
58         if err != nil {
59                 return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error())
60         }
61         defer os.Remove(file.Name())
62
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())
67         }
68
69         f.config = cfg
70         f.config.RootPath = dir
71         f.config.DataPathMap.ServerPath = dir
72         f.config.IsInSync = true
73         f.config.Status = StatusEnable
74
75         return &f.config, nil
76 }
77
78 // GetConfig Get public part of folder config
79 func (f *PathMap) GetConfig() FolderConfig {
80         return f.config
81 }
82
83 // GetFullPath returns the full path
84 func (f *PathMap) GetFullPath(dir string) string {
85         if &dir == nil {
86                 return f.config.DataPathMap.ServerPath
87         }
88         return filepath.Join(f.config.DataPathMap.ServerPath, dir)
89 }
90
91 // Remove a folder
92 func (f *PathMap) Remove() error {
93         // nothing to do
94         return nil
95 }
96
97 // RegisterEventChange requests registration for folder change event
98 func (f *PathMap) RegisterEventChange(cb *EventCB, data *EventCBData) error {
99         return nil
100 }
101
102 // UnRegisterEventChange remove registered callback
103 func (f *PathMap) UnRegisterEventChange() error {
104         return nil
105 }
106
107 // Sync Force folder files synchronization
108 func (f *PathMap) Sync() error {
109         return nil
110 }
111
112 // IsInSync Check if folder files are in-sync
113 func (f *PathMap) IsInSync() (bool, error) {
114         return true, nil
115 }