text
stringlengths
0
357
free(curr_pathNfilename);
free(buf);
return success;
}
/*******************************************************************************
* Import backup
******************************************************************************/
static int import_backup(dc_context_t* context, const char* backup_to_import)
{
int success = 0;
int processed_files_cnt = 0;
int total_files_cnt = 0;
sqlite3_stmt* stmt = NULL;
char* pathNfilename = NULL;
char* repl_from = NULL;
char* repl_to = NULL;
dc_log_info(context, 0, "Import \"%s\" to \"%s\".", backup_to_import, context->dbfile);
if (dc_is_configured(context)) {
dc_log_error(context, 0, "Cannot import backups to accounts in use.");
goto cleanup;
}
/* close and delete the original file - FIXME: we should import to a .bak file and rename it on success. however, currently it is not clear it the import exists in the long run (may be replaced by a restore-from-imap) */
if (dc_sqlite3_is_open(context->sql)) {
dc_sqlite3_close(context->sql);
}
dc_delete_file(context, context->dbfile);
if (dc_file_exist(context, context->dbfile)) {
dc_log_error(context, 0, "Cannot import backups: Cannot delete the old file.");
goto cleanup;
}
/* copy the database file */
if (!dc_copy_file(context, backup_to_import, context->dbfile)) {
goto cleanup; /* error already logged */
}
/* re-open copied database file */
if (!dc_sqlite3_open(context->sql, context->dbfile, 0)) {
goto cleanup;
}
/* copy all blobs to files */
stmt = dc_sqlite3_prepare(context->sql, "SELECT COUNT(*) FROM backup_blobs;");
sqlite3_step(stmt);
total_files_cnt = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
stmt = NULL;
stmt = dc_sqlite3_prepare(context->sql, "SELECT file_name, file_content FROM backup_blobs ORDER BY id;");
while (sqlite3_step(stmt)==SQLITE_ROW)
{
if (context->shall_stop_ongoing) {
goto cleanup;
}
FILE_PROGRESS
const char* file_name = (const char*)sqlite3_column_text (stmt, 0);
int file_bytes = sqlite3_column_bytes(stmt, 1);
const void* file_content = sqlite3_column_blob (stmt, 1);
if (file_bytes > 0 && file_content) {
free(pathNfilename);
pathNfilename = dc_mprintf("%s/%s", context->blobdir, file_name);
if (!dc_write_file(context, pathNfilename, file_content, file_bytes)) {
dc_log_error(context, 0, "Storage full? Cannot write file %s with %i bytes.", pathNfilename, file_bytes);
goto cleanup; /* otherwise the user may believe the stuff is imported correctly, but there are files missing ... */
}
}
}
/* finalize/reset all statements - otherwise the table cannot be DROPped below */
sqlite3_finalize(stmt);
stmt = 0;
dc_sqlite3_execute(context->sql, "DROP TABLE backup_blobs;");
dc_sqlite3_try_execute(context->sql, "VACUUM;");
success = 1;
cleanup:
free(pathNfilename);
free(repl_from);
free(repl_to);
sqlite3_finalize(stmt);
return success;
}