PostgreSQL Source Code  git master
fe-lobj.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * fe-lobj.c
4  * Front-end large object interface
5  *
6  * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/interfaces/libpq/fe-lobj.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #ifdef WIN32
17 /*
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
20  * here.
21  */
22 #include <io.h>
23 #endif
24 
25 #include "postgres_fe.h"
26 
27 #ifdef WIN32
28 #include "win32.h"
29 #else
30 #include <unistd.h>
31 #endif
32 
33 #include <fcntl.h>
34 #include <limits.h>
35 #include <sys/stat.h>
36 
37 #include "libpq-fe.h"
38 #include "libpq-int.h"
39 #include "libpq/libpq-fs.h" /* must come after sys/stat.h */
40 #include "port/pg_bswap.h"
41 
42 #define LO_BUFSIZE 8192
43 
44 static int lo_initialize(PGconn *conn);
45 static Oid lo_import_internal(PGconn *conn, const char *filename, Oid oid);
46 static pg_int64 lo_hton64(pg_int64 host64);
47 static pg_int64 lo_ntoh64(pg_int64 net64);
48 
49 /*
50  * lo_open
51  * opens an existing large object
52  *
53  * returns the file descriptor for use in later lo_* calls
54  * return -1 upon failure.
55  */
56 int
57 lo_open(PGconn *conn, Oid lobjId, int mode)
58 {
59  int fd;
60  int result_len;
61  PQArgBlock argv[2];
62  PGresult *res;
63 
64  if (lo_initialize(conn) < 0)
65  return -1;
66 
67  argv[0].isint = 1;
68  argv[0].len = 4;
69  argv[0].u.integer = lobjId;
70 
71  argv[1].isint = 1;
72  argv[1].len = 4;
73  argv[1].u.integer = mode;
74 
75  res = PQfn(conn, conn->lobjfuncs->fn_lo_open, &fd, &result_len, 1, argv, 2);
77  {
78  PQclear(res);
79  return fd;
80  }
81  else
82  {
83  PQclear(res);
84  return -1;
85  }
86 }
87 
88 /*
89  * lo_close
90  * closes an existing large object
91  *
92  * returns 0 upon success
93  * returns -1 upon failure.
94  */
95 int
97 {
98  PQArgBlock argv[1];
99  PGresult *res;
100  int retval;
101  int result_len;
102 
103  if (lo_initialize(conn) < 0)
104  return -1;
105 
106  argv[0].isint = 1;
107  argv[0].len = 4;
108  argv[0].u.integer = fd;
110  &retval, &result_len, 1, argv, 1);
112  {
113  PQclear(res);
114  return retval;
115  }
116  else
117  {
118  PQclear(res);
119  return -1;
120  }
121 }
122 
123 /*
124  * lo_truncate
125  * truncates an existing large object to the given size
126  *
127  * returns 0 upon success
128  * returns -1 upon failure
129  */
130 int
131 lo_truncate(PGconn *conn, int fd, size_t len)
132 {
133  PQArgBlock argv[2];
134  PGresult *res;
135  int retval;
136  int result_len;
137 
138  if (lo_initialize(conn) < 0)
139  return -1;
140 
141  /* Must check this on-the-fly because it's not there pre-8.3 */
142  if (conn->lobjfuncs->fn_lo_truncate == 0)
143  {
145  libpq_gettext("cannot determine OID of function %s\n"),
146  "lo_truncate");
147  return -1;
148  }
149 
150  /*
151  * Long ago, somebody thought it'd be a good idea to declare this function
152  * as taking size_t ... but the underlying backend function only accepts a
153  * signed int32 length. So throw error if the given value overflows
154  * int32. (A possible alternative is to automatically redirect the call
155  * to lo_truncate64; but if the caller wanted to rely on that backend
156  * function being available, he could have called lo_truncate64 for
157  * himself.)
158  */
159  if (len > (size_t) INT_MAX)
160  {
162  libpq_gettext("argument of lo_truncate exceeds integer range\n"));
163  return -1;
164  }
165 
166  argv[0].isint = 1;
167  argv[0].len = 4;
168  argv[0].u.integer = fd;
169 
170  argv[1].isint = 1;
171  argv[1].len = 4;
172  argv[1].u.integer = (int) len;
173 
175  &retval, &result_len, 1, argv, 2);
176 
178  {
179  PQclear(res);
180  return retval;
181  }
182  else
183  {
184  PQclear(res);
185  return -1;
186  }
187 }
188 
189 /*
190  * lo_truncate64
191  * truncates an existing large object to the given size
192  *
193  * returns 0 upon success
194  * returns -1 upon failure
195  */
196 int
198 {
199  PQArgBlock argv[2];
200  PGresult *res;
201  int retval;
202  int result_len;
203 
204  if (lo_initialize(conn) < 0)
205  return -1;
206 
207  if (conn->lobjfuncs->fn_lo_truncate64 == 0)
208  {
210  libpq_gettext("cannot determine OID of function %s\n"),
211  "lo_truncate64");
212  return -1;
213  }
214 
215  argv[0].isint = 1;
216  argv[0].len = 4;
217  argv[0].u.integer = fd;
218 
219  len = lo_hton64(len);
220  argv[1].isint = 0;
221  argv[1].len = 8;
222  argv[1].u.ptr = (int *) &len;
223 
225  &retval, &result_len, 1, argv, 2);
226 
228  {
229  PQclear(res);
230  return retval;
231  }
232  else
233  {
234  PQclear(res);
235  return -1;
236  }
237 }
238 
239 /*
240  * lo_read
241  * read len bytes of the large object into buf
242  *
243  * returns the number of bytes read, or -1 on failure.
244  * the CALLER must have allocated enough space to hold the result returned
245  */
246 
247 int
248 lo_read(PGconn *conn, int fd, char *buf, size_t len)
249 {
250  PQArgBlock argv[2];
251  PGresult *res;
252  int result_len;
253 
254  if (lo_initialize(conn) < 0)
255  return -1;
256 
257  /*
258  * Long ago, somebody thought it'd be a good idea to declare this function
259  * as taking size_t ... but the underlying backend function only accepts a
260  * signed int32 length. So throw error if the given value overflows
261  * int32.
262  */
263  if (len > (size_t) INT_MAX)
264  {
266  libpq_gettext("argument of lo_read exceeds integer range\n"));
267  return -1;
268  }
269 
270  argv[0].isint = 1;
271  argv[0].len = 4;
272  argv[0].u.integer = fd;
273 
274  argv[1].isint = 1;
275  argv[1].len = 4;
276  argv[1].u.integer = (int) len;
277 
279  (void *) buf, &result_len, 0, argv, 2);
281  {
282  PQclear(res);
283  return result_len;
284  }
285  else
286  {
287  PQclear(res);
288  return -1;
289  }
290 }
291 
292 /*
293  * lo_write
294  * write len bytes of buf into the large object fd
295  *
296  * returns the number of bytes written, or -1 on failure.
297  */
298 int
299 lo_write(PGconn *conn, int fd, const char *buf, size_t len)
300 {
301  PQArgBlock argv[2];
302  PGresult *res;
303  int result_len;
304  int retval;
305 
306  if (lo_initialize(conn) < 0)
307  return -1;
308 
309  /*
310  * Long ago, somebody thought it'd be a good idea to declare this function
311  * as taking size_t ... but the underlying backend function only accepts a
312  * signed int32 length. So throw error if the given value overflows
313  * int32.
314  */
315  if (len > (size_t) INT_MAX)
316  {
318  libpq_gettext("argument of lo_write exceeds integer range\n"));
319  return -1;
320  }
321 
322  argv[0].isint = 1;
323  argv[0].len = 4;
324  argv[0].u.integer = fd;
325 
326  argv[1].isint = 0;
327  argv[1].len = (int) len;
328  argv[1].u.ptr = (int *) unconstify(char *, buf);
329 
331  &retval, &result_len, 1, argv, 2);
333  {
334  PQclear(res);
335  return retval;
336  }
337  else
338  {
339  PQclear(res);
340  return -1;
341  }
342 }
343 
344 /*
345  * lo_lseek
346  * change the current read or write location on a large object
347  */
348 int
349 lo_lseek(PGconn *conn, int fd, int offset, int whence)
350 {
351  PQArgBlock argv[3];
352  PGresult *res;
353  int retval;
354  int result_len;
355 
356  if (lo_initialize(conn) < 0)
357  return -1;
358 
359  argv[0].isint = 1;
360  argv[0].len = 4;
361  argv[0].u.integer = fd;
362 
363  argv[1].isint = 1;
364  argv[1].len = 4;
365  argv[1].u.integer = offset;
366 
367  argv[2].isint = 1;
368  argv[2].len = 4;
369  argv[2].u.integer = whence;
370 
372  &retval, &result_len, 1, argv, 3);
374  {
375  PQclear(res);
376  return retval;
377  }
378  else
379  {
380  PQclear(res);
381  return -1;
382  }
383 }
384 
385 /*
386  * lo_lseek64
387  * change the current read or write location on a large object
388  */
389 pg_int64
390 lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence)
391 {
392  PQArgBlock argv[3];
393  PGresult *res;
394  pg_int64 retval;
395  int result_len;
396 
397  if (lo_initialize(conn) < 0)
398  return -1;
399 
400  if (conn->lobjfuncs->fn_lo_lseek64 == 0)
401  {
403  libpq_gettext("cannot determine OID of function %s\n"),
404  "lo_lseek64");
405  return -1;
406  }
407 
408  argv[0].isint = 1;
409  argv[0].len = 4;
410  argv[0].u.integer = fd;
411 
412  offset = lo_hton64(offset);
413  argv[1].isint = 0;
414  argv[1].len = 8;
415  argv[1].u.ptr = (int *) &offset;
416 
417  argv[2].isint = 1;
418  argv[2].len = 4;
419  argv[2].u.integer = whence;
420 
422  (void *) &retval, &result_len, 0, argv, 3);
423  if (PQresultStatus(res) == PGRES_COMMAND_OK && result_len == 8)
424  {
425  PQclear(res);
426  return lo_ntoh64(retval);
427  }
428  else
429  {
430  PQclear(res);
431  return -1;
432  }
433 }
434 
435 /*
436  * lo_creat
437  * create a new large object
438  * the mode is ignored (once upon a time it had a use)
439  *
440  * returns the oid of the large object created or
441  * InvalidOid upon failure
442  */
443 Oid
445 {
446  PQArgBlock argv[1];
447  PGresult *res;
448  int retval;
449  int result_len;
450 
451  if (lo_initialize(conn) < 0)
452  return InvalidOid;
453 
454  argv[0].isint = 1;
455  argv[0].len = 4;
456  argv[0].u.integer = mode;
458  &retval, &result_len, 1, argv, 1);
460  {
461  PQclear(res);
462  return (Oid) retval;
463  }
464  else
465  {
466  PQclear(res);
467  return InvalidOid;
468  }
469 }
470 
471 /*
472  * lo_create
473  * create a new large object
474  * if lobjId isn't InvalidOid, it specifies the OID to (attempt to) create
475  *
476  * returns the oid of the large object created or
477  * InvalidOid upon failure
478  */
479 Oid
481 {
482  PQArgBlock argv[1];
483  PGresult *res;
484  int retval;
485  int result_len;
486 
487  if (lo_initialize(conn) < 0)
488  return InvalidOid;
489 
490  /* Must check this on-the-fly because it's not there pre-8.1 */
491  if (conn->lobjfuncs->fn_lo_create == 0)
492  {
494  libpq_gettext("cannot determine OID of function %s\n"),
495  "lo_create");
496  return InvalidOid;
497  }
498 
499  argv[0].isint = 1;
500  argv[0].len = 4;
501  argv[0].u.integer = lobjId;
503  &retval, &result_len, 1, argv, 1);
505  {
506  PQclear(res);
507  return (Oid) retval;
508  }
509  else
510  {
511  PQclear(res);
512  return InvalidOid;
513  }
514 }
515 
516 
517 /*
518  * lo_tell
519  * returns the current seek location of the large object
520  */
521 int
523 {
524  int retval;
525  PQArgBlock argv[1];
526  PGresult *res;
527  int result_len;
528 
529  if (lo_initialize(conn) < 0)
530  return -1;
531 
532  argv[0].isint = 1;
533  argv[0].len = 4;
534  argv[0].u.integer = fd;
535 
537  &retval, &result_len, 1, argv, 1);
539  {
540  PQclear(res);
541  return retval;
542  }
543  else
544  {
545  PQclear(res);
546  return -1;
547  }
548 }
549 
550 /*
551  * lo_tell64
552  * returns the current seek location of the large object
553  */
554 pg_int64
556 {
557  pg_int64 retval;
558  PQArgBlock argv[1];
559  PGresult *res;
560  int result_len;
561 
562  if (lo_initialize(conn) < 0)
563  return -1;
564 
565  if (conn->lobjfuncs->fn_lo_tell64 == 0)
566  {
568  libpq_gettext("cannot determine OID of function %s\n"),
569  "lo_tell64");
570  return -1;
571  }
572 
573  argv[0].isint = 1;
574  argv[0].len = 4;
575  argv[0].u.integer = fd;
576 
578  (void *) &retval, &result_len, 0, argv, 1);
579  if (PQresultStatus(res) == PGRES_COMMAND_OK && result_len == 8)
580  {
581  PQclear(res);
582  return lo_ntoh64(retval);
583  }
584  else
585  {
586  PQclear(res);
587  return -1;
588  }
589 }
590 
591 /*
592  * lo_unlink
593  * delete a file
594  */
595 
596 int
598 {
599  PQArgBlock argv[1];
600  PGresult *res;
601  int result_len;
602  int retval;
603 
604  if (lo_initialize(conn) < 0)
605  return -1;
606 
607  argv[0].isint = 1;
608  argv[0].len = 4;
609  argv[0].u.integer = lobjId;
610 
612  &retval, &result_len, 1, argv, 1);
614  {
615  PQclear(res);
616  return retval;
617  }
618  else
619  {
620  PQclear(res);
621  return -1;
622  }
623 }
624 
625 /*
626  * lo_import -
627  * imports a file as an (inversion) large object.
628  *
629  * returns the oid of that object upon success,
630  * returns InvalidOid upon failure
631  */
632 
633 Oid
635 {
637 }
638 
639 /*
640  * lo_import_with_oid -
641  * imports a file as an (inversion) large object.
642  * large object id can be specified.
643  *
644  * returns the oid of that object upon success,
645  * returns InvalidOid upon failure
646  */
647 
648 Oid
649 lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId)
650 {
651  return lo_import_internal(conn, filename, lobjId);
652 }
653 
654 static Oid
656 {
657  int fd;
658  int nbytes,
659  tmp;
660  char buf[LO_BUFSIZE];
661  Oid lobjOid;
662  int lobj;
663  char sebuf[PG_STRERROR_R_BUFLEN];
664 
665  if (conn == NULL)
666  return InvalidOid;
667 
668  /* Since this is the beginning of a query cycle, reset the error state */
670 
671  /*
672  * open the file to be read in
673  */
674  fd = open(filename, O_RDONLY | PG_BINARY, 0666);
675  if (fd < 0)
676  { /* error */
678  libpq_gettext("could not open file \"%s\": %s\n"),
679  filename, strerror_r(errno, sebuf, sizeof(sebuf)));
680  return InvalidOid;
681  }
682 
683  /*
684  * create an inversion object
685  */
686  if (oid == InvalidOid)
687  lobjOid = lo_creat(conn, INV_READ | INV_WRITE);
688  else
689  lobjOid = lo_create(conn, oid);
690 
691  if (lobjOid == InvalidOid)
692  {
693  /* we assume lo_create() already set a suitable error message */
694  (void) close(fd);
695  return InvalidOid;
696  }
697 
698  lobj = lo_open(conn, lobjOid, INV_WRITE);
699  if (lobj == -1)
700  {
701  /* we assume lo_open() already set a suitable error message */
702  (void) close(fd);
703  return InvalidOid;
704  }
705 
706  /*
707  * read in from the file and write to the large object
708  */
709  while ((nbytes = read(fd, buf, LO_BUFSIZE)) > 0)
710  {
711  tmp = lo_write(conn, lobj, buf, nbytes);
712  if (tmp != nbytes)
713  {
714  /*
715  * If lo_write() failed, we are now in an aborted transaction so
716  * there's no need for lo_close(); furthermore, if we tried it
717  * we'd overwrite the useful error result with a useless one. So
718  * just nail the doors shut and get out of town.
719  */
720  (void) close(fd);
721  return InvalidOid;
722  }
723  }
724 
725  if (nbytes < 0)
726  {
727  /* We must do lo_close before setting the errorMessage */
728  int save_errno = errno;
729 
730  (void) lo_close(conn, lobj);
731  (void) close(fd);
732  /* deliberately overwrite any error from lo_close */
735  libpq_gettext("could not read from file \"%s\": %s\n"),
736  filename,
737  strerror_r(save_errno, sebuf, sizeof(sebuf)));
738  return InvalidOid;
739  }
740 
741  (void) close(fd);
742 
743  if (lo_close(conn, lobj) != 0)
744  {
745  /* we assume lo_close() already set a suitable error message */
746  return InvalidOid;
747  }
748 
749  return lobjOid;
750 }
751 
752 /*
753  * lo_export -
754  * exports an (inversion) large object.
755  * returns -1 upon failure, 1 if OK
756  */
757 int
758 lo_export(PGconn *conn, Oid lobjId, const char *filename)
759 {
760  int result = 1;
761  int fd;
762  int nbytes,
763  tmp;
764  char buf[LO_BUFSIZE];
765  int lobj;
766  char sebuf[PG_STRERROR_R_BUFLEN];
767 
768  /*
769  * open the large object.
770  */
771  lobj = lo_open(conn, lobjId, INV_READ);
772  if (lobj == -1)
773  {
774  /* we assume lo_open() already set a suitable error message */
775  return -1;
776  }
777 
778  /*
779  * create the file to be written to
780  */
781  fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666);
782  if (fd < 0)
783  {
784  /* We must do lo_close before setting the errorMessage */
785  int save_errno = errno;
786 
787  (void) lo_close(conn, lobj);
788  /* deliberately overwrite any error from lo_close */
791  libpq_gettext("could not open file \"%s\": %s\n"),
792  filename,
793  strerror_r(save_errno, sebuf, sizeof(sebuf)));
794  return -1;
795  }
796 
797  /*
798  * read in from the large object and write to the file
799  */
800  while ((nbytes = lo_read(conn, lobj, buf, LO_BUFSIZE)) > 0)
801  {
802  tmp = write(fd, buf, nbytes);
803  if (tmp != nbytes)
804  {
805  /* We must do lo_close before setting the errorMessage */
806  int save_errno = errno;
807 
808  (void) lo_close(conn, lobj);
809  (void) close(fd);
810  /* deliberately overwrite any error from lo_close */
813  libpq_gettext("could not write to file \"%s\": %s\n"),
814  filename,
815  strerror_r(save_errno, sebuf, sizeof(sebuf)));
816  return -1;
817  }
818  }
819 
820  /*
821  * If lo_read() failed, we are now in an aborted transaction so there's no
822  * need for lo_close(); furthermore, if we tried it we'd overwrite the
823  * useful error result with a useless one. So skip lo_close() if we got a
824  * failure result.
825  */
826  if (nbytes < 0 ||
827  lo_close(conn, lobj) != 0)
828  {
829  /* assume lo_read() or lo_close() left a suitable error message */
830  result = -1;
831  }
832 
833  /* if we already failed, don't overwrite that msg with a close error */
834  if (close(fd) != 0 && result >= 0)
835  {
837  libpq_gettext("could not write to file \"%s\": %s\n"),
838  filename, strerror_r(errno, sebuf, sizeof(sebuf)));
839  result = -1;
840  }
841 
842  return result;
843 }
844 
845 
846 /*
847  * lo_initialize
848  *
849  * Initialize for a new large-object operation on an existing connection.
850  * Return 0 if OK, -1 on failure.
851  *
852  * If we haven't previously done so, we collect the function OIDs from
853  * pg_proc for all functions that are required for large object operations.
854  */
855 static int
857 {
858  PGresult *res;
859  PGlobjfuncs *lobjfuncs;
860  int n;
861  const char *query;
862  const char *fname;
863  Oid foid;
864 
865  /* Nothing we can do with no connection */
866  if (conn == NULL)
867  return -1;
868 
869  /* Since this is the beginning of a query cycle, reset the error state */
871 
872  /* Nothing else to do if we already collected info */
873  if (conn->lobjfuncs != NULL)
874  return 0;
875 
876  /*
877  * Allocate the structure to hold the function OIDs. We don't store it
878  * into the PGconn until it's successfully filled.
879  */
880  lobjfuncs = (PGlobjfuncs *) malloc(sizeof(PGlobjfuncs));
881  if (lobjfuncs == NULL)
882  {
884  libpq_gettext("out of memory\n"));
885  return -1;
886  }
887  MemSet((char *) lobjfuncs, 0, sizeof(PGlobjfuncs));
888 
889  /*
890  * Execute the query to get all the functions at once. (Not all of them
891  * may exist in older server versions.)
892  */
893  query = "select proname, oid from pg_catalog.pg_proc "
894  "where proname in ("
895  "'lo_open', "
896  "'lo_close', "
897  "'lo_creat', "
898  "'lo_create', "
899  "'lo_unlink', "
900  "'lo_lseek', "
901  "'lo_lseek64', "
902  "'lo_tell', "
903  "'lo_tell64', "
904  "'lo_truncate', "
905  "'lo_truncate64', "
906  "'loread', "
907  "'lowrite') "
908  "and pronamespace = (select oid from pg_catalog.pg_namespace "
909  "where nspname = 'pg_catalog')";
910 
911  res = PQexec(conn, query);
912  if (res == NULL)
913  {
914  free(lobjfuncs);
915  return -1;
916  }
917 
919  {
920  free(lobjfuncs);
921  PQclear(res);
923  libpq_gettext("query to initialize large object functions did not return data\n"));
924  return -1;
925  }
926 
927  /*
928  * Examine the result and put the OID's into the struct
929  */
930  for (n = 0; n < PQntuples(res); n++)
931  {
932  fname = PQgetvalue(res, n, 0);
933  foid = (Oid) atoi(PQgetvalue(res, n, 1));
934  if (strcmp(fname, "lo_open") == 0)
935  lobjfuncs->fn_lo_open = foid;
936  else if (strcmp(fname, "lo_close") == 0)
937  lobjfuncs->fn_lo_close = foid;
938  else if (strcmp(fname, "lo_creat") == 0)
939  lobjfuncs->fn_lo_creat = foid;
940  else if (strcmp(fname, "lo_create") == 0)
941  lobjfuncs->fn_lo_create = foid;
942  else if (strcmp(fname, "lo_unlink") == 0)
943  lobjfuncs->fn_lo_unlink = foid;
944  else if (strcmp(fname, "lo_lseek") == 0)
945  lobjfuncs->fn_lo_lseek = foid;
946  else if (strcmp(fname, "lo_lseek64") == 0)
947  lobjfuncs->fn_lo_lseek64 = foid;
948  else if (strcmp(fname, "lo_tell") == 0)
949  lobjfuncs->fn_lo_tell = foid;
950  else if (strcmp(fname, "lo_tell64") == 0)
951  lobjfuncs->fn_lo_tell64 = foid;
952  else if (strcmp(fname, "lo_truncate") == 0)
953  lobjfuncs->fn_lo_truncate = foid;
954  else if (strcmp(fname, "lo_truncate64") == 0)
955  lobjfuncs->fn_lo_truncate64 = foid;
956  else if (strcmp(fname, "loread") == 0)
957  lobjfuncs->fn_lo_read = foid;
958  else if (strcmp(fname, "lowrite") == 0)
959  lobjfuncs->fn_lo_write = foid;
960  }
961 
962  PQclear(res);
963 
964  /*
965  * Finally check that we got all required large object interface functions
966  * (ones that have been added later than the stone age are instead checked
967  * only if used)
968  */
969  if (lobjfuncs->fn_lo_open == 0)
970  {
972  libpq_gettext("cannot determine OID of function %s\n"),
973  "lo_open");
974  free(lobjfuncs);
975  return -1;
976  }
977  if (lobjfuncs->fn_lo_close == 0)
978  {
980  libpq_gettext("cannot determine OID of function %s\n"),
981  "lo_close");
982  free(lobjfuncs);
983  return -1;
984  }
985  if (lobjfuncs->fn_lo_creat == 0)
986  {
988  libpq_gettext("cannot determine OID of function %s\n"),
989  "lo_creat");
990  free(lobjfuncs);
991  return -1;
992  }
993  if (lobjfuncs->fn_lo_unlink == 0)
994  {
996  libpq_gettext("cannot determine OID of function %s\n"),
997  "lo_unlink");
998  free(lobjfuncs);
999  return -1;
1000  }
1001  if (lobjfuncs->fn_lo_lseek == 0)
1002  {
1004  libpq_gettext("cannot determine OID of function %s\n"),
1005  "lo_lseek");
1006  free(lobjfuncs);
1007  return -1;
1008  }
1009  if (lobjfuncs->fn_lo_tell == 0)
1010  {
1012  libpq_gettext("cannot determine OID of function %s\n"),
1013  "lo_tell");
1014  free(lobjfuncs);
1015  return -1;
1016  }
1017  if (lobjfuncs->fn_lo_read == 0)
1018  {
1020  libpq_gettext("cannot determine OID of function %s\n"),
1021  "loread");
1022  free(lobjfuncs);
1023  return -1;
1024  }
1025  if (lobjfuncs->fn_lo_write == 0)
1026  {
1028  libpq_gettext("cannot determine OID of function %s\n"),
1029  "lowrite");
1030  free(lobjfuncs);
1031  return -1;
1032  }
1033 
1034  /*
1035  * Put the structure into the connection control
1036  */
1037  conn->lobjfuncs = lobjfuncs;
1038  return 0;
1039 }
1040 
1041 /*
1042  * lo_hton64
1043  * converts a 64-bit integer from host byte order to network byte order
1044  */
1045 static pg_int64
1047 {
1048  union
1049  {
1050  pg_int64 i64;
1051  uint32 i32[2];
1052  } swap;
1053  uint32 t;
1054 
1055  /* High order half first, since we're doing MSB-first */
1056  t = (uint32) (host64 >> 32);
1057  swap.i32[0] = pg_hton32(t);
1058 
1059  /* Now the low order half */
1060  t = (uint32) host64;
1061  swap.i32[1] = pg_hton32(t);
1062 
1063  return swap.i64;
1064 }
1065 
1066 /*
1067  * lo_ntoh64
1068  * converts a 64-bit integer from network byte order to host byte order
1069  */
1070 static pg_int64
1072 {
1073  union
1074  {
1075  pg_int64 i64;
1076  uint32 i32[2];
1077  } swap;
1078  pg_int64 result;
1079 
1080  swap.i64 = net64;
1081 
1082  result = (uint32) pg_ntoh32(swap.i32[0]);
1083  result <<= 32;
1084  result |= (uint32) pg_ntoh32(swap.i32[1]);
1085 
1086  return result;
1087 }
#define unconstify(underlying_type, expr)
Definition: c.h:1240
unsigned int uint32
Definition: c.h:441
#define PG_BINARY
Definition: c.h:1268
#define MemSet(start, val, len)
Definition: c.h:1008
PGresult * PQfn(PGconn *conn, int fnid, int *result_buf, int *result_len, int result_is_int, const PQArgBlock *args, int nargs)
Definition: fe-exec.c:2916
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3270
void PQclear(PGresult *res)
Definition: fe-exec.c:718
int PQntuples(const PGresult *res)
Definition: fe-exec.c:3340
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2273
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3735
static Oid lo_import_internal(PGconn *conn, const char *filename, Oid oid)
Definition: fe-lobj.c:655
int lo_tell(PGconn *conn, int fd)
Definition: fe-lobj.c:522
static pg_int64 lo_ntoh64(pg_int64 net64)
Definition: fe-lobj.c:1071
int lo_close(PGconn *conn, int fd)
Definition: fe-lobj.c:96
pg_int64 lo_tell64(PGconn *conn, int fd)
Definition: fe-lobj.c:555
#define LO_BUFSIZE
Definition: fe-lobj.c:42
static int lo_initialize(PGconn *conn)
Definition: fe-lobj.c:856
Oid lo_creat(PGconn *conn, int mode)
Definition: fe-lobj.c:444
int lo_read(PGconn *conn, int fd, char *buf, size_t len)
Definition: fe-lobj.c:248
int lo_lseek(PGconn *conn, int fd, int offset, int whence)
Definition: fe-lobj.c:349
pg_int64 lo_lseek64(PGconn *conn, int fd, pg_int64 offset, int whence)
Definition: fe-lobj.c:390
int lo_open(PGconn *conn, Oid lobjId, int mode)
Definition: fe-lobj.c:57
int lo_write(PGconn *conn, int fd, const char *buf, size_t len)
Definition: fe-lobj.c:299
int lo_truncate(PGconn *conn, int fd, size_t len)
Definition: fe-lobj.c:131
Oid lo_create(PGconn *conn, Oid lobjId)
Definition: fe-lobj.c:480
int lo_unlink(PGconn *conn, Oid lobjId)
Definition: fe-lobj.c:597
Oid lo_import(PGconn *conn, const char *filename)
Definition: fe-lobj.c:634
static pg_int64 lo_hton64(pg_int64 host64)
Definition: fe-lobj.c:1046
int lo_export(PGconn *conn, Oid lobjId, const char *filename)
Definition: fe-lobj.c:758
Oid lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId)
Definition: fe-lobj.c:649
int lo_truncate64(PGconn *conn, int fd, pg_int64 len)
Definition: fe-lobj.c:197
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
#define close(a)
Definition: win32.h:12
#define write(a, b, c)
Definition: win32.h:14
#define read(a, b, c)
Definition: win32.h:13
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:97
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:100
#define INV_READ
Definition: libpq-fs.h:22
#define INV_WRITE
Definition: libpq-fs.h:21
#define libpq_gettext(x)
Definition: libpq-int.h:878
#define pqClearConnErrorState(conn)
Definition: libpq-int.h:851
#define pg_ntoh32(x)
Definition: pg_bswap.h:125
#define pg_hton32(x)
Definition: pg_bswap.h:121
static PgChecksumMode mode
Definition: pg_checksums.c:65
const void size_t len
static char * filename
Definition: pg_dumpall.c:94
static char * buf
Definition: pg_test_fsync.c:67
#define PG_STRERROR_R_BUFLEN
Definition: port.h:243
#define strerror_r
Definition: port.h:242
#define InvalidOid
Definition: postgres_ext.h:36
PG_INT64_TYPE pg_int64
Definition: postgres_ext.h:47
unsigned int Oid
Definition: postgres_ext.h:31
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:267
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:369
static int fd(const char *x, int i)
Definition: preproc-init.c:105
PGconn * conn
Definition: streamutil.c:54
int isint
Definition: libpq-fe.h:249
int * ptr
Definition: libpq-fe.h:252
int integer
Definition: libpq-fe.h:253
union PQArgBlock::@170 u
Oid fn_lo_tell64
Definition: libpq-int.h:278
Oid fn_lo_write
Definition: libpq-int.h:282
Oid fn_lo_creat
Definition: libpq-int.h:272
Oid fn_lo_unlink
Definition: libpq-int.h:274
Oid fn_lo_open
Definition: libpq-int.h:270
Oid fn_lo_truncate64
Definition: libpq-int.h:280
Oid fn_lo_close
Definition: libpq-int.h:271
Oid fn_lo_create
Definition: libpq-int.h:273
Oid fn_lo_truncate
Definition: libpq-int.h:279
Oid fn_lo_lseek
Definition: libpq-int.h:275
Oid fn_lo_tell
Definition: libpq-int.h:277
Oid fn_lo_lseek64
Definition: libpq-int.h:276
Oid fn_lo_read
Definition: libpq-int.h:281
PQExpBufferData errorMessage
Definition: libpq-int.h:585
PGlobjfuncs * lobjfuncs
Definition: libpq-int.h:476
ExecStatusType resultStatus
Definition: libpq-int.h:178