diff options
Diffstat (limited to 'lib/folder')
-rw-r--r-- | lib/folder/folder-interface.go | 15 | ||||
-rw-r--r-- | lib/folder/folder-pathmap.go | 52 | ||||
-rw-r--r-- | lib/folder/folder-st-disable.go | 6 |
3 files changed, 54 insertions, 19 deletions
diff --git a/lib/folder/folder-interface.go b/lib/folder/folder-interface.go index 4beccb8..9eb6829 100644 --- a/lib/folder/folder-interface.go +++ b/lib/folder/folder-interface.go @@ -1,12 +1,12 @@ package folder // FolderType definition -type FolderType int +type FolderType string const ( - TypePathMap = 1 - TypeCloudSync = 2 - TypeCifsSmb = 3 + TypePathMap = "PathMap" + TypeCloudSync = "CloudSync" + TypeCifsSmb = "CIFS" ) // Folder Status definition @@ -61,10 +61,13 @@ type FolderConfig struct { // PathMapConfig Path mapping specific data type PathMapConfig struct { ServerPath string `json:"serverPath"` + + // Don't keep temporary file name (IOW we don't want to save it and reuse it) + CheckFile string `json:"checkFile" xml:"-"` + CheckContent string `json:"checkContent" xml:"-"` } // CloudSyncConfig CloudSync (AKA Syncthing) specific data type CloudSyncConfig struct { - SyncThingID string `json:"syncThingID"` - BuilderSThgID string `json:"builderSThgID"` + SyncThingID string `json:"syncThingID"` } diff --git a/lib/folder/folder-pathmap.go b/lib/folder/folder-pathmap.go index 1020026..e200164 100644 --- a/lib/folder/folder-pathmap.go +++ b/lib/folder/folder-pathmap.go @@ -24,13 +24,20 @@ type PathMap struct { func NewFolderPathMap(gc *xdsconfig.Config) *PathMap { f := PathMap{ globalConfig: gc, + config: FolderConfig{ + Status: StatusDisable, + }, } return &f } // NewUID Get a UUID func (f *PathMap) NewUID(suffix string) string { - return uuid.NewV1().String() + "_" + suffix + uuid := uuid.NewV1().String() + if len(suffix) > 0 { + uuid += "_" + suffix + } + return uuid } // Add a new folder @@ -55,22 +62,43 @@ func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) { if !common.Exists(dir) { return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir) } - file, err := ioutil.TempFile(dir, "xds_pathmap_check") - if err != nil { - return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error()) - } - defer os.Remove(file.Name()) - - msg := "sanity check PathMap Add folder" - n, err := file.Write([]byte(msg)) - if err != nil || n != len(msg) { - return nil, fmt.Errorf("ServerPath sanity check error: %s", err.Error()) - } f.config = cfg f.config.RootPath = dir f.config.DataPathMap.ServerPath = dir f.config.IsInSync = true + + // Verify file created by XDS agent when needed + if cfg.DataPathMap.CheckFile != "" { + errMsg := "ServerPath sanity check error (%d): %v" + ckFile := f.ConvPathCli2Svr(cfg.DataPathMap.CheckFile) + if !common.Exists(ckFile) { + return nil, fmt.Errorf(errMsg, 1, "file not present") + } + if cfg.DataPathMap.CheckContent != "" { + fd, err := os.OpenFile(ckFile, os.O_APPEND|os.O_RDWR, 0600) + if err != nil { + return nil, fmt.Errorf(errMsg, 2, err) + } + defer fd.Close() + + // Check specific message written by agent + content, err := ioutil.ReadAll(fd) + if err != nil { + return nil, fmt.Errorf(errMsg, 3, err) + } + if string(content) != cfg.DataPathMap.CheckContent { + return nil, fmt.Errorf(errMsg, 4, "file content differ") + } + + // Write a specific message that will be check back on agent side + msg := "Pathmap checked message written by xds-server ID: " + f.globalConfig.ServerUID + "\n" + if n, err := fd.WriteString(msg); n != len(msg) || err != nil { + return nil, fmt.Errorf(errMsg, 5, err) + } + } + } + f.config.Status = StatusEnable return &f.config, nil diff --git a/lib/folder/folder-st-disable.go b/lib/folder/folder-st-disable.go index f90b776..7b53ca8 100644 --- a/lib/folder/folder-st-disable.go +++ b/lib/folder/folder-st-disable.go @@ -25,7 +25,11 @@ func NewFolderSTDisable(gc *xdsconfig.Config) *STFolderDisable { // NewUID Get a UUID func (f *STFolderDisable) NewUID(suffix string) string { - return uuid.NewV1().String() + "_" + suffix + uuid := uuid.NewV1().String() + if len(suffix) > 0 { + uuid += "_" + suffix + } + return uuid } // Add a new folder |