From 2c221598028c07c14f6eaa2c7f2fc81b4c445afa Mon Sep 17 00:00:00 2001 From: Henry Hrvoje Tonkovac Date: Sun, 14 Jun 2026 19:51:48 +0200 Subject: [PATCH] block: trim qualified paths in formats Import the std modules used in the disk-format handlers instead of spelling the full paths at every use site. Signed-off-by: Henry Hrvoje Tonkovac Assisted-by: Claude:Opus-4.8 --- block/src/formats/qcow/internal/decoder.rs | 8 +++-- block/src/formats/qcow/internal/mod.rs | 4 +-- .../formats/qcow/internal/qcow_raw_file.rs | 5 +-- block/src/formats/qcow/internal/raw_file.rs | 34 +++++++++---------- block/src/formats/qcow/internal/refcount.rs | 6 ++-- block/src/formats/qcow/mod.rs | 4 ++- block/src/formats/qcow/worker/async_uring.rs | 4 +-- block/src/formats/qcow/worker/sync.rs | 6 ++-- block/src/formats/raw/worker/sync.rs | 5 +-- block/src/formats/vhd/internal/fixed.rs | 12 +++---- block/src/formats/vhd/internal/footer.rs | 6 ++-- block/src/formats/vhd/mod.rs | 4 +-- block/src/formats/vhd/worker/async_uring.rs | 9 ++--- block/src/formats/vhd/worker/sync.rs | 11 +++--- block/src/formats/vhdx/internal/bat.rs | 3 +- block/src/formats/vhdx/internal/header.rs | 9 ++--- block/src/formats/vhdx/internal/io.rs | 3 +- block/src/formats/vhdx/internal/metadata.rs | 7 ++-- block/src/formats/vhdx/internal/mod.rs | 29 +++++++++------- block/src/formats/vhdx/mod.rs | 3 +- block/src/formats/vhdx/worker/sync.rs | 6 ++-- 21 files changed, 97 insertions(+), 81 deletions(-) diff --git a/block/src/formats/qcow/internal/decoder.rs b/block/src/formats/qcow/internal/decoder.rs index f9510baf9..537389c30 100644 --- a/block/src/formats/qcow/internal/decoder.rs +++ b/block/src/formats/qcow/internal/decoder.rs @@ -2,6 +2,8 @@ // // SPDX-License-Identifier: Apache-2.0 +use std::{io, result}; + use thiserror::Error; #[derive(Debug, Error)] @@ -11,12 +13,12 @@ pub enum Error { #[error("Zlib unexpected status: {0:?}")] ZlibUnexpectedStatus(flate2::Status), #[error("Zstd decompress error")] - ZstdDecompress(#[source] std::io::Error), + ZstdDecompress(#[source] io::Error), #[error("Zstd: failed to fill buffer")] - ZstdFillBuffer(#[source] std::io::Error), + ZstdFillBuffer(#[source] io::Error), } -pub type Result = std::result::Result; +pub type Result = result::Result; /// Generic trait for decoding zlib/zstd formats pub trait Decoder: Send + Sync { diff --git a/block/src/formats/qcow/internal/mod.rs b/block/src/formats/qcow/internal/mod.rs index 0f8d76fc4..bbfc15104 100644 --- a/block/src/formats/qcow/internal/mod.rs +++ b/block/src/formats/qcow/internal/mod.rs @@ -21,7 +21,7 @@ use std::io::{self, Seek, SeekFrom}; use std::mem::size_of; use std::os::fd::AsRawFd; use std::path::Path; -use std::str; +use std::{result, str}; pub use header::{ BackingFileConfig, CompressionType, ImageType, IncompatFeatures, MissingFeatureError, @@ -159,7 +159,7 @@ pub enum Error { WritingHeader(#[source] io::Error), } -pub type Result = std::result::Result; +pub type Result = result::Result; /// Concrete backing file variants. pub(crate) enum BackingKind { diff --git a/block/src/formats/qcow/internal/qcow_raw_file.rs b/block/src/formats/qcow/internal/qcow_raw_file.rs index d562e7257..632b0ad37 100644 --- a/block/src/formats/qcow/internal/qcow_raw_file.rs +++ b/block/src/formats/qcow/internal/qcow_raw_file.rs @@ -328,7 +328,7 @@ impl QcowRawFile { self.file.write_all(&data[0..cluster_size]) } - pub fn physical_size(&self) -> Result { + pub fn physical_size(&self) -> io::Result { self.file.metadata().map(|m| m.len()) } } @@ -361,13 +361,14 @@ impl AsFd for QcowRawFile { #[cfg(test)] mod unit_tests { use std::io::{Read, Seek, SeekFrom}; + use std::mem; use vmm_sys_util::tempfile::TempFile; use super::*; fn be_bytes(entries: &[u64]) -> Vec { - let mut v = Vec::with_capacity(std::mem::size_of_val(entries)); + let mut v = Vec::with_capacity(mem::size_of_val(entries)); for e in entries { v.extend_from_slice(&e.to_be_bytes()); } diff --git a/block/src/formats/qcow/internal/raw_file.rs b/block/src/formats/qcow/internal/raw_file.rs index fa33478bf..1a3be85fe 100644 --- a/block/src/formats/qcow/internal/raw_file.rs +++ b/block/src/formats/qcow/internal/raw_file.rs @@ -13,7 +13,7 @@ use std::fs::{File, Metadata}; use std::io::{self, Read, Seek, SeekFrom, Write}; use std::os::fd::{AsFd, BorrowedFd}; use std::os::unix::io::{AsRawFd, RawFd}; -use std::slice; +use std::{result, slice}; use vmm_sys_util::file_traits::FileSync; use vmm_sys_util::seek_hole::SeekHole; @@ -88,15 +88,15 @@ impl RawFile { && buf.len().is_multiple_of(self.alignment) } - pub fn set_len(&self, size: u64) -> std::io::Result<()> { + pub fn set_len(&self, size: u64) -> io::Result<()> { self.file.set_len(size) } - pub fn metadata(&self) -> std::io::Result { + pub fn metadata(&self) -> io::Result { self.file.metadata() } - pub fn try_clone(&self) -> std::io::Result { + pub fn try_clone(&self) -> io::Result { Ok(RawFile { file: self.file.try_clone().expect("RawFile cloning failed"), alignment: self.alignment, @@ -105,11 +105,11 @@ impl RawFile { }) } - pub fn sync_all(&self) -> std::io::Result<()> { + pub fn sync_all(&self) -> io::Result<()> { self.file.sync_all() } - pub fn sync_data(&self) -> std::io::Result<()> { + pub fn sync_data(&self) -> io::Result<()> { self.file.sync_data() } @@ -134,7 +134,7 @@ impl RawFile { } impl Read for RawFile { - fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + fn read(&mut self, buf: &mut [u8]) -> io::Result { if self.is_aligned(buf) { match self.file.read(buf) { Ok(r) => { @@ -214,7 +214,7 @@ impl Read for RawFile { } impl Write for RawFile { - fn write(&mut self, buf: &[u8]) -> std::io::Result { + fn write(&mut self, buf: &[u8]) -> io::Result { if self.is_aligned(buf) { match self.file.write(buf) { Ok(r) => { @@ -307,13 +307,13 @@ impl Write for RawFile { } } - fn flush(&mut self) -> std::io::Result<()> { + fn flush(&mut self) -> io::Result<()> { self.file.sync_all() } } impl Seek for RawFile { - fn seek(&mut self, newpos: SeekFrom) -> std::io::Result { + fn seek(&mut self, newpos: SeekFrom) -> io::Result { match self.file.seek(newpos) { Ok(pos) => { self.position = pos; @@ -325,25 +325,25 @@ impl Seek for RawFile { } impl WriteZeroesAt for RawFile { - fn write_zeroes_at(&mut self, offset: u64, length: usize) -> std::io::Result { + fn write_zeroes_at(&mut self, offset: u64, length: usize) -> io::Result { self.file.write_zeroes_at(offset, length) } } impl PunchHole for RawFile { - fn punch_hole(&mut self, offset: u64, length: u64) -> std::io::Result<()> { + fn punch_hole(&mut self, offset: u64, length: u64) -> io::Result<()> { self.file.punch_hole(offset, length) } } impl FileSync for RawFile { - fn fsync(&mut self) -> std::io::Result<()> { + fn fsync(&mut self) -> io::Result<()> { self.file.fsync() } } impl SeekHole for RawFile { - fn seek_hole(&mut self, offset: u64) -> std::io::Result> { + fn seek_hole(&mut self, offset: u64) -> io::Result> { match self.file.seek_hole(offset) { Ok(pos) => { if let Some(p) = pos { @@ -355,7 +355,7 @@ impl SeekHole for RawFile { } } - fn seek_data(&mut self, offset: u64) -> std::io::Result> { + fn seek_data(&mut self, offset: u64) -> io::Result> { match self.file.seek_data(offset) { Ok(pos) => { if let Some(p) = pos { @@ -369,13 +369,13 @@ impl SeekHole for RawFile { } impl BlockBackend for RawFile { - fn logical_size(&self) -> std::result::Result { + fn logical_size(&self) -> result::Result { Ok(query_device_size(&self.file) .map_err(crate::Error::RawFileError)? .0) } - fn physical_size(&self) -> std::result::Result { + fn physical_size(&self) -> result::Result { Ok(query_device_size(&self.file) .map_err(crate::Error::RawFileError)? .1) diff --git a/block/src/formats/qcow/internal/refcount.rs b/block/src/formats/qcow/internal/refcount.rs index 7f56af55d..77beac7d0 100644 --- a/block/src/formats/qcow/internal/refcount.rs +++ b/block/src/formats/qcow/internal/refcount.rs @@ -4,7 +4,7 @@ // // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause -use std::io; +use std::{io, result}; use libc::EINVAL; use thiserror::Error; @@ -41,7 +41,7 @@ pub enum Error { }, } -pub type Result = std::result::Result; +pub type Result = result::Result; /// Represents the refcount entries for an open qcow file. #[derive(Clone, Debug)] @@ -174,7 +174,7 @@ impl RefCount { if addr != 0 { raw_file.write_refcount_block(addr, block.get_values())?; } else { - return Err(std::io::Error::from_raw_os_error(EINVAL)); + return Err(io::Error::from_raw_os_error(EINVAL)); } block.mark_clean(); } diff --git a/block/src/formats/qcow/mod.rs b/block/src/formats/qcow/mod.rs index 7e34d0492..3ccd849e4 100644 --- a/block/src/formats/qcow/mod.rs +++ b/block/src/formats/qcow/mod.rs @@ -15,6 +15,8 @@ use std::fs::File; #[cfg(any(test, feature = "test-utils"))] use std::io::Seek; use std::os::unix::io::AsRawFd; +#[cfg(any(test, feature = "test-utils"))] +use std::path::Path; use std::sync::Arc; use std::{fmt, io}; @@ -211,7 +213,7 @@ impl QcowTempDisk { Ok(Self { tmp, disk }) } - pub fn path(&self) -> &std::path::Path { + pub fn path(&self) -> &Path { self.tmp.as_path() } diff --git a/block/src/formats/qcow/worker/async_uring.rs b/block/src/formats/qcow/worker/async_uring.rs index 17aa77bfd..d795040fe 100644 --- a/block/src/formats/qcow/worker/async_uring.rs +++ b/block/src/formats/qcow/worker/async_uring.rs @@ -525,7 +525,7 @@ impl QcowAsync { mod unit_tests { use std::io::Write; use std::sync::Arc; - use std::thread; + use std::{mem, thread}; use vm_memory::{Bytes, GuestAddress, GuestMemoryMmap}; use vmm_sys_util::tempfile::TempFile; @@ -902,7 +902,7 @@ mod unit_tests { let mut completion_a = wait_for_completion(async_io.as_mut()); let mut completion_b = wait_for_completion(async_io.as_mut()); if completion_a.user_data > completion_b.user_data { - std::mem::swap(&mut completion_a, &mut completion_b); + mem::swap(&mut completion_a, &mut completion_b); } assert_eq!(completion_tuple(&completion_a), (30, 4096)); assert_eq!(completion_tuple(&completion_b), (40, 4096)); diff --git a/block/src/formats/qcow/worker/sync.rs b/block/src/formats/qcow/worker/sync.rs index 7e90d8361..4ed6170df 100644 --- a/block/src/formats/qcow/worker/sync.rs +++ b/block/src/formats/qcow/worker/sync.rs @@ -338,7 +338,7 @@ impl AsyncIo for QcowSync { #[cfg(test)] mod unit_tests { - use std::fs::{File, OpenOptions}; + use std::fs::{File, OpenOptions, create_dir}; use std::io::{Read, Seek, SeekFrom, Write}; use std::os::fd::RawFd; use std::path::Path; @@ -969,8 +969,8 @@ mod unit_tests { let test_dir = TempDir::new_with_prefix("/tmp/ch").unwrap(); let overlay_dir = test_dir.as_path().join("overlay"); let sibling_dir = test_dir.as_path().join("sibling"); - std::fs::create_dir(&overlay_dir).unwrap(); - std::fs::create_dir(&sibling_dir).unwrap(); + create_dir(&overlay_dir).unwrap(); + create_dir(&sibling_dir).unwrap(); let backing_path = sibling_dir.join("backing.raw"); let overlay_path = overlay_dir.join("overlay.qcow2"); diff --git a/block/src/formats/raw/worker/sync.rs b/block/src/formats/raw/worker/sync.rs index 858fb3855..1bad816bb 100644 --- a/block/src/formats/raw/worker/sync.rs +++ b/block/src/formats/raw/worker/sync.rs @@ -5,6 +5,7 @@ // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause use std::collections::VecDeque; +use std::io; use std::os::unix::io::RawFd; use vmm_sys_util::eventfd::EventFd; @@ -74,7 +75,7 @@ impl AsyncIo for RawSync { } }; if result < 0 { - let error = std::io::Error::last_os_error(); + let error = io::Error::last_os_error(); return Err(if is_read { AsyncIoError::ReadVectored(error) } else { @@ -93,7 +94,7 @@ impl AsyncIo for RawSync { // SAFETY: FFI call let result = unsafe { libc::fsync(self.fd as libc::c_int) }; if result < 0 { - return Err(AsyncIoError::Fsync(std::io::Error::last_os_error())); + return Err(AsyncIoError::Fsync(io::Error::last_os_error())); } if let Some(user_data) = user_data { diff --git a/block/src/formats/vhd/internal/fixed.rs b/block/src/formats/vhd/internal/fixed.rs index 9b8b2d682..3292081d3 100644 --- a/block/src/formats/vhd/internal/fixed.rs +++ b/block/src/formats/vhd/internal/fixed.rs @@ -3,7 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 use std::fs::File; -use std::io::{Read, Seek, SeekFrom, Write}; +use std::io::{self, Read, Seek, SeekFrom, Write}; use std::os::unix::io::{AsRawFd, RawFd}; use super::footer::VhdFooter; @@ -17,7 +17,7 @@ pub struct FixedVhd { } impl FixedVhd { - pub fn new(mut file: File) -> std::io::Result { + pub fn new(mut file: File) -> io::Result { let footer = VhdFooter::new(&mut file)?; Ok(Self { @@ -35,7 +35,7 @@ impl AsRawFd for FixedVhd { } impl Read for FixedVhd { - fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + fn read(&mut self, buf: &mut [u8]) -> io::Result { match self.file.read(buf) { Ok(r) => { self.position = self.position.checked_add(r.try_into().unwrap()).unwrap(); @@ -47,7 +47,7 @@ impl Read for FixedVhd { } impl Write for FixedVhd { - fn write(&mut self, buf: &[u8]) -> std::io::Result { + fn write(&mut self, buf: &[u8]) -> io::Result { match self.file.write(buf) { Ok(r) => { self.position = self.position.checked_add(r.try_into().unwrap()).unwrap(); @@ -57,13 +57,13 @@ impl Write for FixedVhd { } } - fn flush(&mut self) -> std::io::Result<()> { + fn flush(&mut self) -> io::Result<()> { self.file.sync_all() } } impl Seek for FixedVhd { - fn seek(&mut self, newpos: SeekFrom) -> std::io::Result { + fn seek(&mut self, newpos: SeekFrom) -> io::Result { match self.file.seek(newpos) { Ok(pos) => { self.position = pos; diff --git a/block/src/formats/vhd/internal/footer.rs b/block/src/formats/vhd/internal/footer.rs index cf73e7c84..4395fb167 100644 --- a/block/src/formats/vhd/internal/footer.rs +++ b/block/src/formats/vhd/internal/footer.rs @@ -3,7 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 use std::fs::File; -use std::io::{Seek, SeekFrom}; +use std::io::{self, Seek, SeekFrom}; use crate::{DiskTopology, read_aligned_block_size}; @@ -31,7 +31,7 @@ pub struct VhdFooter { } impl VhdFooter { - pub fn new(file: &mut File) -> std::io::Result { + pub fn new(file: &mut File) -> io::Result { let blocksize = DiskTopology::probe(file)?.logical_block_size as usize; // Place the cursor in the last block of the file @@ -120,7 +120,7 @@ impl VhdFooter { } /// Determine image type through file parsing. -pub fn is_fixed_vhd(f: &mut File) -> std::io::Result { +pub fn is_fixed_vhd(f: &mut File) -> io::Result { let footer = VhdFooter::new(f)?; // "conectix" => 0x636f6e6563746978 diff --git a/block/src/formats/vhd/mod.rs b/block/src/formats/vhd/mod.rs index 58f68c2c6..52cf6772b 100644 --- a/block/src/formats/vhd/mod.rs +++ b/block/src/formats/vhd/mod.rs @@ -140,7 +140,7 @@ mod unit_tests { use super::*; use crate::async_io::AsyncIo; #[cfg(feature = "io_uring")] - use crate::async_io::{AsyncIoOperation, OwnedIoBuffer}; + use crate::async_io::{AsyncIoError, AsyncIoOperation, OwnedIoBuffer}; use crate::disk_file::{AsyncDiskFile, DiskSize, PhysicalSize, Resizable}; /// Minimal fixed VHD footer (disk type = 2, current_size = 0x11223344). @@ -214,7 +214,7 @@ mod unit_tests { assert!(matches!( async_io.submit_batch_requests(vec![op]), - Err(crate::async_io::AsyncIoError::ReadVectored(_)) + Err(AsyncIoError::ReadVectored(_)) )); } diff --git a/block/src/formats/vhd/worker/async_uring.rs b/block/src/formats/vhd/worker/async_uring.rs index 20402916f..294459678 100644 --- a/block/src/formats/vhd/worker/async_uring.rs +++ b/block/src/formats/vhd/worker/async_uring.rs @@ -4,6 +4,7 @@ // // SPDX-License-Identifier: Apache-2.0 +use std::io; use std::os::unix::io::RawFd; use vmm_sys_util::eventfd::EventFd; @@ -42,8 +43,8 @@ impl FixedVhdAsync { } fn bounds_error(&self, op: &AsyncIoOperation) -> AsyncIoError { - let error = std::io::Error::new( - std::io::ErrorKind::InvalidData, + let error = io::Error::new( + io::ErrorKind::InvalidData, format!( "Invalid request offset {} and length {}, can't exceed file size {}", op.offset(), @@ -78,13 +79,13 @@ impl AsyncIo for FixedVhdAsync { } fn punch_hole(&mut self, _offset: u64, _length: u64, _user_data: u64) -> AsyncIoResult<()> { - Err(AsyncIoError::PunchHole(std::io::Error::other( + Err(AsyncIoError::PunchHole(io::Error::other( "punch_hole not supported for fixed VHD", ))) } fn write_zeroes(&mut self, _offset: u64, _length: u64, _user_data: u64) -> AsyncIoResult<()> { - Err(AsyncIoError::WriteZeroes(std::io::Error::other( + Err(AsyncIoError::WriteZeroes(io::Error::other( "write_zeroes not supported for fixed VHD", ))) } diff --git a/block/src/formats/vhd/worker/sync.rs b/block/src/formats/vhd/worker/sync.rs index 0aa3f9377..d0a91e183 100644 --- a/block/src/formats/vhd/worker/sync.rs +++ b/block/src/formats/vhd/worker/sync.rs @@ -4,6 +4,7 @@ // // SPDX-License-Identifier: Apache-2.0 +use std::io; use std::os::unix::io::RawFd; use vmm_sys_util::eventfd::EventFd; @@ -17,7 +18,7 @@ pub struct FixedVhdSync { } impl FixedVhdSync { - pub fn new(fd: RawFd, size: u64) -> std::io::Result { + pub fn new(fd: RawFd, size: u64) -> io::Result { Ok(FixedVhdSync { raw_file_sync: RawSync::new(fd), size, @@ -33,8 +34,8 @@ impl AsyncIo for FixedVhdSync { fn submit_data_operation(&mut self, op: AsyncIoOperation) -> AsyncIoResult<()> { let offset = op.offset(); if offset as u64 >= self.size { - let error = std::io::Error::new( - std::io::ErrorKind::InvalidData, + let error = io::Error::new( + io::ErrorKind::InvalidData, format!( "Invalid offset {}, can't be larger than file size {}", offset, self.size @@ -59,13 +60,13 @@ impl AsyncIo for FixedVhdSync { } fn punch_hole(&mut self, _offset: u64, _length: u64, _user_data: u64) -> AsyncIoResult<()> { - Err(AsyncIoError::PunchHole(std::io::Error::other( + Err(AsyncIoError::PunchHole(io::Error::other( "punch_hole not supported for fixed VHD", ))) } fn write_zeroes(&mut self, _offset: u64, _length: u64, _user_data: u64) -> AsyncIoResult<()> { - Err(AsyncIoError::WriteZeroes(std::io::Error::other( + Err(AsyncIoError::WriteZeroes(io::Error::other( "write_zeroes not supported for fixed VHD", ))) } diff --git a/block/src/formats/vhdx/internal/bat.rs b/block/src/formats/vhdx/internal/bat.rs index 04f30e406..826e5ea31 100644 --- a/block/src/formats/vhdx/internal/bat.rs +++ b/block/src/formats/vhdx/internal/bat.rs @@ -5,6 +5,7 @@ use std::fs::File; use std::io::{self, Seek, SeekFrom}; use std::mem::size_of; +use std::result; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use remain::sorted; @@ -39,7 +40,7 @@ pub enum VhdxBatError { WriteBat(#[source] io::Error), } -pub type Result = std::result::Result; +pub type Result = result::Result; #[derive(Default, Clone, Debug)] pub struct BatEntry(pub u64); diff --git a/block/src/formats/vhdx/internal/header.rs b/block/src/formats/vhdx/internal/header.rs index be7ebb124..0882d3a33 100644 --- a/block/src/formats/vhdx/internal/header.rs +++ b/block/src/formats/vhdx/internal/header.rs @@ -6,6 +6,7 @@ use std::collections::btree_map::BTreeMap; use std::fs::File; use std::io::{self, Read, Seek, SeekFrom, Write}; use std::mem::size_of; +use std::{result, slice}; use byteorder::{ByteOrder, LittleEndian, ReadBytesExt}; use remain::sorted; @@ -60,7 +61,7 @@ pub enum VhdxHeaderError { #[error("Failed to read headers {0}")] ReadHeader(#[source] io::Error), #[error("Failed to read metadata {0}")] - ReadMetadata(#[source] std::io::Error), + ReadMetadata(#[source] io::Error), #[error("Failed to read region table entries {0}")] ReadRegionTableEntries(#[source] io::Error), #[error("Failed to read region table header {0}")] @@ -85,7 +86,7 @@ pub enum VhdxHeaderError { WriteHeader(#[source] io::Error), } -pub type Result = std::result::Result; +pub type Result = result::Result; #[derive(Clone, Debug)] pub struct FileTypeIdentifier { @@ -152,7 +153,7 @@ impl Header { fn write_to_buffer(&self, buffer: &mut [u8; HEADER_SIZE as usize]) { // SAFETY: self is a valid header. let reference = - unsafe { std::slice::from_raw_parts((&raw const *self).cast(), HEADER_SIZE as usize) }; + unsafe { slice::from_raw_parts((&raw const *self).cast(), HEADER_SIZE as usize) }; *buffer = reference.try_into().unwrap(); } @@ -337,7 +338,7 @@ pub struct RegionTableEntry { impl RegionTableEntry { /// Reads one Region Entry from a Region Table index that starts from 0 pub fn new(buffer: &[u8]) -> Result { - assert!(buffer.len() == std::mem::size_of::()); + assert!(buffer.len() == size_of::()); // SAFETY: the assertion above makes sure the buffer size is correct. let mut region_table_entry: RegionTableEntry = unsafe { *(buffer.as_ptr().cast()) }; diff --git a/block/src/formats/vhdx/internal/io.rs b/block/src/formats/vhdx/internal/io.rs index 80e52bfbc..924355f38 100644 --- a/block/src/formats/vhdx/internal/io.rs +++ b/block/src/formats/vhdx/internal/io.rs @@ -4,6 +4,7 @@ use std::fs::File; use std::io::{self, Read, Seek, SeekFrom, Write}; +use std::result; use remain::sorted; use thiserror::Error; @@ -32,7 +33,7 @@ pub enum VhdxIoError { WriteBat(#[source] VhdxBatError), } -pub type Result = std::result::Result; +pub type Result = result::Result; macro_rules! align { ($n:expr, $align:expr) => {{ $n.div_ceil($align) * $align }}; diff --git a/block/src/formats/vhdx/internal/metadata.rs b/block/src/formats/vhdx/internal/metadata.rs index f0e31890d..71cdc3284 100644 --- a/block/src/formats/vhdx/internal/metadata.rs +++ b/block/src/formats/vhdx/internal/metadata.rs @@ -5,6 +5,7 @@ use std::fs::File; use std::io::{self, Read, Seek, SeekFrom}; use std::mem::size_of; +use std::result; use byteorder::{LittleEndian, ReadBytesExt}; use remain::sorted; @@ -83,7 +84,7 @@ pub enum VhdxMetadataError { UnsupportedFlag, } -pub type Result = std::result::Result; +pub type Result = result::Result; #[derive(Default, Clone, Debug)] pub struct DiskSpec { @@ -278,7 +279,7 @@ struct MetadataTableHeader { impl MetadataTableHeader { pub fn new(buffer: &[u8]) -> Result { - assert!(buffer.len() == std::mem::size_of::()); + assert!(buffer.len() == size_of::()); // SAFETY: the assertion above makes sure the buffer size is correct. let metadata_table_header: MetadataTableHeader = unsafe { *(buffer.as_ptr().cast()) }; @@ -311,7 +312,7 @@ pub struct MetadataTableEntry { impl MetadataTableEntry { /// Parse one metadata entry from the buffer fn new(buffer: &[u8]) -> Result { - assert!(buffer.len() == std::mem::size_of::()); + assert!(buffer.len() == size_of::()); // SAFETY: the assertion above makes sure the buffer size is correct. let mut metadata_table_entry: MetadataTableEntry = unsafe { *(buffer.as_ptr().cast()) }; diff --git a/block/src/formats/vhdx/internal/mod.rs b/block/src/formats/vhdx/internal/mod.rs index bc3eaed75..0efb895bf 100644 --- a/block/src/formats/vhdx/internal/mod.rs +++ b/block/src/formats/vhdx/internal/mod.rs @@ -4,8 +4,11 @@ use std::collections::btree_map::BTreeMap; use std::fs::File; -use std::io::{Read, Seek, SeekFrom, Write}; +use std::io::{ + Error as IoError, ErrorKind as IoErrorKind, Read, Result as IoResult, Seek, SeekFrom, Write, +}; use std::os::fd::{AsRawFd, RawFd}; +use std::result; use byteorder::{BigEndian, ByteOrder}; use remain::sorted; @@ -42,7 +45,7 @@ pub enum VhdxError { WriteFailed(#[source] VhdxIoError), } -pub type Result = std::result::Result; +pub type Result = result::Result; #[derive(Debug)] pub struct Vhdx { @@ -99,7 +102,7 @@ impl Vhdx { impl Read for Vhdx { /// Wrapper function to satisfy Read trait implementation for VHDx disk. /// Convert the offset to sector index and buffer length to sector count. - fn read(&mut self, buf: &mut [u8]) -> std::result::Result { + fn read(&mut self, buf: &mut [u8]) -> IoResult { let sector_count = (buf.len() as u64).div_ceil(self.disk_spec.logical_sector_size as u64); let sector_index = self.current_offset / self.disk_spec.logical_sector_size as u64; @@ -112,7 +115,7 @@ impl Read for Vhdx { sector_count, ) .map_err(|e| { - std::io::Error::other(format!( + IoError::other(format!( "Failed reading {sector_count} sectors from VHDx at index {sector_index}: {e}" )) })?; @@ -124,13 +127,13 @@ impl Read for Vhdx { } impl Write for Vhdx { - fn flush(&mut self) -> std::result::Result<(), std::io::Error> { + fn flush(&mut self) -> IoResult<()> { self.file.flush() } /// Wrapper function to satisfy Write trait implementation for VHDx disk. /// Convert the offset to sector index and buffer length to sector count. - fn write(&mut self, buf: &[u8]) -> std::result::Result { + fn write(&mut self, buf: &[u8]) -> IoResult { let sector_count = (buf.len() as u64).div_ceil(self.disk_spec.logical_sector_size as u64); let sector_index = self.current_offset / self.disk_spec.logical_sector_size as u64; @@ -138,7 +141,7 @@ impl Write for Vhdx { self.first_write = false; self.vhdx_header .update(&mut self.file) - .map_err(|e| std::io::Error::other(format!("Failed to update VHDx header: {e}")))?; + .map_err(|e| IoError::other(format!("Failed to update VHDx header: {e}")))?; } let result = io::write( @@ -151,7 +154,7 @@ impl Write for Vhdx { sector_count, ) .map_err(|e| { - std::io::Error::other(format!( + IoError::other(format!( "Failed writing {sector_count} sectors on VHDx at index {sector_index}: {e}" )) })?; @@ -165,7 +168,7 @@ impl Write for Vhdx { impl Seek for Vhdx { /// Wrapper function to satisfy Seek trait implementation for VHDx disk. /// Updates the offset field in the Vhdx struct. - fn seek(&mut self, pos: SeekFrom) -> std::io::Result { + fn seek(&mut self, pos: SeekFrom) -> IoResult { let new_offset: Option = match pos { SeekFrom::Start(off) => Some(off), SeekFrom::End(off) => { @@ -194,19 +197,19 @@ impl Seek for Vhdx { return Ok(o); } - Err(std::io::Error::new( - std::io::ErrorKind::InvalidData, + Err(IoError::new( + IoErrorKind::InvalidData, "Failed seek operation", )) } } impl BlockBackend for Vhdx { - fn logical_size(&self) -> std::result::Result { + fn logical_size(&self) -> result::Result { Ok(self.virtual_disk_size()) } - fn physical_size(&self) -> std::result::Result { + fn physical_size(&self) -> result::Result { self.file .metadata() .map(|m| m.len()) diff --git a/block/src/formats/vhdx/mod.rs b/block/src/formats/vhdx/mod.rs index fbf0e6e76..7a48d5b77 100644 --- a/block/src/formats/vhdx/mod.rs +++ b/block/src/formats/vhdx/mod.rs @@ -13,6 +13,7 @@ pub mod internal; pub(crate) mod worker; use std::fs::File; +use std::io; use std::os::fd::AsRawFd; use std::sync::{Arc, Mutex}; @@ -89,7 +90,7 @@ impl disk_file::Resizable for VhdxDisk { fn resize(&mut self, _size: u64) -> BlockResult<()> { Err(BlockError::new( BlockErrorKind::UnsupportedFeature, - DiskFileError::ResizeError(std::io::Error::other("resize not supported for VHDX")), + DiskFileError::ResizeError(io::Error::other("resize not supported for VHDX")), ) .with_op(ErrorOp::Resize)) } diff --git a/block/src/formats/vhdx/worker/sync.rs b/block/src/formats/vhdx/worker/sync.rs index e8d718f14..19a101a83 100644 --- a/block/src/formats/vhdx/worker/sync.rs +++ b/block/src/formats/vhdx/worker/sync.rs @@ -5,7 +5,7 @@ // SPDX-License-Identifier: Apache-2.0 use std::collections::VecDeque; -use std::io::{Read, Seek, SeekFrom, Write}; +use std::io::{self, Read, Seek, SeekFrom, Write}; use std::sync::{Arc, Mutex}; use vmm_sys_util::eventfd::EventFd; @@ -96,13 +96,13 @@ impl AsyncIo for VhdxSync { } fn punch_hole(&mut self, _offset: u64, _length: u64, _user_data: u64) -> AsyncIoResult<()> { - Err(AsyncIoError::PunchHole(std::io::Error::other( + Err(AsyncIoError::PunchHole(io::Error::other( "punch_hole not supported for VHDX", ))) } fn write_zeroes(&mut self, _offset: u64, _length: u64, _user_data: u64) -> AsyncIoResult<()> { - Err(AsyncIoError::WriteZeroes(std::io::Error::other( + Err(AsyncIoError::WriteZeroes(io::Error::other( "write_zeroes not supported for VHDX", ))) }