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

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