Use posix_fallocate when available

This commit is contained in:
emersion 2018-07-21 11:55:54 +01:00
parent ef0a6ea4d2
commit e02c486b60
2 changed files with 25 additions and 23 deletions

View File

@ -130,6 +130,10 @@ else
exclude_headers += 'xwayland.h' exclude_headers += 'xwayland.h'
endif endif
if cc.has_header_symbol('fcntl.h', 'posix_fallocate', prefix: '#define _POSIX_C_SOURCE 200112L')
conf_data.set('WLR_HAS_POSIX_FALLOCATE', true)
endif
includedir = get_option('includedir') includedir = get_option('includedir')
exclude_headers += 'meson.build' exclude_headers += 'meson.build'
install_subdir('include/wlr', install_dir: includedir, exclude_files: exclude_headers) install_subdir('include/wlr', install_dir: includedir, exclude_files: exclude_headers)

View File

@ -23,7 +23,7 @@
* SOFTWARE. * SOFTWARE.
*/ */
#define _XOPEN_SOURCE 700 #define _POSIX_C_SOURCE 200809L
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
@ -35,17 +35,18 @@
#include "util/os-compatibility.h" #include "util/os-compatibility.h"
int os_fd_set_cloexec(int fd) { int os_fd_set_cloexec(int fd) {
long flags; if (fd == -1) {
if (fd == -1)
return -1; return -1;
}
flags = fcntl(fd, F_GETFD); long flags = fcntl(fd, F_GETFD);
if (flags == -1) if (flags == -1) {
return -1; return -1;
}
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
return -1; return -1;
}
return 0; return 0;
} }
@ -58,15 +59,14 @@ int set_cloexec_or_close(int fd) {
return fd; return fd;
} }
int create_tmpfile_cloexec(char *tmpname) int create_tmpfile_cloexec(char *tmpname) {
{
int fd; int fd;
mode_t prev_umask = umask(0066); mode_t prev_umask = umask(0066);
#ifdef HAVE_MKOSTEMP #ifdef HAVE_MKOSTEMP
fd = mkostemp(tmpname, O_CLOEXEC); fd = mkostemp(tmpname, O_CLOEXEC);
if (fd >= 0) if (fd >= 0) {
unlink(tmpname); unlink(tmpname);
}
#else #else
fd = mkstemp(tmpname); fd = mkstemp(tmpname);
if (fd >= 0) { if (fd >= 0) {
@ -102,32 +102,29 @@ int create_tmpfile_cloexec(char *tmpname)
*/ */
int os_create_anonymous_file(off_t size) { int os_create_anonymous_file(off_t size) {
static const char template[] = "/wlroots-shared-XXXXXX"; static const char template[] = "/wlroots-shared-XXXXXX";
const char *path;
char *name;
int fd;
int ret;
path = getenv("XDG_RUNTIME_DIR"); const char *path = getenv("XDG_RUNTIME_DIR");
if (!path) { if (!path) {
errno = ENOENT; errno = ENOENT;
return -1; return -1;
} }
name = malloc(strlen(path) + sizeof(template)); char *name = malloc(strlen(path) + sizeof(template));
if (!name) if (!name) {
return -1; return -1;
}
strcpy(name, path); strcpy(name, path);
strcat(name, template); strcat(name, template);
fd = create_tmpfile_cloexec(name); int fd = create_tmpfile_cloexec(name);
free(name); free(name);
if (fd < 0) {
if (fd < 0)
return -1; return -1;
}
#ifdef HAVE_POSIX_FALLOCATE #ifdef WLR_HAS_POSIX_FALLOCATE
int ret;
do { do {
ret = posix_fallocate(fd, 0, size); ret = posix_fallocate(fd, 0, size);
} while (ret == EINTR); } while (ret == EINTR);
@ -137,6 +134,7 @@ int os_create_anonymous_file(off_t size) {
return -1; return -1;
} }
#else #else
int ret;
do { do {
ret = ftruncate(fd, size); ret = ftruncate(fd, size);
} while (ret < 0 && errno == EINTR); } while (ret < 0 && errno == EINTR);