wait-file 0.1.0

cli utility to monitor for changes in one or multiple files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#[macro_use]
extern crate log;

use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use std::ffi::{ OsString, OsStr };
use std::io::ErrorKind;
use std::mem::MaybeUninit;
use std::os::fd::{AsFd, BorrowedFd, OwnedFd};
use std::path::{Path, PathBuf};
use std::process::{Child, Command};
use std::rc::Rc;
use std::time::Duration;

use clap::Parser;
use inotify::{Event, EventMask, Inotify, WatchDescriptor, WatchMask, Watches};
use rustix::event::{poll, PollFd, PollFlags, Timespec};
use rustix::process::{kill_process, pidfd_open, Pid, PidfdFlags, Signal};

/// Helper trait which maps empty paths to "."
trait RelPath {
    fn rel_path(&self) -> &Path;
    fn rel_parent(&self) -> Option<&Path>;
}

impl RelPath for Path {
    fn rel_path(&self) -> &Path {
        if self.as_os_str().is_empty() {
            Path::new(".")
        } else {
            self
        }
    }

    fn rel_parent(&self) -> Option<&Path> {
        if self == Path::new(".") {
            return None;
        }

        Some(self.parent()?.rel_path())
    }
}

/// In-memory state for a single watched filesystem entry.
#[derive(Default)]
struct FsState {
    /// Whether the target currently exists on disk.
    exists:	bool,
    /// Whether the target has changed since last reset.
    changed:	bool,
}

/// Logical representation of one watched path plus its state.
struct FsEntry<'a> {
    path:	&'a Path,
    state:	RefCell<FsState>,
}

impl <'a> FsEntry<'a> {
    /// Return the watched path.
    pub fn as_path(&'a self) -> &'a Path {
        self.path
    }

    /// Return whether the path exists (according to last check).
    pub fn exists(&self) -> bool {
        self.state.borrow().exists
    }

    /// Return whether the path has changed since last reset.
    pub fn changed(&self) -> bool {
        self.state.borrow().changed
    }

    /// Reset the changed flag so future events can be detected.
    pub fn reset(&self) {
        self.state.borrow_mut().changed = false;
    }

    /// Mark the entry as changed.
    pub fn mark_changed(&self) {
        self.state.borrow_mut().changed = true;
    }

    /// Ensure the appropriate inotify watches are installed for this entry.
    ///
    /// When the path exists, install a watch on the file and on its direct
    /// parent directory.
    ///
    /// When the path does not exist, find the nearest existing ancestor and
    /// watch that directory, marking the watch as either `Directory` or
    /// `Parent` depending on how far away that ancestor is.
    fn check_path(self: &Rc<Self>, map: &mut WatchMap<'a>, watches: &mut Watches)
    {
        trace!("checking path {:?}", self.as_path());

        let path = self.as_path();
        let exists = path.exists();

        self.state.borrow_mut().exists = exists;

        if exists {
            trace!("  path exists; adding watches");

            let d = watches.add(path, WatchMask::CLOSE_WRITE | WatchMask::DELETE_SELF | WatchMask::MOVE_SELF)
                .inspect_err(|e| error!("failed to watch {path:?}: {e:?}"))
                .unwrap();

            map.vec_insert(d, WatchEntry::File(self.clone()));

            if let Some(dir) = path.rel_parent() {
                let d = watches.add(dir, WatchMask::CREATE | WatchMask::MOVED_TO | WatchMask::MOVE_SELF)
                    .inspect_err(|e| error!("failed to watch {dir:?}: {e:?}"))
                    .unwrap();

                map.vec_insert(d, WatchEntry::Directory(self.clone()));
            }
        } else {
            let info = path.ancestors()
                .map(|p| p.rel_path())
                .enumerate()
                .find(|(_, p)| p.exists());

            let Some((idx, dir)) = info else {
                warn!("no parent information found for {path:?}");
                return;
            };

            let d = watches.add(dir, WatchMask::CREATE | WatchMask::MOVED_TO | WatchMask::MOVE_SELF)
                .inspect_err(|e| error!("failed to watch {dir:?}: {e:?}"))
                .unwrap();

            let e = if idx == 0 {
                trace!("  path does not exist; adding watches to direct parent directory {dir:?}");
                WatchEntry::Directory(self.clone())
            } else {
                trace!("  path does not exist; adding watches to indirect parent directory {dir:?}");
                WatchEntry::Parent(self.clone())
            };

            map.vec_insert(d, e);
        }
    }
}

impl <'a> From<&'a Path> for FsEntry<'a> {
    fn from(path: &'a Path) -> Self {
        Self { path, state: Default::default() }
    }
}

#[derive(Clone)]
enum WatchEntry<'a> {
    File(Rc<FsEntry<'a>>),
    // The directory where the entry is directly located
    Directory(Rc<FsEntry<'a>>),
    // Some parent directory of the directory where the entry is located
    Parent(Rc<FsEntry<'a>>),
}

impl std::fmt::Debug for WatchEntry<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::File(arg0) => f.debug_tuple("File").field(&arg0.path).finish(),
            Self::Directory(arg0) => f.debug_tuple("Directory").field(&arg0.path).finish(),
            Self::Parent(arg0) => f.debug_tuple("Parent").field(&arg0.path).finish(),
        }
    }
}

#[derive(clap::Parser, Debug)]
struct Args {
    /// Watch the given path for changes; option can be specified multiple times.
    #[arg(short('w'), long, value_name("PATH"))]
    watch:		Vec<PathBuf>,

    /// Signal to terminate the program gracefully; when program is still
    /// alive after `--term-timeout`, `--sigkill` will be sent.
    #[arg(long, default_value = "15", value_parser = parse_signal, value_name("SIGNAL"))]
    sigterm:		Signal,

    /// Signal to terminate the program forcefully.  Sent when program is still
    /// alive after sending `--sigterm` and waiting for `--term-timeout`.
    #[arg(long, default_value = "9", value_parser = parse_signal, value_name("SIGNAL"))]
    sigkill:		Signal,

    /// Wait the given duration after sending the `--sigterm` signal for
    /// process termination.  When program is still alive after this time,
    /// send the `--sigkill` signal.
    #[arg(long, default_value = "2s", value_parser = parse_timespec, value_name("DURATION"))]
    term_timeout:	Timespec,

    /// Run the program only the given time and exit then
    #[arg(long, conflicts_with="oneshot", value_name("COUNT"))]
    max_restart:	Option<u32>,

    /// Run the program only once and exit then
    #[arg(long, short('1'))]
    oneshot:		Option<bool>,

    /// Wait this time before starting the program.
    #[arg(long, default_value = "500ms", value_parser = humantime::parse_duration, value_name("DURATION"))]
    settle_time:	Duration,

    /// Execute the given command when any of the watched entries is missing.
    /// Program is executed in a shell
    #[arg(long, value_name("SHCMD"))]
    build:		Option<OsString>,

    /// Program to run; when not given, wait-file exits after events have been
    /// detected
    #[arg()]
    argv:		Vec<OsString>,
}

fn parse_timespec(tm: &str) -> Result<Timespec, clap::Error> {
    humantime::parse_duration(tm)
        .map_err(|_| clap::Error::new(clap::error::ErrorKind::InvalidValue))?
        .try_into()
        .map_err(|_| clap::Error::new(clap::error::ErrorKind::ValueValidation))
}

fn parse_signal(sig: &str) -> Result<Signal, clap::Error> {
    match sig.to_lowercase().as_str() {
        "kill"	=> Ok(Signal::KILL),
        "term"	=> Ok(Signal::TERM),
        "hup"	=> Ok(Signal::HUP),
        "int"	=> Ok(Signal::INT),
        "quit"	=> Ok(Signal::QUIT),
        "abort"	=> Ok(Signal::ABORT),
        "usr1"	=> Ok(Signal::USR1),
        "usr2"	=> Ok(Signal::USR2),
        "cont"	=> Ok(Signal::CONT),
        "stop"	=> Ok(Signal::STOP),
        v	=> {
            v.parse()
                .map_err(|_| clap::Error::new(clap::error::ErrorKind::InvalidValue))
                .map(Signal::from_named_raw)?
                .ok_or_else(|| clap::Error::new(clap::error::ErrorKind::ValueValidation))
        }
    }
}

type WatchMap<'a> = HashMap<WatchDescriptor, Vec<WatchEntry<'a>>>;

/// Convenience trait for manipulating the vector values in `WatchMap`.
trait MapUpdate<'a> {
    fn vec_insert(&mut self, wd: WatchDescriptor, e: WatchEntry<'a>);
    fn vec_extend(&mut self, wd: WatchDescriptor, e: Vec<WatchEntry<'a>>);
}

impl <'a> MapUpdate<'a> for WatchMap<'a> {
    fn vec_insert(&mut self, wd: WatchDescriptor, e: WatchEntry<'a>) {
        match self.entry(wd) {
            Entry::Occupied(mut entry) => {
                entry.get_mut().push(e);
            }

            Entry::Vacant(entry) => {
                entry.insert(vec![e]);
            }
        }
    }

    fn vec_extend(&mut self, wd: WatchDescriptor, e: Vec<WatchEntry<'a>>) {
        match self.entry(wd) {
            Entry::Occupied(mut entry) => {
                entry.get_mut().extend(e);
            }

            Entry::Vacant(entry) => {
                entry.insert(e);
            }
        }
    }
}

fn init_paths<'a>(map: &mut WatchMap<'a>, watches: &mut Watches, paths: &[Rc<FsEntry<'a>>])
{
    for p in paths {
        p.reset();
        p.check_path(map, watches);
    }
}

fn check_event<'a>(map: &mut WatchMap<'a>, watches: &mut Watches, ev: Event<&OsStr>) {
    trace!("checking event {ev:?}");

    let Some((wd, entries)) = map.remove_entry(&ev.wd) else {
        // `watches.remove()` creates an extra IGNORED event
        if ev.mask != EventMask::IGNORED {
            warn!("wd {:?} from event not found", ev.wd);
        }
        return;
    };

    let mut new_entries = Vec::with_capacity(entries.len());
    let mut do_remove = ev.mask.contains(EventMask::IGNORED);

    let mut recheck = |e: Rc<FsEntry<'a>>, _ev: &Event<_>| {
        e.check_path(map, watches);
    };

    let fname = ev.name;

    for entry in entries {
        let entry = entry.clone();

        match entry {
            WatchEntry::File(e)	=> {
                debug!("event on file {:?}", e.as_path());

                if ev.mask.contains(EventMask::MOVE_SELF) || ev.mask.contains(EventMask::DELETE_SELF) {
                    do_remove = true;
                    recheck(e, &ev);
                } else if ev.mask.contains(EventMask::CLOSE_WRITE) {
                    e.mark_changed();
                    new_entries.push(WatchEntry::File(e));
                }
            }

            WatchEntry::Directory(e) if e.as_path().file_name() == fname	=> {
                debug!("event on direct parent directory of {:?}", e.as_path());

                e.mark_changed();
                recheck(e, &ev);
            }

            e @ WatchEntry::Directory(_)	=> {
                // some other, unwatched file in the directory containing a
                // watched file has been changed; no action needed, just readd
                // the entry
                new_entries.push(e);
            }

            WatchEntry::Parent(e)		=> recheck(e, &ev),
        }
    }

    if do_remove && new_entries.is_empty() {
        let wd = match map.entry(wd) {
            Entry::Occupied(entry) if entry.get().is_empty()	=> {
                trace!("  removing watch from map");
                let (wd, _) = entry.remove_entry();
                Some(wd)
            },

            Entry::Occupied(_)		=> {
                trace!("  watch readded to map");
                None
            },

            Entry::Vacant(entry)	=> {
                trace!("  orphan wd");
                Some(entry.into_key())
            }
        };

        if !ev.mask.contains(EventMask::IGNORED) && let Some(wd) = wd {
            debug!("  removing wd {wd:?} from watches");
            let _ = watches.remove(wd);
        }
    } else {
        trace!("  readding entries {new_entries:?}");
        map.vec_extend(wd, new_entries);
    }
}

/// Wrapper around a child process and its pidfd for detecting termination.
struct Process {
    child:	Child,
    pid_fd:	OwnedFd,
}

impl Process {
    pub fn run_shell(cmd: &OsStr) -> Result<(), ()> {
        Command::new("/bin/sh")
            .arg("-c")
            .arg(cmd)
            .status()
            .inspect_err(|e| warn!("build command terminated abnormally: {e:?}"))
            .map_err(|_| ())
            .inspect(|c| if !c.success() { warn!("build command failed: {c:?}") })?
            .success()
            .then_some(())
            .ok_or(())
    }

    pub fn new(args: &[OsString]) -> Self {
        let child = Command::new(&args[0])
            .args(&args[1..])
            .spawn()
            .expect("failed to run program");

        let pid = Pid::from_child(&child);

        let pid_fd = pidfd_open(pid, PidfdFlags::empty())
            .expect("failed to open pidfd");

        Self { child, pid_fd }
    }

    pub fn terminate(self, args: &Args) {
        let mut child = self.child;
        let pid = Pid::from_child(&child);
        let fd  = self.pid_fd;
        let res = (|| {
            if let Some(res) = child.try_wait().unwrap() {
                return res;
            }

            let _ = kill_process(pid, args.sigterm);

            let mut pfds = [
                PollFd::new(&fd, PollFlags::IN)
            ];

            let _ = poll(&mut pfds, Some(&args.term_timeout));

            if let Some(res) = child.try_wait().unwrap() {
                return res;
            }

            let _ = kill_process(pid, args.sigkill);

            child.wait().unwrap()
        })();

        if !res.success() {
            warn!("program failed: {res:?}");
        }
    }
}

impl Args {
    fn wait_for_files(&self, paths: &[Rc<FsEntry<'_>>], child: Option<BorrowedFd<'_>>) -> bool {
        let mut inotify = Inotify::init()
            .expect("Error while initializing inotify instance");

        let mut watches = inotify.watches();
        let mut map = WatchMap::new();

        init_paths(&mut map, &mut watches, paths);

        loop {
            let all_exists  = paths.iter().all(|p| p.exists());
            let any_changed = paths.iter().any(|p| p.changed());

            debug!("  exists={all_exists}, changed={any_changed}");

            if all_exists && (child.is_none() || any_changed) {
                break true;
            }

            if !all_exists && let Some(cmd) = &self.build {
                let _ = Process::run_shell(cmd);
            }

            let mut pfds = [
                PollFd::new(&inotify, PollFlags::IN),
                PollFd::new(&inotify, PollFlags::empty()), // dummy; to be replaced by child
            ];

            let pfds = if let Some(fd) = &child {
                pfds[1] = PollFd::new(fd, PollFlags::IN);
                &mut pfds[..]
            } else {
                &mut pfds[..1]
            };

            let _ = poll(pfds, None);

            if child.is_some() && pfds[1].revents().contains(PollFlags::IN) {
                break false;
            }

            let mut buffer = [MaybeUninit::<u8>::uninit(); 2048];
            loop {
                let buffer = unsafe {
                    // SAFETY: inotify will initialize the bytes it reads, and we only
                    // access the initialized portion returned by read_events()
                    core::mem::transmute::<&mut [MaybeUninit<u8>], &mut [u8]>(&mut buffer)
                };
                match inotify.read_events(buffer) {
                    Ok(events)	=> events.for_each(|ev| check_event(&mut map, &mut watches, ev)),
                    Err(e) if e.kind() == ErrorKind::WouldBlock => break,
                    Err(e)	=> panic!("fail to read events: {e:?}"),
                }
            }
        }
    }

    pub fn run(self, exit: Exit) {
        let paths: Vec<_> = self.watch.iter().map(|w| Rc::new(w.as_path().into()))
            .collect();

        debug!("watching {:?}", self.watch);

        let mut process: Option<Process> = None;
        let mut max_restart = self.max_restart
            .or_else(|| self.oneshot.and_then(|v| v.then_some(1)));

        loop {
            let run_prog = self.wait_for_files(&paths, process.as_ref().map(|p| p.pid_fd.as_fd()));

            if let Some(child) = process.take() {
                child.terminate(&self);

                if let Some(v) = &mut max_restart && *v > 0 {
                    *v -= 1;
                }
            }

            if !exit.is_running() {
                break;
            }

            if max_restart == Some(0) {
                break;
            }

            if !run_prog {
                continue;
            }

            if self.argv.is_empty() {
                break;
            }

            std::thread::sleep(self.settle_time);

            process = Some(Process::new(&self.argv));
        }
    }
}

fn main() {
    env_logger::init();

    let args = Args::parse();

    args.run(Exit::new())
}

#[cfg(not(test))]
mod test_stub {
    pub struct Exit;

    impl Exit {
        pub const fn new() -> Self {
            Self
        }

        pub const fn is_running(&self) -> bool {
            true
        }
    }
}

#[cfg(not(test))]
use test_stub as test;

#[cfg(test)]
mod test;

use test::Exit;