[*_restore] Add progress bar to cache_restore and era_restore.

A lot of refactoring common code between the restore tools.
This commit is contained in:
Joe Thornber
2014-08-26 13:05:21 +01:00
parent e31ffe0874
commit 828f654800
18 changed files with 199 additions and 92 deletions

View File

@@ -21,19 +21,22 @@ using namespace std;
namespace {
struct flags {
flags()
: quiet(false) {
}
optional<string> input;
optional<string> output;
bool quiet;
};
int restore(flags const &fs) {
int restore(flags const &fs, bool quiet) {
try {
block_manager<>::ptr bm = open_bm(*fs.output, block_manager<>::READ_WRITE);
metadata::ptr md(new metadata(bm, metadata::CREATE));
emitter::ptr restorer = create_restore_emitter(*md);
check_file_exists(*fs.input);
ifstream in(fs.input->c_str(), ifstream::in);
parse_xml(in, restorer);
parse_xml(*fs.input, restorer, fs.quiet);
} catch (std::exception &e) {
cerr << e.what() << endl;
@@ -49,6 +52,7 @@ namespace {
<< " {-h|--help}" << endl
<< " {-i|--input} <input xml file>" << endl
<< " {-o|--output} <output device or file>" << endl
<< " {-q|--quiet}" << endl
<< " {-V|--version}" << endl;
}
}
@@ -58,11 +62,12 @@ int main(int argc, char **argv)
int c;
flags fs;
char const *prog_name = basename(argv[0]);
char const *short_opts = "hi:o:V";
char const *short_opts = "hi:o:qV";
option const long_opts[] = {
{ "help", no_argument, NULL, 'h'},
{ "input", required_argument, NULL, 'i' },
{ "output", required_argument, NULL, 'o'},
{ "quiet", no_argument, NULL, 'q'},
{ "version", no_argument, NULL, 'V'},
{ NULL, no_argument, NULL, 0 }
};
@@ -81,6 +86,10 @@ int main(int argc, char **argv)
fs.output = optional<string>(string(optarg));
break;
case 'q':
fs.quiet = true;
break;
case 'V':
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
return 0;
@@ -108,7 +117,7 @@ int main(int argc, char **argv)
return 1;
}
return restore(fs);
return restore(fs, fs.quiet);
}
//----------------------------------------------------------------

View File

@@ -159,29 +159,14 @@ era::create_xml_emitter(std::ostream &out)
}
void
era::parse_xml(std::istream &in, emitter::ptr e)
era::parse_xml(std::string const &backup_file, emitter::ptr e, bool quiet)
{
xml_parser p;
XML_SetUserData(p.get_parser(), e.get());
XML_SetElementHandler(p.get_parser(), start_tag, end_tag);
while (!in.eof()) {
char buffer[4096];
in.read(buffer, sizeof(buffer));
size_t len = in.gcount();
int done = in.eof();
if (!XML_Parse(p.get_parser(), buffer, len, done)) {
ostringstream out;
out << "Parse error at line "
<< XML_GetCurrentLineNumber(p.get_parser())
<< ":\n"
<< XML_ErrorString(XML_GetErrorCode(p.get_parser()))
<< endl;
throw runtime_error(out.str());
}
}
p.parse(backup_file, quiet);
}
//----------------------------------------------------------------

View File

@@ -1,7 +1,8 @@
#ifndef ERA_XML_FORMAT_H
#define ERA_XML_FORMAT_H
#include "emitter.h"
#include "base/progress_monitor.h"
#include "era/emitter.h"
#include <iosfwd>
@@ -9,7 +10,7 @@
namespace era {
emitter::ptr create_xml_emitter(std::ostream &out);
void parse_xml(std::istream &in, emitter::ptr e);
void parse_xml(std::string const &backup_file, emitter::ptr e, bool quiet);
}
//----------------------------------------------------------------