mirror of
https://github.com/chatmail/core.git
synced 2026-05-07 17:06:35 +03:00
Merge pull request #263 from link2xt/sql-is_file_in_use-safe
sql.rs: make is_file_in_use() safe
This commit is contained in:
86
src/sql.rs
86
src/sql.rs
@@ -1007,35 +1007,15 @@ pub fn housekeeping(context: &Context) {
|
|||||||
}
|
}
|
||||||
let entry = entry.unwrap();
|
let entry = entry.unwrap();
|
||||||
let name_f = entry.file_name();
|
let name_f = entry.file_name();
|
||||||
let name_c = unsafe { to_cstring(name_f.to_string_lossy()) };
|
let name_s = name_f.to_string_lossy();
|
||||||
|
|
||||||
if unsafe { is_file_in_use(&mut files_in_use, 0 as *const libc::c_char, name_c) }
|
if is_file_in_use(&mut files_in_use, None, &name_s)
|
||||||
|| unsafe {
|
|| is_file_in_use(&mut files_in_use, Some(".increation"), &name_s)
|
||||||
is_file_in_use(
|
|| is_file_in_use(&mut files_in_use, Some(".waveform"), &name_s)
|
||||||
&mut files_in_use,
|
|| is_file_in_use(&mut files_in_use, Some("-preview.jpg"), &name_s)
|
||||||
b".increation\x00" as *const u8 as *const libc::c_char,
|
|
||||||
name_c,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|| unsafe {
|
|
||||||
is_file_in_use(
|
|
||||||
&mut files_in_use,
|
|
||||||
b".waveform\x00" as *const u8 as *const libc::c_char,
|
|
||||||
name_c,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|| unsafe {
|
|
||||||
is_file_in_use(
|
|
||||||
&mut files_in_use,
|
|
||||||
b"-preview.jpg\x00" as *const u8 as *const libc::c_char,
|
|
||||||
name_c,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
{
|
{
|
||||||
unsafe { free(name_c as *mut _) };
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
unsafe { free(name_c as *mut _) };
|
|
||||||
|
|
||||||
unreferenced_count += 1;
|
unreferenced_count += 1;
|
||||||
|
|
||||||
@@ -1089,26 +1069,18 @@ pub fn housekeeping(context: &Context) {
|
|||||||
info!(context, 0, "Housekeeping done.",);
|
info!(context, 0, "Housekeeping done.",);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn is_file_in_use(
|
fn is_file_in_use(files_in_use: &HashSet<String>, namespc_opt: Option<&str>, name: &str) -> bool {
|
||||||
files_in_use: &HashSet<String>,
|
let name_to_check = if let Some(namespc) = namespc_opt {
|
||||||
namespc: *const libc::c_char,
|
let name_len = name.len();
|
||||||
name: *const libc::c_char,
|
let namespc_len = namespc.len();
|
||||||
) -> bool {
|
if name_len <= namespc_len || !name.ends_with(namespc) {
|
||||||
let name_to_check = dc_strdup(name);
|
|
||||||
if !namespc.is_null() {
|
|
||||||
let name_len: libc::c_int = strlen(name) as libc::c_int;
|
|
||||||
let namespc_len: libc::c_int = strlen(namespc) as libc::c_int;
|
|
||||||
if name_len <= namespc_len
|
|
||||||
|| strcmp(&*name.offset((name_len - namespc_len) as isize), namespc) != 0
|
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
*name_to_check.offset((name_len - namespc_len) as isize) = 0 as libc::c_char
|
&name[..name_len - namespc_len]
|
||||||
}
|
} else {
|
||||||
|
name
|
||||||
let contains = files_in_use.contains(as_str(name_to_check));
|
};
|
||||||
free(name_to_check as *mut libc::c_void);
|
files_in_use.contains(name_to_check)
|
||||||
contains
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn maybe_add_file(files_in_use: &mut HashSet<String>, file: impl AsRef<str>) {
|
fn maybe_add_file(files_in_use: &mut HashSet<String>, file: impl AsRef<str>) {
|
||||||
@@ -1162,26 +1134,12 @@ mod test {
|
|||||||
maybe_add_file(&mut files, "$BLOBDIR/world.txt");
|
maybe_add_file(&mut files, "$BLOBDIR/world.txt");
|
||||||
maybe_add_file(&mut files, "world2.txt");
|
maybe_add_file(&mut files, "world2.txt");
|
||||||
|
|
||||||
assert!(unsafe {
|
assert!(is_file_in_use(&mut files, None, "hello"));
|
||||||
is_file_in_use(
|
assert!(!is_file_in_use(&mut files, Some(".txt"), "hello"));
|
||||||
&mut files,
|
assert!(is_file_in_use(
|
||||||
std::ptr::null(),
|
&mut files,
|
||||||
b"hello\x00" as *const u8 as *const _,
|
Some("-suffix"),
|
||||||
)
|
"world.txt-suffix"
|
||||||
});
|
));
|
||||||
assert!(!unsafe {
|
|
||||||
is_file_in_use(
|
|
||||||
&mut files,
|
|
||||||
b".txt\x00" as *const u8 as *const _,
|
|
||||||
b"hello\x00" as *const u8 as *const _,
|
|
||||||
)
|
|
||||||
});
|
|
||||||
assert!(unsafe {
|
|
||||||
is_file_in_use(
|
|
||||||
&mut files,
|
|
||||||
b"-suffix\x00" as *const u8 as *const _,
|
|
||||||
b"world.txt-suffix\x00" as *const u8 as *const _,
|
|
||||||
)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user