Re: [pgsql-hackers-win32] Re : Win32 binaries test / pg_dump problem - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: [pgsql-hackers-win32] Re : Win32 binaries test / pg_dump problem
Date
Msg-id 200407110058.i6B0w0v25179@candle.pha.pa.us
Whole thread Raw
Responses Re: [pgsql-hackers-win32] Re : Win32 binaries test / pg_dump problem
Re: [pgsql-hackers-win32] Re : Win32 binaries test / pg_dump problem
List pgsql-patches
OK, the attached applied patch opens text files in binary mode in psql.
As you said, it already handles CRLF OK, and we need this to read
control-Z in copy files.

I assume we can write control-Z in text mode just fine, right?

---------------------------------------------------------------------------

Andrew Dunstan wrote:
> Bruce Momjian wrote:
>
> >Andrew Dunstan wrote:
> >
> >
> >>Cyril VELTER said:
> >>
> >>
> >>>From: "Andrew Dunstan" <andrew@dunslane.net>
> >>>
> >>>
> >>>
> >>>>Cyril VELTER wrote:
> >>>>
> >>>>
> >>>>
> >>>>>    if you psql -f test.sql basetest from the windows shell to a
> >>>>>    windows
> >>>>>
> >>>>>
> >>>or
> >>>
> >>>
> >>>>>linux database, you'll get a missing data error :
> >>>>>
> >>>>>psql:test.sql:9: ERROR:  missing data for column "b"
> >>>>>CONTEXT:  COPY test, line 1: "a"
> >>>>>
> >>>>>    if you psql -f test.sql basetest from a linux shell to a windows
> >>>>>    or
> >>>>>linux database, you won't get an error
> >>>>>
> >>>>>
> >>>>Sounds like we should have psql open the file in binary mode on
> >>>>Windows. Would that cause problems? I doubt it, but I wonder.
> >>>>
> >>>>
> >>>>
> >>>   you might be right, I just found some information on msdn that fseek
> >>>   for
> >>>example is influenced by ctrl-z when the file is opened in text mode.
> >>>
> >>>   I'm not sure that this is the cause of the second problem (backend
> >>>   crash
> >>>on copy to) though.
> >>>
> >>>   do you known where this modification needs to be done ?
> >>>
> >>>   cyril
> >>>
> >>>
> >>>
> >>probably in src/bin/psql/command.c::process_file()
> >>
> >>instead of mode "r" we should probably use the predefined constant
> >>PG_BINARY_R
> >>
> >>
> >
> >Uh, but it isn't a binary file, it is SQL commands.
> >
> >
> >
>
> If it can have an embedded ^Z it is not a legal Windows text file.
>
> Adding the binary flag will have exactly 2 effects:
> 1. It will inhibit the behaviour of the driver in translating CRLF to
> plain LF
> 2. It will not see ^Z as EOF.
>
> We don't care about the first - we handle CRLF just fine. But we do care
> about the second, and we don't want the library to interpret ^Z as EOF.
>
> cheers
>
> andrew
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
>       subscribe-nomail command to majordomo@postgresql.org so that your
>       message can get through to the mailing list cleanly
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/bin/psql/command.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/command.c,v
retrieving revision 1.117
diff -c -c -r1.117 command.c
*** src/bin/psql/command.c    18 Jun 2004 06:14:04 -0000    1.117
--- src/bin/psql/command.c    11 Jul 2004 00:53:36 -0000
***************
*** 1197,1203 ****
      if (!error)
      {
  #endif
!         stream = fopen(fname, "r");
          if (!stream)
          {
              psql_error("%s: %s\n", fname, strerror(errno));
--- 1197,1203 ----
      if (!error)
      {
  #endif
!         stream = fopen(fname, R_TEXTFILE);
          if (!stream)
          {
              psql_error("%s: %s\n", fname, strerror(errno));
***************
*** 1262,1268 ****
      if (!filename)
          return false;

!     fd = fopen(filename, "r");

      if (!fd)
      {
--- 1262,1268 ----
      if (!filename)
          return false;

!     fd = fopen(filename, R_TEXTFILE);

      if (!fd)
      {
Index: src/bin/psql/common.h
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/common.h,v
retrieving revision 1.35
diff -c -c -r1.35 common.h
*** src/bin/psql/common.h    19 Apr 2004 17:42:58 -0000    1.35
--- src/bin/psql/common.h    11 Jul 2004 00:53:36 -0000
***************
*** 62,65 ****
--- 62,77 ----

  extern char *expand_tilde(char **filename);

+ /*
+  *    WIN32 treats Control-Z as EOF in files opened in text mode.
+  *    Therefore, we open files in binary mode on Win32 so we can read
+  *    literal control-Z.  The other affect is that we see CRLF, but
+  *    that is OK because we can already handle those cleanly.
+  */
+ #ifndef WIN32
+ #define R_TEXTFILE    "r"
+ #else
+ #define R_TEXTFILE    "rb"
+ #endif
+
  #endif   /* COMMON_H */
Index: src/bin/psql/copy.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/copy.c,v
retrieving revision 1.47
diff -c -c -r1.47 copy.c
*** src/bin/psql/copy.c    7 May 2004 00:24:58 -0000    1.47
--- src/bin/psql/copy.c    11 Jul 2004 00:53:37 -0000
***************
*** 516,522 ****
      if (options->from)
      {
          if (options->file)
!             copystream = fopen(options->file, "r");
          else if (!options->psql_inout)
               copystream = pset.cur_cmd_source;
          else
--- 516,522 ----
      if (options->from)
      {
          if (options->file)
!             copystream = fopen(options->file, R_TEXTFILE);
          else if (!options->psql_inout)
               copystream = pset.cur_cmd_source;
          else
Index: src/bin/psql/psqlscan.l
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/psqlscan.l,v
retrieving revision 1.3
diff -c -c -r1.3 psqlscan.l
*** src/bin/psql/psqlscan.l    7 May 2004 00:24:58 -0000    1.3
--- src/bin/psql/psqlscan.l    11 Jul 2004 00:53:38 -0000
***************
*** 1284,1290 ****
                  char        buf[512];
                  size_t        result;

!                 fd = popen(cmd, "r");
                  if (!fd)
                  {
                      psql_error("%s: %s\n", cmd, strerror(errno));
--- 1284,1290 ----
                  char        buf[512];
                  size_t        result;

!                 fd = popen(cmd, R_TEXTFILE);
                  if (!fd)
                  {
                      psql_error("%s: %s\n", cmd, strerror(errno));

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: nested xacts and phantom Xids
Next
From: Bruce Momjian
Date:
Subject: Re: Updated ALTER TABLE ... SET TABLESPACE patch