diff --git a/examples/ceph.rs b/examples/ceph.rs index 1d922b8..3c6e7ca 100644 --- a/examples/ceph.rs +++ b/examples/ceph.rs @@ -17,13 +17,13 @@ extern crate ceph; extern crate libc; -use ceph::JsonData; #[cfg(target_os = "linux")] use ceph::admin_sockets::*; #[cfg(target_os = "linux")] use ceph::ceph as ceph_helpers; #[cfg(target_os = "linux")] use ceph::rados; +use ceph::JsonData; #[cfg(not(target_os = "linux"))] fn main() {} @@ -42,10 +42,10 @@ fn main() { match admin_socket_command("help", "/var/run/ceph/ceph-mon.ip-172-31-31-247.asok") { Ok(json) => { println!("{}", json); - }, + } Err(e) => { println!("{}", e); - }, + } } let rados_version = ceph_helpers::rados_libversion(); @@ -88,22 +88,24 @@ fn main() { Ok((outbuf, outs)) => { match outbuf { Some(output) => println!("Ceph mon command (outbuf):\n{}", output), - None => {}, + None => {} } match outs { Some(output) => println!("Ceph mon command (outs):\n{}", output), - None => {}, + None => {} } - }, + } Err(e) => { println!("{:?}", e); - }, + } } // This command encapsulates the lower level mon, osd, pg commands and returns JsonData // objects based on the key path - println!("{:?}", - cluster.ceph_command("prefix", "status", ceph_helpers::CephCommandTypes::Mon, &["health"])); + println!( + "{:?}", + cluster.ceph_command("prefix", "status", ceph_helpers::CephCommandTypes::Mon, &["health"]) + ); // Get a list of Ceph librados commands in JSON format. // It's very long so it's commented out. @@ -129,14 +131,16 @@ fn main() { Ok((outbuf, outs)) => { match outbuf { Some(output) => println!("Ceph mon command (outbuf):\n{}", output), - None => {}, + None => {} } match outs { Some(output) => println!("Ceph mon command (outs):\n{}", output), - None => {}, + None => {} } - }, - Err(e) => {println!("{:?}", e);}, + } + Err(e) => { + println!("{:?}", e); + } } // Print CephHealth of cluster @@ -146,7 +150,10 @@ fn main() { println!("{}", cluster.ceph_status(&["health", "overall_status"]).unwrap()); // This command encapsulates the lower level mon, osd, pg commands and returns JsonData objects based on the key path - println!("{:?}", cluster.ceph_command("prefix", "status", ceph_helpers::CephCommandTypes::Mon, &["health"])); + println!( + "{:?}", + cluster.ceph_command("prefix", "status", ceph_helpers::CephCommandTypes::Mon, &["health"]) + ); // Get a list of Ceph librados commands in JSON format. // It's very long so it's commented out. diff --git a/rustfmt.toml b/rustfmt.toml index 923dcb8..48d7723 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -16,3 +16,4 @@ wrap_comments = true report_todo = "Always" report_fixme = "Always" +use_try_shorthand = true diff --git a/src/admin_sockets.rs b/src/admin_sockets.rs index 09dbd2a..7986133 100644 --- a/src/admin_sockets.rs +++ b/src/admin_sockets.rs @@ -40,11 +40,11 @@ pub fn admin_socket_raw_command(cmd: &str, socket: &str) -> RadosResult let mut buffer = vec![0; 4]; // Should return 4 bytes with size or indicator. let cmd = &format!("{}\0", cmd); // Terminator so don't add one to commands. - let mut stream = try!(UnixStream::connect(socket)); - let wb = try!(stream.write(cmd.as_bytes())); - let ret_val = try!(stream.read(&mut buffer)); + let mut stream = UnixStream::connect(socket)?; + let wb = stream.write(cmd.as_bytes())?; + let ret_val = stream.read(&mut buffer)?; if ret_val < 4 { - try!(stream.shutdown(Shutdown::Both)); + stream.shutdown(Shutdown::Both)?; return Err(RadosError::new( "Admin socket: Invalid command or socket did not return any data".to_string(), )); diff --git a/src/ceph.rs b/src/ceph.rs index 926a22e..6e98371 100644 --- a/src/ceph.rs +++ b/src/ceph.rs @@ -50,7 +50,7 @@ pub enum CephHealth { Error, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub enum CephCommandTypes { Mon, Osd, @@ -68,38 +68,49 @@ pub(crate) fn get_error(n: c_int) -> RadosResult { named!( parse_header, do_parse!( - char!(CEPH_OSD_TMAP_HDR) >> data_len: le_u32 >> data: take!(data_len) >> (TmapOperation::Header { - data: data.to_vec(), - }) + char!(CEPH_OSD_TMAP_HDR) + >> data_len: le_u32 + >> data: take!(data_len) + >> (TmapOperation::Header { data: data.to_vec() }) ) ); named!( parse_create, do_parse!( - char!(CEPH_OSD_TMAP_CREATE) >> key_name_len: le_u32 >> key_name: take_str!(key_name_len) >> data_len: le_u32 - >> data: take!(data_len) >> (TmapOperation::Create { - name: key_name.to_string(), - data: data.to_vec(), - }) + char!(CEPH_OSD_TMAP_CREATE) + >> key_name_len: le_u32 + >> key_name: take_str!(key_name_len) + >> data_len: le_u32 + >> data: take!(data_len) + >> (TmapOperation::Create { + name: key_name.to_string(), + data: data.to_vec(), + }) ) ); named!( parse_set, do_parse!( - char!(CEPH_OSD_TMAP_SET) >> key_name_len: le_u32 >> key_name: take_str!(key_name_len) >> data_len: le_u32 - >> data: take!(data_len) >> (TmapOperation::Set { - key: key_name.to_string(), - data: data.to_vec(), - }) + char!(CEPH_OSD_TMAP_SET) + >> key_name_len: le_u32 + >> key_name: take_str!(key_name_len) + >> data_len: le_u32 + >> data: take!(data_len) + >> (TmapOperation::Set { + key: key_name.to_string(), + data: data.to_vec(), + }) ) ); named!( parse_remove, do_parse!( - char!(CEPH_OSD_TMAP_RM) >> key_name_len: le_u32 >> key_name: take_str!(key_name_len) + char!(CEPH_OSD_TMAP_RM) + >> key_name_len: le_u32 + >> key_name: take_str!(key_name_len) >> (TmapOperation::Remove { name: key_name.to_string(), }) @@ -120,26 +131,26 @@ impl TmapOperation { match *self { TmapOperation::Header { ref data } => { buffer.push(CEPH_OSD_TMAP_HDR as u8); - try!(buffer.write_u32::(data.len() as u32)); + buffer.write_u32::(data.len() as u32)?; buffer.extend_from_slice(data); } TmapOperation::Set { ref key, ref data } => { buffer.push(CEPH_OSD_TMAP_SET as u8); - try!(buffer.write_u32::(key.len() as u32)); + buffer.write_u32::(key.len() as u32)?; buffer.extend(key.as_bytes()); - try!(buffer.write_u32::(data.len() as u32)); + buffer.write_u32::(data.len() as u32)?; buffer.extend_from_slice(data); } TmapOperation::Create { ref name, ref data } => { buffer.push(CEPH_OSD_TMAP_CREATE as u8); - try!(buffer.write_u32::(name.len() as u32)); + buffer.write_u32::(name.len() as u32)?; buffer.extend(name.as_bytes()); - try!(buffer.write_u32::(data.len() as u32)); + buffer.write_u32::(data.len() as u32)?; buffer.extend_from_slice(data); } TmapOperation::Remove { ref name } => { buffer.push(CEPH_OSD_TMAP_RM as u8); - try!(buffer.write_u32::(name.len() as u32)); + buffer.write_u32::(name.len() as u32)?; buffer.extend(name.as_bytes()); } } @@ -194,11 +205,11 @@ impl Iterator for Pool { namespace.push_str(&CStr::from_ptr(nspace_ptr as *const ::libc::c_char).to_string_lossy()); } - return Some(CephObject { + Some(CephObject { name: object_name.to_string_lossy().into_owned(), entry_locator: object_locator, - namespace: namespace, - }); + namespace, + }) } } } @@ -271,7 +282,7 @@ impl XAttr { XAttr { name: String::new(), value: String::new(), - iter: iter, + iter, } } } @@ -281,30 +292,30 @@ impl Iterator for XAttr { fn next(&mut self) -> Option { // max xattr name is 255 bytes from what I can find - let mut name_buffer: Vec = Vec::with_capacity(255); - // max xattr is 64Kb from what I can find - let mut value_buffer: Vec = Vec::with_capacity(64 * 1024); + let mut name: *const c_char = ptr::null(); + let mut value: *const c_char = ptr::null(); let mut val_length: usize = 0; unsafe { - let ret_code = rados_getxattrs_next( - self.iter, - name_buffer.as_mut_ptr() as *mut *const c_char, - value_buffer.as_mut_ptr() as *mut *const c_char, - &mut val_length, - ); + let ret_code = rados_getxattrs_next(self.iter, &mut name, &mut value, &mut val_length); if ret_code < 0 { // Something failed, however Iterator doesn't return Result so we return None None } // end of iterator reached - else if val_length == 0 { + else if value.is_null() && val_length == 0 { rados_getxattrs_end(self.iter); None } else { + let name = CStr::from_ptr(name); + // value string + let s_bytes = std::slice::from_raw_parts(value, val_length); + // Convert from i8 -> u8 + let bytes: Vec = s_bytes.iter().map(|c| *c as u8).collect(); + Some(XAttr { - name: String::from_utf8_lossy(&name_buffer).into_owned(), - value: String::from_utf8_lossy(&value_buffer).into_owned(), + name: name.to_string_lossy().into_owned(), + value: String::from_utf8_lossy(&bytes).into_owned(), iter: self.iter, }) } @@ -333,7 +344,7 @@ pub struct Rados { phantom: PhantomData, } -unsafe impl Sync for Rados{} +unsafe impl Sync for Rados {} impl Drop for Rados { fn drop(&mut self) { @@ -346,22 +357,22 @@ impl Drop for Rados { } /// Connect to a Ceph cluster and return a connection handle rados_t -pub fn connect_to_ceph<'a>(user_id: &str, config_file: &str) -> RadosResult { - let connect_id = try!(CString::new(user_id)); - let conf_file = try!(CString::new(config_file)); +pub fn connect_to_ceph(user_id: &str, config_file: &str) -> RadosResult { + let connect_id = CString::new(user_id)?; + let conf_file = CString::new(config_file)?; unsafe { let mut cluster_handle: rados_t = ptr::null_mut(); let ret_code = rados_create(&mut cluster_handle, connect_id.as_ptr()); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } let ret_code = rados_conf_read_file(cluster_handle, conf_file.as_ptr()); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } let ret_code = rados_connect(cluster_handle); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } Ok(Rados { rados: cluster_handle, @@ -404,8 +415,8 @@ impl Rados { "Rados should not be connected when this function is called".into(), )); } - let name_str = try!(CString::new(name)); - let value_str = try!(CString::new(value)); + let name_str = CString::new(name)?; + let value_str = CString::new(value)?; unsafe { let ret_code = rados_conf_set(self.rados, name_str.as_ptr(), value_str.as_ptr()); if ret_code < 0 { @@ -417,7 +428,7 @@ impl Rados { /// Get the value of a configuration option pub fn config_get(&self, name: &str) -> RadosResult { - let name_str = try!(CString::new(name)); + let name_str = CString::new(name)?; // 5K should be plenty for a config key right? let mut buffer: Vec = Vec::with_capacity(5120); unsafe { @@ -444,14 +455,14 @@ impl Rados { /// For more details see rados_ioctx_t. pub fn get_rados_ioctx(&self, pool_name: &str) -> RadosResult { self.conn_guard()?; - let pool_name_str = try!(CString::new(pool_name)); + let pool_name_str = CString::new(pool_name)?; unsafe { let mut ioctx: rados_ioctx_t = ptr::null_mut(); let ret_code = rados_ioctx_create(self.rados, pool_name_str.as_ptr(), &mut ioctx); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } - Ok(IoCtx { ioctx: ioctx }) + Ok(IoCtx { ioctx }) } } @@ -464,9 +475,9 @@ impl Rados { 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(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } - Ok(IoCtx { ioctx: ioctx }) + Ok(IoCtx { ioctx }) } } } @@ -507,7 +518,7 @@ impl IoCtx { unsafe { let ret_code = rados_ioctx_pool_stat(self.ioctx, &mut pool_stat); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } Ok(pool_stat) } @@ -518,9 +529,9 @@ impl IoCtx { unsafe { let ret_code = rados_ioctx_pool_set_auid(self.ioctx, auid); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } - return Ok(()); + Ok(()) } } @@ -530,9 +541,9 @@ impl IoCtx { unsafe { let ret_code = rados_ioctx_pool_get_auid(self.ioctx, &mut auid); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } - return Ok(auid); + Ok(auid) } } @@ -542,12 +553,12 @@ impl IoCtx { unsafe { let ret_code = rados_ioctx_pool_requires_alignment(self.ioctx); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } if ret_code == 0 { - return Ok(false); + Ok(false) } else { - return Ok(true); + Ok(true) } } } @@ -557,7 +568,7 @@ impl IoCtx { self.ioctx_guard()?; unsafe { let ret_code = rados_ioctx_pool_required_alignment(self.ioctx); - return Ok(ret_code); + Ok(ret_code) } } @@ -592,14 +603,14 @@ impl IoCtx { buffer.capacity() as c_uint, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } - return Ok(String::from_utf8_lossy(&buffer).into_owned()); + Ok(String::from_utf8_lossy(&buffer).into_owned()) } else if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + Err(RadosError::new(get_error(ret_code as i32)?)) } else { buffer.set_len(ret_code as usize); - return Ok(String::from_utf8_lossy(&buffer).into_owned()); + Ok(String::from_utf8_lossy(&buffer).into_owned()) } } } @@ -607,7 +618,7 @@ impl IoCtx { /// Set the key for mapping objects to pgs within an io context. pub fn rados_locator_set_key(&self, key: &str) -> RadosResult<()> { self.ioctx_guard()?; - let key_str = try!(CString::new(key)); + let key_str = CString::new(key)?; unsafe { rados_ioctx_locator_set_key(self.ioctx, key_str.as_ptr()); } @@ -619,7 +630,7 @@ impl IoCtx { /// domains. The mapping of objects to pgs is also based on this value. pub fn rados_set_namespace(&self, namespace: &str) -> RadosResult<()> { self.ioctx_guard()?; - let namespace_str = try!(CString::new(namespace)); + let namespace_str = CString::new(namespace)?; unsafe { rados_ioctx_set_namespace(self.ioctx, namespace_str.as_ptr()); } @@ -633,7 +644,7 @@ impl IoCtx { unsafe { let ret_code = rados_nobjects_list_open(self.ioctx, &mut rados_list_ctx); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(rados_list_ctx) @@ -643,11 +654,11 @@ impl IoCtx { pub fn rados_snap_create(&self, snap_name: &str) -> RadosResult<()> { self.ioctx_guard()?; - let snap_name_str = try!(CString::new(snap_name)); + let snap_name_str = CString::new(snap_name)?; unsafe { let ret_code = rados_ioctx_snap_create(self.ioctx, snap_name_str.as_ptr()); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -656,12 +667,12 @@ impl IoCtx { /// Delete a pool snapshot pub fn rados_snap_remove(&self, snap_name: &str) -> RadosResult<()> { self.ioctx_guard()?; - let snap_name_str = try!(CString::new(snap_name)); + let snap_name_str = CString::new(snap_name)?; unsafe { let ret_code = rados_ioctx_snap_remove(self.ioctx, snap_name_str.as_ptr()); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -672,13 +683,13 @@ impl IoCtx { /// taken. pub fn rados_snap_rollback(&self, object_name: &str, snap_name: &str) -> RadosResult<()> { self.ioctx_guard()?; - let snap_name_str = try!(CString::new(snap_name)); - let object_name_str = try!(CString::new(object_name)); + let snap_name_str = CString::new(snap_name)?; + let object_name_str = CString::new(object_name)?; unsafe { let ret_code = rados_ioctx_snap_rollback(self.ioctx, object_name_str.as_ptr(), snap_name_str.as_ptr()); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -706,7 +717,7 @@ impl IoCtx { unsafe { let ret_code = rados_ioctx_selfmanaged_snap_create(self.ioctx, &mut snap_id); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(snap_id) @@ -721,7 +732,7 @@ impl IoCtx { unsafe { let ret_code = rados_ioctx_selfmanaged_snap_remove(self.ioctx, snap_id); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -732,12 +743,12 @@ impl IoCtx { /// taken. pub fn rados_selfmanaged_snap_rollback(&self, object_name: &str, snap_id: u64) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); + let object_name_str = CString::new(object_name)?; unsafe { let ret_code = rados_ioctx_selfmanaged_snap_rollback(self.ioctx, object_name_str.as_ptr(), snap_id); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -780,12 +791,12 @@ impl IoCtx { /// Get the id of a pool snapshot pub fn rados_snap_lookup(&self, snap_name: &str) -> RadosResult { self.ioctx_guard()?; - let snap_name_str = try!(CString::new(snap_name)); + let snap_name_str = CString::new(snap_name)?; let mut snap_id: u64 = 0; unsafe { let ret_code = rados_ioctx_snap_lookup(self.ioctx, snap_name_str.as_ptr(), &mut snap_id); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(snap_id) @@ -797,7 +808,7 @@ impl IoCtx { let out_buffer: Vec = Vec::with_capacity(500); let out_buff_size = out_buffer.capacity(); - let out_str = try!(CString::new(out_buffer)); + let out_str = CString::new(out_buffer)?; unsafe { let ret_code = rados_ioctx_snap_get_name( self.ioctx, @@ -807,7 +818,7 @@ impl IoCtx { ); if ret_code == -ERANGE {} if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(out_str.to_string_lossy().into_owned()) @@ -821,7 +832,7 @@ impl IoCtx { unsafe { let ret_code = rados_ioctx_snap_get_stamp(self.ioctx, snap_id, &mut time_id); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(time_id) @@ -842,7 +853,7 @@ impl IoCtx { /// The value of len must be <= UINT_MAX/2. pub fn rados_object_write(&self, object_name: &str, buffer: &[u8], offset: u64) -> RadosResult<()> { self.ioctx_guard()?; - let obj_name_str = try!(CString::new(object_name)); + let obj_name_str = CString::new(object_name)?; unsafe { let ret_code = rados_write( @@ -853,7 +864,7 @@ impl IoCtx { offset, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -864,7 +875,7 @@ impl IoCtx { /// truncated and then written. pub fn rados_object_write_full(&self, object_name: &str, buffer: &[u8]) -> RadosResult<()> { self.ioctx_guard()?; - let obj_name_str = try!(CString::new(object_name)); + let obj_name_str = CString::new(object_name)?; unsafe { let ret_code = rados_write_full( @@ -874,7 +885,7 @@ impl IoCtx { buffer.len(), ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -895,8 +906,8 @@ impl IoCtx { length: usize, ) -> RadosResult<()> { self.ioctx_guard()?; - let dst_name_str = try!(CString::new(dst_object_name)); - let src_name_str = try!(CString::new(src_object_name)); + let dst_name_str = CString::new(dst_object_name)?; + let src_name_str = CString::new(src_object_name)?; unsafe { let ret_code = rados_clone_range( @@ -908,7 +919,7 @@ impl IoCtx { length, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -917,7 +928,7 @@ impl IoCtx { /// Append len bytes from buf into the oid object. pub fn rados_object_append(&self, object_name: &str, buffer: &[u8]) -> RadosResult<()> { self.ioctx_guard()?; - let obj_name_str = try!(CString::new(object_name)); + let obj_name_str = CString::new(object_name)?; unsafe { let ret_code = rados_append( @@ -927,7 +938,7 @@ impl IoCtx { buffer.len(), ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -946,7 +957,7 @@ impl IoCtx { read_offset: u64, ) -> RadosResult { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); + let object_name_str = CString::new(object_name)?; let mut len = fill_buffer.capacity(); if len == 0 { fill_buffer.reserve_exact(1024 * 64); @@ -962,7 +973,7 @@ impl IoCtx { read_offset, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } fill_buffer.set_len(ret_code as usize); Ok(ret_code) @@ -973,12 +984,12 @@ impl IoCtx { /// Note: This does not delete any snapshots of the object. pub fn rados_object_remove(&self, object_name: &str) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); + let object_name_str = CString::new(object_name)?; unsafe { let ret_code = rados_remove(self.ioctx, object_name_str.as_ptr() as *const c_char); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -989,12 +1000,12 @@ impl IoCtx { /// zeroes. If this shrinks the object, the excess data is removed. pub fn rados_object_trunc(&self, object_name: &str, new_size: u64) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); + let object_name_str = CString::new(object_name)?; unsafe { let ret_code = rados_trunc(self.ioctx, object_name_str.as_ptr(), new_size); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1008,8 +1019,8 @@ impl IoCtx { fill_buffer: &mut [u8], ) -> RadosResult { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); - let attr_name_str = try!(CString::new(attr_name)); + let object_name_str = CString::new(object_name)?; + let attr_name_str = CString::new(attr_name)?; unsafe { let ret_code = rados_getxattr( @@ -1020,7 +1031,7 @@ impl IoCtx { fill_buffer.len(), ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } Ok(ret_code) } @@ -1029,8 +1040,8 @@ impl IoCtx { /// Set an extended attribute on an object. pub fn rados_object_setxattr(&self, object_name: &str, attr_name: &str, attr_value: &mut [u8]) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); - let attr_name_str = try!(CString::new(attr_name)); + let object_name_str = CString::new(object_name)?; + let attr_name_str = CString::new(attr_name)?; unsafe { let ret_code = rados_setxattr( @@ -1041,7 +1052,7 @@ impl IoCtx { attr_value.len(), ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1050,8 +1061,8 @@ impl IoCtx { /// Delete an extended attribute from an object. pub fn rados_object_rmxattr(&self, object_name: &str, attr_name: &str) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); - let attr_name_str = try!(CString::new(attr_name)); + let object_name_str = CString::new(object_name)?; + let attr_name_str = CString::new(attr_name)?; unsafe { let ret_code = rados_rmxattr( @@ -1060,7 +1071,7 @@ impl IoCtx { attr_name_str.as_ptr() as *const c_char, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1070,13 +1081,13 @@ impl IoCtx { /// object Used in conjuction with XAttr::new() to iterate. pub fn rados_get_xattr_iterator(&self, object_name: &str) -> RadosResult { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); + let object_name_str = CString::new(object_name)?; let mut xattr_iterator_handle: rados_xattrs_iter_t = ptr::null_mut(); unsafe { let ret_code = rados_getxattrs(self.ioctx, object_name_str.as_ptr(), &mut xattr_iterator_handle); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(xattr_iterator_handle) @@ -1085,24 +1096,24 @@ impl IoCtx { /// Get object stats (size,SystemTime) pub fn rados_object_stat(&self, object_name: &str) -> RadosResult<(u64, SystemTime)> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); + let object_name_str = CString::new(object_name)?; let mut psize: u64 = 0; let mut time: ::libc::time_t = 0; unsafe { let ret_code = rados_stat(self.ioctx, object_name_str.as_ptr(), &mut psize, &mut time); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok((psize, (UNIX_EPOCH + Duration::from_secs(time as u64)))) } /// Update tmap (trivial map) - pub fn rados_object_tmap_update(&self, object_name: &str, update: TmapOperation) -> RadosResult<()> { + pub fn rados_object_tmap_update(&self, object_name: &str, update: &TmapOperation) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); - let buffer = try!(update.serialize()); + let object_name_str = CString::new(object_name)?; + let buffer = update.serialize()?; unsafe { let ret_code = rados_tmap_update( self.ioctx, @@ -1111,7 +1122,7 @@ impl IoCtx { buffer.len(), ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1120,7 +1131,7 @@ impl IoCtx { /// Fetch complete tmap (trivial map) object pub fn rados_object_tmap_get(&self, object_name: &str) -> RadosResult> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); + let object_name_str = CString::new(object_name)?; let mut buffer: Vec = Vec::with_capacity(500); unsafe { @@ -1140,10 +1151,10 @@ impl IoCtx { buffer.capacity(), ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } else if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } match TmapOperation::deserialize(&buffer) { @@ -1174,9 +1185,9 @@ impl IoCtx { output_buffer: &mut [u8], ) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); - let class_name_str = try!(CString::new(class_name)); - let method_name_str = try!(CString::new(method_name)); + let object_name_str = CString::new(object_name)?; + let class_name_str = CString::new(class_name)?; + let method_name_str = CString::new(method_name)?; unsafe { let ret_code = rados_exec( @@ -1190,7 +1201,7 @@ impl IoCtx { output_buffer.len(), ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1201,7 +1212,7 @@ impl IoCtx { /// to the notify, or a timeout is reached. pub fn rados_object_notify(&self, object_name: &str, data: &[u8]) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); + let object_name_str = CString::new(object_name)?; unsafe { let ret_code = rados_notify( @@ -1212,7 +1223,7 @@ impl IoCtx { data.len() as i32, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1237,7 +1248,7 @@ impl IoCtx { buffer: Option<&[u8]>, ) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); + let object_name_str = CString::new(object_name)?; match buffer { Some(buf) => unsafe { @@ -1250,14 +1261,14 @@ impl IoCtx { buf.len() as i32, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } }, None => unsafe { let ret_code = rados_notify_ack(self.ioctx, object_name_str.as_ptr(), notify_id, cookie, ptr::null(), 0); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } }, } @@ -1275,7 +1286,7 @@ impl IoCtx { expected_write_size: u64, ) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); + let object_name_str = CString::new(object_name)?; unsafe { let ret_code = rados_set_alloc_hint( @@ -1285,16 +1296,16 @@ impl IoCtx { expected_write_size, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) } // Perform a compound read operation synchronously - pub fn rados_perform_read_operations(&self, read_op: ReadOperation) -> RadosResult<()> { + pub fn rados_perform_read_operations(&self, read_op: &ReadOperation) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(read_op.object_name.clone())); + let object_name_str = CString::new(read_op.object_name.clone())?; unsafe { let ret_code = rados_read_op_operate( @@ -1304,7 +1315,7 @@ impl IoCtx { read_op.flags as i32, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1313,7 +1324,7 @@ impl IoCtx { // Perform a compound write operation synchronously pub fn rados_commit_write_operations(&self, write_op: &mut WriteOperation) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(write_op.object_name.clone())); + let object_name_str = CString::new(write_op.object_name.clone())?; unsafe { let ret_code = rados_write_op_operate( @@ -1324,7 +1335,7 @@ impl IoCtx { write_op.flags as i32, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1341,10 +1352,10 @@ impl IoCtx { lock_flags: u8, ) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); - let lock_name_str = try!(CString::new(lock_name)); - let cookie_name_str = try!(CString::new(cookie_name)); - let description_str = try!(CString::new(description)); + let object_name_str = CString::new(object_name)?; + let lock_name_str = CString::new(lock_name)?; + let cookie_name_str = CString::new(cookie_name)?; + let description_str = CString::new(description)?; unsafe { let ret_code = rados_lock_exclusive( @@ -1357,7 +1368,7 @@ impl IoCtx { lock_flags, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1375,11 +1386,11 @@ impl IoCtx { lock_flags: u8, ) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); - let lock_name_str = try!(CString::new(lock_name)); - let cookie_name_str = try!(CString::new(cookie_name)); - let description_str = try!(CString::new(description)); - let tag_name_str = try!(CString::new(tag_name)); + let object_name_str = CString::new(object_name)?; + let lock_name_str = CString::new(lock_name)?; + let cookie_name_str = CString::new(cookie_name)?; + let description_str = CString::new(description)?; + let tag_name_str = CString::new(tag_name)?; unsafe { let ret_code = rados_lock_shared( @@ -1393,7 +1404,7 @@ impl IoCtx { lock_flags, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1402,9 +1413,9 @@ impl IoCtx { /// Release a shared or exclusive lock on an object. pub fn rados_object_unlock(&self, object_name: &str, lock_name: &str, cookie_name: &str) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); - let lock_name_str = try!(CString::new(lock_name)); - let cookie_name_str = try!(CString::new(cookie_name)); + let object_name_str = CString::new(object_name)?; + let lock_name_str = CString::new(lock_name)?; + let cookie_name_str = CString::new(cookie_name)?; unsafe { let ret_code = rados_unlock( @@ -1414,7 +1425,7 @@ impl IoCtx { cookie_name_str.as_ptr(), ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1460,10 +1471,10 @@ impl IoCtx { cookie_name: &str, ) -> RadosResult<()> { self.ioctx_guard()?; - let object_name_str = try!(CString::new(object_name)); - let lock_name_str = try!(CString::new(lock_name)); - let cookie_name_str = try!(CString::new(cookie_name)); - let client_name_str = try!(CString::new(client_name)); + let object_name_str = CString::new(object_name)?; + let lock_name_str = CString::new(lock_name)?; + let cookie_name_str = CString::new(cookie_name)?; + let client_name_str = CString::new(client_name)?; unsafe { let ret_code = rados_break_lock( @@ -1474,7 +1485,7 @@ impl IoCtx { cookie_name_str.as_ptr(), ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1484,12 +1495,12 @@ impl IoCtx { impl Rados { pub fn rados_blacklist_client(&self, client: IpAddr, expire_seconds: u32) -> RadosResult<()> { self.conn_guard()?; - let client_address = try!(CString::new(client.to_string())); + let client_address = CString::new(client.to_string())?; unsafe { let ret_code = rados_blacklist_add(self.rados, client_address.as_ptr() as *mut c_char, expire_seconds); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } } Ok(()) @@ -1537,12 +1548,10 @@ impl Rados { let mut cursor = Cursor::new(&pool_buffer); loop { let mut string_buf: Vec = Vec::new(); - let read = try!(cursor.read_until(0x00, &mut string_buf)); - if read == 0 { - // End of the pool_buffer; - break; - } else if read == 1 { - // Read a double \0. Time to break + let read = cursor.read_until(0x00, &mut string_buf)?; + if read == 0 || read == 1 { + // 0 == End of the pool_buffer; + // 1 == Read a double \0. Time to break break; } else { // Read a String @@ -1558,14 +1567,14 @@ impl Rados { /// rule 0. pub fn rados_create_pool(&self, pool_name: &str) -> RadosResult<()> { self.conn_guard()?; - let pool_name_str = try!(CString::new(pool_name)); + let pool_name_str = CString::new(pool_name)?; unsafe { let ret_code = rados_pool_create(self.rados, pool_name_str.as_ptr()); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } } - return Ok(()); + Ok(()) } /// Delete a pool and all data inside it /// The pool is removed from the cluster immediately, but the actual data is @@ -1573,29 +1582,29 @@ impl Rados { /// the background. pub fn rados_delete_pool(&self, pool_name: &str) -> RadosResult<()> { self.conn_guard()?; - let pool_name_str = try!(CString::new(pool_name)); + let pool_name_str = CString::new(pool_name)?; unsafe { let ret_code = rados_pool_delete(self.rados, pool_name_str.as_ptr()); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } } - return Ok(()); + Ok(()) } /// Lookup a Ceph pool id. If the pool doesn't exist it will return /// Ok(None). pub fn rados_lookup_pool(&self, pool_name: &str) -> RadosResult> { self.conn_guard()?; - let pool_name_str = try!(CString::new(pool_name)); + let pool_name_str = CString::new(pool_name)?; unsafe { let ret_code: i64 = rados_pool_lookup(self.rados, pool_name_str.as_ptr()); if ret_code >= 0 { - return Ok(Some(ret_code)); + Ok(Some(ret_code)) } else if ret_code as i32 == -ENOENT { - return Ok(None); + Ok(None) } else { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + Err(RadosError::new(get_error(ret_code as i32)?)) } } } @@ -1622,13 +1631,13 @@ impl Rados { buffer.capacity(), ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + return Err(RadosError::new(get_error(ret_code as i32)?)); } - return Ok(String::from_utf8_lossy(&buffer).into_owned()); + Ok(String::from_utf8_lossy(&buffer).into_owned()) } else if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code as i32)))); + Err(RadosError::new(get_error(ret_code as i32)?)) } else { - return Ok(String::from_utf8_lossy(&buffer).into_owned()); + Ok(String::from_utf8_lossy(&buffer).into_owned()) } } } @@ -1642,11 +1651,7 @@ pub fn rados_libversion() -> RadosVersion { unsafe { rados_version(&mut major, &mut minor, &mut extra); } - return RadosVersion { - major: major, - minor: minor, - extra: extra, - }; + RadosVersion { major, minor, extra } } impl Rados { @@ -1662,11 +1667,11 @@ impl Rados { unsafe { let ret_code = rados_cluster_stat(self.rados, &mut cluster_stat); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } } - return Ok(cluster_stat); + Ok(cluster_stat) } pub fn rados_fsid(&self) -> RadosResult { @@ -1679,7 +1684,7 @@ impl Rados { fsid_buffer.capacity(), ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } // Tell the Vec how much Ceph read into the buffer fsid_buffer.set_len(ret_code as usize); @@ -1697,22 +1702,26 @@ impl Rados { pub fn ping_monitor(&self, mon_id: &str) -> RadosResult { self.conn_guard()?; - let mon_id_str = try!(CString::new(mon_id)); - let out_buffer: Vec = Vec::with_capacity(500); - let out_buff_size = out_buffer.capacity(); - let out_str = try!(CString::new(out_buffer)); + let mon_id_str = CString::new(mon_id)?; + let mut str_length: usize = 0; + let mut out_str: *mut c_char = ptr::null_mut(); unsafe { - let ret_code = rados_ping_monitor( - self.rados, - mon_id_str.as_ptr(), - out_str.as_ptr() as *mut *mut c_char, - out_buff_size as *mut usize, - ); + let ret_code = rados_ping_monitor(self.rados, mon_id_str.as_ptr(), &mut out_str, &mut str_length); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); + } + if !out_str.is_null() { + // valid string + let s_bytes = std::slice::from_raw_parts(out_str, str_length); + // Convert from i8 -> u8 + let bytes: Vec = s_bytes.iter().map(|c| *c as u8).collect(); + // Tell rados we're done with this buffer + rados_buffer_free(out_str); + Ok(String::from_utf8_lossy(&bytes).into_owned()) + } else { + Ok("".into()) } } - Ok(out_str.to_string_lossy().into_owned()) } } @@ -1725,7 +1734,7 @@ pub fn ceph_version(socket: &str) -> Option { let cmd = "version"; admin_socket_command(&cmd, socket).ok().and_then(|json| { - json_data(&json).and_then(|jsondata| json_find(jsondata, &[cmd]).and_then(|data| Some(json_as_string(&data)))) + json_data(&json).and_then(|jsondata| json_find(&jsondata, &[cmd]).and_then(|data| Some(json_as_string(&data)))) }) } @@ -1754,7 +1763,7 @@ impl Rados { Ok((json, _)) => match json { Some(json) => match json_data(&json) { Some(jsondata) => { - let data = json_find(jsondata, keys); + let data = json_find(&jsondata, keys); if data.is_some() { Ok(json_as_string(&data.unwrap())) } else { @@ -1816,7 +1825,7 @@ impl Rados { Ok((json, _)) => match json { Some(json) => match json_data(&json) { Some(jsondata) => { - let data = json_find(jsondata, keys); + let data = json_find(&jsondata, keys); if data.is_some() { Ok(data.unwrap()) } else { @@ -1842,7 +1851,7 @@ impl Rados { Some(json) => match json_data(&json) { Some(jsondata) => { if keys.is_some() { - let data = json_find(jsondata, keys.unwrap()); + let data = json_find(&jsondata, keys.unwrap()); if data.is_some() { Ok(data.unwrap()) } else { @@ -1870,13 +1879,10 @@ impl Rados { format: Option<&str>, ) -> RadosResult<(Option, Option)> { let data: Vec<*mut c_char> = Vec::with_capacity(1); - self.ceph_mon_command_with_data(name, value, format, data) + self.ceph_mon_command_with_data(name, value, format, &data) } - pub fn ceph_mon_command_without_data( - &self, - cmd: &serde_json::Value, - ) -> RadosResult> { + pub fn ceph_mon_command_without_data(&self, cmd: &serde_json::Value) -> RadosResult> { self.conn_guard()?; let cmd_string = cmd.to_string(); debug!("ceph_mon_command_without_data: {}", cmd_string); @@ -1937,7 +1943,7 @@ impl Rados { name: &str, value: &str, format: Option<&str>, - data: Vec<*mut c_char>, + data: &[*mut c_char], ) -> RadosResult<(Option, Option)> { self.conn_guard()?; @@ -1980,7 +1986,7 @@ impl Rados { &mut outs_len, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } // Copy the data from outbuf and then call rados_buffer_free instead libc::free @@ -2015,7 +2021,7 @@ impl Rados { format: Option<&str>, ) -> RadosResult<(Option, Option)> { let data: Vec<*mut c_char> = Vec::with_capacity(1); - self.ceph_osd_command_with_data(id, name, value, format, data) + self.ceph_osd_command_with_data(id, name, value, format, &data) } /// OSD command that does pass in a data payload. @@ -2025,7 +2031,7 @@ impl Rados { name: &str, value: &str, format: Option<&str>, - data: Vec<*mut c_char>, + data: &[*mut c_char], ) -> RadosResult<(Option, Option)> { self.conn_guard()?; @@ -2067,7 +2073,7 @@ impl Rados { &mut outs_len, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } // Copy the data from outbuf and then call rados_buffer_free instead libc::free @@ -2102,7 +2108,7 @@ impl Rados { format: Option<&str>, ) -> RadosResult<(Option, Option)> { let data: Vec<*mut c_char> = Vec::with_capacity(1); - self.ceph_pgs_command_with_data(pg, name, value, format, data) + self.ceph_pgs_command_with_data(pg, name, value, format, &data) } /// PG command that does pass in a data payload. @@ -2112,7 +2118,7 @@ impl Rados { name: &str, value: &str, format: Option<&str>, - data: Vec<*mut c_char>, + data: &[*mut c_char], ) -> RadosResult<(Option, Option)> { self.conn_guard()?; @@ -2155,7 +2161,7 @@ impl Rados { &mut outs_len, ); if ret_code < 0 { - return Err(RadosError::new(try!(get_error(ret_code)))); + return Err(RadosError::new(get_error(ret_code)?)); } // Copy the data from outbuf and then call rados_buffer_free instead libc::free diff --git a/src/ceph_client.rs b/src/ceph_client.rs index a4e5d0e..d3c4bd3 100644 --- a/src/ceph_client.rs +++ b/src/ceph_client.rs @@ -45,17 +45,17 @@ impl CephClient { pub fn new, T2: AsRef>(user_id: T1, config_file: T2) -> Result { let rados_t = match connect_to_ceph(&user_id.as_ref(), &config_file.as_ref()) { Ok(rados_t) => rados_t, - Err(e) => return Err(e.into()), + Err(e) => return Err(e), }; let version: CephVersion = match cmd::version(&rados_t)?.parse() { Ok(v) => v, - Err(e) => return Err(e.into()), + Err(e) => return Err(e), }; Ok(CephClient { - rados_t: rados_t, + rados_t, simulate: false, - version: version, + version, }) } @@ -69,7 +69,7 @@ impl CephClient { let cmd = MonCommand::new().with_prefix("osd out").with("ids", &osd_id); if !self.simulate { - self.run_command(cmd)?; + self.run_command(&cmd)?; } Ok(()) } @@ -78,18 +78,18 @@ impl CephClient { let osd_id = format!("osd.{}", osd_id); let cmd = MonCommand::new().with_prefix("osd crush remove").with_name(&osd_id); if !self.simulate { - self.run_command(cmd)?; + self.run_command(&cmd)?; } Ok(()) } /// Query a ceph pool. - pub fn osd_pool_get(&self, pool: &str, choice: &PoolOption) -> Result { + pub fn osd_pool_get(&self, pool: &str, choice: PoolOption) -> Result { let cmd = MonCommand::new() .with_prefix("osd pool get") .with("pool", pool) .with("var", choice.as_ref()); - if let Ok(result) = self.run_command(cmd) { + if let Ok(result) = self.run_command(&cmd) { let mut l = result.lines(); match l.next() { Some(res) => return Ok(res.into()), @@ -112,7 +112,7 @@ impl CephClient { .with("var", key) .with("value", value); if !self.simulate { - self.run_command(cmd)?; + self.run_command(&cmd)?; } Ok(()) } @@ -141,7 +141,7 @@ impl CephClient { c }; if !self.simulate { - self.run_command(cmd)?; + self.run_command(&cmd)?; } Ok(()) } @@ -161,20 +161,20 @@ impl CephClient { /// # } /// ``` pub fn osd_unset(&self, key: OsdOption) -> Result<(), RadosError> { - cmd::osd_unset(&self.rados_t, &key, self.simulate).map_err(|a| a.into()) + cmd::osd_unset(&self.rados_t, key, self.simulate).map_err(|a| a) } pub fn osd_tree(&self) -> Result { - cmd::osd_tree(&self.rados_t).map_err(|a| a.into()) + cmd::osd_tree(&self.rados_t).map_err(|a| a) } /// Get cluster status pub fn status(&self) -> Result { let cmd = MonCommand::new().with_prefix("status").with_format("json"); - let return_data = self.run_command(cmd)?; + let return_data = self.run_command(&cmd)?; let mut l = return_data.lines(); match l.next() { - Some(res) => return Ok(res.into()), + Some(res) => Ok(res.into()), None => Err(RadosError::Error("No response from ceph for status".into())), } } @@ -199,11 +199,13 @@ impl CephClient { } pub fn auth_del(&self, osd_id: u64) -> Result<(), RadosError> { - Ok(cmd::auth_del(&self.rados_t, osd_id, self.simulate)?) + cmd::auth_del(&self.rados_t, osd_id, self.simulate)?; + Ok(()) } pub fn osd_rm(&self, osd_id: u64) -> Result<(), RadosError> { - Ok(cmd::osd_rm(&self.rados_t, osd_id, self.simulate)?) + cmd::osd_rm(&self.rados_t, osd_id, self.simulate)?; + Ok(()) } pub fn osd_create(&self, id: Option) -> Result { @@ -212,12 +214,14 @@ impl CephClient { // Add a new mgr to the cluster pub fn mgr_auth_add(&self, mgr_id: &str) -> Result<(), RadosError> { - Ok(cmd::mgr_auth_add(&self.rados_t, mgr_id, self.simulate)?) + cmd::mgr_auth_add(&self.rados_t, mgr_id, self.simulate)?; + Ok(()) } // Add a new osd to the cluster pub fn osd_auth_add(&self, osd_id: u64) -> Result<(), RadosError> { - Ok(cmd::osd_auth_add(&self.rados_t, osd_id, self.simulate)?) + cmd::osd_auth_add(&self.rados_t, osd_id, self.simulate)?; + Ok(()) } /// Get a ceph-x key. The id parameter can be either a number or a string @@ -229,7 +233,8 @@ impl CephClient { // ceph osd crush add {id-or-name} {weight} [{bucket-type}={bucket-name} ...] /// add or update crushmap position and weight for an osd pub fn osd_crush_add(&self, osd_id: u64, weight: f64, host: &str) -> Result<(), RadosError> { - Ok(cmd::osd_crush_add(&self.rados_t, osd_id, weight, host, self.simulate)?) + cmd::osd_crush_add(&self.rados_t, osd_id, weight, host, self.simulate)?; + Ok(()) } // Luminous + only @@ -241,7 +246,8 @@ impl CephClient { pub fn mgr_fail(&self, mgr_id: &str) -> Result<(), RadosError> { min_version!(Luminous, self); - Ok(cmd::mgr_fail(&self.rados_t, mgr_id, self.simulate)?) + cmd::mgr_fail(&self.rados_t, mgr_id, self.simulate)?; + Ok(()) } pub fn mgr_list_modules(&self) -> Result, RadosError> { @@ -256,12 +262,14 @@ impl CephClient { pub fn mgr_enable_module(&self, module: &str, force: bool) -> Result<(), RadosError> { min_version!(Luminous, self); - Ok(cmd::mgr_enable_module(&self.rados_t, module, force, self.simulate)?) + cmd::mgr_enable_module(&self.rados_t, module, force, self.simulate)?; + Ok(()) } pub fn mgr_disable_module(&self, module: &str) -> Result<(), RadosError> { min_version!(Luminous, self); - Ok(cmd::mgr_disable_module(&self.rados_t, module, self.simulate)?) + cmd::mgr_disable_module(&self.rados_t, module, self.simulate)?; + Ok(()) } pub fn mgr_metadata(&self) -> Result { @@ -279,7 +287,7 @@ impl CephClient { Ok(cmd::mgr_versions(&self.rados_t)?) } - pub fn run_command(&self, command: MonCommand) -> Result { + pub fn run_command(&self, command: &MonCommand) -> Result { let cmd = command.as_json(); let data: Vec<*mut c_char> = Vec::with_capacity(1); diff --git a/src/ceph_version.rs b/src/ceph_version.rs index ef8d30a..0c52729 100644 --- a/src/ceph_version.rs +++ b/src/ceph_version.rs @@ -46,9 +46,9 @@ impl FromStr for CephVersion { /// `ceph version 10.2.9 (2ee413f77150c0f375ff6f10edd6c8f9c7d060d0)` fn from_str(s: &str) -> Result { use CephVersion::*; - let mut parts = s.split(" "); + let mut parts = s.split_whitespace(); if let (Some(_ceph), Some(_version), Some(version_str)) = (parts.next(), parts.next(), parts.next()) { - let mut version_parts = version_str.split("."); + let mut version_parts = version_str.split('.'); if let (Some(major), Some(minor), Some(_patch)) = (version_parts.next(), version_parts.next(), version_parts.next()) { diff --git a/src/cmd.rs b/src/cmd.rs index 42c5800..f9754e1 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -184,7 +184,7 @@ pub struct StoreStats { pub last_updated: String, } -#[derive(Deserialize, Debug)] +#[derive(Copy, Clone, Deserialize, Debug)] pub enum RoundStatus { #[serde(rename = "finished")] Finished, @@ -192,7 +192,7 @@ pub enum RoundStatus { OnGoing, } -#[derive(Deserialize, Debug)] +#[derive(Copy, Clone, Deserialize, Debug)] pub enum MonState { #[serde(rename = "probing")] Probing, @@ -208,7 +208,7 @@ pub enum MonState { Shutdown, } -#[derive(Deserialize, Debug, Serialize)] +#[derive(Clone, Copy, Deserialize, Debug, Serialize)] pub enum OsdOption { #[serde(rename = "full")] Full, @@ -246,51 +246,51 @@ pub enum OsdOption { impl fmt::Display for OsdOption { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - &OsdOption::Full => write!(f, "full"), - &OsdOption::Pause => write!(f, "pause"), - &OsdOption::NoUp => write!(f, "noup"), - &OsdOption::NoDown => write!(f, "nodown"), - &OsdOption::NoOut => write!(f, "noout"), - &OsdOption::NoIn => write!(f, "noin"), - &OsdOption::NoBackfill => write!(f, "nobackfill"), - &OsdOption::NoRebalance => write!(f, "norebalance"), - &OsdOption::NoRecover => write!(f, "norecover"), - &OsdOption::NoScrub => write!(f, "noscrub"), - &OsdOption::NoDeepScrub => write!(f, "nodeep-scrub"), - &OsdOption::NoTierAgent => write!(f, "notieragent"), - &OsdOption::SortBitwise => write!(f, "sortbitwise"), - &OsdOption::RecoveryDeletes => write!(f, "recovery_deletes"), - &OsdOption::RequireJewelOsds => write!(f, "require_jewel_osds"), - &OsdOption::RequireKrakenOsds => write!(f, "require_kraken_osds"), + match *self { + OsdOption::Full => write!(f, "full"), + OsdOption::Pause => write!(f, "pause"), + OsdOption::NoUp => write!(f, "noup"), + OsdOption::NoDown => write!(f, "nodown"), + OsdOption::NoOut => write!(f, "noout"), + OsdOption::NoIn => write!(f, "noin"), + OsdOption::NoBackfill => write!(f, "nobackfill"), + OsdOption::NoRebalance => write!(f, "norebalance"), + OsdOption::NoRecover => write!(f, "norecover"), + OsdOption::NoScrub => write!(f, "noscrub"), + OsdOption::NoDeepScrub => write!(f, "nodeep-scrub"), + OsdOption::NoTierAgent => write!(f, "notieragent"), + OsdOption::SortBitwise => write!(f, "sortbitwise"), + OsdOption::RecoveryDeletes => write!(f, "recovery_deletes"), + OsdOption::RequireJewelOsds => write!(f, "require_jewel_osds"), + OsdOption::RequireKrakenOsds => write!(f, "require_kraken_osds"), } } } impl AsRef for OsdOption { fn as_ref(&self) -> &str { - match self { - &OsdOption::Full => "full", - &OsdOption::Pause => "pause", - &OsdOption::NoUp => "noup", - &OsdOption::NoDown => "nodown", - &OsdOption::NoOut => "noout", - &OsdOption::NoIn => "noin", - &OsdOption::NoBackfill => "nobackfill", - &OsdOption::NoRebalance => "norebalance", - &OsdOption::NoRecover => "norecover", - &OsdOption::NoScrub => "noscrub", - &OsdOption::NoDeepScrub => "nodeep-scrub", - &OsdOption::NoTierAgent => "notieragent", - &OsdOption::SortBitwise => "sortbitwise", - &OsdOption::RecoveryDeletes => "recovery_deletes", - &OsdOption::RequireJewelOsds => "require_jewel_osds", - &OsdOption::RequireKrakenOsds => "require_kraken_osds", + match *self { + OsdOption::Full => "full", + OsdOption::Pause => "pause", + OsdOption::NoUp => "noup", + OsdOption::NoDown => "nodown", + OsdOption::NoOut => "noout", + OsdOption::NoIn => "noin", + OsdOption::NoBackfill => "nobackfill", + OsdOption::NoRebalance => "norebalance", + OsdOption::NoRecover => "norecover", + OsdOption::NoScrub => "noscrub", + OsdOption::NoDeepScrub => "nodeep-scrub", + OsdOption::NoTierAgent => "notieragent", + OsdOption::SortBitwise => "sortbitwise", + OsdOption::RecoveryDeletes => "recovery_deletes", + OsdOption::RequireJewelOsds => "require_jewel_osds", + OsdOption::RequireKrakenOsds => "require_kraken_osds", } } } -#[derive(Deserialize, Debug, Serialize)] +#[derive(Copy, Clone, Deserialize, Debug, Serialize)] pub enum PoolOption { #[serde(rename = "size")] Size, @@ -388,170 +388,170 @@ pub enum PoolOption { impl fmt::Display for PoolOption { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - &PoolOption::Size => write!(f, "size"), - &PoolOption::MinSize => write!(f, "min_size"), - &PoolOption::CrashReplayInterval => write!(f, "crash_replay_interval"), - &PoolOption::PgNum => write!(f, "pg_num"), - &PoolOption::PgpNum => write!(f, "pgp_num"), - &PoolOption::CrushRule => write!(f, "crush_rule"), - &PoolOption::HashPsPool => write!(f, "hashpspool"), - &PoolOption::NoDelete => write!(f, "nodelete"), - &PoolOption::NoPgChange => write!(f, "nopgchange"), - &PoolOption::NoSizeChange => write!(f, "nosizechange"), - &PoolOption::WriteFadviceDontNeed => write!(f, "write_fadvice_dontneed"), - &PoolOption::NoScrub => write!(f, "noscrub"), - &PoolOption::NoDeepScrub => write!(f, "nodeep-scrub"), - &PoolOption::HitSetType => write!(f, "hit_set_type"), - &PoolOption::HitSetPeriod => write!(f, "hit_set_period"), - &PoolOption::HitSetCount => write!(f, "hit_set_count"), - &PoolOption::HitSetFpp => write!(f, "hit_set_fpp"), - &PoolOption::UseGmtHitset => write!(f, "use_gmt_hitset"), - &PoolOption::TargetMaxBytes => write!(f, "target_max_bytes"), - &PoolOption::TargetMaxObjects => write!(f, "target_max_objects"), - &PoolOption::CacheTargetDirtyRatio => write!(f, "cache_target_dirty_ratio"), - &PoolOption::CacheTargetDirtyHighRatio => write!(f, "cache_target_dirty_high_ratio"), - &PoolOption::CacheTargetFullRatio => write!(f, "cache_target_full_ratio"), - &PoolOption::CacheMinFlushAge => write!(f, "cache_min_flush_age"), - &PoolOption::CacheMinEvictAge => write!(f, "cachem_min_evict_age"), - &PoolOption::Auid => write!(f, "auid"), - &PoolOption::MinReadRecencyForPromote => write!(f, "min_read_recency_for_promote"), - &PoolOption::MinWriteRecencyForPromte => write!(f, "min_write_recency_for_promote"), - &PoolOption::FastRead => write!(f, "fast_read"), - &PoolOption::HitSetGradeDecayRate => write!(f, "hit_set_decay_rate"), - &PoolOption::HitSetSearchLastN => write!(f, "hit_set_search_last_n"), - &PoolOption::ScrubMinInterval => write!(f, "scrub_min_interval"), - &PoolOption::ScrubMaxInterval => write!(f, "scrub_max_interval"), - &PoolOption::DeepScrubInterval => write!(f, "deep_scrub_interval"), - &PoolOption::RecoveryPriority => write!(f, "recovery_priority"), - &PoolOption::RecoveryOpPriority => write!(f, "recovery_op_priority"), - &PoolOption::ScrubPriority => write!(f, "scrub_priority"), - &PoolOption::CompressionMode => write!(f, "compression_mode"), - &PoolOption::CompressionAlgorithm => write!(f, "compression_algorithm"), - &PoolOption::CompressionRequiredRatio => write!(f, "compression_required_ratio"), - &PoolOption::CompressionMaxBlobSize => write!(f, "compression_max_blob_size"), - &PoolOption::CompressionMinBlobSize => write!(f, "compression_min_blob_size"), - &PoolOption::CsumType => write!(f, "csum_type"), - &PoolOption::CsumMinBlock => write!(f, "csum_min_block"), - &PoolOption::CsumMaxBlock => write!(f, "csum_max_block"), - &PoolOption::AllocEcOverwrites => write!(f, "allow_ec_overwrites"), + match *self { + PoolOption::Size => write!(f, "size"), + PoolOption::MinSize => write!(f, "min_size"), + PoolOption::CrashReplayInterval => write!(f, "crash_replay_interval"), + PoolOption::PgNum => write!(f, "pg_num"), + PoolOption::PgpNum => write!(f, "pgp_num"), + PoolOption::CrushRule => write!(f, "crush_rule"), + PoolOption::HashPsPool => write!(f, "hashpspool"), + PoolOption::NoDelete => write!(f, "nodelete"), + PoolOption::NoPgChange => write!(f, "nopgchange"), + PoolOption::NoSizeChange => write!(f, "nosizechange"), + PoolOption::WriteFadviceDontNeed => write!(f, "write_fadvice_dontneed"), + PoolOption::NoScrub => write!(f, "noscrub"), + PoolOption::NoDeepScrub => write!(f, "nodeep-scrub"), + PoolOption::HitSetType => write!(f, "hit_set_type"), + PoolOption::HitSetPeriod => write!(f, "hit_set_period"), + PoolOption::HitSetCount => write!(f, "hit_set_count"), + PoolOption::HitSetFpp => write!(f, "hit_set_fpp"), + PoolOption::UseGmtHitset => write!(f, "use_gmt_hitset"), + PoolOption::TargetMaxBytes => write!(f, "target_max_bytes"), + PoolOption::TargetMaxObjects => write!(f, "target_max_objects"), + PoolOption::CacheTargetDirtyRatio => write!(f, "cache_target_dirty_ratio"), + PoolOption::CacheTargetDirtyHighRatio => write!(f, "cache_target_dirty_high_ratio"), + PoolOption::CacheTargetFullRatio => write!(f, "cache_target_full_ratio"), + PoolOption::CacheMinFlushAge => write!(f, "cache_min_flush_age"), + PoolOption::CacheMinEvictAge => write!(f, "cachem_min_evict_age"), + PoolOption::Auid => write!(f, "auid"), + PoolOption::MinReadRecencyForPromote => write!(f, "min_read_recency_for_promote"), + PoolOption::MinWriteRecencyForPromte => write!(f, "min_write_recency_for_promote"), + PoolOption::FastRead => write!(f, "fast_read"), + PoolOption::HitSetGradeDecayRate => write!(f, "hit_set_decay_rate"), + PoolOption::HitSetSearchLastN => write!(f, "hit_set_search_last_n"), + PoolOption::ScrubMinInterval => write!(f, "scrub_min_interval"), + PoolOption::ScrubMaxInterval => write!(f, "scrub_max_interval"), + PoolOption::DeepScrubInterval => write!(f, "deep_scrub_interval"), + PoolOption::RecoveryPriority => write!(f, "recovery_priority"), + PoolOption::RecoveryOpPriority => write!(f, "recovery_op_priority"), + PoolOption::ScrubPriority => write!(f, "scrub_priority"), + PoolOption::CompressionMode => write!(f, "compression_mode"), + PoolOption::CompressionAlgorithm => write!(f, "compression_algorithm"), + PoolOption::CompressionRequiredRatio => write!(f, "compression_required_ratio"), + PoolOption::CompressionMaxBlobSize => write!(f, "compression_max_blob_size"), + PoolOption::CompressionMinBlobSize => write!(f, "compression_min_blob_size"), + PoolOption::CsumType => write!(f, "csum_type"), + PoolOption::CsumMinBlock => write!(f, "csum_min_block"), + PoolOption::CsumMaxBlock => write!(f, "csum_max_block"), + PoolOption::AllocEcOverwrites => write!(f, "allow_ec_overwrites"), } } } impl AsRef for PoolOption { fn as_ref(&self) -> &str { - match self { - &PoolOption::Size => "size", - &PoolOption::MinSize => "min_size", - &PoolOption::CrashReplayInterval => "crash_replay_interval", - &PoolOption::PgNum => "pg_num", - &PoolOption::PgpNum => "pgp_num", - &PoolOption::CrushRule => "crush_rule", - &PoolOption::HashPsPool => "hashpspool", - &PoolOption::NoDelete => "nodelete", - &PoolOption::NoPgChange => "nopgchange", - &PoolOption::NoSizeChange => "nosizechange", - &PoolOption::WriteFadviceDontNeed => "write_fadvice_dontneed", - &PoolOption::NoScrub => "noscrub", - &PoolOption::NoDeepScrub => "nodeep-scrub", - &PoolOption::HitSetType => "hit_set_type", - &PoolOption::HitSetPeriod => "hit_set_period", - &PoolOption::HitSetCount => "hit_set_count", - &PoolOption::HitSetFpp => "hit_set_fpp", - &PoolOption::UseGmtHitset => "use_gmt_hitset", - &PoolOption::TargetMaxBytes => "target_max_bytes", - &PoolOption::TargetMaxObjects => "target_max_objects", - &PoolOption::CacheTargetDirtyRatio => "cache_target_dirty_ratio", - &PoolOption::CacheTargetDirtyHighRatio => "cache_target_dirty_high_ratio", - &PoolOption::CacheTargetFullRatio => "cache_target_full_ratio", - &PoolOption::CacheMinFlushAge => "cache_min_flush_age", - &PoolOption::CacheMinEvictAge => "cachem_min_evict_age", - &PoolOption::Auid => "auid", - &PoolOption::MinReadRecencyForPromote => "min_read_recency_for_promote", - &PoolOption::MinWriteRecencyForPromte => "min_write_recency_for_promote", - &PoolOption::FastRead => "fast_read", - &PoolOption::HitSetGradeDecayRate => "hit_set_decay_rate", - &PoolOption::HitSetSearchLastN => "hit_set_search_last_n", - &PoolOption::ScrubMinInterval => "scrub_min_interval", - &PoolOption::ScrubMaxInterval => "scrub_max_interval", - &PoolOption::DeepScrubInterval => "deep_scrub_interval", - &PoolOption::RecoveryPriority => "recovery_priority", - &PoolOption::RecoveryOpPriority => "recovery_op_priority", - &PoolOption::ScrubPriority => "scrub_priority", - &PoolOption::CompressionMode => "compression_mode", - &PoolOption::CompressionAlgorithm => "compression_algorithm", - &PoolOption::CompressionRequiredRatio => "compression_required_ratio", - &PoolOption::CompressionMaxBlobSize => "compression_max_blob_size", - &PoolOption::CompressionMinBlobSize => "compression_min_blob_size", - &PoolOption::CsumType => "csum_type", - &PoolOption::CsumMinBlock => "csum_min_block", - &PoolOption::CsumMaxBlock => "csum_max_block", - &PoolOption::AllocEcOverwrites => "allow_ec_overwrites", + match *self { + PoolOption::Size => "size", + PoolOption::MinSize => "min_size", + PoolOption::CrashReplayInterval => "crash_replay_interval", + PoolOption::PgNum => "pg_num", + PoolOption::PgpNum => "pgp_num", + PoolOption::CrushRule => "crush_rule", + PoolOption::HashPsPool => "hashpspool", + PoolOption::NoDelete => "nodelete", + PoolOption::NoPgChange => "nopgchange", + PoolOption::NoSizeChange => "nosizechange", + PoolOption::WriteFadviceDontNeed => "write_fadvice_dontneed", + PoolOption::NoScrub => "noscrub", + PoolOption::NoDeepScrub => "nodeep-scrub", + PoolOption::HitSetType => "hit_set_type", + PoolOption::HitSetPeriod => "hit_set_period", + PoolOption::HitSetCount => "hit_set_count", + PoolOption::HitSetFpp => "hit_set_fpp", + PoolOption::UseGmtHitset => "use_gmt_hitset", + PoolOption::TargetMaxBytes => "target_max_bytes", + PoolOption::TargetMaxObjects => "target_max_objects", + PoolOption::CacheTargetDirtyRatio => "cache_target_dirty_ratio", + PoolOption::CacheTargetDirtyHighRatio => "cache_target_dirty_high_ratio", + PoolOption::CacheTargetFullRatio => "cache_target_full_ratio", + PoolOption::CacheMinFlushAge => "cache_min_flush_age", + PoolOption::CacheMinEvictAge => "cachem_min_evict_age", + PoolOption::Auid => "auid", + PoolOption::MinReadRecencyForPromote => "min_read_recency_for_promote", + PoolOption::MinWriteRecencyForPromte => "min_write_recency_for_promote", + PoolOption::FastRead => "fast_read", + PoolOption::HitSetGradeDecayRate => "hit_set_decay_rate", + PoolOption::HitSetSearchLastN => "hit_set_search_last_n", + PoolOption::ScrubMinInterval => "scrub_min_interval", + PoolOption::ScrubMaxInterval => "scrub_max_interval", + PoolOption::DeepScrubInterval => "deep_scrub_interval", + PoolOption::RecoveryPriority => "recovery_priority", + PoolOption::RecoveryOpPriority => "recovery_op_priority", + PoolOption::ScrubPriority => "scrub_priority", + PoolOption::CompressionMode => "compression_mode", + PoolOption::CompressionAlgorithm => "compression_algorithm", + PoolOption::CompressionRequiredRatio => "compression_required_ratio", + PoolOption::CompressionMaxBlobSize => "compression_max_blob_size", + PoolOption::CompressionMinBlobSize => "compression_min_blob_size", + PoolOption::CsumType => "csum_type", + PoolOption::CsumMinBlock => "csum_min_block", + PoolOption::CsumMaxBlock => "csum_max_block", + PoolOption::AllocEcOverwrites => "allow_ec_overwrites", } } } impl fmt::Display for HealthStatus { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - &HealthStatus::Err => write!(f, "HEALTH_ERR"), - &HealthStatus::Ok => write!(f, "HEALTH_OK"), - &HealthStatus::Warn => write!(f, "HEALTH_WARN"), + match *self { + HealthStatus::Err => write!(f, "HEALTH_ERR"), + HealthStatus::Ok => write!(f, "HEALTH_OK"), + HealthStatus::Warn => write!(f, "HEALTH_WARN"), } } } impl AsRef for HealthStatus { fn as_ref(&self) -> &str { - match self { - &HealthStatus::Err => "HEALTH_ERR", - &HealthStatus::Ok => "HEALTH_OK", - &HealthStatus::Warn => "HEALTH_WARN", + match *self { + HealthStatus::Err => "HEALTH_ERR", + HealthStatus::Ok => "HEALTH_OK", + HealthStatus::Warn => "HEALTH_WARN", } } } impl fmt::Display for MonState { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - &MonState::Probing => write!(f, "probing"), - &MonState::Synchronizing => write!(f, "synchronizing"), - &MonState::Electing => write!(f, "electing"), - &MonState::Leader => write!(f, "leader"), - &MonState::Peon => write!(f, "peon"), - &MonState::Shutdown => write!(f, "shutdown"), + match *self { + MonState::Probing => write!(f, "probing"), + MonState::Synchronizing => write!(f, "synchronizing"), + MonState::Electing => write!(f, "electing"), + MonState::Leader => write!(f, "leader"), + MonState::Peon => write!(f, "peon"), + MonState::Shutdown => write!(f, "shutdown"), } } } impl AsRef for MonState { fn as_ref(&self) -> &str { - match self { - &MonState::Probing => "probing", - &MonState::Synchronizing => "synchronizing", - &MonState::Electing => "electing", - &MonState::Leader => "leader", - &MonState::Peon => "peon", - &MonState::Shutdown => "shutdown", + match *self { + MonState::Probing => "probing", + MonState::Synchronizing => "synchronizing", + MonState::Electing => "electing", + MonState::Leader => "leader", + MonState::Peon => "peon", + MonState::Shutdown => "shutdown", } } } impl fmt::Display for RoundStatus { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - &RoundStatus::Finished => write!(f, "finished"), - &RoundStatus::OnGoing => write!(f, "on-going"), + match *self { + RoundStatus::Finished => write!(f, "finished"), + RoundStatus::OnGoing => write!(f, "on-going"), } } } impl AsRef for RoundStatus { fn as_ref(&self) -> &str { - match self { - &RoundStatus::Finished => "finished", - &RoundStatus::OnGoing => "on-going", + match *self { + RoundStatus::Finished => "finished", + RoundStatus::OnGoing => "on-going", } } } @@ -659,7 +659,7 @@ pub fn osd_crush_remove(cluster_handle: &Rados, osd_id: u64, simulate: bool) -> } /// Query a ceph pool. -pub fn osd_pool_get(cluster_handle: &Rados, pool: &str, choice: &PoolOption) -> RadosResult { +pub fn osd_pool_get(cluster_handle: &Rados, pool: &str, choice: PoolOption) -> RadosResult { let cmd = json!({ "prefix": "osd pool get", "pool": pool, @@ -681,7 +681,7 @@ pub fn osd_pool_get(cluster_handle: &Rados, pool: &str, choice: &PoolOption) -> pub fn osd_pool_set( cluster_handle: &Rados, pool: &str, - key: &PoolOption, + key: PoolOption, value: &str, simulate: bool, ) -> RadosResult<()> { @@ -697,17 +697,18 @@ pub fn osd_pool_set( Ok(()) } -pub fn osd_set(cluster_handle: &Rados, key: &OsdOption, force: bool, simulate: bool) -> RadosResult<()> { - let cmd = match force { - true => json!({ +pub fn osd_set(cluster_handle: &Rados, key: OsdOption, force: bool, simulate: bool) -> RadosResult<()> { + let cmd = if force { + json!({ "prefix": "osd set", "key": key, "sure": "--yes-i-really-mean-it", - }), - false => json!({ + }) + } else { + json!({ "prefix": "osd set", "key": key, - }), + }) }; if !simulate { cluster_handle.ceph_mon_command_without_data(&cmd)?; @@ -715,7 +716,7 @@ pub fn osd_set(cluster_handle: &Rados, key: &OsdOption, force: bool, simulate: b Ok(()) } -pub fn osd_unset(cluster_handle: &Rados, key: &OsdOption, simulate: bool) -> RadosResult<()> { +pub fn osd_unset(cluster_handle: &Rados, key: OsdOption, simulate: bool) -> RadosResult<()> { let cmd = json!({ "prefix": "osd unset", "key": key, @@ -996,16 +997,17 @@ pub fn mgr_list_services(cluster_handle: &Rados) -> RadosResult> { /// Enable a mgr module pub fn mgr_enable_module(cluster_handle: &Rados, module: &str, force: bool, simulate: bool) -> RadosResult<()> { - let cmd = match force { - true => json!({ + let cmd = if force { + json!({ "prefix": "mgr module enable", "module": module, "force": "--force", - }), - false => json!({ + }) + } else { + json!({ "prefix": "mgr module enable", "module": module, - }), + }) }; if !simulate { diff --git a/src/json.rs b/src/json.rs index ab8690e..f098b4e 100644 --- a/src/json.rs +++ b/src/json.rs @@ -33,7 +33,7 @@ pub fn json_data(json_str: &str) -> Option { /// parent object is None then it only looks for the 'child' object. The parent /// object is used for situations where there may be 'child' objects with the /// same name. -pub fn json_find(json_data: JsonData, keys: &[&str]) -> Option { +pub fn json_find(json_data: &JsonData, keys: &[&str]) -> Option { json_data.find_path(keys).cloned() } diff --git a/src/utils.rs b/src/utils.rs index 4ebd382..f7656b3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -30,6 +30,6 @@ use std::process::{Command, Output}; // NOTE: Add Into so a "" can also be passed in... pub fn run_cli(cmd_line: &str) -> Result<(Output)> { - let output = try!(Command::new("sh").arg("-c").arg(cmd_line).output()); + let output = Command::new("sh").arg("-c").arg(cmd_line).output()?; Ok(output) }