text
stringlengths
0
357
int namespc_len = strlen(namespc);
if (name_len<=namespc_len
|| strcmp(&name[name_len-namespc_len], namespc)!=0) {
return 0;
}
name_to_check[name_len-namespc_len] = 0;
}
int ret = (dc_hash_find_str(files_in_use, name_to_check)!=NULL);
free(name_to_check);
return ret;
}
void dc_housekeeping(dc_context_t* context)
{
sqlite3_stmt* stmt = NULL;
DIR* dir_handle = NULL;
struct dirent* dir_entry = NULL;
dc_hash_t files_in_use;
char* path = NULL;
int unreferenced_count = 0;
dc_hash_init(&files_in_use, DC_HASH_STRING, DC_HASH_COPY_KEY);
dc_log_info(context, 0, "Start housekeeping...");
/* collect all files in use */
maybe_add_from_param(context, &files_in_use,
"SELECT param FROM msgs "
" WHERE chat_id!=" DC_STRINGIFY(DC_CHAT_ID_TRASH)
" AND type!=" DC_STRINGIFY(DC_MSG_TEXT) ";",
DC_PARAM_FILE);
maybe_add_from_param(context, &files_in_use,
"SELECT param FROM jobs;",
DC_PARAM_FILE);
maybe_add_from_param(context, &files_in_use,
"SELECT param FROM chats;",
DC_PARAM_PROFILE_IMAGE);
maybe_add_from_param(context, &files_in_use,
"SELECT param FROM contacts;",
DC_PARAM_PROFILE_IMAGE);
stmt = dc_sqlite3_prepare(context->sql,
"SELECT value FROM config;");
while (sqlite3_step(stmt)==SQLITE_ROW) {
maybe_add_file(&files_in_use, (const char*)sqlite3_column_text(stmt, 0));
}
dc_log_info(context, 0, "%i files in use.", (int)dc_hash_cnt(&files_in_use));
/* go through directory and delete unused files */
if ((dir_handle=opendir(context->blobdir))==NULL) {
dc_log_warning(context, 0, "Housekeeping: Cannot open %s.", context->blobdir);
goto cleanup;
}
/* avoid deletion of files that are just created to build a message object */
time_t keep_files_newer_than = time(NULL) - 60*60;
while ((dir_entry=readdir(dir_handle))!=NULL)
{
const char* name = dir_entry->d_name; /* name without path or `.` or `..` */
int name_len = strlen(name);
if ((name_len==1 && name[0]=='.')
|| (name_len==2 && name[0]=='.' && name[1]=='.')) {
continue;
}
if (is_file_in_use(&files_in_use, NULL, name)
|| is_file_in_use(&files_in_use, ".increation", name)
|| is_file_in_use(&files_in_use, ".waveform", name)
|| is_file_in_use(&files_in_use, "-preview.jpg", name)) {
continue;
}
unreferenced_count++;
free(path);
path = dc_mprintf("%s/%s", context->blobdir, name);
struct stat st;
if (stat(path, &st)==0) {
if (st.st_mtime > keep_files_newer_than
|| st.st_atime > keep_files_newer_than
|| st.st_ctime > keep_files_newer_than) {
dc_log_info(context, 0,
"Housekeeping: Keeping new unreferenced file #%i: %s",
unreferenced_count, name);
continue;
}
}
dc_log_info(context, 0,
"Housekeeping: Deleting unreferenced file #%i: %s",
unreferenced_count, name);
dc_delete_file(context, path);