From 47558ea71d523fb71307d394be4bfad8da4664d8 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Sat, 12 Oct 2019 00:15:48 -0500 Subject: hscript: Parse 'mount' key, add tests to ensure --- hscript/disk.cc | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'hscript') diff --git a/hscript/disk.cc b/hscript/disk.cc index d491430..093c111 100644 --- a/hscript/disk.cc +++ b/hscript/disk.cc @@ -20,14 +20,46 @@ using namespace Horizon::Keys; Key *Mount::parseFromData(const std::string data, int lineno, int *errors, int *warnings) { std::string dev, where, opt; + std::string::size_type where_pos, opt_pos; + bool any_failure = false; + long spaces = std::count(data.cbegin(), data.cend(), ' '); if(spaces < 1 || spaces > 2) { if(errors) *errors += 1; + /* Don't bother with any_failure, because this is immediately fatal. */ output_error("installfile:" + std::to_string(lineno), "mount: expected either 2 or 3 elements, got: " + std::to_string(spaces), ""); return nullptr; } + + where_pos = data.find_first_of(' '); + opt_pos = data.find_first_of(' ', where_pos); + + dev = data.substr(0, where_pos); + where = data.substr(where_pos + 1, opt_pos); + if(opt_pos != std::string::npos) { + opt = data.substr(opt_pos); + } + + if(dev.compare(0, 4, "/dev")) { + if(errors) *errors += 1; + any_failure = true; + output_error("installfile:" + std::to_string(lineno), + "mount: element 1: expected device node", + "'" + dev + "' is not a valid device node"); + } + + if(where[0] != '/') { + if(errors) *errors += 1; + any_failure = true; + output_error("installfile:" + std::to_string(lineno), + "mount: element 2: expected absolute path", + "'" + where + "' is not a valid absolute path"); + } + + if(any_failure) return nullptr; + return new Mount(lineno, dev, where, opt); } -- cgit v1.2.3-60-g2f50