fix dc_tools handling of deletion and reading

This commit is contained in:
dignifiedquire
2019-05-02 19:31:49 +02:00
parent 4ed2d1204c
commit 2c69d6e767
2 changed files with 20 additions and 10 deletions

View File

@@ -459,7 +459,7 @@ pub unsafe extern "C" fn stress_functions(context: &dc_context_t) {
); );
assert_eq!(buf_bytes, 7); assert_eq!(buf_bytes, 7);
assert_eq!( assert_eq!(
CStr::from_ptr(buf as *const libc::c_char).to_str().unwrap(), std::str::from_utf8(std::slice::from_raw_parts(buf as *const u8, buf_bytes)).unwrap(),
"content" "content"
); );

View File

@@ -1324,8 +1324,8 @@ pub unsafe fn dc_file_exist(
} }
pub unsafe fn dc_get_filebytes( pub unsafe fn dc_get_filebytes(
mut context: &dc_context_t, context: &dc_context_t,
mut pathNfilename: *const libc::c_char, pathNfilename: *const libc::c_char,
) -> uint64_t { ) -> uint64_t {
let pathNfilename_abs = dc_get_abs_path(context, pathNfilename); let pathNfilename_abs = dc_get_abs_path(context, pathNfilename);
if pathNfilename_abs.is_null() { if pathNfilename_abs.is_null() {
@@ -1344,19 +1344,27 @@ pub unsafe fn dc_get_filebytes(
} }
pub unsafe fn dc_delete_file( pub unsafe fn dc_delete_file(
mut context: &dc_context_t, context: &dc_context_t,
mut pathNfilename: *const libc::c_char, pathNfilename: *const libc::c_char,
) -> libc::c_int { ) -> libc::c_int {
let mut success: libc::c_int = 0i32; let mut success: libc::c_int = 0i32;
let pathNfilename_abs = dc_get_abs_path(context, pathNfilename); let pathNfilename_abs = dc_get_abs_path(context, pathNfilename);
if pathNfilename_abs.is_null() { if pathNfilename_abs.is_null() {
return 0; return 0;
} }
let p = std::ffi::CStr::from_ptr(pathNfilename_abs) let p = std::path::Path::new(
.to_str() std::ffi::CStr::from_ptr(pathNfilename_abs)
.unwrap(); .to_str()
.unwrap(),
);
match fs::remove_file(p) { let res = if p.is_file() {
fs::remove_file(p)
} else {
fs::remove_dir_all(p)
};
match res {
Ok(_) => { Ok(_) => {
success = 1; success = 1;
} }
@@ -1506,8 +1514,10 @@ pub unsafe fn dc_read_file(
match fs::read(p) { match fs::read(p) {
Ok(mut bytes) => { Ok(mut bytes) => {
*buf = bytes.as_mut_ptr() as *mut libc::c_void; *buf = &mut bytes[..] as *mut _ as *mut libc::c_void;
*buf_bytes = bytes.len(); *buf_bytes = bytes.len();
std::mem::forget(bytes);
success = 1; success = 1;
} }
Err(_err) => { Err(_err) => {