[*_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:
@@ -41,35 +41,13 @@ using namespace thin_provisioning;
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
size_t get_file_length(string const &file) {
|
||||
struct stat info;
|
||||
int r;
|
||||
|
||||
r = ::stat(file.c_str(), &info);
|
||||
if (r)
|
||||
throw runtime_error("Couldn't stat backup path");
|
||||
|
||||
return info.st_size;
|
||||
}
|
||||
|
||||
progress_monitor::ptr create_monitor(bool quiet) {
|
||||
if (!quiet && isatty(fileno(stdout)))
|
||||
return create_progress_bar("Restoring");
|
||||
else
|
||||
return create_quiet_progress_monitor();
|
||||
}
|
||||
|
||||
int restore(string const &backup_file, string const &dev, bool quiet) {
|
||||
try {
|
||||
// The block size gets updated by the restorer.
|
||||
metadata::ptr md(new metadata(dev, metadata::CREATE, 128, 0));
|
||||
emitter::ptr restorer = create_restore_emitter(md);
|
||||
|
||||
check_file_exists(backup_file);
|
||||
ifstream in(backup_file.c_str(), ifstream::in);
|
||||
|
||||
progress_monitor::ptr monitor = create_monitor(quiet);
|
||||
parse_xml(in, restorer, get_file_length(backup_file), monitor);
|
||||
parse_xml(backup_file, restorer, quiet);
|
||||
|
||||
} catch (std::exception &e) {
|
||||
cerr << e.what() << endl;
|
||||
|
||||
@@ -217,35 +217,14 @@ tp::create_xml_emitter(ostream &out)
|
||||
}
|
||||
|
||||
void
|
||||
tp::parse_xml(std::istream &in, emitter::ptr e,
|
||||
size_t input_length, base::progress_monitor::ptr monitor)
|
||||
tp::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);
|
||||
|
||||
size_t total = 0;
|
||||
|
||||
while (!in.eof()) {
|
||||
char buffer[1024 * 1024];
|
||||
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());
|
||||
}
|
||||
|
||||
total += len;
|
||||
monitor->update_percent(total * 100 / input_length);
|
||||
}
|
||||
p.parse(backup_file, quiet);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
@@ -28,8 +28,7 @@
|
||||
|
||||
namespace thin_provisioning {
|
||||
emitter::ptr create_xml_emitter(std::ostream &out);
|
||||
void parse_xml(std::istream &in, emitter::ptr e,
|
||||
size_t input_length, base::progress_monitor::ptr p);
|
||||
void parse_xml(std::string const &backup_file, emitter::ptr e, bool quiet);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user