psql: patch to correct filename formatting error in '\s FILE' output - Mailing list pgadmin-hackers

From Ian Lawrence Barwick
Subject psql: patch to correct filename formatting error in '\s FILE' output
Date
Msg-id CAB8KJ=iPBCemv4XZBwXws5owSp_Sq37V1BjJYS1cAsy2QTqFHQ@mail.gmail.com
Whole thread Raw
Responses Re: psql: patch to correct filename formatting error in '\s FILE' output  (Ian Lawrence Barwick <barwick@gmail.com>)
List pgadmin-hackers
I've noticed a filename error in feedback messages from the psql \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

pgadmin-hackers by date:

Previous
From: Guillaume Lelarge
Date:
Subject: Re: Wrong german translated text
Next
From: Ian Lawrence Barwick
Date:
Subject: Re: psql: patch to correct filename formatting error in '\s FILE' output