block: qcow: impl AsFd for RawFile and QcowRawFile

Implement AsFd for both RawFile and QcowRawFile by delegating to
the inner File handle. This enables safe fd borrowing through the
standard AsFd trait, which is a prerequisite for replacing unsafe
libc::dup calls with BorrowedFd::try_clone_to_owned().

Suggested-by: Rob Bradford <rbradford@rivosinc.com>
Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski
2026-03-11 00:05:19 +01:00
committed by Rob Bradford
parent 9be154b03c
commit 8c2794533d
2 changed files with 14 additions and 1 deletions

View File

@@ -7,7 +7,7 @@
use std::fmt::Debug;
use std::io::{self, BufWriter, Read, Seek, SeekFrom, Write};
use std::mem::size_of;
use std::os::fd::{AsRawFd, RawFd};
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, RawFd};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use vmm_sys_util::write_zeroes::WriteZeroes;
@@ -361,3 +361,9 @@ impl AsRawFd for QcowRawFile {
self.file.as_raw_fd()
}
}
impl AsFd for QcowRawFile {
fn as_fd(&self) -> BorrowedFd<'_> {
self.file.as_fd()
}
}

View File

@@ -11,6 +11,7 @@
use std::alloc::{Layout, alloc_zeroed, dealloc};
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;
@@ -397,3 +398,9 @@ impl AsRawFd for RawFile {
self.file.as_raw_fd()
}
}
impl AsFd for RawFile {
fn as_fd(&self) -> BorrowedFd<'_> {
self.file.as_fd()
}
}