Skip to content

Commit

Permalink
Small clean-ups (including fix for UB) (rust-rocksdb#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf authored Jun 28, 2022
1 parent ac4ffa3 commit 701d461
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 87 deletions.
8 changes: 6 additions & 2 deletions src/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use crate::{ffi, ffi_util::to_cpath, Error, DB};

use libc::{c_int, c_uchar};
use std::path::Path;

/// Represents information of a backup including timestamp of the backup
Expand Down Expand Up @@ -82,7 +83,7 @@ impl BackupEngine {
ffi_try!(ffi::rocksdb_backup_engine_create_new_backup_flush(
self.inner,
db.inner,
u8::from(flush_before_backup),
c_uchar::from(flush_before_backup),
));
Ok(())
}
Expand Down Expand Up @@ -219,7 +220,10 @@ impl BackupEngineOptions {
impl RestoreOptions {
pub fn set_keep_log_files(&mut self, keep_log_files: bool) {
unsafe {
ffi::rocksdb_restore_options_set_keep_log_files(self.inner, i32::from(keep_log_files));
ffi::rocksdb_restore_options_set_keep_log_files(
self.inner,
c_int::from(keep_log_files),
);
}
}
}
Expand Down
29 changes: 15 additions & 14 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
WriteBatch, WriteOptions, DEFAULT_COLUMN_FAMILY_NAME,
};

use libc::{self, c_char, c_int, c_void, size_t};
use libc::{self, c_char, c_int, c_uchar, c_void, size_t};
use std::collections::BTreeMap;
use std::ffi::{CStr, CString};
use std::fmt;
Expand Down Expand Up @@ -638,22 +638,22 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
error_if_log_file_exist,
} => ffi_try!(ffi::rocksdb_open_for_read_only(
opts.inner,
cpath.as_ptr() as *const _,
u8::from(error_if_log_file_exist),
cpath.as_ptr(),
c_uchar::from(error_if_log_file_exist),
)),
AccessType::ReadWrite => {
ffi_try!(ffi::rocksdb_open(opts.inner, cpath.as_ptr() as *const _))
ffi_try!(ffi::rocksdb_open(opts.inner, cpath.as_ptr()))
}
AccessType::Secondary { secondary_path } => {
ffi_try!(ffi::rocksdb_open_as_secondary(
opts.inner,
cpath.as_ptr() as *const _,
to_cpath(secondary_path)?.as_ptr() as *const _,
cpath.as_ptr(),
to_cpath(secondary_path)?.as_ptr(),
))
}
AccessType::WithTTL { ttl } => ffi_try!(ffi::rocksdb_open_with_ttl(
opts.inner,
cpath.as_ptr() as *const _,
cpath.as_ptr(),
ttl.as_secs() as c_int,
)),
}
Expand Down Expand Up @@ -682,7 +682,7 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
cfnames.as_ptr(),
cfopts.as_ptr(),
cfhandles.as_mut_ptr(),
u8::from(error_if_log_file_exist),
c_uchar::from(error_if_log_file_exist),
)),
AccessType::ReadWrite => ffi_try!(ffi::rocksdb_open_column_families(
opts.inner,
Expand All @@ -695,23 +695,24 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
AccessType::Secondary { secondary_path } => {
ffi_try!(ffi::rocksdb_open_as_secondary_column_families(
opts.inner,
cpath.as_ptr() as *const _,
to_cpath(secondary_path)?.as_ptr() as *const _,
cpath.as_ptr(),
to_cpath(secondary_path)?.as_ptr(),
cfs_v.len() as c_int,
cfnames.as_ptr(),
cfopts.as_ptr(),
cfhandles.as_mut_ptr(),
))
}
AccessType::WithTTL { ttl } => {
let ttls_v = vec![ttl.as_secs() as c_int; cfs_v.len()];
ffi_try!(ffi::rocksdb_open_column_families_with_ttl(
opts.inner,
cpath.as_ptr(),
cfs_v.len() as c_int,
cfnames.as_ptr(),
cfopts.as_ptr(),
cfhandles.as_mut_ptr(),
&(ttl.as_secs() as c_int) as *const _,
ttls_v.as_ptr(),
))
}
}
Expand All @@ -726,7 +727,7 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
unsafe {
let ptr = ffi_try!(ffi::rocksdb_list_column_families(
opts.inner,
cpath.as_ptr() as *const _,
cpath.as_ptr(),
&mut length,
));

Expand Down Expand Up @@ -763,7 +764,7 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
/// the data to disk.
pub fn flush_wal(&self, sync: bool) -> Result<(), Error> {
unsafe {
ffi_try!(ffi::rocksdb_flush_wal(self.inner, u8::from(sync)));
ffi_try!(ffi::rocksdb_flush_wal(self.inner, c_uchar::from(sync)));
}
Ok(())
}
Expand Down Expand Up @@ -1975,7 +1976,7 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
/// Request stopping background work, if wait is true wait until it's done.
pub fn cancel_all_background_work(&self, wait: bool) {
unsafe {
ffi::rocksdb_cancel_all_background_work(self.inner, u8::from(wait));
ffi::rocksdb_cancel_all_background_work(self.inner, c_uchar::from(wait));
}
}

Expand Down
Loading

0 comments on commit 701d461

Please sign in to comment.