LCOV - differential code coverage report
Current view: top level - src/backend/libpq - be-fsstubs.c (source / functions) Coverage Total Hit UNC UBC GNC CBC
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 86.0 % 278 239 1 38 1 238
Current Date: 2026-11-28 16:48:44 +0900 Functions: 96.6 % 29 28 1 2 26
Baseline: lcov-20261128-baseline Branches: 46.5 % 198 92 3 103 1 91
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 50.0 % 2 1 1 1
(360..) days: 86.2 % 276 238 38 238
Function coverage date bins:
(360..) days: 96.6 % 29 28 1 2 26
Branch coverage date bins:
(7,30] days: 25.0 % 4 1 3 1
(360..) days: 46.9 % 194 91 103 91

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

Generated by: LCOV version 2.0-1