>From 2f75e0d7b3800faee1c7bffe76b06ba36e67c305 Mon Sep 17 00:00:00 2001 From: Brand Huntsman Date: Fri, 20 Oct 2017 21:26:20 -0600 Subject: [PATCH] prevent position history overwrites across multiple instances of nano Signed-off-by: Brand Huntsman --- src/global.c | 1 + src/history.c | 38 ++++++++++++++++++++++++++++++++++++++ src/nano.c | 1 - src/proto.h | 2 +- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/global.c b/src/global.c index 13a138dd..f846759b 100644 --- a/src/global.c +++ b/src/global.c @@ -221,6 +221,7 @@ filestruct *execute_history = NULL; filestruct *executetop = NULL; filestruct *executebot = NULL; +struct stat position_history_stat; poshiststruct *position_history = NULL; /* The list of filenames with their last cursor positions. */ #endif diff --git a/src/history.c b/src/history.c index 11cf6c20..95644153 100644 --- a/src/history.c +++ b/src/history.c @@ -378,6 +378,14 @@ void save_history(void) free(histname); } +/* Update modification time of position history file. */ +void update_poshistory_timestamp(void) +{ + char *poshist = poshistfilename(); + stat(poshist, &position_history_stat); + free(poshist); +} + /* Load the recorded cursor positions for files that were edited. */ void load_poshistory(void) { @@ -440,6 +448,8 @@ void load_poshistory(void) } fclose(hisfile); free(line); + + update_poshistory_timestamp(); } free(poshist); } @@ -479,10 +489,32 @@ void save_poshistory(void) free(path_and_place); } fclose(hisfile); + + update_poshistory_timestamp(); } free(poshist); } +/* Re-load position history file if it has been modified since last load. */ +void reload_poshistory(void) +{ + struct stat st; + char *poshist = poshistfilename(); + stat(poshist, &st); + free(poshist); + if (position_history_stat.st_mtime < st.st_mtime) { + poshiststruct *posptr, *posnext; + for (posptr = position_history; posptr != NULL; posptr = posnext) { + posnext = posptr->next; + free(posptr->filename); + free(posptr); + } + position_history = NULL; + + load_poshistory(); + } +} + /* Update the recorded last file positions, given a filename, a line * and a column. If no entry is found, add a new one at the end. */ void update_poshistory(char *filename, ssize_t lineno, ssize_t xpos) @@ -495,6 +527,8 @@ void update_poshistory(char *filename, ssize_t lineno, ssize_t xpos) return; } + reload_poshistory(); + /* Look for a matching filename in the list. */ for (posptr = position_history; posptr != NULL; posptr = posptr->next) { if (!strcmp(posptr->filename, fullpath)) @@ -543,6 +577,8 @@ void update_poshistory(char *filename, ssize_t lineno, ssize_t xpos) theone->next = NULL; free(fullpath); + + save_poshistory(); } /* Check whether the given file matches an existing entry in the recorded @@ -556,6 +592,8 @@ bool has_old_position(const char *file, ssize_t *line, ssize_t *column) if (fullpath == NULL) return FALSE; + reload_poshistory(); + while (posptr != NULL && strcmp(posptr->filename, fullpath) != 0) posptr = posptr->next; diff --git a/src/nano.c b/src/nano.c index b3888d9d..5cc7e94b 100644 --- a/src/nano.c +++ b/src/nano.c @@ -570,7 +570,6 @@ void finish(void) save_history(); if (ISSET(POS_HISTORY)) { update_poshistory(openfile->filename, openfile->current->lineno, xplustabs() + 1); - save_poshistory(); } #endif diff --git a/src/proto.h b/src/proto.h index 2bc7fe7a..160591be 100644 --- a/src/proto.h +++ b/src/proto.h @@ -168,6 +168,7 @@ extern filestruct *replacebot; extern filestruct *execute_history; extern filestruct *executetop; extern filestruct *executebot; +extern struct stat position_history_stat; extern poshiststruct *position_history; #endif @@ -368,7 +369,6 @@ bool have_dotnano(void); void load_history(void); void save_history(void); void load_poshistory(void); -void save_poshistory(void); void update_poshistory(char *filename, ssize_t lineno, ssize_t xpos); bool has_old_position(const char *file, ssize_t *line, ssize_t *column); #endif -- 2.13.6