Mercurial > dovecot > core-2.2
changeset 12319:22c81f884032
Added file_preallocate() to preallocate space to a file without changing its size.
Implemented for Linux. Doesn't look like other OSes support this.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 20 Oct 2010 17:50:03 +0100 |
parents | 8ccf177754b3 |
children | b884441a713f |
files | configure.in src/lib/file-set-size.c src/lib/file-set-size.h |
diffstat | 3 files changed, 22 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- 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)
--- 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 +}
--- 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