Skip to content

Commit 8375071

Browse files
authored
Consistently use ffi_util::to_cpath to convert Path to CString (tikv#640)
1 parent e26428b commit 8375071

File tree

3 files changed

+15
-73
lines changed

3 files changed

+15
-73
lines changed

src/backup.rs

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
// limitations under the License.
1414
//
1515

16-
use crate::{ffi, Error, DB};
16+
use crate::{ffi, ffi_util::to_cpath, Error, DB};
1717

18-
use std::ffi::CString;
1918
use std::path::Path;
2019

2120
/// Represents information of a backup including timestamp of the backup
@@ -48,16 +47,7 @@ pub struct RestoreOptions {
4847
impl BackupEngine {
4948
/// Open a backup engine with the specified options.
5049
pub fn open<P: AsRef<Path>>(opts: &BackupEngineOptions, path: P) -> Result<Self, Error> {
51-
let path = path.as_ref();
52-
let cpath = if let Ok(e) = CString::new(path.to_string_lossy().as_bytes()) {
53-
e
54-
} else {
55-
return Err(Error::new(
56-
"Failed to convert path to CString \
57-
when opening backup engine"
58-
.to_owned(),
59-
));
60-
};
50+
let cpath = to_cpath(path)?;
6151

6252
let be: *mut ffi::rocksdb_backup_engine_t;
6353
unsafe {
@@ -136,27 +126,8 @@ impl BackupEngine {
136126
wal_dir: W,
137127
opts: &RestoreOptions,
138128
) -> Result<(), Error> {
139-
let db_dir = db_dir.as_ref();
140-
let c_db_dir = if let Ok(c) = CString::new(db_dir.to_string_lossy().as_bytes()) {
141-
c
142-
} else {
143-
return Err(Error::new(
144-
"Failed to convert db_dir to CString \
145-
when restoring from latest backup"
146-
.to_owned(),
147-
));
148-
};
149-
150-
let wal_dir = wal_dir.as_ref();
151-
let c_wal_dir = if let Ok(c) = CString::new(wal_dir.to_string_lossy().as_bytes()) {
152-
c
153-
} else {
154-
return Err(Error::new(
155-
"Failed to convert wal_dir to CString \
156-
when restoring from latest backup"
157-
.to_owned(),
158-
));
159-
};
129+
let c_db_dir = to_cpath(db_dir)?;
130+
let c_wal_dir = to_cpath(wal_dir)?;
160131

161132
unsafe {
162133
ffi_try!(ffi::rocksdb_backup_engine_restore_db_from_latest_backup(
@@ -179,27 +150,8 @@ impl BackupEngine {
179150
opts: &RestoreOptions,
180151
backup_id: u32,
181152
) -> Result<(), Error> {
182-
let db_dir = db_dir.as_ref();
183-
let c_db_dir = if let Ok(c) = CString::new(db_dir.to_string_lossy().as_bytes()) {
184-
c
185-
} else {
186-
return Err(Error::new(
187-
"Failed to convert db_dir to CString \
188-
when restoring from latest backup"
189-
.to_owned(),
190-
));
191-
};
192-
193-
let wal_dir = wal_dir.as_ref();
194-
let c_wal_dir = if let Ok(c) = CString::new(wal_dir.to_string_lossy().as_bytes()) {
195-
c
196-
} else {
197-
return Err(Error::new(
198-
"Failed to convert wal_dir to CString \
199-
when restoring from latest backup"
200-
.to_owned(),
201-
));
202-
};
153+
let c_db_dir = to_cpath(db_dir)?;
154+
let c_wal_dir = to_cpath(wal_dir)?;
203155

204156
unsafe {
205157
ffi_try!(ffi::rocksdb_backup_engine_restore_db_from_backup(

src/checkpoint.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
//!
1818
//! [1]: https://github.com/facebook/rocksdb/wiki/Checkpoints
1919
20-
use crate::{ffi, Error, DB};
21-
use std::ffi::CString;
20+
use crate::{ffi, ffi_util::to_cpath, Error, DB};
2221
use std::marker::PhantomData;
2322
use std::path::Path;
2423

@@ -56,24 +55,15 @@ impl<'db> Checkpoint<'db> {
5655

5756
/// Creates new physical DB checkpoint in directory specified by `path`.
5857
pub fn create_checkpoint<P: AsRef<Path>>(&self, path: P) -> Result<(), Error> {
59-
let path = path.as_ref();
60-
let cpath = if let Ok(c) = CString::new(path.to_string_lossy().as_bytes()) {
61-
c
62-
} else {
63-
return Err(Error::new(
64-
"Failed to convert path to CString when creating DB checkpoint".to_owned(),
65-
));
66-
};
67-
58+
let cpath = to_cpath(path)?;
6859
unsafe {
6960
ffi_try!(ffi::rocksdb_checkpoint_create(
7061
self.inner,
7162
cpath.as_ptr(),
7263
LOG_SIZE_FOR_FLUSH,
7364
));
74-
75-
Ok(())
7665
}
66+
Ok(())
7767
}
7868
}
7969

src/db_options.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::ffi::{CStr, CString};
15+
use std::ffi::CStr;
1616
use std::path::Path;
1717
use std::sync::Arc;
1818

@@ -24,7 +24,7 @@ use crate::{
2424
comparator::{self, ComparatorCallback, CompareFn},
2525
db::DBAccess,
2626
ffi,
27-
ffi_util::CStrLike,
27+
ffi_util::{to_cpath, CStrLike},
2828
merge_operator::{
2929
self, full_merge_callback, partial_merge_callback, MergeFn, MergeOperatorCallback,
3030
},
@@ -1535,7 +1535,7 @@ impl Options {
15351535
///
15361536
/// Default: empty
15371537
pub fn set_db_log_dir<P: AsRef<Path>>(&mut self, path: P) {
1538-
let p = CString::new(path.as_ref().to_string_lossy().as_bytes()).unwrap();
1538+
let p = to_cpath(path).unwrap();
15391539
unsafe {
15401540
ffi::rocksdb_options_set_db_log_dir(self.inner, p.as_ptr());
15411541
}
@@ -2723,7 +2723,7 @@ impl Options {
27232723
/// opts.set_wal_dir("/path/to/dir");
27242724
/// ```
27252725
pub fn set_wal_dir<P: AsRef<Path>>(&mut self, path: P) {
2726-
let p = CString::new(path.as_ref().to_string_lossy().as_bytes()).unwrap();
2726+
let p = to_cpath(path).unwrap();
27272727
unsafe {
27282728
ffi::rocksdb_options_set_wal_dir(self.inner, p.as_ptr());
27292729
}
@@ -3860,12 +3860,12 @@ pub struct DBPath {
38603860
impl DBPath {
38613861
/// Create a new path
38623862
pub fn new<P: AsRef<Path>>(path: P, target_size: u64) -> Result<Self, Error> {
3863-
let p = CString::new(path.as_ref().to_string_lossy().as_bytes()).unwrap();
3863+
let p = to_cpath(path.as_ref()).unwrap();
38643864
let dbpath = unsafe { ffi::rocksdb_dbpath_create(p.as_ptr(), target_size) };
38653865
if dbpath.is_null() {
38663866
Err(Error::new(format!(
38673867
"Could not create path for storing sst files at location: {}",
3868-
path.as_ref().to_string_lossy()
3868+
path.as_ref().display()
38693869
)))
38703870
} else {
38713871
Ok(DBPath { inner: dbpath })

0 commit comments

Comments
 (0)