summaryrefslogtreecommitdiffhomepage
path: root/Private/KernelKit/FileManager.hpp
blob: 311527f9231a028550c12ec931ff41da4b39149d (plain)
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
/*
 *	========================================================
 *
 *	hCore
 * 	Copyright 2024 Mahrouss Logic, all rights reserved.
 *
 * 	========================================================
 */

#pragma once

#include <NewKit/Stream.hpp>
#include <NewKit/Ref.hpp>
#include <FSKit/NewFS.hxx>

#include <NewKit/ErrorID.hpp>
#include <NewKit/Ref.hpp>

/// Main filesystem abstraction manager.

#define kBootFolder "/boot"
#define kBinFolder "/bin"
#define kShLibsFolder "/lib"

#define kSectorSz 512

/// refer to first enum.
#define kFileOpsCount 4

namespace hCore
{
    enum
    {
        kFileWriteAll = 100,
        kFileReadAll = 101,
        kFileReadChunk = 102,
        kFileWriteChunk = 103,
    };

    typedef VoidPtr NodePtr;

    class IFilesystemManager
    {
    public:
        IFilesystemManager() = default;
        virtual ~IFilesystemManager() = default;

    public:
        HCORE_COPY_DEFAULT(IFilesystemManager);

    public:
        static bool Mount(IFilesystemManager* pMount);
        static IFilesystemManager* Unmount();
        static IFilesystemManager* GetMounted();

    public:
        virtual NodePtr Create(const char* path) = 0;
        virtual NodePtr CreateAlias(const char* path) = 0;
        virtual NodePtr CreateDirectory(const char* path) = 0;

    public:
        virtual bool Remove(const char* path) = 0;

    public:
        virtual NodePtr Open(const char* path, const char* r) = 0;

    public:
        virtual void Write(NodePtr node, VoidPtr data, Int32 flags) = 0;
        virtual VoidPtr Read(NodePtr node, Int32 flags, SizeT sz) = 0;

    public:
        virtual bool Seek(NodePtr node, SizeT off) = 0;

    public:
        virtual SizeT Tell(NodePtr node) = 0;
        virtual bool Rewind(NodePtr node) = 0;

    };

    /**
     * @brief Child of IFilesystemManager, takes care of managing NewFS disks.
     */
    class NewFilesystemManager final : public IFilesystemManager
    {
    public:
        explicit NewFilesystemManager();
        ~NewFilesystemManager() override;

    public:
        HCORE_COPY_DEFAULT(NewFilesystemManager);

    public:
        NodePtr Create(const char* path) override;
        NodePtr CreateAlias(const char* path) override;
        NodePtr CreateDirectory(const char* path) override;

    public:
        bool Remove(const char* node) override;

    public:
        NodePtr Open(const char* path, const char* r) override { return nullptr; }

    public:
        void Write(NodePtr node, VoidPtr data, Int32 flags) override { }
        VoidPtr Read(NodePtr node, Int32 flags, SizeT sz) override { return nullptr; }

    public:
        bool Seek(NodePtr node, SizeT off) override { return true; }

    public:
        SizeT Tell(NodePtr node) override { return 0; }
        bool Rewind(NodePtr node) override { this->Seek(node, 0); return true; }

    public:
        NewFSImpl* fIO{ nullptr };

    };

    /**
     * Usable FileStream
     * @tparam Encoding file encoding (char, wchar_t...)
     * @tparam FSClass Filesystem contract who takes care of it.
     */
    template <typename Encoding = char, typename FSClass = NewFilesystemManager>
    class FileStream final
    {
    public:
        explicit FileStream(const Encoding* path);
        ~FileStream();

    public:
        FileStream &operator=(const FileStream &);                                                                         \
        FileStream(const FileStream &);

    public:
        ErrorOr<Int64> WriteAll(const VoidPtr data) noexcept
        {
            if (data == nullptr)
                return ErrorOr<Int64>(ME_INVALID_DATA);

            auto man = FSClass::GetMounted();

            if (man)
            {
                man->Write(fFile, data, kFileWriteAll);
                return ErrorOr<Int64>(0);
            }

            return ErrorOr<Int64>(ME_INVALID_DATA);
        }

        VoidPtr ReadAll() noexcept
        {
            auto man = FSClass::GetMounted();

            if (man)
            {
                VoidPtr ret = man->Read(fFile, kFileReadAll, 0);
                return ret;
            }

            return nullptr;
        }

        voidPtr Read(SizeT offset, SizeT sz)
        {
            auto man = FSClass::GetMounted();

            if (man)
            {
                man->Seek(fFile, offset);
                auto ret = man->Read(fFile, kFileReadChunk, sz);

                return ret;
            }

            return nullptr;
        }

        void Write(SizeT offset, voidPtr data, SizeT sz)
        {
            auto man = FSClass::GetMounted();

            if (man)
            {
                man->Seek(fFile, offset);
                man->Write(fFile, data, sz, kFileReadChunk);
            }
        }

    public:
        const char* MIME() noexcept { return fMime; }

    private:
        NodePtr fFile;
        Char* fMime{ "application-type/*" };

    };

    using FileStreamUTF8 = FileStream<char>;
    using FileStreamUTF16 = FileStream<wchar_t>;

    template <typename Encoding, typename Class>
    FileStream<Encoding, Class>::FileStream(const Encoding* path)
        : fFile(Class::GetMounted()->Open(path, "r+"))
    {}

    template <typename Encoding, typename Class>
    FileStream<Encoding, Class>::~FileStream() = default;
}

#define node_cast(PTR) reinterpret_cast<hCore::NodePtr>(PTR)