88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.38 2001/08/21 20:39:54 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-lobj.c,v 1.39 2001/09/17 20:05:47 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
1818#include <sys/stat.h>
1919#include <errno.h>
2020
21- #include "libpq-fe.h"
22- #include "libpq-int.h"
23-
2421#ifdef WIN32
2522#include "win32.h"
2623#include "io.h"
2724#else
2825#include <unistd.h>
2926#endif
3027
28+ #include "libpq-fe.h"
29+ #include "libpq-int.h"
3130#include "libpq/libpq-fs.h" /* must come after sys/stat.h */
3231
32+
3333#define LO_BUFSIZE 8192
3434
3535static int lo_initialize (PGconn * conn );
3636
37+
3738/*
3839 * lo_open
3940 * opens an existing large object
@@ -123,7 +124,7 @@ lo_close(PGconn *conn, int fd)
123124 * lo_read
124125 * read len bytes of the large object into buf
125126 *
126- * returns the length of bytes read.
127+ * returns the number of bytes read, or -1 on failure .
127128 * the CALLER must have allocated enough space to hold the result returned
128129 */
129130
@@ -166,6 +167,7 @@ lo_read(PGconn *conn, int fd, char *buf, size_t len)
166167 * lo_write
167168 * write len bytes of buf into the large object fd
168169 *
170+ * returns the number of bytes written, or -1 on failure.
169171 */
170172int
171173lo_write (PGconn * conn , int fd , char * buf , size_t len )
@@ -273,7 +275,7 @@ lo_creat(PGconn *conn, int mode)
273275 if (conn -> lobjfuncs == (PGlobjfuncs * ) NULL )
274276 {
275277 if (lo_initialize (conn ) < 0 )
276- return -1 ;
278+ return InvalidOid ;
277279 }
278280
279281 argv [0 ].isint = 1 ;
@@ -373,9 +375,9 @@ lo_unlink(PGconn *conn, Oid lobjId)
373375/*
374376 * lo_import -
375377 * imports a file as an (inversion) large object.
376- * returns the oid of that object upon success,
377- * returns InvalidOid upon failure
378378 *
379+ * returns the oid of that object upon success,
380+ * returns InvalidOid upon failure
379381 */
380382
381383Oid
@@ -409,6 +411,7 @@ lo_import(PGconn *conn, const char *filename)
409411 printfPQExpBuffer (& conn -> errorMessage ,
410412 libpq_gettext ("could not create large object for file \"%s\"\n" ),
411413 filename );
414+ (void ) close (fd );
412415 return InvalidOid ;
413416 }
414417
@@ -418,6 +421,7 @@ lo_import(PGconn *conn, const char *filename)
418421 printfPQExpBuffer (& conn -> errorMessage ,
419422 libpq_gettext ("could not open large object %u\n" ),
420423 lobjOid );
424+ (void ) close (fd );
421425 return InvalidOid ;
422426 }
423427
@@ -432,6 +436,8 @@ lo_import(PGconn *conn, const char *filename)
432436 printfPQExpBuffer (& conn -> errorMessage ,
433437 libpq_gettext ("error while reading file \"%s\"\n" ),
434438 filename );
439+ (void ) close (fd );
440+ (void ) lo_close (conn , lobj );
435441 return InvalidOid ;
436442 }
437443 }
@@ -457,7 +463,7 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
457463 int lobj ;
458464
459465 /*
460- * create an inversion " object"
466+ * open the large object.
461467 */
462468 lobj = lo_open (conn , lobjId , INV_READ );
463469 if (lobj == -1 )
@@ -468,15 +474,16 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
468474 }
469475
470476 /*
471- * open the file to be written to
477+ * create the file to be written to
472478 */
473479 fd = open (filename , O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY , 0666 );
474480 if (fd < 0 )
475481 { /* error */
476482 printfPQExpBuffer (& conn -> errorMessage ,
477483 libpq_gettext ("could not open file \"%s\": %s\n" ),
478484 filename , strerror (errno ));
479- return 0 ;
485+ (void ) lo_close (conn , lobj );
486+ return -1 ;
480487 }
481488
482489 /*
@@ -490,6 +497,8 @@ lo_export(PGconn *conn, Oid lobjId, const char *filename)
490497 printfPQExpBuffer (& conn -> errorMessage ,
491498 libpq_gettext ("error while writing to file \"%s\"\n" ),
492499 filename );
500+ (void ) lo_close (conn , lobj );
501+ (void ) close (fd );
493502 return -1 ;
494503 }
495504 }
0 commit comments