1 /*-------------------------------------------------------------------------
4 * Front-end large object interface
6 * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/interfaces/libpq/fe-lobj.c
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"
36 #include <netinet/in.h> /* for ntohl/htonl */
37 #include <arpa/inet.h>
40 #include "libpq-int.h"
41 #include "libpq/libpq-fs.h" /* must come after sys/stat.h */
43 #define LO_BUFSIZE 8192
45 static int lo_initialize(PGconn *conn);
46 static Oid lo_import_internal(PGconn *conn, const char *filename, Oid oid);
47 static pg_int64 lo_hton64(pg_int64 host64);
48 static pg_int64 lo_ntoh64(pg_int64 net64);
52 * opens an existing large object
54 * returns the file descriptor for use in later lo_* calls
55 * return -1 upon failure.
58 lo_open(PGconn *conn, Oid lobjId, int mode)
65 if (conn == NULL || conn->lobjfuncs == NULL)
67 if (lo_initialize(conn) < 0)
73 argv[0].u.integer = lobjId;
77 argv[1].u.integer = mode;
79 res = PQfn(conn, conn->lobjfuncs->fn_lo_open, &fd, &result_len, 1, argv, 2);
80 if (PQresultStatus(res) == PGRES_COMMAND_OK)
94 * closes an existing large object
96 * returns 0 upon success
97 * returns -1 upon failure.
100 lo_close(PGconn *conn, int fd)
107 if (conn == NULL || conn->lobjfuncs == NULL)
109 if (lo_initialize(conn) < 0)
115 argv[0].u.integer = fd;
116 res = PQfn(conn, conn->lobjfuncs->fn_lo_close,
117 &retval, &result_len, 1, argv, 1);
118 if (PQresultStatus(res) == PGRES_COMMAND_OK)
132 * truncates an existing large object to the given size
134 * returns 0 upon success
135 * returns -1 upon failure
138 lo_truncate(PGconn *conn, int fd, size_t len)
145 if (conn == NULL || conn->lobjfuncs == NULL)
147 if (lo_initialize(conn) < 0)
151 /* Must check this on-the-fly because it's not there pre-8.3 */
152 if (conn->lobjfuncs->fn_lo_truncate == 0)
154 printfPQExpBuffer(&conn->errorMessage,
155 libpq_gettext("cannot determine OID of function lo_truncate\n"));
160 * Long ago, somebody thought it'd be a good idea to declare this function
161 * as taking size_t ... but the underlying backend function only accepts a
162 * signed int32 length. So throw error if the given value overflows
163 * int32. (A possible alternative is to automatically redirect the call
164 * to lo_truncate64; but if the caller wanted to rely on that backend
165 * function being available, he could have called lo_truncate64 for
168 if (len > (size_t) INT_MAX)
170 printfPQExpBuffer(&conn->errorMessage,
171 libpq_gettext("argument of lo_truncate exceeds integer range\n"));
177 argv[0].u.integer = fd;
181 argv[1].u.integer = (int) len;
183 res = PQfn(conn, conn->lobjfuncs->fn_lo_truncate,
184 &retval, &result_len, 1, argv, 2);
186 if (PQresultStatus(res) == PGRES_COMMAND_OK)
200 * truncates an existing large object to the given size
202 * returns 0 upon success
203 * returns -1 upon failure
206 lo_truncate64(PGconn *conn, int fd, pg_int64 len)
213 if (conn == NULL || conn->lobjfuncs == NULL)
215 if (lo_initialize(conn) < 0)
219 if (conn->lobjfuncs->fn_lo_truncate64 == 0)
221 printfPQExpBuffer(&conn->errorMessage,
222 libpq_gettext("cannot determine OID of function lo_truncate64\n"));
228 argv[0].u.integer = fd;
230 len = lo_hton64(len);
233 argv[1].u.ptr = (int *) &len;
235 res = PQfn(conn, conn->lobjfuncs->fn_lo_truncate64,
236 &retval, &result_len, 1, argv, 2);
238 if (PQresultStatus(res) == PGRES_COMMAND_OK)
252 * read len bytes of the large object into buf
254 * returns the number of bytes read, or -1 on failure.
255 * the CALLER must have allocated enough space to hold the result returned
259 lo_read(PGconn *conn, int fd, char *buf, size_t len)
265 if (conn == NULL || conn->lobjfuncs == NULL)
267 if (lo_initialize(conn) < 0)
272 * Long ago, somebody thought it'd be a good idea to declare this function
273 * as taking size_t ... but the underlying backend function only accepts a
274 * signed int32 length. So throw error if the given value overflows
277 if (len > (size_t) INT_MAX)
279 printfPQExpBuffer(&conn->errorMessage,
280 libpq_gettext("argument of lo_read exceeds integer range\n"));
286 argv[0].u.integer = fd;
290 argv[1].u.integer = (int) len;
292 res = PQfn(conn, conn->lobjfuncs->fn_lo_read,
293 (int *) buf, &result_len, 0, argv, 2);
294 if (PQresultStatus(res) == PGRES_COMMAND_OK)
308 * write len bytes of buf into the large object fd
310 * returns the number of bytes written, or -1 on failure.
313 lo_write(PGconn *conn, int fd, const char *buf, size_t len)
320 if (conn == NULL || conn->lobjfuncs == NULL)
322 if (lo_initialize(conn) < 0)
327 * Long ago, somebody thought it'd be a good idea to declare this function
328 * as taking size_t ... but the underlying backend function only accepts a
329 * signed int32 length. So throw error if the given value overflows
332 if (len > (size_t) INT_MAX)
334 printfPQExpBuffer(&conn->errorMessage,
335 libpq_gettext("argument of lo_write exceeds integer range\n"));
341 argv[0].u.integer = fd;
344 argv[1].len = (int) len;
345 argv[1].u.ptr = (int *) buf;
347 res = PQfn(conn, conn->lobjfuncs->fn_lo_write,
348 &retval, &result_len, 1, argv, 2);
349 if (PQresultStatus(res) == PGRES_COMMAND_OK)
363 * change the current read or write location on a large object
366 lo_lseek(PGconn *conn, int fd, int offset, int whence)
373 if (conn == NULL || conn->lobjfuncs == NULL)
375 if (lo_initialize(conn) < 0)
381 argv[0].u.integer = fd;
385 argv[1].u.integer = offset;
389 argv[2].u.integer = whence;
391 res = PQfn(conn, conn->lobjfuncs->fn_lo_lseek,
392 &retval, &result_len, 1, argv, 3);
393 if (PQresultStatus(res) == PGRES_COMMAND_OK)
407 * change the current read or write location on a large object
410 lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence)
417 if (conn == NULL || conn->lobjfuncs == NULL)
419 if (lo_initialize(conn) < 0)
423 if (conn->lobjfuncs->fn_lo_lseek64 == 0)
425 printfPQExpBuffer(&conn->errorMessage,
426 libpq_gettext("cannot determine OID of function lo_lseek64\n"));
432 argv[0].u.integer = fd;
434 offset = lo_hton64(offset);
437 argv[1].u.ptr = (int *) &offset;
441 argv[2].u.integer = whence;
443 res = PQfn(conn, conn->lobjfuncs->fn_lo_lseek64,
444 (int *) &retval, &result_len, 0, argv, 3);
445 if (PQresultStatus(res) == PGRES_COMMAND_OK)
448 return lo_ntoh64(retval);
459 * create a new large object
460 * the mode is ignored (once upon a time it had a use)
462 * returns the oid of the large object created or
463 * InvalidOid upon failure
466 lo_creat(PGconn *conn, int mode)
473 if (conn == NULL || conn->lobjfuncs == NULL)
475 if (lo_initialize(conn) < 0)
481 argv[0].u.integer = mode;
482 res = PQfn(conn, conn->lobjfuncs->fn_lo_creat,
483 &retval, &result_len, 1, argv, 1);
484 if (PQresultStatus(res) == PGRES_COMMAND_OK)
498 * create a new large object
499 * if lobjId isn't InvalidOid, it specifies the OID to (attempt to) create
501 * returns the oid of the large object created or
502 * InvalidOid upon failure
505 lo_create(PGconn *conn, Oid lobjId)
512 if (conn == NULL || conn->lobjfuncs == NULL)
514 if (lo_initialize(conn) < 0)
518 /* Must check this on-the-fly because it's not there pre-8.1 */
519 if (conn->lobjfuncs->fn_lo_create == 0)
521 printfPQExpBuffer(&conn->errorMessage,
522 libpq_gettext("cannot determine OID of function lo_create\n"));
528 argv[0].u.integer = lobjId;
529 res = PQfn(conn, conn->lobjfuncs->fn_lo_create,
530 &retval, &result_len, 1, argv, 1);
531 if (PQresultStatus(res) == PGRES_COMMAND_OK)
546 * returns the current seek location of the large object
549 lo_tell(PGconn *conn, int fd)
556 if (conn == NULL || conn->lobjfuncs == NULL)
558 if (lo_initialize(conn) < 0)
564 argv[0].u.integer = fd;
566 res = PQfn(conn, conn->lobjfuncs->fn_lo_tell,
567 &retval, &result_len, 1, argv, 1);
568 if (PQresultStatus(res) == PGRES_COMMAND_OK)
582 * returns the current seek location of the large object
585 lo_tell64(PGconn *conn, int fd)
592 if (conn == NULL || conn->lobjfuncs == NULL)
594 if (lo_initialize(conn) < 0)
598 if (conn->lobjfuncs->fn_lo_tell64 == 0)
600 printfPQExpBuffer(&conn->errorMessage,
601 libpq_gettext("cannot determine OID of function lo_tell64\n"));
607 argv[0].u.integer = fd;
609 res = PQfn(conn, conn->lobjfuncs->fn_lo_tell64,
610 (int *) &retval, &result_len, 0, argv, 1);
611 if (PQresultStatus(res) == PGRES_COMMAND_OK)
614 return lo_ntoh64(retval);
629 lo_unlink(PGconn *conn, Oid lobjId)
636 if (conn == NULL || conn->lobjfuncs == NULL)
638 if (lo_initialize(conn) < 0)
644 argv[0].u.integer = lobjId;
646 res = PQfn(conn, conn->lobjfuncs->fn_lo_unlink,
647 &retval, &result_len, 1, argv, 1);
648 if (PQresultStatus(res) == PGRES_COMMAND_OK)
662 * imports a file as an (inversion) large object.
664 * returns the oid of that object upon success,
665 * returns InvalidOid upon failure
669 lo_import(PGconn *conn, const char *filename)
671 return lo_import_internal(conn, filename, InvalidOid);
675 * lo_import_with_oid -
676 * imports a file as an (inversion) large object.
677 * large object id can be specified.
679 * returns the oid of that object upon success,
680 * returns InvalidOid upon failure
684 lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId)
686 return lo_import_internal(conn, filename, lobjId);
690 lo_import_internal(PGconn *conn, const char *filename, Oid oid)
695 char buf[LO_BUFSIZE];
701 * open the file to be read in
703 fd = open(filename, O_RDONLY | PG_BINARY, 0666);
706 printfPQExpBuffer(&conn->errorMessage,
707 libpq_gettext("could not open file \"%s\": %s\n"),
708 filename, pqStrerror(errno, sebuf, sizeof(sebuf)));
713 * create an inversion object
715 if (oid == InvalidOid)
716 lobjOid = lo_creat(conn, INV_READ | INV_WRITE);
718 lobjOid = lo_create(conn, oid);
720 if (lobjOid == InvalidOid)
722 /* we assume lo_create() already set a suitable error message */
727 lobj = lo_open(conn, lobjOid, INV_WRITE);
730 /* we assume lo_open() already set a suitable error message */
736 * read in from the file and write to the large object
738 while ((nbytes = read(fd, buf, LO_BUFSIZE)) > 0)
740 tmp = lo_write(conn, lobj, buf, nbytes);
744 * If lo_write() failed, we are now in an aborted transaction so
745 * there's no need for lo_close(); furthermore, if we tried it
746 * we'd overwrite the useful error result with a useless one. So
747 * just nail the doors shut and get out of town.
756 /* We must do lo_close before setting the errorMessage */
757 int save_errno = errno;
759 (void) lo_close(conn, lobj);
761 printfPQExpBuffer(&conn->errorMessage,
762 libpq_gettext("could not read from file \"%s\": %s\n"),
764 pqStrerror(save_errno, sebuf, sizeof(sebuf)));
770 if (lo_close(conn, lobj) != 0)
772 /* we assume lo_close() already set a suitable error message */
781 * exports an (inversion) large object.
782 * returns -1 upon failure, 1 if OK
785 lo_export(PGconn *conn, Oid lobjId, const char *filename)
791 char buf[LO_BUFSIZE];
796 * open the large object.
798 lobj = lo_open(conn, lobjId, INV_READ);
801 /* we assume lo_open() already set a suitable error message */
806 * create the file to be written to
808 fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666);
811 /* We must do lo_close before setting the errorMessage */
812 int save_errno = errno;
814 (void) lo_close(conn, lobj);
815 printfPQExpBuffer(&conn->errorMessage,
816 libpq_gettext("could not open file \"%s\": %s\n"),
818 pqStrerror(save_errno, sebuf, sizeof(sebuf)));
823 * read in from the large object and write to the file
825 while ((nbytes = lo_read(conn, lobj, buf, LO_BUFSIZE)) > 0)
827 tmp = write(fd, buf, nbytes);
830 /* We must do lo_close before setting the errorMessage */
831 int save_errno = errno;
833 (void) lo_close(conn, lobj);
835 printfPQExpBuffer(&conn->errorMessage,
836 libpq_gettext("could not write to file \"%s\": %s\n"),
838 pqStrerror(save_errno, sebuf, sizeof(sebuf)));
844 * If lo_read() failed, we are now in an aborted transaction so there's no
845 * need for lo_close(); furthermore, if we tried it we'd overwrite the
846 * useful error result with a useless one. So skip lo_close() if we got a
850 lo_close(conn, lobj) != 0)
852 /* assume lo_read() or lo_close() left a suitable error message */
856 /* if we already failed, don't overwrite that msg with a close error */
857 if (close(fd) && result >= 0)
859 printfPQExpBuffer(&conn->errorMessage,
860 libpq_gettext("could not write to file \"%s\": %s\n"),
861 filename, pqStrerror(errno, sebuf, sizeof(sebuf)));
872 * Initialize the large object interface for an existing connection.
873 * We ask the backend about the functions OID's in pg_proc for all
874 * functions that are required for large object operations.
877 lo_initialize(PGconn *conn)
880 PGlobjfuncs *lobjfuncs;
890 * Allocate the structure to hold the functions OID's
892 lobjfuncs = (PGlobjfuncs *) malloc(sizeof(PGlobjfuncs));
893 if (lobjfuncs == NULL)
895 printfPQExpBuffer(&conn->errorMessage,
896 libpq_gettext("out of memory\n"));
899 MemSet((char *) lobjfuncs, 0, sizeof(PGlobjfuncs));
902 * Execute the query to get all the functions at once. In 7.3 and later
903 * we need to be schema-safe. lo_create only exists in 8.1 and up.
904 * lo_truncate only exists in 8.3 and up.
906 if (conn->sversion >= 70300)
907 query = "select proname, oid from pg_catalog.pg_proc "
922 "and pronamespace = (select oid from pg_catalog.pg_namespace "
923 "where nspname = 'pg_catalog')";
925 query = "select proname, oid from pg_proc "
926 "where proname = 'lo_open' "
927 "or proname = 'lo_close' "
928 "or proname = 'lo_creat' "
929 "or proname = 'lo_unlink' "
930 "or proname = 'lo_lseek' "
931 "or proname = 'lo_tell' "
932 "or proname = 'loread' "
933 "or proname = 'lowrite'";
935 res = PQexec(conn, query);
942 if (res->resultStatus != PGRES_TUPLES_OK)
946 printfPQExpBuffer(&conn->errorMessage,
947 libpq_gettext("query to initialize large object functions did not return data\n"));
952 * Examine the result and put the OID's into the struct
954 for (n = 0; n < PQntuples(res); n++)
956 fname = PQgetvalue(res, n, 0);
957 foid = (Oid) atoi(PQgetvalue(res, n, 1));
958 if (strcmp(fname, "lo_open") == 0)
959 lobjfuncs->fn_lo_open = foid;
960 else if (strcmp(fname, "lo_close") == 0)
961 lobjfuncs->fn_lo_close = foid;
962 else if (strcmp(fname, "lo_creat") == 0)
963 lobjfuncs->fn_lo_creat = foid;
964 else if (strcmp(fname, "lo_create") == 0)
965 lobjfuncs->fn_lo_create = foid;
966 else if (strcmp(fname, "lo_unlink") == 0)
967 lobjfuncs->fn_lo_unlink = foid;
968 else if (strcmp(fname, "lo_lseek") == 0)
969 lobjfuncs->fn_lo_lseek = foid;
970 else if (strcmp(fname, "lo_lseek64") == 0)
971 lobjfuncs->fn_lo_lseek64 = foid;
972 else if (strcmp(fname, "lo_tell") == 0)
973 lobjfuncs->fn_lo_tell = foid;
974 else if (strcmp(fname, "lo_tell64") == 0)
975 lobjfuncs->fn_lo_tell64 = foid;
976 else if (strcmp(fname, "lo_truncate") == 0)
977 lobjfuncs->fn_lo_truncate = foid;
978 else if (strcmp(fname, "lo_truncate64") == 0)
979 lobjfuncs->fn_lo_truncate64 = foid;
980 else if (strcmp(fname, "loread") == 0)
981 lobjfuncs->fn_lo_read = foid;
982 else if (strcmp(fname, "lowrite") == 0)
983 lobjfuncs->fn_lo_write = foid;
989 * Finally check that we got all required large object interface functions
990 * (ones that have been added later than the stone age are instead checked
993 if (lobjfuncs->fn_lo_open == 0)
995 printfPQExpBuffer(&conn->errorMessage,
996 libpq_gettext("cannot determine OID of function lo_open\n"));
1000 if (lobjfuncs->fn_lo_close == 0)
1002 printfPQExpBuffer(&conn->errorMessage,
1003 libpq_gettext("cannot determine OID of function lo_close\n"));
1007 if (lobjfuncs->fn_lo_creat == 0)
1009 printfPQExpBuffer(&conn->errorMessage,
1010 libpq_gettext("cannot determine OID of function lo_creat\n"));
1014 if (lobjfuncs->fn_lo_unlink == 0)
1016 printfPQExpBuffer(&conn->errorMessage,
1017 libpq_gettext("cannot determine OID of function lo_unlink\n"));
1021 if (lobjfuncs->fn_lo_lseek == 0)
1023 printfPQExpBuffer(&conn->errorMessage,
1024 libpq_gettext("cannot determine OID of function lo_lseek\n"));
1028 if (lobjfuncs->fn_lo_tell == 0)
1030 printfPQExpBuffer(&conn->errorMessage,
1031 libpq_gettext("cannot determine OID of function lo_tell\n"));
1035 if (lobjfuncs->fn_lo_read == 0)
1037 printfPQExpBuffer(&conn->errorMessage,
1038 libpq_gettext("cannot determine OID of function loread\n"));
1042 if (lobjfuncs->fn_lo_write == 0)
1044 printfPQExpBuffer(&conn->errorMessage,
1045 libpq_gettext("cannot determine OID of function lowrite\n"));
1051 * Put the structure into the connection control
1053 conn->lobjfuncs = lobjfuncs;
1059 * converts a 64-bit integer from host byte order to network byte order
1062 lo_hton64(pg_int64 host64)
1071 /* High order half first, since we're doing MSB-first */
1072 t = (uint32) (host64 >> 32);
1073 swap.i32[0] = htonl(t);
1075 /* Now the low order half */
1076 t = (uint32) host64;
1077 swap.i32[1] = htonl(t);
1084 * converts a 64-bit integer from network byte order to host byte order
1087 lo_ntoh64(pg_int64 net64)
1098 result = (uint32) ntohl(swap.i32[0]);
1100 result |= (uint32) ntohl(swap.i32[1]);