浏览代码

Add API to delay and combine inotify events.

父节点
当前提交
80512d799b
共有 5 个文件被更改,包括 54 次插入2 次删除
  1. 1
    0
      Cargo.lock
  2. 1
    1
      Cargo.toml
  3. 47
    0
      src/file_event_delay.rs
  4. 4
    1
      src/file_system_watcher.rs
  5. 1
    0
      src/lib.rs

+ 1
- 0
Cargo.lock 查看文件

@@ -907,6 +907,7 @@ dependencies = [
907 907
  "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)",
908 908
  "mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
909 909
  "pin-project-lite 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
910
+ "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
910 911
  "tokio-macros 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
911 912
 ]
912 913
 

+ 1
- 1
Cargo.toml 查看文件

@@ -10,7 +10,7 @@ serde = { version = "1.0", features = ["derive"] }
10 10
 serde_json = "1.0"
11 11
 dirs = "2.0"
12 12
 byteorder = "1.3"
13
-tokio = { version = "0.2", features = ["fs", "io-util", "macros", "net", "stream", "sync", "tcp"] }
13
+tokio = { version = "0.2", features = ["fs", "io-util", "macros", "net", "stream", "sync", "tcp", "time"] }
14 14
 futures = "0.3.1"
15 15
 futures-tokio-compat = { git = "https://github.com/mgottschlag/futures-tokio-compat.git" }
16 16
 async-tungstenite = { version = "0.3", features = ["tokio-runtime"] }

+ 47
- 0
src/file_event_delay.rs 查看文件

@@ -0,0 +1,47 @@
1
+use std::io;
2
+use std::pin::Pin;
3
+use std::task::{Context, Poll};
4
+use std::time::Duration;
5
+
6
+use futures::stream::Stream;
7
+
8
+/// A stream which delays inotify events and combines them whenever possible.
9
+///
10
+/// The type tries to reduce the number of unnecessary events (e.g., a file which was created and
11
+/// then immediately removed again, or a large number of modification events for the same file) and
12
+/// tries to reconstruct additional information (e.g., if a file was moved from one directory to
13
+/// another, in which case inotify cannot detect the move).
14
+pub struct FileEventDelay<T>
15
+where
16
+    T: Stream<Item = io::Result<inotify::EventOwned>>,
17
+{
18
+    input: T,
19
+    min_delay: Duration,
20
+}
21
+
22
+impl<T> FileEventDelay<T>
23
+where
24
+    T: Stream<Item = io::Result<inotify::EventOwned>>,
25
+{
26
+    pub fn new(input: T, min_delay: Duration) -> Self {
27
+        Self { input, min_delay }
28
+    }
29
+}
30
+
31
+impl<T> Stream for FileEventDelay<T>
32
+where
33
+    T: Stream<Item = io::Result<inotify::EventOwned>>,
34
+{
35
+    type Item = io::Result<FileEvent>;
36
+
37
+    fn poll_next(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
38
+        // TODO: Two arrays, fill one array with inotify events. Whenever a periodic timer elapses,
39
+        // parse contents of the other array and clear it, and swap the two arrays.
40
+        // TODO
41
+        panic!("Not yet implemented.");
42
+    }
43
+}
44
+
45
+pub enum FileEvent {
46
+    // TODO
47
+}

+ 4
- 1
src/file_system_watcher.rs 查看文件

@@ -1,12 +1,14 @@
1 1
 use std::collections::VecDeque;
2 2
 use std::sync::Arc;
3 3
 use std::path::Path;
4
+use std::time::Duration;
4 5
 
5 6
 use inotify::{Inotify, WatchMask};
6 7
 use tokio::sync::{mpsc, oneshot, Mutex};
7 8
 use tokio::task;
8 9
 
9 10
 use super::{Error, FileTree};
11
+use super::file_event_delay::FileEventDelay;
10 12
 
11 13
 pub struct FileSystemWatcher {
12 14
     state_send: mpsc::Sender<(StateChange, oneshot::Sender<()>)>,
@@ -79,7 +81,8 @@ impl FileSystemWatcher {
79 81
         new_directories.push_back(file_tree.lock().await.root_path().to_owned());
80 82
 
81 83
         let mut inotify_buffer = InotifyBuffer { data: [0; 1024] };
82
-        let _inotify_stream = inotify.event_stream(&mut inotify_buffer).unwrap();
84
+        let inotify_stream = inotify.event_stream(&mut inotify_buffer).unwrap();
85
+        let _file_events = FileEventDelay::new(inotify_stream, Duration::from_secs(3));
83 86
         //let watches_by_path = BTreeMap::new();
84 87
         //let paths_by_watch = HashMap::new();
85 88
 

+ 1
- 0
src/lib.rs 查看文件

@@ -1,6 +1,7 @@
1 1
 use std::io;
2 2
 
3 3
 mod database;
4
+mod file_event_delay;
4 5
 mod file_system_watcher;
5 6
 mod file_tree;
6 7
 

正在加载...
取消
保存