1#[cfg(test)]
5mod tests;
6
7use std::cell::{Cell, RefCell};
8use std::ffi::OsString;
9use std::io::{ErrorKind, Read, Write};
10use std::panic::{self, AssertUnwindSafe};
11use std::path::{Path, PathBuf};
12use std::rc::{Rc, Weak};
13use std::time::{Duration, Instant};
14
15use calloop::RegistrationToken;
16use calloop::timer::TimeoutAction;
17use crabshell_reactive::{ReadSignal, Signal, untrack};
18use crabshell_ui::Cx;
19use inotify::{EventMask, WatchMask};
20use serde::Serialize;
21use serde::de::DeserializeOwned;
22
23use crate::Shared;
24use crate::lines::panic_message;
25use crate::watch::{self, Watch};
26
27pub(crate) const MAX_FILE: u64 = 16 * 1024 * 1024;
30
31const WRITING: Duration = Duration::from_millis(50);
36
37pub(crate) struct Files(pub(crate) Rc<Shared>);
40
41pub(crate) struct Follow {
49 shared: Rc<Shared>,
50 path: PathBuf,
51 watch: RefCell<Option<Watch>>,
52 file: RefCell<Option<Watch>>,
55 writing: Cell<Option<RegistrationToken>>,
57 changed: Box<dyn Fn()>,
59}
60
61impl Follow {
62 pub(crate) fn new(
65 shared: &Rc<Shared>,
66 path: PathBuf,
67 changed: impl Fn() + 'static,
68 ) -> Rc<Follow> {
69 let follow = Rc::new(Follow {
70 shared: Rc::clone(shared),
71 path,
72 watch: RefCell::new(None),
73 file: RefCell::new(None),
74 writing: Cell::new(None),
75 changed: Box::new(changed),
76 });
77 follow.rewatch();
78 follow
79 }
80
81 fn rewatch(self: &Rc<Self>) {
84 self.watch.replace(None);
85 self.file.replace(None);
86 let (Some(dir), Some(name)) = (self.path.parent(), self.path.file_name()) else {
87 return;
88 };
89 let mut dir = if dir.as_os_str().is_empty() {
90 Path::new(".")
91 } else {
92 dir
93 };
94 let mut next: OsString = name.to_owned();
95 let own = dir.is_dir();
96 while !dir.is_dir() {
97 let (Some(above), Some(name)) = (dir.parent(), dir.file_name()) else {
98 return;
99 };
100 next = name.to_owned();
101 dir = if above.as_os_str().is_empty() {
102 Path::new(".")
103 } else {
104 above
105 };
106 }
107 let weak = Rc::downgrade(self);
108 let listener = move |change: &watch::Change| {
109 let Some(follow) = weak.upgrade() else {
110 return;
111 };
112 let moved = EventMask::DELETE_SELF | EventMask::MOVE_SELF;
113 match &change.name {
114 None => {
116 if change.mask.intersects(moved) || change.mask.contains(EventMask::Q_OVERFLOW) {
117 follow.rewatch();
118 }
119 follow.read();
120 }
121 Some(changed) if *changed == next && own => {
125 follow.watch_file();
126 if !change.mask.contains(EventMask::CREATE) || change.mask.contains(EventMask::ISDIR) {
127 follow.read();
128 } else if follow.path.metadata().is_ok_and(|meta| meta.len() > 0) {
129 follow.written();
130 }
131 }
132 Some(changed) if *changed == next => {
134 follow.rewatch();
135 follow.read();
136 }
137 Some(_) => {}
138 }
139 };
140 match watch::watch(&self.shared, dir, watch::DIRECTORY, listener) {
141 Ok(watch) => {
142 self.watch.replace(Some(watch));
143 }
144 Err(error) => {
145 let path = self.path.display();
146 tracing::info!(%path, %error, "can't watch the file, so changes to it aren't seen");
147 }
148 }
149 if own {
150 self.watch_file();
151 }
152 }
153
154 fn watch_file(self: &Rc<Self>) {
158 if !self.path.metadata().is_ok_and(|meta| meta.is_file()) {
159 self.file.replace(None);
160 return;
161 }
162 let weak = Rc::downgrade(self);
163 let listener = move |change: &watch::Change| {
164 if let Some(follow) = weak
165 .upgrade()
166 .filter(|_| change.mask.contains(EventMask::MODIFY))
167 {
168 follow.written();
169 }
170 };
171 let watched = watch::watch(&self.shared, &self.path, WatchMask::MODIFY, listener);
176 if let Err(error) = &watched {
177 let path = self.path.display();
178 tracing::debug!(%path, %error, "can't watch the file's writes; its close still counts");
179 }
180 self.file.replace(watched.ok());
181 }
182}
183
184impl Follow {
185 fn written(self: &Rc<Self>) {
189 if self.writing.get().is_some() {
190 return;
191 }
192 let Some(event_loop) = self.shared.event_loop() else {
193 return;
194 };
195 let weak = Rc::downgrade(self);
196 let tick = Box::new(move || {
197 if let Some(follow) = weak.upgrade() {
198 follow.writing.set(None);
199 (follow.changed)();
200 }
201 TimeoutAction::Drop
202 });
203 let token = event_loop.insert_timer(Instant::now() + WRITING, tick);
204 self.writing.set(token);
205 }
206
207 fn read(&self) {
209 self.stop_waiting();
210 (self.changed)();
211 }
212
213 fn stop_waiting(&self) {
214 if let (Some(token), Some(event_loop)) = (self.writing.take(), self.shared.event_loop()) {
215 event_loop.remove(token);
216 }
217 }
218}
219
220impl Drop for Follow {
221 fn drop(&mut self) {
222 self.stop_waiting();
223 }
224}
225
226pub(crate) struct Keep<T>(#[allow(dead_code, reason = "held for its drop")] pub(crate) Rc<T>);
228
229#[derive(Clone)]
232pub struct FileView {
233 inner: Rc<ViewInner>,
234}
235
236#[derive(Clone, Debug, PartialEq, Eq)]
239pub struct FileError {
240 pub kind: FileErrorKind,
242 pub message: String,
245}
246
247#[derive(Clone, Copy, Debug, PartialEq, Eq)]
249pub enum FileErrorKind {
250 NotFound,
252 PermissionDenied,
254 NotAFile,
257 Other,
259}
260
261impl FileError {
262 pub(crate) fn new(kind: FileErrorKind, message: &str) -> FileError {
263 let message = message.to_owned();
264 FileError { kind, message }
265 }
266}
267
268impl From<std::io::Error> for FileError {
269 fn from(error: std::io::Error) -> FileError {
270 let kind = match error.kind() {
271 ErrorKind::NotFound => FileErrorKind::NotFound,
272 ErrorKind::PermissionDenied => FileErrorKind::PermissionDenied,
273 ErrorKind::IsADirectory => FileErrorKind::NotAFile,
274 _ => FileErrorKind::Other,
275 };
276 FileError::new(kind, &error.to_string())
277 }
278}
279
280struct ViewInner {
281 path: PathBuf,
282 follow: RefCell<Option<Rc<Follow>>>,
283 text: Signal<Option<String>>,
285 error: Signal<Option<FileError>>,
286}
287
288pub(crate) fn file_view(cx: &Cx, path: PathBuf) -> FileView {
290 let inner = Rc::new(ViewInner {
291 text: cx.signal(None),
292 error: cx.signal(None),
293 follow: RefCell::new(None),
294 path,
295 });
296 inner.reread();
297 if let Some(files) = cx.service::<Files>() {
298 let weak = Rc::downgrade(&inner);
299 let follow = Follow::new(&files.0, inner.path.clone(), move || {
300 if let Some(inner) = Weak::upgrade(&weak) {
301 inner.reread();
302 }
303 });
304 inner.follow.replace(Some(follow));
305 }
306 cx.owner().signal(Keep(Rc::clone(&inner)));
308 FileView { inner }
309}
310
311impl FileView {
312 pub fn text(&self) -> ReadSignal<Option<String>> {
316 self.inner.text.read_only()
317 }
318
319 pub fn error(&self) -> ReadSignal<Option<FileError>> {
323 self.inner.error.read_only()
324 }
325
326 pub fn write(&self, text: impl AsRef<str>) {
332 let text = text.as_ref();
333 match write_whole(&self.inner.path, text.as_bytes()) {
334 Ok(()) => {
335 set(self.inner.text, Some(text.to_owned()));
336 set(self.inner.error, None);
337 }
338 Err(error) => {
339 let path = self.inner.path.display();
340 tracing::warn!(%path, %error, "couldn't write the file");
341 }
342 }
343 }
344}
345
346impl ViewInner {
347 fn reread(&self) {
349 let (text, error) = match read_text(&self.path) {
350 Ok(text) => (Some(text), None),
351 Err(error) => {
352 if untrack(|| self.error.try_with(|old| old.as_ref() != Some(&error))) == Some(true) {
354 let path = self.path.display();
355 tracing::info!(%path, error = error.message, "can't read the file");
356 }
357 (None, Some(error))
358 }
359 };
360 set(self.text, text);
361 set(self.error, error);
362 }
363}
364
365pub(crate) fn json_file<T>(cx: &Cx, path: PathBuf, default: T) -> Signal<T>
367where
368 T: Serialize + DeserializeOwned + PartialEq + 'static,
369{
370 let value = cx.signal(default);
371 let file = Rc::new(JsonInner {
372 path,
373 value,
374 on_disk: RefCell::new(None),
376 logged: RefCell::new(None),
377 follow: RefCell::new(None),
378 });
379 file.reread();
380 if let Some(files) = cx.service::<Files>() {
381 let weak = Rc::downgrade(&file);
382 let follow = Follow::new(&files.0, file.path.clone(), move || {
383 if let Some(file) = Weak::upgrade(&weak) {
384 file.reread();
385 }
386 });
387 file.follow.replace(Some(follow));
388 }
389 let start = untrack(|| value.try_with(|value| file.to_json(value))).flatten();
392 let writer = Rc::clone(&file);
393 let first = Cell::new(true);
394 cx.effect(move || {
395 let text = value.with(|value| writer.to_json(value));
396 let unset = first.replace(false) && start.is_some() && text == start;
397 if let (Some(text), false) = (text, unset) {
398 untrack(|| writer.write(text));
399 }
400 })
401 .named(format!("cx.json_file {}", file.path.display()));
402 value
403}
404
405struct JsonInner<T: 'static> {
406 path: PathBuf,
407 value: Signal<T>,
408 on_disk: RefCell<Option<String>>,
409 logged: RefCell<Option<String>>,
411 follow: RefCell<Option<Rc<Follow>>>,
412}
413
414impl<T> JsonInner<T>
415where
416 T: Serialize + DeserializeOwned + PartialEq + 'static,
417{
418 fn reread(&self) {
423 let text = match read_text(&self.path) {
424 Ok(text) => text,
425 Err(error) => {
426 self.log_once(format!("can't read the file: {}", error.message));
427 return;
428 }
429 };
430 let applied = panic::catch_unwind(AssertUnwindSafe(|| {
431 let read = serde_json::from_str::<T>(&text)?;
432 let normal = serde_json::to_string_pretty(&read).unwrap_or_else(|_| text.clone());
433 self.on_disk.replace(Some(normal));
434 set(self.value, read);
435 serde_json::Result::Ok(())
436 }));
437 match applied {
438 Ok(Ok(())) => {
439 self.logged.replace(None);
440 }
441 Ok(Err(error)) => self.log_once(format!("the file isn't the JSON expected: {error}")),
442 Err(payload) => {
443 let message = panic_message(payload.as_ref());
444 self.log_once(format!("reading it panicked: {message}"));
445 }
446 }
447 }
448
449 fn to_json(&self, value: &T) -> Option<String> {
452 let path = self.path.display();
453 match panic::catch_unwind(AssertUnwindSafe(|| serde_json::to_string_pretty(value))) {
454 Ok(Ok(text)) => Some(text),
455 Ok(Err(error)) => {
456 tracing::warn!(%path, %error, "couldn't turn the value into JSON");
457 None
458 }
459 Err(payload) => {
460 let message = panic_message(payload.as_ref());
461 tracing::error!("cx.json_file {path}: turning the value into JSON panicked: {message}");
462 None
463 }
464 }
465 }
466
467 fn write(&self, text: String) {
468 if self.on_disk.borrow().as_deref() == Some(text.as_str()) {
469 return;
470 }
471 match write_whole(&self.path, format!("{text}\n").as_bytes()) {
472 Ok(()) => {
473 self.on_disk.replace(Some(text));
474 }
475 Err(error) => {
476 let path = self.path.display();
477 tracing::warn!(%path, %error, "couldn't write the file");
478 }
479 }
480 }
481
482 fn log_once(&self, problem: String) {
483 if self.logged.borrow().as_deref() != Some(problem.as_str()) {
484 let path = self.path.display();
485 tracing::info!(%path, problem, "the JSON file keeps its value");
486 self.logged.replace(Some(problem));
487 }
488 }
489}
490
491fn read_text(path: &Path) -> Result<String, FileError> {
493 if !std::fs::metadata(path)?.is_file() {
495 return Err(FileError::new(FileErrorKind::NotAFile, "it isn't a file"));
496 }
497 let file = open_file(path)?;
498 let mut bytes = Vec::new();
499 file.take(MAX_FILE + 1).read_to_end(&mut bytes)?;
500 if bytes.len() as u64 > MAX_FILE {
501 return Err(FileError::new(FileErrorKind::Other, "it is over 16 MiB"));
502 }
503 String::from_utf8(bytes).map_err(|_| FileError::new(FileErrorKind::Other, "it isn't UTF-8"))
504}
505
506pub(crate) fn open_file(path: &Path) -> Result<std::fs::File, FileError> {
509 use rustix::fs::{Mode, OFlags};
510
511 let flags = OFlags::RDONLY | OFlags::NONBLOCK | OFlags::NOCTTY | OFlags::CLOEXEC;
512 let fd = rustix::fs::open(path, flags, Mode::empty()).map_err(std::io::Error::from)?;
513 let file = std::fs::File::from(fd);
514 if !file.metadata()?.is_file() {
515 return Err(FileError::new(FileErrorKind::NotAFile, "it isn't a file"));
516 }
517 rustix::fs::fcntl_setfl(&file, OFlags::empty()).map_err(std::io::Error::from)?;
519 Ok(file)
520}
521
522fn write_whole(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
527 use std::os::unix::fs::OpenOptionsExt;
528
529 if let Some(dir) = path.parent()
530 && !dir.as_os_str().is_empty()
531 && !dir.exists()
532 {
533 std::fs::create_dir_all(dir)?;
534 }
535 let target = match std::fs::canonicalize(path) {
536 Ok(target) => target,
537 Err(error) if error.kind() == ErrorKind::NotFound => path.to_owned(),
538 Err(error) => return Err(error),
539 };
540 let name = target
541 .file_name()
542 .ok_or_else(|| std::io::Error::other("the path names no file"))?;
543 let mut partial_name = std::ffi::OsString::from(".");
544 partial_name.push(name);
545 partial_name.push(".crabshell-partial");
546 let partial = target.with_file_name(partial_name);
547 match std::fs::remove_file(&partial) {
548 Err(error) if error.kind() != ErrorKind::NotFound => return Err(error),
549 _ => {}
550 }
551 let mode = std::fs::metadata(&target).map_or(0o644, |meta| {
552 use std::os::unix::fs::PermissionsExt;
553 meta.permissions().mode() & 0o7777
554 });
555 let mut file = std::fs::OpenOptions::new()
557 .write(true)
558 .create_new(true)
559 .mode(mode)
560 .open(&partial)?;
561 let written = file.write_all(bytes).and_then(|()| file.sync_all());
562 drop(file);
563 if let Err(error) = written.and_then(|()| std::fs::rename(&partial, &target)) {
564 let _ = std::fs::remove_file(&partial);
565 return Err(error);
566 }
567 Ok(())
568}
569
570pub(crate) fn set<T: PartialEq + 'static>(signal: Signal<T>, value: T) {
573 if untrack(|| signal.try_with(|old| *old != value)) == Some(true) {
574 signal.try_set(value);
575 }
576}