1#include "symlink.hpp"
2
3#include "ServiceManager.hpp"
4
5#include <unistd.h>
6
7RegisterUnit(symlink, Symlink);
8
9Symlink::Symlink(const std::string &id, toml::table &table, std::shared_ptr<const Template> template_, const ArgumentMap &args)
10 : Unit(id, table, template_, args), //
11 linkfile(PopArg(table, key: "link")), //
12 target(PopArg(table, key: "target"))
13{
14}
15
16bool Symlink::Start()
17{
18 status.Starting();
19 if (symlink(target: target.c_str(), linkpath: linkfile.c_str()) != 0)
20 {
21 status.Failed(msg: strerror(errno));
22 return false;
23 }
24
25 status.Started(msg: "created");
26 ServiceManager->OnUnitStarted(unit: this);
27 return true;
28}
29
30bool Symlink::Stop()
31{
32 status.Stopping();
33 if (unlink(path: linkfile.c_str()) != 0)
34 {
35 status.Failed(msg: strerror(errno));
36 return false;
37 }
38
39 status.Inactive();
40 ServiceManager->OnUnitStopped(unit: this);
41 return true;
42}
43