9 common "github.com/iotbzh/xds-common/golib"
10 uuid "github.com/satori/go.uuid"
11 "github.com/syncthing/syncthing/lib/sync"
14 // xmlServerData contains persistent data stored/loaded by server
15 type xmlServerData struct {
16 XMLName xml.Name `xml:"XDS-Server"`
17 Version string `xml:"version,attr"`
18 Data ServerData `xml:"server-data"`
21 type ServerData struct {
25 var sdMutex = sync.NewMutex()
28 func ServerIDGet() (string, error) {
33 if f, err = ServerDataFilenameGet(); err != nil {
36 if err = serverDataRead(f, &d); err != nil || d.ID == "" {
37 // Create a new uuid when not found
38 d.ID = uuid.NewV1().String()
39 if err := serverDataWrite(f, d); err != nil {
46 // serverDataRead reads data saved on disk
47 func serverDataRead(file string, data *ServerData) error {
48 if !common.Exists(file) {
49 return fmt.Errorf("No folder config file found (%s)", file)
53 defer sdMutex.Unlock()
55 fd, err := os.Open(file)
61 xsd := xmlServerData{}
62 err = xml.NewDecoder(fd).Decode(&xsd)
69 // serverDataWrite writes persistant data to disk
70 func serverDataWrite(file string, data ServerData) error {
72 defer sdMutex.Unlock()
74 dir := filepath.Dir(file)
75 if !common.Exists(dir) {
76 if err := os.MkdirAll(dir, 0755); err != nil {
77 return fmt.Errorf("Cannot create server data directory: %s", dir)
81 fd, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
87 xsd := &xmlServerData{
92 enc := xml.NewEncoder(fd)
94 return enc.Encode(xsd)