diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 5ff2a563d..979f8dd7b 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -469,7 +469,8 @@ impl MapData { } Err(_) => { let mut map = Self::create(obj, name, btf_fd)?; - map.pin(name, path).map_err(|error| MapError::PinError { + let path = path.join(name); + map.pin(&path).map_err(|error| MapError::PinError { name: Some(name.into()), error, })?; @@ -542,13 +543,35 @@ impl MapData { }) } - pub(crate) fn pin>(&mut self, name: &str, path: P) -> Result<(), PinError> { + /// Allows the map to be pinned to the provided path. + /// + /// Any directories in the the path provided should have been created by the caller. + /// The path must be on a BPF filesystem. + /// + /// # Errors + /// + /// Returns a [`SyscallError`] if the underlying syscall fails. This may also happen + /// if the path already exists, in which case the inner [`std::io::Error`] kind + /// will be [`std::io::ErrorKind::AlreadyExists`]. + /// + /// # Example + /// + /// ```no_run + /// # let mut bpf = aya::Bpf::load(&[])?; + /// # use aya::maps::MapData; + /// + /// let mut map = MapData::from_pin("/sys/fs/bpf/my_map")?; + /// map.pin("/sys/fs/bpf/my_map2")?; + /// + /// # Ok::<(), Box>(()) + /// ``` + pub fn pin>(&mut self, path: P) -> Result<(), PinError> { use std::os::unix::ffi::OsStrExt as _; + let path = path.as_ref(); let Self { fd, obj: _ } = self; - let path = path.as_ref().join(name); let path_string = CString::new(path.as_os_str().as_bytes()).map_err(|error| { PinError::InvalidPinPath { - path: path.clone(), + path: path.to_path_buf(), error, } })?;