From: Christian Brauner Date: Wed, 18 Oct 2017 17:48:57 +0000 (+0200) Subject: utils: add lxc_make_tmpfile() X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d60663509de95f71ce11ff0a33e7585ad05e426d;p=thirdparty%2Flxc.git utils: add lxc_make_tmpfile() Signed-off-by: Christian Brauner --- diff --git a/src/lxc/utils.c b/src/lxc/utils.c index e6a44a516..501f74d3a 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -2400,3 +2400,23 @@ bool lxc_nic_exists(char *nic) return true; } + +int lxc_make_tmpfile(char *template, bool rm) +{ + int fd, ret; + + fd = mkstemp(template); + if (fd < 0) + return -1; + + if (!rm) + return fd; + + ret = unlink(template); + if (ret < 0) { + close(fd); + return -1; + } + + return fd; +} diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 4aa21cdd4..27428e157 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -466,5 +466,6 @@ typedef __typeof__(((struct statfs *)NULL)->f_type) fs_type_magic; extern bool has_fs_type(const char *path, fs_type_magic magic_val); extern bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val); extern bool lxc_nic_exists(char *nic); +extern int lxc_make_tmpfile(char *template, bool rm); #endif /* __LXC_UTILS_H */