crabshell-services 0.2.1

Crabshell's services: workspaces, tray, audio, clock, power, Bluetooth, network, media players, notifications, apps, programs, files, PAM, greetd and polkit
Documentation
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
//! Files a config reads and writes, as Quickshell's `FileView` and
//! `JsonAdapter`.

#[cfg(test)]
mod tests;

use std::cell::{Cell, RefCell};
use std::ffi::OsString;
use std::io::{ErrorKind, Read, Write};
use std::panic::{self, AssertUnwindSafe};
use std::path::{Path, PathBuf};
use std::rc::{Rc, Weak};

use crabshell_reactive::{ReadSignal, Signal, untrack};
use crabshell_ui::Cx;
use inotify::EventMask;
use serde::Serialize;
use serde::de::DeserializeOwned;

use crate::Shared;
use crate::lines::panic_message;
use crate::watch::{self, Watch};

/// The largest file read; a larger one reads as missing, and that is
/// logged.
pub(crate) const MAX_FILE: u64 = 16 * 1024 * 1024;

/// What `cx.file_view` and `cx.json_file` need from the shell: the loop,
/// to watch files.
pub(crate) struct Files(pub(crate) Rc<Shared>);

/// Follows the file at a path through whatever happens to the directories
/// on the way: one not made yet is waited for from the nearest one that
/// is there, and one removed, renamed or made again is followed anew.
// shortcut: watches the directory the path names, not a link's target's,
// so an edit through another name of a linked file isn't seen; follow
// the link when a config needs that.
struct Follow {
  shared: Rc<Shared>,
  path: PathBuf,
  watch: RefCell<Option<Watch>>,
  /// Reads the file again.
  changed: Box<dyn Fn()>,
}

impl Follow {
  /// Calls `changed` when the file at `path` may have changed: written,
  /// replaced or removed.
  fn new(shared: &Rc<Shared>, path: PathBuf, changed: impl Fn() + 'static) -> Rc<Follow> {
    let follow = Rc::new(Follow {
      shared: Rc::clone(shared),
      path,
      watch: RefCell::new(None),
      changed: Box::new(changed),
    });
    follow.rewatch();
    follow
  }

  /// Watches the file's directory, or while it is missing the nearest
  /// directory above it that is there, for the next name on the way.
  fn rewatch(self: &Rc<Self>) {
    self.watch.replace(None);
    let (Some(dir), Some(name)) = (self.path.parent(), self.path.file_name()) else {
      return;
    };
    let mut dir = if dir.as_os_str().is_empty() {
      Path::new(".")
    } else {
      dir
    };
    let mut next: OsString = name.to_owned();
    let own = dir.is_dir();
    while !dir.is_dir() {
      let (Some(above), Some(name)) = (dir.parent(), dir.file_name()) else {
        return;
      };
      next = name.to_owned();
      dir = if above.as_os_str().is_empty() {
        Path::new(".")
      } else {
        above
      };
    }
    let weak = Rc::downgrade(self);
    let listener = move |change: &watch::Change| {
      let Some(follow) = weak.upgrade() else {
        return;
      };
      let moved = EventMask::DELETE_SELF | EventMask::MOVE_SELF;
      match &change.name {
        // The directory itself went, or changes were lost: start over.
        None => {
          if change.mask.intersects(moved) || change.mask.contains(EventMask::Q_OVERFLOW) {
            follow.rewatch();
          }
          (follow.changed)();
        }
        // Not on a create, when a writer hasn't written yet: its close
        // comes next.
        Some(changed) if *changed == next && own => {
          if !change.mask.contains(EventMask::CREATE) {
            (follow.changed)();
          }
        }
        // A directory on the way came or went.
        Some(changed) if *changed == next => {
          follow.rewatch();
          (follow.changed)();
        }
        Some(_) => {}
      }
    };
    match watch::watch(&self.shared, dir, listener) {
      Ok(watch) => {
        self.watch.replace(Some(watch));
      }
      Err(error) => {
        let path = self.path.display();
        tracing::info!(%path, %error, "can't watch the file, so changes to it aren't seen");
      }
    }
  }
}

/// Keeps a file's watch for as long as the opening context's owner.
struct Keep<T>(#[allow(dead_code, reason = "held for its drop")] Rc<T>);

/// A text file, from `cx.file_view(path)`. Its text is read at once and
/// again when it changes on disk. A clone is cheap and shares it.
#[derive(Clone)]
pub struct FileView {
  inner: Rc<ViewInner>,
}

struct ViewInner {
  path: PathBuf,
  follow: RefCell<Option<Rc<Follow>>>,
  /// Lives under the opening context's owner.
  text: Signal<Option<String>>,
  /// The last error logged, so a file that stays missing logs once.
  logged: RefCell<Option<ErrorKind>>,
}

/// `cx.file_view(path)`.
pub(crate) fn file_view(cx: &Cx, path: PathBuf) -> FileView {
  let inner = Rc::new(ViewInner {
    text: cx.signal(None),
    follow: RefCell::new(None),
    path,
    logged: RefCell::new(None),
  });
  inner.reread();
  if let Some(files) = cx.service::<Files>() {
    let weak = Rc::downgrade(&inner);
    let follow = Follow::new(&files.0, inner.path.clone(), move || {
      if let Some(inner) = Weak::upgrade(&weak) {
        inner.reread();
      }
    });
    inner.follow.replace(Some(follow));
  }
  // So `text()` follows the file even once every `FileView` is dropped.
  cx.owner().signal(Keep(Rc::clone(&inner)));
  FileView { inner }
}

impl FileView {
  /// The file's text, or `None` while it is missing, can't be read, isn't
  /// UTF-8, or is over 16 MiB, each logged once until it reads again.
  pub fn text(&self) -> ReadSignal<Option<String>> {
    self.inner.text.read_only()
  }

  /// Replaces the file's text, through a new file beside it renamed over
  /// it, so a reader never sees half of it, making its directory if it is
  /// missing. A link is followed and the file it names is replaced,
  /// keeping its permissions. A failure is logged and the text stays as
  /// it was.
  pub fn write(&self, text: impl AsRef<str>) {
    let text = text.as_ref();
    match write_whole(&self.inner.path, text.as_bytes()) {
      Ok(()) => set(self.inner.text, Some(text.to_owned())),
      Err(error) => {
        let path = self.inner.path.display();
        tracing::warn!(%path, %error, "couldn't write the file");
      }
    }
  }
}

impl ViewInner {
  /// Reads the file again and shows what it holds now.
  fn reread(&self) {
    let text = match read_text(&self.path) {
      Ok(text) => {
        self.logged.replace(None);
        Some(text)
      }
      Err(error) => {
        if self.logged.replace(Some(error.kind())) != Some(error.kind()) {
          let path = self.path.display();
          tracing::info!(%path, %error, "can't read the file");
        }
        None
      }
    };
    set(self.text, text);
  }
}

/// A JSON file kept in step with a signal: `cx.json_file(path, default)`.
pub(crate) fn json_file<T>(cx: &Cx, path: PathBuf, default: T) -> Signal<T>
where
  T: Serialize + DeserializeOwned + PartialEq + 'static,
{
  let value = cx.signal(default);
  let file = Rc::new(JsonInner {
    path,
    value,
    // What the file holds, as far as this shell knows.
    on_disk: RefCell::new(None),
    logged: RefCell::new(None),
    follow: RefCell::new(None),
  });
  file.reread();
  if let Some(files) = cx.service::<Files>() {
    let weak = Rc::downgrade(&file);
    let follow = Follow::new(&files.0, file.path.clone(), move || {
      if let Some(file) = Weak::upgrade(&weak) {
        file.reread();
      }
    });
    file.follow.replace(Some(follow));
  }
  // The value it starts from, so the first run doesn't make a missing
  // file the config never set.
  let start = untrack(|| value.try_with(|value| file.to_json(value))).flatten();
  let writer = Rc::clone(&file);
  let first = Cell::new(true);
  cx.effect(move || {
    let text = value.with(|value| writer.to_json(value));
    let unset = first.replace(false) && start.is_some() && text == start;
    if let (Some(text), false) = (text, unset) {
      untrack(|| writer.write(text));
    }
  })
  .named(format!("cx.json_file {}", file.path.display()));
  value
}

struct JsonInner<T: 'static> {
  path: PathBuf,
  value: Signal<T>,
  on_disk: RefCell<Option<String>>,
  /// The last read problem logged, so it logs once until it reads again.
  logged: RefCell<Option<String>>,
  follow: RefCell<Option<Rc<Follow>>>,
}

impl<T> JsonInner<T>
where
  T: Serialize + DeserializeOwned + PartialEq + 'static,
{
  /// Reads the file again. A missing file keeps the value; one that
  /// isn't the type's JSON is logged and keeps it too. The config's
  /// `Deserialize`, `Serialize` and `PartialEq` may panic, and this may
  /// run from the loop, where nothing else would catch them.
  fn reread(&self) {
    let text = match read_text(&self.path) {
      Ok(text) => text,
      Err(error) => {
        self.log_once(format!("can't read the file: {error}"));
        return;
      }
    };
    let applied = panic::catch_unwind(AssertUnwindSafe(|| {
      let read = serde_json::from_str::<T>(&text)?;
      let normal = serde_json::to_string_pretty(&read).unwrap_or_else(|_| text.clone());
      self.on_disk.replace(Some(normal));
      set(self.value, read);
      serde_json::Result::Ok(())
    }));
    match applied {
      Ok(Ok(())) => {
        self.logged.replace(None);
      }
      Ok(Err(error)) => self.log_once(format!("the file isn't the JSON expected: {error}")),
      Err(payload) => {
        let message = panic_message(payload.as_ref());
        self.log_once(format!("reading it panicked: {message}"));
      }
    }
  }

  /// `value` as JSON, or `None` when the config's `Serialize` fails or
  /// panics, which is logged.
  fn to_json(&self, value: &T) -> Option<String> {
    let path = self.path.display();
    match panic::catch_unwind(AssertUnwindSafe(|| serde_json::to_string_pretty(value))) {
      Ok(Ok(text)) => Some(text),
      Ok(Err(error)) => {
        tracing::warn!(%path, %error, "couldn't turn the value into JSON");
        None
      }
      Err(payload) => {
        let message = panic_message(payload.as_ref());
        tracing::error!("cx.json_file {path}: turning the value into JSON panicked: {message}");
        None
      }
    }
  }

  fn write(&self, text: String) {
    if self.on_disk.borrow().as_deref() == Some(text.as_str()) {
      return;
    }
    match write_whole(&self.path, format!("{text}\n").as_bytes()) {
      Ok(()) => {
        self.on_disk.replace(Some(text));
      }
      Err(error) => {
        let path = self.path.display();
        tracing::warn!(%path, %error, "couldn't write the file");
      }
    }
  }

  fn log_once(&self, problem: String) {
    if self.logged.borrow().as_deref() != Some(problem.as_str()) {
      let path = self.path.display();
      tracing::info!(%path, problem, "the JSON file keeps its value");
      self.logged.replace(Some(problem));
    }
  }
}

/// The whole of `path` as UTF-8, up to [`MAX_FILE`].
fn read_text(path: &Path) -> std::io::Result<String> {
  let file = std::fs::File::open(path)?;
  let mut bytes = Vec::new();
  file.take(MAX_FILE + 1).read_to_end(&mut bytes)?;
  if bytes.len() as u64 > MAX_FILE {
    return Err(std::io::Error::other("it is over 16 MiB"));
  }
  String::from_utf8(bytes)
    .map_err(|_| std::io::Error::new(ErrorKind::InvalidData, "it isn't UTF-8"))
}

/// Replaces the file at `path` with `bytes` through a new file beside it,
/// renamed over it, making its directory if it is missing. A link is
/// followed, so the file it names is replaced, and a file's permissions
/// are kept.
fn write_whole(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
  use std::os::unix::fs::OpenOptionsExt;

  if let Some(dir) = path.parent()
    && !dir.as_os_str().is_empty()
    && !dir.exists()
  {
    std::fs::create_dir_all(dir)?;
  }
  let target = match std::fs::canonicalize(path) {
    Ok(target) => target,
    Err(error) if error.kind() == ErrorKind::NotFound => path.to_owned(),
    Err(error) => return Err(error),
  };
  let name = target
    .file_name()
    .ok_or_else(|| std::io::Error::other("the path names no file"))?;
  let mut partial_name = std::ffi::OsString::from(".");
  partial_name.push(name);
  partial_name.push(".crabshell-partial");
  let partial = target.with_file_name(partial_name);
  match std::fs::remove_file(&partial) {
    Err(error) if error.kind() != ErrorKind::NotFound => return Err(error),
    _ => {}
  }
  let mode = std::fs::metadata(&target).map_or(0o644, |meta| {
    use std::os::unix::fs::PermissionsExt;
    meta.permissions().mode() & 0o7777
  });
  // New, so it never writes through a link left in its place.
  let mut file = std::fs::OpenOptions::new()
    .write(true)
    .create_new(true)
    .mode(mode)
    .open(&partial)?;
  let written = file.write_all(bytes).and_then(|()| file.sync_all());
  drop(file);
  if let Err(error) = written.and_then(|()| std::fs::rename(&partial, &target)) {
    let _ = std::fs::remove_file(&partial);
    return Err(error);
  }
  Ok(())
}

/// Sets `signal` unless it holds `value` or is gone. The read is
/// untracked, so an effect that opens a file doesn't run again for it.
fn set<T: PartialEq + 'static>(signal: Signal<T>, value: T) {
  if untrack(|| signal.try_with(|old| *old != value)) == Some(true) {
    signal.try_set(value);
  }
}