diff options
Diffstat (limited to 'lib/xdsconfig')
-rw-r--r-- | lib/xdsconfig/builderconfig.go | 5 | ||||
-rw-r--r-- | lib/xdsconfig/config.go | 25 | ||||
-rw-r--r-- | lib/xdsconfig/data.go | 87 | ||||
-rw-r--r-- | lib/xdsconfig/fileconfig.go | 17 |
4 files changed, 119 insertions, 15 deletions
diff --git a/lib/xdsconfig/builderconfig.go b/lib/xdsconfig/builderconfig.go index c64fe9c..6fc1814 100644 --- a/lib/xdsconfig/builderconfig.go +++ b/lib/xdsconfig/builderconfig.go @@ -28,10 +28,7 @@ func NewBuilderConfig(stID string) (BuilderConfig, error) { return b, nil } -// Copy makes a real copy of BuilderConfig -func (c *BuilderConfig) Copy(n BuilderConfig) { - // TODO -} +/*** Private ***/ func getLocalIP() (string, error) { addrs, err := net.InterfaceAddrs() diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go index 84e0778..2f1fa96 100644 --- a/lib/xdsconfig/config.go +++ b/lib/xdsconfig/config.go @@ -13,10 +13,12 @@ import ( // Config parameters (json format) of /config command type Config struct { - Version string `json:"version"` - APIVersion string `json:"apiVersion"` - VersionGitTag string `json:"gitTag"` - Builder BuilderConfig `json:"builder"` + ServerUID string `json:"id"` + Version string `json:"version"` + APIVersion string `json:"apiVersion"` + VersionGitTag string `json:"gitTag"` + SupportedSharing map[string]bool `json:"supportedSharing"` + Builder BuilderConfig `json:"builder"` // Private (un-exported fields in REST GET /config route) Options Options `json:"-"` @@ -55,12 +57,19 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) { dfltSTHomeDir = resDir } + uuid, err := ServerIDGet() + if err != nil { + return nil, err + } + // Define default configuration c := Config{ - Version: cliCtx.App.Metadata["version"].(string), - APIVersion: DefaultAPIVersion, - VersionGitTag: cliCtx.App.Metadata["git-tag"].(string), - Builder: BuilderConfig{}, + ServerUID: uuid, + Version: cliCtx.App.Metadata["version"].(string), + APIVersion: DefaultAPIVersion, + VersionGitTag: cliCtx.App.Metadata["git-tag"].(string), + Builder: BuilderConfig{}, + SupportedSharing: map[string]bool{ /*FIXME USE folder.TypePathMap*/ "PathMap": true}, Options: Options{ ConfigFile: cliCtx.GlobalString("config"), diff --git a/lib/xdsconfig/data.go b/lib/xdsconfig/data.go new file mode 100644 index 0000000..65e0fc6 --- /dev/null +++ b/lib/xdsconfig/data.go @@ -0,0 +1,87 @@ +package xdsconfig + +import ( + "encoding/xml" + "fmt" + "os" + + common "github.com/iotbzh/xds-common/golib" + uuid "github.com/satori/go.uuid" + "github.com/syncthing/syncthing/lib/sync" +) + +// xmlServerData contains persistent data stored/loaded by server +type xmlServerData struct { + XMLName xml.Name `xml:"XDS-Server"` + Version string `xml:"version,attr"` + Data ServerData `xml:"server-data"` +} + +type ServerData struct { + ID string `xml:"id"` +} + +var sdMutex = sync.NewMutex() + +// ServerIDGet +func ServerIDGet() (string, error) { + var f string + var err error + + d := ServerData{} + if f, err = ServerDataFilenameGet(); err != nil { + return "", err + } + if err = serverDataRead(f, &d); err != nil || d.ID == "" { + // Create a new uuid when not found + d.ID = uuid.NewV1().String() + if err := serverDataWrite(f, d); err != nil { + return "", err + } + } + return d.ID, nil +} + +// serverDataRead reads data saved on disk +func serverDataRead(file string, data *ServerData) error { + if !common.Exists(file) { + return fmt.Errorf("No folder config file found (%s)", file) + } + + sdMutex.Lock() + defer sdMutex.Unlock() + + fd, err := os.Open(file) + defer fd.Close() + if err != nil { + return err + } + + xsd := xmlServerData{} + err = xml.NewDecoder(fd).Decode(&xsd) + if err == nil { + *data = xsd.Data + } + return err +} + +// serverDataWrite writes persistant data to disk +func serverDataWrite(file string, data ServerData) error { + sdMutex.Lock() + defer sdMutex.Unlock() + + fd, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) + defer fd.Close() + if err != nil { + return err + } + + xsd := &xmlServerData{ + Version: "1", + Data: data, + } + + enc := xml.NewEncoder(fd) + enc.Indent("", " ") + return enc.Encode(xsd) +} diff --git a/lib/xdsconfig/fileconfig.go b/lib/xdsconfig/fileconfig.go index 2651caf..39560a4 100644 --- a/lib/xdsconfig/fileconfig.go +++ b/lib/xdsconfig/fileconfig.go @@ -16,6 +16,8 @@ const ( ConfigDir = ".xds-server" // GlobalConfigFilename Global config filename GlobalConfigFilename = "config.json" + // ServerDataFilename Server data filename + ServerDataFilename = "server-data.xml" // FoldersConfigFilename Folders config filename FoldersConfigFilename = "server-config_folders.xml" ) @@ -146,11 +148,20 @@ func readGlobalConfig(c *Config, confFile string) error { return nil } -// FoldersConfigFilenameGet -func FoldersConfigFilenameGet() (string, error) { +func configFilenameGet(cfgFile string) (string, error) { usr, err := user.Current() if err != nil { return "", err } - return path.Join(usr.HomeDir, ConfigDir, FoldersConfigFilename), nil + return path.Join(usr.HomeDir, ConfigDir, cfgFile), nil +} + +// FoldersConfigFilenameGet +func FoldersConfigFilenameGet() (string, error) { + return configFilenameGet(FoldersConfigFilename) +} + +// ServerDataFilenameGet +func ServerDataFilenameGet() (string, error) { + return configFilenameGet(ServerDataFilename) } |