|
1 | 1 | // SPDX-FileCopyrightText: Copyright © 2020-2024 Serpent OS Developers |
2 | 2 | // |
3 | 3 | // SPDX-License-Identifier: MPL-2.0 |
4 | | -use std::env::set_current_dir; |
| 4 | + |
5 | 5 | use std::io; |
6 | 6 | use std::os::fd::AsRawFd; |
7 | 7 | use std::path::{Path, PathBuf}; |
8 | 8 | use std::process::Command; |
9 | 9 | use std::ptr::addr_of_mut; |
10 | 10 | use std::sync::atomic::{AtomicI32, Ordering}; |
11 | 11 |
|
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 _}; |
13 | 13 | use nix::libc::SIGCHLD; |
14 | 14 | use nix::mount::{mount, umount2, MntFlags, MsFlags}; |
15 | 15 | use nix::sched::{clone, CloneFlags}; |
@@ -265,7 +265,7 @@ fn pivot(root: &Path, binds: &[Bind]) -> Result<(), ContainerError> { |
265 | 265 | add_mount(Some(root), root, None, MsFlags::MS_BIND)?; |
266 | 266 |
|
267 | 267 | for bind in binds { |
268 | | - let source = bind.source.canonicalize()?; |
| 268 | + let source = bind.source.fs_err_canonicalize()?; |
269 | 269 | let target = root.join(bind.target.strip_prefix("/").unwrap_or(&bind.target)); |
270 | 270 |
|
271 | 271 | add_mount(Some(&source), &target, None, MsFlags::MS_BIND)?; |
@@ -358,6 +358,26 @@ fn add_mount<T: AsRef<Path>>( |
358 | 358 | Ok(()) |
359 | 359 | } |
360 | 360 |
|
| 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 | + |
361 | 381 | fn ignore_sigint() -> Result<(), nix::Error> { |
362 | 382 | let action = SigAction::new(SigHandler::SigIgn, SaFlags::empty(), SigSet::empty()); |
363 | 383 | unsafe { sigaction(Signal::SIGINT, &action)? }; |
@@ -450,8 +470,8 @@ pub enum Error { |
450 | 470 | #[derive(Debug, Error)] |
451 | 471 | enum ContainerError { |
452 | 472 | #[error(transparent)] |
453 | | - Run(#[from] Box<dyn std::error::Error>), |
454 | | - #[error(transparent)] |
| 473 | + Run(Box<dyn std::error::Error>), |
| 474 | + #[error("io")] |
455 | 475 | Io(#[from] io::Error), |
456 | 476 |
|
457 | 477 | // Errors from linux system functions |
|
0 commit comments