Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/wasmfs/wasmfs.h
6174 views
1
// Copyright 2021 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
// This file defines the global state.
7
8
#pragma once
9
10
#include "backend.h"
11
#include "file.h"
12
#include "file_table.h"
13
#include <assert.h>
14
#include <emscripten/html5.h>
15
#include <mutex>
16
#include <sys/stat.h>
17
#include <vector>
18
#include <wasi/api.h>
19
20
namespace wasmfs {
21
22
class WasmFS {
23
24
std::vector<std::unique_ptr<Backend>> backendTable;
25
FileTable fileTable;
26
std::shared_ptr<Directory> rootDirectory;
27
std::shared_ptr<Directory> cwd;
28
std::mutex mutex;
29
30
// Private method to initialize root directory once.
31
// Initializes default directories including dev/stdin, dev/stdout,
32
// dev/stderr. Refers to the same std streams in the open file table.
33
std::shared_ptr<Directory> initRootDirectory();
34
35
// Initialize files specified by --preload-file option.
36
void preloadFiles();
37
38
public:
39
WasmFS();
40
~WasmFS();
41
42
FileTable& getFileTable() { return fileTable; }
43
44
std::shared_ptr<Directory> getRootDirectory() { return rootDirectory; };
45
46
std::shared_ptr<Directory> getCWD() {
47
const std::lock_guard<std::mutex> lock(mutex);
48
return cwd;
49
};
50
51
void setCWD(std::shared_ptr<Directory> directory) {
52
const std::lock_guard<std::mutex> lock(mutex);
53
cwd = directory;
54
};
55
56
backend_t addBackend(std::unique_ptr<Backend> backend) {
57
const std::lock_guard<std::mutex> lock(mutex);
58
backendTable.push_back(std::move(backend));
59
return backendTable.back().get();
60
}
61
};
62
63
// Global state instance.
64
extern WasmFS wasmFS;
65
66
} // namespace wasmfs
67
68