// Copyright 2021 The Emscripten Authors. All rights reserved. // Emscripten is available under two separate licenses, the MIT license and the // University of Illinois/NCSA Open Source License. Both these licenses can be // found in the LICENSE file. // This file defines the global state of the new file system. // Current Status: Work in Progress. // See https://github.com/emscripten-core/emscripten/issues/15041. #pragma once #include "backend.h" #include "file.h" #include "file_table.h" #include #include #include #include #include #include namespace wasmfs { class WasmFS { std::vector> backendTable; FileTable fileTable; std::shared_ptr rootDirectory; std::shared_ptr cwd; std::mutex mutex; // Private method to initialize root directory once. // Initializes default directories including dev/stdin, dev/stdout, // dev/stderr. Refers to the same std streams in the open file table. std::shared_ptr initRootDirectory(); // Initialize files specified by --preload-file option. void preloadFiles(); public: WasmFS(); ~WasmFS(); FileTable& getFileTable() { return fileTable; } std::shared_ptr getRootDirectory() { return rootDirectory; }; std::shared_ptr getCWD() { const std::lock_guard lock(mutex); return cwd; }; void setCWD(std::shared_ptr directory) { const std::lock_guard lock(mutex); cwd = directory; }; backend_t addBackend(std::unique_ptr backend) { const std::lock_guard lock(mutex); backendTable.push_back(std::move(backend)); return backendTable.back().get(); } }; // Global state instance. extern WasmFS wasmFS; } // namespace wasmfs