Giter Site home page Giter Site logo

vfs's Introduction

vfs

test Codacy Badge Codacy Badge

Virtual File System that provides interface of std::filesystem.

Example

#include <cassert>
#include <memory>
#include <string>

#include <vfs.hpp>

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

#include <cassert>
#include <filesystem>

#include <vfs.hpp>

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

#include <cassert>
#include <filesystem>
#include <fstream>
#include <string>

#include <vfs.hpp>

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.