PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
be-fsstubs.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "libpq/be-fsstubs.h"
#include "libpq/libpq-fs.h"
#include "miscadmin.h"
#include "storage/fd.h"
#include "storage/large_object.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
Include dependency graph for be-fsstubs.c:

Go to the source code of this file.

Macros

#define BUFSIZE   8192
 
#define CreateFSContext()
 

Functions

static int newLOfd (LargeObjectDesc *lobjCookie)
 
static void deleteLOfd (int fd)
 
static Oid lo_import_internal (text *filename, Oid lobjOid)
 
Datum be_lo_open (PG_FUNCTION_ARGS)
 
Datum be_lo_close (PG_FUNCTION_ARGS)
 
int lo_read (int fd, char *buf, int len)
 
int lo_write (int fd, const char *buf, int len)
 
Datum be_lo_lseek (PG_FUNCTION_ARGS)
 
Datum be_lo_lseek64 (PG_FUNCTION_ARGS)
 
Datum be_lo_creat (PG_FUNCTION_ARGS)
 
Datum be_lo_create (PG_FUNCTION_ARGS)
 
Datum be_lo_tell (PG_FUNCTION_ARGS)
 
Datum be_lo_tell64 (PG_FUNCTION_ARGS)
 
Datum be_lo_unlink (PG_FUNCTION_ARGS)
 
Datum be_loread (PG_FUNCTION_ARGS)
 
Datum be_lowrite (PG_FUNCTION_ARGS)
 
Datum be_lo_import (PG_FUNCTION_ARGS)
 
Datum be_lo_import_with_oid (PG_FUNCTION_ARGS)
 
Datum be_lo_export (PG_FUNCTION_ARGS)
 
static void lo_truncate_internal (int32 fd, int64 len)
 
Datum be_lo_truncate (PG_FUNCTION_ARGS)
 
Datum be_lo_truncate64 (PG_FUNCTION_ARGS)
 
void AtEOXact_LargeObject (bool isCommit)
 
void AtEOSubXact_LargeObject (bool isCommit, SubTransactionId mySubid, SubTransactionId parentSubid)
 
static bytealo_get_fragment_internal (Oid loOid, int64 offset, int32 nbytes)
 
Datum be_lo_get (PG_FUNCTION_ARGS)
 
Datum be_lo_get_fragment (PG_FUNCTION_ARGS)
 
Datum be_lo_from_bytea (PG_FUNCTION_ARGS)
 
Datum be_lo_put (PG_FUNCTION_ARGS)
 

Variables

bool lo_compat_privileges
 
static LargeObjectDesc ** cookies = NULL
 
static int cookies_size = 0
 
static MemoryContext fscxt = NULL
 

Macro Definition Documentation

#define BUFSIZE   8192

Definition at line 62 of file be-fsstubs.c.

Referenced by be_lo_export(), and lo_import_internal().

#define CreateFSContext ( )
Value:
do { \
if (fscxt == NULL) \
"Filesystem", \
} while (0)
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
MemoryContext TopMemoryContext
Definition: mcxt.c:43
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
static MemoryContext fscxt
Definition: be-fsstubs.c:75

Definition at line 77 of file be-fsstubs.c.

Referenced by be_lo_creat(), be_lo_create(), be_lo_export(), be_lo_from_bytea(), be_lo_open(), be_lo_put(), lo_get_fragment_internal(), and lo_import_internal().

Function Documentation

void AtEOSubXact_LargeObject ( bool  isCommit,
SubTransactionId  mySubid,
SubTransactionId  parentSubid 
)

Definition at line 683 of file be-fsstubs.c.

References cookies_size, deleteLOfd(), i, inv_close(), and LargeObjectDesc::subid.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

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 }
static void deleteLOfd(int fd)
Definition: be-fsstubs.c:760
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:302
SubTransactionId subid
Definition: large_object.h:43
static int cookies_size
Definition: be-fsstubs.c:73
static MemoryContext fscxt
Definition: be-fsstubs.c:75
int i
static LargeObjectDesc ** cookies
Definition: be-fsstubs.c:72
void AtEOXact_LargeObject ( bool  isCommit)

Definition at line 642 of file be-fsstubs.c.

References close_lo_relation(), cookies_size, deleteLOfd(), i, inv_close(), and MemoryContextDelete().

Referenced by AbortTransaction(), CommitTransaction(), and PrepareTransaction().

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. */
669  fscxt = NULL;
670 
671  /* Give inv_api.c a chance to clean up, too */
672  close_lo_relation(isCommit);
673 }
static void deleteLOfd(int fd)
Definition: be-fsstubs.c:760
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:302
void close_lo_relation(bool isCommit)
Definition: inv_api.c:93
static int cookies_size
Definition: be-fsstubs.c:73
static MemoryContext fscxt
Definition: be-fsstubs.c:75
int i
static LargeObjectDesc ** cookies
Definition: be-fsstubs.c:72
Datum be_lo_close ( PG_FUNCTION_ARGS  )

Definition at line 125 of file be-fsstubs.c.

References cookies_size, DEBUG4, deleteLOfd(), elog, ereport, errcode(), errmsg(), ERROR, fd(), inv_close(), PG_GETARG_INT32, and PG_RETURN_INT32.

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 }
static void deleteLOfd(int fd)
Definition: be-fsstubs.c:760
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:302
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
#define DEBUG4
Definition: elog.h:22
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:246
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static int cookies_size
Definition: be-fsstubs.c:73
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
static LargeObjectDesc ** cookies
Definition: be-fsstubs.c:72
Datum be_lo_creat ( PG_FUNCTION_ARGS  )

Definition at line 270 of file be-fsstubs.c.

References CreateFSContext, inv_create(), InvalidOid, and PG_RETURN_OID.

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 }
Oid inv_create(Oid lobjId)
Definition: inv_api.c:206
#define CreateFSContext()
Definition: be-fsstubs.c:77
unsigned int Oid
Definition: postgres_ext.h:31
#define InvalidOid
Definition: postgres_ext.h:36
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
Datum be_lo_create ( PG_FUNCTION_ARGS  )

Definition at line 286 of file be-fsstubs.c.

References CreateFSContext, inv_create(), PG_GETARG_OID, and PG_RETURN_OID.

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 }
Oid inv_create(Oid lobjId)
Definition: inv_api.c:206
#define CreateFSContext()
Definition: be-fsstubs.c:77
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
Datum be_lo_export ( PG_FUNCTION_ARGS  )

Definition at line 505 of file be-fsstubs.c.

References buf, BUFSIZE, CloseTransientFile(), CreateFSContext, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, fd(), filename, inv_close(), inv_open(), INV_READ, inv_read(), MAXPGPATH, OpenTransientFilePerm(), PG_BINARY, PG_CATCH, PG_END_TRY, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RE_THROW, PG_RETURN_INT32, PG_TRY, S_IRGRP, S_IROTH, S_IWGRP, S_IWOTH, superuser(), text_to_cstring_buffer(), and write.

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 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define BUFSIZE
Definition: be-fsstubs.c:62
#define write(a, b, c)
Definition: win32.h:14
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
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define CreateFSContext()
Definition: be-fsstubs.c:77
unsigned int Oid
Definition: postgres_ext.h:31
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
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define INV_READ
Definition: libpq-fs.h:22
#define ERROR
Definition: elog.h:43
#define S_IWGRP
Definition: win32.h:449
#define MAXPGPATH
#define S_IRGRP
Definition: win32.h:448
static char * buf
Definition: pg_test_fsync.c:67
#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
#define S_IWOTH
Definition: win32.h:453
int CloseTransientFile(int fd)
Definition: fd.c:2337
#define PG_CATCH()
Definition: elog.h:293
#define PG_RE_THROW()
Definition: elog.h:314
static char * filename
Definition: pg_dumpall.c:90
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition: inv_api.c:247
int errmsg(const char *fmt,...)
Definition: elog.c:797
static MemoryContext fscxt
Definition: be-fsstubs.c:75
Definition: c.h:433
#define PG_TRY()
Definition: elog.h:284
int inv_read(LargeObjectDesc *obj_desc, char *buf, int nbytes)
Definition: inv_api.c:446
#define PG_END_TRY()
Definition: elog.h:300
Datum be_lo_from_bytea ( PG_FUNCTION_ARGS  )

Definition at line 875 of file be-fsstubs.c.

References Assert, CreateFSContext, inv_close(), inv_create(), inv_open(), INV_WRITE, inv_write(), PG_GETARG_BYTEA_PP, PG_GETARG_OID, PG_RETURN_OID, PG_USED_FOR_ASSERTS_ONLY, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

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 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
Oid inv_create(Oid lobjId)
Definition: inv_api.c:206
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:302
#define CreateFSContext()
Definition: be-fsstubs.c:77
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define Assert(condition)
Definition: c.h:681
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define INV_WRITE
Definition: libpq-fs.h:21
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition: inv_api.c:247
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
static MemoryContext fscxt
Definition: be-fsstubs.c:75
Definition: c.h:433
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:996
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
int inv_write(LargeObjectDesc *obj_desc, const char *buf, int nbytes)
Definition: inv_api.c:533
Datum be_lo_get ( PG_FUNCTION_ARGS  )

Definition at line 840 of file be-fsstubs.c.

References lo_get_fragment_internal(), PG_GETARG_OID, and PG_RETURN_BYTEA_P.

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 }
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
unsigned int Oid
Definition: postgres_ext.h:31
static bytea * lo_get_fragment_internal(Oid loOid, int64 offset, int32 nbytes)
Definition: be-fsstubs.c:773
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
Definition: c.h:433
Datum be_lo_get_fragment ( PG_FUNCTION_ARGS  )

Definition at line 854 of file be-fsstubs.c.

References ereport, errcode(), errmsg(), ERROR, lo_get_fragment_internal(), PG_GETARG_INT32, PG_GETARG_INT64, PG_GETARG_OID, and PG_RETURN_BYTEA_P.

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:246
#define ERROR
Definition: elog.h:43
static bytea * lo_get_fragment_internal(Oid loOid, int64 offset, int32 nbytes)
Definition: be-fsstubs.c:773
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:433
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum be_lo_import ( PG_FUNCTION_ARGS  )

Definition at line 420 of file be-fsstubs.c.

References filename, InvalidOid, lo_import_internal(), PG_GETARG_TEXT_PP, and PG_RETURN_OID.

421 {
423 
425 }
static Oid lo_import_internal(text *filename, Oid lobjOid)
Definition: be-fsstubs.c:441
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define InvalidOid
Definition: postgres_ext.h:36
static char * filename
Definition: pg_dumpall.c:90
Definition: c.h:433
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
Datum be_lo_import_with_oid ( PG_FUNCTION_ARGS  )

Definition at line 432 of file be-fsstubs.c.

References filename, lo_import_internal(), PG_GETARG_OID, PG_GETARG_TEXT_PP, and PG_RETURN_OID.

433 {
435  Oid oid = PG_GETARG_OID(1);
436 
437  PG_RETURN_OID(lo_import_internal(filename, oid));
438 }
static Oid lo_import_internal(text *filename, Oid lobjOid)
Definition: be-fsstubs.c:441
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
static char * filename
Definition: pg_dumpall.c:90
Definition: c.h:433
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
Datum be_lo_lseek ( PG_FUNCTION_ARGS  )

Definition at line 227 of file be-fsstubs.c.

References cookies_size, ereport, errcode(), errmsg(), ERROR, fd(), inv_seek(), PG_GETARG_INT32, PG_RETURN_INT32, and status().

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int64 inv_seek(LargeObjectDesc *obj_desc, int64 offset, int whence)
Definition: inv_api.c:394
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:246
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static int cookies_size
Definition: be-fsstubs.c:73
int errmsg(const char *fmt,...)
Definition: elog.c:797
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
Datum be_lo_lseek64 ( PG_FUNCTION_ARGS  )

Definition at line 252 of file be-fsstubs.c.

References cookies_size, ereport, errcode(), errmsg(), ERROR, fd(), inv_seek(), PG_GETARG_INT32, PG_GETARG_INT64, PG_RETURN_INT64, and status().

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int64 inv_seek(LargeObjectDesc *obj_desc, int64 offset, int whence)
Definition: inv_api.c:394
#define PG_RETURN_INT64(x)
Definition: fmgr.h:327
int errcode(int sqlerrcode)
Definition: elog.c:575
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:246
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static int cookies_size
Definition: be-fsstubs.c:73
int errmsg(const char *fmt,...)
Definition: elog.c:797
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_GETARG_INT64(n)
Definition: fmgr.h:247
Datum be_lo_open ( PG_FUNCTION_ARGS  )

Definition at line 96 of file be-fsstubs.c.

References CreateFSContext, DEBUG4, elog, fd(), inv_open(), newLOfd(), PG_GETARG_INT32, PG_GETARG_OID, and PG_RETURN_INT32.

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
#define CreateFSContext()
Definition: be-fsstubs.c:77
unsigned int Oid
Definition: postgres_ext.h:31
#define DEBUG4
Definition: elog.h:22
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:246
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition: inv_api.c:247
static MemoryContext fscxt
Definition: be-fsstubs.c:75
#define elog
Definition: elog.h:219
static int newLOfd(LargeObjectDesc *lobjCookie)
Definition: be-fsstubs.c:717
Datum be_lo_put ( PG_FUNCTION_ARGS  )

Definition at line 897 of file be-fsstubs.c.

References ACL_UPDATE, ACLCHECK_OK, Assert, CreateFSContext, ereport, errcode(), errmsg(), ERROR, GetUserId(), LargeObjectDesc::id, inv_close(), inv_open(), inv_seek(), INV_WRITE, inv_write(), lo_compat_privileges, PG_GETARG_BYTEA_PP, PG_GETARG_INT64, PG_GETARG_OID, pg_largeobject_aclcheck_snapshot(), PG_RETURN_VOID, PG_USED_FOR_ASSERTS_ONLY, LargeObjectDesc::snapshot, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

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 }
int64 inv_seek(LargeObjectDesc *obj_desc, int64 offset, int whence)
Definition: inv_api.c:394
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
Oid GetUserId(void)
Definition: miscinit.c:284
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:302
int errcode(int sqlerrcode)
Definition: elog.c:575
#define CreateFSContext()
Definition: be-fsstubs.c:77
unsigned int Oid
Definition: postgres_ext.h:31
bool lo_compat_privileges
Definition: be-fsstubs.c:57
#define ERROR
Definition: elog.h:43
Snapshot snapshot
Definition: large_object.h:42
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define ereport(elevel, rest)
Definition: elog.h:122
#define ACL_UPDATE
Definition: parsenodes.h:74
#define PG_RETURN_VOID()
Definition: fmgr.h:309
#define Assert(condition)
Definition: c.h:681
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define INV_WRITE
Definition: libpq-fs.h:21
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition: inv_api.c:247
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
int errmsg(const char *fmt,...)
Definition: elog.c:797
static MemoryContext fscxt
Definition: be-fsstubs.c:75
AclResult pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode, Snapshot snapshot)
Definition: aclchk.c:4470
Definition: c.h:433
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:996
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
int inv_write(LargeObjectDesc *obj_desc, const char *buf, int nbytes)
Definition: inv_api.c:533
Datum be_lo_tell ( PG_FUNCTION_ARGS  )

Definition at line 302 of file be-fsstubs.c.

References cookies_size, ereport, errcode(), errmsg(), ERROR, fd(), inv_tell(), PG_GETARG_INT32, and PG_RETURN_INT32.

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:246
int64 inv_tell(LargeObjectDesc *obj_desc)
Definition: inv_api.c:438
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static int cookies_size
Definition: be-fsstubs.c:73
int errmsg(const char *fmt,...)
Definition: elog.c:797
static LargeObjectDesc ** cookies
Definition: be-fsstubs.c:72
Datum be_lo_tell64 ( PG_FUNCTION_ARGS  )

Definition at line 325 of file be-fsstubs.c.

References cookies_size, ereport, errcode(), errmsg(), ERROR, fd(), inv_tell(), PG_GETARG_INT32, and PG_RETURN_INT64.

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT64(x)
Definition: fmgr.h:327
int errcode(int sqlerrcode)
Definition: elog.c:575
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:246
int64 inv_tell(LargeObjectDesc *obj_desc)
Definition: inv_api.c:438
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
static int cookies_size
Definition: be-fsstubs.c:73
int errmsg(const char *fmt,...)
Definition: elog.c:797
static LargeObjectDesc ** cookies
Definition: be-fsstubs.c:72
Datum be_lo_truncate ( PG_FUNCTION_ARGS  )

Definition at line 618 of file be-fsstubs.c.

References fd(), lo_truncate_internal(), PG_GETARG_INT32, and PG_RETURN_INT32.

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
static void lo_truncate_internal(int32 fd, int64 len)
Definition: be-fsstubs.c:583
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:246
Datum be_lo_truncate64 ( PG_FUNCTION_ARGS  )

Definition at line 628 of file be-fsstubs.c.

References fd(), lo_truncate_internal(), PG_GETARG_INT32, PG_GETARG_INT64, and PG_RETURN_INT32.

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
static void lo_truncate_internal(int32 fd, int64 len)
Definition: be-fsstubs.c:583
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:246
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
Datum be_lo_unlink ( PG_FUNCTION_ARGS  )

Definition at line 341 of file be-fsstubs.c.

References cookies_size, deleteLOfd(), ereport, errcode(), errmsg(), ERROR, GetUserId(), i, inv_close(), inv_drop(), lo_compat_privileges, PG_GETARG_OID, pg_largeobject_ownercheck(), and PG_RETURN_INT32.

Referenced by lo_manage().

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 }
static void deleteLOfd(int fd)
Definition: be-fsstubs.c:760
bool pg_largeobject_ownercheck(Oid lobj_oid, Oid roleid)
Definition: aclchk.c:4679
Oid GetUserId(void)
Definition: miscinit.c:284
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:302
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned int Oid
Definition: postgres_ext.h:31
bool lo_compat_privileges
Definition: be-fsstubs.c:57
#define ERROR
Definition: elog.h:43
int inv_drop(Oid lobjId)
Definition: inv_api.c:318
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define ereport(elevel, rest)
Definition: elog.h:122
static int cookies_size
Definition: be-fsstubs.c:73
int errmsg(const char *fmt,...)
Definition: elog.c:797
static MemoryContext fscxt
Definition: be-fsstubs.c:75
int i
static LargeObjectDesc ** cookies
Definition: be-fsstubs.c:72
Datum be_loread ( PG_FUNCTION_ARGS  )

Definition at line 381 of file be-fsstubs.c.

References fd(), lo_read(), palloc(), PG_GETARG_INT32, PG_RETURN_BYTEA_P, SET_VARSIZE, VARDATA, and VARHDRSZ.

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:439
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:330
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:246
int lo_read(int fd, char *buf, int len)
Definition: be-fsstubs.c:155
void * palloc(Size size)
Definition: mcxt.c:848
Definition: c.h:433
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
Datum be_lowrite ( PG_FUNCTION_ARGS  )

Definition at line 399 of file be-fsstubs.c.

References fd(), lo_write(), PG_GETARG_BYTEA_PP, PG_GETARG_INT32, PG_RETURN_INT32, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

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 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
#define PG_RETURN_INT32(x)
Definition: fmgr.h:314
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:246
int lo_write(int fd, const char *buf, int len)
Definition: be-fsstubs.c:189
#define PG_GETARG_BYTEA_PP(n)
Definition: fmgr.h:272
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
Definition: c.h:433
static void deleteLOfd ( int  fd)
static

Definition at line 760 of file be-fsstubs.c.

References fd().

Referenced by AtEOSubXact_LargeObject(), AtEOXact_LargeObject(), be_lo_close(), and be_lo_unlink().

761 {
762  cookies[fd] = NULL;
763 }
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static LargeObjectDesc ** cookies
Definition: be-fsstubs.c:72
static bytea* lo_get_fragment_internal ( Oid  loOid,
int64  offset,
int32  nbytes 
)
static

Definition at line 773 of file be-fsstubs.c.

References ACL_SELECT, ACLCHECK_OK, Assert, CreateFSContext, ereport, errcode(), errmsg(), ERROR, GetUserId(), LargeObjectDesc::id, inv_close(), inv_open(), INV_READ, inv_read(), inv_seek(), lo_compat_privileges, MaxAllocSize, palloc(), pg_largeobject_aclcheck_snapshot(), PG_USED_FOR_ASSERTS_ONLY, SET_VARSIZE, LargeObjectDesc::snapshot, VARDATA, and VARHDRSZ.

Referenced by be_lo_get(), and be_lo_get_fragment().

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 }
int64 inv_seek(LargeObjectDesc *obj_desc, int64 offset, int whence)
Definition: inv_api.c:394
#define VARDATA(PTR)
Definition: postgres.h:303
Oid GetUserId(void)
Definition: miscinit.c:284
#define VARHDRSZ
Definition: c.h:439
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:302
int errcode(int sqlerrcode)
Definition: elog.c:575
#define CreateFSContext()
Definition: be-fsstubs.c:77
#define INV_READ
Definition: libpq-fs.h:22
bool lo_compat_privileges
Definition: be-fsstubs.c:57
#define ERROR
Definition: elog.h:43
Snapshot snapshot
Definition: large_object.h:42
#define ereport(elevel, rest)
Definition: elog.h:122
#define MaxAllocSize
Definition: memutils.h:40
#define ACL_SELECT
Definition: parsenodes.h:73
#define Assert(condition)
Definition: c.h:681
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition: inv_api.c:247
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
static MemoryContext fscxt
Definition: be-fsstubs.c:75
AclResult pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode, Snapshot snapshot)
Definition: aclchk.c:4470
Definition: c.h:433
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:996
int inv_read(LargeObjectDesc *obj_desc, char *buf, int nbytes)
Definition: inv_api.c:446
static Oid lo_import_internal ( text filename,
Oid  lobjOid 
)
static

Definition at line 441 of file be-fsstubs.c.

References Assert, buf, BUFSIZE, CloseTransientFile(), CreateFSContext, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, fd(), inv_close(), inv_create(), inv_open(), INV_WRITE, inv_write(), MAXPGPATH, OpenTransientFile(), PG_BINARY, PG_USED_FOR_ASSERTS_ONLY, read, superuser(), and text_to_cstring_buffer().

Referenced by be_lo_import(), and be_lo_import_with_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 }
int errhint(const char *fmt,...)
Definition: elog.c:987
#define BUFSIZE
Definition: be-fsstubs.c:62
Oid inv_create(Oid lobjId)
Definition: inv_api.c:206
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:302
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define CreateFSContext()
Definition: be-fsstubs.c:77
unsigned int Oid
Definition: postgres_ext.h:31
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
#define ERROR
Definition: elog.h:43
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2167
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:67
int errcode_for_file_access(void)
Definition: elog.c:598
#define ereport(elevel, rest)
Definition: elog.h:122
int CloseTransientFile(int fd)
Definition: fd.c:2337
#define Assert(condition)
Definition: c.h:681
#define INV_WRITE
Definition: libpq-fs.h:21
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition: inv_api.c:247
int errmsg(const char *fmt,...)
Definition: elog.c:797
static MemoryContext fscxt
Definition: be-fsstubs.c:75
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:996
#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
int lo_read ( int  fd,
char *  buf,
int  len 
)

Definition at line 155 of file be-fsstubs.c.

References ACL_SELECT, ACLCHECK_OK, cookies_size, ereport, errcode(), errmsg(), ERROR, fd(), LargeObjectDesc::flags, GetUserId(), LargeObjectDesc::id, IFS_RD_PERM_OK, inv_read(), lo_compat_privileges, pg_largeobject_aclcheck_snapshot(), LargeObjectDesc::snapshot, and status().

Referenced by be_loread(), dumpBlobs(), exportFile(), and pickout().

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 }
#define IFS_RD_PERM_OK
Definition: large_object.h:50
Oid GetUserId(void)
Definition: miscinit.c:284
int errcode(int sqlerrcode)
Definition: elog.c:575
static int fd(const char *x, int i)
Definition: preproc-init.c:105
bool lo_compat_privileges
Definition: be-fsstubs.c:57
#define ERROR
Definition: elog.h:43
Snapshot snapshot
Definition: large_object.h:42
static char * buf
Definition: pg_test_fsync.c:67
#define ereport(elevel, rest)
Definition: elog.h:122
#define ACL_SELECT
Definition: parsenodes.h:73
static int cookies_size
Definition: be-fsstubs.c:73
int errmsg(const char *fmt,...)
Definition: elog.c:797
AclResult pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode, Snapshot snapshot)
Definition: aclchk.c:4470
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
int inv_read(LargeObjectDesc *obj_desc, char *buf, int nbytes)
Definition: inv_api.c:446
static void lo_truncate_internal ( int32  fd,
int64  len 
)
static

Definition at line 583 of file be-fsstubs.c.

References ACL_UPDATE, ACLCHECK_OK, cookies_size, ereport, errcode(), errmsg(), ERROR, fd(), LargeObjectDesc::flags, GetUserId(), LargeObjectDesc::id, IFS_WR_PERM_OK, IFS_WRLOCK, inv_truncate(), lo_compat_privileges, pg_largeobject_aclcheck_snapshot(), and LargeObjectDesc::snapshot.

Referenced by be_lo_truncate(), and be_lo_truncate64().

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 }
void inv_truncate(LargeObjectDesc *obj_desc, int64 len)
Definition: inv_api.c:726
Oid GetUserId(void)
Definition: miscinit.c:284
int errcode(int sqlerrcode)
Definition: elog.c:575
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define IFS_WRLOCK
Definition: large_object.h:49
bool lo_compat_privileges
Definition: be-fsstubs.c:57
#define ERROR
Definition: elog.h:43
Snapshot snapshot
Definition: large_object.h:42
#define ereport(elevel, rest)
Definition: elog.h:122
#define ACL_UPDATE
Definition: parsenodes.h:74
static int cookies_size
Definition: be-fsstubs.c:73
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define IFS_WR_PERM_OK
Definition: large_object.h:51
AclResult pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode, Snapshot snapshot)
Definition: aclchk.c:4470
static LargeObjectDesc ** cookies
Definition: be-fsstubs.c:72
int lo_write ( int  fd,
const char *  buf,
int  len 
)

Definition at line 189 of file be-fsstubs.c.

References ACL_UPDATE, ACLCHECK_OK, cookies_size, ereport, errcode(), errmsg(), ERROR, fd(), LargeObjectDesc::flags, GetUserId(), LargeObjectDesc::id, IFS_WR_PERM_OK, IFS_WRLOCK, inv_write(), lo_compat_privileges, pg_largeobject_aclcheck_snapshot(), LargeObjectDesc::snapshot, and status().

Referenced by be_lowrite(), dump_lo_buf(), importFile(), and overwrite().

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 }
Oid GetUserId(void)
Definition: miscinit.c:284
int errcode(int sqlerrcode)
Definition: elog.c:575
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define IFS_WRLOCK
Definition: large_object.h:49
bool lo_compat_privileges
Definition: be-fsstubs.c:57
#define ERROR
Definition: elog.h:43
Snapshot snapshot
Definition: large_object.h:42
static char * buf
Definition: pg_test_fsync.c:67
#define ereport(elevel, rest)
Definition: elog.h:122
#define ACL_UPDATE
Definition: parsenodes.h:74
static int cookies_size
Definition: be-fsstubs.c:73
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define IFS_WR_PERM_OK
Definition: large_object.h:51
AclResult pg_largeobject_aclcheck_snapshot(Oid lobj_oid, Oid roleid, AclMode mode, Snapshot snapshot)
Definition: aclchk.c:4470
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
int inv_write(LargeObjectDesc *obj_desc, const char *buf, int nbytes)
Definition: inv_api.c:533
static int newLOfd ( LargeObjectDesc lobjCookie)
static

Definition at line 717 of file be-fsstubs.c.

References Assert, cookies_size, i, MemoryContextAllocZero(), MemSet, and repalloc().

Referenced by be_lo_open().

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 }
#define MemSet(start, val, len)
Definition: c.h:863
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:741
#define Assert(condition)
Definition: c.h:681
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:962
static int cookies_size
Definition: be-fsstubs.c:73
static MemoryContext fscxt
Definition: be-fsstubs.c:75
int i
static LargeObjectDesc ** cookies
Definition: be-fsstubs.c:72

Variable Documentation

LargeObjectDesc** cookies = NULL
static

Definition at line 72 of file be-fsstubs.c.

MemoryContext fscxt = NULL
static

Definition at line 75 of file be-fsstubs.c.