102002625155cf2db780380b654803e5ed0f8d82
[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         "strings"
9
10         common "github.com/iotbzh/xds-common/golib"
11         "github.com/iotbzh/xds-server/lib/xdsconfig"
12         uuid "github.com/satori/go.uuid"
13 )
14
15 // IFOLDER interface implementation for native/path mapping folders
16
17 // PathMap .
18 type PathMap struct {
19         globalConfig *xdsconfig.Config
20         config       FolderConfig
21 }
22
23 // NewFolderPathMap Create a new instance of PathMap
24 func NewFolderPathMap(gc *xdsconfig.Config) *PathMap {
25         f := PathMap{
26                 globalConfig: gc,
27         }
28         return &f
29 }
30
31 // NewUID Get a UUID
32 func (f *PathMap) NewUID(suffix string) string {
33         return uuid.NewV1().String() + "_" + suffix
34 }
35
36 // Add a new folder
37 func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) {
38         if cfg.DataPathMap.ServerPath == "" {
39                 return nil, fmt.Errorf("ServerPath must be set")
40         }
41
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)
46         }
47
48         // Sanity check
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)
53                 }
54         }
55         if !common.Exists(dir) {
56                 return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir)
57         }
58         file, err := ioutil.TempFile(dir, "xds_pathmap_check")
59         if err != nil {
60                 return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error())
61         }
62         defer os.Remove(file.Name())
63
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())
68         }
69
70         f.config = cfg
71         f.config.RootPath = dir
72         f.config.DataPathMap.ServerPath = dir
73         f.config.IsInSync = true
74         f.config.Status = StatusEnable
75
76         return &f.config, nil
77 }
78
79 // GetConfig Get public part of folder config
80 func (f *PathMap) GetConfig() FolderConfig {
81         return f.config
82 }
83
84 // GetFullPath returns the full path of a directory (from server POV)
85 func (f *PathMap) GetFullPath(dir string) string {
86         if &dir == nil {
87                 return f.config.DataPathMap.ServerPath
88         }
89         return filepath.Join(f.config.DataPathMap.ServerPath, dir)
90 }
91
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,
96                         f.config.ClientPath,
97                         f.config.DataPathMap.ServerPath,
98                         -1)
99         }
100         return s
101 }
102
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,
108                         f.config.ClientPath,
109                         -1)
110         }
111         return s
112 }
113
114 // Remove a folder
115 func (f *PathMap) Remove() error {
116         // nothing to do
117         return nil
118 }
119
120 // RegisterEventChange requests registration for folder change event
121 func (f *PathMap) RegisterEventChange(cb *EventCB, data *EventCBData) error {
122         return nil
123 }
124
125 // UnRegisterEventChange remove registered callback
126 func (f *PathMap) UnRegisterEventChange() error {
127         return nil
128 }
129
130 // Sync Force folder files synchronization
131 func (f *PathMap) Sync() error {
132         return nil
133 }
134
135 // IsInSync Check if folder files are in-sync
136 func (f *PathMap) IsInSync() (bool, error) {
137         return true, nil
138 }