Fix bogus failure-return value from lo_create, per report from Gavin

Sherry.  Also clean up leakage of open files and LOs in failure exits
from lo_import and lo_export.
This commit is contained in:
Tom Lane 2001-09-17 20:05:47 +00:00
parent 4f63a0e101
commit a839258db5

View File

@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.38 2001/08/21 20:39:54 momjian Exp $ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.39 2001/09/17 20:05:47 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -18,9 +18,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#include "libpq-fe.h"
#include "libpq-int.h"
#ifdef WIN32 #ifdef WIN32
#include "win32.h" #include "win32.h"
#include "io.h" #include "io.h"
@ -28,12 +25,16 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#include "libpq-fe.h"
#include "libpq-int.h"
#include "libpq/libpq-fs.h" /* must come after sys/stat.h */ #include "libpq/libpq-fs.h" /* must come after sys/stat.h */
#define LO_BUFSIZE 8192 #define LO_BUFSIZE 8192
static int lo_initialize(PGconn *conn); static int lo_initialize(PGconn *conn);
/* /*
* lo_open * lo_open
* opens an existing large object * opens an existing large object
@ -123,7 +124,7 @@ lo_close(PGconn *conn, int fd)
* lo_read * lo_read
* read len bytes of the large object into buf * read len bytes of the large object into buf
* *
* returns the length of bytes read. * returns the number of bytes read, or -1 on failure.
* the CALLER must have allocated enough space to hold the result returned * the CALLER must have allocated enough space to hold the result returned
*/ */
@ -166,6 +167,7 @@ lo_read(PGconn *conn, int fd, char *buf, size_t len)
* lo_write * lo_write
* write len bytes of buf into the large object fd * write len bytes of buf into the large object fd
* *
* returns the number of bytes written, or -1 on failure.
*/ */
int int
lo_write(PGconn *conn, int fd, char *buf, size_t len) lo_write(PGconn *conn, int fd, char *buf, size_t len)
@ -273,7 +275,7 @@ lo_creat(PGconn *conn, int mode)
if (conn->lobjfuncs == (PGlobjfuncs *) NULL) if (conn->lobjfuncs == (PGlobjfuncs *) NULL)
{ {
if (lo_initialize(conn) < 0) if (lo_initialize(conn) < 0)
return -1; return InvalidOid;
} }
argv[0].isint = 1; argv[0].isint = 1;
@ -373,9 +375,9 @@ lo_unlink(PGconn *conn, Oid lobjId)
/* /*
* lo_import - * lo_import -
* imports a file as an (inversion) large object. * imports a file as an (inversion) large object.
* returns the oid of that object upon success,
* returns InvalidOid upon failure
* *
* returns the oid of that object upon success,
* returns InvalidOid upon failure
*/ */
Oid Oid
@ -409,6 +411,7 @@ lo_import(PGconn *conn, const char *filename)
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not create large object for file \"%s\"\n"), libpq_gettext("could not create large object for file \"%s\"\n"),
filename); filename);
(void) close(fd);
return InvalidOid; return InvalidOid;
} }
@ -418,6 +421,7 @@ lo_import(PGconn *conn, const char *filename)
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not open large object %u\n"), libpq_gettext("could not open large object %u\n"),
lobjOid); lobjOid);
(void) close(fd);
return InvalidOid; return InvalidOid;
} }
@ -432,6 +436,8 @@ lo_import(PGconn *conn, const char *filename)
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("error while reading file \"%s\"\n"), libpq_gettext("error while reading file \"%s\"\n"),
filename); filename);
(void) close(fd);
(void) lo_close(conn, lobj);
return InvalidOid; return InvalidOid;
} }
} }
@ -457,7 +463,7 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
int lobj; int lobj;
/* /*
* create an inversion "object" * open the large object.
*/ */
lobj = lo_open(conn, lobjId, INV_READ); lobj = lo_open(conn, lobjId, INV_READ);
if (lobj == -1) if (lobj == -1)
@ -468,7 +474,7 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
} }
/* /*
* open the file to be written to * create the file to be written to
*/ */
fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666); fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666);
if (fd < 0) if (fd < 0)
@ -476,7 +482,8 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("could not open file \"%s\": %s\n"), libpq_gettext("could not open file \"%s\": %s\n"),
filename, strerror(errno)); filename, strerror(errno));
return 0; (void) lo_close(conn, lobj);
return -1;
} }
/* /*
@ -490,6 +497,8 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
printfPQExpBuffer(&conn->errorMessage, printfPQExpBuffer(&conn->errorMessage,
libpq_gettext("error while writing to file \"%s\"\n"), libpq_gettext("error while writing to file \"%s\"\n"),
filename); filename);
(void) lo_close(conn, lobj);
(void) close(fd);
return -1; return -1;
} }
} }