2014-08-07 20:13:01 +05:30
|
|
|
#include "version.h"
|
|
|
|
|
2017-09-28 19:09:24 +05:30
|
|
|
#include "base/file_utils.h"
|
2016-07-11 19:23:03 +05:30
|
|
|
#include "base/output_file_requirements.h"
|
2014-08-27 18:31:31 +05:30
|
|
|
#include "era/commands.h"
|
2014-08-07 20:13:01 +05:30
|
|
|
#include "era/metadata.h"
|
|
|
|
#include "era/restore_emitter.h"
|
|
|
|
#include "era/xml_format.h"
|
|
|
|
#include "persistent-data/file_utils.h"
|
|
|
|
|
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <fstream>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace boost;
|
2014-08-12 14:50:29 +05:30
|
|
|
using namespace era;
|
2014-08-07 20:13:01 +05:30
|
|
|
using namespace persistent_data;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct flags {
|
2014-08-26 17:35:21 +05:30
|
|
|
flags()
|
|
|
|
: quiet(false) {
|
|
|
|
}
|
|
|
|
|
2014-08-07 20:13:01 +05:30
|
|
|
optional<string> input;
|
|
|
|
optional<string> output;
|
2014-08-26 17:35:21 +05:30
|
|
|
bool quiet;
|
2014-08-07 20:13:01 +05:30
|
|
|
};
|
|
|
|
|
2014-08-26 17:35:21 +05:30
|
|
|
int restore(flags const &fs, bool quiet) {
|
2017-09-28 19:09:24 +05:30
|
|
|
bool metadata_touched = false;
|
2014-08-07 20:13:01 +05:30
|
|
|
try {
|
2020-04-30 19:00:01 +05:30
|
|
|
block_manager::ptr bm = open_bm(*fs.output, block_manager::READ_WRITE);
|
2021-10-01 22:56:29 +05:30
|
|
|
file_utils::check_file_exists(*fs.input);
|
2017-09-28 19:09:24 +05:30
|
|
|
metadata_touched = true;
|
2014-08-07 20:13:01 +05:30
|
|
|
metadata::ptr md(new metadata(bm, metadata::CREATE));
|
2014-08-12 14:50:29 +05:30
|
|
|
emitter::ptr restorer = create_restore_emitter(*md);
|
2014-08-07 20:13:01 +05:30
|
|
|
|
2014-08-26 17:35:21 +05:30
|
|
|
parse_xml(*fs.input, restorer, fs.quiet);
|
2014-08-07 20:13:01 +05:30
|
|
|
|
|
|
|
} catch (std::exception &e) {
|
|
|
|
cerr << e.what() << endl;
|
2017-10-05 16:19:01 +05:30
|
|
|
if (metadata_touched)
|
|
|
|
try {
|
|
|
|
file_utils::zero_superblock(*fs.output);
|
|
|
|
} catch (...) {
|
|
|
|
}
|
2014-08-07 20:13:01 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-08 18:21:52 +05:30
|
|
|
}
|
2014-08-07 20:13:01 +05:30
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
era_restore_cmd::era_restore_cmd()
|
|
|
|
: command("era_restore")
|
|
|
|
{
|
2014-08-07 20:13:01 +05:30
|
|
|
}
|
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
void
|
|
|
|
era_restore_cmd::usage(std::ostream &out) const
|
|
|
|
{
|
|
|
|
out << "Usage: " << get_name() << " [options]" << endl
|
|
|
|
<< "Options:" << endl
|
|
|
|
<< " {-h|--help}" << endl
|
|
|
|
<< " {-i|--input} <input xml file>" << endl
|
|
|
|
<< " {-o|--output} <output device or file>" << endl
|
|
|
|
<< " {-q|--quiet}" << endl
|
|
|
|
<< " {-V|--version}" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
era_restore_cmd::run(int argc, char **argv)
|
2014-08-07 20:13:01 +05:30
|
|
|
{
|
|
|
|
int c;
|
|
|
|
flags fs;
|
2014-08-26 17:35:21 +05:30
|
|
|
char const *short_opts = "hi:o:qV";
|
2014-08-07 20:13:01 +05:30
|
|
|
option const long_opts[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h'},
|
|
|
|
{ "input", required_argument, NULL, 'i' },
|
|
|
|
{ "output", required_argument, NULL, 'o'},
|
2014-08-26 17:35:21 +05:30
|
|
|
{ "quiet", no_argument, NULL, 'q'},
|
2014-08-07 20:13:01 +05:30
|
|
|
{ "version", no_argument, NULL, 'V'},
|
|
|
|
{ NULL, no_argument, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
|
|
|
|
switch(c) {
|
|
|
|
case 'h':
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cout);
|
2014-08-07 20:13:01 +05:30
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
fs.input = optional<string>(string(optarg));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
fs.output = optional<string>(string(optarg));
|
|
|
|
break;
|
|
|
|
|
2014-08-26 17:35:21 +05:30
|
|
|
case 'q':
|
|
|
|
fs.quiet = true;
|
|
|
|
break;
|
|
|
|
|
2014-08-07 20:13:01 +05:30
|
|
|
case 'V':
|
|
|
|
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2014-08-07 20:13:01 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc != optind) {
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2014-08-07 20:13:01 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.input) {
|
|
|
|
cerr << "No input file provided." << endl << endl;
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2014-08-07 20:13:01 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:23:03 +05:30
|
|
|
if (fs.output)
|
|
|
|
check_output_file_requirements(*fs.output);
|
|
|
|
|
|
|
|
else {
|
2014-08-07 20:13:01 +05:30
|
|
|
cerr << "No output file provided." << endl << endl;
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2014-08-07 20:13:01 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-26 17:35:21 +05:30
|
|
|
return restore(fs, fs.quiet);
|
2014-08-07 20:13:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|