1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//! **mini-fs** is a filesystem abstraction layer.
//!
//! Currently supported features include:
//!
//! - Access to the local (native) filesystem.
//! - In-memory filesystems.
//! - Tar, tar.gz, and Zip archives.
//! - Logically merging filesystems (useful for overriding some files)
//!
//! ## Merging example
//!
//! This feature is useful when you want to override some files from another
//! location:
//!
//! ```no_run
//! # fn main() -> Result<(), mini_fs::err::Error> {
//! use std::path::Path;
//! # use mini_fs::{Store, Local, Tar, MiniFs, err::Error};
//! let a = Local::new("data/");
//! // |- example.txt
//!
//! let b = Tar::open("archive.tar.gz")?;
//! // |- example.txt
//! // |- hello.txt
//!
//! let files = MiniFs::new().mount("/files", (a, b));
//!
//! assert!(files.open(Path::new("/files/example.txt")).is_ok()); // this "example.txt" is from "a"
//! assert!(files.open(Path::new("/files/hello.txt")).is_ok());
//! # Ok(())
//! # }
//! ```
use std::collections::BTreeMap;
use std::collections::LinkedList;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};

use err::{Error, Result};
pub use file::File;
#[cfg(feature = "tar")]
pub use tar::Tar;
#[cfg(feature = "zip")]
pub use zip::Zip;

/// Error types.
pub mod err;
mod file;
/// Storage from a tarball.
///
/// *To use this module you must enable the "tar" feature.*
#[cfg(feature = "tar")]
pub mod tar;
/// Storage from a Zip file.
///
/// *To use this module you must enable the "zip" feature.*
#[cfg(feature = "zip")]
pub mod zip;

/// Generic filesystem abstraction.
pub trait Store {
    /// Opens file in read-only mode.
    fn open(&self, path: &Path) -> Result<File>;

    /// Opens a file in write-only mode.
    #[allow(unused_variables)]
    fn open_write(&self, path: &Path) -> Result<File> {
        Err(Error::Io(file::write_support_err()))
    }
}

/// Local (native) filesystem.
pub struct Local {
    root: PathBuf,
}

impl Store for Local {
    fn open(&self, path: &Path) -> Result<File> {
        let file = fs::File::open(self.root.join(path))?;
        Ok(File::from_fs(file))
    }

    fn open_write(&self, path: &Path) -> Result<File> {
        let file = fs::OpenOptions::new()
            .write(true)
            // FileMut doesn't impl Read
            .read(false)
            .create(false)
            .open(path)?;

        Ok(File::from_fs(file))
    }
}

impl Local {
    pub fn new<P: Into<PathBuf>>(root: P) -> Self {
        Self { root: root.into() }
    }

    /// Point to the current working directory.
    pub fn pwd() -> Result<Self> {
        Ok(Self::new(env::current_dir()?))
    }
}

/// In-memory file store.
#[derive(Clone)]
pub struct Ram {
    inner: BTreeMap<PathBuf, Vec<u8>>,
}

impl Store for Ram {
    fn open(&self, path: &Path) -> Result<File> {
        self.inner
            .get(path)
            .map(|b| File::from_ram(b))
            .ok_or_else(|| Error::FileNotFound)
    }
}

impl Ram {
    pub fn new() -> Self {
        Self {
            inner: BTreeMap::new(),
        }
    }

    pub fn clear(&mut self) {
        self.inner.clear();
    }

    pub fn touch<P, F>(&mut self, path: P, file: F)
    where
        P: Into<PathBuf>,
        F: Into<Vec<u8>>,
    {
        self.inner.insert(path.into(), file.into());
    }
}

struct Mount {
    path: PathBuf,
    store: Box<dyn Store>,
}

/// Virtual filesystem.
pub struct MiniFs {
    inner: LinkedList<Mount>,
}

impl MiniFs {
    pub fn new() -> Self {
        Self {
            inner: LinkedList::new(),
        }
    }

    pub fn mount<P, F>(mut self, path: P, store: F) -> Self
    where
        P: Into<PathBuf>,
        F: Store + Send + 'static,
    {
        let path = path.into();
        let store = Box::new(store);
        self.inner.push_back(Mount { path, store });
        self
    }

    /// Unmounts store mounted at the given location.
    ///
    /// Returns the unmounted store if the given path was a valid mounting
    /// point. Returns `None` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// # use mini_fs::{Local, MiniFs};
    /// let a = Local::new("/");
    /// let b = Local::new("/etc");
    ///
    /// let mut fs = MiniFs::new().mount("/", a).mount("/etc", b);
    ///
    /// assert!(fs.umount("/etc").is_some());
    /// assert!(fs.umount("/etc").is_none());
    /// ```
    pub fn umount<P: AsRef<Path>>(&mut self, path: P) -> Option<Box<dyn Store>> {
        let path = path.as_ref();
        if let Some(p) = self.inner.iter().rposition(|p| p.path == path) {
            let mut tail = self.inner.split_off(p);
            let fs = tail.pop_front().map(|m| m.store);
            self.inner.append(&mut tail);
            fs
        } else {
            None
        }
    }
}

impl Store for MiniFs {
    fn open(&self, path: &Path) -> Result<File> {
        let next = self.inner.iter().rev().find_map(|mnt| {
            if let Ok(np) = path.strip_prefix(&mnt.path) {
                Some((np, &mnt.store))
            } else {
                None
            }
        });
        if let Some((np, store)) = next {
            store.open(np)
        } else {
            Err(Error::FileNotFound)
        }
    }
}

macro_rules! tuples {
    ($head:ident,) => {};
    ($head:ident, $($tail:ident,)+) => {
        impl<$head, $($tail,)+> Store for ($head, $($tail,)+)
        where
            $head: Store,
            $($tail: Store,)+
        {
            #[allow(non_snake_case)]
            fn open(&self, path: &Path) -> Result<File> {
                let ($head, $($tail,)+) = self;
                match $head.open(path) {
                    Ok(file) => return Ok(file),
                    Err(Error::FileNotFound) => {}
                    Err(err) => return Err(err),
                }
                $(
                match $tail.open(path) {
                    Ok(file) => return Ok(file),
                    Err(Error::FileNotFound) => {}
                    Err(err) => return Err(err),
                }
                )+
                Err(Error::FileNotFound)
            }
        }
        tuples!($($tail,)+);
    };
}

// implement for tuples of up to size 8
tuples! { A, B, C, D, E, F, G, H, }

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}