~aleteoryx/muditaos

ref: ea1d7503ae2cac7032205da04c80399492bb2926 muditaos/module-services/service-desktop/endpoints/filesystem/FS_Helper.cpp -rw-r--r-- 13.5 KiB
ea1d7503 — Lefucjusz [BH-2068] Update What's New database 1 year, 2 months ago
                                                                                
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include <endpoints/filesystem/FS_Helper.hpp>
#include <service-desktop/ServiceDesktop.hpp>
#include <endpoints/message/Sender.hpp>

#include <sys/statvfs.h>

namespace sdesktop::endpoints
{
    namespace
    {
        constexpr auto bytesInMebibyte = 1024LLU * 1024LLU;

        enum FileType
        {
            directory,
            regularFile,
            symlink,
            other,
        };

        auto parseFileEntry(const std::filesystem::directory_entry &entry) -> json11::Json
        {
            uintmax_t size = 0;
            FileType type;

            if (entry.is_directory()) {
                type = FileType::directory;
            }
            else {
                size = entry.file_size();
            }

            if (entry.is_regular_file()) {
                type = FileType::regularFile;
            }
            else if (entry.is_symlink()) {
                type = FileType::symlink;
            }
            else {
                type = FileType::other;
            }

            return json11::Json::object{
                {json::fs::path, entry.path().string()}, {json::fs::fileSize, size}, {json::fs::type, type}};
        }
    } // namespace

    auto FS_Helper::processGet(Context &context) -> ProcessResult
    {
        const auto &body = context.getBody();
        ResponseContext response{};

        if (body[json::fs::fileName].is_string()) {
            response = startGetFile(context);
        }
        else if (body[json::fs::rxID].is_number()) {
            response = getFileChunk(context);
        }
        else if (body[json::fs::listDir].is_string()) {
            const auto &dir = body[json::fs::listDir].string_value();
            response        = requestListDir(dir);
        }
        else {
            LOG_ERROR("Unknown request");
            response = {.status = http::Code::BadRequest};
        }

        return {sent::no, std::move(response)};
    }

    auto FS_Helper::processPut(Context &context) -> ProcessResult
    {
        const auto &body = context.getBody();
        auto code        = http::Code::BadRequest;
        ResponseContext response{};
        if (body[json::fs::fileName].is_string() && body[json::fs::fileSize].is_number() &&
            body[json::fs::fileCrc32].is_string()) {
            response = startSendFile(context);
        }
        else if (body[json::fs::txID].is_number() && body[json::fs::chunkNo].is_number() &&
                 body[json::fs::data].is_string()) {
            response = sendFileChunk(context);
        }
        else if (body[json::fs::renameFile].is_string() && body[json::fs::destFilename].is_string()) {
            const auto &fileName     = body[json::fs::renameFile].string_value();
            const auto &destFilename = body[json::fs::destFilename].string_value();
            code = requestFileRename(fileName, destFilename) ? http::Code::NoContent : http::Code::NotFound;

            response = ResponseContext{.status = code};
        }
        else {
            LOG_ERROR("Bad request, missing or invalid argument");
            response = ResponseContext{.status = http::Code::BadRequest};
        }

        return {sent::no, std::move(response)};
    }

    auto FS_Helper::processDelete(Context &context) -> ProcessResult
    {
        const auto &body = context.getBody();
        auto code        = http::Code::BadRequest;

        if (body[json::fs::removeFile].is_string()) {
            const auto &fileName = body[json::fs::removeFile].string_value();
            try {
                code = requestFileRemoval(fileName) ? http::Code::NoContent : http::Code::NotFound;
            }
            catch (const std::filesystem::filesystem_error &ex) {
                LOG_ERROR("Can't remove requested file %s, error: %d", fileName.c_str(), ex.code().value());
                code = http::Code::InternalServerError;
            }
        }
        else {
            LOG_ERROR("Bad request, missing or invalid argument");
        }

        return {sent::no, ResponseContext{.status = code}};
    }
    auto FS_Helper::requestLogsFlush() const -> void
    {
        auto ownerService = dynamic_cast<ServiceDesktop *>(owner);
        if (ownerService != nullptr) {
            ownerService->requestLogsFlush();
        }
    }

    auto FS_Helper::startGetFile(Context &context) const -> ResponseContext
    {
        const std::filesystem::path filePath = context.getBody()[json::fs::fileName].string_value();

        try {
            requestLogsFlush();
        }
        catch (const std::runtime_error &e) {
            LOG_ERROR("Logs flush exception: %s", e.what());

            json11::Json::object response({{json::common::reason, e.what()}});
            return ResponseContext{.status = http::Code::InternalServerError, .body = response};
        }

        if (!std::filesystem::exists(filePath)) {
            LOG_ERROR("File %s not found", filePath.c_str());

            json11::Json::object response({{json::common::reason, json::fs::fileDoesNotExist}});
            return ResponseContext{.status = http::Code::NotFound, .body = response};
        }

        json11::Json::object response{};
        auto code = http::Code::BadRequest;

        LOG_DEBUG("Checking file");

        try {
            auto [rxID, fileSize] = fileOps.createReceiveIDForFile(filePath);

            code     = http::Code::OK;
            response = json11::Json::object({{json::fs::rxID, static_cast<int>(rxID)},
                                             {json::fs::fileSize, static_cast<int>(fileSize)},
                                             {json::fs::chunkSize, static_cast<int>(FileOperations::ChunkSize)}});
        }
        catch (std::runtime_error &e) {
            LOG_ERROR("FileOperations exception: %s", e.what());

            code     = http::Code::InternalServerError;
            response = json11::Json::object({{json::common::reason, std::string(e.what())}});
        }
        catch (std::exception &e) {
            LOG_ERROR("FileOperations exception: %s", e.what());

            code     = http::Code::BadRequest;
            response = json11::Json::object({{json::common::reason, std::string(e.what())}});
        }

        return ResponseContext{.status = code, .body = response};
    }

    auto FS_Helper::getFileChunk(Context &context) const -> ResponseContext
    {
        const auto rxID    = context.getBody()[json::fs::rxID].int_value();
        const auto chunkNo = context.getBody()[json::fs::chunkNo].int_value();
        FileOperations::DataWithCrc32 dataWithCrc32;

        try {
            dataWithCrc32 = fileOps.getDataForReceiveID(rxID, chunkNo);
        }
        catch (std::exception &e) {
            LOG_ERROR("Exception during getting data: %s", e.what());

            json11::Json::object response({{json::common::reason, e.what()}});
            return ResponseContext{.status = http::Code::BadRequest, .body = response};
        }

        json11::Json::object response{};
        auto code = http::Code::BadRequest;

        if (!dataWithCrc32.data.empty()) {
            code     = http::Code::OK;
            response = json11::Json::object({{json::fs::rxID, static_cast<int>(rxID)},
                                             {json::fs::chunkNo, static_cast<int>(chunkNo)},
                                             {json::fs::data, dataWithCrc32.data}});

            if (!dataWithCrc32.crc32.empty()) {
                response[json::fs::fileCrc32] = dataWithCrc32.crc32;
            }
        }
        else {
            std::ostringstream errorReason;
            errorReason << "Invalid request rxID: " << std::to_string(rxID) << ", chunkNo: " << std::to_string(chunkNo);
            LOG_ERROR("%s", errorReason.str().c_str());

            code     = http::Code::BadRequest;
            response = json11::Json::object({{json::common::reason, errorReason.str()}});
        }

        return ResponseContext{.status = code, .body = response};
    }

    auto FS_Helper::startSendFile(Context &context) const -> ResponseContext
    {
        const auto &body             = context.getBody();
        const auto &filePath         = body[json::fs::fileName].string_value();
        const std::uint32_t fileSize = body[json::fs::fileSize].int_value();
        const auto &fileCrc32        = body[json::fs::fileCrc32].string_value();
        auto code                    = http::Code::BadRequest;

        LOG_DEBUG("Start sending of file: %s", filePath.c_str());

        if (fileSize == 0 || fileCrc32.empty()) {
            LOG_ERROR("File '%s' corrupted", filePath.c_str());

            return ResponseContext{.status = code};
        }

        const auto freeSpaceLeftForUserFilesMiB = getFreeSpaceForUserFilesMiB();

        if ((freeSpaceLeftForUserFilesMiB - (static_cast<float>(fileSize) / bytesInMebibyte)) <= 0) {
            LOG_ERROR("Not enough space left on device!");
            code = http::Code::InsufficientStorage;
            return ResponseContext{.status = code};
        }

        if (!std::filesystem::exists(filePath)) {
            LOG_DEBUG("Creating file %s", filePath.c_str());
        }
        else {
            LOG_DEBUG("Overwriting file %s", filePath.c_str());
        }

        json11::Json::object response{};

        try {
            auto txID = fileOps.createTransmitIDForFile(filePath, fileSize, fileCrc32);

            code     = http::Code::OK;
            response = json11::Json::object({{json::fs::txID, static_cast<int>(txID)},
                                             {json::fs::chunkSize, static_cast<int>(FileOperations::ChunkSize)}});
        }
        catch (std::runtime_error &e) {
            LOG_ERROR("FileOperations exception: %s", e.what());

            code     = http::Code::InternalServerError;
            response = json11::Json::object({{json::common::reason, std::string(e.what())}});
        }
        catch (std::exception &e) {
            LOG_ERROR("FileOperations exception: %s", e.what());

            code     = http::Code::BadRequest;
            response = json11::Json::object({{json::common::reason, std::string(e.what())}});
        }

        return ResponseContext{.status = code, .body = response};
    }

    auto FS_Helper::sendFileChunk(Context &context) const -> ResponseContext
    {
        const auto &body   = context.getBody();
        const auto txID    = body[json::fs::txID].int_value();
        const auto chunkNo = body[json::fs::chunkNo].int_value();
        const auto &data   = body[json::fs::data].string_value();

        if (data.empty()) {
            std::ostringstream errorReason;
            errorReason << "Invalid request txID: " << std::to_string(txID) << ", chunkNo: " << std::to_string(chunkNo);
            LOG_ERROR("%s", errorReason.str().c_str());

            auto code     = http::Code::BadRequest;
            auto response = json11::Json::object({{json::common::reason, errorReason.str()}});

            return ResponseContext{.status = code, .body = response};
        }

        auto returnCode = sys::ReturnCodes::Success;

        try {
            returnCode = fileOps.sendDataForTransmitID(txID, chunkNo, data);
        }
        catch (std::exception &e) {
            LOG_ERROR("Exception during sending data: %s", e.what());
            auto code     = http::Code::NotAcceptable;
            auto response = json11::Json::object({{json::common::reason, e.what()}});

            return ResponseContext{.status = code, .body = response};
        }

        json11::Json::object response{};
        auto code = http::Code::OK;

        if (returnCode == sys::ReturnCodes::Success) {
            response = json11::Json::object(
                {{json::fs::txID, static_cast<int>(txID)}, {json::fs::chunkNo, static_cast<int>(chunkNo)}});
        }
        else {
            code     = http::Code::BadRequest;
            response = json11::Json::object(
                {{json::fs::txID, static_cast<int>(txID)}, {json::fs::chunkNo, static_cast<int>(chunkNo)}});
        }

        return ResponseContext{.status = code, .body = response};
    }

    auto FS_Helper::requestFileRemoval(const std::string &fileName) -> bool
    {
        return std::filesystem::remove(fileName);
    }

    auto FS_Helper::requestFileRename(const std::string &fileName, const std::string &destFilename) noexcept -> bool
    {
        std::error_code ec;
        std::filesystem::rename(fileName, destFilename, ec);
        if (!ec) {
            LOG_ERROR("Failed to rename %s, error: %d", fileName.c_str(), ec.value());
        }
        return !ec;
    }

    auto FS_Helper::requestListDir(const std::string &directory) -> ResponseContext
    {
        if (!std::filesystem::exists(directory)) {
            return ResponseContext{.status = http::Code::NotFound};
        }

        json11::Json::array jsonArr;

        for (const auto &entry : std::filesystem::directory_iterator{directory}) {
            jsonArr.push_back(parseFileEntry(entry));
        }

        json11::Json::object const response({{directory, jsonArr}});
        return ResponseContext{.status = http::Code::OK, .body = response};
    }

    auto FS_Helper::getFreeSpaceForUserFilesMiB() const -> float
    {
        const auto userDiskPath = purefs::dir::getUserDiskPath();
        struct statvfs vfstat
        {};

        if (statvfs(userDiskPath.c_str(), &vfstat) < 0) {
            return 0;
        }

        const auto freeBytes = (static_cast<float>(vfstat.f_bfree) * static_cast<float>(vfstat.f_bsize));
        const auto freeMiBs  = freeBytes / bytesInMebibyte;

        return freeMiBs;
    }
} // namespace sdesktop::endpoints