qcow_utils: use DiskFile trait from disk crate

Drop the local DiskFile trait definition from qcow_utils and use the one
defined by the disk crate, since qcow_utils already depends on disk.

In order to make the switch, use the DiskGetLen::get_len function
instead of seeking to the end of the file to get the current file size.

BUG=None
TEST=emerge-nami crosvm
TEST=cargo build -p qcow_utils

Change-Id: Ie4b3b8ee0fb11ef02fbc322c5b0f9e22b0345bb0
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2056991
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Daniel Verkamp 2020-01-03 13:38:03 -08:00 committed by Commit Bot
parent ed6c972994
commit 1ba620d821

View file

@ -7,14 +7,10 @@
use libc::{EINVAL, EIO, ENOSYS};
use std::ffi::CStr;
use std::fs::OpenOptions;
use std::io::{Seek, SeekFrom};
use std::os::raw::{c_char, c_int};
use disk::{ImageType, QcowFile};
use sys_util::{flock, FileSetLen, FlockOperation};
trait DiskFile: FileSetLen + Seek {}
impl<D: FileSetLen + Seek> DiskFile for D {}
use disk::{DiskFile, ImageType, QcowFile};
use sys_util::{flock, FlockOperation};
#[no_mangle]
pub unsafe extern "C" fn create_qcow_with_size(path: *const c_char, virtual_size: u64) -> c_int {
@ -73,7 +69,7 @@ pub unsafe extern "C" fn expand_disk_image(path: *const c_char, virtual_size: u6
Err(_) => return -EINVAL,
};
let mut disk_image: Box<dyn DiskFile> = match image_type {
let disk_image: Box<dyn DiskFile> = match image_type {
ImageType::Raw => Box::new(raw_image),
ImageType::Qcow2 => match QcowFile::from(raw_image) {
Ok(f) => Box::new(f),
@ -89,7 +85,7 @@ pub unsafe extern "C" fn expand_disk_image(path: *const c_char, virtual_size: u6
// acquired by other instances of this function as well as crosvm
// itself when running a VM, so this should be safe in all cases that
// can access a disk image in normal operation.
let current_size = match disk_image.seek(SeekFrom::End(0)) {
let current_size = match disk_image.get_len() {
Ok(len) => len,
Err(_) => return -EIO,
};