~aleteoryx/muditaos

ref: 6f21138e8587a063267c460f45b11efdb9121a25 muditaos/module-services/service-desktop/endpoints/filesystem/FilesystemEndpoint.cpp -rw-r--r-- 6.2 KiB
6f21138e — Marek Niepieklo [CP-253] Add checksum to package transfer 4 years 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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "FilesystemEndpoint.hpp"
#include "FileOperations.hpp"
#include "service-desktop/DesktopMessages.hpp"
#include "service-desktop/ServiceDesktop.hpp"
#include <purefs/filesystem_paths.hpp>
#include <filesystem>

using namespace parserFSM;

auto FilesystemEndpoint::handle(Context &context) -> void
{
    auto returnCode = sys::ReturnCodes::Success;
    switch (context.getMethod()) {
    case http::Method::get:
        returnCode = runGet(context);
        break;
    case http::Method::post:
        returnCode = runPost(context);
        break;
    default:
        LOG_ERROR("Unhandled method request: %u", static_cast<unsigned>(context.getMethod()));
        returnCode = sys::ReturnCodes::Failure;
        break;
    }
    LOG_DEBUG("returnCode: %u", static_cast<unsigned>(returnCode));
}
static bool isWritable(const fs::path &file)
{
    auto lamb = [](std::FILE *stream) { std::fclose(stream); };

    std::unique_ptr<std::FILE, decltype(lamb)> sf(std::fopen(file.c_str(), "w"), lamb);

    return static_cast<bool>(sf);
}

auto FilesystemEndpoint::startGetFile(Context &context) const -> sys::ReturnCodes
{
    std::filesystem::path filePath = context.getBody()[parserFSM::json::fileName].string_value();

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

        context.setResponseStatus(parserFSM::http::Code::NotFound);
        context.setResponseBody(json11::Json::object({{json::reason, json::filesystem::reasons::fileDoesNotExist}}));
        return sys::ReturnCodes::Failure;
    }

    LOG_DEBUG("Checking file: %s", filePath.c_str());

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

    if (!fileSize) {
        LOG_ERROR("File %s corrupted", filePath.c_str());

        context.setResponseStatus(parserFSM::http::Code::InternalServerError);
        context.setResponseBody(json11::Json::object({{json::reason, json::filesystem::reasons::fileDoesNotExist}}));
        return sys::ReturnCodes::Failure;
    }

    context.setResponseStatus(parserFSM::http::Code::OK);
    context.setResponseBody(
        json11::Json::object({{json::filesystem::rxID, static_cast<int>(rxID)},
                              {json::fileSize, static_cast<int>(fileSize)},
                              {json::filesystem::chunkSize, static_cast<int>(FileOperations::ChunkSize)}}));

    return sys::ReturnCodes::Success;
}

auto FilesystemEndpoint::getFileChunk(Context &context) const -> sys::ReturnCodes
{
    auto returnCode    = sys::ReturnCodes::Failure;
    const auto rxID    = context.getBody()[parserFSM::json::filesystem::rxID].int_value();
    const auto chunkNo = context.getBody()[parserFSM::json::filesystem::chunkNo].int_value();

    const auto data = fileOps.getDataForReceiveID(rxID, chunkNo);

    if (data.length()) {
        context.setResponseStatus(parserFSM::http::Code::OK);
        context.setResponseBody(json11::Json::object({{json::filesystem::rxID, static_cast<int>(rxID)},
                                                      {json::filesystem::chunkNo, static_cast<int>(chunkNo)},
                                                      {json::filesystem::data, data}}));

        returnCode = sys::ReturnCodes::Success;
    }
    else {
        std::ostringstream errorReason;
        errorReason << "Invalid request rxID: " << std::to_string(rxID) << ", chunkNo: " << std::to_string(chunkNo);
        LOG_ERROR("%s", errorReason.str().c_str());

        context.setResponseStatus(parserFSM::http::Code::BadRequest);
        context.setResponseBody(json11::Json::object({{json::reason, errorReason.str()}}));
    }

    return returnCode;
}

auto FilesystemEndpoint::runGet(Context &context) -> sys::ReturnCodes
{
    LOG_DEBUG("Handling GET");
    auto returnCode = sys::ReturnCodes::Failure;

    if (context.getBody()[parserFSM::json::fileName].is_string()) {
        returnCode = startGetFile(context);
    }
    else if (context.getBody()[parserFSM::json::filesystem::rxID].is_number()) {
        returnCode = getFileChunk(context);
    }

    MessageHandler::putToSendQueue(context.createSimpleResponse());

    return returnCode;
}

auto FilesystemEndpoint::runPost(Context &context) -> sys::ReturnCodes
{
    LOG_DEBUG("Handling POST");
    sys::ReturnCodes returnCode = sys::ReturnCodes::Failure;
    std::string cmd             = context.getBody()[parserFSM::json::filesystem::command].string_value();

    context.setResponseBody(
        json11::Json::object({{json::status, std::to_string(static_cast<int>(sys::ReturnCodes::Failure))}}));

    auto owner = static_cast<ServiceDesktop *>(ownerServicePtr);

    if (cmd == parserFSM::json::filesystem::commands::download) {
        fs::path filePath    = context.getBody()[parserFSM::json::fileName].string_value();
        fs::path tmpFilePath = purefs::dir::getUpdatesOSPath() / filePath;

        const uint32_t fileSize = context.getBody()[parserFSM::json::fileSize].int_value();
        const auto fileCrc32    = context.getBody()[parserFSM::json::fileCrc32].string_value();

        LOG_DEBUG("got owner, file %s", tmpFilePath.c_str());

        if (isWritable(tmpFilePath)) {
            LOG_INFO("download %" PRIu32 " bytes to: %s", fileSize, tmpFilePath.c_str());

            if (owner->desktopWorker->startDownload(tmpFilePath, fileSize, fileCrc32) == sys::ReturnCodes::Success) {
                context.setResponseStatus(parserFSM::http::Code::Accepted);
                returnCode = sys::ReturnCodes::Success;
            }
        }
        else {
            LOG_ERROR("download command failed, can't write %" PRIu32 " bytes to: %s", fileSize, tmpFilePath.c_str());
        }
    }
    else if (cmd == parserFSM::json::filesystem::commands::checkFile) {
        fs::path filePath = context.getBody()[parserFSM::json::fileName].string_value();
        LOG_DEBUG("Checking file: %s", filePath.c_str());

        context.setResponseBody(json11::Json::object{{json::fileExists, std::filesystem::exists(filePath)}});
        returnCode = sys::ReturnCodes::Success;
    }
    else {
        LOG_ERROR("unknown command: %s", cmd.c_str());
    }

    MessageHandler::putToSendQueue(context.createSimpleResponse());
    return returnCode;
}