Consistently use ffi_util::to_cpath to convert Path to CString (#640)

master
Michal Nazarewicz 2 years ago committed by GitHub
parent e26428be6a
commit 83750719c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 60
      src/backup.rs
  2. 16
      src/checkpoint.rs
  3. 12
      src/db_options.rs

@ -13,9 +13,8 @@
// limitations under the License. // limitations under the License.
// //
use crate::{ffi, Error, DB}; use crate::{ffi, ffi_util::to_cpath, Error, DB};
use std::ffi::CString;
use std::path::Path; use std::path::Path;
/// Represents information of a backup including timestamp of the backup /// Represents information of a backup including timestamp of the backup
@ -48,16 +47,7 @@ pub struct RestoreOptions {
impl BackupEngine { impl BackupEngine {
/// Open a backup engine with the specified options. /// Open a backup engine with the specified options.
pub fn open<P: AsRef<Path>>(opts: &BackupEngineOptions, path: P) -> Result<Self, Error> { pub fn open<P: AsRef<Path>>(opts: &BackupEngineOptions, path: P) -> Result<Self, Error> {
let path = path.as_ref(); let cpath = to_cpath(path)?;
let cpath = if let Ok(e) = CString::new(path.to_string_lossy().as_bytes()) {
e
} else {
return Err(Error::new(
"Failed to convert path to CString \
when opening backup engine"
.to_owned(),
));
};
let be: *mut ffi::rocksdb_backup_engine_t; let be: *mut ffi::rocksdb_backup_engine_t;
unsafe { unsafe {
@ -136,27 +126,8 @@ impl BackupEngine {
wal_dir: W, wal_dir: W,
opts: &RestoreOptions, opts: &RestoreOptions,
) -> Result<(), Error> { ) -> Result<(), Error> {
let db_dir = db_dir.as_ref(); let c_db_dir = to_cpath(db_dir)?;
let c_db_dir = if let Ok(c) = CString::new(db_dir.to_string_lossy().as_bytes()) { let c_wal_dir = to_cpath(wal_dir)?;
c
} else {
return Err(Error::new(
"Failed to convert db_dir to CString \
when restoring from latest backup"
.to_owned(),
));
};
let wal_dir = wal_dir.as_ref();
let c_wal_dir = if let Ok(c) = CString::new(wal_dir.to_string_lossy().as_bytes()) {
c
} else {
return Err(Error::new(
"Failed to convert wal_dir to CString \
when restoring from latest backup"
.to_owned(),
));
};
unsafe { unsafe {
ffi_try!(ffi::rocksdb_backup_engine_restore_db_from_latest_backup( ffi_try!(ffi::rocksdb_backup_engine_restore_db_from_latest_backup(
@ -179,27 +150,8 @@ impl BackupEngine {
opts: &RestoreOptions, opts: &RestoreOptions,
backup_id: u32, backup_id: u32,
) -> Result<(), Error> { ) -> Result<(), Error> {
let db_dir = db_dir.as_ref(); let c_db_dir = to_cpath(db_dir)?;
let c_db_dir = if let Ok(c) = CString::new(db_dir.to_string_lossy().as_bytes()) { let c_wal_dir = to_cpath(wal_dir)?;
c
} else {
return Err(Error::new(
"Failed to convert db_dir to CString \
when restoring from latest backup"
.to_owned(),
));
};
let wal_dir = wal_dir.as_ref();
let c_wal_dir = if let Ok(c) = CString::new(wal_dir.to_string_lossy().as_bytes()) {
c
} else {
return Err(Error::new(
"Failed to convert wal_dir to CString \
when restoring from latest backup"
.to_owned(),
));
};
unsafe { unsafe {
ffi_try!(ffi::rocksdb_backup_engine_restore_db_from_backup( ffi_try!(ffi::rocksdb_backup_engine_restore_db_from_backup(

@ -17,8 +17,7 @@
//! //!
//! [1]: https://github.com/facebook/rocksdb/wiki/Checkpoints //! [1]: https://github.com/facebook/rocksdb/wiki/Checkpoints
use crate::{ffi, Error, DB}; use crate::{ffi, ffi_util::to_cpath, Error, DB};
use std::ffi::CString;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::path::Path; use std::path::Path;
@ -56,24 +55,15 @@ impl<'db> Checkpoint<'db> {
/// Creates new physical DB checkpoint in directory specified by `path`. /// Creates new physical DB checkpoint in directory specified by `path`.
pub fn create_checkpoint<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> { pub fn create_checkpoint<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
let path = path.as_ref(); let cpath = to_cpath(path)?;
let cpath = if let Ok(c) = CString::new(path.to_string_lossy().as_bytes()) {
c
} else {
return Err(Error::new(
"Failed to convert path to CString when creating DB checkpoint".to_owned(),
));
};
unsafe { unsafe {
ffi_try!(ffi::rocksdb_checkpoint_create( ffi_try!(ffi::rocksdb_checkpoint_create(
self.inner, self.inner,
cpath.as_ptr(), cpath.as_ptr(),
LOG_SIZE_FOR_FLUSH, LOG_SIZE_FOR_FLUSH,
)); ));
Ok(())
} }
Ok(())
} }
} }

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use std::ffi::{CStr, CString}; use std::ffi::CStr;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
@ -24,7 +24,7 @@ use crate::{
comparator::{self, ComparatorCallback, CompareFn}, comparator::{self, ComparatorCallback, CompareFn},
db::DBAccess, db::DBAccess,
ffi, ffi,
ffi_util::CStrLike, ffi_util::{to_cpath, CStrLike},
merge_operator::{ merge_operator::{
self, full_merge_callback, partial_merge_callback, MergeFn, MergeOperatorCallback, self, full_merge_callback, partial_merge_callback, MergeFn, MergeOperatorCallback,
}, },
@ -1535,7 +1535,7 @@ impl Options {
/// ///
/// Default: empty /// Default: empty
pub fn set_db_log_dir<P: AsRef<Path>>(&mut self, path: P) { pub fn set_db_log_dir<P: AsRef<Path>>(&mut self, path: P) {
let p = CString::new(path.as_ref().to_string_lossy().as_bytes()).unwrap(); let p = to_cpath(path).unwrap();
unsafe { unsafe {
ffi::rocksdb_options_set_db_log_dir(self.inner, p.as_ptr()); ffi::rocksdb_options_set_db_log_dir(self.inner, p.as_ptr());
} }
@ -2723,7 +2723,7 @@ impl Options {
/// opts.set_wal_dir("/path/to/dir"); /// opts.set_wal_dir("/path/to/dir");
/// ``` /// ```
pub fn set_wal_dir<P: AsRef<Path>>(&mut self, path: P) { pub fn set_wal_dir<P: AsRef<Path>>(&mut self, path: P) {
let p = CString::new(path.as_ref().to_string_lossy().as_bytes()).unwrap(); let p = to_cpath(path).unwrap();
unsafe { unsafe {
ffi::rocksdb_options_set_wal_dir(self.inner, p.as_ptr()); ffi::rocksdb_options_set_wal_dir(self.inner, p.as_ptr());
} }
@ -3860,12 +3860,12 @@ pub struct DBPath {
impl DBPath { impl DBPath {
/// Create a new path /// Create a new path
pub fn new<P: AsRef<Path>>(path: P, target_size: u64) -> Result<Self, Error> { pub fn new<P: AsRef<Path>>(path: P, target_size: u64) -> Result<Self, Error> {
let p = CString::new(path.as_ref().to_string_lossy().as_bytes()).unwrap(); let p = to_cpath(path.as_ref()).unwrap();
let dbpath = unsafe { ffi::rocksdb_dbpath_create(p.as_ptr(), target_size) }; let dbpath = unsafe { ffi::rocksdb_dbpath_create(p.as_ptr(), target_size) };
if dbpath.is_null() { if dbpath.is_null() {
Err(Error::new(format!( Err(Error::new(format!(
"Could not create path for storing sst files at location: {}", "Could not create path for storing sst files at location: {}",
path.as_ref().to_string_lossy() path.as_ref().display()
))) )))
} else { } else {
Ok(DBPath { inner: dbpath }) Ok(DBPath { inner: dbpath })

Loading…
Cancel
Save