Skip to content

Commit

Permalink
aya: Make MapData::pin pub
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Tucker <[email protected]>
  • Loading branch information
dave-tucker committed Sep 19, 2023
1 parent 3ffb1ed commit 83c334a
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions aya/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})?;
Expand Down Expand Up @@ -542,13 +543,35 @@ impl MapData {
})
}

pub(crate) fn pin<P: AsRef<Path>>(&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<dyn std::error::Error>>(())
/// ```
pub fn pin<P: AsRef<Path>>(&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,
}
})?;
Expand Down

0 comments on commit 83c334a

Please sign in to comment.