psql: small patch to correct filename formatting error in '\s FILE' output

From: Ian Lawrence Barwick <barwick(at)gmail(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: psql: small patch to correct filename formatting error in '\s FILE' output
Date: 2013-01-21 07:58:13
Message-ID: CAB8KJ=hTb89+gMF84E22F37bObrkRDbiP5zEJfw5atWJX7UdLQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I've noticed a filename error in feedback messages from psql's '\s' command
when saving the command line history to a file specified by an absolute
filepath:

psql (9.2.2)
Type "help" for help.

pgdevel=# \s history.txt
Wrote history to file "./history.txt".
pgdevel=# \s /tmp/history.txt
Wrote history to file ".//tmp/history.txt".
pgdevel=# \cd /tmp
pgdevel=# \s /tmp/history.txt
Wrote history to file "/tmp//tmp/history.txt".

The second and third '\s' commands display incorrect filepaths in the feedback
message, despite writing correctly to the specified file.

Also, if the specified file could not be written to, the error message displayed
formats the filepath differently (i.e. it does not prepend the current working
directory), which is potentially confusing, and certainly visually inconsistent:

pgdevel=# \cd /tmp
pgdevel=# \s foo/history.txt
could not save history to file "foo/history.txt": No such file or directory
pgdevel=# \! mkdir foo
pgdevel=# \s foo/history.txt
Wrote history to file "/tmp/foo/history.txt".

The attached patch rectifies these issues by adding a small function
'format_fname()' to psql/stringutils.c which formats the filepath
appropriately, depending on whether an absolute filepath was supplied
or psql's cwd is set.

pgdevel_head=# \s history.txt
Wrote history to file "./history.txt".
pgdevel_head=# \s /tmp/history.txt
Wrote history to file "/tmp/history.txt".
pgdevel_head=# \cd /tmp
pgdevel_head=# \s /tmp/history.txt
Wrote history to file "/tmp/history.txt".

pgdevel_head=# \cd /tmp
pgdevel_head=# \s bar/history.txt
could not save history to file "/tmp/bar/history.txt": No such file
or directory
pgdevel_head=# \! mkdir bar
pgdevel_head=# \s bar/history.txt
Wrote history to file "/tmp/bar/history.txt".

Notes/caveats
- The function 'format_fname()' deterimines whether the supplied filepath is
absolute by checking for the presence of a '/' as the first character. This
strikes me as a bit hacky but I can't think of an alternative.
- As far as I can tell, Windows does not support the '\s' command, so there is
presumably no need to worry about supporting Windows-style file paths
- As far as I can tell, this is the only psql slash command which, after saving
data to a file, provides a feedback message containing the filename/path.

Regards

Ian Lawrence Barwick

Attachment Content-Type Size
psql-save-history-2013-01-21.patch application/octet-stream 17.9 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2013-01-21 08:01:55 Re: Re: Proposal for Allow postgresql.conf values to be changed via SQL [review]
Previous Message Heikki Linnakangas 2013-01-21 07:48:45 Re: CF3+4 (was Re: Parallel query execution)