Skip to content

Commit a4840c0

Browse files
committed
monitor: fix create_dir_all failure when updating screenshots (#6)
1 parent 6ad78c6 commit a4840c0

File tree

5 files changed

+36
-19
lines changed

5 files changed

+36
-19
lines changed

monitor/src/data.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tracing::info;
88

99
use crate::DOTENV;
1010

11-
pub fn get_data_path(path: impl AsRef<Path>) -> eyre::Result<PathBuf> {
11+
pub fn get_data_path<'p>(path: impl Into<Option<&'p Path>>) -> eyre::Result<PathBuf> {
1212
let data = if let Some(path) = &DOTENV.monitor_data_path {
1313
path.into()
1414
} else {
@@ -17,24 +17,39 @@ pub fn get_data_path(path: impl AsRef<Path>) -> eyre::Result<PathBuf> {
1717

1818
fs::create_dir_all(&data)?;
1919

20-
Ok(data.join(path))
20+
Ok(match path.into() {
21+
Some(path) => data.join(path),
22+
None => data,
23+
})
2124
}
2225

23-
pub fn get_runner_data_path(id: usize, path: impl AsRef<Path>) -> eyre::Result<PathBuf> {
24-
let runner_data = get_data_path("runners")?.join(id.to_string());
26+
pub fn get_runner_data_path<'p>(
27+
id: usize,
28+
path: impl Into<Option<&'p Path>>,
29+
) -> eyre::Result<PathBuf> {
30+
let runner_data = get_data_path(Path::new("runners"))?.join(id.to_string());
2531

26-
Ok(runner_data.join(path))
32+
Ok(match path.into() {
33+
Some(path) => runner_data.join(path),
34+
None => runner_data,
35+
})
2736
}
2837

29-
pub fn get_profile_data_path(key: &str, path: impl AsRef<Path>) -> eyre::Result<PathBuf> {
30-
let profile_data = get_data_path("profiles")?.join(key);
38+
pub fn get_profile_data_path<'p>(
39+
key: &str,
40+
path: impl Into<Option<&'p Path>>,
41+
) -> eyre::Result<PathBuf> {
42+
let profile_data = get_data_path(Path::new("profiles"))?.join(key);
3143

32-
Ok(profile_data.join(path))
44+
Ok(match path.into() {
45+
Some(path) => profile_data.join(path),
46+
None => profile_data,
47+
})
3348
}
3449

3550
#[tracing::instrument]
3651
pub fn run_migrations() -> eyre::Result<()> {
37-
let migrations_dir = get_data_path("migrations")?;
52+
let migrations_dir = get_data_path(Path::new("migrations"))?;
3853
create_dir_all(&migrations_dir)?;
3954

4055
for version in 1.. {
@@ -45,9 +60,9 @@ pub fn run_migrations() -> eyre::Result<()> {
4560
match version {
4661
1 => {
4762
info!("Moving per-runner data to runners subdirectory");
48-
let runners_dir = get_data_path("runners")?;
63+
let runners_dir = get_data_path(Path::new("runners"))?;
4964
create_dir_all(&runners_dir)?;
50-
for entry in read_dir(get_data_path(".")?)? {
65+
for entry in read_dir(get_data_path(None)?)? {
5166
let entry = entry?;
5267
// Move entries that parse as a runner id (usize)
5368
if entry

monitor/src/id.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
fs::File,
33
io::{Read, Write},
4+
path::Path,
45
};
56

67
use jane_eyre::eyre::{self, Context};
@@ -15,7 +16,7 @@ pub struct IdGen {
1516

1617
impl IdGen {
1718
pub fn new_load() -> eyre::Result<Self> {
18-
if let Ok(mut file) = File::open(get_data_path("last-runner-id")?) {
19+
if let Ok(mut file) = File::open(get_data_path(Path::new("last-runner-id"))?) {
1920
let mut last = String::default();
2021
file.read_to_string(&mut last)
2122
.wrap_err("Failed to read last runner id")?;
@@ -45,8 +46,8 @@ impl IdGen {
4546
}
4647

4748
fn write_last(&self, last: usize) -> eyre::Result<()> {
48-
let path = get_data_path("last-runner-id")?;
49-
let new_path = get_data_path("last-runner-id.new")?;
49+
let path = get_data_path(Path::new("last-runner-id"))?;
50+
let new_path = get_data_path(Path::new("last-runner-id.new"))?;
5051
let mut file = File::create(&new_path)?;
5152
file.write_all(last.to_string().as_bytes())?;
5253
std::fs::rename(&new_path, &path)?;

monitor/src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::{
1414
fs::File,
1515
io::Read,
1616
net::IpAddr,
17+
path::Path,
1718
process::exit,
1819
sync::{LazyLock, RwLock},
1920
thread,
@@ -232,7 +233,7 @@ async fn main() -> eyre::Result<()> {
232233
.and(warp::filters::method::get())
233234
.and(warp::filters::header::optional("If-None-Match"))
234235
.and_then(|runner_id, if_none_match: Option<String>| async move {
235-
let path = get_runner_data_path(runner_id, "screenshot.png")
236+
let path = get_runner_data_path(runner_id, Path::new("screenshot.png"))
236237
.wrap_err("Failed to compute path")
237238
.map_err(InternalError)?;
238239
let mut file = File::open(path)

monitor/src/profile.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl Profile {
177177
}
178178

179179
fn try_update_screenshot(&self) -> eyre::Result<()> {
180-
let output_dir = get_profile_data_path(&self.base_vm_name, ".")?;
180+
let output_dir = get_profile_data_path(&self.base_vm_name, None)?;
181181
update_screenshot(&self.base_vm_name, &output_dir)?;
182182

183183
Ok(())

monitor/src/runner.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{
22
collections::{BTreeMap, BTreeSet},
33
fmt::Debug,
44
fs,
5-
path::PathBuf,
5+
path::{Path, PathBuf},
66
process::Command,
77
time::{Duration, SystemTime, UNIX_EPOCH},
88
};
@@ -214,7 +214,7 @@ impl Runners {
214214
let Some(guest_name) = runner.guest_name.as_deref() else {
215215
bail!("Tried to screenshot a runner with no libvirt guest: {id}");
216216
};
217-
let output_dir = get_runner_data_path(id, ".")?;
217+
let output_dir = get_runner_data_path(id, None)?;
218218
update_screenshot(guest_name, &output_dir)?;
219219

220220
Ok(())
@@ -226,7 +226,7 @@ impl Runner {
226226
///
227227
/// For use by [`Runners::new`] only. Does not create a runner.
228228
fn new(id: usize) -> eyre::Result<Self> {
229-
let created_time = get_runner_data_path(id, "created-time")?;
229+
let created_time = get_runner_data_path(id, Path::new("created-time"))?;
230230
let created_time = fs::metadata(created_time)?.modified()?;
231231
trace!(?created_time, "[{id}]");
232232

0 commit comments

Comments
 (0)