Skip to content

Commit

Permalink
qcow2-rs: pass &Path to qcow2_rs::utils::qcow2_setup_dev_*
Browse files Browse the repository at this point in the history
Signed-off-by: Ming Lei <tom.leiming@gmail.com>
  • Loading branch information
ming1 committed Jan 18, 2024
1 parent 883993e commit d6b61e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
15 changes: 6 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,7 @@ async fn convert_from_qcow2_dev<T: Qcow2IoOps>(dev: &Qcow2Dev<T>, raw: &Path) ->

async fn convert_to_qcow2_dev<T: Qcow2IoOps>(raw: &Path, dev: &Qcow2Dev<T>) -> Qcow2Result<()> {
let file_orig_size = std::fs::metadata(raw).unwrap().len();
let mut file = std::fs::OpenOptions::new()
.read(true)
.open(raw.to_path_buf())
.unwrap();
let mut file = std::fs::OpenOptions::new().read(true).open(raw).unwrap();

let mut off: u64 = 0;
let buf_size = 64 << 20;
Expand All @@ -624,7 +621,7 @@ async fn convert_to_qcow2_dev<T: Qcow2IoOps>(raw: &Path, dev: &Qcow2Dev<T>) -> Q
Ok(())
}

fn convert_to_qcow2_prep(raw: &Path, qcow2: &PathBuf) -> Qcow2Result<()> {
fn convert_to_qcow2_prep(raw: &Path, qcow2: &Path) -> Qcow2Result<()> {
let cluster_bits = 16;
let cluster_size = 1 << cluster_bits;
let file_orig_size = std::fs::metadata(raw).unwrap().len();
Expand All @@ -644,7 +641,7 @@ fn convert_to_qcow2_prep(raw: &Path, qcow2: &PathBuf) -> Qcow2Result<()> {
}

#[cfg(not(target_os = "linux"))]
fn convert_from_qcow2(qcow2: &PathBuf, raw: &Path) -> Qcow2Result<()> {
fn convert_from_qcow2(qcow2: &Path, raw: &Path) -> Qcow2Result<()> {
let rt = tokio::runtime::Runtime::new().unwrap();

rt.block_on(async {
Expand All @@ -658,7 +655,7 @@ fn convert_from_qcow2(qcow2: &PathBuf, raw: &Path) -> Qcow2Result<()> {
}

#[cfg(target_os = "linux")]
fn convert_from_qcow2(qcow2: &PathBuf, raw: &Path) -> Qcow2Result<()> {
fn convert_from_qcow2(qcow2: &Path, raw: &Path) -> Qcow2Result<()> {
tokio_uring::start(async {
let p = qcow2_rs::qcow2_default_params!(false, true);
let dev = qcow2_rs::utils::qcow2_setup_dev_uring(qcow2, &p)
Expand All @@ -671,7 +668,7 @@ fn convert_from_qcow2(qcow2: &PathBuf, raw: &Path) -> Qcow2Result<()> {
}

#[cfg(not(target_os = "linux"))]
fn convert_to_qcow2(raw: &Path, qcow2: &PathBuf) -> Qcow2Result<()> {
fn convert_to_qcow2(raw: &Path, qcow2: &Path) -> Qcow2Result<()> {
convert_to_qcow2_prep(raw, qcow2)?;
let rt = tokio::runtime::Runtime::new().unwrap();

Expand All @@ -686,7 +683,7 @@ fn convert_to_qcow2(raw: &Path, qcow2: &PathBuf) -> Qcow2Result<()> {
}

#[cfg(target_os = "linux")]
fn convert_to_qcow2(raw: &Path, qcow2: &PathBuf) -> Qcow2Result<()> {
fn convert_to_qcow2(raw: &Path, qcow2: &Path) -> Qcow2Result<()> {
convert_to_qcow2_prep(raw, qcow2)?;

tokio_uring::start(async {
Expand Down
12 changes: 6 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ const MAX_HEADER_SIZE: usize = 65536;
/// Allocate one qcow2 device and qcow2 header needs to be parsed
/// for allocating the device.
pub fn qcow2_alloc_dev_sync<T: Qcow2IoOps>(
path: &PathBuf,
path: &Path,
io: T,
params: &Qcow2DevParams,
) -> Qcow2Result<(Qcow2Dev<T>, Option<PathBuf>)> {
fn read_header(path: &PathBuf, bytes: usize) -> Qcow2Result<Qcow2IoBuf<u8>> {
fn read_header(path: &Path, bytes: usize) -> Qcow2Result<Qcow2IoBuf<u8>> {
use std::io::Read;
let mut buf = Qcow2IoBuf::<u8>::new(bytes);
let mut file = std::fs::File::open(path).unwrap();
Expand Down Expand Up @@ -90,7 +90,7 @@ macro_rules! qcow2_setup_dev_fn {
($type:ty, $fn_name: ident) => {
#[async_recursion(?Send)]
pub async fn $fn_name(
path: &PathBuf,
path: &Path,
params: &Qcow2DevParams,
) -> Qcow2Result<Qcow2Dev<$type>> {
let io = <$type>::new(path, params.is_read_only(), params.is_direct_io()).await;
Expand All @@ -99,7 +99,7 @@ macro_rules! qcow2_setup_dev_fn {
Some(back_path) => {
let p = params.clone();
p.mark_backing_dev(Some(true));
let bdev = $fn_name(&back_path, &p).await?;
let bdev = $fn_name(&back_path.as_path(), &p).await?;
dev.set_backing_dev(Box::new(bdev));
}
_ => {}
Expand All @@ -122,14 +122,14 @@ qcow2_setup_dev_fn!(crate::tokio_io::Qcow2IoTokio, qcow2_setup_dev_tokio);
#[macro_export]
macro_rules! qcow2_setup_dev_fn_sync {
($type:ty, $fn_name: ident) => {
pub fn $fn_name(path: &PathBuf, params: &Qcow2DevParams) -> Qcow2Result<Qcow2Dev<$type>> {
pub fn $fn_name(path: &Path, params: &Qcow2DevParams) -> Qcow2Result<Qcow2Dev<$type>> {
let io = <$type>::new(path, params.is_read_only(), params.is_direct_io());
let (mut dev, backing) = qcow2_alloc_dev_sync(&path, io, params)?;
match backing {
Some(back_path) => {
let p = params.clone();
p.mark_backing_dev(Some(true));
let bdev = $fn_name(&back_path, &p)?;
let bdev = $fn_name(&back_path.as_path(), &p)?;
dev.set_backing_dev(Box::new(bdev));
}
_ => {}
Expand Down

0 comments on commit d6b61e5

Please sign in to comment.