coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] dd: add punchhole feature


From: Maxime de Roucy
Subject: [PATCH] dd: add punchhole feature
Date: Mon, 13 Feb 2017 22:37:57 +0100

* src/dd.c: add conv=punchhole feature which deallocate space/data from
  input file after each write 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/copy.c            |  2 +-
 src/copy.h            |  2 ++
 src/dd.c              | 26 ++++++++++++++++++++++++--
 src/local.mk          |  3 +++
 tests/dd/punchhole.sh | 31 +++++++++++++++++++++++++++++++
 tests/local.mk        |  1 +
 7 files changed, 69 insertions(+), 3 deletions(-)
 create mode 100755 tests/dd/punchhole.sh

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 629136b01..1199733cf 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 write operation.
+Use with care, it will modify @samp{ibs} and you can loose data.
+See @command{fallocate} man page (option @option{--punch-hole}) for details.
+
 @end table
 
 @item iflag=@var{flag}[,@var{flag}]@dots{}
diff --git a/src/copy.c b/src/copy.c
index e3832c23a..fc24648f5 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -163,7 +163,7 @@ utimens_symlink (char const *file, struct timespec const 
*timespec)
    speculative preallocation on file systems such as XFS.
    Return values as per fallocate(2) except ENOSYS etc. are ignored.  */
 
-static int
+int
 punch_hole (int fd, off_t offset, off_t length)
 {
   int ret = 0;
diff --git a/src/copy.h b/src/copy.h
index f7047bccd..be43c300a 100644
--- a/src/copy.h
+++ b/src/copy.h
@@ -292,4 +292,6 @@ void cp_options_default (struct cp_options *);
 bool chown_failure_ok (struct cp_options const *) _GL_ATTRIBUTE_PURE;
 mode_t cached_umask (void);
 
+int punch_hole (int, off_t, off_t);
+
 #endif
diff --git a/src/dd.c b/src/dd.c
index 3638a0a28..f02ad6d7b 100644
--- a/src/dd.c
+++ b/src/dd.c
@@ -25,7 +25,9 @@
 #include <getopt.h>
 
 #include "system.h"
+#include "backupfile.h"
 #include "close-stream.h"
+#include "copy.h"
 #include "die.h"
 #include "error.h"
 #include "fd-reopen.h"
@@ -129,7 +131,8 @@ enum
     C_FDATASYNC = 040000,
     C_FSYNC = 0100000,
 
-    C_SPARSE = 0200000
+    C_SPARSE = 0200000,
+    C_PUNCHHOLE = 0400000
   };
 
 /* Status levels.  */
@@ -298,6 +301,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 write. 
 */
   {"", 0}
 };
 
@@ -608,6 +612,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 write (use with care)\n\
 "), stdout);
       fputs (_("\
 \n\
@@ -2227,6 +2232,18 @@ dd_copy (void)
             w_full++;
           else
             w_partial++;
+
+          if (conversions_mask & C_PUNCHHOLE)
+            {
+              int punch_hole_ret = punch_hole (STDIN_FILENO,
+                                               input_offset - n_bytes_read,
+                                               n_bytes_read);
+
+              if (punch_hole_ret != 0 && status_level != STATUS_NONE)
+                error (0, errno, _("error punching hole in %s, offset %zu, 
length %ld"),
+                       quoteaf (input_file), input_offset, nread);
+            }
+
           continue;
         }
 
@@ -2379,7 +2396,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/src/local.mk b/src/local.mk
index 5b25fcb87..6858088a6 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -218,6 +218,7 @@ src_vdir_LDADD = $(src_ls_LDADD)
 src_cp_LDADD += $(copy_ldadd)
 src_ginstall_LDADD += $(copy_ldadd)
 src_mv_LDADD += $(copy_ldadd)
+src_dd_LDADD += $(copy_ldadd)
 
 src_mv_LDADD += $(remove_ldadd)
 src_rm_LDADD += $(remove_ldadd)
@@ -367,6 +368,8 @@ src_timeout_SOURCES = src/timeout.c src/operand2sig.c
 src_mv_SOURCES = src/mv.c src/remove.c $(copy_sources) $(selinux_sources)
 src_rm_SOURCES = src/rm.c src/remove.c
 
+src_dd_SOURCES = src/dd.c $(copy_sources)
+
 src_mkdir_SOURCES = src/mkdir.c src/prog-fprintf.c $(selinux_sources)
 src_rmdir_SOURCES = src/rmdir.c src/prog-fprintf.c
 
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.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]