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

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