22// Emscripten is available under two separate licenses, the MIT license and the
33// University of Illinois/NCSA Open Source License. Both these licenses can be
44// found in the LICENSE file.
5+
56// This file defines the file object of the new file system.
67// Current Status: Work in Progress.
78// See https://github.com/emscripten-core/emscripten/issues/15041.
@@ -25,6 +26,7 @@ class Backend;
2526// This represents an opaque pointer to a Backend. A user may use this to
2627// specify a backend in file operations.
2728using backend_t = Backend*;
29+ const backend_t NullBackend = nullptr ;
2830
2931class File : public std ::enable_shared_from_this<File> {
3032
@@ -62,6 +64,8 @@ class File : public std::enable_shared_from_this<File> {
6264 return (ino_t )this ;
6365 }
6466
67+ backend_t getBackend () { return backend; }
68+
6569 class Handle {
6670
6771 protected:
@@ -101,7 +105,8 @@ class File : public std::enable_shared_from_this<File> {
101105 }
102106
103107protected:
104- File (FileKind kind, mode_t mode) : kind(kind), mode(mode) {}
108+ File (FileKind kind, mode_t mode, backend_t backend)
109+ : kind(kind), mode(mode), backend(backend) {}
105110 // A mutex is needed for multiple accesses to the same file.
106111 std::recursive_mutex mutex;
107112
@@ -121,6 +126,9 @@ class File : public std::enable_shared_from_this<File> {
121126 // dependencies where the parent and child have shared_ptrs that reference
122127 // each other. This prevents the case in which an uncollectable cycle occurs.
123128 std::weak_ptr<File> parent;
129+
130+ // This specifies which backend a file is associated with.
131+ backend_t backend;
124132};
125133
126134class DataFile : public File {
@@ -131,7 +139,8 @@ class DataFile : public File {
131139
132140public:
133141 static constexpr FileKind expectedKind = File::DataFileKind;
134- DataFile (mode_t mode) : File(File::DataFileKind, mode) {}
142+ DataFile (mode_t mode, backend_t backend)
143+ : File(File::DataFileKind, mode, backend) {}
135144 virtual ~DataFile () = default ;
136145
137146 class Handle : public File ::Handle {
@@ -161,15 +170,10 @@ class Directory : public File {
161170 // This value was also copied from the existing file system.
162171 size_t getSize () override { return 4096 ; }
163172
164- // This specifies which backend a directory is associated with. By default,
165- // files and sub-directories added to this directory's entries will be created
166- // through this same backend unless an alternative is specified.
167- backend_t backend;
168-
169173public:
170174 static constexpr FileKind expectedKind = File::DirectoryKind;
171175 Directory (mode_t mode, backend_t backend)
172- : File(File::DirectoryKind, mode), backend( backend) {}
176+ : File(File::DirectoryKind, mode, backend) {}
173177
174178 struct Entry {
175179 std::string name;
@@ -231,8 +235,6 @@ class Directory : public File {
231235 return entries;
232236 }
233237
234- backend_t getBackend () { return getDir ()->backend ; }
235-
236238#ifdef WASMFS_DEBUG
237239 void printKeys () {
238240 for (auto keyPair : getDir ()->entries ) {
@@ -253,7 +255,6 @@ class Directory : public File {
253255 }
254256 }
255257};
256-
257258// Obtains parent directory of a given pathname.
258259// Will return a nullptr if the parent is not a directory.
259260// Will error if the forbiddenAncestor is encountered while processing.
@@ -265,8 +266,8 @@ getDir(std::vector<std::string>::iterator begin,
265266 long & err,
266267 std::shared_ptr<File> forbiddenAncestor = nullptr );
267268
268- // Return a vector of the '/'-delimited components of a path. The first element
269- // will be "/" iff the path is an absolute path.
269+ // Return a vector of the '/'-delimited components of a path. The first
270+ // element will be "/" iff the path is an absolute path.
270271std::vector<std::string> splitPath (char * pathname);
271272
272273} // namespace wasmfs
0 commit comments