# HG changeset patch # User Timo Sirainen # Date 1032155577 -10800 # Node ID 14bad0a48eb445fdf78c0d7b66dff8f1a409a464 # Parent ff68d1f1e75b394fa204338d232e2b0a3a2a00ec file_set_size() was buggy when it was supposed to shrink file diff -r ff68d1f1e75b -r 14bad0a48eb4 src/lib/file-set-size.c --- a/src/lib/file-set-size.c Mon Sep 16 08:44:59 2002 +0300 +++ b/src/lib/file-set-size.c Mon Sep 16 08:52:57 2002 +0300 @@ -32,38 +32,23 @@ int file_set_size(int fd, off_t size) { char block[1024]; - int ret, old_errno; off_t pos; i_assert(size >= 0); - /* FIXME: this may not be good idea, since mmap()ing and writing - to it creates fragmentation. */ - - /* try truncating it to the size we want. if this succeeds, the written - area is full of zeros - exactly what we want. however, this may not - work at all, in which case we fallback to write()ing the zeros. */ - ret = -1;/*ftruncate(fd, size)*/; - old_errno = errno; - pos = lseek(fd, 0, SEEK_END); - if (ret != -1 && pos == size) - return 0; - if (pos < 0) return -1; - if (pos > size) { - /* ftruncate() failed for some reason, even while we were - trying to make the file smaller */ - errno = old_errno; - return -1; - } - size -= pos; + if (size < pos) + return ftruncate(fd, size); + if (size == pos) + return 0; /* start growing the file */ memset(block, 0, sizeof(block)); + size -= pos; while ((uoff_t)size > sizeof(block)) { /* write in 1kb blocks */ if (write_full(fd, block, sizeof(block)) < 0)