[*_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:
@@ -20,11 +20,30 @@ using namespace std;
|
||||
//----------------------------------------------------------------
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
auto_ptr<progress_monitor> create_monitor(bool quiet) {
|
||||
if (!quiet && isatty(fileno(stdout)))
|
||||
return create_progress_bar("Restoring");
|
||||
else
|
||||
return create_quiet_progress_monitor();
|
||||
}
|
||||
|
||||
struct flags {
|
||||
flags()
|
||||
: metadata_version(1),
|
||||
override_metadata_version(false),
|
||||
clean_shutdown(true) {
|
||||
clean_shutdown(true),
|
||||
quiet(false) {
|
||||
}
|
||||
|
||||
optional<string> input;
|
||||
@@ -33,6 +52,7 @@ namespace {
|
||||
uint32_t metadata_version;
|
||||
bool override_metadata_version;
|
||||
bool clean_shutdown;
|
||||
bool quiet;
|
||||
};
|
||||
|
||||
int restore(flags const &fs) {
|
||||
@@ -48,7 +68,9 @@ namespace {
|
||||
|
||||
check_file_exists(*fs.input);
|
||||
ifstream in(fs.input->c_str(), ifstream::in);
|
||||
parse_xml(in, restorer);
|
||||
|
||||
auto_ptr<progress_monitor> monitor = create_monitor(fs.quiet);
|
||||
parse_xml(in, restorer, get_file_length(*fs.input), *monitor);
|
||||
|
||||
} catch (std::exception &e) {
|
||||
cerr << e.what() << endl;
|
||||
@@ -64,6 +86,7 @@ namespace {
|
||||
<< " {-h|--help}" << endl
|
||||
<< " {-i|--input} <input xml file>" << endl
|
||||
<< " {-o|--output} <output device or file>" << endl
|
||||
<< " {-q|--quiet}" << endl
|
||||
<< " {-V|--version}" << endl
|
||||
<< endl
|
||||
<< " {--debug-override-metadata-version} <integer>" << endl
|
||||
@@ -77,13 +100,14 @@ 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[] = {
|
||||
{ "debug-override-metadata-version", required_argument, NULL, 0 },
|
||||
{ "omit-clean-shutdown", no_argument, NULL, 1 },
|
||||
{ "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 }
|
||||
};
|
||||
@@ -111,6 +135,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;
|
||||
|
||||
@@ -236,13 +236,16 @@ caching::create_xml_emitter(ostream &out)
|
||||
}
|
||||
|
||||
void
|
||||
caching::parse_xml(istream &in, emitter::ptr e)
|
||||
caching::parse_xml(istream &in, emitter::ptr e,
|
||||
size_t input_length, base::progress_monitor &monitor)
|
||||
{
|
||||
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[4096];
|
||||
in.read(buffer, sizeof(buffer));
|
||||
@@ -258,6 +261,9 @@ caching::parse_xml(istream &in, emitter::ptr e)
|
||||
<< endl;
|
||||
throw runtime_error(out.str());
|
||||
}
|
||||
|
||||
total += len;
|
||||
monitor.update_percent(total * 100 / input_length);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef CACHE_XML_FORMAT_H
|
||||
#define CACHE_XML_FORMAT_H
|
||||
|
||||
#include "base/progress_monitor.h"
|
||||
#include "emitter.h"
|
||||
|
||||
#include <iosfwd>
|
||||
@@ -9,7 +10,8 @@
|
||||
|
||||
namespace caching {
|
||||
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_len, base::progress_monitor &monitor);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user