mini-fs

Crates.iomini-fs
lib.rsmini-fs
version0.2.2
sourcesrc
created_at2019-04-28 00:02:34.970416
updated_at2019-05-23 18:22:10.892482
descriptionApplication filesystem to simplify reading files from both the native filesystem and file archives (tar, tar.gz & zip).
homepage
repositoryhttps://github.com/germangb/mini-fs
max_upload_size
id130583
size65,500
german gömez (germangb)

documentation

README

mini-fs

Cargo package Build Status docs.rs docs Master docs

mini-fs is an extensible virtual filesystem for the application layer.

Supports reading from both the native filesystem, as well as Tar & Zip archives.

[dependencies]
mini-fs = "0.2"

An example showcasing the API:

use mini_fs::prelude::*;
use mini_fs::{LocalFs, TarFs, MiniFs};

// Declare some file systems.
let local = LocalFs::pwd()?;
let tar = TarFs::open("archive.tar.gz")?;

// Mount them.
let mut fs = MiniFs::new()
    .mount("/data", local)
    .mount("/archived", tar);

// To open (read) files:
let file = fs.open("/data/example.gif")?;

Overlay filesystem

You can merge multiple file systems so they share the same mount point using a tuple. This allows you to override files between locations.

Example use cases:

  • Config files with default fallbacks.
  • Replacing the assets from a game (modding).
let a = LocalFs::new("data/");
// |- example.txt

let b = TarFs::open("archive.tar.gz")?;
// |- example.txt
// |- hello.txt

let files = MiniFs::new().mount("/files", (a, b));

assert!(files.open("/files/example.txt").is_ok()); // this "example.txt" is from "a"
assert!(files.open("/files/hello.txt").is_ok());

Note that if you tried to first mount a, followed by b under the same mount point, the first one would be shadowed by b.

Extensible

It is possible to define a new file store so you can read files from an archive format that is not directly supported by this crate.

You'll need to:

  1. Define two types: the store itselt, and the type of file that it returns.
  2. Implement the Store trait on the first type, and UserFile on the second.

For example, say you want to implement a file store (MyZip) based on a Zip archive (this crate already has an implementation, but let's say you want to improve it).

First, define types for both the Storage and the File that it will return.

use std::io;
use mini_fs::{Store, UserFile};

// This is the filesystem that will be mounted.
struct MyZip { /*...*/ }

// This example MyZip implementatin will return a slice of bytes for each
// entry in the archive. It is wrapped in a Cursor to enable IO later.
struct MyZipEntry(io::Cursor<Box<[u8]>>);

impl UserFile for MyZipEntry {}

Implement IO on MyZipEntry.

impl io::Read for MyZipEntry {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.0.read(buf)
    }
}

impl io::Seek for MyZipEntry { /* skipped */ }

And Finally, implement the Store trait on MyZip

impl Store for MyZip {
    type File = MyZipEntry;
    
    fn open_path(&self, path: &Path) -> io::Result<MyZipEntry> {
        // Fetch file
        // ...
    }
}

And that is all. Now you can mount MyZip and/or use it as part of a tuple.

License

MIT License

Copyright (c) 2019 German Gomez Bajo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Commit count: 62

cargo fmt