From 07a2de27b57848789171dee2559e4bd78d0e921c Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Mon, 21 Oct 2019 01:36:47 -0500 Subject: hscript: Implement DiskLabel, add tests --- hscript/disk.cc | 106 +++++++++++++++++++++++++++++++++++++++++++++--------- hscript/disk.hh | 25 +++++++++++++ hscript/script.cc | 25 ++++++++++++- 3 files changed, 138 insertions(+), 18 deletions(-) (limited to 'hscript') diff --git a/hscript/disk.cc b/hscript/disk.cc index 137d3cf..875d84f 100644 --- a/hscript/disk.cc +++ b/hscript/disk.cc @@ -28,6 +28,34 @@ using namespace Horizon::Keys; + +#ifdef HAS_INSTALL_ENV +/*! Determine if _block is a valid block device. + * @param key The key associated with this test. + * @param line The line number where the key exists. + * @param _block The path to test. + * @returns true if _block is valid, false otherwise. + * @note Will output_error if an error occurs. + */ +bool is_block_device(const std::string &key, int line, const std::string &_block) { + struct stat blk_stat; + const char *block_c = _block.c_str(); + if(access(block_c, F_OK) != 0 || stat(block_c, &blk_stat) != 0) { + output_error("installfile:" + std::to_string(line), + key + ": error opening device " + _block, + strerror(errno)); + return false; + } + if(!S_ISBLK(blk_stat.st_mode)) { + output_error("installfile:" + std::to_string(line), + key + ": " + _block + " is not a valid block device"); + return false; + } + return true; +} +#endif /* HAS_INSTALL_ENV */ + + Key *DiskId::parseFromData(const std::string &data, int lineno, int *errors, int *warnings) { std::string block, ident; @@ -46,26 +74,15 @@ Key *DiskId::parseFromData(const std::string &data, int lineno, int *errors, } bool DiskId::validate(ScriptOptions options) const { - /* We only validate if running in an Installation Environment. */ - if(!options.test(InstallEnvironment)) return true; - #ifdef HAS_INSTALL_ENV - /* Unlike 'mount', 'diskid' *does* require that the block device exist - * before installation begins. This test is always valid. */ - struct stat blk_stat; - const char *block_c = _block.c_str(); - if(access(block_c, F_OK) != 0 || stat(block_c, &blk_stat) != 0) { - output_error("installfile:" + std::to_string(line), - "diskid: error opening device " + _block, - strerror(errno)); - return false; - } - if(!S_ISBLK(blk_stat.st_mode)) { - output_error("installfile:" + std::to_string(line), - "diskid: " + _block + " is not a valid block device"); - return false; + /* We only validate if running in an Installation Environment. */ + if(options.test(InstallEnvironment)) { + /* Unlike 'mount', 'diskid' *does* require that the block device exist + * before installation begins. This test is always valid. */ + return is_block_device("diskid", this->lineno(), _block); } #endif /* HAS_INSTALL_ENV */ + return true; } @@ -117,6 +134,61 @@ bool DiskId::execute(ScriptOptions options) const { return match; } + +Key *DiskLabel::parseFromData(const std::string &data, int lineno, int *errors, + int *warnings) { + std::string block, label; + std::string::size_type sep = data.find_first_of(' '); + LabelType type; + + /* REQ: Runner.Validate.disklabel.Validity */ + if(sep == std::string::npos || data.length() == sep + 1) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "disklabel: expected a label type", + "valid format for disklabel is: [disk] [type]"); + return nullptr; + } + + block = data.substr(0, sep); + label = data.substr(sep + 1); + std::transform(label.begin(), label.end(), label.begin(), ::tolower); + /* REQ: Runner.Validate.disklabel.LabelType */ + if(label == "apm") { + type = APM; + } else if(label == "mbr") { + type = MBR; + } else if(label == "gpt") { + type = GPT; + } else { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "disklabel: '" + label + "' is not a valid label type", + "valid label types are: apm, mbr, gpt"); + return nullptr; + } + + return new DiskLabel(lineno, block, type); +} + +bool DiskLabel::validate(ScriptOptions options) const { +#ifdef HAS_INSTALL_ENV + /* REQ: Runner.Validate.disklabel.Block */ + if(options.test(InstallEnvironment)) { + /* disklabels are created before any others, so we can check now */ + return is_block_device("disklabel", this->lineno(), _block); + } +#endif /* HAS_INSTALL_ENV */ + + return true; +} + +bool DiskLabel::execute(ScriptOptions) const { + /* TODO XXX NOTIMPLEMENTED */ + return false; +} + + Key *Mount::parseFromData(const std::string &data, int lineno, int *errors, int *warnings) { std::string dev, where, opt; diff --git a/hscript/disk.hh b/hscript/disk.hh index 02ecb98..f8a9574 100644 --- a/hscript/disk.hh +++ b/hscript/disk.hh @@ -37,6 +37,31 @@ public: }; class DiskLabel : public Key { +public: + /*! The type of disklabel. */ + enum LabelType { + /*! Apple Partition Map (APM) */ + APM, + /*! Master Boot Record (MBR) */ + MBR, + /*! GUID Partition Table (GPT) */ + GPT + }; +private: + const std::string _block; + const LabelType _type; + + DiskLabel(int _line, const std::string &_b, const LabelType &_t) : + Key(_line), _block(_b), _type(_t) {} +public: + /*! Retrieve the block device that this key identifies. */ + const std::string device() const { return this->_block; } + /*! Retrieve the type of disklabel for the block device. */ + const LabelType type() const { return this->_type; } + + static Key *parseFromData(const std::string &, int, int*, int*); + bool validate(ScriptOptions) const override; + bool execute(ScriptOptions) const override; }; class Partition : public Key { diff --git a/hscript/script.cc b/hscript/script.cc index 785e40e..ee638bb 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -103,6 +103,8 @@ struct Script::ScriptPrivate { /*! Disk identification keys */ std::vector< std::unique_ptr > diskids; + /*! Disklabel configuration keys */ + std::vector< std::unique_ptr > disklabels; /*! Target system's mountpoints. */ std::vector< std::unique_ptr > mounts; @@ -157,6 +159,10 @@ struct Script::ScriptPrivate { std::unique_ptr diskid(dynamic_cast(obj)); this->diskids.push_back(std::move(diskid)); return true; + } else if(key_name == "disklabel") { + std::unique_ptr l(dynamic_cast(obj)); + this->disklabels.push_back(std::move(l)); + return true; } else if(key_name == "mount") { std::unique_ptr mount(dynamic_cast(obj)); this->mounts.push_back(std::move(mount)); @@ -496,7 +502,7 @@ const Script *Script::load(std::istream &sstream, bool Script::validate() const { int failures = 0; - std::set seen_diskids, seen_mounts; + std::set seen_diskids, seen_labels, seen_mounts; std::map seen_iface; /* REQ: Runner.Validate.network */ @@ -698,6 +704,23 @@ bool Script::validate() const { seen_diskids.insert(diskid->device()); } + /* REQ: Runner.Validate.disklabel */ + for(auto &label : this->internal->disklabels) { + if(!label->validate(this->opts)) { + failures++; + continue; + } + + /* REQ: Runner.Validate.disklabel.Unique */ + if(seen_labels.find(label->device()) != seen_labels.end()) { + failures++; + output_error("installfile:" + std::to_string(label->lineno()), + "disklabel: device " + label->device() + + " already has a label queued"); + } + seen_labels.insert(label->device()); + } + /* REQ: Runner.Validate.mount */ for(auto &mount : this->internal->mounts) { if(!mount->validate(this->opts)) { -- cgit v1.2.3-60-g2f50