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 ;
1918use std:: path:: Path ;
2019
2120/// Represents information of a backup including timestamp of the backup
@@ -48,16 +47,7 @@ pub struct RestoreOptions {
4847impl 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(
0 commit comments