[RFC wayland 09/18] os: Add wl_os_resize_file()

Derek Foreman derekf at osg.samsung.com
Tue Feb 9 16:55:56 UTC 2016


We have a couple of callers of ftruncate and posix_fallocate around, with
ifdef protecting the posix_fallocate calls.  Combine all this into a
single wl_os_resize_file().

Signed-off-by: Derek Foreman <derekf at osg.samsung.com>
---
 cursor/wayland-cursor.c | 10 +---------
 src/wayland-os.c        | 37 +++++++++++++++++++++++--------------
 src/wayland-os.h        |  3 +++
 3 files changed, 27 insertions(+), 23 deletions(-)

diff --git a/cursor/wayland-cursor.c b/cursor/wayland-cursor.c
index 7319812..691310d 100644
--- a/cursor/wayland-cursor.c
+++ b/cursor/wayland-cursor.c
@@ -33,8 +33,6 @@
 #include <string.h>
 #include <unistd.h>
 #include <sys/mman.h>
-#include <fcntl.h>
-#include <errno.h>
 
 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
 
@@ -81,15 +79,9 @@ err_free:
 static int
 shm_pool_resize(struct shm_pool *pool, int size)
 {
-	if (ftruncate(pool->fd, size) < 0)
+	if (wl_os_resize_file(pool->fd, size) < 0)
 		return 0;
 
-#ifdef HAVE_POSIX_FALLOCATE
-	errno = posix_fallocate(pool->fd, 0, size);
-	if (errno != 0)
-		return 0;
-#endif
-
 	wl_shm_pool_resize(pool->pool, size);
 
 	munmap(pool->data, pool->size);
diff --git a/src/wayland-os.c b/src/wayland-os.c
index 40e63d7..2cd6bf3 100644
--- a/src/wayland-os.c
+++ b/src/wayland-os.c
@@ -188,6 +188,28 @@ create_tmpfile_cloexec(char *tmpname)
 	return fd;
 }
 
+int wl_os_resize_file(int fd, off_t size)
+{
+	int ret;
+
+#ifdef HAVE_POSIX_FALLOCATE
+	ret = posix_fallocate(fd, 0, size);
+	if (ret != 0) {
+		close(fd);
+		errno = ret;
+		return -1;
+	}
+#endif
+	/* If we were shrinking the file we'll still need ftruncate */
+	ret = ftruncate(fd, size);
+	if (ret < 0) {
+		close(fd);
+		return -1;
+	}
+
+	return 0;
+}
+
 /*
  * Create a new, unique, anonymous file of the given size, and
  * return the file descriptor for it. The file descriptor is set
@@ -216,7 +238,6 @@ wl_os_create_anonymous_file(off_t size)
 	const char *path;
 	char *name;
 	int fd;
-	int ret;
 
 	path = getenv("XDG_RUNTIME_DIR");
 	if (!path) {
@@ -241,20 +262,8 @@ wl_os_create_anonymous_file(off_t size)
 	if (!size)
 		return fd;
 
-#ifdef HAVE_POSIX_FALLOCATE
-	ret = posix_fallocate(fd, 0, size);
-	if (ret != 0) {
-		close(fd);
-		errno = ret;
+	if (wl_os_resize_file(fd, size) < 0)
 		return -1;
-	}
-#else
-	ret = ftruncate(fd, size);
-	if (ret < 0) {
-		close(fd);
-		return -1;
-	}
-#endif
 
 	return fd;
 }
diff --git a/src/wayland-os.h b/src/wayland-os.h
index 465a638..300a343 100644
--- a/src/wayland-os.h
+++ b/src/wayland-os.h
@@ -47,6 +47,9 @@ int
 wl_os_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
 
 int
+wl_os_resize_file(int fd, off_t size);
+
+int
 wl_os_create_anonymous_file(off_t size);
 
 /*
-- 
2.7.0



More information about the wayland-devel mailing list