Skip to content

Commit

Permalink
Fix IoCtx segfault (#85)
Browse files Browse the repository at this point in the history
* Fix IoCtx segfault

* fix example to be compatible with new interface
  • Loading branch information
Ten0 authored Sep 3, 2024
1 parent d46bba1 commit 6d9aa79
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
7 changes: 5 additions & 2 deletions examples/rados_striper.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#[cfg(feature = "rados_striper")]
use {ceph::ceph as ceph_helpers, ceph::error::RadosError, nix::errno::Errno, std::env, std::str};
use {
ceph::ceph as ceph_helpers, ceph::error::RadosError, nix::errno::Errno, std::env, std::str,
std::sync::Arc,
};

#[cfg(not(feature = "rados_striper"))]
fn main() {}
Expand All @@ -11,7 +14,7 @@ fn main() {
let pool_name = "ceph-rust-test";

println!("Connecting to ceph");
let cluster = ceph_helpers::connect_to_ceph(user_id, &config_file).unwrap();
let cluster = Arc::new(ceph_helpers::connect_to_ceph(user_id, &config_file).unwrap());

println!("Creating pool {}", pool_name);
match cluster.rados_create_pool(pool_name) {
Expand Down
16 changes: 12 additions & 4 deletions src/ceph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use crate::rados_striper::*;
use crate::status::*;
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::sync::Arc;
use std::{ptr, str};

use crate::utils::*;
Expand Down Expand Up @@ -333,6 +334,7 @@ impl Iterator for XAttr {
/// Owns a ioctx handle
pub struct IoCtx {
ioctx: rados_ioctx_t,
_rados: Arc<Rados>,
}

unsafe impl Send for IoCtx {}
Expand Down Expand Up @@ -480,7 +482,7 @@ impl Rados {
/// Create an io context. The io context allows you to perform operations
/// within a particular pool.
/// For more details see rados_ioctx_t.
pub fn get_rados_ioctx(&self, pool_name: &str) -> RadosResult<IoCtx> {
pub fn get_rados_ioctx(self: &Arc<Self>, pool_name: &str) -> RadosResult<IoCtx> {
self.conn_guard()?;
let pool_name_str = CString::new(pool_name)?;
unsafe {
Expand All @@ -489,22 +491,28 @@ impl Rados {
if ret_code < 0 {
return Err(ret_code.into());
}
Ok(IoCtx { ioctx })
Ok(IoCtx {
ioctx,
_rados: self.clone(),
})
}
}

/// Create an io context. The io context allows you to perform operations
/// within a particular pool.
/// For more details see rados_ioctx_t.
pub fn get_rados_ioctx2(&self, pool_id: i64) -> RadosResult<IoCtx> {
pub fn get_rados_ioctx2(self: &Arc<Self>, pool_id: i64) -> RadosResult<IoCtx> {
self.conn_guard()?;
unsafe {
let mut ioctx: rados_ioctx_t = ptr::null_mut();
let ret_code = rados_ioctx_create2(self.rados, pool_id, &mut ioctx);
if ret_code < 0 {
return Err(ret_code.into());
}
Ok(IoCtx { ioctx })
Ok(IoCtx {
ioctx,
_rados: self.clone(),
})
}
}
}
Expand Down

0 comments on commit 6d9aa79

Please sign in to comment.