github.com/rawahars/moby@v24.0.4+incompatible/daemon/id.go (about)

     1  package daemon // import "github.com/docker/docker/daemon"
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/docker/docker/pkg/ioutils"
     7  	"github.com/google/uuid"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  // loadOrCreateID loads the engine's ID from idPath, or generates a new ID
    12  // if it doesn't exist. It returns the ID, and any error that occurred when
    13  // saving the file.
    14  //
    15  // Note that this function expects the daemon's root directory to already have
    16  // been created with the right permissions and ownership (usually this would
    17  // be done by daemon.CreateDaemonRoot().
    18  func loadOrCreateID(idPath string) (string, error) {
    19  	var id string
    20  	idb, err := os.ReadFile(idPath)
    21  	if os.IsNotExist(err) {
    22  		id = uuid.New().String()
    23  		if err := ioutils.AtomicWriteFile(idPath, []byte(id), os.FileMode(0600)); err != nil {
    24  			return "", errors.Wrap(err, "error saving ID file")
    25  		}
    26  	} else if err != nil {
    27  		return "", errors.Wrapf(err, "error loading ID file %s", idPath)
    28  	} else {
    29  		id = string(idb)
    30  	}
    31  	return id, nil
    32  }