mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 17:36:29 +03:00
refactor(tools): use rust-natie for file operations
This commit is contained in:
File diff suppressed because one or more lines are too long
261
src/dc_tools.rs
261
src/dc_tools.rs
@@ -1,3 +1,5 @@
|
|||||||
|
use std::fs;
|
||||||
|
|
||||||
use libc;
|
use libc;
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
|
|
||||||
@@ -1303,6 +1305,10 @@ pub unsafe fn dc_file_exist(
|
|||||||
mut pathNfilename: *const libc::c_char,
|
mut pathNfilename: *const libc::c_char,
|
||||||
) -> libc::c_int {
|
) -> libc::c_int {
|
||||||
let pathNfilename_abs = dc_get_abs_path(context, pathNfilename);
|
let pathNfilename_abs = dc_get_abs_path(context, pathNfilename);
|
||||||
|
if pathNfilename_abs.is_null() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
let exist = {
|
let exist = {
|
||||||
let p = std::path::Path::new(
|
let p = std::path::Path::new(
|
||||||
std::ffi::CStr::from_ptr(pathNfilename_abs)
|
std::ffi::CStr::from_ptr(pathNfilename_abs)
|
||||||
@@ -1322,12 +1328,15 @@ pub unsafe fn dc_get_filebytes(
|
|||||||
mut pathNfilename: *const libc::c_char,
|
mut 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() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
let filebytes = {
|
let filebytes = {
|
||||||
let p = std::ffi::CStr::from_ptr(pathNfilename_abs)
|
let p = std::ffi::CStr::from_ptr(pathNfilename_abs)
|
||||||
.to_str()
|
.to_str()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
std::fs::metadata(p).unwrap().len()
|
fs::metadata(p).unwrap().len()
|
||||||
};
|
};
|
||||||
|
|
||||||
free(pathNfilename_abs as *mut libc::c_void);
|
free(pathNfilename_abs as *mut libc::c_void);
|
||||||
@@ -1339,119 +1348,69 @@ pub unsafe fn dc_delete_file(
|
|||||||
mut pathNfilename: *const libc::c_char,
|
mut 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 mut pathNfilename_abs: *mut libc::c_char = 0 as *mut libc::c_char;
|
let pathNfilename_abs = dc_get_abs_path(context, pathNfilename);
|
||||||
pathNfilename_abs = dc_get_abs_path(context, pathNfilename);
|
if pathNfilename_abs.is_null() {
|
||||||
if !pathNfilename_abs.is_null() {
|
return 0;
|
||||||
if remove(pathNfilename_abs) != 0i32 {
|
}
|
||||||
|
let p = std::ffi::CStr::from_ptr(pathNfilename_abs)
|
||||||
|
.to_str()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
match fs::remove_file(p) {
|
||||||
|
Ok(_) => {
|
||||||
|
success = 1;
|
||||||
|
}
|
||||||
|
Err(_err) => {
|
||||||
dc_log_warning(
|
dc_log_warning(
|
||||||
context,
|
context,
|
||||||
0i32,
|
0i32,
|
||||||
b"Cannot delete \"%s\".\x00" as *const u8 as *const libc::c_char,
|
b"Cannot delete \"%s\".\x00" as *const u8 as *const libc::c_char,
|
||||||
pathNfilename,
|
pathNfilename,
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
success = 1i32
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(pathNfilename_abs as *mut libc::c_void);
|
free(pathNfilename_abs as *mut libc::c_void);
|
||||||
return success;
|
success
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn dc_copy_file(
|
pub unsafe fn dc_copy_file(
|
||||||
mut context: &dc_context_t,
|
context: &dc_context_t,
|
||||||
mut src: *const libc::c_char,
|
src: *const libc::c_char,
|
||||||
mut dest: *const libc::c_char,
|
dest: *const libc::c_char,
|
||||||
) -> libc::c_int {
|
) -> libc::c_int {
|
||||||
let mut current_block: u64;
|
let mut success = 0;
|
||||||
let mut success: libc::c_int = 0i32;
|
|
||||||
let mut src_abs: *mut libc::c_char = 0 as *mut libc::c_char;
|
let src_abs = dc_get_abs_path(context, src);
|
||||||
let mut dest_abs: *mut libc::c_char = 0 as *mut libc::c_char;
|
let dest_abs = dc_get_abs_path(context, dest);
|
||||||
let mut fd_src: libc::c_int = -1i32;
|
|
||||||
let mut fd_dest: libc::c_int = -1i32;
|
if src_abs.is_null() || dest_abs.is_null() {
|
||||||
let mut buf: [libc::c_char; 4096] = [0; 4096];
|
return 0;
|
||||||
let mut bytes_read = 0;
|
}
|
||||||
let mut anything_copied: libc::c_int = 0i32;
|
|
||||||
src_abs = dc_get_abs_path(context, src);
|
let src_p = std::ffi::CStr::from_ptr(src_abs).to_str().unwrap();
|
||||||
if !(src_abs.is_null() || {
|
let dest_p = std::ffi::CStr::from_ptr(dest_abs).to_str().unwrap();
|
||||||
dest_abs = dc_get_abs_path(context, dest);
|
|
||||||
dest_abs.is_null()
|
match fs::copy(src_p, dest_p) {
|
||||||
}) {
|
Ok(_) => {
|
||||||
fd_src = open(src_abs, 0i32);
|
success = 1;
|
||||||
if fd_src < 0i32 {
|
}
|
||||||
|
Err(_) => {
|
||||||
dc_log_error(
|
dc_log_error(
|
||||||
context,
|
context,
|
||||||
0i32,
|
0,
|
||||||
b"Cannot open source file \"%s\".\x00" as *const u8 as *const libc::c_char,
|
b"Cannot copy \"%s\" to \"%s\".\x00" as *const u8 as *const libc::c_char,
|
||||||
src,
|
src,
|
||||||
);
|
|
||||||
} else {
|
|
||||||
fd_dest = open(dest_abs, 0x1i32 | 0x200i32 | 0x800i32, 0o666i32);
|
|
||||||
if fd_dest < 0i32 {
|
|
||||||
dc_log_error(
|
|
||||||
context,
|
|
||||||
0i32,
|
|
||||||
b"Cannot open destination file \"%s\".\x00" as *const u8 as *const libc::c_char,
|
|
||||||
dest,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
loop {
|
|
||||||
bytes_read = read(
|
|
||||||
fd_src,
|
|
||||||
buf.as_mut_ptr() as *mut libc::c_void,
|
|
||||||
4096i32 as size_t,
|
|
||||||
) as size_t;
|
|
||||||
if !(bytes_read > 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if write(fd_dest, buf.as_mut_ptr() as *const libc::c_void, bytes_read)
|
|
||||||
!= bytes_read as isize
|
|
||||||
{
|
|
||||||
dc_log_error(
|
|
||||||
context,
|
|
||||||
0i32,
|
|
||||||
b"Cannot write %i bytes to \"%s\".\x00" as *const u8
|
|
||||||
as *const libc::c_char,
|
|
||||||
bytes_read,
|
|
||||||
dest,
|
dest,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
anything_copied = 1i32
|
|
||||||
}
|
|
||||||
if 0 == anything_copied {
|
|
||||||
close(fd_src);
|
|
||||||
fd_src = -1i32;
|
|
||||||
if dc_get_filebytes(context, src) != 0 {
|
|
||||||
dc_log_error(
|
|
||||||
context,
|
|
||||||
0i32,
|
|
||||||
b"Different size information for \"%s\".\x00" as *const u8
|
|
||||||
as *const libc::c_char,
|
|
||||||
bytes_read,
|
|
||||||
dest,
|
|
||||||
);
|
|
||||||
current_block = 610040589300051390;
|
|
||||||
} else {
|
|
||||||
current_block = 5634871135123216486;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
current_block = 5634871135123216486;
|
|
||||||
}
|
|
||||||
match current_block {
|
|
||||||
610040589300051390 => {}
|
|
||||||
_ => success = 1i32,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if fd_src >= 0i32 {
|
|
||||||
close(fd_src);
|
|
||||||
}
|
|
||||||
if fd_dest >= 0i32 {
|
|
||||||
close(fd_dest);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(src_abs as *mut libc::c_void);
|
free(src_abs as *mut libc::c_void);
|
||||||
free(dest_abs as *mut libc::c_void);
|
free(dest_abs as *mut libc::c_void);
|
||||||
return success;
|
success
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn dc_create_folder(
|
pub unsafe fn dc_create_folder(
|
||||||
mut context: &dc_context_t,
|
mut context: &dc_context_t,
|
||||||
mut pathNfilename: *const libc::c_char,
|
mut pathNfilename: *const libc::c_char,
|
||||||
@@ -1485,24 +1444,28 @@ pub unsafe fn dc_create_folder(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn dc_write_file(
|
pub unsafe fn dc_write_file(
|
||||||
mut context: &dc_context_t,
|
context: &dc_context_t,
|
||||||
mut pathNfilename: *const libc::c_char,
|
pathNfilename: *const libc::c_char,
|
||||||
mut buf: *const libc::c_void,
|
buf: *const libc::c_void,
|
||||||
mut buf_bytes: size_t,
|
buf_bytes: size_t,
|
||||||
) -> libc::c_int {
|
) -> libc::c_int {
|
||||||
let mut f: *mut libc::FILE = 0 as *mut libc::FILE;
|
let mut success = 0;
|
||||||
let mut success: libc::c_int = 0i32;
|
let pathNfilename_abs = dc_get_abs_path(context, pathNfilename);
|
||||||
let mut pathNfilename_abs: *mut libc::c_char = 0 as *mut libc::c_char;
|
if pathNfilename_abs.is_null() {
|
||||||
pathNfilename_abs = dc_get_abs_path(context, pathNfilename);
|
return 0;
|
||||||
if !pathNfilename_abs.is_null() {
|
}
|
||||||
f = fopen(
|
|
||||||
pathNfilename_abs,
|
let p = std::ffi::CStr::from_ptr(pathNfilename_abs)
|
||||||
b"wb\x00" as *const u8 as *const libc::c_char,
|
.to_str()
|
||||||
);
|
.unwrap();
|
||||||
if !f.is_null() {
|
|
||||||
if fwrite(buf, 1, buf_bytes, f) == buf_bytes {
|
let bytes = std::slice::from_raw_parts(buf as *const u8, buf_bytes);
|
||||||
success = 1i32
|
|
||||||
} else {
|
match fs::write(p, bytes) {
|
||||||
|
Ok(_) => {
|
||||||
|
success = 1;
|
||||||
|
}
|
||||||
|
Err(_err) => {
|
||||||
dc_log_warning(
|
dc_log_warning(
|
||||||
context,
|
context,
|
||||||
0i32,
|
0i32,
|
||||||
@@ -1511,71 +1474,56 @@ pub unsafe fn dc_write_file(
|
|||||||
pathNfilename,
|
pathNfilename,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
fclose(f);
|
|
||||||
} else {
|
|
||||||
dc_log_warning(
|
|
||||||
context,
|
|
||||||
0i32,
|
|
||||||
b"Cannot open \"%s\" for writing.\x00" as *const u8 as *const libc::c_char,
|
|
||||||
pathNfilename,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(pathNfilename_abs as *mut libc::c_void);
|
free(pathNfilename_abs as *mut libc::c_void);
|
||||||
return success;
|
success
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn dc_read_file(
|
pub unsafe fn dc_read_file(
|
||||||
mut context: &dc_context_t,
|
context: &dc_context_t,
|
||||||
mut pathNfilename: *const libc::c_char,
|
pathNfilename: *const libc::c_char,
|
||||||
mut buf: *mut *mut libc::c_void,
|
buf: *mut *mut libc::c_void,
|
||||||
mut buf_bytes: *mut size_t,
|
buf_bytes: *mut size_t,
|
||||||
) -> libc::c_int {
|
) -> libc::c_int {
|
||||||
let mut success: libc::c_int = 0i32;
|
let mut success = 0;
|
||||||
let mut pathNfilename_abs: *mut libc::c_char = 0 as *mut libc::c_char;
|
|
||||||
let mut f: *mut libc::FILE = 0 as *mut libc::FILE;
|
|
||||||
if pathNfilename.is_null() || buf.is_null() || buf_bytes.is_null() {
|
if pathNfilename.is_null() || buf.is_null() || buf_bytes.is_null() {
|
||||||
return 0i32;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
*buf = 0 as *mut libc::c_void;
|
*buf = 0 as *mut libc::c_void;
|
||||||
*buf_bytes = 0i32 as size_t;
|
*buf_bytes = 0i32 as size_t;
|
||||||
pathNfilename_abs = dc_get_abs_path(context, pathNfilename);
|
|
||||||
if !pathNfilename_abs.is_null() {
|
let pathNfilename_abs = dc_get_abs_path(context, pathNfilename);
|
||||||
f = fopen(
|
if pathNfilename_abs.is_null() {
|
||||||
pathNfilename_abs,
|
return 0;
|
||||||
b"rb\x00" as *const u8 as *const libc::c_char,
|
|
||||||
);
|
|
||||||
if !f.is_null() {
|
|
||||||
fseek(f, 0, 2i32);
|
|
||||||
*buf_bytes = ftell(f) as size_t;
|
|
||||||
fseek(f, 0, 0i32);
|
|
||||||
if !(*buf_bytes <= 0) {
|
|
||||||
*buf = malloc((*buf_bytes).wrapping_add(1));
|
|
||||||
if !(*buf).is_null() {
|
|
||||||
*(*buf as *mut libc::c_char).offset(*buf_bytes as isize) = 0i32 as libc::c_char;
|
|
||||||
if !(fread(*buf, 1, *buf_bytes, f) != *buf_bytes) {
|
|
||||||
success = 1i32
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let p = std::ffi::CStr::from_ptr(pathNfilename_abs)
|
||||||
|
.to_str()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
match fs::read(p) {
|
||||||
|
Ok(mut bytes) => {
|
||||||
|
*buf = bytes.as_mut_ptr() as *mut libc::c_void;
|
||||||
|
*buf_bytes = bytes.len();
|
||||||
|
success = 1;
|
||||||
}
|
}
|
||||||
}
|
Err(_err) => {
|
||||||
}
|
|
||||||
}
|
|
||||||
if !f.is_null() {
|
|
||||||
fclose(f);
|
|
||||||
}
|
|
||||||
if success == 0i32 {
|
|
||||||
free(*buf);
|
|
||||||
*buf = 0 as *mut libc::c_void;
|
|
||||||
*buf_bytes = 0i32 as size_t;
|
|
||||||
dc_log_warning(
|
dc_log_warning(
|
||||||
context,
|
context,
|
||||||
0i32,
|
0,
|
||||||
b"Cannot read \"%s\" or file is empty.\x00" as *const u8 as *const libc::c_char,
|
b"Cannot read \"%s\" or file is empty.\x00" as *const u8 as *const libc::c_char,
|
||||||
pathNfilename,
|
pathNfilename,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
free(pathNfilename_abs as *mut libc::c_void);
|
free(pathNfilename_abs as *mut libc::c_void);
|
||||||
return success;
|
success
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn dc_get_fine_pathNfilename(
|
pub unsafe fn dc_get_fine_pathNfilename(
|
||||||
mut context: &dc_context_t,
|
mut context: &dc_context_t,
|
||||||
mut pathNfolder: *const libc::c_char,
|
mut pathNfolder: *const libc::c_char,
|
||||||
@@ -1632,6 +1580,7 @@ pub unsafe fn dc_get_fine_pathNfilename(
|
|||||||
free(pathNfolder_wo_slash as *mut libc::c_void);
|
free(pathNfolder_wo_slash as *mut libc::c_void);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn dc_is_blobdir_path(
|
pub unsafe fn dc_is_blobdir_path(
|
||||||
mut context: &dc_context_t,
|
mut context: &dc_context_t,
|
||||||
mut path: *const libc::c_char,
|
mut path: *const libc::c_char,
|
||||||
@@ -1643,6 +1592,7 @@ pub unsafe fn dc_is_blobdir_path(
|
|||||||
}
|
}
|
||||||
return 0i32;
|
return 0i32;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn dc_make_rel_path(mut context: &dc_context_t, mut path: *mut *mut libc::c_char) {
|
pub unsafe fn dc_make_rel_path(mut context: &dc_context_t, mut path: *mut *mut libc::c_char) {
|
||||||
if path.is_null() || (*path).is_null() {
|
if path.is_null() || (*path).is_null() {
|
||||||
return;
|
return;
|
||||||
@@ -1655,6 +1605,7 @@ pub unsafe fn dc_make_rel_path(mut context: &dc_context_t, mut path: *mut *mut l
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn dc_make_rel_and_copy(
|
pub unsafe fn dc_make_rel_and_copy(
|
||||||
mut context: &dc_context_t,
|
mut context: &dc_context_t,
|
||||||
mut path: *mut *mut libc::c_char,
|
mut path: *mut *mut libc::c_char,
|
||||||
|
|||||||
Reference in New Issue
Block a user