2013-08-16 20:59:41 +05:30
|
|
|
#include "version.h"
|
|
|
|
|
2017-04-29 23:21:52 +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 "caching/commands.h"
|
2013-09-11 16:10:46 +05:30
|
|
|
#include "caching/metadata.h"
|
2013-09-12 17:03:32 +05:30
|
|
|
#include "caching/restore_emitter.h"
|
2013-09-11 16:10:46 +05:30
|
|
|
#include "caching/xml_format.h"
|
|
|
|
#include "persistent-data/file_utils.h"
|
|
|
|
|
2013-10-15 14:54:12 +05:30
|
|
|
#include <boost/lexical_cast.hpp>
|
2013-09-11 16:10:46 +05:30
|
|
|
#include <fstream>
|
2013-08-16 20:59:41 +05:30
|
|
|
#include <getopt.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <string>
|
|
|
|
|
2013-10-15 14:54:12 +05:30
|
|
|
using namespace boost;
|
2013-09-11 16:10:46 +05:30
|
|
|
using namespace caching;
|
|
|
|
using namespace persistent_data;
|
2017-04-29 23:21:52 +05:30
|
|
|
using namespace file_utils;
|
2013-08-16 20:59:41 +05:30
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2016-02-04 14:32:42 +05:30
|
|
|
unique_ptr<progress_monitor> create_monitor(bool quiet) {
|
2014-08-26 17:35:21 +05:30
|
|
|
if (!quiet && isatty(fileno(stdout)))
|
|
|
|
return create_progress_bar("Restoring");
|
|
|
|
else
|
|
|
|
return create_quiet_progress_monitor();
|
|
|
|
}
|
|
|
|
|
2013-10-15 14:54:12 +05:30
|
|
|
struct flags {
|
|
|
|
flags()
|
|
|
|
: metadata_version(1),
|
2014-08-26 17:35:21 +05:30
|
|
|
clean_shutdown(true),
|
2016-09-30 20:51:20 +05:30
|
|
|
quiet(false),
|
|
|
|
override_metadata_version(false),
|
|
|
|
override_version(1) {
|
2013-10-15 14:54:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
optional<string> input;
|
|
|
|
optional<string> output;
|
2013-10-29 18:16:23 +05:30
|
|
|
|
2013-10-15 14:54:12 +05:30
|
|
|
uint32_t metadata_version;
|
2013-10-29 18:16:23 +05:30
|
|
|
bool clean_shutdown;
|
2014-08-26 17:35:21 +05:30
|
|
|
bool quiet;
|
2016-09-30 20:51:20 +05:30
|
|
|
|
|
|
|
bool override_metadata_version;
|
|
|
|
unsigned override_version;
|
2013-10-15 14:54:12 +05:30
|
|
|
};
|
|
|
|
|
2017-03-01 21:06:34 +05:30
|
|
|
void override_version(metadata::ptr md, flags const &fs) {
|
|
|
|
md->sb_.version = fs.override_version;
|
|
|
|
md->commit(fs.clean_shutdown);
|
|
|
|
}
|
|
|
|
|
2013-10-15 14:54:12 +05:30
|
|
|
int restore(flags const &fs) {
|
2017-09-28 19:09:24 +05:30
|
|
|
bool metadata_touched = false;
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
try {
|
2020-04-30 19:00:01 +05:30
|
|
|
block_manager::ptr bm = open_bm(*fs.output, block_manager::READ_WRITE);
|
2017-09-28 19:09:24 +05:30
|
|
|
|
|
|
|
check_file_exists(*fs.input);
|
|
|
|
ifstream in(fs.input->c_str(), ifstream::in);
|
|
|
|
|
|
|
|
metadata_touched = true;
|
2013-09-11 16:10:46 +05:30
|
|
|
metadata::ptr md(new metadata(bm, metadata::CREATE));
|
2017-08-29 17:03:32 +05:30
|
|
|
emitter::ptr restorer = create_restore_emitter(md,
|
|
|
|
fs.metadata_version,
|
|
|
|
fs.clean_shutdown ? CLEAN_SHUTDOWN : NO_CLEAN_SHUTDOWN);
|
2013-09-11 16:10:46 +05:30
|
|
|
|
2016-02-04 14:32:42 +05:30
|
|
|
unique_ptr<progress_monitor> monitor = create_monitor(fs.quiet);
|
2014-08-26 17:35:21 +05:30
|
|
|
parse_xml(in, restorer, get_file_length(*fs.input), *monitor);
|
2013-09-11 16:10:46 +05:30
|
|
|
|
2017-03-01 21:06:34 +05:30
|
|
|
if (fs.override_metadata_version)
|
|
|
|
override_version(md, fs);
|
|
|
|
|
2013-09-11 16:10:46 +05:30
|
|
|
} catch (std::exception &e) {
|
2017-09-28 19:09:24 +05:30
|
|
|
if (metadata_touched)
|
|
|
|
zero_superblock(*fs.output);
|
2013-09-11 16:10:46 +05:30
|
|
|
cerr << e.what() << endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-08 18:21:52 +05:30
|
|
|
}
|
2013-09-11 16:10:46 +05:30
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
//----------------------------------------------------------------
|
2013-10-15 14:54:12 +05:30
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
cache_restore_cmd::cache_restore_cmd()
|
|
|
|
: command("cache_restore")
|
|
|
|
{
|
2013-08-16 20:59:41 +05:30
|
|
|
}
|
|
|
|
|
2016-01-08 18:21:52 +05:30
|
|
|
void
|
|
|
|
cache_restore_cmd::usage(std::ostream &out) const
|
|
|
|
{
|
|
|
|
out << "Usage: " << get_name() << " [options]" << endl
|
|
|
|
<< "Options:" << endl
|
2016-09-30 20:51:20 +05:30
|
|
|
<< " {-h|--help}\n"
|
|
|
|
<< " {-i|--input} <input xml file>\n"
|
|
|
|
<< " {-o|--output} <output device or file>\n"
|
|
|
|
<< " {-q|--quiet}\n"
|
|
|
|
<< " {--metadata-version} <1 or 2>\n"
|
|
|
|
<< " {-V|--version}\n"
|
|
|
|
<< "\n"
|
2016-01-08 18:21:52 +05:30
|
|
|
<< " {--debug-override-metadata-version} <integer>" << endl
|
|
|
|
<< " {--omit-clean-shutdown}" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cache_restore_cmd::run(int argc, char **argv)
|
2013-08-16 20:59:41 +05:30
|
|
|
{
|
|
|
|
int c;
|
2013-10-15 14:54:12 +05:30
|
|
|
flags fs;
|
2014-08-26 17:35:21 +05:30
|
|
|
char const *short_opts = "hi:o:qV";
|
2013-08-16 20:59:41 +05:30
|
|
|
option const long_opts[] = {
|
2013-10-15 14:54:12 +05:30
|
|
|
{ "debug-override-metadata-version", required_argument, NULL, 0 },
|
2013-10-29 18:16:23 +05:30
|
|
|
{ "omit-clean-shutdown", no_argument, NULL, 1 },
|
2016-09-30 20:51:20 +05:30
|
|
|
{ "metadata-version", required_argument, NULL, 2 },
|
2013-08-16 20:59:41 +05:30
|
|
|
{ "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'},
|
2013-08-16 20:59:41 +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) {
|
2013-10-15 14:54:12 +05:30
|
|
|
case 0:
|
|
|
|
fs.override_metadata_version = true;
|
2016-09-30 20:51:20 +05:30
|
|
|
fs.override_version = lexical_cast<uint32_t>(optarg);
|
2013-10-15 14:54:12 +05:30
|
|
|
break;
|
|
|
|
|
2013-10-29 18:16:23 +05:30
|
|
|
case 1:
|
|
|
|
fs.clean_shutdown = false;
|
|
|
|
break;
|
|
|
|
|
2016-09-30 20:51:20 +05:30
|
|
|
case 2:
|
|
|
|
fs.metadata_version = lexical_cast<uint32_t>(optarg);
|
|
|
|
if ((fs.metadata_version < MIN_METADATA_VERSION) ||
|
|
|
|
(fs.metadata_version > MAX_METADATA_VERSION)) {
|
|
|
|
cerr << "Bad metadata version\n\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-08-16 20:59:41 +05:30
|
|
|
case 'h':
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cout);
|
2013-08-16 20:59:41 +05:30
|
|
|
return 0;
|
|
|
|
|
|
|
|
case 'i':
|
2013-10-15 14:54:12 +05:30
|
|
|
fs.input = optional<string>(string(optarg));
|
2013-08-16 20:59:41 +05:30
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
2013-10-15 14:54:12 +05:30
|
|
|
fs.output = optional<string>(string(optarg));
|
2013-08-16 20:59:41 +05:30
|
|
|
break;
|
|
|
|
|
2014-08-26 17:35:21 +05:30
|
|
|
case 'q':
|
|
|
|
fs.quiet = true;
|
|
|
|
break;
|
|
|
|
|
2013-08-16 20:59:41 +05:30
|
|
|
case 'V':
|
|
|
|
cout << THIN_PROVISIONING_TOOLS_VERSION << endl;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2013-08-16 20:59:41 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc != optind) {
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2013-08-16 20:59:41 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-15 14:54:12 +05:30
|
|
|
if (!fs.input) {
|
2013-08-16 20:59:41 +05:30
|
|
|
cerr << "No input file provided." << endl << endl;
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2013-08-16 20:59:41 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-11 19:23:03 +05:30
|
|
|
if (fs.output)
|
|
|
|
check_output_file_requirements(*fs.output);
|
|
|
|
|
|
|
|
else {
|
2013-08-16 20:59:41 +05:30
|
|
|
cerr << "No output file provided." << endl << endl;
|
2016-01-08 18:21:52 +05:30
|
|
|
usage(cerr);
|
2013-08-16 20:59:41 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-15 14:54:12 +05:30
|
|
|
return restore(fs);
|
2013-08-16 20:59:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|