-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathlib.rs
30 lines (27 loc) · 941 Bytes
/
lib.rs
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
use std::path::PathBuf;
use anyhow::Context as _;
use nix::sys::utsname::uname;
/// Kernel modules are in `/lib/modules`.
/// They may be in the root of this directory,
/// or in subdirectory named after the kernel release.
pub fn resolve_modules_dir() -> anyhow::Result<PathBuf> {
let modules_dir = PathBuf::from("/lib/modules");
let stat = modules_dir
.metadata()
.with_context(|| format!("{} doesn't exist", modules_dir.display()))?;
if stat.is_dir() {
return Ok(modules_dir);
}
let utsname = uname().context("failed to get kernel release")?;
let release = utsname.release();
let modules_dir = modules_dir.join(release);
let stat = modules_dir
.metadata()
.with_context(|| format!("{} doesn't exist", modules_dir.display()))?;
anyhow::ensure!(
stat.is_dir(),
"{} is not a directory",
modules_dir.display()
);
Ok(modules_dir)
}