2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2021-07-06 23:48:01 -04:00
|
|
|
use deno_core::parking_lot::Mutex;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::AsyncRefCell;
|
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-12-16 11:14:12 -05:00
|
|
|
use deno_core::RcRef;
|
|
|
|
use deno_core::Resource;
|
2021-03-19 13:25:37 -04:00
|
|
|
use deno_core::ResourceId;
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
use deno_core::op2;
|
2022-03-14 13:44:15 -04:00
|
|
|
|
2024-06-06 23:37:53 -04:00
|
|
|
use deno_permissions::PermissionsContainer;
|
2020-02-21 13:21:51 -05:00
|
|
|
use notify::event::Event as NotifyEvent;
|
2024-08-07 12:15:57 -04:00
|
|
|
use notify::event::ModifyKind;
|
2020-02-21 13:21:51 -05:00
|
|
|
use notify::Error as NotifyError;
|
|
|
|
use notify::EventKind;
|
|
|
|
use notify::RecommendedWatcher;
|
|
|
|
use notify::RecursiveMode;
|
|
|
|
use notify::Watcher;
|
|
|
|
use serde::Serialize;
|
2020-12-16 11:14:12 -05:00
|
|
|
use std::borrow::Cow;
|
2020-09-10 09:57:45 -04:00
|
|
|
use std::cell::RefCell;
|
2020-02-21 13:21:51 -05:00
|
|
|
use std::convert::From;
|
2024-10-22 23:52:58 -04:00
|
|
|
use std::path::Path;
|
2020-02-21 13:21:51 -05:00
|
|
|
use std::path::PathBuf;
|
2020-08-18 12:30:13 -04:00
|
|
|
use std::rc::Rc;
|
2024-10-22 23:52:58 -04:00
|
|
|
use std::sync::Arc;
|
2020-02-21 13:21:51 -05:00
|
|
|
use tokio::sync::mpsc;
|
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
deno_core::extension!(
|
|
|
|
deno_fs_events,
|
2023-03-18 18:30:04 -04:00
|
|
|
ops = [op_fs_events_open, op_fs_events_poll],
|
2023-03-17 14:22:15 -04:00
|
|
|
);
|
2020-02-21 13:21:51 -05:00
|
|
|
|
|
|
|
struct FsEventsResource {
|
2024-10-22 04:41:08 -04:00
|
|
|
receiver: AsyncRefCell<mpsc::Receiver<Result<FsEvent, NotifyError>>>,
|
2020-12-16 11:14:12 -05:00
|
|
|
cancel: CancelHandle,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Resource for FsEventsResource {
|
|
|
|
fn name(&self) -> Cow<str> {
|
|
|
|
"fsEvents".into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn close(self: Rc<Self>) {
|
|
|
|
self.cancel.cancel();
|
|
|
|
}
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Represents a file system event.
|
|
|
|
///
|
|
|
|
/// We do not use the event directly from the notify crate. We flatten
|
|
|
|
/// the structure into this simpler structure. We want to only make it more
|
|
|
|
/// complex as needed.
|
|
|
|
///
|
|
|
|
/// Feel free to expand this struct as long as you can add tests to demonstrate
|
|
|
|
/// the complexity.
|
2024-10-22 23:52:58 -04:00
|
|
|
#[derive(Serialize, Debug, Clone)]
|
2020-02-21 13:21:51 -05:00
|
|
|
struct FsEvent {
|
2021-11-23 05:30:24 -05:00
|
|
|
kind: &'static str,
|
2020-02-21 13:21:51 -05:00
|
|
|
paths: Vec<PathBuf>,
|
2021-11-23 05:30:24 -05:00
|
|
|
flag: Option<&'static str>,
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<NotifyEvent> for FsEvent {
|
|
|
|
fn from(e: NotifyEvent) -> Self {
|
|
|
|
let kind = match e.kind {
|
|
|
|
EventKind::Any => "any",
|
|
|
|
EventKind::Access(_) => "access",
|
|
|
|
EventKind::Create(_) => "create",
|
2024-08-07 12:15:57 -04:00
|
|
|
EventKind::Modify(modify_kind) => match modify_kind {
|
|
|
|
ModifyKind::Name(_) => "rename",
|
|
|
|
ModifyKind::Any
|
|
|
|
| ModifyKind::Data(_)
|
|
|
|
| ModifyKind::Metadata(_)
|
|
|
|
| ModifyKind::Other => "modify",
|
|
|
|
},
|
2020-02-21 13:21:51 -05:00
|
|
|
EventKind::Remove(_) => "remove",
|
2021-11-23 05:30:24 -05:00
|
|
|
EventKind::Other => "other",
|
|
|
|
};
|
|
|
|
let flag = e.flag().map(|f| match f {
|
|
|
|
notify::event::Flag::Rescan => "rescan",
|
|
|
|
});
|
2020-02-21 13:21:51 -05:00
|
|
|
FsEvent {
|
|
|
|
kind,
|
|
|
|
paths: e.paths,
|
2021-11-23 05:30:24 -05:00
|
|
|
flag,
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 23:52:58 -04:00
|
|
|
type WatchSender = (Vec<String>, mpsc::Sender<Result<FsEvent, NotifyError>>);
|
|
|
|
|
|
|
|
struct WatcherState {
|
|
|
|
senders: Arc<Mutex<Vec<WatchSender>>>,
|
|
|
|
watcher: RecommendedWatcher,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn starts_with_canonicalized(path: &Path, prefix: &str) -> bool {
|
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
let path = path.canonicalize().ok();
|
|
|
|
#[allow(clippy::disallowed_methods)]
|
|
|
|
let prefix = std::fs::canonicalize(prefix).ok();
|
|
|
|
match (path, prefix) {
|
|
|
|
(Some(path), Some(prefix)) => path.starts_with(prefix),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-22 04:41:08 -04:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub enum FsEventsError {
|
|
|
|
#[error(transparent)]
|
|
|
|
Resource(deno_core::error::AnyError),
|
|
|
|
#[error(transparent)]
|
2024-11-04 12:17:21 -05:00
|
|
|
Permission(#[from] deno_permissions::PermissionCheckError),
|
2024-10-22 04:41:08 -04:00
|
|
|
#[error(transparent)]
|
|
|
|
Notify(#[from] NotifyError),
|
|
|
|
#[error(transparent)]
|
|
|
|
Canceled(#[from] deno_core::Canceled),
|
|
|
|
}
|
|
|
|
|
2024-10-22 23:52:58 -04:00
|
|
|
fn start_watcher(
|
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
paths: Vec<String>,
|
2024-10-22 23:52:58 -04:00
|
|
|
sender: mpsc::Sender<Result<FsEvent, NotifyError>>,
|
|
|
|
) -> Result<(), FsEventsError> {
|
|
|
|
if let Some(watcher) = state.try_borrow_mut::<WatcherState>() {
|
|
|
|
watcher.senders.lock().push((paths, sender));
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let senders = Arc::new(Mutex::new(vec![(paths, sender)]));
|
|
|
|
|
|
|
|
let sender_clone = senders.clone();
|
|
|
|
let watcher: RecommendedWatcher = Watcher::new(
|
|
|
|
move |res: Result<NotifyEvent, NotifyError>| {
|
|
|
|
let res2 = res.map(FsEvent::from).map_err(FsEventsError::Notify);
|
|
|
|
for (paths, sender) in sender_clone.lock().iter() {
|
|
|
|
// Ignore result, if send failed it means that watcher was already closed,
|
|
|
|
// but not all messages have been flushed.
|
|
|
|
|
|
|
|
// Only send the event if the path matches one of the paths that the user is watching
|
|
|
|
if let Ok(event) = &res2 {
|
|
|
|
if paths.iter().any(|path| {
|
|
|
|
event.paths.iter().any(|event_path| {
|
|
|
|
same_file::is_same_file(event_path, path).unwrap_or(false)
|
|
|
|
|| starts_with_canonicalized(event_path, path)
|
|
|
|
})
|
|
|
|
}) {
|
|
|
|
let _ = sender.try_send(Ok(event.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Default::default(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
state.put::<WatcherState>(WatcherState { watcher, senders });
|
|
|
|
|
|
|
|
Ok(())
|
2021-03-18 14:42:01 -04:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2]
|
|
|
|
#[smi]
|
2020-08-28 11:08:24 -04:00
|
|
|
fn op_fs_events_open(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2024-10-22 23:52:58 -04:00
|
|
|
recursive: bool,
|
|
|
|
#[serde] paths: Vec<String>,
|
2024-10-22 04:41:08 -04:00
|
|
|
) -> Result<ResourceId, FsEventsError> {
|
|
|
|
let (sender, receiver) = mpsc::channel::<Result<FsEvent, NotifyError>>(16);
|
2024-10-22 23:52:58 -04:00
|
|
|
|
|
|
|
start_watcher(state, paths.clone(), sender)?;
|
|
|
|
|
|
|
|
let recursive_mode = if recursive {
|
2020-02-21 13:21:51 -05:00
|
|
|
RecursiveMode::Recursive
|
|
|
|
} else {
|
|
|
|
RecursiveMode::NonRecursive
|
|
|
|
};
|
2024-10-22 23:52:58 -04:00
|
|
|
for path in &paths {
|
2024-09-16 16:39:37 -04:00
|
|
|
let path = state
|
2023-01-07 11:25:34 -05:00
|
|
|
.borrow_mut::<PermissionsContainer>()
|
2024-11-04 12:17:21 -05:00
|
|
|
.check_read(path, "Deno.watchFs()")?;
|
2024-10-22 23:52:58 -04:00
|
|
|
|
|
|
|
let watcher = state.borrow_mut::<WatcherState>();
|
|
|
|
watcher.watcher.watch(&path, recursive_mode)?;
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|
2020-12-16 11:14:12 -05:00
|
|
|
let resource = FsEventsResource {
|
|
|
|
receiver: AsyncRefCell::new(receiver),
|
|
|
|
cancel: Default::default(),
|
|
|
|
};
|
|
|
|
let rid = state.resource_table.add(resource);
|
2021-04-05 12:40:24 -04:00
|
|
|
Ok(rid)
|
2021-03-18 14:42:01 -04:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:10:43 -04:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2020-08-28 11:08:24 -04:00
|
|
|
async fn op_fs_events_poll(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-11 18:10:43 -04:00
|
|
|
#[smi] rid: ResourceId,
|
2024-10-22 04:41:08 -04:00
|
|
|
) -> Result<Option<FsEvent>, FsEventsError> {
|
|
|
|
let resource = state
|
|
|
|
.borrow()
|
|
|
|
.resource_table
|
|
|
|
.get::<FsEventsResource>(rid)
|
|
|
|
.map_err(FsEventsError::Resource)?;
|
2020-12-16 11:14:12 -05:00
|
|
|
let mut receiver = RcRef::map(&resource, |r| &r.receiver).borrow_mut().await;
|
|
|
|
let cancel = RcRef::map(resource, |r| &r.cancel);
|
|
|
|
let maybe_result = receiver.recv().or_cancel(cancel).await?;
|
|
|
|
match maybe_result {
|
2021-04-05 12:40:24 -04:00
|
|
|
Some(Ok(value)) => Ok(Some(value)),
|
2024-10-22 04:41:08 -04:00
|
|
|
Some(Err(err)) => Err(FsEventsError::Notify(err)),
|
2021-04-05 12:40:24 -04:00
|
|
|
None => Ok(None),
|
2020-12-16 11:14:12 -05:00
|
|
|
}
|
2020-02-21 13:21:51 -05:00
|
|
|
}
|