Skip to content

Commit 84498b7

Browse files
authored
Merge pull request #378 from jplatte/jplatte/container-error
Add more context to container errors
2 parents 2b66d7b + fa5d60f commit 84498b7

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

crates/container/src/lib.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
// SPDX-FileCopyrightText: Copyright © 2020-2024 Serpent OS Developers
22
//
33
// SPDX-License-Identifier: MPL-2.0
4-
use std::env::set_current_dir;
4+
55
use std::io;
66
use std::os::fd::AsRawFd;
77
use std::path::{Path, PathBuf};
88
use std::process::Command;
99
use std::ptr::addr_of_mut;
1010
use std::sync::atomic::{AtomicI32, Ordering};
1111

12-
use fs_err::{self as fs, copy, create_dir_all, remove_dir};
12+
use fs_err::{self as fs, copy, create_dir_all, remove_dir, PathExt as _};
1313
use nix::libc::SIGCHLD;
1414
use nix::mount::{mount, umount2, MntFlags, MsFlags};
1515
use nix::sched::{clone, CloneFlags};
@@ -265,7 +265,7 @@ fn pivot(root: &Path, binds: &[Bind]) -> Result<(), ContainerError> {
265265
add_mount(Some(root), root, None, MsFlags::MS_BIND)?;
266266

267267
for bind in binds {
268-
let source = bind.source.canonicalize()?;
268+
let source = bind.source.fs_err_canonicalize()?;
269269
let target = root.join(bind.target.strip_prefix("/").unwrap_or(&bind.target));
270270

271271
add_mount(Some(&source), &target, None, MsFlags::MS_BIND)?;
@@ -358,6 +358,26 @@ fn add_mount<T: AsRef<Path>>(
358358
Ok(())
359359
}
360360

361+
fn set_current_dir(path: impl AsRef<Path>) -> io::Result<()> {
362+
#[derive(Debug, Error)]
363+
#[error("failed to set current directory to `{}`", path.display())]
364+
struct SetCurrentDirError {
365+
source: io::Error,
366+
path: PathBuf,
367+
}
368+
369+
let path = path.as_ref();
370+
std::env::set_current_dir(path).map_err(|source| {
371+
io::Error::new(
372+
source.kind(),
373+
SetCurrentDirError {
374+
source,
375+
path: path.to_owned(),
376+
},
377+
)
378+
})
379+
}
380+
361381
fn ignore_sigint() -> Result<(), nix::Error> {
362382
let action = SigAction::new(SigHandler::SigIgn, SaFlags::empty(), SigSet::empty());
363383
unsafe { sigaction(Signal::SIGINT, &action)? };
@@ -450,8 +470,8 @@ pub enum Error {
450470
#[derive(Debug, Error)]
451471
enum ContainerError {
452472
#[error(transparent)]
453-
Run(#[from] Box<dyn std::error::Error>),
454-
#[error(transparent)]
473+
Run(Box<dyn std::error::Error>),
474+
#[error("io")]
455475
Io(#[from] io::Error),
456476

457477
// Errors from linux system functions

0 commit comments

Comments
 (0)