~aleteoryx/muditaos

ref: 52ca291b30c634cee5286a5fa2b3febd0685fec4 muditaos/module-vfs/include/user/deprecated/vfs.hpp -rw-r--r-- 4.8 KiB
52ca291b — Lucjan Bryndza [EGD-5097] Fix minor issues with littlefs fuse 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
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <stdint.h>
#include <string>
#include <vector>
#include <sstream>
#include <filesystem>
#include <log/log.hpp>
#include <atomic>
#include <boot/bootconfig.hpp>
#include "vfs_globals.hpp"

#ifndef TARGET_Linux
#include "board/cross/eMMC/eMMC.hpp"
#endif


namespace fs = std::filesystem;

namespace purefs
{
    namespace extension
    {
        inline constexpr auto crc32 = ".crc32";
    }

    namespace buffer
    {
        inline constexpr auto crc_buf       = 1024;
        inline constexpr auto crc_char_size = 9;
        inline constexpr auto crc_radix     = 16;
        inline constexpr auto tar_buf       = 8192 * 4;
        inline constexpr auto copy_buf      = 8192 * 4;
    } // namespace buffer

    namespace json
    {
        inline constexpr auto main            = "main";
        inline constexpr auto os_type         = "ostype";
        inline constexpr auto os_image        = "imagename";
        inline constexpr auto os_version      = "version";
        inline constexpr auto version_major   = "major";
        inline constexpr auto version_minor   = "minor";
        inline constexpr auto version_patch   = "patch";
        inline constexpr auto version_string  = "string";
        inline constexpr auto timestamp       = "timestamp";
        inline constexpr auto misc            = "misc";
        inline constexpr auto builddate       = "builddate";
        inline constexpr auto git_info        = "git";
        inline constexpr auto os_git_tag      = "git_tag";
        inline constexpr auto os_git_revision = "git_commit";
        inline constexpr auto os_git_branch   = "git_branch";
        inline constexpr auto bootloader      = "bootloader";
    } // namespace json

}; // namespace purefs

/* NOTE: VFS global object class is now deprecated more information
 * on the confuence page:
 * https://appnroll.atlassian.net/wiki/spaces/MFP/pages/706248802/VFS+library+migration+guide
 */
class vfs
{
  public:
    // using FsEvent = vfsn::utility::vfsNotifier::FsEvent;
    enum class FileAttributes
    {
        ReadOnly,
        Writable,
        Directory
    };

    struct DirectoryEntry
    {
        std::string fileName;
        FileAttributes attributes;
        uint32_t fileSize;
        json11::Json to_json() const
        {
            return (json11::Json::object{{"name", fileName}, {"size", std::to_string(fileSize)}});
        }
    };

    struct FilesystemStats
    {
        std::string type;
        uint32_t freeMbytes;
        uint32_t freePercent;
        uint32_t totalMbytes;
    };
    vfs();
    ~vfs();
    void Init();
    [[deprecated]] FILE *fopen(const char *filename, const char *mode);
    [[deprecated]] int fclose(FILE *stream);
    [[deprecated]] int remove(const char *name);
    [[deprecated]] size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
    [[deprecated]] size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
    [[deprecated]] int fseek(FILE *stream, long int offset, int origin);
    [[deprecated]] long int ftell(FILE *stream);
    [[deprecated]] void rewind(FILE *stream);
    [[deprecated]] size_t filelength(FILE *stream);
    [[deprecated]] std::string getcurrdir();
    [[deprecated]] char *fgets(char *buffer, size_t count, FILE *stream);
    [[deprecated]] bool eof(FILE *stream);
    [[deprecated]] std::vector<DirectoryEntry> listdir(const char *path,
                                                       const std::string &ext     = "",
                                                       const bool bypassRootCheck = false);
    [[deprecated]] std::string getline(FILE *stream, uint32_t length = 1024);
    [[deprecated]] size_t fprintf(FILE *stream, const char *format, ...);
    [[deprecated]] FilesystemStats getFilesystemStats();
    [[deprecated]] std::string relativeToRoot(const std::string path);
    [[deprecated]] std::string lastErrnoToStr();
    [[deprecated]] bool isDir(const char *path);
    [[deprecated]] bool fileExists(const char *path);
    [[deprecated]] int deltree(const char *path);
    [[deprecated]] int mkdir(const char *dir);
    [[deprecated]] int rename(const char *oldname, const char *newname);
    /*
    [[deprecated]] void registerNotificationHandler(vfsn::utility::vfsNotifier::NotifyHandler handler)
    {
        chnNotifier.registerNotificationHandler(handler);
    }
    */
    [[deprecated]] auto getAbsolutePath(std::string_view path) const -> std::string;

#ifndef TARGET_Linux
    bsp::eMMC emmc;
#endif

    /// FF_Disk_t *emmcFFDisk{};

    auto isInitialized() const noexcept
    {
        return initDone.load();
    }

  private:
    // vfsn::utility::vfsNotifier chnNotifier;
    static std::atomic<bool> initDone;
    boot::BootConfig bootConfig;
};

extern vfs vfs;