[thin_restore] Add a progress monitor

This commit is contained in:
Joe Thornber
2014-08-05 11:47:57 +01:00
parent f534664f96
commit fccc1dfcb0
6 changed files with 134 additions and 4 deletions

View File

@@ -41,6 +41,24 @@ 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() {
if (isatty(fileno(stdout)))
return create_progress_bar("Restoring");
else
return create_quiet_progress_monitor();
}
int restore(string const &backup_file, string const &dev) {
try {
// The block size gets updated by the restorer.
@@ -49,7 +67,9 @@ namespace {
check_file_exists(backup_file);
ifstream in(backup_file.c_str(), ifstream::in);
parse_xml(in, restorer);
progress_monitor::ptr monitor = create_monitor();
parse_xml(in, restorer, get_file_length(backup_file), monitor);
} catch (std::exception &e) {
cerr << e.what() << endl;

View File

@@ -256,7 +256,8 @@ tp::create_xml_emitter(ostream &out)
}
void
tp::parse_xml(std::istream &in, emitter::ptr e)
tp::parse_xml(std::istream &in, emitter::ptr e,
size_t input_length, base::progress_monitor::ptr monitor)
{
XML_Parser parser = XML_ParserCreate(NULL);
if (!parser)
@@ -265,8 +266,10 @@ tp::parse_xml(std::istream &in, emitter::ptr e)
XML_SetUserData(parser, e.get());
XML_SetElementHandler(parser, start_tag, end_tag);
size_t total = 0;
while (!in.eof()) {
char buffer[4096];
char buffer[1024 * 1024];
in.read(buffer, sizeof(buffer));
size_t len = in.gcount();
int done = in.eof();
@@ -280,6 +283,9 @@ tp::parse_xml(std::istream &in, emitter::ptr e)
<< endl;
throw runtime_error(out.str());
}
total += len;
monitor->update_percent(total * 100 / input_length);
}
}

View File

@@ -20,6 +20,7 @@
#define XML_FORMAT_H
#include "emitter.h"
#include "base/progress_monitor.h"
#include <iosfwd>
@@ -27,7 +28,8 @@
namespace thin_provisioning {
emitter::ptr create_xml_emitter(std::ostream &out);
void parse_xml(std::istream &in, emitter::ptr e);
void parse_xml(std::istream &in, emitter::ptr e,
size_t input_length, base::progress_monitor::ptr p);
}
//----------------------------------------------------------------