tweaks: don't construct the positions-history filename time and again

master
Benno Schulenberg 2017-11-02 19:31:02 +01:00
parent bfc53f308c
commit ea47879911
1 changed files with 14 additions and 24 deletions

View File

@ -42,6 +42,8 @@ static bool history_changed = FALSE;
/* Whether any of the history lists has changed. */ /* Whether any of the history lists has changed. */
static struct stat stat_of_positions_file; static struct stat stat_of_positions_file;
/* The last-obtained stat information of the positions file. */ /* The last-obtained stat information of the positions file. */
static char *poshistname = NULL;
/* The name of the positions-history file. */
/* Initialize the lists of historical search and replace strings /* Initialize the lists of historical search and replace strings
* and the list of historical executed commands. */ * and the list of historical executed commands. */
@ -287,6 +289,9 @@ bool have_statedir(void)
return FALSE; return FALSE;
} }
if (ISSET(POS_HISTORY))
poshistname = concatenate(statedir, POSITION_HISTORY);
return TRUE; return TRUE;
} }
@ -388,26 +393,16 @@ void save_history(void)
free(histname); free(histname);
} }
/* Update the modification time of the position history file. */
void update_posfile_timestamp(void)
{
char *poshist = concatenate(statedir, POSITION_HISTORY);
stat(poshist, &stat_of_positions_file);
free(poshist);
}
/* Load the recorded cursor positions for files that were edited. */ /* Load the recorded cursor positions for files that were edited. */
void load_poshistory(void) void load_poshistory(void)
{ {
char *poshist = concatenate(statedir, POSITION_HISTORY); FILE *hisfile = fopen(poshistname, "rb");
FILE *hisfile = fopen(poshist, "rb");
if (hisfile == NULL) { if (hisfile == NULL) {
if (errno != ENOENT) { if (errno != ENOENT) {
/* When reading failed, don't save history when we quit. */ /* When reading failed, don't save history when we quit. */
UNSET(POS_HISTORY); UNSET(POS_HISTORY);
history_error(N_("Error reading %s: %s"), poshist, strerror(errno)); history_error(N_("Error reading %s: %s"), poshistname, strerror(errno));
} }
} else { } else {
char *line = NULL, *lineptr, *xptr; char *line = NULL, *lineptr, *xptr;
@ -460,23 +455,21 @@ void load_poshistory(void)
fclose(hisfile); fclose(hisfile);
free(line); free(line);
update_posfile_timestamp(); stat(poshistname, &stat_of_positions_file);
} }
free(poshist);
} }
/* Save the recorded cursor positions for files that were edited. */ /* Save the recorded cursor positions for files that were edited. */
void save_poshistory(void) void save_poshistory(void)
{ {
char *poshist = concatenate(statedir, POSITION_HISTORY);
poshiststruct *posptr; poshiststruct *posptr;
FILE *hisfile = fopen(poshist, "wb"); FILE *hisfile = fopen(poshistname, "wb");
if (hisfile == NULL) if (hisfile == NULL)
fprintf(stderr, _("Error writing %s: %s\n"), poshist, strerror(errno)); fprintf(stderr, _("Error writing %s: %s\n"), poshistname, strerror(errno));
else { else {
/* Don't allow others to read or write the history file. */ /* Don't allow others to read or write the history file. */
chmod(poshist, S_IRUSR | S_IWUSR); chmod(poshistname, S_IRUSR | S_IWUSR);
for (posptr = position_history; posptr != NULL; posptr = posptr->next) { for (posptr = position_history; posptr != NULL; posptr = posptr->next) {
char *path_and_place; char *path_and_place;
@ -496,24 +489,21 @@ void save_poshistory(void)
if (fwrite(path_and_place, sizeof(char), length, hisfile) < length) if (fwrite(path_and_place, sizeof(char), length, hisfile) < length)
fprintf(stderr, _("Error writing %s: %s\n"), fprintf(stderr, _("Error writing %s: %s\n"),
poshist, strerror(errno)); poshistname, strerror(errno));
free(path_and_place); free(path_and_place);
} }
fclose(hisfile); fclose(hisfile);
update_posfile_timestamp(); stat(poshistname, &stat_of_positions_file);
} }
free(poshist);
} }
/* Reload the position history file if it has been modified since last load. */ /* Reload the position history file if it has been modified since last load. */
void reload_positions_if_needed(void) void reload_positions_if_needed(void)
{ {
char *poshist = concatenate(statedir, POSITION_HISTORY);
struct stat newstat; struct stat newstat;
stat(poshist, &newstat); stat(poshistname, &newstat);
free(poshist);
if (newstat.st_mtime != stat_of_positions_file.st_mtime) { if (newstat.st_mtime != stat_of_positions_file.st_mtime) {
poshiststruct *ptr, *nextone; poshiststruct *ptr, *nextone;