[thin_check] Spot XML and be helpful.

This commit is contained in:
Joe Thornber 2017-09-15 15:22:04 +01:00
parent 9ba75c890b
commit b10d8d4440
3 changed files with 37 additions and 4 deletions

View File

@ -19,7 +19,8 @@
assert-equal assert-equal
assert-eof assert-eof
assert-starts-with) assert-starts-with
assert-matches)
(import (import
(chezscheme) (chezscheme)
@ -27,6 +28,7 @@
(list-utils) (list-utils)
(logging) (logging)
(process) (process)
(regex)
(temp-file) (temp-file)
(utils) (utils)
(srfi s8 receive)) (srfi s8 receive))
@ -215,6 +217,9 @@
(dsp ", ") (dsp ", ")
(wrt str))))) (wrt str)))))
(define (assert-matches pattern str)
(unless ((regex pattern) str)
(fail (fmt #f "string should match: " pattern ", " str))))
) )

View File

@ -111,6 +111,19 @@
(with-valid-metadata (md) (with-valid-metadata (md)
(thin-check "--clear-needs-check-flag" md))) (thin-check "--clear-needs-check-flag" md)))
(define-scenario (thin-check tiny-metadata)
"Prints helpful message in case XML metadata given"
(with-thin-xml (xml)
(receive (_ stderr) (run-fail "thin_check" xml)
(assert-starts-with "Metadata device/file too small. Is this binary metadata?" stderr))))
(define-scenario (thin-check spot-accidental-xml-data)
"Prints helpful message if XML metadata given"
(with-thin-xml (xml)
(system (fmt #f "man bash >> " xml))
(receive (_ stderr) (run-fail "thin_check" xml)
(assert-matches ".*This looks like XML. thin_check only checks the binary metadata format." stderr))))
;;;----------------------------------------------------------- ;;;-----------------------------------------------------------
;;; thin_restore scenarios ;;; thin_restore scenarios
;;;----------------------------------------------------------- ;;;-----------------------------------------------------------

View File

@ -24,6 +24,7 @@
#include "base/application.h" #include "base/application.h"
#include "base/error_state.h" #include "base/error_state.h"
#include "base/file_utils.h"
#include "base/nested_output.h" #include "base/nested_output.h"
#include "persistent-data/data-structures/btree_counter.h" #include "persistent-data/data-structures/btree_counter.h"
#include "persistent-data/space-maps/core.h" #include "persistent-data/space-maps/core.h"
@ -201,13 +202,25 @@ namespace {
return err; return err;
} }
error_state metadata_check(string const &path, flags fs) { void check_for_xml(block_manager<>::ptr bm, nested_output &out) {
block_manager<>::ptr bm = open_bm(path); block_manager<>::read_ref b = bm->read_lock(superblock_detail::SUPERBLOCK_LOCATION);
if (!strncmp(reinterpret_cast<const char *>(b.data()), "<superblock", 10))
out << "This looks like XML. thin_check only checks the binary metadata format." << end_message();
}
error_state metadata_check(string const &path, flags fs) {
nested_output out(cerr, 2); nested_output out(cerr, 2);
if (fs.quiet) if (fs.quiet)
out.disable(); out.disable();
if (file_utils::get_file_length(path) < persistent_data::MD_BLOCK_SIZE) {
out << "Metadata device/file too small. Is this binary metadata?"
<< end_message();
return FATAL;
}
block_manager<>::ptr bm = open_bm(path);
superblock_reporter sb_rep(out); superblock_reporter sb_rep(out);
devices_reporter dev_rep(out); devices_reporter dev_rep(out);
mapping_reporter mapping_rep(out); mapping_reporter mapping_rep(out);
@ -218,8 +231,10 @@ namespace {
check_superblock(bm, sb_rep); check_superblock(bm, sb_rep);
} }
if (sb_rep.get_error() == FATAL) if (sb_rep.get_error() == FATAL) {
check_for_xml(bm, out);
return FATAL; return FATAL;
}
superblock_detail::superblock sb = read_superblock(bm); superblock_detail::superblock sb = read_superblock(bm);
transaction_manager::ptr tm = open_tm(bm); transaction_manager::ptr tm = open_tm(bm);