Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unmount disks on MacOS before writing #140

Merged
merged 2 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion nix/cross-helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ in rec {

crossParams = if host == target then {
cc = pkgs.stdenv.cc;
extraBuildEnv = { };
extraBuildEnv = {
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
};
} else rec {
cc = pkgsCross.stdenv.cc;
extraBuildEnv = {
Expand Down
5 changes: 5 additions & 0 deletions src/writer_process/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub enum ErrorType {
VerificationFailed,
UnexpectedTermination,
UnknownChildProcError(String),
FailedToUnmount { message: String, exit_code: i32 },
}

impl From<std::io::Error> for ErrorType {
Expand All @@ -75,6 +76,10 @@ impl Display for ErrorType {
ErrorType::UnknownChildProcError(err) => {
write!(f, "Unknown error occurred in child process: {err}")
}
ErrorType::FailedToUnmount { message, exit_code } => write!(
f,
"Failed to unmount disk (exit code {exit_code})\n{message}"
),
}
}
}
34 changes: 33 additions & 1 deletion src/writer_process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
//! IT IS NOT TO BE USED DIRECTLY BY THE USER! ITS API HAS NO STABILITY GUARANTEES!

use std::fs::OpenOptions;
use std::os::unix::process::ExitStatusExt;
use std::process::{Command, Stdio};
use std::{
fs::File,
io::{self, Read, Seek, Write},
};

use aligned_vec::avec_rt;
use interprocess::local_socket::{prelude::*, GenericFilePath};
use tracing::info;
use tracing::{debug, info};
use tracing_unwrap::ResultExt;

use crate::childproc_common::child_init;
Expand Down Expand Up @@ -60,6 +62,36 @@ pub fn main() {
}

fn run(mut tx: impl FnMut(StatusMessage), args: &WriterProcessConfig) -> Result<(), ErrorType> {
if cfg!(target_os = "macos") && args.target_type == device::Type::Disk {
let mut command = Command::new("diskutil");
command
.arg("unmountdisk")
.arg(&args.dest)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());

info!(?command, "spawning process to unmount disk");
let mut child = command.spawn()?;
debug!("successfully ran diskutil, waiting on child process");

let exit = child.wait()?;
let mut stderr = String::new();
child.stderr.take().unwrap().read_to_string(&mut stderr)?;
let mut stdout = String::new();
child.stdout.take().unwrap().read_to_string(&mut stdout)?;

debug!(?exit, ?stderr, "child exited");

let exit_code = exit.into_raw();
if !exit.success() {
return Err(ErrorType::FailedToUnmount {
message: format!("stderr: {stderr}\nstdout: {stdout}"),
exit_code,
});
}
}

info!("Opening file {}", args.src.to_string_lossy());
let mut file = File::open(&args.src).unwrap_or_log();
let size = file.seek(io::SeekFrom::End(0))?;
Expand Down