Migration to AGL gerrit (update go import)
[src/xds/xds-server.git] / lib / xdsconfig / data.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package xdsconfig
19
20 import (
21         "encoding/xml"
22         "fmt"
23         "os"
24         "path/filepath"
25
26         common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib"
27         uuid "github.com/satori/go.uuid"
28         "github.com/syncthing/syncthing/lib/sync"
29 )
30
31 // xmlServerData contains persistent data stored/loaded by server
32 type xmlServerData struct {
33         XMLName xml.Name   `xml:"XDS-Server"`
34         Version string     `xml:"version,attr"`
35         Data    ServerData `xml:"server-data"`
36 }
37
38 type ServerData struct {
39         ID string `xml:"id"`
40 }
41
42 var sdMutex = sync.NewMutex()
43
44 // ServerIDGet
45 func ServerIDGet() (string, error) {
46         var f string
47         var err error
48
49         d := ServerData{}
50         if f, err = ServerDataFilenameGet(); err != nil {
51                 return "", err
52         }
53         if err = serverDataRead(f, &d); err != nil || d.ID == "" {
54                 // Create a new uuid when not found
55                 d.ID = uuid.NewV1().String()
56                 if err := serverDataWrite(f, d); err != nil {
57                         return "", err
58                 }
59         }
60         return d.ID, nil
61 }
62
63 // serverDataRead reads data saved on disk
64 func serverDataRead(file string, data *ServerData) error {
65         if !common.Exists(file) {
66                 return fmt.Errorf("No folder config file found (%s)", file)
67         }
68
69         sdMutex.Lock()
70         defer sdMutex.Unlock()
71
72         fd, err := os.Open(file)
73         defer fd.Close()
74         if err != nil {
75                 return err
76         }
77
78         xsd := xmlServerData{}
79         err = xml.NewDecoder(fd).Decode(&xsd)
80         if err == nil {
81                 *data = xsd.Data
82         }
83         return err
84 }
85
86 // serverDataWrite writes persistant data to disk
87 func serverDataWrite(file string, data ServerData) error {
88         sdMutex.Lock()
89         defer sdMutex.Unlock()
90
91         dir := filepath.Dir(file)
92         if !common.Exists(dir) {
93                 if err := os.MkdirAll(dir, 0755); err != nil {
94                         return fmt.Errorf("Cannot create server data directory: %s", dir)
95                 }
96         }
97
98         fd, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
99         defer fd.Close()
100         if err != nil {
101                 return err
102         }
103
104         xsd := &xmlServerData{
105                 Version: "1",
106                 Data:    data,
107         }
108
109         enc := xml.NewEncoder(fd)
110         enc.Indent("", "  ")
111         return enc.Encode(xsd)
112 }