|
| 1 | +use core::mem::size_of; |
| 2 | + |
| 3 | +use crate::{backend, ffi, io, path}; |
| 4 | +use backend::fd::{AsFd, OwnedFd}; |
| 5 | +use backend::fs::types::{HandleFlags, OFlags}; |
| 6 | + |
| 7 | +/// This maximum is more of a "guideline"; the man page for name_to_handle_at(2) indicates it could |
| 8 | +/// increase in the future. |
| 9 | +const MAX_HANDLE_SIZE: usize = 128; |
| 10 | + |
| 11 | +/// The minimum size of a `struct file_handle` is the size of an int and an unsigned int, for the |
| 12 | +/// length and type fields. |
| 13 | +const HANDLE_STRUCT_SIZE: usize = size_of::<ffi::c_uint>() + size_of::<ffi::c_int>(); |
| 14 | + |
| 15 | +/// An opaque identifier for a file. |
| 16 | +/// |
| 17 | +/// While the C struct definition in `fcntl.h` exposes fields like length and type, in reality, |
| 18 | +/// user applications cannot usefully interpret (or modify) the separate fields of a file handle, so |
| 19 | +/// this implementation treats the file handle as an entirely opaque sequence of bytes. |
| 20 | +#[derive(Debug)] |
| 21 | +pub struct FileHandle { |
| 22 | + raw: Box<[u8]>, |
| 23 | +} |
| 24 | + |
| 25 | +impl FileHandle { |
| 26 | + fn new(size: usize) -> Self { |
| 27 | + let handle_allocation_size: usize = HANDLE_STRUCT_SIZE + size; |
| 28 | + let bytes = vec![0; handle_allocation_size]; |
| 29 | + |
| 30 | + let mut handle = Self { |
| 31 | + raw: Box::from(bytes), |
| 32 | + }; |
| 33 | + handle.set_handle_bytes(size); |
| 34 | + |
| 35 | + handle |
| 36 | + } |
| 37 | + |
| 38 | + /// Create a file handle from a sequence of bytes. |
| 39 | + /// |
| 40 | + /// # Panics |
| 41 | + /// |
| 42 | + /// Panics if the given handle is malformed, suggesting that it did not originate from a |
| 43 | + /// previous call to name_to_handle_at(). |
| 44 | + pub fn from_raw(raw: Box<[u8]>) -> Self { |
| 45 | + assert!(raw.len() >= HANDLE_STRUCT_SIZE); |
| 46 | + |
| 47 | + let handle = Self { raw }; |
| 48 | + |
| 49 | + assert!(handle.raw.len() >= handle.get_handle_bytes() + HANDLE_STRUCT_SIZE); |
| 50 | + |
| 51 | + handle |
| 52 | + } |
| 53 | + |
| 54 | + /// Get the raw bytes of a file handle. |
| 55 | + pub fn into_raw(self) -> Box<[u8]> { |
| 56 | + self.raw |
| 57 | + } |
| 58 | + |
| 59 | + /// Set the `handle_bytes` field (first 4 bytes of the struct) to the given length. |
| 60 | + fn set_handle_bytes(&mut self, size: usize) { |
| 61 | + self.raw[0..size_of::<ffi::c_uint>()].copy_from_slice(&(size as ffi::c_uint).to_ne_bytes()); |
| 62 | + } |
| 63 | + |
| 64 | + /// Get the length of the file handle data by reading the `handle_bytes` field |
| 65 | + fn get_handle_bytes(&self) -> usize { |
| 66 | + ffi::c_uint::from_ne_bytes( |
| 67 | + self.raw[0..size_of::<ffi::c_uint>()] |
| 68 | + .try_into() |
| 69 | + .expect("Vector should be long enough"), |
| 70 | + ) as usize |
| 71 | + } |
| 72 | + |
| 73 | + fn as_mut_ptr(&mut self) -> *mut ffi::c_void { |
| 74 | + self.raw.as_mut_ptr() as *mut _ |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// An identifier for a mount that is returned by [`name_to_handle_at`]. |
| 79 | +/// |
| 80 | +/// [`name_to_handle_at`]: crate::fs::name_to_handle_at |
| 81 | +#[derive(Debug)] |
| 82 | +pub enum MountId { |
| 83 | + /// By default a MountId is a C int. |
| 84 | + Regular(ffi::c_int), |
| 85 | + /// When `AT_HANDLE_MNT_ID_UNIQUE` is passed in `HandleFlags`, MountId is a u64. |
| 86 | + Unique(u64), |
| 87 | +} |
| 88 | + |
| 89 | +/// `name_to_handle_at(dirfd, path, flags)` - Gets a filehandle given a path. |
| 90 | +/// |
| 91 | +/// # References |
| 92 | +/// - [Linux] |
| 93 | +/// |
| 94 | +/// [Linux]: https://man7.org/linux/man-pages/man2/open_by_handle_at.2.html |
| 95 | +pub fn name_to_handle_at<Fd: AsFd, P: path::Arg>( |
| 96 | + dirfd: Fd, |
| 97 | + path: P, |
| 98 | + flags: HandleFlags, |
| 99 | +) -> io::Result<(FileHandle, MountId)> { |
| 100 | + // name_to_handle_at(2) takes the mount_id parameter as either a 32-bit or 64-bit int pointer |
| 101 | + // depending on the flag AT_HANDLE_MNT_ID_UNIQUE |
| 102 | + let mount_id_unique: bool = flags.contains(HandleFlags::MNT_ID_UNIQUE); |
| 103 | + let mut mount_id_int: ffi::c_int = 0; |
| 104 | + let mut mount_id_64: u64 = 0; |
| 105 | + let mount_id_ptr: *mut ffi::c_void = if mount_id_unique { |
| 106 | + &mut mount_id_64 as *mut u64 as *mut _ |
| 107 | + } else { |
| 108 | + &mut mount_id_int as *mut ffi::c_int as *mut _ |
| 109 | + }; |
| 110 | + |
| 111 | + // The MAX_HANDLE_SZ constant is not a fixed upper bound, because the kernel is permitted to |
| 112 | + // increase it in the future. So, the loop is needed in the rare case that MAX_HANDLE_SZ was |
| 113 | + // insufficient. |
| 114 | + let mut handle_size: usize = MAX_HANDLE_SIZE; |
| 115 | + path.into_with_c_str(|path| loop { |
| 116 | + let mut file_handle = FileHandle::new(handle_size); |
| 117 | + |
| 118 | + let ret = backend::fs::syscalls::name_to_handle_at( |
| 119 | + dirfd.as_fd(), |
| 120 | + path, |
| 121 | + file_handle.as_mut_ptr(), |
| 122 | + mount_id_ptr, |
| 123 | + flags, |
| 124 | + ); |
| 125 | + |
| 126 | + // If EOVERFLOW was returned, and the handle size was increased, we need to try again with |
| 127 | + // a larger handle. If the handle size was not increased, EOVERFLOW was due to some other |
| 128 | + // cause, and should be returned to the user. |
| 129 | + if let Err(e) = ret { |
| 130 | + if e == io::Errno::OVERFLOW && file_handle.get_handle_bytes() > handle_size { |
| 131 | + handle_size = file_handle.get_handle_bytes(); |
| 132 | + continue; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + let mount_id = if mount_id_unique { |
| 137 | + MountId::Unique(mount_id_64) |
| 138 | + } else { |
| 139 | + MountId::Regular(mount_id_int) |
| 140 | + }; |
| 141 | + |
| 142 | + return ret.map(|_| (file_handle, mount_id)); |
| 143 | + }) |
| 144 | +} |
| 145 | + |
| 146 | +#[cfg(test)] |
| 147 | +mod tests { |
| 148 | + use super::*; |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn test_name_to_handle() { |
| 152 | + let (_, mount_id) = |
| 153 | + name_to_handle_at(crate::fs::CWD, "Cargo.toml", HandleFlags::empty()).unwrap(); |
| 154 | + assert!(matches!(mount_id, MountId::Regular(_))); |
| 155 | + |
| 156 | + match name_to_handle_at(crate::fs::CWD, "Cargo.toml", HandleFlags::MNT_ID_UNIQUE) { |
| 157 | + // On a new enough kernel, AT_HANDLE_MNT_ID_UNIQUE should succeed: |
| 158 | + Ok((_, mount_id)) => assert!(matches!(mount_id, MountId::Unique(_))), |
| 159 | + // But it should be rejected with -EINVAL on an older kernel: |
| 160 | + Err(e) => assert!(e == io::Errno::INVAL), |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments