~aleteoryx/muditaos

ref: 69e5fe52231248586d8590cc4017d52d1425700f muditaos/module-services/service-desktop/endpoints/filesystem/FileContext.cpp -rw-r--r-- 4.2 KiB
69e5fe52 — Maciej Gibowicz [MOS-144] Fix Increased CPU usage 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 <endpoints/filesystem/FileContext.hpp>
#include <log/log.hpp>
#include <utility>
#include <fstream>

FileContext::FileContext(const std::filesystem::path &path,
                         std::size_t size,
                         std::size_t chunkSize,
                         std::size_t offset)
    : path(path), size(size), offset(offset), chunkSize(chunkSize)
{
    if (!size || !chunkSize) {
        throw std::invalid_argument("Invalid FileContext arguments");
    }

    runningCrc32Digest.reset();
}

FileContext::~FileContext()
{
}

FileReadContext::FileReadContext(const std::filesystem::path &path,
                                 std::size_t size,
                                 std::size_t chunkSize,
                                 std::size_t offset)
    : FileContext(path, size, chunkSize, offset)
{}

FileReadContext::~FileReadContext()
{}

FileWriteContext::FileWriteContext(const std::filesystem::path &path,
                                   std::size_t size,
                                   std::size_t chunkSize,
                                   std::string crc32Digest,
                                   std::size_t offset)
    : FileContext(path, size, chunkSize, offset), crc32Digest(std::move(crc32Digest))
{}

FileWriteContext::~FileWriteContext()
{}

auto FileContext::advanceFileOffset(std::size_t bySize) -> void
{
    offset += bySize;
}

auto FileContext::reachedEOF() const -> bool
{
    return offset >= size;
}

auto FileContext::chunksInQuantity(std::size_t quantity) const -> std::size_t
{
    return (quantity + chunkSize - 1) / chunkSize;
}

auto FileContext::totalChunksInFile() const -> std::size_t
{
    return chunksInQuantity(size);
}

auto FileContext::expectedChunkInFile() const -> std::size_t
{
    return 1 + chunksInQuantity(offset);
}

auto FileContext::validateChunkRequest(std::uint32_t chunkNo) const -> bool
{
    return !(chunkNo < 1 || chunkNo > totalChunksInFile() || chunkNo != expectedChunkInFile());
}

auto FileContext::fileHash() const -> std::string
{
    return runningCrc32Digest.getHash();
}

auto FileReadContext::read() -> std::vector<std::uint8_t>
{
    LOG_DEBUG("Getting file data");

    std::ifstream file(path, std::ios::binary);

    if (!file.is_open() || file.fail()) {
        LOG_ERROR("File %s open error", path.c_str());
        throw std::runtime_error("File open error");
    }

    file.seekg(offset);

    auto dataLeft = std::min(static_cast<std::size_t>(chunkSize), (size - offset));

    std::vector<std::uint8_t> buffer(dataLeft);

    file.read(reinterpret_cast<char *>(buffer.data()), dataLeft);

    if (file.bad()) {
        LOG_ERROR("File %s read error", path.c_str());
        throw std::runtime_error("File read error");
    }

    runningCrc32Digest.add(buffer.data(), dataLeft);

    LOG_DEBUG("Read %u bytes", static_cast<unsigned int>(dataLeft));
    advanceFileOffset(dataLeft);

    if (reachedEOF()) {
        LOG_INFO("Reached EOF");
    }

    return buffer;
}

auto FileWriteContext::write(const std::vector<std::uint8_t> &data) -> void
{
    LOG_DEBUG("Sending file data");

    std::ofstream file(path, std::ios::binary | std::ios::app);

    if (!file.is_open() || file.fail()) {
        LOG_ERROR("File %s open error", path.c_str());
        throw std::runtime_error("File open error");
    }

    file.seekp(offset);

    auto dataLeft = std::min(static_cast<std::size_t>(chunkSize), (size - offset));

    file.write(reinterpret_cast<const char *>(data.data()), dataLeft);
    file.flush();

    if (file.bad()) {
        LOG_ERROR("File %s write error", path.c_str());
        throw std::runtime_error("File write error");
    }

    runningCrc32Digest.add(data.data(), dataLeft);

    LOG_DEBUG("Written %u bytes", static_cast<unsigned int>(dataLeft));

    advanceFileOffset(dataLeft);

    if (reachedEOF()) {
        LOG_INFO("Reached EOF of %s", path.c_str());
    }
}

auto FileWriteContext::crc32Matches() const -> bool
{
    LOG_DEBUG("Hash: %s", fileHash().c_str());
    return crc32Digest == fileHash();
}

auto FileWriteContext::removeFile() -> void
{
    std::error_code ec;
    std::filesystem::remove(path, ec);
}