[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] dd: add punchhole feature
From: |
maxime . deroucy |
Subject: |
[PATCH] dd: add punchhole feature |
Date: |
Fri, 3 Feb 2017 13:58:37 +0100 |
From: Maxime de Roucy <address@hidden>
* src/dd.c: add conv=punchhole feature which deallocate space/data from
input file after each read operation.
* doc/coreutils.texi: document the new punchhole feature
* test/dd/punchhole.sh: dumb/minor test for the new punchhole feature
---
doc/coreutils.texi | 7 +++++++
src/dd.c | 24 ++++++++++++++++++++++--
tests/dd/punchhole.sh | 31 +++++++++++++++++++++++++++++++
tests/local.mk | 1 +
4 files changed, 61 insertions(+), 2 deletions(-)
create mode 100755 tests/dd/punchhole.sh
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 629136b01..1a3b9d964 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -8700,6 +8700,13 @@ write of output data.
Synchronize output data and metadata just before finishing. This
forces a physical write of output data and metadata.
+@item punchhole
+@opindex punchhole
+@cindex data read from input, deallocating
+Deallocate data read from @samp{ibs} after each read operation.
+Modify @samp{ibs}.
+See @command{fallocate} man page (option @option{--punch-hole}) for details.
+
@end table
@item iflag=@var{flag}[,@var{flag}]@dots{}
diff --git a/src/dd.c b/src/dd.c
index 3638a0a28..97b1cbcb2 100644
--- a/src/dd.c
+++ b/src/dd.c
@@ -129,7 +129,8 @@ enum
C_FDATASYNC = 040000,
C_FSYNC = 0100000,
- C_SPARSE = 0200000
+ C_SPARSE = 0200000,
+ C_PUNCHHOLE = 0400000
};
/* Status levels. */
@@ -298,6 +299,7 @@ static struct symbol_value const conversions[] =
{"sync", C_SYNC}, /* Pad input records to ibs with NULs. */
{"fdatasync", C_FDATASYNC}, /* Synchronize output data before finishing. */
{"fsync", C_FSYNC}, /* Also synchronize output metadata. */
+ {"punchhole", C_PUNCHHOLE}, /* Deallocate data from input after each read.
*/
{"", 0}
};
@@ -608,6 +610,7 @@ Each CONV symbol may be:\n\
noerror continue after read errors\n\
fdatasync physically write output file data before finishing\n\
fsync likewise, but also write metadata\n\
+ punchhole deallocate data from input after each read\n\
"), stdout);
fputs (_("\
\n\
@@ -2147,6 +2150,18 @@ dd_copy (void)
else
nread = iread_fnc (STDIN_FILENO, ibuf, input_blocksize);
+ if (nread > 0 && (conversions_mask & C_PUNCHHOLE))
+ {
+ int fallocate_retrun = fallocate (STDIN_FILENO,
+ FALLOC_FL_PUNCH_HOLE |
+ FALLOC_FL_KEEP_SIZE,
+ input_offset, nread);
+
+ if (fallocate_retrun != 0 && status_level != STATUS_NONE)
+ error (0, errno, _("error punching hole in %s, offset %zu, length
%ld"),
+ quoteaf (input_file), input_offset, nread);
+ }
+
if (nread >= 0 && i_nocache)
invalidate_cache (STDIN_FILENO, nread);
@@ -2379,7 +2394,12 @@ main (int argc, char **argv)
}
else
{
- if (ifd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
+ int input_main_flag = O_RDONLY;
+ if (conversions_mask & C_PUNCHHOLE)
+ input_main_flag = O_RDWR;
+
+ if (ifd_reopen (STDIN_FILENO, input_file,
+ input_main_flag | input_flags, 0) < 0)
die (EXIT_FAILURE, errno, _("failed to open %s"),
quoteaf (input_file));
}
diff --git a/tests/dd/punchhole.sh b/tests/dd/punchhole.sh
new file mode 100755
index 000000000..3a3ef3cc3
--- /dev/null
+++ b/tests/dd/punchhole.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+# Copyright (C) 2012-2017 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ dd
+is_local_dir_ . || very_expensive_
+require_sparse_support_
+
+printf 'abcd' > file.in
+cp file.in exp_out
+printf '\000\000\000\000' > exp_in
+dd if=file.in bs=1 conv=punchhole > out
+compare exp_in file.in || fail=1
+compare exp_out out || fail=1
+
+rm file.in exp_out exp_in
+Exit $fail
diff --git a/tests/local.mk b/tests/local.mk
index 30d6d7c87..0f97e4519 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -504,6 +504,7 @@ all_tests = \
tests/dd/no-allocate.sh \
tests/dd/nocache.sh \
tests/dd/not-rewound.sh \
+ tests/dd/punchhole.sh \
tests/dd/reblock.sh \
tests/dd/skip-seek.pl \
tests/dd/skip-seek2.sh \
--
2.11.0