https://github.com/lesomnus/vfs
Virtual File System with `std::filesystem` API.
https://github.com/lesomnus/vfs
filesystem fs memfs unionfs vfs
Last synced: about 1 year ago
JSON representation
Virtual File System with `std::filesystem` API.
- Host: GitHub
- URL: https://github.com/lesomnus/vfs
- Owner: lesomnus
- License: apache-2.0
- Created: 2023-05-11T16:53:19.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-12T09:19:46.000Z (almost 3 years ago)
- Last Synced: 2025-03-30T01:04:49.650Z (about 1 year ago)
- Topics: filesystem, fs, memfs, unionfs, vfs
- Language: C++
- Homepage:
- Size: 289 KB
- Stars: 21
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vfs
[](https://github.com/lesomnus/vfs/actions/workflows/test.yaml)
[](https://app.codacy.com/gh/lesomnus/vfs/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
[](https://app.codacy.com/gh/lesomnus/vfs/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
Virtual File System that provides interface of `std::filesystem`.
## Example
```cpp
#include
#include
#include
#include
void work(vfs::Fs& fs) {
fs.create_directories("foo/bar");
fs.create_symlink("foo/bar", "baz");
*fs.open_write("baz/food") << "Royale with cheese";
}
int main(int argc, char* argv[]) {
auto sandbox = vfs::make_vfs();
work(*sandbox);
std::string line;
std::getline(*sandbox->open_read("foo/bar/food"), line);
assert(line == "Royale with cheese");
return 0;
}
```
## Features
### File Systems
- `vfs::make_os_fs` Proxy of `std::filesystem`.
- `vfs::make_vfs` Basic file system.
- `vfs::make_mem_fs` Files are stored on the memory.
- `vfs::make_union_fs` Provides a single coherent file system over multiple file systems.
- `vfs::make_read_only_fs` Makes the given file system read-only.
### Utilities
- `vfs::Fs::change_root` Changes the root directory.
- `vfs::Fs::mount` Mounts different file system.
- `vfs::Fs::copy` Copies a file between file systems.
## About Current Working Directory
```cpp
#include
#include
#include
int main(int argc, char* argv[]) {
// Assume a directory "/tmp/foo/" and a regular file "/tmp/bar/foo" exist.
{
namespace fs = std::filesystem;
fs::current_path("/tmp");
assert(fs::is_directory("foo")); // References "/tmp/foo/".
fs::current_path("/tmp/bar");
assert(not fs::is_directory("foo")); // References "/tmp/bar/foo".
}
{
auto const fs = vfs::make_os_fs();
auto const tmp = fs->current_path("/tmp");
assert(tmp->is_directory("foo")); // References "/tmp/foo/".
auto const bar = fs->current_path("/tmp/bar");
assert(not bar->is_directory("foo")); // References "/tmp/bar/foo".
assert(tmp->is_directory("foo")); // References "/tmp/foo/".
}
return 0;
}
```
Standard `std::filesystem::current_path(std::filesystem::path const& p)` changes the Current Working Directory (CWD), which can have an impact on any code in the current process that relies on relative paths, potentially causing unintended results.
In *vfs*, since the file system is an object, each `vfs::Fs` holds its own CWD. `vfs::Fs::current_path(...)` returns a new instance that references the same file system but has a different CWD, instead of replacing the CWD of the existing instance.
## Mount
```cpp
#include
#include
#include
#include
#include
int main(int argc, char* argv[]) {
namespace fs = std::filesystem;
auto const sandbox = vfs::make_vfs();
sandbox->create_directories("foo/bar");
sandbox->mount("foo/bar", *vfs::make_os_fs(), "/tmp");
*sandbox->open_write("foo/bar/food") << "Royale with cheese";
asser(sandbox->exists("foo/bar/food"));
asser(fs::exists("/tmp/food"));
{
std::string line;
std::getline(*sandbox->open_read("foo/bar/food"), line);
asser(line == "Royale with cheese");
}
{
std::ifstream food("/tmp/food");
std::string line;
std::getline(food, line);
asser(line == "Royale with cheese");
}
sandbox->unmount("foo/bar");
asser(not sandbox->exists("foo/bar/food"));
asser(fs::exists("/tmp/food"));
return 0;
}
```
`Fs::mount` is similar to a bind mount.
It allows you to mount a file from a filesystem to a different path or mount a file from a different filesystem.