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
#define NULL
Definition: c.h:229
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 674 of file be-fsstubs.c.

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

Referenced by AbortSubTransaction(), and CommitSubTransaction().

676 {
677  int i;
678 
679  if (fscxt == NULL) /* no LO operations in this xact */
680  return;
681 
682  for (i = 0; i < cookies_size; i++)
683  {
684  LargeObjectDesc *lo = cookies[i];
685 
686  if (lo != NULL && lo->subid == mySubid)
687  {
688  if (isCommit)
689  lo->subid = parentSubid;
690  else
691  {
692  /*
693  * Make sure we do not call inv_close twice if it errors out
694  * for some reason. Better a leak than a crash.
695  */
696  deleteLOfd(i);
697  inv_close(lo);
698  }
699  }
700  }
701 }
static void deleteLOfd(int fd)
Definition: be-fsstubs.c:751
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:320
SubTransactionId subid
Definition: large_object.h:43
#define NULL
Definition: c.h:229
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 633 of file be-fsstubs.c.

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

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

634 {
635  int i;
636 
637  if (fscxt == NULL)
638  return; /* no LO operations in this xact */
639 
640  /*
641  * Close LO fds and clear cookies array so that LO fds are no longer good.
642  * On abort we skip the close step.
643  */
644  for (i = 0; i < cookies_size; i++)
645  {
646  if (cookies[i] != NULL)
647  {
648  if (isCommit)
649  inv_close(cookies[i]);
650  deleteLOfd(i);
651  }
652  }
653 
654  /* Needn't actually pfree since we're about to zap context */
655  cookies = NULL;
656  cookies_size = 0;
657 
658  /* Release the LO memory context to prevent permanent memory leaks. */
660  fscxt = NULL;
661 
662  /* Give inv_api.c a chance to clean up, too */
663  close_lo_relation(isCommit);
664 }
static void deleteLOfd(int fd)
Definition: be-fsstubs.c:751
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:320
void close_lo_relation(bool isCommit)
Definition: inv_api.c:102
#define NULL
Definition: c.h:229
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(), NULL, 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:751
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:320
#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:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
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:224
#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:224
#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, OpenTransientFile(), PG_BINARY, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_INT32, 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  fd = OpenTransientFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY,
542  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
543  umask(oumask);
544  if (fd < 0)
545  ereport(ERROR,
547  errmsg("could not create server file \"%s\": %m",
548  fnamebuf)));
549 
550  /*
551  * read in from the inversion file and write to the filesystem
552  */
553  while ((nbytes = inv_read(lobj, buf, BUFSIZE)) > 0)
554  {
555  tmp = write(fd, buf, nbytes);
556  if (tmp != nbytes)
557  ereport(ERROR,
559  errmsg("could not write server file \"%s\": %m",
560  fnamebuf)));
561  }
562 
563  CloseTransientFile(fd);
564  inv_close(lobj);
565 
566  PG_RETURN_INT32(1);
567 }
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
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:320
#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:1039
#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:66
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
int OpenTransientFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:2144
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:2305
static char * filename
Definition: pg_dumpall.c:90
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition: inv_api.c:265
int errmsg(const char *fmt,...)
Definition: elog.c:797
static MemoryContext fscxt
Definition: be-fsstubs.c:75
Definition: c.h:439
int inv_read(LargeObjectDesc *obj_desc, char *buf, int nbytes)
Definition: inv_api.c:464
Datum be_lo_from_bytea ( PG_FUNCTION_ARGS  )

Definition at line 866 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.

867 {
868  Oid loOid = PG_GETARG_OID(0);
869  bytea *str = PG_GETARG_BYTEA_PP(1);
870  LargeObjectDesc *loDesc;
871  int written PG_USED_FOR_ASSERTS_ONLY;
872 
873  CreateFSContext();
874 
875  loOid = inv_create(loOid);
876  loDesc = inv_open(loOid, INV_WRITE, fscxt);
877  written = inv_write(loDesc, VARDATA_ANY(str), VARSIZE_ANY_EXHDR(str));
878  Assert(written == VARSIZE_ANY_EXHDR(str));
879  inv_close(loDesc);
880 
881  PG_RETURN_OID(loOid);
882 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
Oid inv_create(Oid lobjId)
Definition: inv_api.c:224
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:320
#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:676
#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:265
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
static MemoryContext fscxt
Definition: be-fsstubs.c:75
Definition: c.h:439
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:991
#define PG_RETURN_OID(x)
Definition: fmgr.h:320
int inv_write(LargeObjectDesc *obj_desc, const char *buf, int nbytes)
Definition: inv_api.c:551
Datum be_lo_get ( PG_FUNCTION_ARGS  )

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

References lo_get_fragment_internal(), PG_GETARG_OID, PG_RETURN_BYTEA_P, and result.

832 {
833  Oid loOid = PG_GETARG_OID(0);
834  bytea *result;
835 
836  result = lo_get_fragment_internal(loOid, 0, -1);
837 
838  PG_RETURN_BYTEA_P(result);
839 }
return result
Definition: formatting.c:1633
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:329
unsigned int Oid
Definition: postgres_ext.h:31
static bytea * lo_get_fragment_internal(Oid loOid, int64 offset, int32 nbytes)
Definition: be-fsstubs.c:764
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
Definition: c.h:439
Datum be_lo_get_fragment ( PG_FUNCTION_ARGS  )

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

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

846 {
847  Oid loOid = PG_GETARG_OID(0);
848  int64 offset = PG_GETARG_INT64(1);
849  int32 nbytes = PG_GETARG_INT32(2);
850  bytea *result;
851 
852  if (nbytes < 0)
853  ereport(ERROR,
854  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
855  errmsg("requested length cannot be negative")));
856 
857  result = lo_get_fragment_internal(loOid, offset, nbytes);
858 
859  PG_RETURN_BYTEA_P(result);
860 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
int errcode(int sqlerrcode)
Definition: elog.c:575
return result
Definition: formatting.c:1633
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:329
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
static bytea * lo_get_fragment_internal(Oid loOid, int64 offset, int32 nbytes)
Definition: be-fsstubs.c:764
#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:439
#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:439
#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:439
#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(), NULL, 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:412
#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:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
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:224
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(), NULL, 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:412
#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:256
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
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:224
#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(), NULL, 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:256
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define NULL
Definition: c.h:229
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition: inv_api.c:265
static MemoryContext fscxt
Definition: be-fsstubs.c:75
#define elog
Definition: elog.h:219
static int newLOfd(LargeObjectDesc *lobjCookie)
Definition: be-fsstubs.c:708
Datum be_lo_put ( PG_FUNCTION_ARGS  )

Definition at line 888 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.

889 {
890  Oid loOid = PG_GETARG_OID(0);
891  int64 offset = PG_GETARG_INT64(1);
892  bytea *str = PG_GETARG_BYTEA_PP(2);
893  LargeObjectDesc *loDesc;
894  int written PG_USED_FOR_ASSERTS_ONLY;
895 
896  CreateFSContext();
897 
898  loDesc = inv_open(loOid, INV_WRITE, fscxt);
899 
900  /* Permission check */
901  if (!lo_compat_privileges &&
903  GetUserId(),
904  ACL_UPDATE,
905  loDesc->snapshot) != ACLCHECK_OK)
906  ereport(ERROR,
907  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
908  errmsg("permission denied for large object %u",
909  loDesc->id)));
910 
911  inv_seek(loDesc, offset, SEEK_SET);
912  written = inv_write(loDesc, VARDATA_ANY(str), VARSIZE_ANY_EXHDR(str));
913  Assert(written == VARSIZE_ANY_EXHDR(str));
914  inv_close(loDesc);
915 
916  PG_RETURN_VOID();
917 }
int64 inv_seek(LargeObjectDesc *obj_desc, int64 offset, int whence)
Definition: inv_api.c:412
#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:320
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:676
#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:265
#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:439
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:991
#define PG_GETARG_INT64(n)
Definition: fmgr.h:247
int inv_write(LargeObjectDesc *obj_desc, const char *buf, int nbytes)
Definition: inv_api.c:551
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(), NULL, 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:256
int64 inv_tell(LargeObjectDesc *obj_desc)
Definition: inv_api.c:456
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
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(), NULL, 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:256
int64 inv_tell(LargeObjectDesc *obj_desc)
Definition: inv_api.c:456
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
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 609 of file be-fsstubs.c.

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

610 {
611  int32 fd = PG_GETARG_INT32(0);
612  int32 len = PG_GETARG_INT32(1);
613 
614  lo_truncate_internal(fd, len);
615  PG_RETURN_INT32(0);
616 }
#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:574
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:256
Datum be_lo_truncate64 ( PG_FUNCTION_ARGS  )

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

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

620 {
621  int32 fd = PG_GETARG_INT32(0);
622  int64 len = PG_GETARG_INT64(1);
623 
624  lo_truncate_internal(fd, len);
625  PG_RETURN_INT32(0);
626 }
#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:574
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:256
#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, NULL, 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:751
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:320
#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:336
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define ereport(elevel, rest)
Definition: elog.h:122
#define NULL
Definition: c.h:229
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:445
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:329
static int fd(const char *x, int i)
Definition: preproc-init.c:105
signed int int32
Definition: c.h:256
int lo_read(int fd, char *buf, int len)
Definition: be-fsstubs.c:155
void * palloc(Size size)
Definition: mcxt.c:849
Definition: c.h:439
#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:256
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:439
static void deleteLOfd ( int  fd)
static

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

References fd(), and NULL.

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

752 {
753  cookies[fd] = NULL;
754 }
static int fd(const char *x, int i)
Definition: preproc-init.c:105
#define NULL
Definition: c.h:229
static LargeObjectDesc ** cookies
Definition: be-fsstubs.c:72
static bytea* lo_get_fragment_internal ( Oid  loOid,
int64  offset,
int32  nbytes 
)
static

Definition at line 764 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, NULL, palloc(), pg_largeobject_aclcheck_snapshot(), PG_USED_FOR_ASSERTS_ONLY, result, SET_VARSIZE, LargeObjectDesc::snapshot, VARDATA, and VARHDRSZ.

Referenced by be_lo_get(), and be_lo_get_fragment().

765 {
766  LargeObjectDesc *loDesc;
767  int64 loSize;
768  int64 result_length;
769  int total_read PG_USED_FOR_ASSERTS_ONLY;
770  bytea *result = NULL;
771 
772  /*
773  * We don't actually need to store into fscxt, but create it anyway to
774  * ensure that AtEOXact_LargeObject knows there is state to clean up
775  */
776  CreateFSContext();
777 
778  loDesc = inv_open(loOid, INV_READ, fscxt);
779 
780  /* Permission check */
781  if (!lo_compat_privileges &&
783  GetUserId(),
784  ACL_SELECT,
785  loDesc->snapshot) != ACLCHECK_OK)
786  ereport(ERROR,
787  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
788  errmsg("permission denied for large object %u",
789  loDesc->id)));
790 
791  /*
792  * Compute number of bytes we'll actually read, accommodating nbytes == -1
793  * and reads beyond the end of the LO.
794  */
795  loSize = inv_seek(loDesc, 0, SEEK_END);
796  if (loSize > offset)
797  {
798  if (nbytes >= 0 && nbytes <= loSize - offset)
799  result_length = nbytes; /* request is wholly inside LO */
800  else
801  result_length = loSize - offset; /* adjust to end of LO */
802  }
803  else
804  result_length = 0; /* request is wholly outside LO */
805 
806  /*
807  * A result_length calculated from loSize may not fit in a size_t. Check
808  * that the size will satisfy this and subsequently-enforced size limits.
809  */
810  if (result_length > MaxAllocSize - VARHDRSZ)
811  ereport(ERROR,
812  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
813  errmsg("large object read request is too large")));
814 
815  result = (bytea *) palloc(VARHDRSZ + result_length);
816 
817  inv_seek(loDesc, offset, SEEK_SET);
818  total_read = inv_read(loDesc, VARDATA(result), result_length);
819  Assert(total_read == result_length);
820  SET_VARSIZE(result, result_length + VARHDRSZ);
821 
822  inv_close(loDesc);
823 
824  return result;
825 }
int64 inv_seek(LargeObjectDesc *obj_desc, int64 offset, int whence)
Definition: inv_api.c:412
#define VARDATA(PTR)
Definition: postgres.h:303
Oid GetUserId(void)
Definition: miscinit.c:284
#define VARHDRSZ
Definition: c.h:445
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:320
int errcode(int sqlerrcode)
Definition: elog.c:575
#define CreateFSContext()
Definition: be-fsstubs.c:77
return result
Definition: formatting.c:1633
#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 NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition: inv_api.c:265
void * palloc(Size size)
Definition: mcxt.c:849
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:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:991
int inv_read(LargeObjectDesc *obj_desc, char *buf, int nbytes)
Definition: inv_api.c:464
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, S_IRWXU);
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:224
void inv_close(LargeObjectDesc *obj_desc)
Definition: inv_api.c:320
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:1039
#define ERROR
Definition: elog.h:43
#define MAXPGPATH
static char * buf
Definition: pg_test_fsync.c:66
int OpenTransientFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:2144
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:2305
#define Assert(condition)
Definition: c.h:676
#define INV_WRITE
Definition: libpq-fs.h:21
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition: inv_api.c:265
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:991
#define read(a, b, c)
Definition: win32.h:13
int inv_write(LargeObjectDesc *obj_desc, const char *buf, int nbytes)
Definition: inv_api.c:551
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, NULL, 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:66
#define ereport(elevel, rest)
Definition: elog.h:122
#define ACL_SELECT
Definition: parsenodes.h:73
#define NULL
Definition: c.h:229
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:224
int inv_read(LargeObjectDesc *obj_desc, char *buf, int nbytes)
Definition: inv_api.c:464
static void lo_truncate_internal ( int32  fd,
int64  len 
)
static

Definition at line 574 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, NULL, pg_largeobject_aclcheck_snapshot(), and LargeObjectDesc::snapshot.

Referenced by be_lo_truncate(), and be_lo_truncate64().

575 {
576  LargeObjectDesc *lobj;
577 
578  if (fd < 0 || fd >= cookies_size || cookies[fd] == NULL)
579  ereport(ERROR,
580  (errcode(ERRCODE_UNDEFINED_OBJECT),
581  errmsg("invalid large-object descriptor: %d", fd)));
582  lobj = cookies[fd];
583 
584  if ((lobj->flags & IFS_WRLOCK) == 0)
585  ereport(ERROR,
586  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
587  errmsg("large object descriptor %d was not opened for writing",
588  fd)));
589 
590  /* Permission checks --- first time through only */
591  if ((lobj->flags & IFS_WR_PERM_OK) == 0)
592  {
593  if (!lo_compat_privileges &&
595  GetUserId(),
596  ACL_UPDATE,
597  lobj->snapshot) != ACLCHECK_OK)
598  ereport(ERROR,
599  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
600  errmsg("permission denied for large object %u",
601  lobj->id)));
602  lobj->flags |= IFS_WR_PERM_OK;
603  }
604 
605  inv_truncate(lobj, len);
606 }
void inv_truncate(LargeObjectDesc *obj_desc, int64 len)
Definition: inv_api.c:744
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
#define NULL
Definition: c.h:229
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, NULL, 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:66
#define ereport(elevel, rest)
Definition: elog.h:122
#define ACL_UPDATE
Definition: parsenodes.h:74
#define NULL
Definition: c.h:229
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:224
int inv_write(LargeObjectDesc *obj_desc, const char *buf, int nbytes)
Definition: inv_api.c:551
static int newLOfd ( LargeObjectDesc lobjCookie)
static

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

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

Referenced by be_lo_open().

709 {
710  int i,
711  newsize;
712 
713  /* Try to find a free slot */
714  for (i = 0; i < cookies_size; i++)
715  {
716  if (cookies[i] == NULL)
717  {
718  cookies[i] = lobjCookie;
719  return i;
720  }
721  }
722 
723  /* No free slot, so make the array bigger */
724  if (cookies_size <= 0)
725  {
726  /* First time through, arbitrarily make 64-element array */
727  i = 0;
728  newsize = 64;
729  cookies = (LargeObjectDesc **)
730  MemoryContextAllocZero(fscxt, newsize * sizeof(LargeObjectDesc *));
731  cookies_size = newsize;
732  }
733  else
734  {
735  /* Double size of array */
736  i = cookies_size;
737  newsize = cookies_size * 2;
738  cookies = (LargeObjectDesc **)
739  repalloc(cookies, newsize * sizeof(LargeObjectDesc *));
740  MemSet(cookies + cookies_size, 0,
741  (newsize - cookies_size) * sizeof(LargeObjectDesc *));
742  cookies_size = newsize;
743  }
744 
745  Assert(cookies[i] == NULL);
746  cookies[i] = lobjCookie;
747  return i;
748 }
#define MemSet(start, val, len)
Definition: c.h:858
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:742
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:963
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.