diff --git a/caching/cache_restore.cc b/caching/cache_restore.cc index 22cc452..b672932 100644 --- a/caching/cache_restore.cc +++ b/caching/cache_restore.cc @@ -5,12 +5,14 @@ #include "caching/xml_format.h" #include "persistent-data/file_utils.h" +#include #include #include #include #include #include +using namespace boost; using namespace caching; using namespace persistent_data; using namespace std; @@ -18,14 +20,31 @@ using namespace std; //---------------------------------------------------------------- namespace { - int restore(string const &xml_file, string const &dev) { + struct flags { + flags() + : metadata_version(1), + override_metadata_version(false) { + } + + optional input; + optional output; + uint32_t metadata_version; + bool override_metadata_version; + }; + + int restore(flags const &fs) { try { - block_manager<>::ptr bm = open_bm(dev, block_io<>::READ_WRITE); + block_manager<>::ptr bm = open_bm(*fs.output, block_io<>::READ_WRITE); metadata::ptr md(new metadata(bm, metadata::CREATE)); emitter::ptr restorer = create_restore_emitter(md); - check_file_exists(xml_file); - ifstream in(xml_file.c_str(), ifstream::in); + if (fs.override_metadata_version) { + cerr << "overriding" << endl; + md->sb_.version = fs.metadata_version; + } + + check_file_exists(*fs.input); + ifstream in(fs.input->c_str(), ifstream::in); parse_xml(in, restorer); } catch (std::exception &e) { @@ -42,17 +61,21 @@ namespace { << " {-h|--help}" << endl << " {-i|--input} " << endl << " {-o|--output} " << endl - << " {-V|--version}" << endl; + << " {-V|--version}" << endl + << endl + << " {--debug-override-metadata-version} " << endl; + } } int main(int argc, char **argv) { int c; - string input, output; + flags fs; char const *prog_name = basename(argv[0]); char const *short_opts = "hi:o:V"; option const long_opts[] = { + { "debug-override-metadata-version", required_argument, NULL, 0 }, { "help", no_argument, NULL, 'h'}, { "input", required_argument, NULL, 'i' }, { "output", required_argument, NULL, 'o'}, @@ -62,16 +85,21 @@ int main(int argc, char **argv) while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) { switch(c) { + case 0: + fs.metadata_version = lexical_cast(optarg); + fs.override_metadata_version = true; + break; + case 'h': usage(cout, prog_name); return 0; case 'i': - input = optarg; + fs.input = optional(string(optarg)); break; case 'o': - output = optarg; + fs.output = optional(string(optarg)); break; case 'V': @@ -89,20 +117,19 @@ int main(int argc, char **argv) return 1; } - if (input.empty()) { + if (!fs.input) { cerr << "No input file provided." << endl << endl; usage(cerr, prog_name); return 1; } - if (output.empty()) { + if (!fs.output) { cerr << "No output file provided." << endl << endl; usage(cerr, prog_name); return 1; } - return restore(input, output); + return restore(fs); } //---------------------------------------------------------------- - diff --git a/caching/superblock.cc b/caching/superblock.cc index 30c733c..0d550e8 100644 --- a/caching/superblock.cc +++ b/caching/superblock.cc @@ -55,7 +55,7 @@ namespace { uint32_t const SUPERBLOCK_MAGIC = 06142003; uint32_t const VERSION_BEGIN = 1; - uint32_t const VERSION_END = 3; + uint32_t const VERSION_END = 2; } //---------------------------------------------------------------- diff --git a/features/cache_check.feature b/features/cache_check.feature index 5fbc371..74fb0ba 100644 --- a/features/cache_check.feature +++ b/features/cache_check.feature @@ -86,3 +86,9 @@ Feature: cache_check When I run `cache_check metadata.bin` Then it should pass + Scenario: Invalid metadata version causes a fail + Given a small xml file + And input file + And I run cache_restore with -i metadata.xml -o input --debug-override-metadata-version 12345 + When I run `cache_check input` + Then it should fail diff --git a/features/cache_restore.feature b/features/cache_restore.feature index 6b649cb..b9eef7d 100644 --- a/features/cache_restore.feature +++ b/features/cache_restore.feature @@ -9,7 +9,8 @@ Feature: thin_restore Scenario: print help (-h) When I run cache_restore with -h - Then it should pass with: + Then it should pass + And the output should contain exactly: """ Usage: cache_restore [options] @@ -18,11 +19,15 @@ Feature: thin_restore {-i|--input} {-o|--output} {-V|--version} + + {--debug-override-metadata-version} + """ Scenario: print help (--help) When I run cache_restore with -h - Then it should pass with: + Then it should pass + And the output should contain exactly: """ Usage: cache_restore [options] @@ -31,6 +36,9 @@ Feature: thin_restore {-i|--input} {-o|--output} {-V|--version} + + {--debug-override-metadata-version} + """ Scenario: missing input file @@ -53,9 +61,14 @@ Feature: thin_restore No output file provided. """ - @announce Scenario: successfully restores a valid xml file Given a small xml file And an empty dev file When I run cache_restore with -i metadata.xml -o metadata.bin Then it should pass + + Scenario: accepts --debug-override-metadata-version + Given a small xml file + And an empty dev file + When I run cache_restore with -i metadata.xml -o metadata.bin --debug-override-metadata-version 10298 + Then it should pass