~aleteoryx/muditaos

ref: f56609aa3b5fad0701ed91adc4c375a7737bdc7e muditaos/module-vfs/src/deprecated/vfs-utils.cpp -rw-r--r-- 1.3 KiB
f56609aa — Lucjan Bryndza [EGD-4502] Fix and remove old vfs boot 5 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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <vfs.hpp>
#include <time/time_conversion.hpp>
#include <random>
#include <ticks.hpp>
#include <source/version.hpp>

#include <purefs/filesystem_paths.hpp>


std::string vfs::loadFileAsString(const fs::path &fileToLoad)
{
    auto lamb = [](vfs::FILE *stream) { ::vfs.fclose(stream); };
    std::unique_ptr<char[]> readBuf(new char[purefs::buffer::tar_buf]);
    std::unique_ptr<vfs::FILE, decltype(lamb)> fp(fopen(fileToLoad.c_str(), "r"), lamb);
    std::string contents;
    size_t readSize;

    if (fp.get() != nullptr) {
        while (!eof(fp.get())) {
            readSize = fread(readBuf.get(), 1, purefs::buffer::tar_buf, fp.get());
            contents.append(static_cast<const char *>(readBuf.get()), readSize);
        }
    }

    return contents;
}

bool vfs::replaceWithString(const fs::path &fileToModify, const std::string &stringToWrite)
{
    auto lamb = [](vfs::FILE *stream) { ::vfs.fclose(stream); };
    std::unique_ptr<vfs::FILE, decltype(lamb)> fp(::vfs.fopen(fileToModify.c_str(), "w"), lamb);

    if (fp.get() != nullptr) {
        size_t dataWritten = fprintf(fp.get(), stringToWrite.c_str());
        return dataWritten == stringToWrite.length();
    }
    else {
        return false;
    }
}