Allow turning off file watching in memofs.

Also preemptively bumping version to 0.1.3 so I don't forget on next release
This commit is contained in:
Lucien Greathouse
2020-06-17 14:06:44 -07:00
parent 486b067567
commit fc27b2911e
5 changed files with 38 additions and 5 deletions

2
Cargo.lock generated
View File

@@ -1083,7 +1083,7 @@ dependencies = [
[[package]]
name = "memofs"
version = "0.1.2"
version = "0.1.3"
dependencies = [
"crossbeam-channel",
"fs-err",

View File

@@ -95,7 +95,7 @@ uuid = { version = "0.8.1", features = ["v4", "serde"] }
winreg = "0.6.2"
[build-dependencies]
memofs = { version = "0.1.0", path = "memofs" }
memofs = { version = "0.1.3", path = "memofs" }
anyhow = "1.0.27"
bincode = "1.2.1"

View File

@@ -1,6 +1,10 @@
# memofs Changelog
## Unreleased Changes
* Added `set_watch_enabled` to `Vfs` and `VfsLock` to allow turning off file watching.
## 0.1.2 (2020-03-29)
* `VfsSnapshot` now implements Serde's `Serialize` and `Deserialize` traits.
## 0.1.1 (2020-03-18)
* Improved error messages using the [fs-err](https://crates.io/crates/fs-err) crate.

View File

@@ -1,7 +1,7 @@
[package]
name = "memofs"
description = "Virtual filesystem with configurable backends."
version = "0.1.2"
version = "0.1.3"
authors = ["Lucien Greathouse <me@lpghatguy.com>"]
edition = "2018"
readme = "README.md"

View File

@@ -140,13 +140,18 @@ pub enum VfsEvent {
/// the public interfaces to this type.
struct VfsInner {
backend: Box<dyn VfsBackend>,
watch_enabled: bool,
}
impl VfsInner {
fn read<P: AsRef<Path>>(&mut self, path: P) -> io::Result<Arc<Vec<u8>>> {
let path = path.as_ref();
let contents = self.backend.read(path)?;
self.backend.watch(path)?;
if self.watch_enabled {
self.backend.watch(path)?;
}
Ok(Arc::new(contents))
}
@@ -159,7 +164,11 @@ impl VfsInner {
fn read_dir<P: AsRef<Path>>(&mut self, path: P) -> io::Result<ReadDir> {
let path = path.as_ref();
let dir = self.backend.read_dir(path)?;
self.backend.watch(path)?;
if self.watch_enabled {
self.backend.watch(path)?;
}
Ok(dir)
}
@@ -215,6 +224,7 @@ impl Vfs {
pub fn new<B: VfsBackend>(backend: B) -> Self {
let lock = VfsInner {
backend: Box::new(backend),
watch_enabled: true,
};
Self {
@@ -229,6 +239,16 @@ impl Vfs {
}
}
/// Turns automatic file watching on or off. Enabled by default.
///
/// Turning off file watching may be useful for single-use cases, especially
/// on platforms like macOS where registering file watches has significant
/// performance cost.
pub fn set_watch_enabled(&self, enabled: bool) {
let mut inner = self.inner.lock().unwrap();
inner.watch_enabled = enabled;
}
/// Read a file from the VFS, or the underlying backend if it isn't
/// resident.
///
@@ -318,6 +338,15 @@ pub struct VfsLock<'a> {
}
impl VfsLock<'_> {
/// Turns automatic file watching on or off. Enabled by default.
///
/// Turning off file watching may be useful for single-use cases, especially
/// on platforms like macOS where registering file watches has significant
/// performance cost.
pub fn set_watch_enabled(&mut self, enabled: bool) {
self.inner.watch_enabled = enabled;
}
/// Read a file from the VFS, or the underlying backend if it isn't
/// resident.
///