Only reload if data files were changed (replacement)

This is a replacement for commits 57dd3d6 and 912124b.

The idea is to move the check for modified files and the list initialization
into io_load_data(), and let io_load_data() decide what to load. A new
argument is used to force a load.

The return code from new_data() (the renamed version of
io_check_data_files_modified()) tells which files have changed.

Signed-off-by: Lars Henriksen <LarsHenriksen@get2net.dk>
Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lars Henriksen 2018-08-23 20:45:38 +02:00 committed by Lukas Fleischer
parent ab0fe68225
commit 5ad0019b23
4 changed files with 68 additions and 57 deletions

View File

@ -738,7 +738,7 @@ int parse_args(int argc, char **argv)
io_check_file(path_todo); io_check_file(path_todo);
io_check_file(path_conf); io_check_file(path_conf);
config_load(); /* To get output date format. */ config_load(); /* To get output date format. */
io_load_data(&filter); io_load_data(&filter, FORCE);
if (grep_filter) { if (grep_filter) {
io_save_todo(path_todo); io_save_todo(path_todo);
io_save_apts(path_apts); io_save_apts(path_apts);
@ -760,7 +760,7 @@ int parse_args(int argc, char **argv)
io_check_file(path_todo); io_check_file(path_todo);
io_check_file(path_conf); io_check_file(path_conf);
config_load(); /* To get output date format. */ config_load(); /* To get output date format. */
io_load_data(&filter); io_load_data(&filter, FORCE);
/* Use default values for non-specified format strings. */ /* Use default values for non-specified format strings. */
fmt_apt = fmt_apt ? fmt_apt : " - %S -> %E\n\t%m\n"; fmt_apt = fmt_apt ? fmt_apt : " - %S -> %E\n\t%m\n";
@ -779,13 +779,12 @@ int parse_args(int argc, char **argv)
} else if (gc) { } else if (gc) {
io_check_file(path_apts); io_check_file(path_apts);
io_check_file(path_todo); io_check_file(path_todo);
io_load_data(NULL); io_load_data(NULL, FORCE);
note_gc(); note_gc();
} else if (import) { } else if (import) {
io_check_file(path_apts); io_check_file(path_apts);
io_check_file(path_todo); io_check_file(path_todo);
/* Get default pager in case we need to show a log file. */ io_load_data(NULL, FORCE);
io_load_data(NULL);
if (dump_imported) { if (dump_imported) {
/* /*
* Use default values for non-specified format strings. * Use default values for non-specified format strings.
@ -809,7 +808,7 @@ int parse_args(int argc, char **argv)
} else if (export) { } else if (export) {
io_check_file(path_apts); io_check_file(path_apts);
io_check_file(path_todo); io_check_file(path_todo);
io_load_data(&filter); io_load_data(&filter, FORCE);
io_export_data(xfmt, export_uid); io_export_data(xfmt, export_uid);
} else if (daemon) { } else if (daemon) {
notify_init_vars(); notify_init_vars();

View File

@ -551,15 +551,6 @@ int main(int argc, char **argv)
textdomain(PACKAGE); textdomain(PACKAGE);
#endif /* ENABLE_NLS */ #endif /* ENABLE_NLS */
/* Thread-safe data structure init */
apoint_llist_init();
recur_apoint_llist_init();
/* Initialize non-thread-safe data structures. */
event_llist_init();
recur_event_llist_init();
todo_init_list();
/* /*
* Begin by parsing and handling command line arguments. * Begin by parsing and handling command line arguments.
* The data path is also initialized here. * The data path is also initialized here.
@ -627,11 +618,7 @@ int main(int argc, char **argv)
config_load(); config_load();
wins_erase_status_bar(); wins_erase_status_bar();
io_load_keys(conf.pager); io_load_keys(conf.pager);
io_load_data(NULL, FORCE);
run_hook("pre-load");
io_load_data(NULL);
run_hook("post-load");
wins_slctd_set(conf.default_panel); wins_slctd_set(conf.default_panel);
wins_resize(); wins_resize();
/* /*

View File

@ -172,6 +172,8 @@
/* Mnemonics */ /* Mnemonics */
#define NOHILT 0 /* 'No highlight' argument */ #define NOHILT 0 /* 'No highlight' argument */
#define NOFORCE 0
#define FORCE 1
#define ERROR_MSG(...) do { \ #define ERROR_MSG(...) do { \
char msg[BUFSIZ]; \ char msg[BUFSIZ]; \
@ -858,7 +860,7 @@ unsigned io_save_keys(void);
int io_save_cal(enum save_display); int io_save_cal(enum save_display);
void io_load_app(struct item_filter *); void io_load_app(struct item_filter *);
void io_load_todo(struct item_filter *); void io_load_todo(struct item_filter *);
void io_load_data(struct item_filter *); void io_load_data(struct item_filter *, int);
int io_reload_data(void); int io_reload_data(void);
void io_load_keys(const char *); void io_load_keys(const char *);
int io_check_dir(const char *); int io_check_dir(const char *);

View File

@ -503,7 +503,7 @@ static int resolve_save_conflict(void)
{ {
char *msg_um_asktype = NULL; char *msg_um_asktype = NULL;
const char *msg_um_prefix = const char *msg_um_prefix =
_("Data files were changed since loaded:"); _("Data have changed since loaded:");
const char *msg_um_overwrite = _("(o)verwrite"); const char *msg_um_overwrite = _("(o)verwrite");
const char *msg_um_merge = _("(m)erge"); const char *msg_um_merge = _("(m)erge");
const char *msg_um_keep = _("(c)ancel"); const char *msg_um_keep = _("(c)ancel");
@ -532,25 +532,37 @@ static int resolve_save_conflict(void)
return ret; return ret;
} }
static int io_check_data_files_modified() /* Return codes for new_data(). */
#define NONEW 0
#define APTS 1
#define TODO 2
#define APTS_TODO 3
#define NOKNOW 4
static int new_data()
{ {
char sha1_new[SHA1_DIGESTLEN * 2 + 1]; char sha1_new[SHA1_DIGESTLEN * 2 + 1];
int ret = 1; int ret = NONEW;
io_mutex_lock(); io_mutex_lock();
if (io_compute_hash(path_apts, sha1_new)) { if (io_compute_hash(path_apts, sha1_new)) {
if (strncmp(sha1_new, apts_sha1, SHA1_DIGESTLEN * 2) != 0) if (strncmp(sha1_new, apts_sha1, SHA1_DIGESTLEN * 2) != 0) {
goto cleanup; ret |= APTS;
}
} else {
ret = NOKNOW;
goto cleanup;
} }
if (io_compute_hash(path_todo, sha1_new)) { if (io_compute_hash(path_todo, sha1_new)) {
if (strncmp(sha1_new, todo_sha1, SHA1_DIGESTLEN * 2) != 0) if (strncmp(sha1_new, todo_sha1, SHA1_DIGESTLEN * 2) != 0) {
goto cleanup; ret |= TODO;
}
} else {
ret = NOKNOW;
goto cleanup;
} }
ret = 0;
cleanup: cleanup:
io_mutex_unlock(); io_mutex_unlock();
return ret; return ret;
@ -574,7 +586,7 @@ int io_save_cal(enum save_display display)
if (read_only) if (read_only)
return IO_SAVE_CANCEL; return IO_SAVE_CANCEL;
if (io_check_data_files_modified() && (ret = resolve_save_conflict())) if (new_data() && (ret = resolve_save_conflict()))
return ret; return ret;
run_hook("pre-save"); run_hook("pre-save");
@ -905,14 +917,42 @@ void io_load_todo(struct item_filter *filter)
file_close(data_file, __FILE_POS__); file_close(data_file, __FILE_POS__);
} }
/* Load appointments and todo items */ /*
void io_load_data(struct item_filter *filter) * Load appointments and todo items.
* Unless told otherwise, the function will only load a file that has changed
* since last saved or loaded, see new_data() return codes.
*/
void io_load_data(struct item_filter *filter, int force)
{ {
run_hook("pre-load");
if (force)
force = APTS_TODO;
else
force = new_data();
io_mutex_lock(); io_mutex_lock();
io_load_app(filter);
io_load_todo(filter); if (force & APTS) {
io_mutex_unlock(); apoint_llist_free();
event_llist_free();
recur_apoint_llist_free();
recur_event_llist_free();
apoint_llist_init();
event_llist_init();
recur_apoint_llist_init();
recur_event_llist_init();
io_load_app(filter);
}
if (force & TODO) {
todo_free_list();
todo_init_list();
io_load_todo(filter);
}
io_unset_modified(); io_unset_modified();
io_mutex_unlock();
run_hook("post-load");
} }
int io_reload_data(void) int io_reload_data(void)
@ -922,6 +962,7 @@ int io_reload_data(void)
_("The data files were reloaded successfully"); _("The data files were reloaded successfully");
const char *enter = _("Press [ENTER] to continue"); const char *enter = _("Press [ENTER] to continue");
int ret = 0; int ret = 0;
int load = NOFORCE;
if (io_get_modified()) { if (io_get_modified()) {
const char *msg_um_prefix = const char *msg_um_prefix =
@ -936,9 +977,11 @@ int io_reload_data(void)
switch (status_ask_choice(msg_um_asktype, msg_um_choice, 3)) { switch (status_ask_choice(msg_um_asktype, msg_um_choice, 3)) {
case 1: case 1:
load = FORCE;
break; break;
case 2: case 2:
io_merge_data(); io_merge_data();
load = FORCE;
break; break;
case 3: case 3:
/* FALLTHROUGH */ /* FALLTHROUGH */
@ -947,27 +990,7 @@ int io_reload_data(void)
} }
} }
run_hook("pre-load"); io_load_data(NULL, load);
if (!io_check_data_files_modified())
goto cleanup;
/* Reinitialize data structures. */
apoint_llist_free();
event_llist_free();
recur_apoint_llist_free();
recur_event_llist_free();
todo_free_list();
apoint_llist_init();
event_llist_init();
recur_apoint_llist_init();
recur_event_llist_init();
todo_init_list();
io_load_data(NULL);
run_hook("post-load");
if (show_dialogs()) { if (show_dialogs()) {
status_mesg(reload_success, enter); status_mesg(reload_success, enter);