vfs 0.13.0

A virtual filesystem for Rust
Documentation
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
//! An overlay file system combining two filesystems, an upper layer with read/write access and a lower layer with only read access

use crate::async_vfs::{AsyncFileSystem, AsyncVfsPath, SeekAndRead};
use crate::error::VfsErrorKind;
use crate::{VfsMetadata, VfsResult};

use async_std::io::Write;
use async_trait::async_trait;
use futures::stream::{Stream, StreamExt};
use std::collections::HashSet;
use std::time::SystemTime;

/// An overlay file system combining several filesystems into one, an upper layer with read/write access and lower layers with only read access
///
/// Files in upper layers shadow those in lower layers. Directories are the merged view of all layers.
///
/// NOTE: To allow removing files and directories (e.g. via remove_file()) from the lower layer filesystems, this mechanism creates a `.whiteout` folder in the root of the upper level filesystem to mark removed files
///
#[derive(Debug, Clone)]
pub struct AsyncOverlayFS {
    layers: Vec<AsyncVfsPath>,
}

impl AsyncOverlayFS {
    /// Create a new overlay FileSystem from the given layers, only the first layer is written to
    pub fn new(layers: &[AsyncVfsPath]) -> Self {
        if layers.is_empty() {
            panic!("AsyncOverlayFS needs at least one layer")
        }
        AsyncOverlayFS {
            layers: layers.to_vec(),
        }
    }

    fn write_layer(&self) -> &AsyncVfsPath {
        &self.layers[0]
    }

    async fn read_path(&self, path: &str) -> VfsResult<AsyncVfsPath> {
        if path.is_empty() {
            return Ok(self.layers[0].clone());
        }
        if self.whiteout_path(path)?.exists().await? {
            return Err(VfsErrorKind::FileNotFound.into());
        }
        for layer in &self.layers {
            let layer_path = layer.join(&path[1..])?;
            if layer_path.exists().await? {
                return Ok(layer_path);
            }
        }
        let read_path = self.write_layer().join(&path[1..])?;
        if !read_path.exists().await? {
            return Err(VfsErrorKind::FileNotFound.into());
        }
        Ok(read_path)
    }

    fn write_path(&self, path: &str) -> VfsResult<AsyncVfsPath> {
        if path.is_empty() {
            return Ok(self.layers[0].clone());
        }
        self.write_layer().join(&path[1..])
    }

    fn whiteout_path(&self, path: &str) -> VfsResult<AsyncVfsPath> {
        if path.is_empty() {
            return self.write_layer().join(".whiteout/_wo");
        }
        self.write_layer()
            .join(format!(".whiteout/{}_wo", &path[1..]))
    }

    async fn ensure_has_parent(&self, path: &str) -> VfsResult<()> {
        let separator = path.rfind('/');
        if let Some(index) = separator {
            let parent_path = &path[..index];
            if self.exists(parent_path).await? {
                self.write_path(parent_path)?.create_dir_all().await?;
                return Ok(());
            }
        }
        Err(VfsErrorKind::Other("Parent path does not exist".into()).into())
    }
}

#[async_trait]
impl AsyncFileSystem for AsyncOverlayFS {
    async fn read_dir(
        &self,
        path: &str,
    ) -> VfsResult<Box<dyn Stream<Item = String> + Send + Unpin>> {
        let actual_path = if !path.is_empty() { &path[1..] } else { path };
        if !self.read_path(path).await?.exists().await? {
            return Err(VfsErrorKind::FileNotFound.into());
        }
        let mut entries = HashSet::<String>::new();
        for layer in &self.layers {
            let layer_path = layer.join(actual_path)?;
            if layer_path.exists().await? {
                let mut path_stream = layer_path.read_dir().await?;
                while let Some(path) = path_stream.next().await {
                    entries.insert(path.filename());
                }
            }
        }
        // remove whiteout entries that have been removed
        let whiteout_path = self.write_layer().join(format!(".whiteout{path}"))?;
        if whiteout_path.exists().await? {
            let mut path_stream = whiteout_path.read_dir().await?;
            while let Some(path) = path_stream.next().await {
                let filename = path.filename();
                if filename.ends_with("_wo") {
                    entries.remove(&filename[..filename.len() - 3]);
                }
            }
        }
        Ok(Box::new(futures::stream::iter(entries)))
    }

    async fn create_dir(&self, path: &str) -> VfsResult<()> {
        self.ensure_has_parent(path).await?;
        self.write_path(path)?.create_dir().await?;
        let whiteout_path = self.whiteout_path(path)?;
        if whiteout_path.exists().await? {
            whiteout_path.remove_file().await?;
        }
        Ok(())
    }

    async fn open_file(&self, path: &str) -> VfsResult<Box<dyn SeekAndRead + Send + Unpin>> {
        self.read_path(path).await?.open_file().await
    }

    async fn create_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send + Unpin>> {
        self.ensure_has_parent(path).await?;
        let result = self.write_path(path)?.create_file().await?;
        let whiteout_path = self.whiteout_path(path)?;
        if whiteout_path.exists().await? {
            whiteout_path.remove_file().await?;
        }
        Ok(result)
    }

    async fn append_file(&self, path: &str) -> VfsResult<Box<dyn Write + Send + Unpin>> {
        let write_path = self.write_path(path)?;
        if !write_path.exists().await? {
            self.ensure_has_parent(path).await?;
            self.read_path(path).await?.copy_file(&write_path).await?;
        }
        write_path.append_file().await
    }

    async fn metadata(&self, path: &str) -> VfsResult<VfsMetadata> {
        self.read_path(path).await?.metadata().await
    }

    async fn set_creation_time(&self, path: &str, time: SystemTime) -> VfsResult<()> {
        self.write_path(path)?.set_creation_time(time).await
    }

    async fn set_modification_time(&self, path: &str, time: SystemTime) -> VfsResult<()> {
        self.write_path(path)?.set_modification_time(time).await
    }

    async fn set_access_time(&self, path: &str, time: SystemTime) -> VfsResult<()> {
        self.write_path(path)?.set_access_time(time).await
    }

    async fn exists(&self, path: &str) -> VfsResult<bool> {
        if self
            .whiteout_path(path)
            .map_err(|err| err.with_context(|| "whiteout_path"))?
            .exists()
            .await?
        {
            return Ok(false);
        }
        match self.read_path(path).await {
            Ok(p) => p.exists().await,
            Err(_) => Ok(false),
        }
    }

    async fn remove_file(&self, path: &str) -> VfsResult<()> {
        // Ensure path exists
        self.read_path(path).await?;
        let write_path = self.write_path(path)?;
        if write_path.exists().await? {
            write_path.remove_file().await?;
        }
        let whiteout_path = self.whiteout_path(path)?;
        whiteout_path.parent().create_dir_all().await?;
        whiteout_path.create_file().await?;
        Ok(())
    }

    async fn remove_dir(&self, path: &str) -> VfsResult<()> {
        // Ensure path exists
        self.read_path(path).await?;
        let write_path = self.write_path(path)?;
        if write_path.exists().await? {
            write_path.remove_dir().await?;
        }
        let whiteout_path = self.whiteout_path(path)?;
        whiteout_path.parent().create_dir_all().await?;
        whiteout_path.create_file().await?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::async_vfs::AsyncMemoryFS;

    use async_std::io::WriteExt;
    use futures::stream::StreamExt;

    test_async_vfs!({
        let upper_root: AsyncVfsPath = AsyncMemoryFS::new().into();
        let lower_root: AsyncVfsPath = AsyncMemoryFS::new().into();
        AsyncOverlayFS::new(&[upper_root, lower_root])
    });

    fn create_roots() -> (AsyncVfsPath, AsyncVfsPath, AsyncVfsPath) {
        let lower_root: AsyncVfsPath = AsyncMemoryFS::new().into();
        let upper_root: AsyncVfsPath = AsyncMemoryFS::new().into();
        let overlay_root: AsyncVfsPath =
            AsyncOverlayFS::new(&[upper_root.clone(), lower_root.clone()]).into();
        (lower_root, upper_root, overlay_root)
    }

    #[tokio::test]
    async fn read() -> VfsResult<()> {
        let (lower_root, upper_root, overlay_root) = create_roots();
        let lower_path = lower_root.join("foo.txt")?;
        let upper_path = upper_root.join("foo.txt")?;
        let overlay_path = overlay_root.join("foo.txt")?;
        lower_path
            .create_file()
            .await?
            .write_all(b"Hello Lower")
            .await?;
        assert_eq!(&overlay_path.read_to_string().await?, "Hello Lower");
        upper_path
            .create_file()
            .await?
            .write_all(b"Hello Upper")
            .await?;
        assert_eq!(&overlay_path.read_to_string().await?, "Hello Upper");
        lower_path.remove_file().await?;
        assert_eq!(&overlay_path.read_to_string().await?, "Hello Upper");
        upper_path.remove_file().await?;
        assert!(
            !overlay_path.exists().await?,
            "File should not exist anymore"
        );
        Ok(())
    }

    #[tokio::test]
    async fn read_dir() -> VfsResult<()> {
        let (lower_root, upper_root, overlay_root) = create_roots();
        upper_root.join("foo/upper")?.create_dir_all().await?;
        upper_root.join("foo/common")?.create_dir_all().await?;
        lower_root.join("foo/common")?.create_dir_all().await?;
        lower_root.join("foo/lower")?.create_dir_all().await?;
        let entries: Vec<_> = overlay_root.join("foo")?.read_dir().await?.collect().await;
        let mut paths: Vec<_> = entries.iter().map(|path| path.as_str()).collect();
        paths.sort();
        assert_eq!(paths, vec!["/foo/common", "/foo/lower", "/foo/upper"]);
        Ok(())
    }

    #[tokio::test]
    async fn read_dir_root() -> VfsResult<()> {
        let (lower_root, upper_root, overlay_root) = create_roots();
        upper_root.join("upper")?.create_dir_all().await?;
        upper_root.join("common")?.create_dir_all().await?;
        lower_root.join("common")?.create_dir_all().await?;
        lower_root.join("lower")?.create_dir_all().await?;
        let entries: Vec<_> = overlay_root.read_dir().await?.collect().await;
        let mut paths: Vec<_> = entries.iter().map(|path| path.as_str()).collect();
        paths.sort();
        assert_eq!(paths, vec!["/common", "/lower", "/upper"]);
        Ok(())
    }

    #[tokio::test]
    async fn create_dir() -> VfsResult<()> {
        let (lower_root, _upper_root, overlay_root) = create_roots();
        lower_root.join("foo")?.create_dir_all().await?;
        assert!(
            overlay_root.join("foo")?.exists().await?,
            "dir should exist"
        );
        overlay_root.join("foo/bar")?.create_dir().await?;
        assert!(
            overlay_root.join("foo/bar")?.exists().await?,
            "dir should exist"
        );
        Ok(())
    }

    #[tokio::test]
    async fn create_file() -> VfsResult<()> {
        let (lower_root, _upper_root, overlay_root) = create_roots();
        lower_root.join("foo")?.create_dir_all().await?;
        assert!(
            overlay_root.join("foo")?.exists().await?,
            "dir should exist"
        );
        overlay_root.join("foo/bar")?.create_file().await?;
        assert!(
            overlay_root.join("foo/bar")?.exists().await?,
            "file should exist"
        );
        Ok(())
    }

    #[tokio::test]
    async fn append_file() -> VfsResult<()> {
        let (lower_root, _upper_root, overlay_root) = create_roots();
        lower_root.join("foo")?.create_dir_all().await?;
        lower_root
            .join("foo/bar.txt")?
            .create_file()
            .await?
            .write_all(b"Hello Lower\n")
            .await?;
        overlay_root
            .join("foo/bar.txt")?
            .append_file()
            .await?
            .write_all(b"Hello Overlay\n")
            .await?;
        assert_eq!(
            &overlay_root.join("foo/bar.txt")?.read_to_string().await?,
            "Hello Lower\nHello Overlay\n"
        );
        Ok(())
    }

    #[tokio::test]
    async fn remove_file() -> VfsResult<()> {
        let (lower_root, _upper_root, overlay_root) = create_roots();
        lower_root.join("foo")?.create_dir_all().await?;
        lower_root
            .join("foo/bar.txt")?
            .create_file()
            .await?
            .write_all(b"Hello Lower\n")
            .await?;
        assert!(
            overlay_root.join("foo/bar.txt")?.exists().await?,
            "file should exist"
        );

        overlay_root.join("foo/bar.txt")?.remove_file().await?;
        assert!(
            !overlay_root.join("foo/bar.txt")?.exists().await?,
            "file should not exist anymore"
        );

        overlay_root
            .join("foo/bar.txt")?
            .create_file()
            .await?
            .write_all(b"Hello Overlay\n")
            .await?;
        assert!(
            overlay_root.join("foo/bar.txt")?.exists().await?,
            "file should exist"
        );
        assert_eq!(
            &overlay_root.join("foo/bar.txt")?.read_to_string().await?,
            "Hello Overlay\n"
        );
        Ok(())
    }

    #[tokio::test]
    async fn remove_dir() -> VfsResult<()> {
        let (lower_root, _upper_root, overlay_root) = create_roots();
        lower_root.join("foo")?.create_dir_all().await?;
        lower_root.join("foo/bar")?.create_dir_all().await?;
        assert!(
            overlay_root.join("foo/bar")?.exists().await?,
            "dir should exist"
        );

        overlay_root.join("foo/bar")?.remove_dir().await?;
        assert!(
            !overlay_root.join("foo/bar")?.exists().await?,
            "dir should not exist anymore"
        );

        overlay_root.join("foo/bar")?.create_dir().await?;
        assert!(
            overlay_root.join("foo/bar")?.exists().await?,
            "dir should exist"
        );
        Ok(())
    }

    #[tokio::test]
    async fn read_dir_removed_entries() -> VfsResult<()> {
        let (lower_root, _upper_root, overlay_root) = create_roots();
        lower_root.join("foo")?.create_dir_all().await?;
        lower_root.join("foo/bar")?.create_dir_all().await?;
        lower_root.join("foo/bar.txt")?.create_dir_all().await?;

        let entries: Vec<_> = overlay_root.join("foo")?.read_dir().await?.collect().await;
        let mut paths: Vec<_> = entries.iter().map(|path| path.as_str()).collect();
        paths.sort();
        assert_eq!(paths, vec!["/foo/bar", "/foo/bar.txt"]);
        overlay_root.join("foo/bar")?.remove_dir().await?;
        overlay_root.join("foo/bar.txt")?.remove_file().await?;

        let entries: Vec<_> = overlay_root.join("foo")?.read_dir().await?.collect().await;
        let mut paths: Vec<_> = entries.iter().map(|path| path.as_str()).collect();
        paths.sort();
        assert_eq!(paths, vec![] as Vec<&str>);

        Ok(())
    }
}

#[cfg(test)]
mod tests_physical {
    use super::*;
    use crate::async_vfs::AsyncPhysicalFS;

    test_async_vfs!(futures::executor::block_on(async {
        let temp_dir = std::env::temp_dir();
        let dir = temp_dir.join(uuid::Uuid::new_v4().to_string());
        let lower_path = dir.join("lower");
        async_std::fs::create_dir_all(&lower_path).await.unwrap();
        let upper_path = dir.join("upper");
        async_std::fs::create_dir_all(&upper_path).await.unwrap();

        let upper_root: AsyncVfsPath = AsyncPhysicalFS::new(upper_path).into();
        let lower_root: AsyncVfsPath = AsyncPhysicalFS::new(lower_path).into();
        AsyncOverlayFS::new(&[upper_root, lower_root])
    }));
}