diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2018-11-19 08:44:57 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-19 08:44:57 -0800 |
commit | 64a29e383bb1c12b94a3f6f6be4538f9758d7fb9 (patch) | |
tree | f840ae31f0cb6c7d9ea6579e0b778fe58dd609b2 /libpod/runtime.go | |
parent | 4eecc8cf70728d733aa8d1c948093d9ae4a334df (diff) | |
parent | 027d6ca6de3644414ee1f847d161184d027e4137 (diff) | |
download | podman-64a29e383bb1c12b94a3f6f6be4538f9758d7fb9.tar.gz podman-64a29e383bb1c12b94a3f6f6be4538f9758d7fb9.tar.bz2 podman-64a29e383bb1c12b94a3f6f6be4538f9758d7fb9.zip |
Merge pull request #1806 from giuseppe/rootless-create-default-files
rootless: create user conf files when they don't exist
Diffstat (limited to 'libpod/runtime.go')
-rw-r--r-- | libpod/runtime.go | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/libpod/runtime.go b/libpod/runtime.go index 318cd0369..9feae03fc 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -264,6 +264,7 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) { configPath := ConfigPath foundConfig := true + rootlessConfigPath := "" if rootless.IsRootless() { home := os.Getenv("HOME") if runtime.config.SignaturePolicyPath == "" { @@ -272,7 +273,10 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) { runtime.config.SignaturePolicyPath = newPath } } - configPath = filepath.Join(home, ".config/containers/libpod.conf") + + rootlessConfigPath = filepath.Join(home, ".config/containers/libpod.conf") + + configPath = rootlessConfigPath if _, err := os.Stat(configPath); err != nil { foundConfig = false } @@ -317,6 +321,22 @@ func NewRuntime(options ...RuntimeOption) (runtime *Runtime, err error) { if err := makeRuntime(runtime); err != nil { return nil, err } + + if !foundConfig && rootlessConfigPath != "" { + os.MkdirAll(filepath.Dir(rootlessConfigPath), 0755) + file, err := os.OpenFile(rootlessConfigPath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) + if err != nil && !os.IsExist(err) { + return nil, errors.Wrapf(err, "cannot open file %s", rootlessConfigPath) + } + if err == nil { + defer file.Close() + enc := toml.NewEncoder(file) + if err := enc.Encode(runtime.config); err != nil { + os.Remove(rootlessConfigPath) + } + } + } + return runtime, nil } |