[*_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

@@ -60,16 +60,16 @@ namespace {
//----------------------------------------------------------------
base::progress_monitor::ptr
std::auto_ptr<base::progress_monitor>
base::create_progress_bar(std::string const &title)
{
return progress_monitor::ptr(new progress_bar(title));
return auto_ptr<progress_monitor>(new progress_bar(title));
}
base::progress_monitor::ptr
std::auto_ptr<base::progress_monitor>
base::create_quiet_progress_monitor()
{
return progress_monitor::ptr(new quiet_progress());
return auto_ptr<progress_monitor>(new quiet_progress());
}
//----------------------------------------------------------------

View File

@@ -2,6 +2,7 @@
#define BASE_PROGRESS_MONITOR_H
#include <boost/shared_ptr.hpp>
#include <memory>
#include <string>
//----------------------------------------------------------------
@@ -9,15 +10,13 @@
namespace base {
class progress_monitor {
public:
typedef boost::shared_ptr<progress_monitor> ptr;
virtual ~progress_monitor() {}
virtual void update_percent(unsigned) = 0;
};
progress_monitor::ptr create_progress_bar(std::string const &title);
progress_monitor::ptr create_quiet_progress_monitor();
std::auto_ptr<progress_monitor> create_progress_bar(std::string const &title);
std::auto_ptr<progress_monitor> create_quiet_progress_monitor();
}
//----------------------------------------------------------------

View File

@@ -1,5 +1,67 @@
#include "xml_utils.h"
#include "persistent-data/file_utils.h"
#include <fstream>
#include <iostream>
using namespace xml_utils;
//----------------------------------------------------------------
void
xml_parser::parse(std::string const &backup_file, bool quiet)
{
persistent_data::check_file_exists(backup_file);
ifstream in(backup_file.c_str(), ifstream::in);
std::auto_ptr<base::progress_monitor> monitor = create_monitor(quiet);
size_t total = 0;
size_t input_length = get_file_length(backup_file);
while (!in.eof()) {
char buffer[4096];
in.read(buffer, sizeof(buffer));
size_t len = in.gcount();
int done = in.eof();
if (!XML_Parse(parser_, buffer, len, done)) {
ostringstream out;
out << "Parse error at line "
<< XML_GetCurrentLineNumber(parser_)
<< ":\n"
<< XML_ErrorString(XML_GetErrorCode(parser_))
<< endl;
throw runtime_error(out.str());
}
total += len;
monitor->update_percent(total * 100 / input_length);
}
}
size_t
xml_parser::get_file_length(string const &file) const
{
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<base::progress_monitor>
xml_parser::create_monitor(bool quiet)
{
if (!quiet && isatty(fileno(stdout)))
return base::create_progress_bar("Restoring");
else
return base::create_quiet_progress_monitor();
}
//----------------------------------------------------------------
void
@@ -21,5 +83,4 @@ xml_utils::build_attributes(attributes &a, char const **attr)
}
}
//----------------------------------------------------------------

View File

@@ -1,9 +1,11 @@
#ifndef BASE_XML_UTILS_H
#define BASE_XML_UTILS_H
#include <base/progress_monitor.h>
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
#include <expat.h>
#include <iosfwd>
#include <map>
using namespace std;
@@ -30,7 +32,12 @@ namespace xml_utils {
return parser_;
}
void parse(std::string const &backup_file, bool quiet);
private:
size_t get_file_length(string const &file) const;
auto_ptr<base::progress_monitor> create_monitor(bool quiet);
XML_Parser parser_;
};