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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
#include "slang-memory-file-system.h"
// For Path::
#include "slang-io.h"
#include "slang-blob.h"
#include "slang-implicit-directory-collector.h"
namespace Slang
{
MemoryFileSystem::MemoryFileSystem()
{
m_rootEntry.initDirectory("/");
}
void* MemoryFileSystem::getInterface(const Guid& guid)
{
if ( guid == ISlangUnknown::getTypeGuid() ||
guid == ISlangCastable::getTypeGuid() ||
guid == ISlangFileSystem::getTypeGuid() ||
guid == ISlangFileSystemExt::getTypeGuid() ||
guid == ISlangMutableFileSystem::getTypeGuid())
{
return static_cast<ISlangMutableFileSystem*>(this);
}
return nullptr;
}
void* MemoryFileSystem::getObject(const Guid& guid)
{
SLANG_UNUSED(guid);
return nullptr;
}
void* MemoryFileSystem::castAs(const Guid& guid)
{
if (auto ptr = getInterface(guid))
{
return ptr;
}
return getObject(guid);
}
void MemoryFileSystem::_clear()
{
m_entries = Dictionary<String, Entry>();
}
MemoryFileSystem::Entry* MemoryFileSystem::_getEntryFromCanonicalPath(const String& canonicalPath)
{
if (canonicalPath == toSlice("."))
{
return &m_rootEntry;
}
else
{
return m_entries.tryGetValue(canonicalPath);
}
}
MemoryFileSystem::Entry* MemoryFileSystem::_getEntryFromPath(const char* path, String* outPath)
{
StringBuilder buffer;
if (SLANG_FAILED(_getCanonical(path, buffer)))
{
return nullptr;
}
if (outPath)
{
*outPath = buffer;
}
return _getEntryFromCanonicalPath(buffer);
}
SlangResult MemoryFileSystem::_loadFile(const char* path, Entry** outEntry)
{
*outEntry = nullptr;
Entry* entry = _getEntryFromPath(path);
if (entry == nullptr || entry->m_type != SLANG_PATH_TYPE_FILE)
{
return SLANG_E_NOT_FOUND;
}
*outEntry = entry;
return SLANG_OK;
}
SlangResult MemoryFileSystem::loadFile(char const* path, ISlangBlob** outBlob)
{
Entry* entry;
SLANG_RETURN_ON_FAIL(_loadFile(path, &entry));
ISlangBlob* contents = entry->m_contents;
contents->addRef();
*outBlob = contents;
return SLANG_OK;
}
SlangResult MemoryFileSystem::getFileUniqueIdentity(const char* path, ISlangBlob** outUniqueIdentity)
{
return getPath(PathKind::Canonical, path, outUniqueIdentity);
}
SlangResult MemoryFileSystem::calcCombinedPath(SlangPathType fromPathType, const char* fromPath, const char* path, ISlangBlob** pathOut)
{
String combinedPath;
switch (fromPathType)
{
case SLANG_PATH_TYPE_FILE:
{
combinedPath = Path::combine(Path::getParentDirectory(fromPath), path);
break;
}
case SLANG_PATH_TYPE_DIRECTORY:
{
combinedPath = Path::combine(fromPath, path);
break;
}
}
*pathOut = StringBlob::moveCreate(combinedPath).detach();
return SLANG_OK;
}
SlangResult MemoryFileSystem::getPathType(const char* path, SlangPathType* outPathType)
{
if (auto entry = _getEntryFromPath(path))
{
*outPathType = entry->m_type;
return SLANG_OK;
}
// Not found
return SLANG_E_NOT_FOUND;
}
SlangResult MemoryFileSystem::getPath(PathKind kind, const char* path, ISlangBlob** outPath)
{
switch (kind)
{
case PathKind::Simplified:
{
String simplifiedPath = Path::simplify(path);
*outPath = StringBlob::moveCreate(simplifiedPath).detach();
return SLANG_OK;
}
case PathKind::Display:
case PathKind::Canonical:
{
StringBuilder buffer;
SLANG_RETURN_ON_FAIL(_getCanonical(path, buffer));
*outPath = StringBlob::moveCreate(buffer).detach();
return SLANG_OK;
}
default: break;
}
return SLANG_E_NOT_AVAILABLE;
}
SlangResult MemoryFileSystem::enumeratePathContents(const char* path, FileSystemContentsCallBack callback, void* userData)
{
String canonicalPath;
Entry* entry = _getEntryFromPath(path, &canonicalPath);
if (!entry || entry->m_type != SLANG_PATH_TYPE_DIRECTORY)
{
return SLANG_E_NOT_FOUND;
}
ImplicitDirectoryCollector collector(canonicalPath, true);
// If it is a directory, we need to see if there is anything in it
for (const auto& [_, childEntry] : m_entries)
{
collector.addPath(childEntry.m_type, childEntry.m_canonicalPath.getUnownedSlice());
}
return collector.enumerate(callback, userData);
}
SlangResult MemoryFileSystem::saveFile(const char* path, const void* data, size_t size)
{
Entry* entry;
SLANG_RETURN_ON_FAIL(_requireFile(path, &entry));
auto contents = RawBlob::create(data, size);
entry->setContents(size, contents);
return SLANG_OK;
}
SlangResult MemoryFileSystem::saveFileBlob(const char* path, ISlangBlob* dataBlob)
{
if (!dataBlob)
{
return SLANG_E_INVALID_ARG;
}
Entry* entry;
SLANG_RETURN_ON_FAIL(_requireFile(path, &entry));
entry->setContents(dataBlob->getBufferSize(), dataBlob);
return SLANG_OK;
}
SlangResult MemoryFileSystem::_getCanonical(const char* path, StringBuilder& outCanonicalPath)
{
StringBuilder canonicalPath;
SLANG_RETURN_ON_FAIL(Path::simplify(UnownedStringSlice(path), Path::SimplifyStyle::AbsoluteOnlyAndNoRoot, outCanonicalPath));
return SLANG_OK;
}
SlangResult MemoryFileSystem::_getCanonicalWithExistingParent(const char* path, StringBuilder& outCanonicalPath)
{
SLANG_RETURN_ON_FAIL(_getCanonical(path, outCanonicalPath));
// Get the parent to the canoncial path (which should be canonical itself)
auto parent = Path::getParentDirectory(outCanonicalPath);
if (parent.getLength())
{
// The parent has to be a directory
Entry* parentEntry = _getEntryFromCanonicalPath(parent);
if (parentEntry == nullptr || parentEntry->m_type != SLANG_PATH_TYPE_DIRECTORY)
{
return SLANG_E_NOT_FOUND;
}
}
return SLANG_OK;
}
SlangResult MemoryFileSystem::_requireFile(const char* path, Entry** outEntry)
{
*outEntry = nullptr;
StringBuilder canonicalPath;
SLANG_RETURN_ON_FAIL(_getCanonicalWithExistingParent(path, canonicalPath));
Entry* foundEntry = _getEntryFromCanonicalPath(canonicalPath);
if (foundEntry)
{
if (foundEntry->m_type != SLANG_PATH_TYPE_FILE)
{
// Can only set if it's already a file, if it's anything else it's an error
return SLANG_FAIL;
}
}
else
{
Entry entry;
entry.initFile(canonicalPath);
m_entries.add(canonicalPath, entry);
foundEntry = _getEntryFromCanonicalPath(canonicalPath);
}
// It must be found and be a file
SLANG_ASSERT(foundEntry && foundEntry->m_type == SLANG_PATH_TYPE_FILE && foundEntry->m_canonicalPath == canonicalPath);
*outEntry = foundEntry;
return SLANG_OK;
}
SlangResult MemoryFileSystem::remove(const char* path)
{
String canonicalPath;
Entry* entry = _getEntryFromPath(path, &canonicalPath);
// If there is an entry and not the root of the file system
if (entry && entry != &m_rootEntry)
{
if (entry->m_type == SLANG_PATH_TYPE_DIRECTORY)
{
ImplicitDirectoryCollector collector(canonicalPath);
// If it is a directory, we need to see if there is anything in it
for (const auto& [_, childEntry] : m_entries)
{
collector.addPath(childEntry.m_type, childEntry.m_canonicalPath.getUnownedSlice());
if (collector.hasContent())
{
// Directory is not empty
return SLANG_FAIL;
}
}
}
// Reset so doesn't hold references/keep memory in scope
entry->reset();
m_entries.remove(canonicalPath);
return SLANG_OK;
}
return SLANG_E_NOT_FOUND;
}
SlangResult MemoryFileSystem::createDirectory(const char* path)
{
StringBuilder canonicalPath;
SLANG_RETURN_ON_FAIL(_getCanonicalWithExistingParent(path, canonicalPath));
if (_getEntryFromCanonicalPath(canonicalPath))
{
return SLANG_FAIL;
}
Entry entry;
entry.initDirectory(canonicalPath);
m_entries.add(canonicalPath, entry);
return SLANG_OK;
}
} // namespace Slang
|