block: Remove redundant BlockBackend trait

BlockBackend predated the disk_file trait family and only carried
logical_size and physical_size, which the disk backends expose
through the disk_file traits DiskSize and PhysicalSize.

It added no polymorphism while its Read, Write and Seek supertraits
forced an unused cursor. Dropping the trait removes the dead code
it was keeping alive.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski
2026-06-23 17:19:52 +02:00
committed by Bo Chen
parent 17cc156ccb
commit 755d42eec0
6 changed files with 10 additions and 87 deletions

View File

@@ -3,17 +3,15 @@
// SPDX-License-Identifier: Apache-2.0
use std::fs::File;
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::io;
use std::os::unix::io::{AsRawFd, RawFd};
use super::footer::VhdFooter;
use crate::BlockBackend;
#[derive(Debug)]
pub struct FixedVhd {
file: File,
size: u64,
position: u64,
}
impl FixedVhd {
@@ -23,7 +21,6 @@ impl FixedVhd {
Ok(Self {
file,
size: footer.current_size(),
position: 0,
})
}
@@ -38,53 +35,13 @@ impl AsRawFd for FixedVhd {
}
}
impl Read for FixedVhd {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.file.read(buf) {
Ok(r) => {
self.position = self.position.checked_add(r.try_into().unwrap()).unwrap();
Ok(r)
}
Err(e) => Err(e),
}
}
}
impl Write for FixedVhd {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
match self.file.write(buf) {
Ok(r) => {
self.position = self.position.checked_add(r.try_into().unwrap()).unwrap();
Ok(r)
}
Err(e) => Err(e),
}
}
fn flush(&mut self) -> io::Result<()> {
self.file.sync_all()
}
}
impl Seek for FixedVhd {
fn seek(&mut self, newpos: SeekFrom) -> io::Result<u64> {
match self.file.seek(newpos) {
Ok(pos) => {
self.position = pos;
Ok(pos)
}
Err(e) => Err(e),
}
}
}
impl BlockBackend for FixedVhd {
fn logical_size(&self) -> Result<u64, crate::Error> {
impl FixedVhd {
pub(crate) fn logical_size(&self) -> Result<u64, crate::Error> {
Ok(self.size)
}
/// Returns the physical size of the underlying file.
fn physical_size(&self) -> Result<u64, crate::Error> {
pub(crate) fn physical_size(&self) -> Result<u64, crate::Error> {
self.file
.metadata()
.map(|m| m.len())
@@ -97,7 +54,6 @@ impl Clone for FixedVhd {
Self {
file: self.file.try_clone().expect("FixedVhd cloning failed"),
size: self.size,
position: self.position,
}
}
}

View File

@@ -26,7 +26,7 @@ use self::worker::sync::FixedVhdSync;
use crate::async_io::{AsyncIo, BorrowedDiskFd, DiskFileError};
use crate::disk_file::DiskSize;
use crate::error::{BlockError, BlockErrorKind, BlockResult, ErrorOp};
use crate::{AlignedFile, BlockBackend, DiskTopology, Error, disk_file};
use crate::{AlignedFile, DiskTopology, Error, disk_file};
#[derive(Debug)]
pub struct VhdDisk {

View File

@@ -19,7 +19,6 @@ use self::bat::{BatEntry, VhdxBatError};
use self::header::{RegionInfo, RegionTableEntry, VhdxHeader, VhdxHeaderError};
use self::io::VhdxIoError;
use self::metadata::{DiskSpec, VhdxMetadataError};
use crate::BlockBackend;
use crate::aligned_file::AlignedFile;
mod bat;
@@ -207,12 +206,8 @@ impl Seek for Vhdx {
}
}
impl BlockBackend for Vhdx {
fn logical_size(&self) -> result::Result<u64, crate::Error> {
Ok(self.virtual_disk_size())
}
fn physical_size(&self) -> result::Result<u64, crate::Error> {
impl Vhdx {
pub(crate) fn physical_size(&self) -> result::Result<u64, crate::Error> {
self.aligned
.file()
.metadata()

View File

@@ -22,7 +22,7 @@ pub use internal::{Vhdx, VhdxError};
use self::worker::sync::VhdxSync;
use crate::async_io::{AsyncIo, BorrowedDiskFd, DiskFileError};
use crate::error::{BlockError, BlockErrorKind, BlockResult, ErrorOp};
use crate::{BlockBackend, Error, disk_file};
use crate::{Error, disk_file};
#[derive(Debug)]
pub struct VhdxDisk {