Skip to content

Commit 35587db

Browse files
committed
fs: introduce open_by_handle_at()
1 parent bdb8a1b commit 35587db

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

src/backend/libc/fs/syscalls.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1911,6 +1911,29 @@ pub(crate) fn name_to_handle_at(
19111911
}
19121912
}
19131913

1914+
#[cfg(target_os = "linux")]
1915+
pub(crate) fn open_by_handle_at(
1916+
mount_fd: BorrowedFd<'_>,
1917+
handle: *const core::ffi::c_void,
1918+
flags: OFlags,
1919+
) -> io::Result<OwnedFd> {
1920+
syscall! {
1921+
fn open_by_handle_at(
1922+
mount_fd: c::c_int,
1923+
handle: *const ffi::c_void,
1924+
flags: u32
1925+
) via SYS_open_by_handle_at -> c::c_int
1926+
}
1927+
1928+
unsafe {
1929+
ret_owned_fd(open_by_handle_at(
1930+
borrowed_fd(mount_fd),
1931+
handle,
1932+
flags.bits(),
1933+
))
1934+
}
1935+
}
1936+
19141937
#[cfg(target_os = "linux")]
19151938
pub(crate) fn sendfile(
19161939
out_fd: BorrowedFd<'_>,

src/backend/linux_raw/fs/syscalls.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,15 @@ pub(crate) fn name_to_handle_at(
16771677
}
16781678
}
16791679

1680+
#[inline]
1681+
pub(crate) fn open_by_handle_at(
1682+
mount_fd: BorrowedFd<'_>,
1683+
handle: *const core::ffi::c_void,
1684+
flags: OFlags,
1685+
) -> io::Result<OwnedFd> {
1686+
unsafe { ret_owned_fd(syscall!(__NR_open_by_handle_at, mount_fd, handle, flags)) }
1687+
}
1688+
16801689
// Some linux_raw_sys structs have unsigned types for values which are
16811690
// interpreted as signed. This defines a utility or casting to the
16821691
// same-sized signed type.

src/fs/filehandle.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ impl FileHandle {
7373
fn as_mut_ptr(&mut self) -> *mut ffi::c_void {
7474
self.raw.as_mut_ptr() as *mut _
7575
}
76+
77+
fn as_ptr(&self) -> *const ffi::c_void {
78+
self.raw.as_ptr() as *const _
79+
}
7680
}
7781

7882
/// An identifier for a mount that is returned by [`name_to_handle_at`].
@@ -143,6 +147,20 @@ pub fn name_to_handle_at<Fd: AsFd, P: path::Arg>(
143147
})
144148
}
145149

150+
/// `open_by_handle_at(mount_fd, handle, flags)` - Open a file by filehandle.
151+
///
152+
/// # References
153+
/// - [Linux]
154+
///
155+
/// [Linux]: https://man7.org/linux/man-pages/man2/open_by_handle_at.2.html
156+
pub fn open_by_handle_at<Fd: AsFd>(
157+
mount_fd: Fd,
158+
handle: &FileHandle,
159+
flags: OFlags,
160+
) -> io::Result<OwnedFd> {
161+
backend::fs::syscalls::open_by_handle_at(mount_fd.as_fd(), handle.as_ptr(), flags)
162+
}
163+
146164
#[cfg(test)]
147165
mod tests {
148166
use super::*;

0 commit comments

Comments
 (0)