|
3 | 3 | // file, You can obtain one at https://mozilla.org/MPL/2.0/. |
4 | 4 |
|
5 | 5 | use anyhow::{anyhow, Context, Result}; |
6 | | -use log::warn; |
7 | | -use std::{fs, path::Path, process::Command}; |
| 6 | +use log::{debug, warn}; |
| 7 | +use std::{ |
| 8 | + fs, |
| 9 | + path::{Path, PathBuf}, |
| 10 | + process::Command, |
| 11 | +}; |
8 | 12 | use tempfile::{tempdir, TempDir}; |
9 | 13 |
|
10 | 14 | pub struct IsoWriter { |
@@ -58,3 +62,143 @@ impl IsoWriter { |
58 | 62 | Ok(()) |
59 | 63 | } |
60 | 64 | } |
| 65 | + |
| 66 | +pub struct IsoReader { |
| 67 | + iso_file: PathBuf, |
| 68 | +} |
| 69 | + |
| 70 | +impl IsoReader { |
| 71 | + pub fn new<P: AsRef<Path>>(iso: P) -> Self { |
| 72 | + Self { |
| 73 | + iso_file: PathBuf::from(iso.as_ref()), |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + pub fn read<P: AsRef<Path>>(&self, path: P) -> Result<Vec<u8>> { |
| 78 | + let loop_dev = loopback_setup(&self.iso_file)?; |
| 79 | + |
| 80 | + let tmpdir = tempdir()?; |
| 81 | + mount(&loop_dev, &tmpdir)?; |
| 82 | + |
| 83 | + let src = tmpdir.path().join(&path); |
| 84 | + let data = fs::read(src)?; |
| 85 | + |
| 86 | + unmount(&tmpdir)?; |
| 87 | + |
| 88 | + loopback_teardown(&loop_dev)?; |
| 89 | + |
| 90 | + Ok(data) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// create loopback device for iso file and get the device path from |
| 95 | +// losetup stdout |
| 96 | +fn loopback_setup<P: AsRef<Path>>(iso_file: P) -> Result<String> { |
| 97 | + let mut cmd = Command::new("losetup"); |
| 98 | + let output = cmd |
| 99 | + .arg("-f") |
| 100 | + .output() |
| 101 | + .with_context(|| "unable to execute \"losetup\"")?; |
| 102 | + |
| 103 | + debug!("executing command: \"{:#?}\"", cmd); |
| 104 | + |
| 105 | + if !output.status.success() { |
| 106 | + warn!("command failed with status: {}", output.status); |
| 107 | + warn!("stderr: \"{}\"", String::from_utf8_lossy(&output.stderr)); |
| 108 | + return Err(anyhow!("Failed to get next available loopback device")); |
| 109 | + } |
| 110 | + |
| 111 | + let loop_dev = String::from(String::from_utf8(output.stdout)?.trim()); |
| 112 | + debug!("got loopback device: {}", loop_dev); |
| 113 | + |
| 114 | + let mut cmd = Command::new("losetup"); |
| 115 | + let output = cmd |
| 116 | + .arg(&loop_dev) |
| 117 | + .arg(iso_file.as_ref()) |
| 118 | + .output() |
| 119 | + .with_context(|| "failed to execute \"losetup\"")?; |
| 120 | + |
| 121 | + debug!("executing command: \"{:#?}\"", cmd); |
| 122 | + if output.status.success() { |
| 123 | + Ok(loop_dev) |
| 124 | + } else { |
| 125 | + warn!("command failed with status: {}", output.status); |
| 126 | + warn!("stderr: \"{}\"", String::from_utf8_lossy(&output.stderr)); |
| 127 | + Err(anyhow!( |
| 128 | + "Failed to create loopback device {} for ISO file {}", |
| 129 | + loop_dev, |
| 130 | + iso_file.as_ref().display() |
| 131 | + )) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +fn loopback_teardown<P: AsRef<Path>>(loop_dev: P) -> Result<()> { |
| 136 | + // tear down the loopback device |
| 137 | + let mut cmd = Command::new("losetup"); |
| 138 | + let output = cmd |
| 139 | + .arg("-d") |
| 140 | + .arg(loop_dev.as_ref()) |
| 141 | + .output() |
| 142 | + .context("failed to execute \"losetup\"")?; |
| 143 | + |
| 144 | + if output.status.success() { |
| 145 | + Ok(()) |
| 146 | + } else { |
| 147 | + warn!("command failed with status: {}", output.status); |
| 148 | + warn!("stderr: \"{}\"", String::from_utf8_lossy(&output.stderr)); |
| 149 | + Err(anyhow!( |
| 150 | + "Failed to delete loopback device {}", |
| 151 | + loop_dev.as_ref().display() |
| 152 | + )) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// TODO: sys_mount crate |
| 157 | +fn mount<P: AsRef<Path>, Q: AsRef<Path>>( |
| 158 | + device: P, |
| 159 | + mount_point: Q, |
| 160 | +) -> Result<()> { |
| 161 | + let mut cmd = Command::new("mount"); |
| 162 | + let output = cmd |
| 163 | + .arg(device.as_ref()) |
| 164 | + .arg(mount_point.as_ref()) |
| 165 | + .output() |
| 166 | + .with_context(|| { |
| 167 | + format!( |
| 168 | + "failed to mount \"{}\" at \"{}\"", |
| 169 | + device.as_ref().display(), |
| 170 | + mount_point.as_ref().display() |
| 171 | + ) |
| 172 | + })?; |
| 173 | + |
| 174 | + if output.status.success() { |
| 175 | + Ok(()) |
| 176 | + } else { |
| 177 | + warn!("command failed with status: {}", output.status); |
| 178 | + warn!("stderr: \"{}\"", String::from_utf8_lossy(&output.stderr)); |
| 179 | + Err(anyhow!( |
| 180 | + "Failed to mount device {} at {}", |
| 181 | + device.as_ref().display(), |
| 182 | + mount_point.as_ref().display() |
| 183 | + )) |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +fn unmount<P: AsRef<Path>>(mount_point: P) -> Result<()> { |
| 188 | + // unmount now that we've got the data we need |
| 189 | + let mut cmd = Command::new("umount"); |
| 190 | + let output = cmd.arg(mount_point.as_ref()).output().with_context(|| { |
| 191 | + format!("failed to unmount \"{}\"", mount_point.as_ref().display()) |
| 192 | + })?; |
| 193 | + |
| 194 | + if output.status.success() { |
| 195 | + Ok(()) |
| 196 | + } else { |
| 197 | + warn!("command failed with status: {}", output.status); |
| 198 | + warn!("stderr: \"{}\"", String::from_utf8_lossy(&output.stderr)); |
| 199 | + Err(anyhow!( |
| 200 | + "Failed to unmount at {}", |
| 201 | + mount_point.as_ref().display() |
| 202 | + )) |
| 203 | + } |
| 204 | +} |
0 commit comments