dovecot-2.0: Added file_preallocate() to preallocate space to a ...

dovecot at dovecot.org dovecot at dovecot.org
Wed Oct 20 19:51:16 EEST 2010


details:   http://hg.dovecot.org/dovecot-2.0/rev/22c81f884032
changeset: 12319:22c81f884032
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Oct 20 17:50:03 2010 +0100
description:
Added file_preallocate() to preallocate space to a file without changing its size.
Implemented for Linux. Doesn't look like other OSes support this.

diffstat:

 configure.in            |   4 ++--
 src/lib/file-set-size.c |  16 ++++++++++++++++
 src/lib/file-set-size.h |   4 ++++
 3 files changed, 22 insertions(+), 2 deletions(-)

diffs (70 lines):

diff -r 8ccf177754b3 -r 22c81f884032 configure.in
--- a/configure.in	Wed Oct 20 16:07:03 2010 +0100
+++ b/configure.in	Wed Oct 20 17:50:03 2010 +0100
@@ -273,7 +273,7 @@
   sys/quota.h sys/fs/ufs_quota.h ufs/ufs/quota.h jfs/quota.h sys/fs/quota_common.h \
   mntent.h sys/mnttab.h sys/event.h sys/time.h sys/mkdev.h linux/dqblk_xfs.h \
   xfs/xqm.h execinfo.h ucontext.h malloc_np.h sys/utsname.h sys/vmount.h \
-  sys/utsname.h glob.h)
+  sys/utsname.h glob.h linux/falloc.h)
 
 dnl * gcc specific options
 if test "x$ac_cv_c_compiler_gnu" = "xyes"; then
@@ -369,7 +369,7 @@
 	       setrlimit setproctitle seteuid setreuid setegid setresgid \
 	       strtoull strtoll strtouq strtoq \
 	       setpriority quotactl getmntent kqueue kevent backtrace_symbols \
-	       walkcontext dirfd clearenv malloc_usable_size glob)
+	       walkcontext dirfd clearenv malloc_usable_size glob fallocate)
 
 AC_CHECK_LIB(rt, clock_gettime, [
   AC_DEFINE(HAVE_CLOCK_GETTIME,, Define if you have the clock_gettime function)
diff -r 8ccf177754b3 -r 22c81f884032 src/lib/file-set-size.c
--- a/src/lib/file-set-size.c	Wed Oct 20 16:07:03 2010 +0100
+++ b/src/lib/file-set-size.c	Wed Oct 20 17:50:03 2010 +0100
@@ -7,12 +7,16 @@
 #ifdef HAVE_POSIX_FALLOCATE
 #  define _XOPEN_SOURCE 600 /* Required by glibc, breaks Solaris 9 */
 #endif
+#define _GNU_SOURCE /* for fallocate() */
 #include "lib.h"
 #include "file-set-size.h"
 
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/stat.h>
+#ifdef HAVE_LINUX_FALLOC_H
+#  include <linux/falloc.h>
+#endif
 
 int file_set_size(int fd, off_t size)
 {
@@ -76,3 +80,15 @@
 	}
 	return 0;
 }
+
+int file_preallocate(int fd ATTR_UNUSED, off_t size ATTR_UNUSED)
+{
+#if defined(HAVE_FALLOCATE) && defined(FALLOC_FL_KEEP_SIZE)
+	/* Linux */
+	if (fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, size) < 0)
+		return errno == ENOSYS ? 0 : -1;
+	return 1;
+#else
+	return 0;
+#endif
+}
diff -r 8ccf177754b3 -r 22c81f884032 src/lib/file-set-size.h
--- a/src/lib/file-set-size.h	Wed Oct 20 16:07:03 2010 +0100
+++ b/src/lib/file-set-size.h	Wed Oct 20 17:50:03 2010 +0100
@@ -5,5 +5,9 @@
    be zeros. The file offset may be anywhere after this call.
    Returns -1 if failed, 0 if successful. */
 int file_set_size(int fd, off_t size);
+/* Preallocate file to given size, without actually changing the size
+   reported by stat(). Returns 1 if ok, 0 if not supported by this filesystem,
+   -1 if error. */
+int file_preallocate(int fd, off_t size);
 
 #endif


More information about the dovecot-cvs mailing list