From: Cong Wang Date: Fri, 18 Nov 2011 08:51:37 +0000 (+0800) Subject: fallocate: add FALLOC_FL_PUNCH_HOLE support X-Git-Tag: v2.21-rc1~163 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=411fd3c27aed72c50677aaf006ebfa6df39a1cfc;p=thirdparty%2Futil-linux.git fallocate: add FALLOC_FL_PUNCH_HOLE support Recent Linux kernel supports FALLOC_FL_PUNCH_HOLE in fallocate(2). This patch adds FALLOC_FL_PUNCH_HOLE support to fallocate utility, by introducing a new option -p|--punch-hole. [kzak@redhat.com: - fix merge conflict in fallocate.1] Cc: Karel Zak Cc: Eric Sandeen Signed-off-by: Cong Wang Signed-off-by: Karel Zak --- diff --git a/sys-utils/fallocate.1 b/sys-utils/fallocate.1 index c0dd483e74..653ebfbe41 100644 --- a/sys-utils/fallocate.1 +++ b/sys-utils/fallocate.1 @@ -5,6 +5,7 @@ fallocate \- preallocate space to a file .SH SYNOPSIS .B fallocate .RB [ \-n ] +.RB [ \-p ] .RB [ \-o .IR offset ] .B \-l @@ -31,6 +32,8 @@ same meaning as "KiB") or decimal (10^N) suffixes KB, MB, GB, PB and EB. .IP "\fB\-n, \-\-keep-size\fP" Do not modify the apparent length of the file. This may effectively allocate blocks past EOF, which can be removed with a truncate. +.IP "\fB\-p, \-\-punch-hole\fP" +Punch holes in the file, the range should not exceed the length of the file. .IP "\fB\-o, \-\-offset\fP \fIoffset\fP Specifies the beginning offset of the allocation, in bytes. .IP "\fB\-l, \-\-length\fP \fIlength\fP diff --git a/sys-utils/fallocate.c b/sys-utils/fallocate.c index 13d7277fe8..b58485a780 100644 --- a/sys-utils/fallocate.c +++ b/sys-utils/fallocate.c @@ -40,6 +40,7 @@ # include /* for FALLOC_FL_* flags */ #else # define FALLOC_FL_KEEP_SIZE 1 +# define FALLOC_FL_PUNCH_HOLE 2 #endif #include "nls.h" @@ -57,6 +58,7 @@ static void __attribute__((__noreturn__)) usage(FILE *out) fputs(_("\nOptions:\n"), out); fputs(_(" -h, --help this help\n" " -n, --keep-size don't modify the length of the file\n" + " -p, --punch-hole punch holes in the file\n" " -o, --offset offset of the allocation, in bytes\n" " -l, --length length of the allocation, in bytes\n"), out); @@ -88,6 +90,7 @@ int main(int argc, char **argv) static const struct option longopts[] = { { "help", 0, 0, 'h' }, { "keep-size", 0, 0, 'n' }, + { "punch-hole", 0, 0, 'p' }, { "offset", 1, 0, 'o' }, { "length", 1, 0, 'l' }, { NULL, 0, 0, 0 } @@ -97,11 +100,14 @@ int main(int argc, char **argv) bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); - while ((c = getopt_long(argc, argv, "hnl:o:", longopts, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "hnpl:o:", longopts, NULL)) != -1) { switch(c) { case 'h': usage(stdout); break; + case 'p': + mode |= FALLOC_FL_PUNCH_HOLE; + /* fall through */ case 'n': mode |= FALLOC_FL_KEEP_SIZE; break;