Re: COPY when 'filename' is a directory - Mailing list pgsql-bugs

From Bruce Momjian
Subject Re: COPY when 'filename' is a directory
Date
Msg-id 200202232145.g1NLjkn11672@candle.pha.pa.us
Whole thread Raw
In response to Re: COPY when 'filename' is a directory  (Brent Verner <brent@rcfile.org>)
Responses Re: COPY when 'filename' is a directory
List pgsql-bugs
OK'ed by Peter.

Patch applied.  Thanks.

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


Brent Verner wrote:
> [2002-01-17 07:08] Brent Verner said:
> | [2002-01-16 17:47] Douglas Trainor said:
> | | I'd like to report a bug in 7.1.3 psql (at least on a 64-bit Alpha Linux box).
> | | Maybe someone can confirm this or maybe it's fixed in 7.2
> |
> | The attached patch is for 7.2, but might apply (with some fuzz) to 7.1.x.
> |
> | files:
> |   src/backend/commands/copy.c
> |   src/bin/psql/copy.c
> |
> | note:
> |   Add fstat / S_ISDIR checks to make sure we're not trying to use a directory
> |   for COPY TO/FROM.
>
> ...too early in the AM.  The proper/complete patch is attached this
> time.
>
>   b

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
Index: src/backend/commands/copy.c
===================================================================
RCS file: /var/cvsup/pgsql/src/backend/commands/copy.c,v
retrieving revision 1.144
diff -c -r1.144 copy.c
*** src/backend/commands/copy.c    4 Dec 2001 21:19:57 -0000    1.144
--- src/backend/commands/copy.c    17 Jan 2002 12:24:17 -0000
***************
*** 326,337 ****
          }
          else
          {
              fp = AllocateFile(filename, PG_BINARY_R);
!             if (fp == NULL)
                  elog(ERROR, "COPY command, running in backend with "
                       "effective uid %d, could not open file '%s' for "
                       "reading.  Errno = %s (%d).",
                       (int) geteuid(), filename, strerror(errno), errno);
          }
          CopyFrom(rel, binary, oids, fp, delim, null_print);
      }
--- 326,345 ----
          }
          else
          {
+       struct stat st;
              fp = AllocateFile(filename, PG_BINARY_R);
!
!       if (fp == NULL)
                  elog(ERROR, "COPY command, running in backend with "
                       "effective uid %d, could not open file '%s' for "
                       "reading.  Errno = %s (%d).",
                       (int) geteuid(), filename, strerror(errno), errno);
+
+       fstat(fileno(fp),&st);
+       if( S_ISDIR(st.st_mode) ){
+         fclose(fp);
+         elog(ERROR,"COPY: %s is a directory.",filename);
+       }
          }
          CopyFrom(rel, binary, oids, fp, delim, null_print);
      }
***************
*** 360,365 ****
--- 368,374 ----
          else
          {
              mode_t        oumask; /* Pre-existing umask value */
+       struct stat st;

              /*
               * Prevent write to relative path ... too easy to shoot
***************
*** 378,383 ****
--- 387,397 ----
                       "effective uid %d, could not open file '%s' for "
                       "writing.  Errno = %s (%d).",
                       (int) geteuid(), filename, strerror(errno), errno);
+       fstat(fileno(fp),&st);
+       if( S_ISDIR(st.st_mode) ){
+         fclose(fp);
+         elog(ERROR,"COPY: %s is a directory.",filename);
+       }
          }
          CopyTo(rel, binary, oids, fp, delim, null_print);
      }
Index: src/bin/psql/copy.c
===================================================================
RCS file: /var/cvsup/pgsql/src/bin/psql/copy.c,v
retrieving revision 1.19
diff -c -r1.19 copy.c
*** src/bin/psql/copy.c    2 Jun 2001 18:25:18 -0000    1.19
--- src/bin/psql/copy.c    17 Jan 2002 12:25:07 -0000
***************
*** 11,16 ****
--- 11,17 ----
  #include <errno.h>
  #include <assert.h>
  #include <signal.h>
+ #include <sys/stat.h>
  #ifndef WIN32
  #include <unistd.h>                /* for isatty */
  #else
***************
*** 233,238 ****
--- 234,240 ----
      struct copy_options *options;
      PGresult   *result;
      bool        success;
+   struct stat st;

      /* parse options */
      options = parse_slash_copy(args);
***************
*** 292,298 ****
          free_copy_options(options);
          return false;
      }
!
      result = PSQLexec(query);

      switch (PQresultStatus(result))
--- 294,309 ----
          free_copy_options(options);
          return false;
      }
!   /* make sure the specified file is not a directory */
!   fstat(fileno(copystream),&st);
!   if( S_ISDIR(st.st_mode) ){
!     fclose(copystream);
!         psql_error("%s: cannot COPY TO/FROM a directory\n",
!                    options->file);
!         free_copy_options(options);
!         return false;
!   }
!
      result = PSQLexec(query);

      switch (PQresultStatus(result))

pgsql-bugs by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Bug #597: ResultSet.next() throws NullPointerException
Next
From: Tom Lane
Date:
Subject: Re: COPY when 'filename' is a directory