From: Eric Engestrom Date: Mon, 3 Jun 2019 16:51:37 +0000 (+0100) Subject: util: add os_file_create_unique() X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=1b259f1ae798099de280dd0ee10018d1fd54be04;p=mesa.git util: add os_file_create_unique() Signed-off-by: Eric Engestrom Reviewed-by: Kristian H. Kristensen Reviewed-by: Marek Olšák --- diff --git a/src/util/os_file.c b/src/util/os_file.c index 756164c3dfe..ae41506332d 100644 --- a/src/util/os_file.c +++ b/src/util/os_file.c @@ -6,7 +6,30 @@ #include "os_file.h" #include +#include #include +#include + + +#if defined(WIN32) +#include +#define open _open +#define fdopen _fdopen +#define O_CREAT _O_CREAT +#define O_EXCL _O_EXCL +#define O_WRONLY _O_WRONLY +#endif + + +FILE * +os_file_create_unique(const char *filename, int filemode) +{ + int fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, filemode); + if (fd == -1) + return NULL; + return fdopen(fd, "w"); +} + #if defined(__linux__) diff --git a/src/util/os_file.h b/src/util/os_file.h index 2f97c19ed55..d691302d12d 100644 --- a/src/util/os_file.h +++ b/src/util/os_file.h @@ -8,10 +8,21 @@ #ifndef _OS_FILE_H_ #define _OS_FILE_H_ +#include + #ifdef __cplusplus extern "C" { #endif +/* + * Create a new file and opens it for writing-only. + * If the given filename already exists, nothing is done and NULL is returned. + * `errno` gets set to the failure reason; if that is not EEXIST, the caller + * might want to do something other than trying again. + */ +FILE * +os_file_create_unique(const char *filename, int filemode); + /* * Read a file. * Returns a char* that the caller must free(), or NULL and sets errno.