use std::io::Write;
use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CommandSpec {
pub program: String,
pub args: Vec<String>,
}
impl CommandSpec {
pub(crate) fn new(program: &str, args: &[&str]) -> Self {
Self {
program: program.to_owned(),
args: args.iter().map(|arg| (*arg).to_owned()).collect(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CandidateKind {
Mounted,
Unmounted,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeviceCandidate {
pub mount: PathBuf,
pub label: String,
pub kind: CandidateKind,
}
pub fn scan_candidates() -> Vec<DeviceCandidate> {
scan_under(Path::new("/run/media"))
}
pub fn scan_under(root: &Path) -> Vec<DeviceCandidate> {
let mut candidates = Vec::new();
let Ok(users) = std::fs::read_dir(root) else {
return candidates;
};
for user in users.flatten() {
let Ok(volumes) = std::fs::read_dir(user.path()) else {
continue;
};
for volume in volumes.flatten() {
if volume.path().join("iPod_Control").is_dir() {
candidates.push(DeviceCandidate {
label: volume.file_name().to_string_lossy().into_owned(),
mount: volume.path(),
kind: CandidateKind::Mounted,
});
}
}
}
candidates.sort_by(|a, b| a.mount.cmp(&b.mount));
candidates
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AppleDisk {
pub partition: PathBuf,
pub label: String,
}
pub fn scan_apple_disks(sys_block_root: &Path) -> Vec<AppleDisk> {
let mut disks = Vec::new();
let Ok(entries) = std::fs::read_dir(sys_block_root) else {
return disks;
};
for entry in entries.flatten() {
let sys_text = |rel: &str| -> String {
std::fs::read_to_string(entry.path().join(rel))
.map(|text| text.trim().to_owned())
.unwrap_or_default()
};
let vendor = sys_text("device/vendor");
if vendor != "Apple" {
continue;
}
let model = sys_text("device/model");
let Ok(parts) = std::fs::read_dir(entry.path()) else {
continue;
};
let data = parts
.flatten()
.filter(|part| part.path().join("partition").is_file())
.filter_map(|part| {
let size: u64 = std::fs::read_to_string(part.path().join("size"))
.ok()?
.trim()
.parse()
.ok()?;
let name = part.file_name().to_string_lossy().into_owned();
Some((size, name))
})
.max_by_key(|(size, _)| *size);
let Some((_, part_name)) = data else {
continue;
};
let label = if model.is_empty() {
vendor
} else {
format!("{vendor} {model}")
};
disks.push(AppleDisk {
partition: PathBuf::from("/dev").join(part_name),
label,
});
}
disks.sort_by(|a, b| a.partition.cmp(&b.partition));
disks
}
pub fn default_mount_path(label: &str, user: &str) -> PathBuf {
Path::new("/run/media").join(user).join(label)
}
pub fn merge_candidates(
mounted: Vec<(DeviceCandidate, Option<PathBuf>)>,
disks: Vec<AppleDisk>,
user: &str,
) -> Vec<(DeviceCandidate, Option<PathBuf>)> {
let mut out: Vec<(DeviceCandidate, Option<PathBuf>)> = Vec::new();
let mut seen_devs: Vec<Option<PathBuf>> = Vec::new();
for (candidate, dev) in mounted {
if seen_devs.contains(&dev) {
continue;
}
seen_devs.push(dev.clone());
let candidate = match candidate.kind {
CandidateKind::Mounted if dev.is_none() => DeviceCandidate {
mount: default_mount_path(&candidate.label, user),
label: candidate.label,
kind: CandidateKind::Unmounted,
},
kind_state => DeviceCandidate {
mount: candidate.mount,
label: candidate.label,
kind: kind_state,
},
};
out.push((candidate, dev));
}
for disk in disks {
if seen_devs.iter().any(|dev| dev.as_deref() == Some(disk.partition.as_path())) {
continue;
}
seen_devs.push(Some(disk.partition.clone()));
out.push((
DeviceCandidate {
mount: default_mount_path(&disk.label, user),
label: disk.label,
kind: CandidateKind::Unmounted,
},
Some(disk.partition),
));
}
out.sort_by(|a, b| a.0.mount.cmp(&b.0.mount));
out
}
pub struct ProcMount {
pub source: String,
pub target: String,
pub options: String,
}
pub fn parse_proc_mounts_full(content: &str) -> Vec<ProcMount> {
content
.lines()
.filter_map(|line| {
let mut fields = line.split_whitespace();
let source = fields.next()?;
let target = fields.next()?;
fields.next()?;
let options = fields.next().unwrap_or_default().to_owned();
Some(ProcMount {
source: unescape_mount(source),
target: unescape_mount(target),
options,
})
})
.collect()
}
pub fn parse_proc_mounts(content: &str) -> Vec<(String, String)> {
parse_proc_mounts_full(content)
.into_iter()
.map(|mount| (mount.source, mount.target))
.collect()
}
fn unescape_mount(text: &str) -> String {
let bytes = text.as_bytes();
let mut out: Vec<u8> = Vec::with_capacity(bytes.len());
let mut index = 0;
while index < bytes.len() {
let escape = bytes[index] == b'\\'
&& index + 4 <= bytes.len()
&& bytes[index + 1..index + 4].iter().all(u8::is_ascii_digit);
if escape {
let byte = u8::from_str_radix(&text[index + 1..index + 4], 8).unwrap_or(b'\\');
out.push(byte);
index += 4;
} else {
out.push(bytes[index]);
index += 1;
}
}
String::from_utf8_lossy(&out).into_owned()
}
pub enum MountDecision {
AlreadyMounted,
ForeignReadOnly,
ConflictsWith(String),
Free,
}
fn options_contain(options: &str, token: &str) -> bool {
options.split(',').any(|option| option == token)
}
pub fn decide_mount_full(mounts: &[ProcMount], source: &Path, target: &Path) -> MountDecision {
let target = target.display().to_string();
let source = source.display().to_string();
for mount in mounts {
if mount.target == target {
return if mount.source != source {
MountDecision::ConflictsWith(mount.source.clone())
} else if options_contain(&mount.options, "ro") {
MountDecision::ForeignReadOnly
} else {
MountDecision::AlreadyMounted
};
}
}
MountDecision::Free
}
pub fn count_mounts_with_source(mounts: &[(String, String)], source: &Path) -> usize {
let source = source.display().to_string();
mounts
.iter()
.filter(|(mounted_source, _)| *mounted_source == source)
.count()
}
pub fn discover_devices() -> Vec<(DeviceCandidate, Option<PathBuf>)> {
let mounted = scan_candidates()
.into_iter()
.map(|candidate| {
let dev = resolve_block_device(&candidate.mount);
(candidate, dev)
})
.collect::<Vec<_>>();
let disks = scan_apple_disks(Path::new("/sys/block"));
let user = std::env::var("USER").unwrap_or_else(|_| "user".to_owned());
merge_candidates(mounted, disks, &user)
}
pub fn resolve_block_device(mount: &Path) -> Option<PathBuf> {
let dev = std::fs::metadata(mount).ok()?.dev();
block_device_from_dev(dev, Path::new("/sys/dev/block"))
}
pub fn block_device_from_dev(dev: u64, sys_block_dir: &Path) -> Option<PathBuf> {
let id = format!("{}:{}", dev_major(dev), dev_minor(dev));
let link = std::fs::read_link(sys_block_dir.join(id)).ok()?;
let name = link.file_name()?; Some(PathBuf::from("/dev").join(name))
}
pub fn current_uid_gid() -> Option<(u32, u32)> {
let meta = std::fs::metadata("/proc/self").ok()?;
Some((meta.uid(), meta.gid()))
}
fn dev_major(dev: u64) -> u32 {
(((dev >> 8) & 0xfff) | ((dev >> 32) & !0xfff)) as u32
}
fn dev_minor(dev: u64) -> u32 {
((dev & 0xff) | ((dev >> 12) & !0xff)) as u32
}
pub fn parent_disk(block_dev: &Path, sys_block_dir: &Path) -> PathBuf {
let Ok(meta) = std::fs::metadata(block_dev) else {
return block_dev.to_owned();
};
let id = format!("{}:{}", dev_major(meta.dev()), dev_minor(meta.dev()));
let entry = sys_block_dir.join(id); let Ok(link) = std::fs::read_link(&entry) else {
return block_dev.to_owned();
};
if !entry.join("partition").exists() {
return block_dev.to_owned();
}
let resolved = if link.is_absolute() {
link
} else {
sys_block_dir.join(link)
};
match resolved.parent().and_then(Path::file_name) {
Some(disk) => PathBuf::from("/dev").join(disk),
None => block_dev.to_owned(),
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MountSpec {
pub block_dev: PathBuf,
pub mount_path: PathBuf,
pub uid: u32,
pub gid: u32,
}
pub fn build_mount_command(spec: &MountSpec) -> CommandSpec {
CommandSpec {
program: "mount".to_owned(),
args: vec![
"-t".to_owned(),
"hfsplus".to_owned(),
"-o".to_owned(),
format!("force,uid={},gid={},umask=22,nls=utf8", spec.uid, spec.gid),
spec.block_dev.display().to_string(),
spec.mount_path.display().to_string(),
],
}
}
pub fn build_mount_sequence(spec: &MountSpec) -> Vec<CommandSpec> {
vec![
CommandSpec::new("mkdir", &["-p", &spec.mount_path.display().to_string()]),
build_mount_command(spec),
]
}
pub fn build_unmount_command(block_dev: &Path) -> CommandSpec {
CommandSpec::new("udisksctl", &["unmount", "-b", &block_dev.display().to_string()])
}
pub fn build_shutdown_steps(
block_dev: &Path,
sys_block_dir: &Path,
mounts: &[(String, String)],
) -> (Vec<CommandSpec>, CommandSpec) {
let layers = count_mounts_with_source(mounts, block_dev).min(EJECT_DRAIN_LIMIT);
let mut steps = vec![CommandSpec::new("sync", &[])];
steps.extend(std::iter::repeat_n(
CommandSpec::new("udisksctl", &["unmount", "-b", &block_dev.display().to_string()]),
layers,
));
let power_off = CommandSpec::new(
"udisksctl",
&[
"power-off",
"-b",
&parent_disk(block_dev, sys_block_dir).display().to_string(),
],
);
(steps, power_off)
}
pub const EJECT_DRAIN_LIMIT: usize = 10;
pub fn shutdown_step_label(spec: &CommandSpec) -> &'static str {
match (spec.program.as_str(), spec.args.first().map(String::as_str)) {
("udisksctl", Some("power-off")) => "power-off",
("udisksctl", _) => "unmount",
_ => "sync",
}
}
pub fn shutdown_step_error(spec: &CommandSpec, stderr: &str) -> String {
format!("шаг {} (sudo): {}", shutdown_step_label(spec), stderr.trim())
}
pub fn build_sudo_command(spec: &CommandSpec) -> Command {
let mut command = Command::new("sudo");
command
.arg("-S") .arg("-p")
.arg("") .arg(&spec.program)
.args(&spec.args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
command
}
pub fn run_sudo_command(password: &str, spec: &CommandSpec) -> std::io::Result<Output> {
let mut child = build_sudo_command(spec).spawn()?;
{
let stdin = child.stdin.as_mut().ok_or_else(|| {
std::io::Error::other("stdin не открыт: build_sudo_command не настроил pipe")
})?;
let _ = stdin.write_all(password.as_bytes());
let _ = stdin.write_all(b"\n");
} child.wait_with_output()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MountFailure {
WrongPassword,
DirtyJournal,
AlreadyMounted,
Other,
}
pub fn classify_mount_failure(stderr: &str) -> MountFailure {
let stderr = stderr.to_lowercase();
const PASSWORD_MARKERS: [&str; 4] = [
"sorry, try again",
"incorrect password",
"no password was provided",
"authentication failure",
];
if PASSWORD_MARKERS.iter().any(|marker| stderr.contains(marker)) {
return MountFailure::WrongPassword;
}
if stderr.contains("journal") || stderr.contains("uninitialized node") {
return MountFailure::DirtyJournal;
}
if stderr.contains("already mounted")
|| stderr.contains("is busy")
|| stderr.contains("resource busy")
{
return MountFailure::AlreadyMounted;
}
MountFailure::Other
}
#[derive(Debug, thiserror::Error)]
pub enum MountError {
#[error("sudo отклонил пароль")]
WrongPassword,
#[error("журнал HFS+ грязный — force-монтирование невозможно")]
DirtyJournal,
#[error("том уже смонтирован")]
AlreadyMounted,
#[error(
"том смонтирован системой только для чтения — \
закройте приложения, использующие iPod, и повторите подключение"
)]
ForeignReadOnly(String),
#[error("mount завершился с ошибкой")]
Failed,
#[error("не удалось выполнить sudo: {0}")]
Io(#[from] std::io::Error),
}
impl MountError {
pub fn hint(&self) -> Option<&'static str> {
match self {
MountError::WrongPassword => {
Some("Проверьте пароль sudo (раскладка, CapsLock).")
}
MountError::DirtyJournal => Some(
"Журнал HFS+ грязный (обычно после прерванного синка). \
Выполните: fsck.hfsplus -f <блочное устройство> — \
см. docs/device-ipod-nano-7g.md.",
),
MountError::AlreadyMounted => {
Some("Том уже смонтирован; при необходимости отмонтируйте и повторите.")
}
MountError::Failed | MountError::Io(_) | MountError::ForeignReadOnly(_) => None,
}
}
}
pub fn classify_mount_output(output: &Output) -> Result<(), MountError> {
if output.status.success() {
return Ok(());
}
let stderr = String::from_utf8_lossy(&output.stderr);
Err(match classify_mount_failure(&stderr) {
MountFailure::WrongPassword => MountError::WrongPassword,
MountFailure::DirtyJournal => MountError::DirtyJournal,
MountFailure::AlreadyMounted => MountError::AlreadyMounted,
MountFailure::Other => MountError::Failed,
})
}
pub fn unmount_failure_class(stderr: &str) -> MountError {
match classify_mount_failure(stderr) {
MountFailure::WrongPassword => MountError::WrongPassword,
_ => MountError::ForeignReadOnly(stderr.to_owned()),
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::os::unix::process::ExitStatusExt;
use tempfile::TempDir;
fn touch_dir(path: &Path) {
std::fs::create_dir_all(path).unwrap();
}
#[test]
fn scan_under_finds_ipod_volumes() {
let root = TempDir::new().unwrap();
touch_dir(&root.path().join("user1/uuid-abc/iPod_Control"));
touch_dir(&root.path().join("user1/plain-volume"));
touch_dir(&root.path().join("user2/uuid-def/iPod_Control"));
let found = scan_under(root.path());
assert_eq!(
found,
vec![
DeviceCandidate {
mount: root.path().join("user1/uuid-abc"),
label: "uuid-abc".to_owned(),
kind: CandidateKind::Mounted,
},
DeviceCandidate {
mount: root.path().join("user2/uuid-def"),
label: "uuid-def".to_owned(),
kind: CandidateKind::Mounted,
},
]
);
}
fn fake_apple_disk(root: &Path, name: &str, partitions: &[(&str, u64)]) {
let disk = root.join(name);
touch_dir(&disk.join("device"));
std::fs::write(disk.join("device/vendor"), b"Apple\n").unwrap();
std::fs::write(disk.join("device/model"), b"iPod\n").unwrap();
for (part, size) in partitions {
let dir = disk.join(part);
touch_dir(&dir);
std::fs::write(dir.join("partition"), b"2\n").unwrap();
std::fs::write(dir.join("size"), size.to_string()).unwrap();
}
}
#[test]
fn apple_disk_scan_picks_the_largest_partition() {
let sys = TempDir::new().unwrap();
fake_apple_disk(
sys.path(),
"sda",
&[("sda1", 488), ("sda2", 30_800_368)],
);
let disks = scan_apple_disks(sys.path());
assert_eq!(
disks,
vec![AppleDisk {
partition: PathBuf::from("/dev/sda2"),
label: "Apple iPod".to_owned(),
}]
);
}
#[test]
fn apple_disk_scan_ignores_other_vendors_and_partitionless_disks() {
let sys = TempDir::new().unwrap();
let ata = sys.path().join("nvme0n1");
touch_dir(&ata.join("device"));
std::fs::write(ata.join("device/vendor"), b"ATA\n").unwrap();
touch_dir(&sys.path().join("sdb/device")); std::fs::write(sys.path().join("sdb/device/vendor"), b"Apple\n").unwrap();
assert!(scan_apple_disks(sys.path()).is_empty());
}
#[test]
fn merge_drops_disk_when_its_partition_is_already_mounted() {
let mounted = vec![(
DeviceCandidate {
mount: PathBuf::from("/run/media/user/uuid"),
label: "uuid".to_owned(),
kind: CandidateKind::Mounted,
},
Some(PathBuf::from("/dev/sda2")),
)];
let disks = vec![AppleDisk {
partition: PathBuf::from("/dev/sda2"),
label: "Apple iPod".to_owned(),
}];
let merged = merge_candidates(mounted, disks, "user");
assert_eq!(merged.len(), 1);
assert_eq!(merged[0].0.kind, CandidateKind::Mounted);
}
#[test]
fn merge_adds_unmounted_disk_with_planned_mount_path() {
let disks = vec![AppleDisk {
partition: PathBuf::from("/dev/sda2"),
label: "Apple iPod".to_owned(),
}];
let merged = merge_candidates(Vec::new(), disks, "user");
assert_eq!(merged.len(), 1);
let (candidate, dev) = &merged[0];
assert_eq!(candidate.kind, CandidateKind::Unmounted);
assert_eq!(candidate.mount, PathBuf::from("/run/media/user/Apple iPod"));
assert_eq!(candidate.label, "Apple iPod");
assert_eq!(dev.as_deref(), Some(Path::new("/dev/sda2")));
}
#[test]
fn merge_dedupes_stacked_mounts_of_one_devnode() {
let mounted = vec![
(
DeviceCandidate {
mount: PathBuf::from("/run/media/user/iPod"),
label: "iPod".to_owned(),
kind: CandidateKind::Mounted,
},
Some(PathBuf::from("/dev/sda2")),
),
(
DeviceCandidate {
mount: PathBuf::from("/run/media/user/iPod1"),
label: "iPod1".to_owned(),
kind: CandidateKind::Mounted,
},
Some(PathBuf::from("/dev/sda2")),
),
];
let disks = vec![AppleDisk {
partition: PathBuf::from("/dev/sda2"),
label: "Apple iPod".to_owned(),
}];
let merged = merge_candidates(mounted, disks, "user");
assert_eq!(merged.len(), 1, "one devnode — one candidate");
assert_eq!(merged[0].0.kind, CandidateKind::Mounted);
assert_eq!(merged[0].0.mount, PathBuf::from("/run/media/user/iPod"));
assert_eq!(merged[0].1.as_deref(), Some(Path::new("/dev/sda2")));
}
#[test]
fn stale_mount_without_devnode_shows_as_unmounted() {
let mounted = vec![(
DeviceCandidate {
mount: PathBuf::from("/run/media/user/iPod"),
label: "iPod".to_owned(),
kind: CandidateKind::Mounted,
},
None,
)];
let merged = merge_candidates(mounted, Vec::new(), "user");
assert_eq!(merged.len(), 1);
assert_eq!(merged[0].0.kind, CandidateKind::Unmounted);
assert_eq!(
merged[0].0.mount,
PathBuf::from("/run/media/user/iPod"),
"planned mount path"
);
assert_eq!(merged[0].1, None);
}
#[test]
fn proc_mounts_parses_fields_and_octal_escapes() {
let mounts = parse_proc_mounts(
"/dev/sda2 /run/media/user/iPod hfsplus ro 0 0\n"
.replace("\\n", "\n")
.as_str(),
);
assert_eq!(
mounts,
vec![(
"/dev/sda2".to_owned(),
"/run/media/user/iPod".to_owned(),
)
]);
let escaped = "/dev/sda\\0402 /run/media/user/iPod\\0402 hfsplus ro 0 0\\n";
let mounts = parse_proc_mounts(escaped);
assert_eq!(
mounts,
vec![(
"/dev/sda 2".to_owned(),
"/run/media/user/iPod 2".to_owned(),
)
]);
}
#[test]
fn proc_mounts_full_captures_options_and_decodes_escapes() {
let mounts = parse_proc_mounts_full(
"/dev/sda2 /run/media/user/iPod\\0402 hfsplus ro,relatime 0 0\n",
);
assert_eq!(mounts.len(), 1);
assert_eq!(mounts[0].source, "/dev/sda2");
assert_eq!(mounts[0].target, "/run/media/user/iPod 2");
assert_eq!(mounts[0].options, "ro,relatime");
let pairs = parse_proc_mounts("/dev/sda2 /run/media/user/iPod hfsplus ro 0 0\n");
assert_eq!(
pairs,
vec![(
"/dev/sda2".to_owned(),
"/run/media/user/iPod".to_owned(),
)]
);
}
#[test]
fn mount_decision_is_idempotent_or_honest() {
let mounts = parse_proc_mounts_full(
"/dev/sda2 /run/media/user/iPod hfsplus rw,relatime 0 0\n\
/dev/sdb1 /run/media/user/other vfat rw 0 0\n",
);
let target = Path::new("/run/media/user/iPod");
assert!(matches!(
decide_mount_full(&mounts, Path::new("/dev/sda2"), target),
MountDecision::AlreadyMounted
));
assert!(matches!(
decide_mount_full(&mounts, Path::new("/dev/sdc1"), target),
MountDecision::ConflictsWith(ref other) if other == "/dev/sda2"
));
assert!(matches!(
decide_mount_full(
&mounts,
Path::new("/dev/sda2"),
Path::new("/run/media/user/new")
),
MountDecision::Free
));
}
#[test]
fn mount_decision_flags_foreign_read_only_by_ro_token() {
let ro = parse_proc_mounts_full(
"/dev/sda2 /run/media/user/iPod hfsplus ro,relatime 0 0\n",
);
assert!(matches!(
decide_mount_full(&ro, Path::new("/dev/sda2"), Path::new("/run/media/user/iPod")),
MountDecision::ForeignReadOnly
));
for options in ["rw,relatime", "rw,errors=remount-ro"] {
let mounts = parse_proc_mounts_full(&format!(
"/dev/sda2 /run/media/user/iPod hfsplus {options} 0 0\n"
));
assert!(
matches!(
decide_mount_full(
&mounts,
Path::new("/dev/sda2"),
Path::new("/run/media/user/iPod")
),
MountDecision::AlreadyMounted
),
"options {options:?} must not read as ro"
);
}
}
#[test]
fn unmount_command_targets_block_device() {
let spec = build_unmount_command(Path::new("/dev/sdb2"));
assert_eq!(spec.program, "udisksctl");
assert_eq!(
spec.args,
vec!["unmount".to_owned(), "-b".to_owned(), "/dev/sdb2".to_owned()]
);
}
#[test]
fn unmount_failure_class_splits_sudo_auth_from_ro_failure() {
assert!(matches!(
unmount_failure_class("sudo: 1 incorrect password attempt"),
MountError::WrongPassword
));
assert!(matches!(
unmount_failure_class("Sorry, try again."),
MountError::WrongPassword
));
assert!(matches!(
unmount_failure_class("Error: The drive is in use"),
MountError::ForeignReadOnly(ref message) if message == "Error: The drive is in use"
));
}
#[test]
fn foreign_read_only_display_carries_advice_only() {
let error = MountError::ForeignReadOnly("Error: The drive is in use".to_owned());
assert!(!error.to_string().contains("Error:"));
assert!(error.to_string().contains("закройте приложения"));
}
#[test]
fn count_mounts_with_source_counts_stacked_layers() {
let mounts = vec![
("/dev/sda2".to_owned(), "/run/media/user/iPod".to_owned()),
("/dev/sda2".to_owned(), "/run/media/user/iPod1".to_owned()),
("/dev/sdb1".to_owned(), "/run/media/user/other".to_owned()),
];
assert_eq!(count_mounts_with_source(&mounts, Path::new("/dev/sda2")), 2);
assert_eq!(count_mounts_with_source(&mounts, Path::new("/dev/sdb1")), 1);
assert_eq!(count_mounts_with_source(&[], Path::new("/dev/sda2")), 0);
assert_eq!(count_mounts_with_source(&mounts, Path::new("/dev/sda1")), 0);
}
#[test]
fn mount_sequence_is_mkdir_then_mount() {
let spec = MountSpec {
block_dev: PathBuf::from("/dev/sda2"),
mount_path: PathBuf::from("/run/media/user/Apple iPod"),
uid: 1000,
gid: 1000,
};
let steps = build_mount_sequence(&spec);
assert_eq!(steps.len(), 2);
assert_eq!(steps[0].program, "mkdir");
assert_eq!(
steps[0].args,
vec![
"-p".to_owned(),
"/run/media/user/Apple iPod".to_owned(),
]
);
assert_eq!(steps[1], build_mount_command(&spec));
}
#[test]
fn scan_under_missing_root_is_empty() {
assert!(scan_under(Path::new("/nonexistent/media/root")).is_empty());
}
#[test]
fn ipod_control_must_be_a_directory() {
let root = TempDir::new().unwrap();
touch_dir(&root.path().join("user/vol"));
std::fs::write(root.path().join("user/vol/iPod_Control"), b"file").unwrap();
assert!(scan_under(root.path()).is_empty());
}
#[test]
fn dev_number_decodes_major_and_minor() {
let sys = TempDir::new().unwrap();
let block_dir = sys.path().join("dev/block");
touch_dir(&block_dir);
std::os::unix::fs::symlink(
"../../devices/platform/usb/block/sda/sda2",
block_dir.join("8:2"),
)
.unwrap();
std::os::unix::fs::symlink(
"../../devices/pci/block/nvme0n1/nvme0n1p3",
block_dir.join("259:3"),
)
.unwrap();
assert_eq!(
block_device_from_dev((8 << 8) | 2, &block_dir),
Some(PathBuf::from("/dev/sda2"))
);
assert_eq!(
block_device_from_dev((259 << 8) | 3, &block_dir),
Some(PathBuf::from("/dev/nvme0n1p3"))
);
}
#[test]
fn block_device_unknown_when_sysfs_lacks_entry() {
let sys = TempDir::new().unwrap();
assert_eq!(
block_device_from_dev((8 << 8) | 5, &sys.path().join("dev/block")),
None
);
}
fn fake_block_device(root: &Path, link_target: &str, is_partition: bool) -> (PathBuf, PathBuf) {
let dev_node = root.join("dev-node");
std::fs::write(&dev_node, b"").unwrap();
let meta = std::fs::metadata(&dev_node).unwrap();
let id = format!("{}:{}", dev_major(meta.dev()), dev_minor(meta.dev()));
let sys_block_dir = root.join("sys/dev/block");
touch_dir(&sys_block_dir);
std::os::unix::fs::symlink(link_target, sys_block_dir.join(&id)).unwrap();
if is_partition {
let target_dir = sys_block_dir.join(link_target);
std::fs::create_dir_all(&target_dir).unwrap();
std::fs::write(target_dir.join("partition"), b"").unwrap();
}
(dev_node, sys_block_dir)
}
#[test]
fn parent_disk_partition_resolves_parent_via_sysfs() {
let sys = TempDir::new().unwrap();
let (dev, sys_block_dir) = fake_block_device(
sys.path(),
"../../devices/platform/usb/block/sda/sda2",
true,
);
assert_eq!(parent_disk(&dev, &sys_block_dir), PathBuf::from("/dev/sda"));
}
#[test]
fn parent_disk_nvme_partition_resolves_parent_via_sysfs() {
let sys = TempDir::new().unwrap();
let (dev, sys_block_dir) = fake_block_device(
sys.path(),
"../../devices/pci0000:00/block/nvme0n1/nvme0n1p2",
true,
);
assert_eq!(
parent_disk(&dev, &sys_block_dir),
PathBuf::from("/dev/nvme0n1")
);
}
#[test]
fn parent_disk_whole_disk_without_marker_stays_itself() {
let sys = TempDir::new().unwrap();
let (dev, sys_block_dir) = fake_block_device(
sys.path(),
"../../devices/platform/usb/block/sda/sda",
false,
);
assert_eq!(parent_disk(&dev, &sys_block_dir), dev);
}
#[test]
fn parent_disk_virtual_devices_without_marker_stay_themselves() {
for name in ["loop0", "dm-0", "zram0"] {
let sys = TempDir::new().unwrap();
let target = format!("../../devices/platform/virtual/block/{name}");
let (dev, sys_block_dir) = fake_block_device(sys.path(), &target, false);
assert_eq!(parent_disk(&dev, &sys_block_dir), dev, "case: {name}");
}
}
#[test]
fn parent_disk_unknown_sysfs_entry_falls_back_to_itself() {
let sys = TempDir::new().unwrap();
let dev_node = sys.path().join("dev-node");
std::fs::write(&dev_node, b"").unwrap();
let sys_block_dir = sys.path().join("sys/dev/block");
touch_dir(&sys_block_dir);
assert_eq!(parent_disk(&dev_node, &sys_block_dir), dev_node);
}
#[test]
fn mount_command_matches_runbook_recipe() {
let spec = MountSpec {
block_dev: PathBuf::from("/dev/sda2"),
mount_path: PathBuf::from("/run/media/user/uuid"),
uid: 1000,
gid: 1000,
};
let command = build_mount_command(&spec);
assert_eq!(command.program, "mount");
assert_eq!(
command.args,
vec![
"-t".to_owned(),
"hfsplus".to_owned(),
"-o".to_owned(),
"force,uid=1000,gid=1000,umask=22,nls=utf8".to_owned(),
"/dev/sda2".to_owned(),
"/run/media/user/uuid".to_owned(),
]
);
}
#[test]
fn sudo_command_hides_password_in_stdin_not_argv() {
let spec = build_mount_command(&MountSpec {
block_dev: PathBuf::from("/dev/sda2"),
mount_path: PathBuf::from("/run/media/user/uuid"),
uid: 1000,
gid: 1000,
});
let sudo = build_sudo_command(&spec);
assert_eq!(sudo.get_program().to_string_lossy(), "sudo");
let args: Vec<_> = sudo
.get_args()
.map(|arg| arg.to_string_lossy().into_owned())
.collect();
assert_eq!(&args[..3], &["-S", "-p", ""]);
assert_eq!(&args[3], &spec.program);
assert_eq!(&args[4..], spec.args.as_slice());
}
#[test]
fn shutdown_steps_are_sync_drain_then_power_off_last() {
let sys = TempDir::new().unwrap();
let (dev, sys_block_dir) =
fake_block_device(sys.path(), "../../devices/platform/usb/block/sda/sda2", true);
let mounts: Vec<(String, String)> = (0..7)
.map(|_| {
(
dev.display().to_string(),
"/run/media/user/uuid".to_owned(),
)
})
.collect();
let (steps, power_off) = build_shutdown_steps(&dev, &sys_block_dir, &mounts);
assert_eq!(steps.len(), 1 + 7, "sync + one unmount per layer");
assert_eq!(steps[0].program, "sync");
assert!(steps[1..].iter().all(|step| step.program == "udisksctl"
&& step.args
== vec![
"unmount".to_owned(),
"-b".to_owned(),
dev.display().to_string()
]));
assert_eq!(power_off.program, "udisksctl");
assert_eq!(
power_off.args,
vec!["power-off".to_owned(), "-b".to_owned(), "/dev/sda".to_owned()]
);
}
#[test]
fn shutdown_drain_is_bounded_and_covers_an_empty_mount_table() {
let sys = TempDir::new().unwrap();
let (dev, sys_block_dir) =
fake_block_device(sys.path(), "../../devices/platform/usb/block/sda/sda2", true);
let mounts: Vec<(String, String)> = (0..EJECT_DRAIN_LIMIT * 2)
.map(|_| {
(
dev.display().to_string(),
"/run/media/user/uuid".to_owned(),
)
})
.collect();
let (steps, _) = build_shutdown_steps(&dev, &sys_block_dir, &mounts);
assert_eq!(steps.len(), 1 + EJECT_DRAIN_LIMIT, "drain cap holds");
let (empty, power_off) = build_shutdown_steps(&dev, &sys_block_dir, &[]);
assert_eq!(empty.len(), 1);
assert_eq!(empty[0].program, "sync");
assert_eq!(power_off.program, "udisksctl");
}
#[test]
fn shutdown_step_errors_are_named_and_uniform() {
let sync = CommandSpec::new("sync", &[]);
let unmount = CommandSpec::new("udisksctl", &["unmount", "-b", "/dev/sda2"]);
let power_off = CommandSpec::new("udisksctl", &["power-off", "-b", "/dev/sda"]);
assert_eq!(shutdown_step_label(&sync), "sync");
assert_eq!(shutdown_step_label(&unmount), "unmount");
assert_eq!(shutdown_step_label(&power_off), "power-off");
assert_eq!(
shutdown_step_error(&unmount, " Error: The drive is in use "),
"шаг unmount (sudo): Error: The drive is in use"
);
assert_eq!(
shutdown_step_error(&power_off, "Нет такого файла или процесса (os error 2)"),
"шаг power-off (sudo): Нет такого файла или процесса (os error 2)"
);
}
#[test]
fn wrong_password_markers_are_classified() {
assert_eq!(
classify_mount_failure("sudo: 1 incorrect password attempt"),
MountFailure::WrongPassword
);
assert_eq!(
classify_mount_failure("Sorry, try again."),
MountFailure::WrongPassword
);
assert_eq!(
classify_mount_failure("sudo: no password was provided"),
MountFailure::WrongPassword
);
}
#[test]
fn dirty_journal_markers_are_classified() {
assert_eq!(
classify_mount_failure("hfsplus: journal is not empty"),
MountFailure::DirtyJournal
);
assert_eq!(
classify_mount_failure("hfsplus: uninitialized node"),
MountFailure::DirtyJournal
);
}
#[test]
fn busy_and_mounted_markers_are_classified() {
assert_eq!(
classify_mount_failure("mount: /run/media/u/x: already mounted on /run/media/u/x."),
MountFailure::AlreadyMounted
);
assert_eq!(
classify_mount_failure("mount: /run/media/u/x is busy"),
MountFailure::AlreadyMounted
);
}
#[test]
fn unknown_stderr_is_other() {
assert_eq!(
classify_mount_failure("mount: unknown filesystem type 'hfsx'"),
MountFailure::Other
);
assert_eq!(classify_mount_failure(""), MountFailure::Other);
}
#[test]
fn mount_output_success_is_ok() {
let output = Output {
status: std::process::ExitStatus::from_raw(0),
stdout: Vec::new(),
stderr: Vec::new(),
};
assert!(classify_mount_output(&output).is_ok());
}
#[test]
fn mount_output_failure_maps_to_kind() {
let output = Output {
status: std::process::ExitStatus::from_raw(256),
stdout: Vec::new(),
stderr: b"hfsplus: journal is not empty\n".to_vec(),
};
assert!(matches!(
classify_mount_output(&output),
Err(MountError::DirtyJournal)
));
}
#[test]
fn hints_exist_for_recoverable_errors() {
assert!(MountError::DirtyJournal.hint().unwrap().contains("fsck.hfsplus"));
assert!(MountError::WrongPassword.hint().is_some());
assert!(MountError::AlreadyMounted.hint().is_some());
assert!(MountError::Failed.hint().is_none());
}
#[test]
fn current_uid_gid_is_available_on_linux() {
let (uid, _gid) = current_uid_gid().expect("/proc/self exists on Linux");
assert_ne!(uid, u32::MAX);
}
}