summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Momjian1998-08-29 18:19:59 +0000
committerBruce Momjian1998-08-29 18:19:59 +0000
commitf1d0e64ff995e37205b7309c0b3be231acf0e349 (patch)
tree7c2dcde7d3eaf12d33c9149da120080a1db1147a
parent88800aac14c54f595d288be0e1fac8720f5f5b5d (diff)
Prevent file descriptor leak from failed COPY.
-rw-r--r--src/backend/commands/copy.c22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index 070be8861c2..fa016c7353d 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -6,7 +6,7 @@
*
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.56 1998/08/29 05:27:15 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.57 1998/08/29 18:19:59 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -108,12 +108,21 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
the class.
----------------------------------------------------------------------------*/
- FILE *fp;
+ static FILE *fp; /* static for cleanup */
+ static bool file_opened = false; /* static for cleanup */
Relation rel;
extern char *UserName; /* defined in global.c */
const AclMode required_access = from ? ACL_WR : ACL_RD;
int result;
+ /*
+ * Close previous file opened for COPY but failed with elog().
+ * There should be a better way, but would not be modular.
+ * Prevents file descriptor leak. bjm 1998/08/29
+ */
+ if (file_opened)
+ FreeFile(fp);
+
rel = heap_openr(relname);
if (rel == NULL)
elog(ERROR, "COPY command failed. Class %s "
@@ -146,14 +155,13 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
}
else
{
- /* if we elog() out, the file stays open */
fp = AllocateFile(filename, "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).",
geteuid(), filename, strerror(errno), errno);
- /* Above should not return */
+ file_opened = true;
}
CopyFrom(rel, binary, oids, fp, delim);
}
@@ -174,7 +182,6 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
mode_t oumask; /* Pre-existing umask value */
oumask = umask((mode_t) 0);
- /* if we elog() out, the file stays open */
fp = AllocateFile(filename, "w");
umask(oumask);
if (fp == NULL)
@@ -182,12 +189,15 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
"effective uid %d, could not open file '%s' for "
"writing. Errno = %s (%d).",
geteuid(), filename, strerror(errno), errno);
- /* Above should not return */
+ file_opened = true;
}
CopyTo(rel, binary, oids, fp, delim);
}
if (!pipe)
+ {
FreeFile(fp);
+ file_opened = false;
+ }
else if (!from && !binary)
{
fputs("\\.\n", fp);