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
use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::{create_dir_all, File};
use std::io::{BufRead, Cursor, Result as IoResult, Seek, Write};
use std::io::{BufReader, BufWriter, Error as IoError, ErrorKind};
use std::path::{Path, PathBuf};
use std::rc::Rc;

pub trait ReadSeek: Seek + BufRead {}
impl<R: BufRead + Seek> ReadSeek for R {}

#[derive(Clone)]
pub struct RealFileSystem {
    pub root: PathBuf,
}

#[derive(Clone, Debug)]
pub struct FakeFileSystem {
    root: PathBuf,
    mapping: Rc<RefCell<HashMap<PathBuf, Vec<u8>>>>,
}

pub trait FileSystem {
    fn duplicate(&self) -> Box<FileSystem>;
    fn subsystem(&self, path: &Path) -> Box<FileSystem>;
    fn exists(&self, path: &Path) -> bool;
    fn read(&self, path: &Path, f: &mut FnMut(&mut ReadSeek) -> IoResult<()>) -> IoResult<()>;
    fn write(&self, path: &Path, f: &mut FnMut(&mut Write) -> IoResult<()>) -> IoResult<()>;
    fn full_path_for(&self, path: &Path) -> PathBuf;
    fn files(&self) -> Vec<PathBuf>;
    fn remove(&self, path: &Path) -> IoResult<()>;
    fn is_empty(&self) -> bool {
        self.files().is_empty()
    }
    fn copy(&self, from: &Path, to: &Path) -> IoResult<()> {
        self.write(to, &mut |writer| {
            self.read(from, &mut |reader| {
                ::std::io::copy(reader, writer).map(|_| ())
            })
        })
    }
}

impl FakeFileSystem {
    pub fn new() -> Self {
        FakeFileSystem {
            root: PathBuf::from("/"),
            mapping: Rc::new(RefCell::new(HashMap::new())),
        }
    }
}

impl FileSystem for RealFileSystem {
    fn subsystem(&self, path: &Path) -> Box<FileSystem> {
        assert!(path.is_relative(), "path must be relative");
        let mut new = self.clone();
        new.root.push(path);
        Box::new(new)
    }

    fn remove(&self, path: &Path) -> IoResult<()> {
        let path = self.root.join(path);
        ::std::fs::remove_file(path)
    }

    fn duplicate(&self) -> Box<FileSystem> {
        Box::new(self.clone())
    }

    fn exists(&self, path: &Path) -> bool {
        let path = self.root.join(path);
        path.exists()
    }

    fn read(&self, path: &Path, f: &mut FnMut(&mut ReadSeek) -> IoResult<()>) -> IoResult<()> {
        let path = self.root.join(path);
        match File::open(path) {
            Ok(file) => {
                let mut file = BufReader::new(file);
                f(&mut file)
            }
            Err(e) => Err(e),
        }
    }

    fn write(&self, path: &Path, f: &mut FnMut(&mut Write) -> IoResult<()>) -> IoResult<()> {
        let path = self.root.join(path);
        create_dir_all(path.parent().unwrap())?;

        match File::create(path) {
            Ok(file) => {
                let mut file = BufWriter::new(file);
                f(&mut file)
            }
            Err(e) => Err(e),
        }
    }

    fn full_path_for(&self, path: &Path) -> PathBuf {
        self.root.join(path)
    }

    fn files(&self) -> Vec<PathBuf> {
        ::walkdir::WalkDir::new(&self.root)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().is_file())
            .map(|p| p.path().to_owned())
            .filter_map(|p| p.strip_prefix(&self.root).ok().map(|p| p.to_owned()))
            .collect()
    }
}

impl FileSystem for FakeFileSystem {
    fn subsystem(&self, path: &Path) -> Box<FileSystem> {
        assert!(path.is_relative(), "path must be relative");
        let mut new = self.clone();
        new.root.push(path);
        Box::new(new)
    }
    fn duplicate(&self) -> Box<FileSystem> {
        Box::new(self.clone())
    }

    fn exists(&self, path: &Path) -> bool {
        let path = self.root.join(path);
        self.mapping.borrow().contains_key(&path)
    }

    fn remove(&self, path: &Path) -> IoResult<()> {
        let path = self.root.join(path);
        self.mapping.borrow_mut().remove(&path);
        Ok(())
    }

    fn read(&self, path: &Path, f: &mut FnMut(&mut ReadSeek) -> IoResult<()>) -> IoResult<()> {
        let path = self.root.join(path);

        let contents = match self.mapping.borrow().get(&path) {
            Some(contents) => contents.clone(),
            None => {
                return Err(IoError::new(
                    ErrorKind::NotFound,
                    format!("{:?} does not exist", path),
                ))
            }
        };

        f(&mut Cursor::new(&contents[..]))
    }

    fn write(&self, path: &Path, f: &mut FnMut(&mut Write) -> IoResult<()>) -> IoResult<()> {
        let path = self.root.join(path);

        let mut contents = vec![];
        f(&mut contents)?;

        self.mapping.borrow_mut().insert(path, contents);
        Ok(())
    }

    fn full_path_for(&self, path: &Path) -> PathBuf {
        self.root.join(path)
    }

    fn files(&self) -> Vec<PathBuf> {
        let root = self.root.clone();
        self.mapping
            .borrow()
            .keys()
            .filter_map(|p| p.strip_prefix(&root).ok())
            .map(|p| p.into())
            .collect()
    }
}