text
stringlengths
0
357
dc_log_info(context, 0, "Backup \"%s\" to \"%s\".", context->dbfile, dest_pathNfilename);
if (!dc_copy_file(context, context->dbfile, dest_pathNfilename)) {
goto cleanup; /* error already logged */
}
dc_sqlite3_open(context->sql, context->dbfile, 0);
closed = 0;
/* add all files as blobs to the database copy (this does not require the source to be locked, neigher the destination as it is used only here) */
if ((dest_sql=dc_sqlite3_new(context/*for logging only*/))==NULL
|| !dc_sqlite3_open(dest_sql, dest_pathNfilename, 0)) {
goto cleanup; /* error already logged */
}
if (!dc_sqlite3_table_exists(dest_sql, "backup_blobs")) {
if (!dc_sqlite3_execute(dest_sql, "CREATE TABLE backup_blobs (id INTEGER PRIMARY KEY, file_name, file_content);")) {
goto cleanup; /* error already logged */
}
}
/* scan directory, pass 1: collect file info */
total_files_cnt = 0;
if ((dir_handle=opendir(context->blobdir))==NULL) {
dc_log_error(context, 0, "Backup: Cannot get info for blob-directory \"%s\".", context->blobdir);
goto cleanup;
}
while ((dir_entry=readdir(dir_handle))!=NULL) {
total_files_cnt++;
}
closedir(dir_handle);
dir_handle = NULL;
if (total_files_cnt>0)
{
/* scan directory, pass 2: copy files */
if ((dir_handle=opendir(context->blobdir))==NULL) {
dc_log_error(context, 0, "Backup: Cannot copy from blob-directory \"%s\".", context->blobdir);
goto cleanup;
}
stmt = dc_sqlite3_prepare(dest_sql, "INSERT INTO backup_blobs (file_name, file_content) VALUES (?, ?);");
while ((dir_entry=readdir(dir_handle))!=NULL)
{
if (context->shall_stop_ongoing) {
delete_dest_file = 1;
goto cleanup;
}
FILE_PROGRESS
char* name = dir_entry->d_name; /* name without path; may also be `.` or `..` */
int name_len = strlen(name);
if ((name_len==1 && name[0]=='.')
|| (name_len==2 && name[0]=='.' && name[1]=='.')
|| (name_len > prefix_len && strncmp(name, DC_BAK_PREFIX, prefix_len)==0 && name_len > suffix_len && strncmp(&name[name_len-suffix_len-1], "." DC_BAK_SUFFIX, suffix_len)==0)) {
//dc_log_info(context, 0, "Backup: Skipping \"%s\".", name);
continue;
}
//dc_log_info(context, 0, "Backup \"%s\".", name);
free(curr_pathNfilename);
curr_pathNfilename = dc_mprintf("%s/%s", context->blobdir, name);
free(buf);
if (!dc_read_file(context, curr_pathNfilename, &buf, &buf_bytes) || buf==NULL || buf_bytes<=0) {
continue;
}
sqlite3_bind_text(stmt, 1, name, -1, SQLITE_STATIC);
sqlite3_bind_blob(stmt, 2, buf, buf_bytes, SQLITE_STATIC);
if (sqlite3_step(stmt)!=SQLITE_DONE) {
dc_log_error(context, 0, "Disk full? Cannot add file \"%s\" to backup.", curr_pathNfilename);
goto cleanup; /* this is not recoverable! writing to the sqlite database should work! */
}
sqlite3_reset(stmt);
}
}
else
{
dc_log_info(context, 0, "Backup: No files to copy.", context->blobdir);
}
/* done - set some special config values (do this last to avoid importing crashed backups) */
dc_sqlite3_set_config_int(dest_sql, "backup_time", now);
context->cb(context, DC_EVENT_IMEX_FILE_WRITTEN, (uintptr_t)dest_pathNfilename, 0);
success = 1;
cleanup:
if (dir_handle) { closedir(dir_handle); }
if (closed) { dc_sqlite3_open(context->sql, context->dbfile, 0); }
sqlite3_finalize(stmt);
dc_sqlite3_close(dest_sql);
dc_sqlite3_unref(dest_sql);
if (delete_dest_file) { dc_delete_file(context, dest_pathNfilename); }
free(dest_pathNfilename);