Fallback to mkostemp if O_TMPFILE fails

This commit is contained in:
Kovid Goyal 2021-04-27 12:03:24 +05:30
parent 162e498347
commit ae1df38c88
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -82,14 +82,8 @@ new(PyTypeObject *type, PyObject UNUSED *args, PyObject UNUSED *kwds) {
}
static int
open_cache_file(const char *cache_path) {
open_cache_file_without_tmpfile(const char *cache_path) {
int fd = -1;
#ifdef O_TMPFILE
while (fd < 0) {
fd = safe_open(cache_path, O_TMPFILE | O_CLOEXEC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
if (fd > -1 || errno != EINTR) break;
}
#else
size_t sz = strlen(cache_path) + 16;
FREE_AFTER_FUNCTION char *buf = calloc(1, sz);
if (!buf) { errno = ENOMEM; return -1; }
@ -99,6 +93,20 @@ open_cache_file(const char *cache_path) {
if (fd > -1 || errno != EINTR) break;
}
if (fd > -1) unlink(buf);
return fd;
}
static int
open_cache_file(const char *cache_path) {
int fd = -1;
#ifdef O_TMPFILE
while (fd < 0) {
fd = safe_open(cache_path, O_TMPFILE | O_CLOEXEC | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
if (fd > -1 || errno != EINTR) break;
}
if (fd == -1) fd = open_cache_file_without_tmpfile(cache_path);
#else
fd = open_cache_file_without_tmpfile(cache_path);
#endif
return fd;
}