8 common "github.com/iotbzh/xds-common/golib"
9 uuid "github.com/satori/go.uuid"
10 "github.com/syncthing/syncthing/lib/sync"
13 // xmlServerData contains persistent data stored/loaded by server
14 type xmlServerData struct {
15 XMLName xml.Name `xml:"XDS-Server"`
16 Version string `xml:"version,attr"`
17 Data ServerData `xml:"server-data"`
20 type ServerData struct {
24 var sdMutex = sync.NewMutex()
27 func ServerIDGet() (string, error) {
32 if f, err = ServerDataFilenameGet(); err != nil {
35 if err = serverDataRead(f, &d); err != nil || d.ID == "" {
36 // Create a new uuid when not found
37 d.ID = uuid.NewV1().String()
38 if err := serverDataWrite(f, d); err != nil {
45 // serverDataRead reads data saved on disk
46 func serverDataRead(file string, data *ServerData) error {
47 if !common.Exists(file) {
48 return fmt.Errorf("No folder config file found (%s)", file)
52 defer sdMutex.Unlock()
54 fd, err := os.Open(file)
60 xsd := xmlServerData{}
61 err = xml.NewDecoder(fd).Decode(&xsd)
68 // serverDataWrite writes persistant data to disk
69 func serverDataWrite(file string, data ServerData) error {
71 defer sdMutex.Unlock()
73 fd, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
79 xsd := &xmlServerData{
84 enc := xml.NewEncoder(fd)
86 return enc.Encode(xsd)