From af253296d9a98d9822ab5e3d05f647aa22300b48 Mon Sep 17 00:00:00 2001 From: Sebastian Ramacher Date: Sun, 1 Dec 2019 17:20:51 +0100 Subject: [PATCH] Also use SHA256 of documents to lookup fileinfo --- zathura/database-plain.c | 8 ++++---- zathura/database-sqlite.c | 4 ++-- zathura/database.c | 12 ++++++------ zathura/database.h | 15 ++++++++++----- zathura/zathura.c | 6 ++++-- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/zathura/database-plain.c b/zathura/database-plain.c index 5d22fac..628580d 100644 --- a/zathura/database-plain.c +++ b/zathura/database-plain.c @@ -633,8 +633,8 @@ plain_save_jumplist(zathura_database_t* db, const char* file, girara_list_t* jum } static bool -plain_set_fileinfo(zathura_database_t* db, const char* file, zathura_fileinfo_t* - file_info) +plain_set_fileinfo(zathura_database_t* db, const char* file, const uint8_t* hash_sha256, + zathura_fileinfo_t* file_info) { ZathuraPlainDatabase* plaindb = ZATHURA_PLAINDATABASE(db); ZathuraPlainDatabasePrivate* priv = zathura_plaindatabase_get_instance_private(plaindb); @@ -663,8 +663,8 @@ plain_set_fileinfo(zathura_database_t* db, const char* file, zathura_fileinfo_t* } static bool -plain_get_fileinfo(zathura_database_t* db, const char* file, zathura_fileinfo_t* - file_info) +plain_get_fileinfo(zathura_database_t* db, const char* file, const uint8_t* hash_sha256, + zathura_fileinfo_t* file_info) { if (db == NULL || file == NULL || file_info == NULL) { return false; diff --git a/zathura/database-sqlite.c b/zathura/database-sqlite.c index fe18bc3..e6d6474 100644 --- a/zathura/database-sqlite.c +++ b/zathura/database-sqlite.c @@ -649,7 +649,7 @@ sqlite_load_jumplist(zathura_database_t* db, const char* file) } static bool -sqlite_set_fileinfo(zathura_database_t* db, const char* file, +sqlite_set_fileinfo(zathura_database_t* db, const char* file, const uint8_t* hash_sha256, zathura_fileinfo_t* file_info) { if (db == NULL || file == NULL || file_info == NULL) { @@ -690,7 +690,7 @@ sqlite_set_fileinfo(zathura_database_t* db, const char* file, } static bool -sqlite_get_fileinfo(zathura_database_t* db, const char* file, +sqlite_get_fileinfo(zathura_database_t* db, const char* file, const uint8_t* hash_sha256, zathura_fileinfo_t* file_info) { if (db == NULL || file == NULL || file_info == NULL) { diff --git a/zathura/database.c b/zathura/database.c index a9cf245..06ca538 100644 --- a/zathura/database.c +++ b/zathura/database.c @@ -52,21 +52,21 @@ zathura_db_save_jumplist(zathura_database_t* db, const char* file, girara_list_t } bool -zathura_db_set_fileinfo(zathura_database_t* db, const char* file, +zathura_db_set_fileinfo(zathura_database_t* db, const char* file, const uint8_t* hash_sha256, zathura_fileinfo_t* file_info) { - g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL && file_info != NULL, false); + g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL && hash_sha256 != NULL && file_info != NULL, false); - return ZATHURA_DATABASE_GET_INTERFACE(db)->set_fileinfo(db, file, file_info); + return ZATHURA_DATABASE_GET_INTERFACE(db)->set_fileinfo(db, file, hash_sha256, file_info); } bool -zathura_db_get_fileinfo(zathura_database_t* db, const char* file, +zathura_db_get_fileinfo(zathura_database_t* db, const char* file, const uint8_t* hash_sha256, zathura_fileinfo_t* file_info) { - g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL && file_info != NULL, false); + g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL && hash_sha256 != NULL && file_info != NULL, false); - return ZATHURA_DATABASE_GET_INTERFACE(db)->get_fileinfo(db, file, file_info); + return ZATHURA_DATABASE_GET_INTERFACE(db)->get_fileinfo(db, file, hash_sha256, file_info); } girara_list_t* diff --git a/zathura/database.h b/zathura/database.h index fec8fdd..9723755 100644 --- a/zathura/database.h +++ b/zathura/database.h @@ -4,6 +4,8 @@ #define DATABASE_H #include +#include + #include #include @@ -48,9 +50,9 @@ struct _ZathuraDatabaseInterface bool (*save_jumplist)(ZathuraDatabase* db, const char* file, girara_list_t* jumplist); - bool (*set_fileinfo)(ZathuraDatabase* db, const char* file, zathura_fileinfo_t* file_info); + bool (*set_fileinfo)(ZathuraDatabase* db, const char* file, const uint8_t* hash_sha256, zathura_fileinfo_t* file_info); - bool (*get_fileinfo)(ZathuraDatabase* db, const char* file, zathura_fileinfo_t* file_info); + bool (*get_fileinfo)(ZathuraDatabase* db, const char* file, const uint8_t* hash_sha256, zathura_fileinfo_t* file_info); girara_list_t* (*get_recent_files)(ZathuraDatabase* db, int max, const char* basepath); }; @@ -115,20 +117,23 @@ bool zathura_db_save_jumplist(ZathuraDatabase* db, const char* file, girara_list * * @param db The database instance * @param file The file to which the file info belongs. + * @param hash_sha256 The file's hash * @param file_info The file info * @return true on success, false otherwise. */ -bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, +bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, const uint8_t* hash_sha256, zathura_fileinfo_t* file_info); -/* Get file info (last site, ...) from the database. +/* Get file info (last site, ...) from the database. The info is first looked up by file and then by + * its hash. * * @param db The database instance * @param file The file to which the file info belongs. + * @param hash_sha256 The file's hash * @param file_info The file info * @return true on success, false otherwise. */ -bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file, +bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file, const uint8_t* hash_sha256, zathura_fileinfo_t* file_info); /* Get a list of recent files from the database. The most recent file is listed diff --git a/zathura/zathura.c b/zathura/zathura.c index 5e6334b..ce6da7e 100644 --- a/zathura/zathura.c +++ b/zathura/zathura.c @@ -1006,7 +1006,8 @@ document_open(zathura_t* zathura, const char* path, const char* uri, const char* }; bool known_file = false; if (zathura->database != NULL) { - known_file = zathura_db_get_fileinfo(zathura->database, file_path, &file_info); + const uint8_t* file_hash = zathura_document_get_hash(document); + known_file = zathura_db_get_fileinfo(zathura->database, file_path, file_hash, &file_info); } /* set page offset */ @@ -1360,6 +1361,7 @@ static void save_fileinfo_to_db(zathura_t* zathura) { const char* path = zathura_document_get_path(zathura->document); + const uint8_t* file_hash = zathura_document_get_hash(zathura->document); zathura_fileinfo_t file_info = { .current_page = zathura_document_get_current_page_number(zathura->document), @@ -1381,7 +1383,7 @@ save_fileinfo_to_db(zathura_t* zathura) &(file_info.page_right_to_left)); /* save file info */ - zathura_db_set_fileinfo(zathura->database, path, &file_info); + zathura_db_set_fileinfo(zathura->database, path, file_hash, &file_info); /* save jumplist */ zathura_db_save_jumplist(zathura->database, path, zathura->jumplist.list);