Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
141 lines (125 loc) · 4.12 KB
/
Copy pathmain.cpp
File metadata and controls
141 lines (125 loc) · 4.12 KB
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
// Custom backend: implement IBackend and mount "content generated by the program" into the virtual filesystem.
// This backend is read-only: each /gen/<n>.txt file contains the square of n, and the directory always has 10 files.
#include "vfs/VirtualFileSystem.h"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <string>
USING_NS_VFS;
namespace
{
// Read-only string stream: a custom backend usually needs its own IFileStream
class StringStream : public IFileStream
{
public:
explicit StringStream(std::string data) : m_data(std::move(data)) {}
bool open(std::string_view, IFileStream::Mode) override { return false; } // constructed by the backend; reopening is not supported
int close() override
{
m_open = false;
return 0;
}
int64_t seek(int64_t offset, int origin) const override
{
int64_t base = origin == SEEK_SET ? 0 : origin == SEEK_CUR ? m_pos : static_cast<int64_t>(m_data.size());
if (base + offset < 0)
return -1;
m_pos = base + offset;
return m_pos;
}
int read(void* buf, unsigned int size) const override
{
if (!m_open)
return -1;
int64_t avail = std::max<int64_t>(0, static_cast<int64_t>(m_data.size()) - m_pos);
size_t n = static_cast<size_t>(std::min<int64_t>(avail, size));
std::memcpy(buf, m_data.data() + m_pos, n);
m_pos += static_cast<int64_t>(n);
return static_cast<int>(n);
}
int write(const void*, unsigned int) const override { return -1; } // read-only
int64_t tell() const override { return m_pos; }
int64_t size() const override { return static_cast<int64_t>(m_data.size()); }
bool isOpen() const override { return m_open; }
private:
std::string m_data;
mutable int64_t m_pos = 0; // IFileStream methods are const, so the position has to be mutable
bool m_open = true;
};
// Backend that generates content. rel is a normalized path relative to the mount point (no leading '/', empty string for the root)
class SquaresBackend : public IBackend
{
public:
uint32_t capabilities() const override { return BackendCaps::Read; }
Error stat(std::string_view rel, Stat& out) const override
{
if (rel.empty())
{
out.type = FileType::DIR;
out.size = 0;
return Error::OK;
}
int n = 0;
if (!parse(rel, n))
return Error::NOT_FOUND;
out.type = FileType::FILE;
out.size = static_cast<int64_t>(content(n).size());
return Error::OK;
}
std::unique_ptr<IFileStream> open(std::string_view rel, IFileStream::Mode mode, Error* err) override
{
int n = 0;
Error e = Error::OK;
if (mode != IFileStream::Mode::READ)
e = Error::READ_ONLY;
else if (rel.empty())
e = Error::IS_DIR;
else if (!parse(rel, n))
e = Error::NOT_FOUND;
if (err)
*err = e;
return e == Error::OK ? std::make_unique<StringStream>(content(n)) : nullptr;
}
Error list(std::string_view relDir, const std::function<bool(const DirEntry&)>& cb) const override
{
if (!relDir.empty())
return parseName(relDir) ? Error::NOT_DIR : Error::NOT_FOUND;
for (int i = 0; i < 10; ++i)
if (!cb(DirEntry{std::to_string(i) + ".txt", FileType::FILE}))
break;
return Error::OK;
}
const char* typeName() const override { return "squares"; }
private:
static bool parseName(std::string_view rel)
{
int n = 0;
return parse(rel, n);
}
// "<0..9>.txt" → n
static bool parse(std::string_view rel, int& n)
{
if (rel.size() != 5 || rel.substr(1) != ".txt" || rel[0] < '0' || rel[0] > '9')
return false;
n = rel[0] - '0';
return true;
}
static std::string content(int n) { return std::to_string(n * n); }
};
} // namespace
int main()
{
VirtualFileSystem fs;
fs.mount("/gen", std::make_shared<SquaresBackend>());
// Stack a writable memory layer on top: writes land in memory and hide the generated content
fs.mountMemory("/gen", MountOptions{1, MountFlags::WriteTarget});
std::string text;
fs.readFile("/gen/7.txt", text);
std::printf("/gen/7.txt = %s\n", text.c_str());
fs.writeFile("/gen/7.txt", "overridden");
fs.readFile("/gen/7.txt", text);
std::printf("/gen/7.txt = %s (after write)\n", text.c_str());
std::printf("/gen/42.txt exists: %s\n", fs.exists("/gen/42.txt") ? "yes" : "no");
std::printf("/gen has %zu entries\n", fs.list("/gen").size());
return 0;
}