From patchwork Thu May 4 17:31:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Sandeen X-Patchwork-Id: 9712383 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 9380360387 for ; Thu, 4 May 2017 17:32:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7F6BA2766D for ; Thu, 4 May 2017 17:32:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7392228355; Thu, 4 May 2017 17:32:06 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4503E28557 for ; Thu, 4 May 2017 17:32:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755220AbdEDRcA (ORCPT ); Thu, 4 May 2017 13:32:00 -0400 Received: from sandeen.net ([63.231.237.45]:47484 "EHLO sandeen.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756061AbdEDRb4 (ORCPT ); Thu, 4 May 2017 13:31:56 -0400 Received: from [10.0.0.4] (liberator [10.0.0.4]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by sandeen.net (Postfix) with ESMTPSA id EE9A07AAB9A; Thu, 4 May 2017 12:31:54 -0500 (CDT) Subject: [PATCH V2] punch-alternating: add some options To: Eric Sandeen , fstests References: From: Eric Sandeen Message-ID: <1a55a8a0-00dd-bb73-c888-5c1a889c7302@sandeen.net> Date: Thu, 4 May 2017 12:31:54 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Sender: fstests-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: fstests@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- V2: don't allow interval of zero, or extra arguments other than the filename after the parsed options. -- To unsubscribe from this list: send the line "unsubscribe fstests" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html 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 #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; }