diff options
Diffstat (limited to 'lib/folder')
-rw-r--r-- | lib/folder/folder-interface.go | 80 | ||||
-rw-r--r-- | lib/folder/folder-pathmap.go | 175 | ||||
-rw-r--r-- | lib/folder/folder-st-disable.go | 91 |
3 files changed, 0 insertions, 346 deletions
diff --git a/lib/folder/folder-interface.go b/lib/folder/folder-interface.go deleted file mode 100644 index 3208869..0000000 --- a/lib/folder/folder-interface.go +++ /dev/null @@ -1,80 +0,0 @@ -package folder - -// FolderType definition -type FolderType string - -const ( - TypePathMap = "PathMap" - TypeCloudSync = "CloudSync" - TypeCifsSmb = "CIFS" -) - -// Folder Status definition -const ( - StatusErrorConfig = "ErrorConfig" - StatusDisable = "Disable" - StatusEnable = "Enable" - StatusPause = "Pause" - StatusSyncing = "Syncing" -) - -type EventCBData map[string]interface{} -type EventCB func(cfg *FolderConfig, data *EventCBData) - -// IFOLDER Folder interface -type IFOLDER interface { - NewUID(suffix string) string // Get a new folder UUID - Add(cfg FolderConfig) (*FolderConfig, error) // Add a new folder - GetConfig() FolderConfig // Get folder public configuration - GetFullPath(dir string) string // Get folder full path - ConvPathCli2Svr(s string) string // Convert path from Client to Server - ConvPathSvr2Cli(s string) string // Convert path from Server to Client - Remove() error // Remove a folder - Update(cfg FolderConfig) (*FolderConfig, error) // Update a new folder - RegisterEventChange(cb *EventCB, data *EventCBData) error // Request events registration (sent through WS) - UnRegisterEventChange() error // Un-register events - Sync() error // Force folder files synchronization - IsInSync() (bool, error) // Check if folder files are in-sync -} - -// FolderConfig is the config for one folder -type FolderConfig struct { - ID string `json:"id"` - Label string `json:"label"` - ClientPath string `json:"path"` - Type FolderType `json:"type"` - Status string `json:"status"` - IsInSync bool `json:"isInSync"` - DefaultSdk string `json:"defaultSdk"` - ClientData string `json:"clientData"` // free form field that can used by client - - // Not exported fields from REST API point of view - RootPath string `json:"-"` - - // FIXME: better to define an equivalent to union data and then implement - // UnmarshalJSON/MarshalJSON to decode/encode according to Type value - // Data interface{} `json:"data"` - - // Specific data depending on which Type is used - DataPathMap PathMapConfig `json:"dataPathMap,omitempty"` - DataCloudSync CloudSyncConfig `json:"dataCloudSync,omitempty"` -} - -// FolderConfigUpdatableFields List fields that can be updated using Update function -var FolderConfigUpdatableFields = []string{ - "Label", "DefaultSdk", "ClientData", -} - -// 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"` -} diff --git a/lib/folder/folder-pathmap.go b/lib/folder/folder-pathmap.go deleted file mode 100644 index c5691a3..0000000 --- a/lib/folder/folder-pathmap.go +++ /dev/null @@ -1,175 +0,0 @@ -package folder - -import ( - "fmt" - "io/ioutil" - "os" - "path/filepath" - "strings" - - common "github.com/iotbzh/xds-common/golib" - "github.com/iotbzh/xds-server/lib/xdsconfig" - uuid "github.com/satori/go.uuid" -) - -// IFOLDER interface implementation for native/path mapping folders - -// PathMap . -type PathMap struct { - globalConfig *xdsconfig.Config - config FolderConfig -} - -// NewFolderPathMap Create a new instance of PathMap -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 { - uuid := uuid.NewV1().String() - if len(suffix) > 0 { - uuid += "_" + suffix - } - return uuid -} - -// Add a new folder -func (f *PathMap) Add(cfg FolderConfig) (*FolderConfig, error) { - if cfg.DataPathMap.ServerPath == "" { - return nil, fmt.Errorf("ServerPath must be set") - } - - // Use shareRootDir if ServerPath is a relative path - dir := cfg.DataPathMap.ServerPath - if !filepath.IsAbs(dir) { - dir = filepath.Join(f.globalConfig.FileConf.ShareRootDir, dir) - } - - // Sanity check - if !common.Exists(dir) { - // try to create if not existing - if err := os.MkdirAll(dir, 0755); err != nil { - return nil, fmt.Errorf("Cannot create ServerPath directory: %s", dir) - } - } - if !common.Exists(dir) { - return nil, fmt.Errorf("ServerPath directory is not accessible: %s", dir) - } - - 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 -} - -// GetConfig Get public part of folder config -func (f *PathMap) GetConfig() FolderConfig { - return f.config -} - -// GetFullPath returns the full path of a directory (from server POV) -func (f *PathMap) GetFullPath(dir string) string { - if &dir == nil { - return f.config.DataPathMap.ServerPath - } - return filepath.Join(f.config.DataPathMap.ServerPath, dir) -} - -// ConvPathCli2Svr Convert path from Client to Server -func (f *PathMap) ConvPathCli2Svr(s string) string { - if f.config.ClientPath != "" && f.config.DataPathMap.ServerPath != "" { - return strings.Replace(s, - f.config.ClientPath, - f.config.DataPathMap.ServerPath, - -1) - } - return s -} - -// ConvPathSvr2Cli Convert path from Server to Client -func (f *PathMap) ConvPathSvr2Cli(s string) string { - if f.config.ClientPath != "" && f.config.DataPathMap.ServerPath != "" { - return strings.Replace(s, - f.config.DataPathMap.ServerPath, - f.config.ClientPath, - -1) - } - return s -} - -// Remove a folder -func (f *PathMap) Remove() error { - // nothing to do - return nil -} - -// Update update some fields of a folder -func (f *PathMap) Update(cfg FolderConfig) (*FolderConfig, error) { - if f.config.ID != cfg.ID { - return nil, fmt.Errorf("Invalid id") - } - f.config = cfg - return &f.config, nil -} - -// RegisterEventChange requests registration for folder change event -func (f *PathMap) RegisterEventChange(cb *EventCB, data *EventCBData) error { - return nil -} - -// UnRegisterEventChange remove registered callback -func (f *PathMap) UnRegisterEventChange() error { - return nil -} - -// Sync Force folder files synchronization -func (f *PathMap) Sync() error { - return nil -} - -// IsInSync Check if folder files are in-sync -func (f *PathMap) IsInSync() (bool, error) { - return true, nil -} diff --git a/lib/folder/folder-st-disable.go b/lib/folder/folder-st-disable.go deleted file mode 100644 index e936494..0000000 --- a/lib/folder/folder-st-disable.go +++ /dev/null @@ -1,91 +0,0 @@ -package folder - -import ( - "github.com/iotbzh/xds-server/lib/xdsconfig" - uuid "github.com/satori/go.uuid" -) - -// IFOLDER interface implementation for disabled Syncthing folders -// It's a "fallback" interface used to keep syncthing folders config even -// when syncthing is not running. - -// STFolderDisable . -type STFolderDisable struct { - globalConfig *xdsconfig.Config - config FolderConfig -} - -// NewFolderSTDisable Create a new instance of STFolderDisable -func NewFolderSTDisable(gc *xdsconfig.Config) *STFolderDisable { - f := STFolderDisable{ - globalConfig: gc, - } - return &f -} - -// NewUID Get a UUID -func (f *STFolderDisable) NewUID(suffix string) string { - uuid := uuid.NewV1().String() - if len(suffix) > 0 { - uuid += "_" + suffix - } - return uuid -} - -// Add a new folder -func (f *STFolderDisable) Add(cfg FolderConfig) (*FolderConfig, error) { - f.config = cfg - f.config.Status = StatusDisable - f.config.IsInSync = false - return &f.config, nil -} - -// GetConfig Get public part of folder config -func (f *STFolderDisable) GetConfig() FolderConfig { - return f.config -} - -// GetFullPath returns the full path of a directory (from server POV) -func (f *STFolderDisable) GetFullPath(dir string) string { - return "" -} - -// ConvPathCli2Svr Convert path from Client to Server -func (f *STFolderDisable) ConvPathCli2Svr(s string) string { - return "" -} - -// ConvPathSvr2Cli Convert path from Server to Client -func (f *STFolderDisable) ConvPathSvr2Cli(s string) string { - return "" -} - -// Remove a folder -func (f *STFolderDisable) Remove() error { - return nil -} - -// Update update some fields of a folder -func (f *STFolderDisable) Update(cfg FolderConfig) (*FolderConfig, error) { - return nil, nil -} - -// RegisterEventChange requests registration for folder change event -func (f *STFolderDisable) RegisterEventChange(cb *EventCB, data *EventCBData) error { - return nil -} - -// UnRegisterEventChange remove registered callback -func (f *STFolderDisable) UnRegisterEventChange() error { - return nil -} - -// Sync Force folder files synchronization -func (f *STFolderDisable) Sync() error { - return nil -} - -// IsInSync Check if folder files are in-sync -func (f *STFolderDisable) IsInSync() (bool, error) { - return false, nil -} |