From: Sebastien Douheret Date: Thu, 30 Nov 2017 10:15:14 +0000 (+0100) Subject: Create server data directory when not existing. X-Git-Tag: v1.0.0-rc1~3 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=commitdiff_plain;ds=sidebyside;h=0ddbe56c19c6fc820bc7011abc22c371c9244d88;p=src%2Fxds%2Fxds-server.git Create server data directory when not existing. --- diff --git a/lib/xdsconfig/config.go b/lib/xdsconfig/config.go index 0201d40..ae02577 100644 --- a/lib/xdsconfig/config.go +++ b/lib/xdsconfig/config.go @@ -54,6 +54,7 @@ func Init(cliCtx *cli.Context, log *logrus.Logger) (*Config, error) { dfltSTHomeDir = resDir } + // Retrieve Server ID (or create one the first time) uuid, err := ServerIDGet() if err != nil { return nil, err diff --git a/lib/xdsconfig/data.go b/lib/xdsconfig/data.go index 65e0fc6..b80e9a4 100644 --- a/lib/xdsconfig/data.go +++ b/lib/xdsconfig/data.go @@ -4,6 +4,7 @@ import ( "encoding/xml" "fmt" "os" + "path/filepath" common "github.com/iotbzh/xds-common/golib" uuid "github.com/satori/go.uuid" @@ -70,6 +71,13 @@ func serverDataWrite(file string, data ServerData) error { sdMutex.Lock() defer sdMutex.Unlock() + dir := filepath.Dir(file) + if !common.Exists(dir) { + if err := os.MkdirAll(dir, 0755); err != nil { + return fmt.Errorf("Cannot create server data directory: %s", dir) + } + } + fd, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) defer fd.Close() if err != nil {