1// Copyright 2016 Alex Regueiro
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
1516use crate::{ffi, Error};
17use libc::{self, c_char, c_void, size_t};
18use std::ffi::{CStr, CString};
19use std::path::Path;
20use std::ptr;
2122pub(crate) unsafe fn from_cstr(ptr: *const c_char) -> String {
23let cstr = CStr::from_ptr(ptr as *const _);
24 String::from_utf8_lossy(cstr.to_bytes()).into_owned()
25}
2627pub(crate) unsafe fn raw_data(ptr: *const c_char, size: usize) -> Option<Vec<u8>> {
28if ptr.is_null() {
29None
30} else {
31let mut dst = vec![0; size];
32 ptr::copy_nonoverlapping(ptr as *const u8, dst.as_mut_ptr(), size);
3334Some(dst)
35 }
36}
3738pub fn error_message(ptr: *const c_char) -> String {
39unsafe {
40let s = from_cstr(ptr);
41 ffi::rocksdb_free(ptr as *mut c_void);
42 s
43 }
44}
4546pub fn opt_bytes_to_ptr<T: AsRef<[u8]>>(opt: Option<T>) -> *const c_char {
47match opt {
48Some(v) => v.as_ref().as_ptr() as *const c_char,
49None => ptr::null(),
50 }
51}
5253pub(crate) fn to_cpath<P: AsRef<Path>>(path: P) -> Result<CString, Error> {
54match CString::new(path.as_ref().to_string_lossy().as_bytes()) {
55Ok(c) => Ok(c),
56Err(e) => Err(Error::new(format!(
57"Failed to convert path to CString: {e}"
58))),
59 }
60}
6162macro_rules! ffi_try {
63 ( $($function:ident)::*() ) => {
64ffi_try_impl!($($function)::*())
65 };
6667 ( $($function:ident)::*( $arg1:expr $(, $arg:expr)* $(,)? ) ) => {
68ffi_try_impl!($($function)::*($arg1 $(, $arg)* ,))
69 };
70}
7172macro_rules! ffi_try_impl {
73 ( $($function:ident)::*( $($arg:expr,)*) ) => {{
74let mut err: *mut ::libc::c_char = ::std::ptr::null_mut();
75let result = $($function)::*($($arg,)* &mut err);
76if !err.is_null() {
77return Err(Error::new($crate::ffi_util::error_message(err)));
78 }
79 result
80 }};
81}
8283/// Value which can be converted into a C string.
84///
85/// The trait is used as argument to functions which wish to accept either
86/// [`&str`] or [`&CStr`](CStr) arguments while internally need to interact with
87/// C APIs. Accepting [`&str`] may be more convenient for users but requires
88/// conversion into [`CString`] internally which requires allocation. With this
89/// trait, latency-conscious users may choose to prepare [`CStr`] in advance and
90/// then pass it directly without having to incur the conversion cost.
91///
92/// To use the trait, function should accept `impl CStrLike` and after baking
93/// the argument (with [`CStrLike::bake`] method) it can use it as a [`&CStr`](CStr)
94/// (since the baked result dereferences into [`CStr`]).
95///
96/// # Example
97///
98/// ```
99/// use std::ffi::{CStr, CString};
100/// use rocksdb::CStrLike;
101///
102/// fn strlen(arg: impl CStrLike) -> std::result::Result<usize, String> {
103/// let baked = arg.bake().map_err(|err| err.to_string())?;
104/// Ok(unsafe { libc::strlen(baked.as_ptr()) })
105/// }
106///
107/// const FOO: &str = "foo";
108/// const BAR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"bar\0") };
109///
110/// assert_eq!(Ok(3), strlen(FOO));
111/// assert_eq!(Ok(3), strlen(BAR));
112/// ```
113pub trait CStrLike {
114type Baked: std::ops::Deref<Target = CStr>;
115type Error: std::fmt::Debug + std::fmt::Display;
116117/// Bakes self into value which can be freely converted into [`&CStr`](CStr).
118 ///
119 /// This may require allocation and may fail if `self` has invalid value.
120fn bake(self) -> Result<Self::Baked, Self::Error>;
121122/// Consumers and converts value into an owned [`CString`].
123 ///
124 /// If `Self` is already a `CString` simply returns it; if it’s a reference
125 /// to a `CString` then the value is cloned. In other cases this may
126 /// require allocation and may fail if `self` has invalid value.
127fn into_c_string(self) -> Result<CString, Self::Error>;
128}
129130impl CStrLike for &str {
131type Baked = CString;
132type Error = std::ffi::NulError;
133134fn bake(self) -> Result<Self::Baked, Self::Error> {
135 CString::new(self)
136 }
137fn into_c_string(self) -> Result<CString, Self::Error> {
138 CString::new(self)
139 }
140}
141142// This is redundant for the most part and exists so that `foo(&string)` (where
143// `string: String` works just as if `foo` took `arg: &str` argument.
144impl CStrLike for &String {
145type Baked = CString;
146type Error = std::ffi::NulError;
147148fn bake(self) -> Result<Self::Baked, Self::Error> {
149 CString::new(self.as_bytes())
150 }
151fn into_c_string(self) -> Result<CString, Self::Error> {
152 CString::new(self.as_bytes())
153 }
154}
155156impl CStrLike for &CStr {
157type Baked = Self;
158type Error = std::convert::Infallible;
159160fn bake(self) -> Result<Self::Baked, Self::Error> {
161Ok(self)
162 }
163fn into_c_string(self) -> Result<CString, Self::Error> {
164Ok(self.to_owned())
165 }
166}
167168// This exists so that if caller constructs a `CString` they can pass it into
169// the function accepting `CStrLike` argument. Some of such functions may take
170// the argument whereas otherwise they would need to allocated a new owned
171// object.
172impl CStrLike for CString {
173type Baked = CString;
174type Error = std::convert::Infallible;
175176fn bake(self) -> Result<Self::Baked, Self::Error> {
177Ok(self)
178 }
179fn into_c_string(self) -> Result<CString, Self::Error> {
180Ok(self)
181 }
182}
183184// This is redundant for the most part and exists so that `foo(&cstring)` (where
185// `string: CString` works just as if `foo` took `arg: &CStr` argument.
186impl<'a> CStrLike for &'a CString {
187type Baked = &'a CStr;
188type Error = std::convert::Infallible;
189190fn bake(self) -> Result<Self::Baked, Self::Error> {
191Ok(self)
192 }
193fn into_c_string(self) -> Result<CString, Self::Error> {
194Ok(self.clone())
195 }
196}
197198/// Owned malloc-allocated memory slice.
199/// Do not derive `Clone` for this because it will cause double-free.
200pub struct CSlice {
201 data: *const c_char,
202 len: size_t,
203}
204205impl CSlice {
206/// Constructing such a slice may be unsafe.
207 ///
208 /// # Safety
209 /// The caller must ensure that the pointer and length are valid.
210 /// Moreover, `CSlice` takes the ownership of the memory and will free it
211 /// using `rocksdb_free`. The caller must ensure that the memory is
212 /// allocated by `malloc` in RocksDB and will not be freed by any other
213 /// means.
214pub(crate) unsafe fn from_raw_parts(data: *const c_char, len: size_t) -> Self {
215Self { data, len }
216 }
217}
218219impl AsRef<[u8]> for CSlice {
220fn as_ref(&self) -> &[u8] {
221unsafe { std::slice::from_raw_parts(self.data as *const u8, self.len) }
222 }
223}
224225impl Drop for CSlice {
226fn drop(&mut self) {
227unsafe {
228 ffi::rocksdb_free(self.data as *mut c_void);
229 }
230 }
231}
232233#[test]
234fn test_c_str_like_bake() {
235fn test<S: CStrLike>(value: S) -> Result<usize, S::Error> {
236 value
237 .bake()
238 .map(|value| unsafe { libc::strlen(value.as_ptr()) })
239 }
240241assert_eq!(Ok(3), test("foo")); // &str
242assert_eq!(Ok(3), test(&String::from("foo"))); // String
243assert_eq!(Ok(3), test(CString::new("foo").unwrap().as_ref())); // &CStr
244assert_eq!(Ok(3), test(&CString::new("foo").unwrap())); // &CString
245assert_eq!(Ok(3), test(CString::new("foo").unwrap())); // CString
246247assert_eq!(3, test("foo\0bar").err().unwrap().nul_position());
248}
249250#[test]
251fn test_c_str_like_into() {
252fn test<S: CStrLike>(value: S) -> Result<CString, S::Error> {
253 value.into_c_string()
254 }
255256let want = CString::new("foo").unwrap();
257258assert_eq!(Ok(want.clone()), test("foo")); // &str
259assert_eq!(Ok(want.clone()), test(&String::from("foo"))); // &String
260assert_eq!(
261Ok(want.clone()),
262 test(CString::new("foo").unwrap().as_ref())
263 ); // &CStr
264assert_eq!(Ok(want.clone()), test(&CString::new("foo").unwrap())); // &CString
265assert_eq!(Ok(want), test(CString::new("foo").unwrap())); // CString
266267assert_eq!(3, test("foo\0bar").err().unwrap().nul_position());
268}