2018-09-18 11:14:04 -04:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
2019-03-13 07:34:27 -04:00
|
|
|
"github.com/google/uuid"
|
2018-09-18 11:14:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func loadOrCreateUUID(path string) (string, error) {
|
|
|
|
err := os.MkdirAll(filepath.Dir(path), 0755)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-03-13 07:34:27 -04:00
|
|
|
var id string
|
|
|
|
idb, err := ioutil.ReadFile(path)
|
2018-09-18 11:14:04 -04:00
|
|
|
if os.IsNotExist(err) {
|
2019-03-13 07:34:27 -04:00
|
|
|
id = uuid.New().String()
|
|
|
|
if err := ioutils.AtomicWriteFile(path, []byte(id), os.FileMode(0600)); err != nil {
|
2018-09-18 11:14:04 -04:00
|
|
|
return "", fmt.Errorf("Error saving uuid file: %s", err)
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return "", fmt.Errorf("Error loading uuid file %s: %s", path, err)
|
2019-03-13 07:34:27 -04:00
|
|
|
} else {
|
|
|
|
idp, err := uuid.Parse(string(idb))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Error parsing uuid in file %s: %s", path, err)
|
|
|
|
}
|
|
|
|
id = idp.String()
|
2018-09-18 11:14:04 -04:00
|
|
|
}
|
2019-03-13 07:34:27 -04:00
|
|
|
return id, nil
|
2018-09-18 11:14:04 -04:00
|
|
|
}
|