Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
database.c File Reference
#include "system/atomic.h"
#include "common/database.h"
#include "common/datetime.h"
#include "common/file_location.h"
#include "common/global_mutexes.h"
#include "develop/iop_order.h"
#include "common/styles.h"
#include "common/history.h"
#include "common/legacy_presets.h"
#include <gio/gio.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "common/utility.h"
#include "widgets/dialog.h"
+ Include dependency graph for database.c:

Go to the source code of this file.

Data Structures

struct  dt_database_t
 

Macros

#define CURRENT_DATABASE_VERSION_LIBRARY   36
 
#define CURRENT_DATABASE_VERSION_DATA   9
 
#define MAX_NESTED_TRANSACTIONS   0
 
#define _SQLITE3_EXEC(a, b, c, d, e)
 
#define TRY_EXEC(_query, _message)
 
#define TRY_STEP(_stmt, _expected, _message)
 
#define TRY_PREPARE(_stmt, _query, _message)
 
#define FINALIZE
 
#define FINALIZE
 
#define FINALIZE
 
#define FINALIZE
 
#define TRY_EXEC(_query, _message)
 
#define TRY_STEP(_stmt, _expected, _message)
 
#define TRY_PREPARE(_stmt, _query, _message)
 
#define FINALIZE
 
#define ERRCHECK   {if (err!=NULL) {dt_print(DT_DEBUG_SQL, "[db maintenance] maintenance error: '%s'\n",err); sqlite3_free(err); err=NULL;}}
 

Typedefs

typedef struct dt_database_t dt_database_t
 

Functions

static void _database_migrate_to_xdg_structure ()
 
static void _database_delete_mipmaps_files ()
 
static gboolean _migrate_schema (dt_database_t *db, int version)
 
static int _upgrade_library_schema_step (dt_database_t *db, int version)
 
static int _upgrade_data_schema_step (dt_database_t *db, int version)
 
static gboolean _upgrade_library_schema (dt_database_t *db, int version)
 
static gboolean _upgrade_data_schema (dt_database_t *db, int version)
 
static void _create_library_schema (dt_database_t *db)
 
static void _create_data_schema (dt_database_t *db)
 
static void _create_memory_schema (dt_database_t *db)
 
static void _sanitize_db (dt_database_t *db)
 
void dt_database_take_error (dt_database_t *db, dt_database_error_t *error)
 
void dt_database_error_free (dt_database_error_t *error)
 
int dt_database_delete_lock_files (const char *dbfilename)
 
static gboolean pid_is_alive (int pid)
 
static gboolean _lock_single_database (dt_database_t *db, const char *dbfilename, char **lockfile)
 
static gboolean _lock_databases (dt_database_t *db)
 
void ask_for_upgrade (const gchar *dbname, const gboolean has_gui)
 
void dt_database_backup (const char *filename)
 
int _get_pragma_int_val (sqlite3 *db, const char *pragma)
 
gchar * _get_pragma_string_val (sqlite3 *db, const char *pragma)
 
void dt_database_set_prompt_handler (dt_database_prompt_handler_t handler)
 
static dt_database_response_t _ask_user (const dt_database_prompt_t prompt, const char *dbfilename, const char *quick_check, const gboolean snapshot_available)
 
dt_database_tdt_database_init (const char *alternative, const gboolean load_data, const gboolean has_gui)
 
void dt_database_destroy (const dt_database_t *db)
 
sqlite3 * dt_database_get (const dt_database_t *db)
 
const gchar * dt_database_get_path (const struct dt_database_t *db)
 
gboolean dt_database_get_lock_acquired (const dt_database_t *db)
 
void dt_database_cleanup_busy_statements (const struct dt_database_t *db)
 
void dt_database_perform_maintenance (const struct dt_database_t *db)
 
gboolean _ask_for_maintenance (const gboolean has_gui, const gboolean closing_time, const guint64 size)
 
static gboolean _is_mem_db (const struct dt_database_t *db)
 
gboolean dt_database_maybe_maintenance (const struct dt_database_t *db, const gboolean has_gui, const gboolean closing_time)
 
void dt_database_optimize (const struct dt_database_t *db)
 
static void _print_backup_progress (int remaining, int total)
 
static int _backup_db (sqlite3 *src_db, const char *src_db_name, const char *dest_filename, void(*xProgress)(int, int))
 
gboolean dt_database_snapshot (const struct dt_database_t *db)
 
gboolean dt_database_maybe_snapshot (const struct dt_database_t *db)
 
static gboolean _get_iso8601_int (const gchar *text, gsize length, gint *value)
 
static gint _db_snap_sort (gconstpointer a, gconstpointer b, gpointer user_data)
 
char ** dt_database_snaps_to_remove (const struct dt_database_t *db)
 
gchar * dt_database_get_most_recent_snap (const char *db_filename)
 
void dt_database_start_transaction_debug (const struct dt_database_t *db)
 
void dt_database_release_transaction_debug (const struct dt_database_t *db)
 
void dt_database_rollback_transaction (const struct dt_database_t *db)
 
void dt_database_begin_transaction_batch (const struct dt_database_t *db)
 
void dt_database_end_transaction_batch (const struct dt_database_t *db)
 

Variables

static dt_atomic_int _trxid
 
static dt_atomic_int _trx_batch_level
 
static gpointer _trx_owner = NULL
 
static gpointer _trx_batch_owner = NULL
 
static dt_database_prompt_handler_t _prompt_handler = NULL
 

Macro Definition Documentation

◆ _SQLITE3_EXEC

#define _SQLITE3_EXEC (   a,
  b,
  c,
  d,
 
)
Value:
if(sqlite3_exec(a, b, c, d, e) != SQLITE_OK) \
{ \
all_ok = FALSE; \
failing_query = b; \
goto end; \
}
#define FALSE
Definition ashift_lsd.c:158

Definition at line 120 of file database.c.

◆ CURRENT_DATABASE_VERSION_DATA

#define CURRENT_DATABASE_VERSION_DATA   9

Definition at line 86 of file database.c.

◆ CURRENT_DATABASE_VERSION_LIBRARY

#define CURRENT_DATABASE_VERSION_LIBRARY   36

Definition at line 85 of file database.c.

◆ ERRCHECK

#define ERRCHECK   {if (err!=NULL) {dt_print(DT_DEBUG_SQL, "[db maintenance] maintenance error: '%s'\n",err); sqlite3_free(err); err=NULL;}}

Definition at line 3573 of file database.c.

◆ FINALIZE [1/5]

#define FINALIZE

Definition at line 480 of file database.c.

◆ FINALIZE [2/5]

#define FINALIZE
Value:
do \
{ \
sqlite3_finalize(stmt); \
sqlite3_finalize(select_stmt); \
sqlite3_finalize(count_clashes_stmt); \
sqlite3_finalize(update_name_stmt); \
sqlite3_finalize(insert_stmt); \
sqlite3_finalize(delete_stmt); \
} while(0)

Definition at line 480 of file database.c.

◆ FINALIZE [3/5]

#define FINALIZE
Value:
do \
{ \
sqlite3_finalize(stmt); \
sqlite3_finalize(insert_stmt); \
sqlite3_finalize(select_stmt); \
sqlite3_finalize(delete_stmt); \
sqlite3_finalize(update_name_stmt); \
sqlite3_finalize(select_new_stmt); \
sqlite3_finalize(copy_style_items_stmt); \
sqlite3_finalize(delete_style_items_stmt); \
} while(0)

Definition at line 480 of file database.c.

◆ FINALIZE [4/5]

#define FINALIZE

Definition at line 480 of file database.c.

◆ FINALIZE [5/5]

#define FINALIZE
Value:
do \
{ \
sqlite3_finalize(stmt); stmt = NULL; /* NULL so that finalize becomes a NOP */ \
} while(0)

Definition at line 480 of file database.c.

◆ MAX_NESTED_TRANSACTIONS

#define MAX_NESTED_TRANSACTIONS   0

Definition at line 89 of file database.c.

◆ TRY_EXEC [1/2]

#define TRY_EXEC (   _query,
  _message 
)
Value:
do \
{ \
if(sqlite3_exec(db->handle, _query, NULL, NULL, NULL) != SQLITE_OK) \
{ \
fprintf(stderr, _message); \
fprintf(stderr, "[init] %s\n", sqlite3_errmsg(db->handle)); \
sqlite3_exec(db->handle, "ROLLBACK TRANSACTION", NULL, NULL, NULL); \
return version; \
} \
} while(0)
#define FINALIZE
Definition database.c:480

Definition at line 440 of file database.c.

◆ TRY_EXEC [2/2]

#define TRY_EXEC (   _query,
  _message 
)
Value:
do \
{ \
if(sqlite3_exec(db->handle, _query, NULL, NULL, NULL) != SQLITE_OK) \
{ \
fprintf(stderr, _message); \
fprintf(stderr, "[init] %s\n", sqlite3_errmsg(db->handle)); \
sqlite3_exec(db->handle, "ROLLBACK TRANSACTION", NULL, NULL, NULL); \
return FALSE; \
} \
} while(0)

Definition at line 440 of file database.c.

◆ TRY_PREPARE [1/2]

#define TRY_PREPARE (   _stmt,
  _query,
  _message 
)
Value:
do \
{ \
if(sqlite3_prepare_v2(db->handle, _query, -1, &_stmt, NULL) != SQLITE_OK) \
{ \
fprintf(stderr, _message); \
fprintf(stderr, "[init] %s\n", sqlite3_errmsg(db->handle)); \
sqlite3_exec(db->handle, "ROLLBACK TRANSACTION", NULL, NULL, NULL); \
return version; \
} \
} while(0)

Definition at line 466 of file database.c.

◆ TRY_PREPARE [2/2]

#define TRY_PREPARE (   _stmt,
  _query,
  _message 
)
Value:
do \
{ \
if(sqlite3_prepare_v2(db->handle, _query, -1, &_stmt, NULL) != SQLITE_OK) \
{ \
fprintf(stderr, _message); \
fprintf(stderr, "[init] %s\n", sqlite3_errmsg(db->handle)); \
sqlite3_exec(db->handle, "ROLLBACK TRANSACTION", NULL, NULL, NULL); \
return FALSE; \
} \
} while(0)

Definition at line 466 of file database.c.

◆ TRY_STEP [1/2]

#define TRY_STEP (   _stmt,
  _expected,
  _message 
)
Value:
do \
{ \
if(sqlite3_step(_stmt) != _expected) \
{ \
fprintf(stderr, _message); \
fprintf(stderr, "[init] %s\n", sqlite3_errmsg(db->handle)); \
sqlite3_exec(db->handle, "ROLLBACK TRANSACTION", NULL, NULL, NULL); \
return version; \
} \
} while(0)

Definition at line 453 of file database.c.

◆ TRY_STEP [2/2]

#define TRY_STEP (   _stmt,
  _expected,
  _message 
)
Value:
do \
{ \
if(sqlite3_step(_stmt) != _expected) \
{ \
fprintf(stderr, _message); \
fprintf(stderr, "[init] %s\n", sqlite3_errmsg(db->handle)); \
sqlite3_exec(db->handle, "ROLLBACK TRANSACTION", NULL, NULL, NULL); \
return FALSE; \
} \
} while(0)

Definition at line 453 of file database.c.

Typedef Documentation

◆ dt_database_t

typedef struct dt_database_t dt_database_t

Function Documentation

◆ _ask_for_maintenance()

gboolean _ask_for_maintenance ( const gboolean  has_gui,
const gboolean  closing_time,
const guint64  size 
)

◆ _ask_user()

static dt_database_response_t _ask_user ( const dt_database_prompt_t  prompt,
const char *  dbfilename,
const char *  quick_check,
const gboolean  snapshot_available 
)
static

Definition at line 2975 of file database.c.

References _prompt_handler, DT_DATABASE_RESPONSE_CLOSE, and IS_NULL_PTR.

Referenced by dt_database_init().

◆ _backup_db()

static int _backup_db ( sqlite3 *  src_db,
const char *  src_db_name,
const char *  dest_filename,
void(*)(int, int)  xProgress 
)
static

Definition at line 3768 of file database.c.

References _get_pragma_int_val(), DT_DEBUG_SQL, dt_free, dt_print(), MAX, MIN, and void().

Referenced by dt_database_snapshot().

◆ _create_data_schema()

static void _create_data_schema ( dt_database_t db)
static

Definition at line 2462 of file database.c.

References CURRENT_DATABASE_VERSION_DATA, and dt_database_t::handle.

Referenced by dt_database_init().

◆ _create_library_schema()

static void _create_library_schema ( dt_database_t db)
static
Todo:
: make sure to bump CURRENT_DATABASE_VERSION_LIBRARY and add a

Definition at line 2356 of file database.c.

References CURRENT_DATABASE_VERSION_LIBRARY, and dt_database_t::handle.

Referenced by dt_database_init().

◆ _create_memory_schema()

static void _create_memory_schema ( dt_database_t db)
static

Definition at line 2515 of file database.c.

References dt_database_t::handle.

Referenced by dt_database_init().

◆ _database_delete_mipmaps_files()

static void _database_delete_mipmaps_files ( )
static

Definition at line 3531 of file database.c.

References dt_concat_path_file(), dt_loc_get_user_cache_dir(), and PATH_MAX.

Referenced by dt_database_init().

◆ _database_migrate_to_xdg_structure()

static void _database_migrate_to_xdg_structure ( )
static

◆ _db_snap_sort()

static gint _db_snap_sort ( gconstpointer  a,
gconstpointer  b,
gpointer  user_data 
)
static

Definition at line 4023 of file database.c.

References _get_iso8601_int(), day, and IS_NULL_PTR.

Referenced by dt_database_snaps_to_remove().

◆ _get_iso8601_int()

static gboolean _get_iso8601_int ( const gchar *  text,
gsize  length,
gint *  value 
)
static

Definition at line 4003 of file database.c.

References FALSE, i, TRUE, v, and value.

Referenced by _db_snap_sort().

◆ _get_pragma_int_val()

int _get_pragma_int_val ( sqlite3 *  db,
const char *  pragma 
)

Definition at line 2926 of file database.c.

References dt_free.

Referenced by _backup_db(), dt_database_maybe_maintenance(), and dt_database_perform_maintenance().

◆ _get_pragma_string_val()

gchar * _get_pragma_string_val ( sqlite3 *  db,
const char *  pragma 
)

Definition at line 2942 of file database.c.

References dt_free.

Referenced by dt_database_init().

◆ _is_mem_db()

◆ _lock_databases()

◆ _lock_single_database()

static gboolean _lock_single_database ( dt_database_t db,
const char *  dbfilename,
char **  lockfile 
)
static

◆ _migrate_schema()

static gboolean _migrate_schema ( dt_database_t db,
int  version 
)
static

Definition at line 129 of file database.c.

References _SQLITE3_EXEC, dt_free, FALSE, dt_database_t::handle, i, IS_NULL_PTR, name, and TRUE.

Referenced by dt_database_init().

◆ _print_backup_progress()

static void _print_backup_progress ( int  remaining,
int  total 
)
static
Todo:
if we have closing splashpage - this can be used to advance progressbar :)

Definition at line 3762 of file database.c.

References DT_DEBUG_SQL, and dt_print().

Referenced by dt_database_snapshot().

◆ _sanitize_db()

static void _sanitize_db ( dt_database_t db)
static

Definition at line 2559 of file database.c.

References dt_free, dt_util_foo_to_utf8(), and dt_database_t::handle.

Referenced by dt_database_init().

◆ _upgrade_data_schema()

static gboolean _upgrade_data_schema ( dt_database_t db,
int  version 
)
static

Definition at line 2342 of file database.c.

References _upgrade_data_schema_step(), CURRENT_DATABASE_VERSION_DATA, FALSE, and TRUE.

Referenced by dt_database_init().

◆ _upgrade_data_schema_step()

◆ _upgrade_library_schema()

static gboolean _upgrade_library_schema ( dt_database_t db,
int  version 
)
static

Definition at line 2327 of file database.c.

References _upgrade_library_schema_step(), CURRENT_DATABASE_VERSION_LIBRARY, FALSE, and TRUE.

Referenced by dt_database_init().

◆ _upgrade_library_schema_step()

◆ ask_for_upgrade()

void ask_for_upgrade ( const gchar *  dbname,
const gboolean  has_gui 
)

Definition at line 2850 of file database.c.

References dt_free, and dt_gui_show_standalone_yes_no_dialog().

Referenced by dt_database_init().

◆ dt_database_backup()

void dt_database_backup ( const char *  filename)

Definition at line 2882 of file database.c.

References darktable_package_version, dt_free, FALSE, k, and TRUE.

Referenced by dt_database_init().

◆ dt_database_begin_transaction_batch()

◆ dt_database_cleanup_busy_statements()

void dt_database_cleanup_busy_statements ( const struct dt_database_t db)

cleanup busy statements on closing dt, just before performing maintenance

Definition at line 3555 of file database.c.

References DT_DEBUG_SQL, dt_print(), and dt_database_t::handle.

Referenced by dt_cleanup().

◆ dt_database_delete_lock_files()

int dt_database_delete_lock_files ( const char *  dbfilename)

Delete data.db.lock and library.db.lock beside dbfilename. Returns 0 when every lock file that existed was removed. Call only once the user has agreed.

Definition at line 2673 of file database.c.

References dt_free, i, and IS_NULL_PTR.

Referenced by dt_database_show_error().

◆ dt_database_destroy()

◆ dt_database_end_transaction_batch()

◆ dt_database_error_free()

void dt_database_error_free ( dt_database_error_t error)

Release the strings owned by error.

Definition at line 2664 of file database.c.

References dt_free, error(), and IS_NULL_PTR.

Referenced by dt_database_show_error().

◆ dt_database_get()

◆ dt_database_get_lock_acquired()

gboolean dt_database_get_lock_acquired ( const dt_database_t db)

Definition at line 3550 of file database.c.

References dt_database_t::lock_acquired.

Referenced by dt_init().

◆ dt_database_get_most_recent_snap()

gchar * dt_database_get_most_recent_snap ( const char *  db_filename)

get possibly the freshest snapshot to restore

Definition at line 4361 of file database.c.

References DT_DEBUG_SQL, dt_free, dt_print(), error(), and IS_NULL_PTR.

Referenced by dt_database_init().

◆ dt_database_get_path()

const gchar * dt_database_get_path ( const struct dt_database_t db)

Returns database path

Definition at line 3497 of file database.c.

References dt_database_t::dbfilename_library.

Referenced by dt_mipmap_cache_get_filename().

◆ dt_database_init()

◆ dt_database_maybe_maintenance()

gboolean dt_database_maybe_maintenance ( const struct dt_database_t db,
const gboolean  has_gui,
const gboolean  closing_time 
)

conditionally perfrom db maintenance

Definition at line 3675 of file database.c.

References _ask_for_maintenance(), _get_pragma_int_val(), _is_mem_db(), dt_conf_get_int(), dt_conf_get_string_const(), DT_DEBUG_SQL, dt_print(), FALSE, dt_database_t::handle, and TRUE.

Referenced by dt_cleanup(), and dt_init().

◆ dt_database_maybe_snapshot()

gboolean dt_database_maybe_snapshot ( const struct dt_database_t db)

check if creating database snapshot is recommended

Definition at line 3868 of file database.c.

References _is_mem_db(), dt_database_t::dbfilename_library, dt_conf_get_string_const(), DT_DEBUG_SQL, dt_free, dt_print(), error(), FALSE, IS_NULL_PTR, and TRUE.

Referenced by dt_cleanup().

◆ dt_database_optimize()

void dt_database_optimize ( const struct dt_database_t db)

perform pre-db-close optimizations (always call when quiting darktable)

Definition at line 3752 of file database.c.

References _is_mem_db(), DT_DEBUG_SQLITE3_EXEC, and dt_database_t::handle.

Referenced by dt_cleanup().

◆ dt_database_perform_maintenance()

void dt_database_perform_maintenance ( const struct dt_database_t db)

◆ dt_database_release_transaction_debug()

◆ dt_database_rollback_transaction()

◆ dt_database_set_prompt_handler()

void dt_database_set_prompt_handler ( dt_database_prompt_handler_t  handler)

Install the handler dt_database_init() asks through. NULL removes it.

Must be registered BEFORE dt_database_init() runs – which is before dt_gui_gtk_init(), so this one cannot go where the film/collection/folder-survey handlers go. darktable.c registers it, being the only thing that knows this early whether there will be a GUI.

With no handler, every prompt answers CLOSE: a corrupt database is not deleted or restored on the strength of a question nobody was asked. That is also what makes a headless run safe – these dialogs used to be built unconditionally, with no has_gui guard, on a GTK that ansel-cli never initialises.

Definition at line 2968 of file database.c.

References _prompt_handler.

Referenced by dt_database_gui_register_handlers().

◆ dt_database_snaps_to_remove()

char ** dt_database_snaps_to_remove ( const struct dt_database_t db)

get list of snapshot files to remove after successful snapshot

Definition at line 4076 of file database.c.

References _db_snap_sort(), _is_mem_db(), dt_database_t::dbfilename_data, dt_database_t::dbfilename_library, dt_conf_get_int(), DT_DEBUG_SQL, dt_free, dt_print(), error(), FALSE, and IS_NULL_PTR.

Referenced by dt_cleanup().

◆ dt_database_snapshot()

gboolean dt_database_snapshot ( const struct dt_database_t db)

simply create db snapshot of both library and data

Definition at line 3818 of file database.c.

References _backup_db(), _is_mem_db(), _print_backup_progress(), dt_database_t::dbfilename_data, dt_database_t::dbfilename_library, dt_free, FALSE, dt_database_t::handle, and TRUE.

Referenced by backup_database_callback(), and dt_cleanup().

◆ dt_database_start_transaction_debug()

◆ dt_database_take_error()

void dt_database_take_error ( struct dt_database_t db,
dt_database_error_t error 
)

Move the pending error out of db into error, clearing it on the database. Consumes it: a second call reports no error.

Definition at line 2649 of file database.c.

References error(), dt_database_t::error_dbfilename, dt_database_t::error_message, dt_database_t::error_other_pid, IS_NULL_PTR, and dt_database_t::lock_acquired.

Referenced by dt_database_show_error().

◆ pid_is_alive()

static gboolean pid_is_alive ( int  pid)
static

Definition at line 2697 of file database.c.

References dt_free, FALSE, pid_is_alive(), and TRUE.

Referenced by _lock_single_database(), and pid_is_alive().

Variable Documentation

◆ _prompt_handler

dt_database_prompt_handler_t _prompt_handler = NULL
static

Definition at line 2966 of file database.c.

Referenced by _ask_user(), and dt_database_set_prompt_handler().

◆ _trx_batch_level

◆ _trx_batch_owner

◆ _trx_owner

◆ _trxid