summaryrefslogtreecommitdiff
path: root/src/resources.hpp
blob: b211d558801256fd8d2498eda83947eb3eb7e2ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma once

#include "smb.hpp"
#include "system.hpp"

namespace interfaces
{
struct MountPointStateMachine;
}

namespace resource
{

class Error : public std::runtime_error
{
  public:
    Error(std::errc errorCode, std::string message) :
        std::runtime_error(message), errorCode(errorCode)
    {
    }

    const std::errc errorCode;
};

class Directory
{
  public:
    Directory() = delete;
    Directory(const Directory&) = delete;
    Directory(Directory&& other) = delete;
    Directory& operator=(const Directory&) = delete;
    Directory& operator=(Directory&& other) = delete;

    explicit Directory(std::filesystem::path name) :
        path(std::filesystem::temp_directory_path() / name)
    {
        std::error_code ec;

        if (!std::filesystem::create_directory(path, ec))
        {
            LogMsg(Logger::Error, ec,
                   " : Unable to create mount directory: ", path);
            throw Error(std::errc::io_error,
                        "Failed to create mount directory");
        }
    }

    ~Directory()
    {
        std::error_code ec;

        if (!std::filesystem::remove(path, ec))
        {
            LogMsg(Logger::Error, ec, " : Unable to remove directory ", path);
        }
    }

    std::filesystem::path getPath() const
    {
        return path;
    }

  private:
    std::filesystem::path path;
};

class Mount
{
  public:
    Mount() = delete;
    Mount(const Mount&) = delete;
    Mount(Mount&& other) = delete;
    Mount& operator=(const Mount&) = delete;
    Mount& operator=(Mount&& other) = delete;

    explicit Mount(
        std::unique_ptr<Directory> directory, SmbShare& smb,
        const std::filesystem::path& remote, bool rw,
        const std::unique_ptr<utils::CredentialsProvider>& credentials) :
        directory(std::move(directory))
    {
        if (!smb.mount(remote, rw, credentials))
        {
            throw Error(std::errc::invalid_argument,
                        "Failed to mount CIFS share");
        }
    }

    ~Mount()
    {
        if (int result = ::umount(directory->getPath().string().c_str()))
        {
            LogMsg(Logger::Error, result, " : Unable to unmout directory ",
                   directory->getPath());
        }
    }

    std::filesystem::path getPath() const
    {
        return directory->getPath();
    }

  private:
    std::unique_ptr<Directory> directory;
};

class Process
{
  public:
    Process() = delete;
    Process(const Process&) = delete;
    Process(Process&& other) = delete;
    Process& operator=(const Process&) = delete;
    Process& operator=(Process&& other) = delete;
    Process(interfaces::MountPointStateMachine& machine,
            std::shared_ptr<::Process> process) :
        machine(&machine),
        process(std::move(process))
    {
        if (!this->process)
        {
            throw Error(std::errc::io_error, "Failed to create process");
        }
    }

    ~Process();

    template <class... Args>
    auto spawn(Args&&... args)
    {
        if (process->spawn(std::forward<Args>(args)...))
        {
            spawned = true;
            return true;
        }
        return false;
    }

  private:
    interfaces::MountPointStateMachine* machine;
    std::shared_ptr<::Process> process = nullptr;
    bool spawned = false;
};

class Gadget
{
  public:
    Gadget() = delete;
    Gadget& operator=(const Gadget&) = delete;
    Gadget& operator=(Gadget&& other) = delete;
    Gadget(const Gadget&) = delete;
    Gadget(Gadget&& other) = delete;

    Gadget(interfaces::MountPointStateMachine& machine, StateChange devState);
    ~Gadget();

  private:
    interfaces::MountPointStateMachine* machine;
    int32_t status;
};

} // namespace resource