Moved project creation in a modal windows
[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 )
12
13 // IFOLDER interface implementation for native/path mapping folders
14
15 // PathMap .
16 type PathMap struct {
17         globalConfig *xdsconfig.Config
18         config       FolderConfig
19 }
20
21 // NewFolderPathMap Create a new instance of PathMap
22 func NewFolderPathMap(gc *xdsconfig.Config) *PathMap {
23         f := PathMap{
24                 globalConfig: gc,
25         }
26         return &f
27 }
28
29 // Add a new folder
30 func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) {
31         if cfg.DataPathMap.ServerPath == "" {
32                 return nil, fmt.Errorf("ServerPath must be set")
33         }
34
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)
39         }
40
41         // Sanity check
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)
46                 }
47         }
48         if !common.Exists(dir) {
49                 return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir)
50         }
51         file, err := ioutil.TempFile(dir, "xds_pathmap_check")
52         if err != nil {
53                 return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error())
54         }
55         defer os.Remove(file.Name())
56
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())
61         }
62
63         f.config = cfg
64         f.config.RootPath = dir
65         f.config.DataPathMap.ServerPath = dir
66         f.config.Status = StatusEnable
67
68         return &f.config, nil
69 }
70
71 // GetConfig Get public part of folder config
72 func (f *PathMap) GetConfig() FolderConfig {
73         return f.config
74 }
75
76 // GetFullPath returns the full path
77 func (f *PathMap) GetFullPath(dir string) string {
78         if &dir == nil {
79                 return f.config.DataPathMap.ServerPath
80         }
81         return filepath.Join(f.config.DataPathMap.ServerPath, dir)
82 }
83
84 // Remove a folder
85 func (f *PathMap) Remove() error {
86         // nothing to do
87         return nil
88 }
89
90 // Sync Force folder files synchronization
91 func (f *PathMap) Sync() error {
92         return nil
93 }
94
95 // IsInSync Check if folder files are in-sync
96 func (f *PathMap) IsInSync() (bool, error) {
97         return true, nil
98 }