[dovecot-cvs] dovecot/src/lib file-set-size.c,1.4,1.5
tss at dovecot.org
tss at dovecot.org
Fri Mar 30 13:17:50 EEST 2007
Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv14699/src/lib
Modified Files:
file-set-size.c
Log Message:
Use posix_fallocate() if possible. Also did minor optimizations to the
implementation.
Index: file-set-size.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/file-set-size.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- file-set-size.c 26 Aug 2003 21:18:16 -0000 1.4
+++ file-set-size.c 30 Mar 2007 10:17:48 -0000 1.5
@@ -1,38 +1,47 @@
/* Copyright (c) 2002-2003 Timo Sirainen */
+#define _XOPEN_SOURCE 600 /* Required by glibc */
#include "lib.h"
-#include "write-full.h"
#include "file-set-size.h"
#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
int file_set_size(int fd, off_t size)
{
- char block[1024];
- off_t pos;
+#ifndef HAVE_POSIX_FALLOCATE
+ char block[4096];
+ off_t offset;
+ ssize_t ret;
+#endif
+ struct stat st;
i_assert(size >= 0);
- pos = lseek(fd, 0, SEEK_END);
- if (pos < 0)
+ if (fstat(fd, &st) < 0)
return -1;
- if (size < pos)
+ if (size < st.st_size)
return ftruncate(fd, size);
- if (size == pos)
+ if (size == st.st_size)
return 0;
+#ifdef HAVE_POSIX_FALLOCATE
+ return posix_fallocate(fd, 0, size);
+#else
/* start growing the file */
- memset(block, 0, sizeof(block));
+ offset = st.st_size;
+ memset(block, 0, I_MIN((ssize_t)sizeof(block), size - offset));
- size -= pos;
- while ((uoff_t)size > sizeof(block)) {
- /* write in 1kb blocks */
- if (write_full(fd, block, sizeof(block)) < 0)
+ while (offset < size) {
+ ret = pwrite(fd, block,
+ I_MIN((ssize_t)sizeof(block), size - offset),
+ offset);
+ if (ret < 0)
return -1;
- size -= sizeof(block);
+ offset += size;
}
-
- /* write the remainder */
- return write_full(fd, block, (size_t)size) < 0 ? -1 : 0;
+ return 0;
+#endif
}
More information about the dovecot-cvs
mailing list