All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Sandeen <sandeen@sandeen.net>
To: Eric Sandeen <sandeen@redhat.com>, fstests <fstests@vger.kernel.org>
Subject: [PATCH V2] punch-alternating: add some options
Date: Thu, 4 May 2017 12:31:54 -0500	[thread overview]
Message-ID: <1a55a8a0-00dd-bb73-c888-5c1a889c7302@sandeen.net> (raw)
In-Reply-To: <ad311156-c99a-4269-051e-082b259bb0de@redhat.com>

I didn't end up using this, but somebody else might find
it useful, so sending it.

This change lets us specify punch patterns other than
literally every other block.

i.e. punch-alternating with no options will do:

...HDHDHDHDHDHD...

-i 4 -s 2 will do:

...DDHHDDHHDDHH...

or -i 3 -s 1 will do:

...DDHDDHDDHDDH...

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---

V2: don't allow interval of zero, or extra arguments
    other than the filename after the parsed options.

diff --git a/src/punch-alternating.c b/src/punch-alternating.c
index 4148622..3503ded 100644
--- a/src/punch-alternating.c
+++ b/src/punch-alternating.c
@@ -11,6 +11,14 @@
 #include <string.h>
 #include "global.h"
 
+void usage(char *cmd)
+{
+	printf("Usage: %s [-i interval] [-s size] file\n", cmd);
+	printf("Punches every other block in the file by default,\n");
+	printf("or 'size' blocks every 'interval' blocks with options.\n");
+	exit(1);
+}
+
 int main(int argc, char *argv[])
 {
 	struct stat	s;
@@ -21,14 +29,32 @@ int main(int argc, char *argv[])
 	off_t		sz;
 	int		mode;
 	int		error;
+	int		c;
+	int		size = 1;	/* punch $SIZE blocks ... */
+	int		interval = 2;	/* every $INTERVAL blocks */
 
-	if (argc != 2) {
-		printf("Usage: %s file\n", argv[0]);
-		printf("Punches every other block in the file.\n");
-		return 1;
+	while ((c = getopt(argc, argv, "i:s:")) != EOF) {
+		switch (c) {
+		case 'i':
+			interval = atoi(optarg);
+			break;
+		case 's':
+			size = atoi(optarg);
+			break;
+		default:
+			usage(argv[0]);
+		}
 	}
 
-	fd = open(argv[1], O_WRONLY);
+	if (!interval) {
+		printf("Please specify an interval > 0\n");
+		usage(argv[0]);
+	}
+
+	if (optind != argc - 1)
+		usage(argv[0]);
+
+	fd = open(argv[optind], O_WRONLY);
 	if (fd < 0)
 		goto err;
 
@@ -44,8 +70,8 @@ int main(int argc, char *argv[])
 	blksz = sf.f_bsize;
 
 	mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
-	for (offset = 0; offset < sz; offset += blksz * 2) {
-		error = fallocate(fd, mode, offset, blksz);
+	for (offset = 0; offset < sz; offset += blksz * interval) {
+		error = fallocate(fd, mode, offset, blksz * size);
 		if (error)
 			goto err;
 	}


  parent reply	other threads:[~2017-05-04 17:31 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-04 15:16 [PATCH] punch-alternating: add some options Eric Sandeen
2017-05-04 15:49 ` Darrick J. Wong
2017-05-04 17:13   ` Eric Sandeen
2017-05-04 17:31 ` Eric Sandeen [this message]
2017-05-04 18:25   ` [PATCH V3] " Eric Sandeen
2017-05-05  4:32     ` Eryu Guan
2017-05-05  5:21       ` Darrick J. Wong
2017-05-05 13:19       ` Eric Sandeen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1a55a8a0-00dd-bb73-c888-5c1a889c7302@sandeen.net \
    --to=sandeen@sandeen.net \
    --cc=fstests@vger.kernel.org \
    --cc=sandeen@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.