Blame SOURCES/dmpd-thin_repair-cache_repair-Check-input-file-exists-earlier.patch

819fdd
 base/file_utils.cc                          |  4 ++--
819fdd
 base/file_utils.h                           |  2 +-
819fdd
 caching/cache_repair.cc                     |  8 +++++++-
819fdd
 functional-tests/cache-functional-tests.scm | 32 ++++++++++++++++++++++++++---
819fdd
 functional-tests/era-functional-tests.scm   |  4 ++--
819fdd
 functional-tests/functional-tests.scm       |  4 ++--
819fdd
 functional-tests/thin-functional-tests.scm  | 27 ++++++++++++++++++++++--
819fdd
 thin-provisioning/thin_repair.cc            |  8 +++++++-
819fdd
 8 files changed, 75 insertions(+), 14 deletions(-)
819fdd
819fdd
diff --git a/base/file_utils.cc b/base/file_utils.cc
819fdd
index ba8957c..7883cfe 100644
819fdd
--- a/base/file_utils.cc
819fdd
+++ b/base/file_utils.cc
819fdd
@@ -66,13 +66,13 @@ file_utils::file_exists(string const &path) {
819fdd
 }
819fdd
 
819fdd
 void
819fdd
-file_utils::check_file_exists(string const &file) {
819fdd
+file_utils::check_file_exists(string const &file, bool must_be_regular_file) {
819fdd
 	struct stat info;
819fdd
 	int r = ::stat(file.c_str(), &info;;
819fdd
 	if (r)
819fdd
 		throw runtime_error("Couldn't stat file");
819fdd
 
819fdd
-	if (!S_ISREG(info.st_mode))
819fdd
+	if (must_be_regular_file && !S_ISREG(info.st_mode))
819fdd
 		throw runtime_error("Not a regular file");
819fdd
 }
819fdd
 
819fdd
diff --git a/base/file_utils.h b/base/file_utils.h
819fdd
index 2ee20ab..3edcc9e 100644
819fdd
--- a/base/file_utils.h
819fdd
+++ b/base/file_utils.h
819fdd
@@ -10,7 +10,7 @@
819fdd
 namespace file_utils {
819fdd
 	int open_file(std::string const &path, int flags);
819fdd
 	bool file_exists(std::string const &path);
819fdd
-	void check_file_exists(std::string const &file;;
819fdd
+	void check_file_exists(std::string const &file, bool must_be_regular_file = true);
819fdd
 	int create_block_file(std::string const &path, off_t file_size);
819fdd
 	int open_block_file(std::string const &path, off_t min_size, bool writeable, bool excl = true);
819fdd
 	uint64_t get_file_length(std::string const &file;;
819fdd
diff --git a/caching/cache_repair.cc b/caching/cache_repair.cc
819fdd
index 9587d5f..8a837a8 100644
819fdd
--- a/caching/cache_repair.cc
819fdd
+++ b/caching/cache_repair.cc
819fdd
@@ -2,6 +2,7 @@
819fdd
 #include <getopt.h>
819fdd
 #include <libgen.h>
819fdd
 
819fdd
+#include "base/file_utils.h"
819fdd
 #include "base/output_file_requirements.h"
819fdd
 #include "caching/commands.h"
819fdd
 #include "caching/metadata.h"
819fdd
@@ -29,12 +30,17 @@ namespace {
819fdd
 	}
819fdd
 
819fdd
 	int repair(string const &old_path, string const &new_path) {
819fdd
+		bool metadata_touched = false;
819fdd
 		try {
819fdd
+			file_utils::check_file_exists(new_path, false);
819fdd
+			metadata_touched = true;
819fdd
 			metadata_dump(open_metadata_for_read(old_path),
819fdd
 				      output_emitter(new_path),
819fdd
 				      true);
819fdd
 
819fdd
 		} catch (std::exception &e) {
819fdd
+			if (metadata_touched)
819fdd
+				file_utils::zero_superblock(new_path);
819fdd
 			cerr << e.what() << endl;
819fdd
 			return 1;
819fdd
 		}
819fdd
@@ -110,7 +116,7 @@ cache_repair_cmd::run(int argc, char **argv)
819fdd
 		check_output_file_requirements(*output_path);
819fdd
 
819fdd
 	else {
819fdd
-		cerr << "no output file provided" << endl;
819fdd
+		cerr << "No output file provided." << endl;
819fdd
 		usage(cerr);
819fdd
 		return 1;
819fdd
 	}
819fdd
diff --git a/functional-tests/cache-functional-tests.scm b/functional-tests/cache-functional-tests.scm
819fdd
index 0e70b94..9b3a203 100644
819fdd
--- a/functional-tests/cache-functional-tests.scm
819fdd
+++ b/functional-tests/cache-functional-tests.scm
819fdd
@@ -15,6 +15,7 @@
819fdd
   (define-tool cache-dump)
819fdd
   (define-tool cache-restore)
819fdd
   (define-tool cache-metadata-size)
819fdd
+  (define-tool cache-repair)
819fdd
 
819fdd
   (define-syntax with-cache-xml
819fdd
     (syntax-rules ()
819fdd
@@ -35,7 +36,8 @@
819fdd
     (syntax-rules ()
819fdd
       ((_ (md) b1 b2 ...)
819fdd
        (with-temp-file-sized ((md "cache.bin" (to-bytes (meg 4))))
819fdd
-         b1 b2 ...))))
819fdd
+         (system (fmt #f "dd if=/usr/bin/ls of=" md " bs=4096 > /dev/null 2>&1"))
819fdd
+           b1 b2 ...))))
819fdd
 
819fdd
   (define-syntax with-empty-metadata
819fdd
     (syntax-rules ()
819fdd
@@ -180,7 +182,7 @@
819fdd
     "the input file can't be found"
819fdd
     (with-empty-metadata (md)
819fdd
       (run-fail-rcv (_ stderr) (cache-restore "-i no-such-file -o" md)
819fdd
-        (assert-superblock-untouched md)
819fdd
+        (assert-superblock-all-zeroes md)
819fdd
         (assert-starts-with "Couldn't stat file" stderr))))
819fdd
 
819fdd
   (define-scenario (cache-restore garbage-input-file)
819fdd
@@ -188,7 +190,7 @@
819fdd
     (with-empty-metadata (md)
819fdd
       (with-temp-file-sized ((xml "cache.xml" 4096))
819fdd
         (run-fail-rcv (_ stderr) (cache-restore "-i" xml "-o" md)
819fdd
-          (assert-superblock-untouched md)))))
819fdd
+          (assert-superblock-all-zeroes md)))))
819fdd
 
819fdd
   (define-scenario (cache-restore missing-output-file)
819fdd
     "the output file can't be found"
819fdd
@@ -354,4 +356,28 @@
819fdd
     (run-ok-rcv (stdout stderr) (cache-metadata-size "--nr-blocks 67108864")
819fdd
       (assert-equal "3678208 sectors" stdout)
819fdd
       (assert-eof stderr)))
819fdd
+
819fdd
+  ;;;-----------------------------------------------------------
819fdd
+  ;;; cache_repair scenarios
819fdd
+  ;;;-----------------------------------------------------------
819fdd
+  (define-scenario (cache-repair missing-input-file)
819fdd
+    "the input file can't be found"
819fdd
+    (with-empty-metadata (md)
819fdd
+      (run-fail-rcv (_ stderr) (cache-repair "-i no-such-file -o" md)
819fdd
+        (assert-superblock-all-zeroes md)
819fdd
+        (assert-starts-with "Couldn't stat path" stderr))))
819fdd
+
819fdd
+  (define-scenario (cache-repair garbage-input-file)
819fdd
+    "the input file is just zeroes"
819fdd
+    (with-empty-metadata (md1)
819fdd
+      (with-corrupt-metadata (md2)
819fdd
+        (run-fail-rcv (_ stderr) (cache-repair "-i" md1 "-o" md2)
819fdd
+          (assert-superblock-all-zeroes md2)))))
819fdd
+
819fdd
+  (define-scenario (cache-repair missing-output-file)
819fdd
+    "the output file can't be found"
819fdd
+    (with-cache-xml (xml)
819fdd
+      (run-fail-rcv (_ stderr) (cache-repair "-i" xml)
819fdd
+        (assert-starts-with "No output file provided." stderr))))
819fdd
+
819fdd
 )
819fdd
diff --git a/functional-tests/era-functional-tests.scm b/functional-tests/era-functional-tests.scm
819fdd
index 373fe34..890f0ff 100644
819fdd
--- a/functional-tests/era-functional-tests.scm
819fdd
+++ b/functional-tests/era-functional-tests.scm
819fdd
@@ -153,7 +153,7 @@
819fdd
     "the input file can't be found"
819fdd
     (with-empty-metadata (md)
819fdd
       (run-fail-rcv (_ stderr) (era-restore "-i no-such-file -o" md)
819fdd
-        (assert-superblock-untouched md)
819fdd
+        (assert-superblock-all-zeroes md)
819fdd
         (assert-starts-with "Couldn't stat file" stderr))))
819fdd
 
819fdd
   (define-scenario (era-restore garbage-input-file)
819fdd
@@ -161,7 +161,7 @@
819fdd
     (with-empty-metadata (md)
819fdd
       (with-temp-file-sized ((xml "era.xml" 4096))
819fdd
         (run-fail-rcv (_ stderr) (era-restore "-i " xml "-o" md)
819fdd
-          (assert-superblock-untouched md)))))
819fdd
+          (assert-superblock-all-zeroes md)))))
819fdd
 
819fdd
   (define-scenario (era-restore output-unspecified)
819fdd
     "Fails if no metadata dev specified"
819fdd
diff --git a/functional-tests/functional-tests.scm b/functional-tests/functional-tests.scm
819fdd
index aa5b95c..758498e 100644
819fdd
--- a/functional-tests/functional-tests.scm
819fdd
+++ b/functional-tests/functional-tests.scm
819fdd
@@ -21,7 +21,7 @@
819fdd
     assert-eof
819fdd
     assert-starts-with
819fdd
     assert-matches
819fdd
-    assert-superblock-untouched
819fdd
+    assert-superblock-all-zeroes
819fdd
     assert-member?
819fdd
     assert-raises-thunk
819fdd
     assert-raises
819fdd
@@ -259,7 +259,7 @@
819fdd
               (loop (+ i 1))
819fdd
               #f)))))
819fdd
 
819fdd
-  (define (assert-superblock-untouched md)
819fdd
+  (define (assert-superblock-all-zeroes md)
819fdd
     (with-bcache (cache md 1)
819fdd
       (with-block (b cache 0 (get-flags))
819fdd
         (unless (all-zeroes? (block-data b) 4096)
819fdd
diff --git a/functional-tests/thin-functional-tests.scm b/functional-tests/thin-functional-tests.scm
819fdd
index d096b78..6bb7d24 100644
819fdd
--- a/functional-tests/thin-functional-tests.scm
819fdd
+++ b/functional-tests/thin-functional-tests.scm
819fdd
@@ -37,10 +37,12 @@
819fdd
            b1 b2 ...)))))
819fdd
 
819fdd
   ;;; It would be nice if the metadata was at least similar to valid data.
819fdd
+  ;;; Here I'm just using the start of the ls binary as 'random' data.
819fdd
   (define-syntax with-corrupt-metadata
819fdd
     (syntax-rules ()
819fdd
       ((_ (md) b1 b2 ...)
819fdd
        (with-temp-file-sized ((md "thin.bin" (meg 4)))
819fdd
+         (system (fmt #f "dd if=/usr/bin/ls of=" md " bs=4096 > /dev/null 2>&1"))
819fdd
          b1 b2 ...))))
819fdd
 
819fdd
   (define-syntax with-empty-metadata
819fdd
@@ -167,7 +169,7 @@
819fdd
     "the input file can't be found"
819fdd
     (with-empty-metadata (md)
819fdd
       (run-fail-rcv (_ stderr) (thin-restore "-i no-such-file -o" md)
819fdd
-        (assert-superblock-untouched md)
819fdd
+        (assert-superblock-all-zeroes md)
819fdd
         (assert-starts-with "Couldn't stat file" stderr))))
819fdd
 
819fdd
   (define-scenario (thin-restore garbage-input-file)
819fdd
@@ -175,7 +177,7 @@
819fdd
     (with-empty-metadata (md)
819fdd
       (with-temp-file-sized ((xml "thin.xml" 4096))
819fdd
         (run-fail-rcv (_ stderr) (thin-restore "-i " xml "-o" md)
819fdd
-          (assert-superblock-untouched md)))))
819fdd
+          (assert-superblock-all-zeroes md)))))
819fdd
 
819fdd
   (define-scenario (thin-restore missing-output-file)
819fdd
     "the output file can't be found"
819fdd
@@ -333,4 +335,25 @@
819fdd
       (with-empty-metadata (md)
819fdd
         (run-fail-rcv (_ stderr) (thin-repair "-i" xml "-o" md)
819fdd
           #t))))
819fdd
+
819fdd
+  (define-scenario (thin-repair missing-input-file)
819fdd
+    "the input file can't be found"
819fdd
+    (with-empty-metadata (md)
819fdd
+      (run-fail-rcv (_ stderr) (thin-repair "-i no-such-file -o" md)
819fdd
+        (assert-superblock-all-zeroes md)
819fdd
+        (assert-starts-with "Couldn't stat file" stderr))))
819fdd
+
819fdd
+  (define-scenario (thin-repair garbage-input-file)
819fdd
+    "the input file is just zeroes"
819fdd
+    (with-empty-metadata (md1)
819fdd
+      (with-corrupt-metadata (md2)
819fdd
+        (run-fail-rcv (_ stderr) (thin-repair "-i " md1 "-o" md2)
819fdd
+          (assert-superblock-all-zeroes md2)))))
819fdd
+
819fdd
+  (define-scenario (thin-repair missing-output-file)
819fdd
+    "the output file can't be found"
819fdd
+    (with-thin-xml (xml)
819fdd
+      (run-fail-rcv (_ stderr) (thin-repair "-i " xml)
819fdd
+        (assert-starts-with "No output file provided." stderr))))
819fdd
+
819fdd
   )
819fdd
diff --git a/thin-provisioning/thin_repair.cc b/thin-provisioning/thin_repair.cc
819fdd
index e22b193..0673753 100644
819fdd
--- a/thin-provisioning/thin_repair.cc
819fdd
+++ b/thin-provisioning/thin_repair.cc
819fdd
@@ -2,6 +2,7 @@
819fdd
 #include <getopt.h>
819fdd
 #include <libgen.h>
819fdd
 
819fdd
+#include "base/file_utils.h"
819fdd
 #include "base/output_file_requirements.h"
819fdd
 #include "persistent-data/file_utils.h"
819fdd
 #include "thin-provisioning/commands.h"
819fdd
@@ -17,15 +18,20 @@ using namespace thin_provisioning;
819fdd
 
819fdd
 namespace {
819fdd
 	int repair(string const &old_path, string const &new_path) {
819fdd
+		bool metadata_touched = false;
819fdd
 		try {
819fdd
 			// block size gets updated by the restorer
819fdd
 			block_manager<>::ptr new_bm = open_bm(new_path, block_manager<>::READ_WRITE);
819fdd
+			file_utils::check_file_exists(old_path, false);
819fdd
+			metadata_touched = true;
819fdd
 			metadata::ptr new_md(new metadata(new_bm, metadata::CREATE, 128, 0));
819fdd
 			emitter::ptr e = create_restore_emitter(new_md);
819fdd
 			block_manager<>::ptr old_bm = open_bm(old_path, block_manager<>::READ_ONLY);
819fdd
 			metadata_repair(old_bm, e);
819fdd
 
819fdd
 		} catch (std::exception &e) {
819fdd
+			if (metadata_touched)
819fdd
+				file_utils::zero_superblock(new_path);
819fdd
 			cerr << e.what() << endl;
819fdd
 			return 1;
819fdd
 		}
819fdd
@@ -101,7 +107,7 @@ thin_repair_cmd::run(int argc, char **argv)
819fdd
 		check_output_file_requirements(*output_path);
819fdd
 
819fdd
 	else {
819fdd
-		cerr << "no output file provided" << endl;
819fdd
+		cerr << "No output file provided." << endl;
819fdd
 		usage(cerr);
819fdd
 		return 1;
819fdd
 	}