PostgreSQL Source Code git master
Loading...
Searching...
No Matches
be-fsstubs.c File Reference
#include "postgres.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "access/xact.h"
#include "catalog/pg_largeobject.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 "utils/snapmgr.h"
#include "varatt.h"
Include dependency graph for be-fsstubs.c:

Go to the source code of this file.

Macros

#define BUFSIZE   8192
 

Functions

static int newLOfd (void)
 
static void closeLOfd (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

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

Macro Definition Documentation

◆ BUFSIZE

#define BUFSIZE   8192

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

Function Documentation

◆ AtEOSubXact_LargeObject()

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

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

656{
657 int i;
658
659 if (fscxt == NULL) /* no LO operations in this xact */
660 return;
661
662 for (i = 0; i < cookies_size; i++)
663 {
665
666 if (lo != NULL && lo->subid == mySubid)
667 {
668 if (isCommit)
669 lo->subid = parentSubid;
670 else
671 closeLOfd(i);
672 }
673 }
674}
static MemoryContext fscxt
Definition be-fsstubs.c:75
static void closeLOfd(int fd)
Definition be-fsstubs.c:722
static LargeObjectDesc ** cookies
Definition be-fsstubs.c:71
static int cookies_size
Definition be-fsstubs.c:72
int i
Definition isn.c:77
static int fb(int x)
SubTransactionId subid

References closeLOfd(), cookies, cookies_size, fb(), fscxt, i, and LargeObjectDesc::subid.

Referenced by AbortSubTransaction(), and CommitSubTransaction().

◆ AtEOXact_LargeObject()

void AtEOXact_LargeObject ( bool  isCommit)

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

609{
610 int i;
611
613 return; /* no LO operations in this xact */
614
615 /*
616 * Close LO fds and clear cookies array so that LO fds are no longer good.
617 * The memory context and resource owner holding them are going away at
618 * the end-of-transaction anyway, but on commit, we need to close them to
619 * avoid warnings about leaked resources at commit. On abort we can skip
620 * this step.
621 */
622 if (isCommit)
623 {
624 for (i = 0; i < cookies_size; i++)
625 {
626 if (cookies[i] != NULL)
627 closeLOfd(i);
628 }
629 }
630
631 /* Needn't actually pfree since we're about to zap context */
632 cookies = NULL;
633 cookies_size = 0;
634
635 /* Release the LO memory context to prevent permanent memory leaks. */
636 if (fscxt)
638 fscxt = NULL;
639
640 /* Give inv_api.c a chance to clean up, too */
642
643 lo_cleanup_needed = false;
644}
static bool lo_cleanup_needed
Definition be-fsstubs.c:74
void close_lo_relation(bool isCommit)
Definition inv_api.c:97
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:475

References close_lo_relation(), closeLOfd(), cookies, cookies_size, fb(), fscxt, i, lo_cleanup_needed, and MemoryContextDelete().

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

◆ be_lo_close()

Datum be_lo_close ( PG_FUNCTION_ARGS  )

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

127{
129
133 errmsg("invalid large-object descriptor: %d", fd)));
134
135#ifdef FSDB
136 elog(DEBUG4, "lo_close(%d)", fd);
137#endif
138
139 closeLOfd(fd);
140
142}
int32_t int32
Definition c.h:679
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define ereport(elevel,...)
Definition elog.h:152
#define DEBUG4
Definition elog.h:28
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
static char * errmsg
static int fd(const char *x, int i)

References closeLOfd(), cookies, cookies_size, DEBUG4, elog, ereport, errcode(), errmsg, ERROR, fb(), fd(), PG_GETARG_INT32, and PG_RETURN_INT32.

◆ be_lo_creat()

Datum be_lo_creat ( PG_FUNCTION_ARGS  )

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

250{
251 Oid lobjId;
252
253 PreventCommandIfReadOnly("lo_creat()");
254
255 lo_cleanup_needed = true;
257
259}
#define PG_RETURN_OID(x)
Definition fmgr.h:361
Oid inv_create(Oid lobjId)
Definition inv_api.c:173
#define InvalidOid
unsigned int Oid
void PreventCommandIfReadOnly(const char *cmdname)
Definition utility.c:409

References fb(), inv_create(), InvalidOid, lo_cleanup_needed, PG_RETURN_OID, and PreventCommandIfReadOnly().

◆ be_lo_create()

Datum be_lo_create ( PG_FUNCTION_ARGS  )

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

263{
265
266 PreventCommandIfReadOnly("lo_create()");
267
268 lo_cleanup_needed = true;
270
272}
#define PG_GETARG_OID(n)
Definition fmgr.h:275

References fb(), inv_create(), lo_cleanup_needed, PG_GETARG_OID, PG_RETURN_OID, and PreventCommandIfReadOnly().

◆ be_lo_export()

Datum be_lo_export ( PG_FUNCTION_ARGS  )

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

487{
490 int fd;
491 int nbytes;
492 char buf[BUFSIZE];
493 char fnamebuf[MAXPGPATH];
496
497 /*
498 * open the inversion object (no need to test for failure)
499 */
500 lo_cleanup_needed = true;
502
503 /*
504 * open the file to be written to
505 *
506 * Note: we reduce backend's normal 077 umask to the slightly friendlier
507 * 022. This code used to drop it all the way to 0, but creating
508 * world-writable export files doesn't seem wise.
509 */
512 PG_TRY();
513 {
516 }
517 PG_FINALLY();
518 {
519 umask(oumask);
520 }
521 PG_END_TRY();
522 if (fd < 0)
525 errmsg("could not create server file \"%s\": %m",
526 fnamebuf)));
527
528 /*
529 * read in from the inversion file and write to the filesystem
530 */
531 while ((nbytes = inv_read(lobj, buf, BUFSIZE)) > 0)
532 {
533 ssize_t tmp;
534
535 tmp = write(fd, buf, nbytes);
536 if (tmp != nbytes)
539 errmsg("could not write server file \"%s\": %m",
540 fnamebuf)));
541 }
542
543 if (CloseTransientFile(fd) != 0)
546 errmsg("could not close file \"%s\": %m",
547 fnamebuf)));
548
550
552}
#define BUFSIZE
Definition be-fsstubs.c:61
#define PG_BINARY
Definition c.h:1431
int errcode_for_file_access(void)
Definition elog.c:898
#define PG_TRY(...)
Definition elog.h:374
#define PG_END_TRY(...)
Definition elog.h:399
#define PG_FINALLY(...)
Definition elog.h:391
int CloseTransientFile(int fd)
Definition fd.c:2855
int OpenTransientFilePerm(const char *fileName, int fileFlags, mode_t fileMode)
Definition fd.c:2687
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
#define write(a, b, c)
Definition win32.h:14
LargeObjectDesc * inv_open(Oid lobjId, int flags, MemoryContext mcxt)
Definition inv_api.c:215
int inv_read(LargeObjectDesc *obj_desc, char *buf, int nbytes)
Definition inv_api.c:450
void inv_close(LargeObjectDesc *obj_desc)
Definition inv_api.c:299
#define INV_READ
Definition libpq-fs.h:22
MemoryContext CurrentMemoryContext
Definition mcxt.c:161
#define MAXPGPATH
static char * filename
Definition pg_dumpall.c:120
static char buf[DEFAULT_XLOG_SEG_SIZE]
Definition c.h:835
void text_to_cstring_buffer(const text *src, char *dst, size_t dst_len)
Definition varlena.c:248
#define S_IROTH
Definition win32_port.h:320
#define S_IRGRP
Definition win32_port.h:308
#define S_IWOTH
Definition win32_port.h:323
#define S_IRUSR
Definition win32_port.h:296
#define S_IWUSR
Definition win32_port.h:299
#define S_IWGRP
Definition win32_port.h:311

References buf, BUFSIZE, CloseTransientFile(), CurrentMemoryContext, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), fd(), filename, inv_close(), inv_open(), inv_read(), INV_READ, lo_cleanup_needed, MAXPGPATH, OpenTransientFilePerm(), PG_BINARY, PG_END_TRY, PG_FINALLY, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_RETURN_INT32, PG_TRY, S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, text_to_cstring_buffer(), and write.

◆ be_lo_from_bytea()

Datum be_lo_from_bytea ( PG_FUNCTION_ARGS  )

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

834{
839
840 PreventCommandIfReadOnly("lo_from_bytea()");
841
842 lo_cleanup_needed = true;
848
850}
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:308
#define Assert(condition)
Definition c.h:1002
#define PG_GETARG_BYTEA_PP(n)
Definition fmgr.h:309
const char * str
int inv_write(LargeObjectDesc *obj_desc, const char *buf, int nbytes)
Definition inv_api.c:543
#define INV_WRITE
Definition libpq-fs.h:21
static Size VARSIZE_ANY_EXHDR(const void *PTR)
Definition varatt.h:472
static char * VARDATA_ANY(const void *PTR)
Definition varatt.h:486

References Assert, CurrentMemoryContext, fb(), inv_close(), inv_create(), inv_open(), inv_write(), INV_WRITE, lo_cleanup_needed, PG_GETARG_BYTEA_PP, PG_GETARG_OID, PG_RETURN_OID, PG_USED_FOR_ASSERTS_ONLY, PreventCommandIfReadOnly(), str, VARDATA_ANY(), and VARSIZE_ANY_EXHDR().

◆ be_lo_get()

Datum be_lo_get ( PG_FUNCTION_ARGS  )

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

799{
801 bytea *result;
802
804
806}
static bytea * lo_get_fragment_internal(Oid loOid, int64 offset, int32 nbytes)
Definition be-fsstubs.c:747
uint32 result
#define PG_RETURN_BYTEA_P(x)
Definition fmgr.h:373

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

◆ be_lo_get_fragment()

Datum be_lo_get_fragment ( PG_FUNCTION_ARGS  )

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

813{
815 int64 offset = PG_GETARG_INT64(1);
816 int32 nbytes = PG_GETARG_INT32(2);
817 bytea *result;
818
819 if (nbytes < 0)
822 errmsg("requested length cannot be negative")));
823
824 result = lo_get_fragment_internal(loOid, offset, nbytes);
825
827}
int64_t int64
Definition c.h:680
#define PG_GETARG_INT64(n)
Definition fmgr.h:284

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

◆ be_lo_import()

Datum be_lo_import ( PG_FUNCTION_ARGS  )

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

404{
406
408}
static Oid lo_import_internal(text *filename, Oid lobjOid)
Definition be-fsstubs.c:424

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

◆ be_lo_import_with_oid()

Datum be_lo_import_with_oid ( PG_FUNCTION_ARGS  )

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

416{
418 Oid oid = PG_GETARG_OID(1);
419
421}

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

◆ be_lo_lseek()

Datum be_lo_lseek ( PG_FUNCTION_ARGS  )

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

207{
209 int32 offset = PG_GETARG_INT32(1);
211 int64 status;
212
216 errmsg("invalid large-object descriptor: %d", fd)));
217
218 status = inv_seek(cookies[fd], offset, whence);
219
220 /* guard against result overflow */
221 if (status != (int32) status)
224 errmsg("lo_lseek result out of range for large-object descriptor %d",
225 fd)));
226
227 PG_RETURN_INT32((int32) status);
228}
int64 inv_seek(LargeObjectDesc *obj_desc, int64 offset, int whence)
Definition inv_api.c:388

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

◆ be_lo_lseek64()

Datum be_lo_lseek64 ( PG_FUNCTION_ARGS  )

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

232{
234 int64 offset = PG_GETARG_INT64(1);
236 int64 status;
237
241 errmsg("invalid large-object descriptor: %d", fd)));
242
243 status = inv_seek(cookies[fd], offset, whence);
244
245 PG_RETURN_INT64(status);
246}
#define PG_RETURN_INT64(x)
Definition fmgr.h:370

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

◆ be_lo_open()

Datum be_lo_open ( PG_FUNCTION_ARGS  )

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

88{
92 int fd;
93
94#ifdef FSDB
95 elog(DEBUG4, "lo_open(%u,%d)", lobjId, mode);
96#endif
97
98 if (mode & INV_WRITE)
99 PreventCommandIfReadOnly("lo_open(INV_WRITE)");
100
101 /*
102 * Allocate a large object descriptor first. This will also create
103 * 'fscxt' if this is the first LO opened in this transaction.
104 */
105 fd = newLOfd();
106
109
110 /*
111 * We must register the snapshot in TopTransaction's resowner so that it
112 * stays alive until the LO is closed rather than until the current portal
113 * shuts down.
114 */
115 if (lobjDesc->snapshot)
116 lobjDesc->snapshot = RegisterSnapshotOnOwner(lobjDesc->snapshot,
118
119 Assert(cookies[fd] == NULL);
121
123}
static int newLOfd(void)
Definition be-fsstubs.c:681
static PgChecksumMode mode
ResourceOwner TopTransactionResourceOwner
Definition resowner.c:175
Snapshot RegisterSnapshotOnOwner(Snapshot snapshot, ResourceOwner owner)
Definition snapmgr.c:837
SubTransactionId GetCurrentSubTransactionId(void)
Definition xact.c:793

References Assert, cookies, DEBUG4, elog, fb(), fd(), fscxt, GetCurrentSubTransactionId(), inv_open(), INV_WRITE, mode, newLOfd(), PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_INT32, PreventCommandIfReadOnly(), RegisterSnapshotOnOwner(), and TopTransactionResourceOwner.

◆ be_lo_put()

◆ be_lo_tell()

Datum be_lo_tell ( PG_FUNCTION_ARGS  )

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

276{
278 int64 offset;
279
283 errmsg("invalid large-object descriptor: %d", fd)));
284
285 offset = inv_tell(cookies[fd]);
286
287 /* guard against result overflow */
288 if (offset != (int32) offset)
291 errmsg("lo_tell result out of range for large-object descriptor %d",
292 fd)));
293
294 PG_RETURN_INT32((int32) offset);
295}
int64 inv_tell(LargeObjectDesc *obj_desc)
Definition inv_api.c:437

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

◆ be_lo_tell64()

Datum be_lo_tell64 ( PG_FUNCTION_ARGS  )

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

299{
301 int64 offset;
302
306 errmsg("invalid large-object descriptor: %d", fd)));
307
308 offset = inv_tell(cookies[fd]);
309
310 PG_RETURN_INT64(offset);
311}

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

◆ be_lo_truncate()

Datum be_lo_truncate ( PG_FUNCTION_ARGS  )

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

581{
584
585 PreventCommandIfReadOnly("lo_truncate()");
586
589}
static void lo_truncate_internal(int32 fd, int64 len)
Definition be-fsstubs.c:559
const void size_t len

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

◆ be_lo_truncate64()

Datum be_lo_truncate64 ( PG_FUNCTION_ARGS  )

◆ be_lo_unlink()

Datum be_lo_unlink ( PG_FUNCTION_ARGS  )

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

315{
317
318 PreventCommandIfReadOnly("lo_unlink()");
319
323 errmsg("large object %u does not exist", lobjId)));
324
325 /*
326 * Must be owner of the large object. It would be cleaner to check this
327 * in inv_drop(), but we want to throw the error before not after closing
328 * relevant FDs.
329 */
334 errmsg("must be owner of large object %u", lobjId)));
335
336 /*
337 * If there are any open LO FDs referencing that ID, close 'em.
338 */
339 if (fscxt != NULL)
340 {
341 int i;
342
343 for (i = 0; i < cookies_size; i++)
344 {
345 if (cookies[i] != NULL && cookies[i]->id == lobjId)
346 closeLOfd(i);
347 }
348 }
349
350 /*
351 * inv_drop does not create a need for end-of-transaction cleanup and
352 * hence we don't need to set lo_cleanup_needed.
353 */
355}
bool object_ownercheck(Oid classid, Oid objectid, Oid roleid)
Definition aclchk.c:4156
bool lo_compat_privileges
Definition inv_api.c:56
int inv_drop(Oid lobjId)
Definition inv_api.c:311
Oid GetUserId(void)
Definition miscinit.c:470
bool LargeObjectExists(Oid loid)

References closeLOfd(), cookies, cookies_size, ereport, errcode(), errmsg, ERROR, fb(), fscxt, GetUserId(), i, inv_drop(), LargeObjectExists(), lo_compat_privileges, object_ownercheck(), PG_GETARG_OID, PG_RETURN_INT32, and PreventCommandIfReadOnly().

Referenced by lo_manage().

◆ be_loread()

Datum be_loread ( PG_FUNCTION_ARGS  )

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

363{
366 bytea *retval;
367 int totalread;
368
369 if (len < 0)
370 len = 0;
371
372 retval = (bytea *) palloc(VARHDRSZ + len);
373 totalread = lo_read(fd, VARDATA(retval), len);
374 SET_VARSIZE(retval, totalread + VARHDRSZ);
375
376 PG_RETURN_BYTEA_P(retval);
377}
int lo_read(int fd, char *buf, int len)
Definition be-fsstubs.c:154
#define VARHDRSZ
Definition c.h:840
void * palloc(Size size)
Definition mcxt.c:1390
static char * VARDATA(const void *PTR)
Definition varatt.h:305
static void SET_VARSIZE(void *PTR, Size len)
Definition varatt.h:432

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

◆ be_lowrite()

Datum be_lowrite ( PG_FUNCTION_ARGS  )

◆ closeLOfd()

static void closeLOfd ( int  fd)
static

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

723{
725
726 /*
727 * Make sure we do not try to free twice if this errors out for some
728 * reason. Better a leak than a crash.
729 */
730 lobj = cookies[fd];
731 cookies[fd] = NULL;
732
733 if (lobj->snapshot)
737}
void UnregisterSnapshotFromOwner(Snapshot snapshot, ResourceOwner owner)
Definition snapmgr.c:879

References cookies, fb(), fd(), inv_close(), TopTransactionResourceOwner, and UnregisterSnapshotFromOwner().

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

◆ lo_get_fragment_internal()

static bytea * lo_get_fragment_internal ( Oid  loOid,
int64  offset,
int32  nbytes 
)
static

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

748{
753 bytea *result = NULL;
754
755 lo_cleanup_needed = true;
757
758 /*
759 * Compute number of bytes we'll actually read, accommodating nbytes == -1
760 * and reads beyond the end of the LO.
761 */
763 if (loSize > offset)
764 {
765 if (nbytes >= 0 && nbytes <= loSize - offset)
766 result_length = nbytes; /* request is wholly inside LO */
767 else
768 result_length = loSize - offset; /* adjust to end of LO */
769 }
770 else
771 result_length = 0; /* request is wholly outside LO */
772
773 /*
774 * A result_length calculated from loSize may not fit in a size_t. Check
775 * that the size will satisfy this and subsequently-enforced size limits.
776 */
780 errmsg("large object read request is too large")));
781
783
784 inv_seek(loDesc, offset, SEEK_SET);
788
790
791 return result;
792}
#define MaxAllocSize
Definition fe_memutils.h:22

References Assert, CurrentMemoryContext, ereport, errcode(), errmsg, ERROR, fb(), inv_close(), inv_open(), inv_read(), INV_READ, inv_seek(), lo_cleanup_needed, MaxAllocSize, palloc(), PG_USED_FOR_ASSERTS_ONLY, result, SET_VARSIZE(), VARDATA(), and VARHDRSZ.

Referenced by be_lo_get(), and be_lo_get_fragment().

◆ lo_import_internal()

static Oid lo_import_internal ( text filename,
Oid  lobjOid 
)
static

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

425{
426 int fd;
427 ssize_t nbytes;
429 char buf[BUFSIZE];
430 char fnamebuf[MAXPGPATH];
432 Oid oid;
433
434 PreventCommandIfReadOnly("lo_import()");
435
436 /*
437 * open the file to be read in
438 */
441 if (fd < 0)
444 errmsg("could not open server file \"%s\": %m",
445 fnamebuf)));
446
447 /*
448 * create an inversion object
449 */
450 lo_cleanup_needed = true;
451 oid = inv_create(lobjOid);
452
453 /*
454 * read in from the filesystem and write to the inversion object
455 */
457
458 while ((nbytes = read(fd, buf, BUFSIZE)) > 0)
459 {
460 tmp = inv_write(lobj, buf, nbytes);
461 Assert(tmp == nbytes);
462 }
463
464 if (nbytes < 0)
467 errmsg("could not read server file \"%s\": %m",
468 fnamebuf)));
469
471
472 if (CloseTransientFile(fd) != 0)
475 errmsg("could not close file \"%s\": %m",
476 fnamebuf)));
477
478 return oid;
479}
int OpenTransientFile(const char *fileName, int fileFlags)
Definition fd.c:2678
#define read(a, b, c)
Definition win32.h:13

References Assert, buf, BUFSIZE, CloseTransientFile(), CurrentMemoryContext, ereport, errcode_for_file_access(), errmsg, ERROR, fb(), fd(), filename, inv_close(), inv_create(), inv_open(), inv_write(), INV_WRITE, lo_cleanup_needed, MAXPGPATH, OpenTransientFile(), PG_BINARY, PG_USED_FOR_ASSERTS_ONLY, PreventCommandIfReadOnly(), read, and text_to_cstring_buffer().

Referenced by be_lo_import(), and be_lo_import_with_oid().

◆ lo_read()

int lo_read ( int  fd,
char buf,
int  len 
)

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

155{
156 int status;
158
162 errmsg("invalid large-object descriptor: %d", fd)));
163 lobj = cookies[fd];
164
165 /*
166 * Check state. inv_read() would throw an error anyway, but we want the
167 * error to be about the FD's state not the underlying privilege; it might
168 * be that the privilege exists but user forgot to ask for read mode.
169 */
170 if ((lobj->flags & IFS_RDLOCK) == 0)
173 errmsg("large object descriptor %d was not opened for reading",
174 fd)));
175
176 status = inv_read(lobj, buf, len);
177
178 return status;
179}
#define IFS_RDLOCK

References buf, cookies, cookies_size, ereport, errcode(), errmsg, ERROR, fb(), fd(), IFS_RDLOCK, inv_read(), and len.

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

◆ lo_truncate_internal()

static void lo_truncate_internal ( int32  fd,
int64  len 
)
static

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

560{
562
566 errmsg("invalid large-object descriptor: %d", fd)));
567 lobj = cookies[fd];
568
569 /* see comment in lo_read() */
570 if ((lobj->flags & IFS_WRLOCK) == 0)
573 errmsg("large object descriptor %d was not opened for writing",
574 fd)));
575
577}
void inv_truncate(LargeObjectDesc *obj_desc, int64 len)
Definition inv_api.c:738
#define IFS_WRLOCK

References cookies, cookies_size, ereport, errcode(), errmsg, ERROR, fb(), fd(), IFS_WRLOCK, inv_truncate(), and len.

Referenced by be_lo_truncate(), and be_lo_truncate64().

◆ lo_write()

int lo_write ( int  fd,
const char buf,
int  len 
)

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

183{
184 int status;
186
190 errmsg("invalid large-object descriptor: %d", fd)));
191 lobj = cookies[fd];
192
193 /* see comment in lo_read() */
194 if ((lobj->flags & IFS_WRLOCK) == 0)
197 errmsg("large object descriptor %d was not opened for writing",
198 fd)));
199
200 status = inv_write(lobj, buf, len);
201
202 return status;
203}

References buf, cookies, cookies_size, ereport, errcode(), errmsg, ERROR, fb(), fd(), IFS_WRLOCK, inv_write(), and len.

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

◆ newLOfd()

static int newLOfd ( void  )
static

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

682{
683 int i,
684 newsize;
685
686 lo_cleanup_needed = true;
687 if (fscxt == NULL)
689 "Filesystem",
691
692 /* Try to find a free slot */
693 for (i = 0; i < cookies_size; i++)
694 {
695 if (cookies[i] == NULL)
696 return i;
697 }
698
699 /* No free slot, so make the array bigger */
700 if (cookies_size <= 0)
701 {
702 /* First time through, arbitrarily make 64-element array */
703 i = 0;
704 newsize = 64;
707 }
708 else
709 {
710 /* Double size of array */
711 i = cookies_size;
712 newsize = cookies_size * 2;
713 cookies =
715 }
717
718 return i;
719}
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1269
MemoryContext TopMemoryContext
Definition mcxt.c:167
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define repalloc0_array(pointer, type, oldcount, count)
Definition palloc.h:122

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, cookies, cookies_size, fb(), fscxt, i, lo_cleanup_needed, MemoryContextAllocZero(), repalloc0_array, and TopMemoryContext.

Referenced by be_lo_open().

Variable Documentation

◆ cookies

◆ cookies_size

◆ fscxt

MemoryContext fscxt = NULL
static

◆ lo_cleanup_needed