| 1 | #include "path.hpp" |
| 2 | |
| 3 | #include "ServiceManager.hpp" |
| 4 | |
| 5 | #include <sys/stat.h> |
| 6 | #include <unistd.h> |
| 7 | |
| 8 | RegisterUnit(path, Path); |
| 9 | |
| 10 | Path::Path(const std::string &id, toml::table &table, std::shared_ptr<const Template> template_, const ArgumentMap &args) |
| 11 | : Unit(id, table, template_, args), // |
| 12 | path(PopArg(table, key: "path" )) |
| 13 | { |
| 14 | if (path.empty()) |
| 15 | std::cerr << "path: missing path" << std::endl; |
| 16 | } |
| 17 | |
| 18 | bool Path::Start() |
| 19 | { |
| 20 | status.Starting(msg: "creating..." ); |
| 21 | const auto err = mkdir(pathname: path.c_str(), mode: 0755); |
| 22 | if (err != 0 && errno != EEXIST) |
| 23 | { |
| 24 | status.Failed(msg: strerror(errno)); |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | status.Started(msg: "created" ); |
| 29 | ServiceManager->OnUnitStarted(unit: this); |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | bool Path::Stop() |
| 34 | { |
| 35 | status.Stopping(msg: "removing..." ); |
| 36 | if (rmdir(path: path.c_str()) != 0) |
| 37 | { |
| 38 | status.Failed(msg: strerror(errno)); |
| 39 | return false; |
| 40 | } |
| 41 | status.Inactive(); |
| 42 | ServiceManager->OnUnitStopped(unit: this); |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | void Path::onPrint(std::ostream &os) const |
| 47 | { |
| 48 | os << " path: " << this->path << std::endl; |
| 49 | } |
| 50 | |