Gaia API GitHub

Memory file system

gaia-memfs is a file system that lives in memory until it is closed, for tests or scratch work. It supports the whole java.nio.file API, so code written against Path and Files runs on it unchanged.

<dependency>
  <groupId>be.imgn.gaia</groupId>
  <artifactId>gaia-memfs</artifactId>
</dependency>

Creating a file system

MemoryFileSystems creates file systems. Their files are gone once they are closed:

try (FileSystem fileSystem = MemoryFileSystems.newFileSystem()) {
    Path file = fileSystem.getPath("/work/hello.txt");
    Files.createDirectories(file.getParent());
    Files.writeString(file, "Hello");
}

Each file system has a name, unique unless given, and its paths have URIs of the mem scheme with that name: mem://test/work/hello.txt for the path /work/hello.txt of the file system test, or mem://test/C:/work/hello.txt for C:\work\hello.txt on a Windows-style one. While the file system is open, Path.of(uri) finds its paths from such URIs, and FileSystems.getFileSystem(URI.create("mem://test")) finds it. FileSystems.newFileSystem(URI.create("mem://test"), env) creates one too, given its configuration under the key "configuration".

Everything is supported, except Path.toFile() and FileChannel.map(), which can't work on files in memory:

The file systems are thread-safe.

Configuration

A Configuration sets how a file system behaves. Three presets look like real systems:

Preset Root Working directory Names Attribute views
unix() / /work matched exactly basic, owner, posix, unix
macOS() / /work case ignored, Unicode normalized to NFD basic, owner, posix, unix
windows() C:\ C:\work case ignored basic, owner, dos, acl, user

forCurrentPlatform() picks the one that looks like the operating system the JVM runs on.

toBuilder() changes a configuration, and newBuilder(PathStyle) starts one from scratch:

Configuration small = Configuration.windows().toBuilder()
        .roots("C:\\", "D:\\")
        .maxSize(16 * 1024 * 1024)
        .currentUser("alice", "staff")
        .build();
FileSystem fileSystem = MemoryFileSystems.newFileSystem(small);

The builder sets:

Permissions

Permissions are always stored. They are enforced only when a current user is configured, with currentUser("alice", "staff"): then the POSIX permissions of the posix view, the read-only flag of the dos view and the entries of a non-empty access control list of the acl view are checked against that user and their groups, and a denied operation throws AccessDeniedException. The user root is never denied.