From 9c9e400b68f9cfb2270434fb8a16cc0df906ab8b Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Sun, 24 May 2020 20:35:14 -0500 Subject: hscript: Implement 'svcenable' key and tests --- hscript/meta.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'hscript/meta.cc') diff --git a/hscript/meta.cc b/hscript/meta.cc index 9b0eb83..d43e58e 100644 --- a/hscript/meta.cc +++ b/hscript/meta.cc @@ -656,3 +656,47 @@ bool SigningKey::execute() const { #endif /* HAS_INSTALL_ENV */ return true; /* LCOV_EXCL_LINE */ } + +Key *SvcEnable::parseFromData(const std::string &data, int lineno, int *errors, + int *, const Script *script) { + const static std::string valid_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.-_"; + + if(data.find_first_not_of(valid_chars) != std::string::npos) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "svcenable: invalid service name", data); + return nullptr; + } + + return new SvcEnable(script, lineno, data); +} + +bool SvcEnable::execute() const { + const std::string target = script->targetDirectory() + + "/etc/runlevels/default/" + _value; + const std::string initd = "/etc/init.d/" + _value; + output_info("installfile:" + std::to_string(line), + "svcenable: enabling service " + _value); + + if(script->options().test(Simulate)) { + std::cout << "ln -s " << initd << " " << target << std::endl; + return true; + } + +#ifdef HAS_INSTALL_ENV + error_code ec; + if(!fs::exists(script->targetDirectory() + initd, ec)) { + output_warning("installfile:" + std::to_string(line), + "svcenable: service '" + _value + "' may be missing"); + } + + fs::create_symlink(initd, target, ec); + if(ec) { + output_error("installfile:" + std::to_string(line), + "svcenable: could not enable service " + _value, + ec.message()); + return false; + } +#endif /* HAS_INSTALL_ENV */ + return true; /* LCOV_EXCL_LINE */ +} -- cgit v1.2.3-60-g2f50