main.c (main): If we are resizing a plain file which is smaller
than the requested size, then we will attempt to
transparently extend the filesize in a sparse fashion by
writing a block at the end of the requested part of the
filesystem.
main.c (main), resize2fs.c (resize_fs), resize2fs.h: Change the
function prototype of resize_fs() so that it can modify
the new_size parameter with the actual new size of the
filesystem after the resize operation. (This can
sometimes be less than the requested new size if there
isn't enough space to create the necessary block group
metadata for that last bit of disk space.) Resize2fs now
prints the actual new size of the filesystem when it finishes.
diff --git a/resize/main.c b/resize/main.c
index cff2d0f..238e799 100644
--- a/resize/main.c
+++ b/resize/main.c
@@ -19,6 +19,7 @@
extern int optind;
#endif
#include <fcntl.h>
+#include <sys/stat.h>
#include "resize2fs.h"
@@ -119,6 +120,7 @@
blk_t max_size = 0;
io_manager io_ptr;
char *tmp;
+ struct stat st_buf;
initialize_ext2_error_table();
@@ -223,6 +225,21 @@
}
if (!new_size)
new_size = max_size;
+ /*
+ * If we are resizing a plain file, and it's not big enough,
+ * automatically extend it in a sparse fashion by writing the
+ * last requested block.
+ */
+ if ((new_size > max_size) &&
+ (stat(device_name, &st_buf) == 0) &&
+ S_ISREG(st_buf.st_mode) &&
+ ((tmp = malloc(fs->blocksize)) != 0)) {
+ memset(tmp, 0, fs->blocksize);
+ retval = io_channel_write_blk(fs->io, new_size-1, 1, tmp);
+ if (retval == 0)
+ max_size = new_size;
+ free(tmp);
+ }
if (!force && (new_size > max_size)) {
fprintf(stderr, _("The containing partition (or device)"
" is only %d blocks.\nYou requested a new size"
@@ -240,7 +257,7 @@
device_name);
exit(1);
}
- retval = resize_fs(fs, new_size, flags,
+ retval = resize_fs(fs, &new_size, flags,
((flags & RESIZE_PERCENT_COMPLETE) ?
resize_progress_func : 0));
if (retval) {