Merge history file if it was modified after start

w3m reads the history file on startup and writes it on exit. That means
if you open multiple instances of w3m, the history file will contain the
history of the last instance closed. All other history changes are lost.

Check if the modification time of the history file has changed before
writing. If it has changed read the history file from the disk into a
new history. Push the entries that are in the current history but not in
the history file into the new history and write the new history to disk.
This commit is contained in:
Rene Kita
2022-12-25 15:33:33 +01:00
parent e0c9a02784
commit c77029570d
2 changed files with 36 additions and 0 deletions

View File

@@ -2,6 +2,19 @@
#include "fm.h"
#ifdef USE_HISTORY
/* Merge entries from their history into ours */
static int
mergeHistory(Hist *ours, Hist *theirs)
{
HistItem *item;
for (item = theirs->list->first; item; item = item->next)
if (!getHashHist(ours, item->ptr))
pushHist(ours, (char *)item->ptr);
return 0;
}
Buffer *
historyBuffer(Hist *hist)
{
@@ -36,12 +49,19 @@ loadHistory(Hist *hist)
{
FILE *f;
Str line;
struct stat st;
if (hist == NULL)
return 1;
if ((f = fopen(rcFile(HISTORY_FILE), "rt")) == NULL)
return 1;
if (fstat(fileno(f), &st) == -1) {
fclose(f);
return 1;
}
hist->mtime = (long long)st.st_mtim.tv_sec;
while (!feof(f)) {
line = Strfgets(f);
Strchop(line);
@@ -59,12 +79,27 @@ void
saveHistory(Hist *hist, size_t size)
{
FILE *f;
Hist *fhist;
HistItem *item;
char *histf;
char *tmpf;
int rename_ret;
struct stat st;
if (hist == NULL || hist->list == NULL)
return;
histf = rcFile(HISTORY_FILE);
if (stat(histf, &st) == -1)
goto fail;
if (hist->mtime != (long long)st.st_mtim.tv_sec) {
fhist = newHist();
if (loadHistory(fhist) || mergeHistory(fhist, hist))
disp_err_message("Can't merge history", FALSE);
else
hist = fhist;
}
tmpf = tmpfname(TMPF_HIST, NULL)->ptr;
if ((f = fopen(tmpf, "w")) == NULL)
goto fail;

View File

@@ -16,6 +16,7 @@ typedef struct {
HistList *list;
HistItem *current;
Hash_sv *hash;
long long mtime;
} Hist;
extern Hist *newHist(void);