two-way file system sync
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

file_system_watcher.rs 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. use std::sync::Arc;
  2. use std::time::Duration;
  3. use fswatcher::{FileEventDelay, FileSystemEvent, StopReason};
  4. use futures::future::{self, Either};
  5. use tokio::stream::StreamExt;
  6. use tokio::sync::{mpsc, oneshot, Mutex};
  7. use tokio::task;
  8. use super::{Error, FileTree, SynchronizationError};
  9. pub struct FileSystemWatcher {
  10. state_send: mpsc::Sender<(StateChange, oneshot::Sender<()>)>,
  11. }
  12. impl FileSystemWatcher {
  13. pub fn new(
  14. file_tree: Arc<Mutex<FileTree>>,
  15. errors: mpsc::Sender<SynchronizationError>,
  16. ) -> Result<FileSystemWatcher, Error> {
  17. // Spawn the task that watches the file system as soon as unpause() is called.
  18. let (state_send, pause_receive) = mpsc::channel(1);
  19. tokio::spawn(async move {
  20. Self::task_paused(file_tree, pause_receive, errors).await;
  21. });
  22. Ok(FileSystemWatcher { state_send })
  23. }
  24. pub async fn pause(&mut self) {
  25. let (send, receive) = oneshot::channel();
  26. self.state_send
  27. .send((StateChange::Pause, send))
  28. .await
  29. .unwrap();
  30. receive.await.unwrap();
  31. }
  32. pub async fn unpause(&mut self) {
  33. let (send, receive) = oneshot::channel();
  34. self.state_send
  35. .send((StateChange::Unpause, send))
  36. .await
  37. .unwrap();
  38. receive.await.unwrap();
  39. }
  40. async fn task_paused(
  41. mut file_tree: Arc<Mutex<FileTree>>,
  42. mut state_receive: mpsc::Receiver<(StateChange, oneshot::Sender<()>)>,
  43. mut errors: mpsc::Sender<SynchronizationError>,
  44. ) {
  45. let mut paused = true;
  46. loop {
  47. if paused {
  48. // If we are paused, we only need to wait for state changes.
  49. let (new_state, send) = state_receive.recv().await.unwrap();
  50. match new_state {
  51. StateChange::Pause => paused = true,
  52. StateChange::Unpause => paused = false,
  53. StateChange::Terminate => break, // No reply, see drop().
  54. }
  55. send.send(()).unwrap();
  56. } else {
  57. // We were unpaused, initialize the file system watcher and start waiting for
  58. // that as well.
  59. let next_state =
  60. Self::task_unpaused(&mut file_tree, &mut state_receive, &mut errors).await;
  61. match next_state {
  62. StateChange::Pause => paused = true,
  63. StateChange::Unpause => paused = false,
  64. StateChange::Terminate => break,
  65. }
  66. }
  67. }
  68. }
  69. async fn task_unpaused(
  70. file_tree: &mut Arc<Mutex<FileTree>>,
  71. state_receive: &mut mpsc::Receiver<(StateChange, oneshot::Sender<()>)>,
  72. errors: &mut mpsc::Sender<SynchronizationError>,
  73. ) -> StateChange {
  74. let root_path = file_tree.lock().await.root_path().to_owned();
  75. let fsw = match fswatcher::FileSystemWatcher::new(&root_path) {
  76. Ok(fsw) => fsw,
  77. Err(e) => return Self::wait_for_pause(e.into(), state_receive, errors).await,
  78. };
  79. let mut file_events = FileEventDelay::new(fsw, Duration::from_secs(3));
  80. let mut error_needs_pause = None;
  81. while error_needs_pause.is_none() {
  82. // We only want to wait for file system events until a state change pauses the file
  83. // system watcher.
  84. match future::select(Box::pin(file_events.next()), Box::pin(state_receive.recv())).await
  85. {
  86. Either::Left((event, _state_future)) => {
  87. // We cannot get any end-of-stream None here - we would first get a "Stopped"
  88. // file event.
  89. if let Err(error) =
  90. Self::handle_file_event(file_tree, errors, event.unwrap()).await
  91. {
  92. error_needs_pause = Some(error);
  93. }
  94. }
  95. Either::Right((next_state, _file_event_future)) => {
  96. match next_state.unwrap() {
  97. (StateChange::Unpause, send) => send.send(()).unwrap(),
  98. (StateChange::Pause, send) => {
  99. send.send(()).unwrap();
  100. return StateChange::Pause;
  101. }
  102. (StateChange::Terminate, _) => return StateChange::Terminate,
  103. };
  104. }
  105. };
  106. }
  107. return Self::wait_for_pause(error_needs_pause.unwrap(), state_receive, errors).await;
  108. }
  109. async fn wait_for_pause(
  110. reason: Error,
  111. state_receive: &mut mpsc::Receiver<(StateChange, oneshot::Sender<()>)>,
  112. errors: &mut mpsc::Sender<SynchronizationError>,
  113. ) -> StateChange {
  114. match errors
  115. .send(SynchronizationError {
  116. needs_pause: true,
  117. error: reason,
  118. })
  119. .await
  120. {
  121. Ok(()) => (),
  122. Err(_) => panic!("Could not send synchronization error!"),
  123. };
  124. loop {
  125. match state_receive.recv().await.unwrap() {
  126. (StateChange::Unpause, send) => send.send(()).unwrap(),
  127. (StateChange::Pause, send) => {
  128. send.send(()).unwrap();
  129. return StateChange::Pause;
  130. }
  131. (StateChange::Terminate, _) => return StateChange::Terminate,
  132. }
  133. }
  134. }
  135. async fn handle_file_event(
  136. _file_tree: &mut Arc<Mutex<FileTree>>,
  137. errors: &mut mpsc::Sender<SynchronizationError>,
  138. event: FileSystemEvent,
  139. ) -> Result<(), Error> {
  140. match event {
  141. FileSystemEvent::Stopped(reason) => match reason {
  142. StopReason::DirectoryRemoved => {
  143. return Err(Error::DirectoryRemoved);
  144. }
  145. },
  146. FileSystemEvent::DirectoryWatched(_path) => {
  147. // TODO
  148. }
  149. FileSystemEvent::DirectoryCreated(_path) => {
  150. // TODO
  151. }
  152. FileSystemEvent::DirectoryModified(_path) => {
  153. // TODO
  154. }
  155. FileSystemEvent::DirectoryRemoved(_path) => {
  156. // TODO
  157. }
  158. FileSystemEvent::DirectoryMoved(_from, _to) => {
  159. // TODO
  160. }
  161. FileSystemEvent::FileCreated(_path) => {
  162. // TODO
  163. }
  164. FileSystemEvent::FileModified(_path) => {
  165. // TODO
  166. }
  167. FileSystemEvent::FileRemoved(_path) => {
  168. // TODO
  169. }
  170. FileSystemEvent::FileMoved(_from, _to) => {
  171. // TODO
  172. }
  173. FileSystemEvent::Error(e) => {
  174. errors
  175. .send(SynchronizationError {
  176. needs_pause: false,
  177. error: e.into(),
  178. })
  179. .await
  180. .ok();
  181. }
  182. }
  183. // Yield to other parts of the program to minimize the impact of scanning large
  184. // directory trees.
  185. // TODO: Check whether this is really necessary and how large the impact on
  186. // performance is.
  187. task::yield_now().await;
  188. Ok(())
  189. }
  190. }
  191. impl Drop for FileSystemWatcher {
  192. fn drop(&mut self) {
  193. // The receiver is immediately dropped, so the task must not send a reply.
  194. // TODO: This is racy, the task should be synchronously paused first.
  195. let (send, _receive) = oneshot::channel();
  196. // Here, try_send() always succeeds, because the channel always has enough capacity.
  197. // pause() and unpause() are synchronous and wait until the task has emptied the channel.
  198. self.state_send
  199. .try_send((StateChange::Terminate, send))
  200. .unwrap();
  201. }
  202. }
  203. struct InotifyBuffer {
  204. data: [u8; 1024],
  205. }
  206. impl AsMut<[u8]> for InotifyBuffer {
  207. fn as_mut<'a>(&'a mut self) -> &'a mut [u8] {
  208. &mut self.data
  209. }
  210. }
  211. impl AsRef<[u8]> for InotifyBuffer {
  212. fn as_ref<'a>(&'a self) -> &'a [u8] {
  213. &self.data
  214. }
  215. }
  216. #[derive(Debug)]
  217. enum StateChange {
  218. Pause,
  219. Unpause,
  220. Terminate,
  221. }