diff options
author | Giuseppe Scrivano <gscrivan@redhat.com> | 2018-10-23 22:06:14 +0200 |
---|---|---|
committer | Giuseppe Scrivano <gscrivan@redhat.com> | 2018-10-23 22:13:17 +0200 |
commit | dfc689efc9a5746b0c31147562c5051c45874002 (patch) | |
tree | a8cc2e5d875e4b3f23174fd1e4a41b459fa83b1a /libpod | |
parent | 10bab99ea01006c4ca0048e6177d753f0732add7 (diff) | |
download | podman-dfc689efc9a5746b0c31147562c5051c45874002.tar.gz podman-dfc689efc9a5746b0c31147562c5051c45874002.tar.bz2 podman-dfc689efc9a5746b0c31147562c5051c45874002.zip |
create: fix writing cidfile when using rootless
prevent opening the same file twice, since we re-exec podman in
rootless mode. While at it, also solve a possible race between the
check for the file and writing to it. Another process could have
created the file in the meanwhile and we would just end up overwriting
it.
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
Diffstat (limited to 'libpod')
-rw-r--r-- | libpod/util.go | 15 |
1 files changed, 4 insertions, 11 deletions
diff --git a/libpod/util.go b/libpod/util.go index 3b51e4fcc..7007b29cd 100644 --- a/libpod/util.go +++ b/libpod/util.go @@ -24,22 +24,15 @@ const ( DefaultTransport = "docker://" ) -// WriteFile writes a provided string to a provided path -func WriteFile(content string, path string) error { +// OpenExclusiveFile opens a file for writing and ensure it doesn't already exist +func OpenExclusiveFile(path string) (*os.File, error) { baseDir := filepath.Dir(path) if baseDir != "" { if _, err := os.Stat(baseDir); err != nil { - return err + return nil, err } } - f, err := os.Create(path) - if err != nil { - return err - } - defer f.Close() - f.WriteString(content) - f.Sync() - return nil + return os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666) } // FuncTimer helps measure the execution time of a function |