Commit df9ebf1e authored by Tom Lane's avatar Tom Lane

Remove workaround for ancient incompatibility between readline and libedit.

GNU readline defines the return value of write_history() as "zero if OK,
else an errno code".  libedit's version of that function used to have a
different definition (to wit, "-1 if error, else the number of lines
written to the file").  We tried to work around that by checking whether
errno had become nonzero, but this method has never been kosher according
to the published API of either library.  It's reportedly completely broken
in recent Ubuntu releases: psql bleats about "No such file or directory"
when saving ~/.psql_history, even though the write worked fine.

However, libedit has been following the readline definition since somewhere
around 2006, so it seems all right to finally break compatibility with
ancient libedit releases and trust that the return value is what readline
specifies.  (I'm not sure when the various Linux distributions incorporated
this fix, but I did find that OS X has been shipping fixed versions since
10.5/Leopard.)

If anyone is still using such an ancient libedit, they will find that psql
complains it can't write ~/.psql_history at exit, even when the file was
written correctly.  This is no worse than the behavior we're fixing for
current releases.

Back-patch to all supported branches.
parent 364c006c
...@@ -382,6 +382,7 @@ static bool ...@@ -382,6 +382,7 @@ static bool
saveHistory(char *fname, int max_lines) saveHistory(char *fname, int max_lines)
{ {
#ifdef USE_READLINE #ifdef USE_READLINE
int errnum;
/* /*
* Suppressing the write attempt when HISTFILE is set to /dev/null may * Suppressing the write attempt when HISTFILE is set to /dev/null may
...@@ -405,10 +406,6 @@ saveHistory(char *fname, int max_lines) ...@@ -405,10 +406,6 @@ saveHistory(char *fname, int max_lines)
* history from other concurrent sessions (although there are still * history from other concurrent sessions (although there are still
* race conditions when two sessions exit at about the same time). If * race conditions when two sessions exit at about the same time). If
* we don't have those functions, fall back to write_history(). * we don't have those functions, fall back to write_history().
*
* Note: return value of write_history is not standardized across GNU
* readline and libedit. Therefore, check for errno becoming set to
* see if the write failed. Similarly for append_history.
*/ */
#if defined(HAVE_HISTORY_TRUNCATE_FILE) && defined(HAVE_APPEND_HISTORY) #if defined(HAVE_HISTORY_TRUNCATE_FILE) && defined(HAVE_APPEND_HISTORY)
{ {
...@@ -430,9 +427,8 @@ saveHistory(char *fname, int max_lines) ...@@ -430,9 +427,8 @@ saveHistory(char *fname, int max_lines)
nlines = Min(max_lines, history_lines_added); nlines = Min(max_lines, history_lines_added);
else else
nlines = history_lines_added; nlines = history_lines_added;
errno = 0; errnum = append_history(nlines, fname);
(void) append_history(nlines, fname); if (errnum == 0)
if (errno == 0)
return true; return true;
} }
#else /* don't have append support */ #else /* don't have append support */
...@@ -441,15 +437,14 @@ saveHistory(char *fname, int max_lines) ...@@ -441,15 +437,14 @@ saveHistory(char *fname, int max_lines)
if (max_lines >= 0) if (max_lines >= 0)
stifle_history(max_lines); stifle_history(max_lines);
/* ... and overwrite file. Tough luck for concurrent sessions. */ /* ... and overwrite file. Tough luck for concurrent sessions. */
errno = 0; errnum = write_history(fname);
(void) write_history(fname); if (errnum == 0)
if (errno == 0)
return true; return true;
} }
#endif #endif
psql_error("could not save history to file \"%s\": %s\n", psql_error("could not save history to file \"%s\": %s\n",
fname, strerror(errno)); fname, strerror(errnum));
} }
#endif #endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment