PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
be-fsstubs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * be-fsstubs.c
4  * Builtin functions for open/close/read/write operations on large objects
5  *
6  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/libpq/be-fsstubs.c
12  *
13  * NOTES
14  * This should be moved to a more appropriate place. It is here
15  * for lack of a better place.
16  *
17  * These functions store LargeObjectDesc structs in a private MemoryContext,
18  * which means that large object descriptors hang around until we destroy
19  * the context at transaction end. It'd be possible to prolong the lifetime
20  * of the context so that LO FDs are good across transactions (for example,
21  * we could release the context only if we see that no FDs remain open).
22  * But we'd need additional state in order to do the right thing at the
23  * end of an aborted transaction. FDs opened during an aborted xact would
24  * still need to be closed, since they might not be pointing at valid
25  * relations at all. Locking semantics are also an interesting problem
26  * if LOs stay open across transactions. For now, we'll stick with the
27  * existing documented semantics of LO FDs: they're only good within a
28  * transaction.
29  *
30  * As of PostgreSQL 8.0, much of the angst expressed above is no longer
31  * relevant, and in fact it'd be pretty easy to allow LO FDs to stay
32  * open across transactions. (Snapshot relevancy would still be an issue.)
33  * However backwards compatibility suggests that we should stick to the
34  * status quo.
35  *
36  *-------------------------------------------------------------------------
37  */
38 
39 #include "postgres.h"
40 
41 #include <fcntl.h>
42 #include <sys/stat.h>
43 #include <unistd.h>
44 
45 #include "libpq/be-fsstubs.h"
46 #include "libpq/libpq-fs.h"
47 #include "miscadmin.h"
48 #include "storage/fd.h"
49 #include "storage/large_object.h"
50 #include "utils/acl.h"
51 #include "utils/builtins.h"
52 #include "utils/memutils.h"
53 
54 /*
55  * compatibility flag for permission checks
56  */
58 
59 /* define this to enable debug logging */
60 /* #define FSDB 1 */
61 /* chunk size for lo_import/lo_export transfers */
62 #define BUFSIZE 8192
63 
64 /*
65  * LO "FD"s are indexes into the cookies array.
66  *
67  * A non-null entry is a pointer to a LargeObjectDesc allocated in the
68  * LO private memory context "fscxt". The cookies array itself is also
69  * dynamically allocated in that context. Its current allocated size is
70  * cookies_len entries, of which any unused entries will be NULL.
71  */
72 static LargeObjectDesc **cookies = NULL;
73 static int cookies_size = 0;
74 
75 static MemoryContext fscxt = NULL;
76 
77 #define CreateFSContext() \
78  do { \
79  if (fscxt == NULL) \
80  fscxt = AllocSetContextCreate(TopMemoryContext, \
81  "Filesystem", \
82  ALLOCSET_DEFAULT_SIZES); \
83  } while (0)
84 
85 
86 static int newLOfd(LargeObjectDesc *lobjCookie);
87 static void deleteLOfd(int fd);
88 static Oid lo_import_internal(text *filename, Oid lobjOid);
89 
90 
91 /*****************************************************************************
92  * File Interfaces for Large Objects
93  *****************************************************************************/
94 
95 Datum
97 {
98  Oid lobjId = PG_GETARG_OID(0);
99  int32 mode = PG_GETARG_INT32(1);
100  LargeObjectDesc *lobjDesc;
101  int fd;
102 
103 #if FSDB
104  elog(DEBUG4, "lo_open(%u,%d)", lobjId, mode);
105 #endif
106 
107  CreateFSContext();
108 
109  lobjDesc = inv_open(lobjId, mode, fscxt);
110 
111  if (lobjDesc == NULL)
112  { /* lookup failed */
113 #if FSDB
114  elog(DEBUG4, "could not open large object %u", lobjId);
115 #endif
116  PG_RETURN_INT32(-1);
117  }
118 
119  fd = newLOfd(lobjDesc);
120 
121  PG_RETURN_INT32(fd);
122 }
123 
124 Datum
126 {
127  int32 fd = PG_GETARG_INT32(0);
128 
129  if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
130  ereport(ERROR,
131  (errcode(ERRCODE_UNDEFINED_OBJECT),
132  errmsg("invalid large-object descriptor: %d", fd)));
133 
134 #if FSDB
135  elog(DEBUG4, "lo_close(%d)", fd);
136 #endif
137 
138  inv_close(cookies[fd]);
139 
140  deleteLOfd(fd);
141 
142  PG_RETURN_INT32(0);
143 }
144 
145 
146 /*****************************************************************************
147  * Bare Read/Write operations --- these are not fmgr-callable!
148  *
149  * We assume the large object supports byte oriented reads and seeks so
150  * that our work is easier.
151  *
152  *****************************************************************************/
153 
154 int
155 lo_read(int fd, char *buf, int len)
156 {
157  int status;
158  LargeObjectDesc *lobj;
159 
160  if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
161  ereport(ERROR,
162  (errcode(ERRCODE_UNDEFINED_OBJECT),
163  errmsg("invalid large-object descriptor: %d", fd)));
164  lobj = cookies[fd];
165 
166  /* We don't bother to check IFS_RDLOCK, since it's always set */
167 
168  /* Permission checks --- first time through only */
169  if ((lobj->flags & IFS_RD_PERM_OK) == 0)
170  {
171  if (!lo_compat_privileges &&
173  GetUserId(),
174  ACL_SELECT,
175  lobj->snapshot) != ACLCHECK_OK)
176  ereport(ERROR,
177  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
178  errmsg("permission denied for large object %u",
179  lobj->id)));
180  lobj->flags |= IFS_RD_PERM_OK;
181  }
182 
183  status = inv_read(lobj, buf, len);
184 
185  return status;
186 }
187 
188 int
189 lo_write(int fd, const char *buf, int len)
190 {
191  int status;
192  LargeObjectDesc *lobj;
193 
194  if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
195  ereport(ERROR,
196  (errcode(ERRCODE_UNDEFINED_OBJECT),
197  errmsg("invalid large-object descriptor: %d", fd)));
198  lobj = cookies[fd];
199 
200  if ((lobj->flags & IFS_WRLOCK) == 0)
201  ereport(ERROR,
202  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
203  errmsg("large object descriptor %d was not opened for writing",
204  fd)));
205 
206  /* Permission checks --- first time through only */
207  if ((lobj->flags & IFS_WR_PERM_OK) == 0)
208  {
209  if (!lo_compat_privileges &&
211  GetUserId(),
212  ACL_UPDATE,
213  lobj->snapshot) != ACLCHECK_OK)
214  ereport(ERROR,
215  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
216  errmsg("permission denied for large object %u",
217  lobj->id)));
218  lobj->flags |= IFS_WR_PERM_OK;
219  }
220 
221  status = inv_write(lobj, buf, len);
222 
223  return status;
224 }
225 
226 Datum
228 {
229  int32 fd = PG_GETARG_INT32(0);
230  int32 offset = PG_GETARG_INT32(1);
231  int32 whence = PG_GETARG_INT32(2);
232  int64 status;
233 
234  if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
235  ereport(ERROR,
236  (errcode(ERRCODE_UNDEFINED_OBJECT),
237  errmsg("invalid large-object descriptor: %d", fd)));
238 
239  status = inv_seek(cookies[fd], offset, whence);
240 
241  /* guard against result overflow */
242  if (status != (int32) status)
243  ereport(ERROR,
244  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
245  errmsg("lo_lseek result out of range for large-object descriptor %d",
246  fd)));
247 
248  PG_RETURN_INT32((int32) status);
249 }
250 
251 Datum
253 {
254  int32 fd = PG_GETARG_INT32(0);
255  int64 offset = PG_GETARG_INT64(1);
256  int32 whence = PG_GETARG_INT32(2);
257  int64 status;
258 
259  if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
260  ereport(ERROR,
261  (errcode(ERRCODE_UNDEFINED_OBJECT),
262  errmsg("invalid large-object descriptor: %d", fd)));
263 
264  status = inv_seek(cookies[fd], offset, whence);
265 
266  PG_RETURN_INT64(status);
267 }
268 
269 Datum
271 {
272  Oid lobjId;
273 
274  /*
275  * We don't actually need to store into fscxt, but create it anyway to
276  * ensure that AtEOXact_LargeObject knows there is state to clean up
277  */
278  CreateFSContext();
279 
280  lobjId = inv_create(InvalidOid);
281 
282  PG_RETURN_OID(lobjId);
283 }
284 
285 Datum
287 {
288  Oid lobjId = PG_GETARG_OID(0);
289 
290  /*
291  * We don't actually need to store into fscxt, but create it anyway to
292  * ensure that AtEOXact_LargeObject knows there is state to clean up
293  */
294  CreateFSContext();
295 
296  lobjId = inv_create(lobjId);
297 
298  PG_RETURN_OID(lobjId);
299 }
300 
301 Datum
303 {
304  int32 fd = PG_GETARG_INT32(0);
305  int64 offset;
306 
307  if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
308  ereport(ERROR,
309  (errcode(ERRCODE_UNDEFINED_OBJECT),
310  errmsg("invalid large-object descriptor: %d", fd)));
311 
312  offset = inv_tell(cookies[fd]);
313 
314  /* guard against result overflow */
315  if (offset != (int32) offset)
316  ereport(ERROR,
317  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
318  errmsg("lo_tell result out of range for large-object descriptor %d",
319  fd)));
320 
321  PG_RETURN_INT32((int32) offset);
322 }
323 
324 Datum
326 {
327  int32 fd = PG_GETARG_INT32(0);
328  int64 offset;
329 
330  if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
331  ereport(ERROR,
332  (errcode(ERRCODE_UNDEFINED_OBJECT),
333  errmsg("invalid large-object descriptor: %d", fd)));
334 
335  offset = inv_tell(cookies[fd]);
336 
337  PG_RETURN_INT64(offset);
338 }
339 
340 Datum
342 {
343  Oid lobjId = PG_GETARG_OID(0);
344 
345  /* Must be owner of the largeobject */
346  if (!lo_compat_privileges &&
348  ereport(ERROR,
349  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
350  errmsg("must be owner of large object %u", lobjId)));
351 
352  /*
353  * If there are any open LO FDs referencing that ID, close 'em.
354  */
355  if (fscxt != NULL)
356  {
357  int i;
358 
359  for (i = 0; i < cookies_size; i++)
360  {
361  if (cookies[i] != NULL && cookies[i]->id == lobjId)
362  {
363  inv_close(cookies[i]);
364  deleteLOfd(i);
365  }
366  }
367  }
368 
369  /*
370  * inv_drop does not create a need for end-of-transaction cleanup and
371  * hence we don't need to have created fscxt.
372  */
373  PG_RETURN_INT32(inv_drop(lobjId));
374 }
375 
376 /*****************************************************************************
377  * Read/Write using bytea
378  *****************************************************************************/
379 
380 Datum
382 {
383  int32 fd = PG_GETARG_INT32(0);
384  int32 len = PG_GETARG_INT32(1);
385  bytea *retval;
386  int totalread;
387 
388  if (len < 0)
389  len = 0;
390 
391  retval = (bytea *) palloc(VARHDRSZ + len);
392  totalread = lo_read(fd, VARDATA(retval), len);
393  SET_VARSIZE(retval, totalread + VARHDRSZ);
394 
395  PG_RETURN_BYTEA_P(retval);
396 }
397 
398 Datum
400 {
401  int32 fd = PG_GETARG_INT32(0);
402  bytea *wbuf = PG_GETARG_BYTEA_PP(1);
403  int bytestowrite;
404  int totalwritten;
405 
406  bytestowrite = VARSIZE_ANY_EXHDR(wbuf);
407  totalwritten = lo_write(fd, VARDATA_ANY(wbuf), bytestowrite);
408  PG_RETURN_INT32(totalwritten);
409 }
410 
411 /*****************************************************************************
412  * Import/Export of Large Object
413  *****************************************************************************/
414 
415 /*
416  * lo_import -
417  * imports a file as an (inversion) large object.
418  */
419 Datum
421 {
423 
425 }
426 
427 /*
428  * lo_import_with_oid -
429  * imports a file as an (inversion) large object specifying oid.
430  */
431 Datum
433 {
435  Oid oid = PG_GETARG_OID(1);
436 
437  PG_RETURN_OID(lo_import_internal(filename, oid));
438 }
439 
440 static Oid
442 {
443  int fd;
444  int nbytes,
446  char buf[BUFSIZE];
447  char fnamebuf[MAXPGPATH];
448  LargeObjectDesc *lobj;
449  Oid oid;
450 
451 #ifndef ALLOW_DANGEROUS_LO_FUNCTIONS
452  if (!superuser())
453  ereport(ERROR,
454  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
455  errmsg("must be superuser to use server-side lo_import()"),
456  errhint("Anyone can use the client-side lo_import() provided by libpq.")));
457 #endif
458 
459  CreateFSContext();
460 
461  /*
462  * open the file to be read in
463  */
464  text_to_cstring_buffer(filename, fnamebuf, sizeof(fnamebuf));
465  fd = OpenTransientFile(fnamebuf, O_RDONLY | PG_BINARY);
466  if (fd < 0)
467  ereport(ERROR,
469  errmsg("could not open server file \"%s\": %m",
470  fnamebuf)));
471 
472  /*
473  * create an inversion object
474  */
475  oid = inv_create(lobjOid);
476 
477  /*
478  * read in from the filesystem and write to the inversion object
479  */
480  lobj = inv_open(oid, INV_WRITE, fscxt);
481 
482  while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
483  {
484  tmp = inv_write(lobj, buf, nbytes);
485  Assert(tmp == nbytes);
486  }
487 
488  if (nbytes < 0)
489  ereport(ERROR,
491  errmsg("could not read server file \"%s\": %m",
492  fnamebuf)));
493 
494  inv_close(lobj);
495  CloseTransientFile(fd);
496 
497  return oid;
498 }
499 
500 /*
501  * lo_export -
502  * exports an (inversion) large object.
503  */
504 Datum
506 {
507  Oid lobjId = PG_GETARG_OID(0);
509  int fd;
510  int nbytes,
511  tmp;
512  char buf[BUFSIZE];
513  char fnamebuf[MAXPGPATH];
514  LargeObjectDesc *lobj;
515  mode_t oumask;
516 
517 #ifndef ALLOW_DANGEROUS_LO_FUNCTIONS
518  if (!superuser())
519  ereport(ERROR,
520  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
521  errmsg("must be superuser to use server-side lo_export()"),
522  errhint("Anyone can use the client-side lo_export() provided by libpq.")));
523 #endif
524 
525  CreateFSContext();
526 
527  /*
528  * open the inversion object (no need to test for failure)
529  */
530  lobj = inv_open(lobjId, INV_READ, fscxt);
531 
532  /*
533  * open the file to be written to
534  *
535  * Note: we reduce backend's normal 077 umask to the slightly friendlier
536  * 022. This code used to drop it all the way to 0, but creating
537  * world-writable export files doesn't seem wise.
538  */
539  text_to_cstring_buffer(filename, fnamebuf, sizeof(fnamebuf));
540  oumask = umask(S_IWGRP | S_IWOTH);
541  PG_TRY();
542  {
543  fd = OpenTransientFilePerm(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY,
544  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
545  }
546  PG_CATCH();
547  {
548  umask(oumask);
549  PG_RE_THROW();
550  }
551  PG_END_TRY();
552  umask(oumask);
553  if (fd < 0)
554  ereport(ERROR,
556  errmsg("could not create server file \"%s\": %m",
557  fnamebuf)));
558 
559  /*
560  * read in from the inversion file and write to the filesystem
561  */
562  while ((nbytes = inv_read(lobj, buf, BUFSIZE)) > 0)
563  {
564  tmp = write(fd, buf, nbytes);
565  if (tmp != nbytes)
566  ereport(ERROR,
568  errmsg("could not write server file \"%s\": %m",
569  fnamebuf)));
570  }
571 
572  CloseTransientFile(fd);
573  inv_close(lobj);
574 
575  PG_RETURN_INT32(1);
576 }
577 
578 /*
579  * lo_truncate -
580  * truncate a large object to a specified length
581  */
582 static void
584 {
585  LargeObjectDesc *lobj;
586 
587  if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
588  ereport(ERROR,
589  (errcode(ERRCODE_UNDEFINED_OBJECT),
590  errmsg("invalid large-object descriptor: %d", fd)));
591  lobj = cookies[fd];
592 
593  if ((lobj->flags & IFS_WRLOCK) == 0)
594  ereport(ERROR,
595  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
596  errmsg("large object descriptor %d was not opened for writing",
597  fd)));
598 
599  /* Permission checks --- first time through only */
600  if ((lobj->flags & IFS_WR_PERM_OK) == 0)
601  {
602  if (!lo_compat_privileges &&
604  GetUserId(),
605  ACL_UPDATE,
606  lobj->snapshot) != ACLCHECK_OK)
607  ereport(ERROR,
608  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
609  errmsg("permission denied for large object %u",
610  lobj->id)));
611  lobj->flags |= IFS_WR_PERM_OK;
612  }
613 
614  inv_truncate(lobj, len);
615 }
616 
617 Datum
619 {
620  int32 fd = PG_GETARG_INT32(0);
621  int32 len = PG_GETARG_INT32(1);
622 
623  lo_truncate_internal(fd, len);
624  PG_RETURN_INT32(0);
625 }
626 
627 Datum
629 {
630  int32 fd = PG_GETARG_INT32(0);
631  int64 len = PG_GETARG_INT64(1);
632 
633  lo_truncate_internal(fd, len);
634  PG_RETURN_INT32(0);
635 }
636 
637 /*
638  * AtEOXact_LargeObject -
639  * prepares large objects for transaction commit
640  */
641 void
642 AtEOXact_LargeObject(bool isCommit)
643 {
644  int i;
645 
646  if (fscxt == NULL)
647  return; /* no LO operations in this xact */
648 
649  /*
650  * Close LO fds and clear cookies array so that LO fds are no longer good.
651  * On abort we skip the close step.
652  */
653  for (i = 0; i < cookies_size; i++)
654  {
655  if (cookies[i] != NULL)
656  {
657  if (isCommit)
658  inv_close(cookies[i]);
659  deleteLOfd(i);
660  }
661  }
662 
663  /* Needn't actually pfree since we're about to zap context */
664  cookies = NULL;
665  cookies_size = 0;
666 
667  /* Release the LO memory context to prevent permanent memory leaks. */
668  MemoryContextDelete(fscxt);
669  fscxt = NULL;
670 
671  /* Give inv_api.c a chance to clean up, too */
672  close_lo_relation(isCommit);
673 }
674 
675 /*
676  * AtEOSubXact_LargeObject
677  * Take care of large objects at subtransaction commit/abort
678  *
679  * Reassign LOs created/opened during a committing subtransaction
680  * to the parent subtransaction. On abort, just close them.
681  */
682 void
684  SubTransactionId parentSubid)
685 {
686  int i;
687 
688  if (fscxt == NULL) /* no LO operations in this xact */
689  return;
690 
691  for (i = 0; i < cookies_size; i++)
692  {
693  LargeObjectDesc *lo = cookies[i];
694 
695  if (lo != NULL && lo->subid == mySubid)
696  {
697  if (isCommit)
698  lo->subid = parentSubid;
699  else
700  {
701  /*
702  * Make sure we do not call inv_close twice if it errors out
703  * for some reason. Better a leak than a crash.
704  */
705  deleteLOfd(i);
706  inv_close(lo);
707  }
708  }
709  }
710 }
711 
712 /*****************************************************************************
713  * Support routines for this file
714  *****************************************************************************/
715 
716 static int
718 {
719  int i,
720  newsize;
721 
722  /* Try to find a free slot */
723  for (i = 0; i < cookies_size; i++)
724  {
725  if (cookies[i] == NULL)
726  {
727  cookies[i] = lobjCookie;
728  return i;
729  }
730  }
731 
732  /* No free slot, so make the array bigger */
733  if (cookies_size <= 0)
734  {
735  /* First time through, arbitrarily make 64-element array */
736  i = 0;
737  newsize = 64;
738  cookies = (LargeObjectDesc **)
739  MemoryContextAllocZero(fscxt, newsize * sizeof(LargeObjectDesc *));
740  cookies_size = newsize;
741  }
742  else
743  {
744  /* Double size of array */
745  i = cookies_size;
746  newsize = cookies_size * 2;
747  cookies = (LargeObjectDesc **)
748  repalloc(cookies, newsize * sizeof(LargeObjectDesc *));
749  MemSet(cookies + cookies_size, 0,
750  (newsize - cookies_size) * sizeof(LargeObjectDesc *));
751  cookies_size = newsize;
752  }
753 
754  Assert(cookies[i] == NULL);
755  cookies[i] = lobjCookie;
756  return i;
757 }
758 
759 static void
761 {
762  cookies[fd] = NULL;
763 }
764 
765 /*****************************************************************************
766  * Wrappers oriented toward SQL callers
767  *****************************************************************************/
768 
769 /*
770  * Read [offset, offset+nbytes) within LO; when nbytes is -1, read to end.
771  */
772 static bytea *
773 lo_get_fragment_internal(Oid loOid, int64 offset, int32 nbytes)
774 {
775  LargeObjectDesc *loDesc;
776  int64 loSize;
777  int64 result_length;
778  int total_read PG_USED_FOR_ASSERTS_ONLY;
779  bytea *result = NULL;
780 
781  /*
782  * We don't actually need to store into fscxt, but create it anyway to
783  * ensure that AtEOXact_LargeObject knows there is state to clean up
784  */
785  CreateFSContext();
786 
787  loDesc = inv_open(loOid, INV_READ, fscxt);
788 
789  /* Permission check */
790  if (!lo_compat_privileges &&
792  GetUserId(),
793  ACL_SELECT,
794  loDesc->snapshot) != ACLCHECK_OK)
795  ereport(ERROR,
796  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
797  errmsg("permission denied for large object %u",
798  loDesc->id)));
799 
800  /*
801  * Compute number of bytes we'll actually read, accommodating nbytes == -1
802  * and reads beyond the end of the LO.
803  */
804  loSize = inv_seek(loDesc, 0, SEEK_END);
805  if (loSize > offset)
806  {
807  if (nbytes >= 0 && nbytes <= loSize - offset)
808  result_length = nbytes; /* request is wholly inside LO */
809  else
810  result_length = loSize - offset; /* adjust to end of LO */
811  }
812  else
813  result_length = 0; /* request is wholly outside LO */
814 
815  /*
816  * A result_length calculated from loSize may not fit in a size_t. Check
817  * that the size will satisfy this and subsequently-enforced size limits.
818  */
819  if (result_length > MaxAllocSize - VARHDRSZ)
820  ereport(ERROR,
821  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
822  errmsg("large object read request is too large")));
823 
824  result = (bytea *) palloc(VARHDRSZ + result_length);
825 
826  inv_seek(loDesc, offset, SEEK_SET);
827  total_read = inv_read(loDesc, VARDATA(result), result_length);
828  Assert(total_read == result_length);
829  SET_VARSIZE(result, result_length + VARHDRSZ);
830 
831  inv_close(loDesc);
832 
833  return result;
834 }
835 
836 /*
837  * Read entire LO
838  */
839 Datum
841 {
842  Oid loOid = PG_GETARG_OID(0);
843  bytea *result;
844 
845  result = lo_get_fragment_internal(loOid, 0, -1);
846 
847  PG_RETURN_BYTEA_P(result);
848 }
849 
850 /*
851  * Read range within LO
852  */
853 Datum
855 {
856  Oid loOid = PG_GETARG_OID(0);
857  int64 offset = PG_GETARG_INT64(1);
858  int32 nbytes = PG_GETARG_INT32(2);
859  bytea *result;
860 
861  if (nbytes < 0)
862  ereport(ERROR,
863  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
864  errmsg("requested length cannot be negative")));
865 
866  result = lo_get_fragment_internal(loOid, offset, nbytes);
867 
868  PG_RETURN_BYTEA_P(result);
869 }
870 
871 /*
872  * Create LO with initial contents given by a bytea argument
873  */
874 Datum
876 {
877  Oid loOid = PG_GETARG_OID(0);
878  bytea *str = PG_GETARG_BYTEA_PP(1);
879  LargeObjectDesc *loDesc;
880  int written PG_USED_FOR_ASSERTS_ONLY;
881 
882  CreateFSContext();
883 
884  loOid = inv_create(loOid);
885  loDesc = inv_open(loOid, INV_WRITE, fscxt);
886  written = inv_write(loDesc, VARDATA_ANY(str), VARSIZE_ANY_EXHDR(str));
887  Assert(written == VARSIZE_ANY_EXHDR(str));
888  inv_close(loDesc);
889 
890  PG_RETURN_OID(loOid);
891 }
892 
893 /*
894  * Update range within LO
895  */
896 Datum
898 {
899  Oid loOid = PG_GETARG_OID(0);
900  int64 offset = PG_GETARG_INT64(1);
901  bytea *str = PG_GETARG_BYTEA_PP(2);
902  LargeObjectDesc *loDesc;
903  int written PG_USED_FOR_ASSERTS_ONLY;
904 
905  CreateFSContext();
906 
907  loDesc = inv_open(loOid, INV_WRITE, fscxt);
908 
909  /* Permission check */
910  if (!lo_compat_privileges &&
912  GetUserId(),
913  ACL_UPDATE,
914  loDesc->snapshot) != ACLCHECK_OK)
915  ereport(ERROR,
916  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
917  errmsg("permission denied for large object %u",
918  loDesc->id)));
919 
920  inv_seek(loDesc, offset, SEEK_SET);
921  written = inv_write(loDesc, VARDATA_ANY(str), VARSIZE_ANY_EXHDR(str));
922  Assert(written == VARSIZE_ANY_EXHDR(str));
923  inv_close(loDesc);
924 
925  PG_RETURN_VOID();
926 }
Datum be_lo_get(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:840
static void deleteLOfd(int fd)
Definition: be-fsstubs.c:760
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int64 inv_seek(LargeObjectDesc *obj_desc, int64 offset, int whence)
Definition: inv_api.c:394
Datum be_lo_truncate(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:618
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
Datum be_lo_tell64(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:325
#define IFS_RD_PERM_OK
Definition: large_object.h:50
int errhint(const char *fmt,...)
Definition: elog.c:987
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define VARDATA(PTR)
Definition: postgres.h:303
void inv_truncate(LargeObjectDesc *obj_desc, int64 len)
Definition: inv_api.c:726
#define BUFSIZE
Definition: be-fsstubs.c:62
bool pg_largeobject_ownercheck(Oid lobj_oid, Oid roleid)
Definition: aclchk.c:4679
Oid inv_create(Oid lobjId)
Definition: inv_api.c:206
Oid GetUserId(void)
Definition: miscinit.c:284
#define write(a, b, c)
Definition: win32.h:14
#define PG_RETURN_INT64(x)
Definition: fmgr.h:327
Datum be_lo_tell(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:302
Datum be_loread(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:381
#define VARHDRSZ
Definition: c.h:439
Datum be_lo_put(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:897
static Oid lo_import_internal(text *filename, Oid lobjOid)
Definition: be-fsstubs.c:441
int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition: fd.c:2176
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:302
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
Datum be_lo_lseek(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:227
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define MemSet(start, val, len)
Definition: c.h:863
#define CreateFSContext()
Definition: be-fsstubs.c:77
uint32 SubTransactionId
Definition: c.h:395
static void lo_truncate_internal(int32 fd, int64 len)
Definition: be-fsstubs.c:583
Datum be_lo_from_bytea(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:875
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
unsigned int Oid
Definition: postgres_ext.h:31
#define DEBUG4
Definition: elog.h:22
SubTransactionId subid
Definition: large_object.h:43
static int fd(const char *x, int i)
Definition: preproc-init.c:105
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition: varlena.c:213
#define PG_BINARY
Definition: c.h:1044
Datum be_lo_get_fragment(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:854
#define IFS_WRLOCK
Definition: large_object.h:49
Datum be_lo_open(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:96
signed int int32
Definition: c.h:246
Datum be_lo_close(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:125
int64 inv_tell(LargeObjectDesc *obj_desc)
Definition: inv_api.c:438
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define INV_READ
Definition: libpq-fs.h:22
bool lo_compat_privileges
Definition: be-fsstubs.c:57
#define ERROR
Definition: elog.h:43
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2167
#define S_IWGRP
Definition: win32.h:449
Snapshot snapshot
Definition: large_object.h:42
#define MAXPGPATH
#define S_IRGRP
Definition: win32.h:448
static bytea * lo_get_fragment_internal(Oid loOid, int64 offset, int32 nbytes)
Definition: be-fsstubs.c:773
int inv_drop(Oid lobjId)
Definition: inv_api.c:318
static char * buf
Definition: pg_test_fsync.c:67
int lo_read(int fd, char *buf, int len)
Definition: be-fsstubs.c:155
Datum be_lo_create(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:286
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
int errcode_for_file_access(void)
Definition: elog.c:598
#define S_IROTH
Definition: win32.h:452
#define ereport(elevel, rest)
Definition: elog.h:122
Datum be_lo_truncate64(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:628
int lo_write(int fd, const char *buf, int len)
Definition: be-fsstubs.c:189
Datum be_lo_import_with_oid(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:432
#define S_IWOTH
Definition: win32.h:453
#define MaxAllocSize
Definition: memutils.h:40
int CloseTransientFile(int fd)
Definition: fd.c:2337
void AtEOSubXact_LargeObject(bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
Definition: be-fsstubs.c:683
Datum be_lo_lseek64(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:252
#define ACL_UPDATE
Definition: parsenodes.h:74
void close_lo_relation(bool isCommit)
Definition: inv_api.c:93
uintptr_t Datum
Definition: postgres.h:372
#define ACL_SELECT
Definition: parsenodes.h:73
Datum be_lo_creat(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:270
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:741
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_RETURN_VOID()
Definition: fmgr.h:309
#define PG_CATCH()
Definition: elog.h:293
Datum be_lo_unlink(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:341
#define Assert(condition)
Definition: c.h:681
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define PG_RE_THROW()
Definition: elog.h:314
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:962
#define INV_WRITE
Definition: libpq-fs.h:21
static int cookies_size
Definition: be-fsstubs.c:73
Datum be_lo_import(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:420
static char * filename
Definition: pg_dumpall.c:90
Datum be_lo_export(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:505
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition: inv_api.c:247
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define IFS_WR_PERM_OK
Definition: large_object.h:51
static MemoryContext fscxt
Definition: be-fsstubs.c:75
int i
AclResult pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode, Snapshot snapshot)
Definition: aclchk.c:4470
Definition: c.h:433
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define elog
Definition: elog.h:219
static LargeObjectDesc ** cookies
Definition: be-fsstubs.c:72
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:996
#define PG_TRY()
Definition: elog.h:284
void AtEOXact_LargeObject(bool isCommit)
Definition: be-fsstubs.c:642
Datum be_lowrite(PG_FUNCTION_ARGS)
Definition: be-fsstubs.c:399
static int newLOfd(LargeObjectDesc *lobjCookie)
Definition: be-fsstubs.c:717
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
int inv_read(LargeObjectDesc *obj_desc, char *buf, int nbytes)
Definition: inv_api.c:446
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
#define PG_END_TRY()
Definition: elog.h:300
#define read(a, b, c)
Definition: win32.h:13
int inv_write(LargeObjectDesc *obj_desc, const char *buf, int nbytes)
Definition: inv_api.c:533