Merge branch '2016-07-07-improve-thin-repair-error-message' into v0.7-devel

Conflicts:
	Makefile.in
	VERSION
	configure.ac
	persistent-data/file_utils.cc
This commit is contained in:
Joe Thornber 2016-07-11 15:51:30 +01:00
commit c2fdbad322
17 changed files with 131 additions and 17 deletions

View File

@ -25,6 +25,7 @@ PROGRAMS=\
all: $(PROGRAMS) all: $(PROGRAMS)
SOURCE=\ SOURCE=\
base/output_file_requirements.cc \
base/application.cc \ base/application.cc \
base/base64.cc \ base/base64.cc \
base/disk_units.cc \ base/disk_units.cc \

View File

@ -61,8 +61,14 @@ application::run(int argc, char **argv)
std::list<command::ptr>::const_iterator it; std::list<command::ptr>::const_iterator it;
for (it = cmds_.begin(); it != cmds_.end(); ++it) { for (it = cmds_.begin(); it != cmds_.end(); ++it) {
if (cmd == (*it)->get_name()) if (cmd == (*it)->get_name()) {
return (*it)->run(argc, argv); try {
return (*it)->run(argc, argv);
} catch (std::exception const &e) {
cerr << e.what() << "\n";
return 1;
}
}
} }
std::cerr << "Unknown command '" << cmd << "'\n"; std::cerr << "Unknown command '" << cmd << "'\n";

View File

@ -0,0 +1,51 @@
#include "base/output_file_requirements.h"
#include <iostream>
#include <linux/fs.h>
#include <sstream>
#include <stdexcept>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
using namespace base;
using namespace std;
//----------------------------------------------------------------
namespace {
void explain_output_file_requirements() {
ostringstream out;
out << "The output file should either be a block device,\n"
<< "or an existing file. The file needs to be large\n"
<< "enough to hold the metadata.";
throw runtime_error(out.str());
}
unsigned const MIN_SIZE = 32 * 1024;
}
void
base::check_output_file_requirements(string const &path)
{
struct stat info;
int r = ::stat(path.c_str(), &info);
if (r) {
cerr << "Output file does not exist.\n\n";
explain_output_file_requirements();
}
if (!info.st_size) {
cerr << "Zero size output file.\n\n";
explain_output_file_requirements();
}
if (info.st_size < MIN_SIZE) {
cerr << "Output file too small.\n\n";
explain_output_file_requirements();
}
}
//----------------------------------------------------------------

View File

@ -0,0 +1,14 @@
#ifndef BASE_OUTPUT_FILE_REQUIREMENTS_H
#define BASE_OUTPUT_FILE_REQUIREMENTS_H
#include <string>
//----------------------------------------------------------------
namespace base {
void check_output_file_requirements(std::string const &path);
}
//----------------------------------------------------------------
#endif

View File

@ -2,6 +2,7 @@
#include <getopt.h> #include <getopt.h>
#include <libgen.h> #include <libgen.h>
#include "base/output_file_requirements.h"
#include "caching/commands.h" #include "caching/commands.h"
#include "caching/metadata.h" #include "caching/metadata.h"
#include "caching/metadata_dump.h" #include "caching/metadata_dump.h"
@ -105,7 +106,10 @@ cache_repair_cmd::run(int argc, char **argv)
return 1; return 1;
} }
if (!output_path) { if (output_path)
check_output_file_requirements(*output_path);
else {
cerr << "no output file provided" << endl; cerr << "no output file provided" << endl;
usage(cerr); usage(cerr);
return 1; return 1;

View File

@ -1,5 +1,6 @@
#include "version.h" #include "version.h"
#include "base/output_file_requirements.h"
#include "caching/commands.h" #include "caching/commands.h"
#include "caching/metadata.h" #include "caching/metadata.h"
#include "caching/restore_emitter.h" #include "caching/restore_emitter.h"
@ -169,7 +170,10 @@ cache_restore_cmd::run(int argc, char **argv)
return 1; return 1;
} }
if (!fs.output) { if (fs.output)
check_output_file_requirements(*fs.output);
else {
cerr << "No output file provided." << endl << endl; cerr << "No output file provided." << endl << endl;
usage(cerr); usage(cerr);
return 1; return 1;

View File

@ -1,5 +1,6 @@
#include "version.h" #include "version.h"
#include "base/output_file_requirements.h"
#include "era/commands.h" #include "era/commands.h"
#include "era/metadata.h" #include "era/metadata.h"
#include "era/restore_emitter.h" #include "era/restore_emitter.h"
@ -121,7 +122,10 @@ era_restore_cmd::run(int argc, char **argv)
return 1; return 1;
} }
if (!fs.output) { if (fs.output)
check_output_file_requirements(*fs.output);
else {
cerr << "No output file provided." << endl << endl; cerr << "No output file provided." << endl << endl;
usage(cerr); usage(cerr);
return 1; return 1;

View File

@ -24,6 +24,11 @@ Given(/^a corrupt superblock$/) do
end end
end end
Given(/^a tiny file$/) do
run_simple("rm -f tiny")
run_simple("fallocate -l 123 tiny")
end
When(/^I run thin_check with (.*?)$/) do |opts| When(/^I run thin_check with (.*?)$/) do |opts|
run_simple("thin_check #{opts} #{dev_file}", false) run_simple("thin_check #{opts} #{dev_file}", false)
end end

View File

@ -55,6 +55,18 @@ Feature: thin_restore
No output file provided. No output file provided.
""" """
Scenario: tiny output file
Given a tiny file
When I run thin_restore with -i metadata.xml -o tiny
Then it should fail with:
"""
Output file too small.
The output file should either be a block device,
or an existing file. The file needs to be large
enough to hold the metadata.
"""
Scenario: --quiet is accepted Scenario: --quiet is accepted
Given valid thin metadata Given valid thin metadata
When I run thin_restore with -i metadata.xml -o metadata.bin --quiet When I run thin_restore with -i metadata.xml -o metadata.bin --quiet

View File

@ -32,7 +32,8 @@ This tool cannot be run on live metadata.
Input file or device with binary metadata. Input file or device with binary metadata.
.IP "\fB\-o, \-\-output\fP \fI{device|file}\fP" .IP "\fB\-o, \-\-output\fP \fI{device|file}\fP"
Output file or device for repaired binary metadata. Output file or device for repaired binary metadata. If a file is used
then it must be preallocated, and large enough to hold the metadata.
.IP "\fB\-h, \-\-help\fP" .IP "\fB\-h, \-\-help\fP"
Print help and exit. Print help and exit.

View File

@ -30,7 +30,8 @@ This tool cannot be run on live metadata.
Input file or device with metadata. Input file or device with metadata.
.IP "\fB\-o, \-\-output\fP \fI{device|file}\fP" .IP "\fB\-o, \-\-output\fP \fI{device|file}\fP"
Output file or device. Output file or device for repaired binary metadata. If a file is used
then it must be preallocated, and large enough to hold the metadata.
.IP "\fB{\-\-debug-override-metadata-version}\fP \fI<integer>\fP" .IP "\fB{\-\-debug-override-metadata-version}\fP \fI<integer>\fP"
ONLY FOR DEBUGGING PURPOSES: ONLY FOR DEBUGGING PURPOSES:

View File

@ -32,7 +32,8 @@ This tool cannot be run on live metadata.
Input file or device with binary metadata. Input file or device with binary metadata.
.IP "\fB\-o, \-\-output\fP \fI{device|file}\fP" .IP "\fB\-o, \-\-output\fP \fI{device|file}\fP"
Output file or device for repaired binary metadata. Output file or device for repaired binary metadata. If a file is used
then it must be preallocated, and large enough to hold the metadata.
.IP "\fB\-h, \-\-help\fP" .IP "\fB\-h, \-\-help\fP"
Print help and exit. Print help and exit.

View File

@ -33,7 +33,8 @@ Suppress output messages, return only exit code.
Input file or device with metadata. Input file or device with metadata.
.IP "\fB\-o, \-\-output\fP \fI{device|file}\fP" .IP "\fB\-o, \-\-output\fP \fI{device|file}\fP"
Output file or device. Output file or device for repaired binary metadata. If a file is used
then it must be preallocated, and large enough to hold the metadata.
.IP "\fB\-h, \-\-help\fP" .IP "\fB\-h, \-\-help\fP"
Print help and exit. Print help and exit.

View File

@ -22,9 +22,12 @@ persistent_data::get_nr_blocks(string const &path, sector_t block_size)
block_address nr_blocks; block_address nr_blocks;
int r = ::stat(path.c_str(), &info); int r = ::stat(path.c_str(), &info);
if (r) if (r) {
throw runtime_error("Couldn't stat dev path " + path + ": " + ostringstream out;
strerror(errno)); out << "Couldn't stat dev path '" << path << "': "
<< strerror(errno);
throw runtime_error(out.str());
}
if (S_ISREG(info.st_mode) && info.st_size) if (S_ISREG(info.st_mode) && info.st_size)
nr_blocks = div_down<block_address>(info.st_size, block_size); nr_blocks = div_down<block_address>(info.st_size, block_size);

View File

@ -2,6 +2,7 @@
#include <getopt.h> #include <getopt.h>
#include <libgen.h> #include <libgen.h>
#include "base/output_file_requirements.h"
#include "persistent-data/file_utils.h" #include "persistent-data/file_utils.h"
#include "thin-provisioning/commands.h" #include "thin-provisioning/commands.h"
#include "human_readable_format.h" #include "human_readable_format.h"
@ -102,7 +103,10 @@ thin_repair_cmd::run(int argc, char **argv)
return 1; return 1;
} }
if (!output_path) { if (output_path)
check_output_file_requirements(*output_path);
else {
cerr << "no output file provided" << endl; cerr << "no output file provided" << endl;
usage(cerr); usage(cerr);
return 1; return 1;

View File

@ -16,6 +16,7 @@
// with thin-provisioning-tools. If not, see // with thin-provisioning-tools. If not, see
// <http://www.gnu.org/licenses/>. // <http://www.gnu.org/licenses/>.
#include "base/output_file_requirements.h"
#include "persistent-data/file_utils.h" #include "persistent-data/file_utils.h"
#include "thin-provisioning/commands.h" #include "thin-provisioning/commands.h"
#include "thin-provisioning/emitter.h" #include "thin-provisioning/emitter.h"
@ -138,7 +139,8 @@ thin_restore_cmd::run(int argc, char **argv)
cerr << "No output file provided." << endl << endl; cerr << "No output file provided." << endl << endl;
usage(cerr); usage(cerr);
return 1; return 1;
} } else
check_output_file_requirements(output);
return restore(input, output, quiet); return restore(input, output, quiet);
} }

View File

@ -78,9 +78,9 @@ TEST_OBJECTS=$(subst .cc,.gmo,$(TEST_SOURCE))
%.gmo: %.cc %.gmo: %.cc
@echo " [CXX] $<" @echo " [CXX] $<"
$(V) $(CXX) -c $(INCLUDES) $(GMOCK_INCLUDES) $(CXXFLAGS) $(GMOCK_FLAGS) -o $@ $< $(V) $(CXX) -c $(CPPFLAGS) $(GMOCK_INCLUDES) $(CXXFLAGS) $(GMOCK_FLAGS) -o $@ $<
@echo " [DEP] $<" @echo " [DEP] $<"
$(V) $(CXX) -MM -MT $(subst .cc,.o,$<) $(INCLUDES) $(GMOCK_INCLUDES) $(CXXFLAGS) $(GMOCK_FLAGS) $< > $*.$$$$; \ $(V) $(CXX) -MM -MT $(subst .cc,.o,$<) $(CPPFLAGS) $(GMOCK_INCLUDES) $(CXXFLAGS) $(GMOCK_FLAGS) $< > $*.$$$$; \
sed 's,\([^ :]*\)\.o[ :]*,\1.o \1.gmo $* : Makefile ,g' < $*.$$$$ > $*.d; \ sed 's,\([^ :]*\)\.o[ :]*,\1.o \1.gmo $* : Makefile ,g' < $*.$$$$ > $*.d; \
$(RM) $*.$$$$ $(RM) $*.$$$$
@ -88,7 +88,7 @@ unit-tests/unit_tests: $(TEST_OBJECTS) lib/libgmock.a lib/libpdata.a
@echo " [LD] $<" @echo " [LD] $<"
$(V)g++ $(CXXFLAGS) $(LDFLAGS) -o $@ $(TEST_OBJECTS) $(LIBS) $(GMOCK_LIBS) $(LIBEXPAT) $(V)g++ $(CXXFLAGS) $(LDFLAGS) -o $@ $(TEST_OBJECTS) $(LIBS) $(GMOCK_LIBS) $(LIBEXPAT)
.PHONEY: unit-test .PHONY: unit-test
unit-test: unit-tests/unit_tests unit-test: unit-tests/unit_tests
unit-tests/unit_tests unit-tests/unit_tests