1 /*-------------------------------------------------------------------------
4 * Front-end large object interface
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/interfaces/libpq/fe-lobj.c,v 1.66 2008/04/10 15:20:52 mha Exp $
13 *-------------------------------------------------------------------------
18 * As unlink/rename are #define'd in port.h (via postgres_fe.h), io.h
19 * must be included first on MS C. Might as well do it for all WIN32's
25 #include "postgres_fe.h"
37 #include "libpq-int.h"
38 #include "libpq/libpq-fs.h" /* must come after sys/stat.h */
40 #define LO_BUFSIZE 8192
42 static int lo_initialize(PGconn *conn);
45 lo_import_internal(PGconn *conn, const char *filename, const Oid oid);
49 * opens an existing large object
51 * returns the file descriptor for use in later lo_* calls
52 * return -1 upon failure.
55 lo_open(PGconn *conn, Oid lobjId, int mode)
62 if (conn->lobjfuncs == NULL)
64 if (lo_initialize(conn) < 0)
70 argv[0].u.integer = lobjId;
74 argv[1].u.integer = mode;
76 res = PQfn(conn, conn->lobjfuncs->fn_lo_open, &fd, &result_len, 1, argv, 2);
77 if (PQresultStatus(res) == PGRES_COMMAND_OK)
91 * closes an existing large object
93 * returns 0 upon success
94 * returns -1 upon failure.
97 lo_close(PGconn *conn, int fd)
104 if (conn->lobjfuncs == NULL)
106 if (lo_initialize(conn) < 0)
112 argv[0].u.integer = fd;
113 res = PQfn(conn, conn->lobjfuncs->fn_lo_close,
114 &retval, &result_len, 1, argv, 1);
115 if (PQresultStatus(res) == PGRES_COMMAND_OK)
129 * truncates an existing large object to the given size
131 * returns 0 upon success
132 * returns -1 upon failure
135 lo_truncate(PGconn *conn, int fd, size_t len)
142 if (conn->lobjfuncs == NULL)
144 if (lo_initialize(conn) < 0)
148 /* Must check this on-the-fly because it's not there pre-8.3 */
149 if (conn->lobjfuncs->fn_lo_truncate == 0)
151 printfPQExpBuffer(&conn->errorMessage,
152 libpq_gettext("cannot determine OID of function lo_truncate\n"));
158 argv[0].u.integer = fd;
162 argv[1].u.integer = len;
164 res = PQfn(conn, conn->lobjfuncs->fn_lo_truncate,
165 &retval, &result_len, 1, argv, 2);
167 if (PQresultStatus(res) == PGRES_COMMAND_OK)
182 * read len bytes of the large object into buf
184 * returns the number of bytes read, or -1 on failure.
185 * the CALLER must have allocated enough space to hold the result returned
189 lo_read(PGconn *conn, int fd, char *buf, size_t len)
195 if (conn->lobjfuncs == NULL)
197 if (lo_initialize(conn) < 0)
203 argv[0].u.integer = fd;
207 argv[1].u.integer = len;
209 res = PQfn(conn, conn->lobjfuncs->fn_lo_read,
210 (int *) buf, &result_len, 0, argv, 2);
211 if (PQresultStatus(res) == PGRES_COMMAND_OK)
225 * write len bytes of buf into the large object fd
227 * returns the number of bytes written, or -1 on failure.
230 lo_write(PGconn *conn, int fd, const char *buf, size_t len)
237 if (conn->lobjfuncs == NULL)
239 if (lo_initialize(conn) < 0)
248 argv[0].u.integer = fd;
252 argv[1].u.ptr = (int *) buf;
254 res = PQfn(conn, conn->lobjfuncs->fn_lo_write,
255 &retval, &result_len, 1, argv, 2);
256 if (PQresultStatus(res) == PGRES_COMMAND_OK)
270 * change the current read or write location on a large object
271 * currently, only L_SET is a legal value for whence
276 lo_lseek(PGconn *conn, int fd, int offset, int whence)
283 if (conn->lobjfuncs == NULL)
285 if (lo_initialize(conn) < 0)
291 argv[0].u.integer = fd;
295 argv[1].u.integer = offset;
299 argv[2].u.integer = whence;
301 res = PQfn(conn, conn->lobjfuncs->fn_lo_lseek,
302 &retval, &result_len, 1, argv, 3);
303 if (PQresultStatus(res) == PGRES_COMMAND_OK)
317 * create a new large object
318 * the mode is ignored (once upon a time it had a use)
320 * returns the oid of the large object created or
321 * InvalidOid upon failure
324 lo_creat(PGconn *conn, int mode)
331 if (conn->lobjfuncs == NULL)
333 if (lo_initialize(conn) < 0)
339 argv[0].u.integer = mode;
340 res = PQfn(conn, conn->lobjfuncs->fn_lo_creat,
341 &retval, &result_len, 1, argv, 1);
342 if (PQresultStatus(res) == PGRES_COMMAND_OK)
356 * create a new large object
357 * if lobjId isn't InvalidOid, it specifies the OID to (attempt to) create
359 * returns the oid of the large object created or
360 * InvalidOid upon failure
363 lo_create(PGconn *conn, Oid lobjId)
370 if (conn->lobjfuncs == NULL)
372 if (lo_initialize(conn) < 0)
376 /* Must check this on-the-fly because it's not there pre-8.1 */
377 if (conn->lobjfuncs->fn_lo_create == 0)
379 printfPQExpBuffer(&conn->errorMessage,
380 libpq_gettext("cannot determine OID of function lo_create\n"));
386 argv[0].u.integer = lobjId;
387 res = PQfn(conn, conn->lobjfuncs->fn_lo_create,
388 &retval, &result_len, 1, argv, 1);
389 if (PQresultStatus(res) == PGRES_COMMAND_OK)
404 * returns the current seek location of the large object
409 lo_tell(PGconn *conn, int fd)
416 if (conn->lobjfuncs == NULL)
418 if (lo_initialize(conn) < 0)
424 argv[0].u.integer = fd;
426 res = PQfn(conn, conn->lobjfuncs->fn_lo_tell,
427 &retval, &result_len, 1, argv, 1);
428 if (PQresultStatus(res) == PGRES_COMMAND_OK)
447 lo_unlink(PGconn *conn, Oid lobjId)
454 if (conn->lobjfuncs == NULL)
456 if (lo_initialize(conn) < 0)
462 argv[0].u.integer = lobjId;
464 res = PQfn(conn, conn->lobjfuncs->fn_lo_unlink,
465 &retval, &result_len, 1, argv, 1);
466 if (PQresultStatus(res) == PGRES_COMMAND_OK)
480 * imports a file as an (inversion) large object.
482 * returns the oid of that object upon success,
483 * returns InvalidOid upon failure
487 lo_import(PGconn *conn, const char *filename)
489 return lo_import_internal(conn, filename, InvalidOid);
493 * lo_import_with_oid -
494 * imports a file as an (inversion) large object.
495 * large object id can be specified.
497 * returns the oid of that object upon success,
498 * returns InvalidOid upon failure
502 lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId)
504 return lo_import_internal(conn, filename, lobjId);
508 lo_import_internal(PGconn *conn, const char *filename, const Oid oid)
513 char buf[LO_BUFSIZE];
519 * open the file to be read in
521 fd = open(filename, O_RDONLY | PG_BINARY, 0666);
524 printfPQExpBuffer(&conn->errorMessage,
525 libpq_gettext("could not open file \"%s\": %s\n"),
526 filename, pqStrerror(errno, sebuf, sizeof(sebuf)));
531 * create an inversion object
533 if (oid == InvalidOid)
534 lobjOid = lo_creat(conn, INV_READ | INV_WRITE);
536 lobjOid = lo_create(conn, oid);
538 if (lobjOid == InvalidOid)
540 /* we assume lo_create() already set a suitable error message */
545 lobj = lo_open(conn, lobjOid, INV_WRITE);
548 /* we assume lo_open() already set a suitable error message */
554 * read in from the file and write to the large object
556 while ((nbytes = read(fd, buf, LO_BUFSIZE)) > 0)
558 tmp = lo_write(conn, lobj, buf, nbytes);
562 * If lo_write() failed, we are now in an aborted transaction so
563 * there's no need for lo_close(); furthermore, if we tried it
564 * we'd overwrite the useful error result with a useless one. So
565 * just nail the doors shut and get out of town.
574 printfPQExpBuffer(&conn->errorMessage,
575 libpq_gettext("could not read from file \"%s\": %s\n"),
576 filename, pqStrerror(errno, sebuf, sizeof(sebuf)));
577 lobjOid = InvalidOid;
582 if (lo_close(conn, lobj) != 0)
584 /* we assume lo_close() already set a suitable error message */
593 * exports an (inversion) large object.
594 * returns -1 upon failure, 1 if OK
597 lo_export(PGconn *conn, Oid lobjId, const char *filename)
603 char buf[LO_BUFSIZE];
608 * open the large object.
610 lobj = lo_open(conn, lobjId, INV_READ);
613 /* we assume lo_open() already set a suitable error message */
618 * create the file to be written to
620 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666);
623 printfPQExpBuffer(&conn->errorMessage,
624 libpq_gettext("could not open file \"%s\": %s\n"),
625 filename, pqStrerror(errno, sebuf, sizeof(sebuf)));
626 (void) lo_close(conn, lobj);
631 * read in from the large object and write to the file
633 while ((nbytes = lo_read(conn, lobj, buf, LO_BUFSIZE)) > 0)
635 tmp = write(fd, buf, nbytes);
638 printfPQExpBuffer(&conn->errorMessage,
639 libpq_gettext("could not write to file \"%s\": %s\n"),
640 filename, pqStrerror(errno, sebuf, sizeof(sebuf)));
641 (void) lo_close(conn, lobj);
648 * If lo_read() failed, we are now in an aborted transaction so there's no
649 * need for lo_close(); furthermore, if we tried it we'd overwrite the
650 * useful error result with a useless one. So skip lo_close() if we got a
654 lo_close(conn, lobj) != 0)
656 /* assume lo_read() or lo_close() left a suitable error message */
662 printfPQExpBuffer(&conn->errorMessage,
663 libpq_gettext("could not write to file \"%s\": %s\n"),
664 filename, pqStrerror(errno, sebuf, sizeof(sebuf)));
675 * Initialize the large object interface for an existing connection.
676 * We ask the backend about the functions OID's in pg_proc for all
677 * functions that are required for large object operations.
680 lo_initialize(PGconn *conn)
683 PGlobjfuncs *lobjfuncs;
690 * Allocate the structure to hold the functions OID's
692 lobjfuncs = (PGlobjfuncs *) malloc(sizeof(PGlobjfuncs));
693 if (lobjfuncs == NULL)
695 printfPQExpBuffer(&conn->errorMessage,
696 libpq_gettext("out of memory\n"));
699 MemSet((char *) lobjfuncs, 0, sizeof(PGlobjfuncs));
702 * Execute the query to get all the functions at once. In 7.3 and later
703 * we need to be schema-safe. lo_create only exists in 8.1 and up.
704 * lo_truncate only exists in 8.3 and up.
706 if (conn->sversion >= 70300)
707 query = "select proname, oid from pg_catalog.pg_proc "
719 "and pronamespace = (select oid from pg_catalog.pg_namespace "
720 "where nspname = 'pg_catalog')";
722 query = "select proname, oid from pg_proc "
723 "where proname = 'lo_open' "
724 "or proname = 'lo_close' "
725 "or proname = 'lo_creat' "
726 "or proname = 'lo_unlink' "
727 "or proname = 'lo_lseek' "
728 "or proname = 'lo_tell' "
729 "or proname = 'loread' "
730 "or proname = 'lowrite'";
732 res = PQexec(conn, query);
739 if (res->resultStatus != PGRES_TUPLES_OK)
743 printfPQExpBuffer(&conn->errorMessage,
744 libpq_gettext("query to initialize large object functions did not return data\n"));
749 * Examine the result and put the OID's into the struct
751 for (n = 0; n < PQntuples(res); n++)
753 fname = PQgetvalue(res, n, 0);
754 foid = (Oid) atoi(PQgetvalue(res, n, 1));
755 if (!strcmp(fname, "lo_open"))
756 lobjfuncs->fn_lo_open = foid;
757 else if (!strcmp(fname, "lo_close"))
758 lobjfuncs->fn_lo_close = foid;
759 else if (!strcmp(fname, "lo_creat"))
760 lobjfuncs->fn_lo_creat = foid;
761 else if (!strcmp(fname, "lo_create"))
762 lobjfuncs->fn_lo_create = foid;
763 else if (!strcmp(fname, "lo_unlink"))
764 lobjfuncs->fn_lo_unlink = foid;
765 else if (!strcmp(fname, "lo_lseek"))
766 lobjfuncs->fn_lo_lseek = foid;
767 else if (!strcmp(fname, "lo_tell"))
768 lobjfuncs->fn_lo_tell = foid;
769 else if (!strcmp(fname, "lo_truncate"))
770 lobjfuncs->fn_lo_truncate = foid;
771 else if (!strcmp(fname, "loread"))
772 lobjfuncs->fn_lo_read = foid;
773 else if (!strcmp(fname, "lowrite"))
774 lobjfuncs->fn_lo_write = foid;
780 * Finally check that we really got all large object interface functions
782 if (lobjfuncs->fn_lo_open == 0)
784 printfPQExpBuffer(&conn->errorMessage,
785 libpq_gettext("cannot determine OID of function lo_open\n"));
789 if (lobjfuncs->fn_lo_close == 0)
791 printfPQExpBuffer(&conn->errorMessage,
792 libpq_gettext("cannot determine OID of function lo_close\n"));
796 if (lobjfuncs->fn_lo_creat == 0)
798 printfPQExpBuffer(&conn->errorMessage,
799 libpq_gettext("cannot determine OID of function lo_creat\n"));
803 if (lobjfuncs->fn_lo_unlink == 0)
805 printfPQExpBuffer(&conn->errorMessage,
806 libpq_gettext("cannot determine OID of function lo_unlink\n"));
810 if (lobjfuncs->fn_lo_lseek == 0)
812 printfPQExpBuffer(&conn->errorMessage,
813 libpq_gettext("cannot determine OID of function lo_lseek\n"));
817 if (lobjfuncs->fn_lo_tell == 0)
819 printfPQExpBuffer(&conn->errorMessage,
820 libpq_gettext("cannot determine OID of function lo_tell\n"));
824 if (lobjfuncs->fn_lo_read == 0)
826 printfPQExpBuffer(&conn->errorMessage,
827 libpq_gettext("cannot determine OID of function loread\n"));
831 if (lobjfuncs->fn_lo_write == 0)
833 printfPQExpBuffer(&conn->errorMessage,
834 libpq_gettext("cannot determine OID of function lowrite\n"));
840 * Put the structure into the connection control
842 conn->lobjfuncs = lobjfuncs;