LCOV - differential code coverage report
Current view: top level - src/interfaces/libpq - fe-lobj.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: ba12a202ce1b5581dc0ed149cf3f637d7897ad5d vs 2866d8c7dbfc9d882a7d80fef93fbbe763709932 Lines: 41.2 % 362 149 9 204 5 144 21 5
Current Date: 2026-08-27 14:31:44 +0300 Functions: 55.6 % 18 10 7 1 6 4 2
Baseline: lcov-20260827-baseline Branches: 44.1 % 170 75 95 75
Baseline Date: 2026-08-27 14:31:58 +0300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 35.7 % 14 5 9 5
(30,360] days: 33.3 % 3 1 2 1
(360..) days: 41.4 % 345 143 202 143
Function coverage date bins:
(360..) days: 55.6 % 18 10 7 1 6 4
Branch coverage date bins:
(360..) days: 44.1 % 170 75 95 75

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * fe-lobj.c
                                  4                 :                :  *    Front-end large object interface
                                  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/interfaces/libpq/fe-lobj.c
                                 12                 :                :  *
                                 13                 :                :  *-------------------------------------------------------------------------
                                 14                 :                :  */
                                 15                 :                : 
                                 16                 :                : #ifdef WIN32
                                 17                 :                : /*
                                 18                 :                :  *  As unlink/rename are #define'd in port.h (via postgres_fe.h), io.h
                                 19                 :                :  *  must be included first on MS C.  Might as well do it for all WIN32's
                                 20                 :                :  *  here.
                                 21                 :                :  */
                                 22                 :                : #include <io.h>
                                 23                 :                : #endif
                                 24                 :                : 
                                 25                 :                : #include "postgres_fe.h"
                                 26                 :                : 
                                 27                 :                : #ifdef WIN32
                                 28                 :                : #include "win32.h"
                                 29                 :                : #else
                                 30                 :                : #include <unistd.h>
                                 31                 :                : #endif
                                 32                 :                : 
                                 33                 :                : #include <fcntl.h>
                                 34                 :                : #include <limits.h>
                                 35                 :                : #include <sys/stat.h>
                                 36                 :                : 
                                 37                 :                : #include "libpq-fe.h"
                                 38                 :                : #include "libpq-int.h"
                                 39                 :                : #include "libpq/libpq-fs.h"       /* must come after sys/stat.h */
                                 40                 :                : #include "port/pg_bswap.h"
                                 41                 :                : 
                                 42                 :                : #define LO_BUFSIZE        8192
                                 43                 :                : 
                                 44                 :                : static int  lo_initialize(PGconn *conn);
                                 45                 :                : static Oid  lo_import_internal(PGconn *conn, const char *filename, Oid oid);
                                 46                 :                : 
                                 47                 :                : /*
                                 48                 :                :  * lo_open
                                 49                 :                :  *    opens an existing large object
                                 50                 :                :  *
                                 51                 :                :  * returns the file descriptor for use in later lo_* calls
                                 52                 :                :  * return -1 upon failure.
                                 53                 :                :  */
                                 54                 :                : int
10580 bruce@momjian.us           55                 :CBC          98 : lo_open(PGconn *conn, Oid lobjId, int mode)
                                 56                 :                : {
                                 57                 :                :     int         fd;
                                 58                 :                :     int         result_len;
                                 59                 :                :     PQArgBlock  argv[2];
                                 60                 :                :     PGresult   *res;
                                 61                 :                : 
 2054 tgl@sss.pgh.pa.us          62         [ -  + ]:             98 :     if (lo_initialize(conn) < 0)
 2054 tgl@sss.pgh.pa.us          63                 :UBC           0 :         return -1;
                                 64                 :                : 
10581 bruce@momjian.us           65                 :CBC          98 :     argv[0].isint = 1;
                                 66                 :             98 :     argv[0].len = 4;
                                 67                 :             98 :     argv[0].u.integer = lobjId;
                                 68                 :                : 
                                 69                 :             98 :     argv[1].isint = 1;
                                 70                 :             98 :     argv[1].len = 4;
                                 71                 :             98 :     argv[1].u.integer = mode;
                                 72                 :                : 
   13 nathan@postgresql.or       73                 :GNC          98 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_open, &fd, -1, &result_len, 1, argv, 2);
10581 bruce@momjian.us           74         [ +  - ]:CBC          98 :     if (PQresultStatus(res) == PGRES_COMMAND_OK)
                                 75                 :                :     {
                                 76                 :             98 :         PQclear(res);
                                 77                 :             98 :         return fd;
                                 78                 :                :     }
                                 79                 :                :     else
                                 80                 :                :     {
10192 tgl@sss.pgh.pa.us          81                 :UBC           0 :         PQclear(res);
10581 bruce@momjian.us           82                 :              0 :         return -1;
                                 83                 :                :     }
                                 84                 :                : }
                                 85                 :                : 
                                 86                 :                : /*
                                 87                 :                :  * lo_close
                                 88                 :                :  *    closes an existing large object
                                 89                 :                :  *
                                 90                 :                :  * returns 0 upon success
                                 91                 :                :  * returns -1 upon failure.
                                 92                 :                :  */
                                 93                 :                : int
10580 bruce@momjian.us           94                 :CBC          98 : lo_close(PGconn *conn, int fd)
                                 95                 :                : {
                                 96                 :                :     PQArgBlock  argv[1];
                                 97                 :                :     PGresult   *res;
                                 98                 :                :     int         retval;
                                 99                 :                :     int         result_len;
                                100                 :                : 
 2054 tgl@sss.pgh.pa.us         101         [ -  + ]:             98 :     if (lo_initialize(conn) < 0)
 2054 tgl@sss.pgh.pa.us         102                 :UBC           0 :         return -1;
                                103                 :                : 
10581 bruce@momjian.us          104                 :CBC          98 :     argv[0].isint = 1;
                                105                 :             98 :     argv[0].len = 4;
                                106                 :             98 :     argv[0].u.integer = fd;
   13 nathan@postgresql.or      107                 :GNC          98 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_close,
                                108                 :                :                 &retval, -1, &result_len, 1, argv, 1);
10581 bruce@momjian.us          109         [ +  - ]:CBC          98 :     if (PQresultStatus(res) == PGRES_COMMAND_OK)
                                110                 :                :     {
                                111                 :             98 :         PQclear(res);
                                112                 :             98 :         return retval;
                                113                 :                :     }
                                114                 :                :     else
                                115                 :                :     {
10192 tgl@sss.pgh.pa.us         116                 :UBC           0 :         PQclear(res);
10581 bruce@momjian.us          117                 :              0 :         return -1;
                                118                 :                :     }
                                119                 :                : }
                                120                 :                : 
                                121                 :                : /*
                                122                 :                :  * lo_truncate
                                123                 :                :  *    truncates an existing large object to the given size
                                124                 :                :  *
                                125                 :                :  * returns 0 upon success
                                126                 :                :  * returns -1 upon failure
                                127                 :                :  */
                                128                 :                : int
 7117                           129                 :              0 : lo_truncate(PGconn *conn, int fd, size_t len)
                                130                 :                : {
                                131                 :                :     PQArgBlock  argv[2];
                                132                 :                :     PGresult   *res;
                                133                 :                :     int         retval;
                                134                 :                :     int         result_len;
                                135                 :                : 
 2054 tgl@sss.pgh.pa.us         136         [ #  # ]:              0 :     if (lo_initialize(conn) < 0)
                                137                 :              0 :         return -1;
                                138                 :                : 
                                139                 :                :     /* Must check this on-the-fly because it's not there pre-8.3 */
 7117 bruce@momjian.us          140         [ #  # ]:              0 :     if (conn->lobjfuncs->fn_lo_truncate == 0)
                                141                 :                :     {
 1381 peter@eisentraut.org      142                 :              0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                143                 :                :                                 "lo_truncate");
 7117 bruce@momjian.us          144                 :              0 :         return -1;
                                145                 :                :     }
                                146                 :                : 
                                147                 :                :     /*
                                148                 :                :      * Long ago, somebody thought it'd be a good idea to declare this function
                                149                 :                :      * as taking size_t ... but the underlying backend function only accepts a
                                150                 :                :      * signed int32 length.  So throw error if the given value overflows
                                151                 :                :      * int32.  (A possible alternative is to automatically redirect the call
                                152                 :                :      * to lo_truncate64; but if the caller wanted to rely on that backend
                                153                 :                :      * function being available, he could have called lo_truncate64 for
                                154                 :                :      * himself.)
                                155                 :                :      */
 5071 tgl@sss.pgh.pa.us         156         [ #  # ]:              0 :     if (len > (size_t) INT_MAX)
                                157                 :                :     {
 1381 peter@eisentraut.org      158                 :              0 :         libpq_append_conn_error(conn, "argument of lo_truncate exceeds integer range");
 5071 tgl@sss.pgh.pa.us         159                 :              0 :         return -1;
                                160                 :                :     }
                                161                 :                : 
 7117 bruce@momjian.us          162                 :              0 :     argv[0].isint = 1;
                                163                 :              0 :     argv[0].len = 4;
                                164                 :              0 :     argv[0].u.integer = fd;
                                165                 :                : 
                                166                 :              0 :     argv[1].isint = 1;
                                167                 :              0 :     argv[1].len = 4;
 5071 tgl@sss.pgh.pa.us         168                 :              0 :     argv[1].u.integer = (int) len;
                                169                 :                : 
   13 nathan@postgresql.or      170                 :UNC           0 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_truncate,
                                171                 :                :                 &retval, -1, &result_len, 1, argv, 2);
                                172                 :                : 
 7117 bruce@momjian.us          173         [ #  # ]:UBC           0 :     if (PQresultStatus(res) == PGRES_COMMAND_OK)
                                174                 :                :     {
                                175                 :              0 :         PQclear(res);
                                176                 :              0 :         return retval;
                                177                 :                :     }
                                178                 :                :     else
                                179                 :                :     {
                                180                 :              0 :         PQclear(res);
                                181                 :              0 :         return -1;
                                182                 :                :     }
                                183                 :                : }
                                184                 :                : 
                                185                 :                : /*
                                186                 :                :  * lo_truncate64
                                187                 :                :  *    truncates an existing large object to the given size
                                188                 :                :  *
                                189                 :                :  * returns 0 upon success
                                190                 :                :  * returns -1 upon failure
                                191                 :                :  */
                                192                 :                : int
  520 tmunro@postgresql.or      193                 :              0 : lo_truncate64(PGconn *conn, int fd, int64_t len)
                                194                 :                : {
                                195                 :                :     PQArgBlock  argv[2];
                                196                 :                :     PGresult   *res;
                                197                 :                :     int         retval;
                                198                 :                :     int         result_len;
                                199                 :                : 
 2054 tgl@sss.pgh.pa.us         200         [ #  # ]:              0 :     if (lo_initialize(conn) < 0)
                                201                 :              0 :         return -1;
                                202                 :                : 
 5072 ishii@postgresql.org      203         [ #  # ]:              0 :     if (conn->lobjfuncs->fn_lo_truncate64 == 0)
                                204                 :                :     {
 1381 peter@eisentraut.org      205                 :              0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                206                 :                :                                 "lo_truncate64");
 5072 ishii@postgresql.org      207                 :              0 :         return -1;
                                208                 :                :     }
                                209                 :                : 
                                210                 :              0 :     argv[0].isint = 1;
                                211                 :              0 :     argv[0].len = 4;
                                212                 :              0 :     argv[0].u.integer = fd;
                                213                 :                : 
   21 nathan@postgresql.or      214                 :UNC           0 :     len = pg_hton64(len);
 5072 ishii@postgresql.org      215                 :UBC           0 :     argv[1].isint = 0;
                                216                 :              0 :     argv[1].len = 8;
                                217                 :              0 :     argv[1].u.ptr = (int *) &len;
                                218                 :                : 
   13 nathan@postgresql.or      219                 :UNC           0 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_truncate64,
                                220                 :                :                 &retval, -1, &result_len, 1, argv, 2);
                                221                 :                : 
 5072 ishii@postgresql.org      222         [ #  # ]:UBC           0 :     if (PQresultStatus(res) == PGRES_COMMAND_OK)
                                223                 :                :     {
                                224                 :              0 :         PQclear(res);
                                225                 :              0 :         return retval;
                                226                 :                :     }
                                227                 :                :     else
                                228                 :                :     {
                                229                 :              0 :         PQclear(res);
                                230                 :              0 :         return -1;
                                231                 :                :     }
                                232                 :                : }
                                233                 :                : 
                                234                 :                : /*
                                235                 :                :  * lo_read
                                236                 :                :  *    read len bytes of the large object into buf
                                237                 :                :  *
                                238                 :                :  * returns the number of bytes read, or -1 on failure.
                                239                 :                :  * the CALLER must have allocated enough space to hold the result returned
                                240                 :                :  */
                                241                 :                : 
                                242                 :                : int
 9786 bruce@momjian.us          243                 :CBC         465 : lo_read(PGconn *conn, int fd, char *buf, size_t len)
                                244                 :                : {
                                245                 :                :     PQArgBlock  argv[2];
                                246                 :                :     PGresult   *res;
                                247                 :                :     int         result_len;
                                248                 :                : 
 2054 tgl@sss.pgh.pa.us         249         [ -  + ]:            465 :     if (lo_initialize(conn) < 0)
 2054 tgl@sss.pgh.pa.us         250                 :UBC           0 :         return -1;
                                251                 :                : 
                                252                 :                :     /*
                                253                 :                :      * Long ago, somebody thought it'd be a good idea to declare this function
                                254                 :                :      * as taking size_t ... but the underlying backend function only accepts a
                                255                 :                :      * signed int32 length.  So throw error if the given value overflows
                                256                 :                :      * int32.
                                257                 :                :      */
 5071 tgl@sss.pgh.pa.us         258         [ -  + ]:CBC         465 :     if (len > (size_t) INT_MAX)
                                259                 :                :     {
 1381 peter@eisentraut.org      260                 :UBC           0 :         libpq_append_conn_error(conn, "argument of lo_read exceeds integer range");
 5071 tgl@sss.pgh.pa.us         261                 :              0 :         return -1;
                                262                 :                :     }
                                263                 :                : 
10581 bruce@momjian.us          264                 :CBC         465 :     argv[0].isint = 1;
                                265                 :            465 :     argv[0].len = 4;
                                266                 :            465 :     argv[0].u.integer = fd;
                                267                 :                : 
                                268                 :            465 :     argv[1].isint = 1;
                                269                 :            465 :     argv[1].len = 4;
 5071 tgl@sss.pgh.pa.us         270                 :            465 :     argv[1].u.integer = (int) len;
                                271                 :                : 
  108 nathan@postgresql.or      272                 :            465 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_read,
                                273                 :                :                 (void *) buf, len, &result_len, 0, argv, 2);
10581 bruce@momjian.us          274         [ +  - ]:            465 :     if (PQresultStatus(res) == PGRES_COMMAND_OK)
                                275                 :                :     {
                                276                 :            465 :         PQclear(res);
                                277                 :            465 :         return result_len;
                                278                 :                :     }
                                279                 :                :     else
                                280                 :                :     {
10192 tgl@sss.pgh.pa.us         281                 :UBC           0 :         PQclear(res);
10581 bruce@momjian.us          282                 :              0 :         return -1;
                                283                 :                :     }
                                284                 :                : }
                                285                 :                : 
                                286                 :                : /*
                                287                 :                :  * lo_write
                                288                 :                :  *    write len bytes of buf into the large object fd
                                289                 :                :  *
                                290                 :                :  * returns the number of bytes written, or -1 on failure.
                                291                 :                :  */
                                292                 :                : int
 7294 bruce@momjian.us          293                 :CBC         657 : lo_write(PGconn *conn, int fd, const char *buf, size_t len)
                                294                 :                : {
                                295                 :                :     PQArgBlock  argv[2];
                                296                 :                :     PGresult   *res;
                                297                 :                :     int         result_len;
                                298                 :                :     int         retval;
                                299                 :                : 
 2054 tgl@sss.pgh.pa.us         300         [ -  + ]:            657 :     if (lo_initialize(conn) < 0)
 2054 tgl@sss.pgh.pa.us         301                 :UBC           0 :         return -1;
                                302                 :                : 
                                303                 :                :     /*
                                304                 :                :      * Long ago, somebody thought it'd be a good idea to declare this function
                                305                 :                :      * as taking size_t ... but the underlying backend function only accepts a
                                306                 :                :      * signed int32 length.  So throw error if the given value overflows
                                307                 :                :      * int32.
                                308                 :                :      */
 5071 tgl@sss.pgh.pa.us         309         [ -  + ]:CBC         657 :     if (len > (size_t) INT_MAX)
                                310                 :                :     {
 1381 peter@eisentraut.org      311                 :UBC           0 :         libpq_append_conn_error(conn, "argument of lo_write exceeds integer range");
 5071 tgl@sss.pgh.pa.us         312                 :              0 :         return -1;
                                313                 :                :     }
                                314                 :                : 
10581 bruce@momjian.us          315                 :CBC         657 :     argv[0].isint = 1;
                                316                 :            657 :     argv[0].len = 4;
                                317                 :            657 :     argv[0].u.integer = fd;
                                318                 :                : 
                                319                 :            657 :     argv[1].isint = 0;
 5071 tgl@sss.pgh.pa.us         320                 :            657 :     argv[1].len = (int) len;
 2767 peter@eisentraut.org      321                 :            657 :     argv[1].u.ptr = (int *) unconstify(char *, buf);
                                322                 :                : 
   13 nathan@postgresql.or      323                 :GNC         657 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_write,
                                324                 :                :                 &retval, -1, &result_len, 1, argv, 2);
10581 bruce@momjian.us          325         [ +  - ]:CBC         657 :     if (PQresultStatus(res) == PGRES_COMMAND_OK)
                                326                 :                :     {
                                327                 :            657 :         PQclear(res);
                                328                 :            657 :         return retval;
                                329                 :                :     }
                                330                 :                :     else
                                331                 :                :     {
10192 tgl@sss.pgh.pa.us         332                 :UBC           0 :         PQclear(res);
10581 bruce@momjian.us          333                 :              0 :         return -1;
                                334                 :                :     }
                                335                 :                : }
                                336                 :                : 
                                337                 :                : /*
                                338                 :                :  * lo_lseek
                                339                 :                :  *    change the current read or write location on a large object
                                340                 :                :  */
                                341                 :                : int
10580                           342                 :              0 : lo_lseek(PGconn *conn, int fd, int offset, int whence)
                                343                 :                : {
                                344                 :                :     PQArgBlock  argv[3];
                                345                 :                :     PGresult   *res;
                                346                 :                :     int         retval;
                                347                 :                :     int         result_len;
                                348                 :                : 
 2054 tgl@sss.pgh.pa.us         349         [ #  # ]:              0 :     if (lo_initialize(conn) < 0)
                                350                 :              0 :         return -1;
                                351                 :                : 
10581 bruce@momjian.us          352                 :              0 :     argv[0].isint = 1;
                                353                 :              0 :     argv[0].len = 4;
                                354                 :              0 :     argv[0].u.integer = fd;
                                355                 :                : 
                                356                 :              0 :     argv[1].isint = 1;
                                357                 :              0 :     argv[1].len = 4;
                                358                 :              0 :     argv[1].u.integer = offset;
                                359                 :                : 
                                360                 :              0 :     argv[2].isint = 1;
                                361                 :              0 :     argv[2].len = 4;
                                362                 :              0 :     argv[2].u.integer = whence;
                                363                 :                : 
   13 nathan@postgresql.or      364                 :UNC           0 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_lseek,
                                365                 :                :                 &retval, -1, &result_len, 1, argv, 3);
10581 bruce@momjian.us          366         [ #  # ]:UBC           0 :     if (PQresultStatus(res) == PGRES_COMMAND_OK)
                                367                 :                :     {
                                368                 :              0 :         PQclear(res);
                                369                 :              0 :         return retval;
                                370                 :                :     }
                                371                 :                :     else
                                372                 :                :     {
10192 tgl@sss.pgh.pa.us         373                 :              0 :         PQclear(res);
10581 bruce@momjian.us          374                 :              0 :         return -1;
                                375                 :                :     }
                                376                 :                : }
                                377                 :                : 
                                378                 :                : /*
                                379                 :                :  * lo_lseek64
                                380                 :                :  *    change the current read or write location on a large object
                                381                 :                :  */
                                382                 :                : int64_t
  520 tmunro@postgresql.or      383                 :              0 : lo_lseek64(PGconn *conn, int fd, int64_t offset, int whence)
                                384                 :                : {
                                385                 :                :     PQArgBlock  argv[3];
                                386                 :                :     PGresult   *res;
                                387                 :                :     int64       retval;
                                388                 :                :     int         result_len;
                                389                 :                : 
 2054 tgl@sss.pgh.pa.us         390         [ #  # ]:              0 :     if (lo_initialize(conn) < 0)
                                391                 :              0 :         return -1;
                                392                 :                : 
 5072 ishii@postgresql.org      393         [ #  # ]:              0 :     if (conn->lobjfuncs->fn_lo_lseek64 == 0)
                                394                 :                :     {
 1381 peter@eisentraut.org      395                 :              0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                396                 :                :                                 "lo_lseek64");
 5072 ishii@postgresql.org      397                 :              0 :         return -1;
                                398                 :                :     }
                                399                 :                : 
                                400                 :              0 :     argv[0].isint = 1;
                                401                 :              0 :     argv[0].len = 4;
                                402                 :              0 :     argv[0].u.integer = fd;
                                403                 :                : 
   21 nathan@postgresql.or      404                 :UNC           0 :     offset = pg_hton64(offset);
 5072 ishii@postgresql.org      405                 :UBC           0 :     argv[1].isint = 0;
                                406                 :              0 :     argv[1].len = 8;
                                407                 :              0 :     argv[1].u.ptr = (int *) &offset;
                                408                 :                : 
                                409                 :              0 :     argv[2].isint = 1;
                                410                 :              0 :     argv[2].len = 4;
                                411                 :              0 :     argv[2].u.integer = whence;
                                412                 :                : 
  108 nathan@postgresql.or      413                 :              0 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_lseek64,
                                414                 :                :                 (void *) &retval, sizeof(retval), &result_len, 0, argv, 3);
 4190 tgl@sss.pgh.pa.us         415   [ #  #  #  # ]:              0 :     if (PQresultStatus(res) == PGRES_COMMAND_OK && result_len == 8)
                                416                 :                :     {
 5072 ishii@postgresql.org      417                 :              0 :         PQclear(res);
   21 nathan@postgresql.or      418                 :UNC           0 :         return pg_ntoh64(retval);
                                419                 :                :     }
                                420                 :                :     else
                                421                 :                :     {
 5072 ishii@postgresql.org      422                 :UBC           0 :         PQclear(res);
                                423                 :              0 :         return -1;
                                424                 :                :     }
                                425                 :                : }
                                426                 :                : 
                                427                 :                : /*
                                428                 :                :  * lo_creat
                                429                 :                :  *    create a new large object
                                430                 :                :  * the mode is ignored (once upon a time it had a use)
                                431                 :                :  *
                                432                 :                :  * returns the oid of the large object created or
                                433                 :                :  * InvalidOid upon failure
                                434                 :                :  */
                                435                 :                : Oid
10580 bruce@momjian.us          436                 :CBC           9 : lo_creat(PGconn *conn, int mode)
                                437                 :                : {
                                438                 :                :     PQArgBlock  argv[1];
                                439                 :                :     PGresult   *res;
                                440                 :                :     int         retval;
                                441                 :                :     int         result_len;
                                442                 :                : 
 2054 tgl@sss.pgh.pa.us         443         [ -  + ]:              9 :     if (lo_initialize(conn) < 0)
 2054 tgl@sss.pgh.pa.us         444                 :UBC           0 :         return InvalidOid;
                                445                 :                : 
10581 bruce@momjian.us          446                 :CBC           9 :     argv[0].isint = 1;
                                447                 :              9 :     argv[0].len = 4;
                                448                 :              9 :     argv[0].u.integer = mode;
   13 nathan@postgresql.or      449                 :GNC           9 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_creat,
                                450                 :                :                 &retval, -1, &result_len, 1, argv, 1);
10581 bruce@momjian.us          451         [ +  - ]:CBC           9 :     if (PQresultStatus(res) == PGRES_COMMAND_OK)
                                452                 :                :     {
                                453                 :              9 :         PQclear(res);
                                454                 :              9 :         return (Oid) retval;
                                455                 :                :     }
                                456                 :                :     else
                                457                 :                :     {
10192 tgl@sss.pgh.pa.us         458                 :UBC           0 :         PQclear(res);
10581 bruce@momjian.us          459                 :              0 :         return InvalidOid;
                                460                 :                :     }
                                461                 :                : }
                                462                 :                : 
                                463                 :                : /*
                                464                 :                :  * lo_create
                                465                 :                :  *    create a new large object
                                466                 :                :  * if lobjId isn't InvalidOid, it specifies the OID to (attempt to) create
                                467                 :                :  *
                                468                 :                :  * returns the oid of the large object created or
                                469                 :                :  * InvalidOid upon failure
                                470                 :                :  */
                                471                 :                : Oid
 7745 tgl@sss.pgh.pa.us         472                 :              0 : lo_create(PGconn *conn, Oid lobjId)
                                473                 :                : {
                                474                 :                :     PQArgBlock  argv[1];
                                475                 :                :     PGresult   *res;
                                476                 :                :     int         retval;
                                477                 :                :     int         result_len;
                                478                 :                : 
 2054                           479         [ #  # ]:              0 :     if (lo_initialize(conn) < 0)
                                480                 :              0 :         return InvalidOid;
                                481                 :                : 
                                482                 :                :     /* Must check this on-the-fly because it's not there pre-8.1 */
 7745                           483         [ #  # ]:              0 :     if (conn->lobjfuncs->fn_lo_create == 0)
                                484                 :                :     {
 1381 peter@eisentraut.org      485                 :              0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                486                 :                :                                 "lo_create");
 7745 tgl@sss.pgh.pa.us         487                 :              0 :         return InvalidOid;
                                488                 :                :     }
                                489                 :                : 
                                490                 :              0 :     argv[0].isint = 1;
                                491                 :              0 :     argv[0].len = 4;
                                492                 :              0 :     argv[0].u.integer = lobjId;
   13 nathan@postgresql.or      493                 :UNC           0 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_create,
                                494                 :                :                 &retval, -1, &result_len, 1, argv, 1);
 7745 tgl@sss.pgh.pa.us         495         [ #  # ]:UBC           0 :     if (PQresultStatus(res) == PGRES_COMMAND_OK)
                                496                 :                :     {
                                497                 :              0 :         PQclear(res);
                                498                 :              0 :         return (Oid) retval;
                                499                 :                :     }
                                500                 :                :     else
                                501                 :                :     {
                                502                 :              0 :         PQclear(res);
                                503                 :              0 :         return InvalidOid;
                                504                 :                :     }
                                505                 :                : }
                                506                 :                : 
                                507                 :                : 
                                508                 :                : /*
                                509                 :                :  * lo_tell
                                510                 :                :  *    returns the current seek location of the large object
                                511                 :                :  */
                                512                 :                : int
10580 bruce@momjian.us          513                 :              0 : lo_tell(PGconn *conn, int fd)
                                514                 :                : {
                                515                 :                :     int         retval;
                                516                 :                :     PQArgBlock  argv[1];
                                517                 :                :     PGresult   *res;
                                518                 :                :     int         result_len;
                                519                 :                : 
 2054 tgl@sss.pgh.pa.us         520         [ #  # ]:              0 :     if (lo_initialize(conn) < 0)
                                521                 :              0 :         return -1;
                                522                 :                : 
10581 bruce@momjian.us          523                 :              0 :     argv[0].isint = 1;
                                524                 :              0 :     argv[0].len = 4;
                                525                 :              0 :     argv[0].u.integer = fd;
                                526                 :                : 
   13 nathan@postgresql.or      527                 :UNC           0 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_tell,
                                528                 :                :                 &retval, -1, &result_len, 1, argv, 1);
10581 bruce@momjian.us          529         [ #  # ]:UBC           0 :     if (PQresultStatus(res) == PGRES_COMMAND_OK)
                                530                 :                :     {
                                531                 :              0 :         PQclear(res);
                                532                 :              0 :         return retval;
                                533                 :                :     }
                                534                 :                :     else
                                535                 :                :     {
10192 tgl@sss.pgh.pa.us         536                 :              0 :         PQclear(res);
10581 bruce@momjian.us          537                 :              0 :         return -1;
                                538                 :                :     }
                                539                 :                : }
                                540                 :                : 
                                541                 :                : /*
                                542                 :                :  * lo_tell64
                                543                 :                :  *    returns the current seek location of the large object
                                544                 :                :  */
                                545                 :                : int64_t
 5072 ishii@postgresql.org      546                 :              0 : lo_tell64(PGconn *conn, int fd)
                                547                 :                : {
                                548                 :                :     int64       retval;
                                549                 :                :     PQArgBlock  argv[1];
                                550                 :                :     PGresult   *res;
                                551                 :                :     int         result_len;
                                552                 :                : 
 2054 tgl@sss.pgh.pa.us         553         [ #  # ]:              0 :     if (lo_initialize(conn) < 0)
                                554                 :              0 :         return -1;
                                555                 :                : 
 5072 ishii@postgresql.org      556         [ #  # ]:              0 :     if (conn->lobjfuncs->fn_lo_tell64 == 0)
                                557                 :                :     {
 1381 peter@eisentraut.org      558                 :              0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                559                 :                :                                 "lo_tell64");
 5072 ishii@postgresql.org      560                 :              0 :         return -1;
                                561                 :                :     }
                                562                 :                : 
                                563                 :              0 :     argv[0].isint = 1;
                                564                 :              0 :     argv[0].len = 4;
                                565                 :              0 :     argv[0].u.integer = fd;
                                566                 :                : 
  108 nathan@postgresql.or      567                 :              0 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_tell64,
                                568                 :                :                 (void *) &retval, sizeof(retval), &result_len, 0, argv, 1);
 4190 tgl@sss.pgh.pa.us         569   [ #  #  #  # ]:              0 :     if (PQresultStatus(res) == PGRES_COMMAND_OK && result_len == 8)
                                570                 :                :     {
 5072 ishii@postgresql.org      571                 :              0 :         PQclear(res);
   21 nathan@postgresql.or      572                 :UNC           0 :         return pg_ntoh64(retval);
                                573                 :                :     }
                                574                 :                :     else
                                575                 :                :     {
 5072 ishii@postgresql.org      576                 :UBC           0 :         PQclear(res);
                                577                 :              0 :         return -1;
                                578                 :                :     }
                                579                 :                : }
                                580                 :                : 
                                581                 :                : /*
                                582                 :                :  * lo_unlink
                                583                 :                :  *    delete a file
                                584                 :                :  */
                                585                 :                : 
                                586                 :                : int
10580 bruce@momjian.us          587                 :CBC          16 : lo_unlink(PGconn *conn, Oid lobjId)
                                588                 :                : {
                                589                 :                :     PQArgBlock  argv[1];
                                590                 :                :     PGresult   *res;
                                591                 :                :     int         result_len;
                                592                 :                :     int         retval;
                                593                 :                : 
 2054 tgl@sss.pgh.pa.us         594         [ -  + ]:             16 :     if (lo_initialize(conn) < 0)
 2054 tgl@sss.pgh.pa.us         595                 :UBC           0 :         return -1;
                                596                 :                : 
10581 bruce@momjian.us          597                 :CBC          16 :     argv[0].isint = 1;
                                598                 :             16 :     argv[0].len = 4;
                                599                 :             16 :     argv[0].u.integer = lobjId;
                                600                 :                : 
   13 nathan@postgresql.or      601                 :GNC          16 :     res = PQnfn(conn, conn->lobjfuncs->fn_lo_unlink,
                                602                 :                :                 &retval, -1, &result_len, 1, argv, 1);
10581 bruce@momjian.us          603         [ +  - ]:CBC          16 :     if (PQresultStatus(res) == PGRES_COMMAND_OK)
                                604                 :                :     {
                                605                 :             16 :         PQclear(res);
                                606                 :             16 :         return retval;
                                607                 :                :     }
                                608                 :                :     else
                                609                 :                :     {
10192 tgl@sss.pgh.pa.us         610                 :UBC           0 :         PQclear(res);
10581 bruce@momjian.us          611                 :              0 :         return -1;
                                612                 :                :     }
                                613                 :                : }
                                614                 :                : 
                                615                 :                : /*
                                616                 :                :  * lo_import -
                                617                 :                :  *    imports a file as an (inversion) large object.
                                618                 :                :  *
                                619                 :                :  * returns the oid of that object upon success,
                                620                 :                :  * returns InvalidOid upon failure
                                621                 :                :  */
                                622                 :                : 
                                623                 :                : Oid
 9786 bruce@momjian.us          624                 :CBC           9 : lo_import(PGconn *conn, const char *filename)
                                625                 :                : {
 6735 ishii@postgresql.org      626                 :              9 :     return lo_import_internal(conn, filename, InvalidOid);
                                627                 :                : }
                                628                 :                : 
                                629                 :                : /*
                                630                 :                :  * lo_import_with_oid -
                                631                 :                :  *    imports a file as an (inversion) large object.
                                632                 :                :  *    large object id can be specified.
                                633                 :                :  *
                                634                 :                :  * returns the oid of that object upon success,
                                635                 :                :  * returns InvalidOid upon failure
                                636                 :                :  */
                                637                 :                : 
                                638                 :                : Oid
 6735 ishii@postgresql.org      639                 :UBC           0 : lo_import_with_oid(PGconn *conn, const char *filename, Oid lobjId)
                                640                 :                : {
                                641                 :              0 :     return lo_import_internal(conn, filename, lobjId);
                                642                 :                : }
                                643                 :                : 
                                644                 :                : static Oid
 5258 tgl@sss.pgh.pa.us         645                 :CBC           9 : lo_import_internal(PGconn *conn, const char *filename, Oid oid)
                                646                 :                : {
                                647                 :                :     int         fd;
                                648                 :                :     ssize_t     nbytes;
                                649                 :                :     int         tmp;
                                650                 :                :     char        buf[LO_BUFSIZE];
                                651                 :                :     Oid         lobjOid;
                                652                 :                :     int         lobj;
                                653                 :                :     char        sebuf[PG_STRERROR_R_BUFLEN];
                                654                 :                : 
 2054                           655         [ -  + ]:              9 :     if (conn == NULL)
 2054 tgl@sss.pgh.pa.us         656                 :UBC           0 :         return InvalidOid;
                                657                 :                : 
                                658                 :                :     /* Since this is the beginning of a query cycle, reset the error state */
 1651 tgl@sss.pgh.pa.us         659                 :CBC           9 :     pqClearConnErrorState(conn);
                                660                 :                : 
                                661                 :                :     /*
                                662                 :                :      * open the file to be read in
                                663                 :                :      */
 9582 bruce@momjian.us          664                 :              9 :     fd = open(filename, O_RDONLY | PG_BINARY, 0666);
10581                           665         [ -  + ]:              9 :     if (fd < 0)
                                666                 :                :     {                           /* error */
 1381 peter@eisentraut.org      667                 :UBC           0 :         libpq_append_conn_error(conn, "could not open file \"%s\": %s",
 1196 tgl@sss.pgh.pa.us         668                 :              0 :                                 filename, strerror_r(errno, sebuf, sizeof(sebuf)));
10581 bruce@momjian.us          669                 :              0 :         return InvalidOid;
                                670                 :                :     }
                                671                 :                : 
                                672                 :                :     /*
                                673                 :                :      * create an inversion object
                                674                 :                :      */
 6735 ishii@postgresql.org      675         [ +  - ]:CBC           9 :     if (oid == InvalidOid)
                                676                 :              9 :         lobjOid = lo_creat(conn, INV_READ | INV_WRITE);
                                677                 :                :     else
 6735 ishii@postgresql.org      678                 :UBC           0 :         lobjOid = lo_create(conn, oid);
                                679                 :                : 
10581 bruce@momjian.us          680         [ -  + ]:CBC           9 :     if (lobjOid == InvalidOid)
                                681                 :                :     {
                                682                 :                :         /* we assume lo_create() already set a suitable error message */
 9110 tgl@sss.pgh.pa.us         683                 :UBC           0 :         (void) close(fd);
10581 bruce@momjian.us          684                 :              0 :         return InvalidOid;
                                685                 :                :     }
                                686                 :                : 
10581 bruce@momjian.us          687                 :CBC           9 :     lobj = lo_open(conn, lobjOid, INV_WRITE);
                                688         [ -  + ]:              9 :     if (lobj == -1)
                                689                 :                :     {
                                690                 :                :         /* we assume lo_open() already set a suitable error message */
 9110 tgl@sss.pgh.pa.us         691                 :UBC           0 :         (void) close(fd);
10581 bruce@momjian.us          692                 :              0 :         return InvalidOid;
                                693                 :                :     }
                                694                 :                : 
                                695                 :                :     /*
                                696                 :                :      * read in from the file and write to the large object
                                697                 :                :      */
10581 bruce@momjian.us          698         [ +  + ]:CBC         666 :     while ((nbytes = read(fd, buf, LO_BUFSIZE)) > 0)
                                699                 :                :     {
                                700                 :            657 :         tmp = lo_write(conn, lobj, buf, nbytes);
 7379 tgl@sss.pgh.pa.us         701         [ -  + ]:            657 :         if (tmp != nbytes)
                                702                 :                :         {
                                703                 :                :             /*
                                704                 :                :              * If lo_write() failed, we are now in an aborted transaction so
                                705                 :                :              * there's no need for lo_close(); furthermore, if we tried it
                                706                 :                :              * we'd overwrite the useful error result with a useless one. So
                                707                 :                :              * just nail the doors shut and get out of town.
                                708                 :                :              */
 9110 tgl@sss.pgh.pa.us         709                 :UBC           0 :             (void) close(fd);
10581 bruce@momjian.us          710                 :              0 :             return InvalidOid;
                                711                 :                :         }
                                712                 :                :     }
                                713                 :                : 
 7379 tgl@sss.pgh.pa.us         714         [ -  + ]:CBC           9 :     if (nbytes < 0)
                                715                 :                :     {
                                716                 :                :         /* We must do lo_close before setting the errorMessage */
 5071 tgl@sss.pgh.pa.us         717                 :UBC           0 :         int         save_errno = errno;
                                718                 :                : 
                                719                 :              0 :         (void) lo_close(conn, lobj);
                                720                 :              0 :         (void) close(fd);
                                721                 :                :         /* deliberately overwrite any error from lo_close */
 1651                           722                 :              0 :         pqClearConnErrorState(conn);
 1381 peter@eisentraut.org      723                 :              0 :         libpq_append_conn_error(conn, "could not read from file \"%s\": %s",
                                724                 :                :                                 filename,
                                725                 :                :                                 strerror_r(save_errno, sebuf, sizeof(sebuf)));
 5071 tgl@sss.pgh.pa.us         726                 :              0 :         return InvalidOid;
                                727                 :                :     }
                                728                 :                : 
10581 bruce@momjian.us          729                 :CBC           9 :     (void) close(fd);
                                730                 :                : 
 7379 tgl@sss.pgh.pa.us         731         [ -  + ]:              9 :     if (lo_close(conn, lobj) != 0)
                                732                 :                :     {
                                733                 :                :         /* we assume lo_close() already set a suitable error message */
 7379 tgl@sss.pgh.pa.us         734                 :UBC           0 :         return InvalidOid;
                                735                 :                :     }
                                736                 :                : 
10581 bruce@momjian.us          737                 :CBC           9 :     return lobjOid;
                                738                 :                : }
                                739                 :                : 
                                740                 :                : /*
                                741                 :                :  * lo_export -
                                742                 :                :  *    exports an (inversion) large object.
                                743                 :                :  * returns -1 upon failure, 1 if OK
                                744                 :                :  */
                                745                 :                : int
 9786                           746                 :              4 : lo_export(PGconn *conn, Oid lobjId, const char *filename)
                                747                 :                : {
 7379 tgl@sss.pgh.pa.us         748                 :              4 :     int         result = 1;
                                749                 :                :     int         fd;
                                750                 :                :     int         nbytes;
                                751                 :                :     char        buf[LO_BUFSIZE];
                                752                 :                :     int         lobj;
                                753                 :                :     char        sebuf[PG_STRERROR_R_BUFLEN];
                                754                 :                : 
                                755                 :                :     /*
                                756                 :                :      * open the large object.
                                757                 :                :      */
10581 bruce@momjian.us          758                 :              4 :     lobj = lo_open(conn, lobjId, INV_READ);
                                759         [ -  + ]:              4 :     if (lobj == -1)
                                760                 :                :     {
                                761                 :                :         /* we assume lo_open() already set a suitable error message */
10581 bruce@momjian.us          762                 :UBC           0 :         return -1;
                                763                 :                :     }
                                764                 :                : 
                                765                 :                :     /*
                                766                 :                :      * create the file to be written to
                                767                 :                :      */
 9582 bruce@momjian.us          768                 :CBC           4 :     fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666);
10581                           769         [ -  + ]:              4 :     if (fd < 0)
                                770                 :                :     {
                                771                 :                :         /* We must do lo_close before setting the errorMessage */
 5071 tgl@sss.pgh.pa.us         772                 :UBC           0 :         int         save_errno = errno;
                                773                 :                : 
                                774                 :              0 :         (void) lo_close(conn, lobj);
                                775                 :                :         /* deliberately overwrite any error from lo_close */
 1651                           776                 :              0 :         pqClearConnErrorState(conn);
 1381 peter@eisentraut.org      777                 :              0 :         libpq_append_conn_error(conn, "could not open file \"%s\": %s",
                                778                 :                :                                 filename,
                                779                 :                :                                 strerror_r(save_errno, sebuf, sizeof(sebuf)));
 9110 tgl@sss.pgh.pa.us         780                 :              0 :         return -1;
                                781                 :                :     }
                                782                 :                : 
                                783                 :                :     /*
                                784                 :                :      * read in from the large object and write to the file
                                785                 :                :      */
10581 bruce@momjian.us          786         [ +  + ]:CBC         332 :     while ((nbytes = lo_read(conn, lobj, buf, LO_BUFSIZE)) > 0)
                                787                 :                :     {
                                788                 :                :         ssize_t     tmp;
                                789                 :                : 
                                790                 :            328 :         tmp = write(fd, buf, nbytes);
 7379 tgl@sss.pgh.pa.us         791         [ -  + ]:            328 :         if (tmp != nbytes)
                                792                 :                :         {
                                793                 :                :             /* We must do lo_close before setting the errorMessage */
 5071 tgl@sss.pgh.pa.us         794                 :UBC           0 :             int         save_errno = errno;
                                795                 :                : 
 9110                           796                 :              0 :             (void) lo_close(conn, lobj);
                                797                 :              0 :             (void) close(fd);
                                798                 :                :             /* deliberately overwrite any error from lo_close */
 1651                           799                 :              0 :             pqClearConnErrorState(conn);
 1381 peter@eisentraut.org      800                 :              0 :             libpq_append_conn_error(conn, "could not write to file \"%s\": %s",
                                801                 :                :                                     filename,
                                802                 :                :                                     strerror_r(save_errno, sebuf, sizeof(sebuf)));
10581 bruce@momjian.us          803                 :              0 :             return -1;
                                804                 :                :         }
                                805                 :                :     }
                                806                 :                : 
                                807                 :                :     /*
                                808                 :                :      * If lo_read() failed, we are now in an aborted transaction so there's no
                                809                 :                :      * need for lo_close(); furthermore, if we tried it we'd overwrite the
                                810                 :                :      * useful error result with a useless one. So skip lo_close() if we got a
                                811                 :                :      * failure result.
                                812                 :                :      */
 7379 tgl@sss.pgh.pa.us         813   [ +  -  -  + ]:CBC           8 :     if (nbytes < 0 ||
                                814                 :              4 :         lo_close(conn, lobj) != 0)
                                815                 :                :     {
                                816                 :                :         /* assume lo_read() or lo_close() left a suitable error message */
 7379 tgl@sss.pgh.pa.us         817                 :UBC           0 :         result = -1;
                                818                 :                :     }
                                819                 :                : 
                                820                 :                :     /* if we already failed, don't overwrite that msg with a close error */
 2609 peter@eisentraut.org      821   [ -  +  -  - ]:CBC           4 :     if (close(fd) != 0 && result >= 0)
                                822                 :                :     {
 1381 peter@eisentraut.org      823                 :UBC           0 :         libpq_append_conn_error(conn, "could not write to file \"%s\": %s",
 1196 tgl@sss.pgh.pa.us         824                 :              0 :                                 filename, strerror_r(errno, sebuf, sizeof(sebuf)));
 7379                           825                 :              0 :         result = -1;
                                826                 :                :     }
                                827                 :                : 
 7379 tgl@sss.pgh.pa.us         828                 :CBC           4 :     return result;
                                829                 :                : }
                                830                 :                : 
                                831                 :                : 
                                832                 :                : /*
                                833                 :                :  * lo_initialize
                                834                 :                :  *
                                835                 :                :  * Initialize for a new large-object operation on an existing connection.
                                836                 :                :  * Return 0 if OK, -1 on failure.
                                837                 :                :  *
                                838                 :                :  * If we haven't previously done so, we collect the function OIDs from
                                839                 :                :  * pg_proc for all functions that are required for large object operations.
                                840                 :                :  */
                                841                 :                : static int
10580 bruce@momjian.us          842                 :           1343 : lo_initialize(PGconn *conn)
                                843                 :                : {
                                844                 :                :     PGresult   *res;
                                845                 :                :     PGlobjfuncs *lobjfuncs;
                                846                 :                :     int         n;
                                847                 :                :     const char *query;
                                848                 :                :     const char *fname;
                                849                 :                :     Oid         foid;
                                850                 :                : 
                                851                 :                :     /* Nothing we can do with no connection */
 2054 tgl@sss.pgh.pa.us         852         [ -  + ]:           1343 :     if (conn == NULL)
 5258 tgl@sss.pgh.pa.us         853                 :UBC           0 :         return -1;
                                854                 :                : 
                                855                 :                :     /* Since this is the beginning of a query cycle, reset the error state */
 1651 tgl@sss.pgh.pa.us         856                 :CBC        1343 :     pqClearConnErrorState(conn);
                                857                 :                : 
                                858                 :                :     /* Nothing else to do if we already collected info */
 2054                           859         [ +  + ]:           1343 :     if (conn->lobjfuncs != NULL)
                                860                 :           1293 :         return 0;
                                861                 :                : 
                                862                 :                :     /*
                                863                 :                :      * Allocate the structure to hold the function OIDs.  We don't store it
                                864                 :                :      * into the PGconn until it's successfully filled.
                                865                 :                :      */
10581 bruce@momjian.us          866                 :             50 :     lobjfuncs = (PGlobjfuncs *) malloc(sizeof(PGlobjfuncs));
 8268 neilc@samurai.com         867         [ -  + ]:             50 :     if (lobjfuncs == NULL)
                                868                 :                :     {
 1381 peter@eisentraut.org      869                 :UBC           0 :         libpq_append_conn_error(conn, "out of memory");
10581 bruce@momjian.us          870                 :              0 :         return -1;
                                871                 :                :     }
  561 peter@eisentraut.org      872   [ +  -  -  +  :CBC          50 :     MemSet(lobjfuncs, 0, sizeof(PGlobjfuncs));
                                     -  -  -  -  -  
                                                 - ]
                                873                 :                : 
                                874                 :                :     /*
                                875                 :                :      * Execute the query to get all the functions at once.  (Not all of them
                                876                 :                :      * may exist in older server versions.)
                                877                 :                :      */
 2002 heikki.linnakangas@i      878                 :             50 :     query = "select proname, oid from pg_catalog.pg_proc "
                                879                 :                :         "where proname in ("
                                880                 :                :         "'lo_open', "
                                881                 :                :         "'lo_close', "
                                882                 :                :         "'lo_creat', "
                                883                 :                :         "'lo_create', "
                                884                 :                :         "'lo_unlink', "
                                885                 :                :         "'lo_lseek', "
                                886                 :                :         "'lo_lseek64', "
                                887                 :                :         "'lo_tell', "
                                888                 :                :         "'lo_tell64', "
                                889                 :                :         "'lo_truncate', "
                                890                 :                :         "'lo_truncate64', "
                                891                 :                :         "'loread', "
                                892                 :                :         "'lowrite') "
                                893                 :                :         "and pronamespace = (select oid from pg_catalog.pg_namespace "
                                894                 :                :         "where nspname = 'pg_catalog')";
                                895                 :                : 
 8210 tgl@sss.pgh.pa.us         896                 :             50 :     res = PQexec(conn, query);
 8268 neilc@samurai.com         897         [ -  + ]:             50 :     if (res == NULL)
                                898                 :                :     {
10581 bruce@momjian.us          899                 :UBC           0 :         free(lobjfuncs);
                                900                 :              0 :         return -1;
                                901                 :                :     }
                                902                 :                : 
10581 bruce@momjian.us          903         [ -  + ]:CBC          50 :     if (res->resultStatus != PGRES_TUPLES_OK)
                                904                 :                :     {
10581 bruce@momjian.us          905                 :UBC           0 :         free(lobjfuncs);
                                906                 :              0 :         PQclear(res);
 1381 peter@eisentraut.org      907                 :              0 :         libpq_append_conn_error(conn, "query to initialize large object functions did not return data");
10581 bruce@momjian.us          908                 :              0 :         return -1;
                                909                 :                :     }
                                910                 :                : 
                                911                 :                :     /*
                                912                 :                :      * Examine the result and put the OID's into the struct
                                913                 :                :      */
10581 bruce@momjian.us          914         [ +  + ]:CBC         700 :     for (n = 0; n < PQntuples(res); n++)
                                915                 :                :     {
                                916                 :            650 :         fname = PQgetvalue(res, n, 0);
                                917                 :            650 :         foid = (Oid) atoi(PQgetvalue(res, n, 1));
 5357 peter_e@gmx.net           918         [ +  + ]:            650 :         if (strcmp(fname, "lo_open") == 0)
10581 bruce@momjian.us          919                 :             50 :             lobjfuncs->fn_lo_open = foid;
 5357 peter_e@gmx.net           920         [ +  + ]:            600 :         else if (strcmp(fname, "lo_close") == 0)
10581 bruce@momjian.us          921                 :             50 :             lobjfuncs->fn_lo_close = foid;
 5357 peter_e@gmx.net           922         [ +  + ]:            550 :         else if (strcmp(fname, "lo_creat") == 0)
10581 bruce@momjian.us          923                 :             50 :             lobjfuncs->fn_lo_creat = foid;
 5357 peter_e@gmx.net           924         [ +  + ]:            500 :         else if (strcmp(fname, "lo_create") == 0)
 7745 tgl@sss.pgh.pa.us         925                 :             50 :             lobjfuncs->fn_lo_create = foid;
 5357 peter_e@gmx.net           926         [ +  + ]:            450 :         else if (strcmp(fname, "lo_unlink") == 0)
10581 bruce@momjian.us          927                 :             50 :             lobjfuncs->fn_lo_unlink = foid;
 5357 peter_e@gmx.net           928         [ +  + ]:            400 :         else if (strcmp(fname, "lo_lseek") == 0)
10581 bruce@momjian.us          929                 :             50 :             lobjfuncs->fn_lo_lseek = foid;
 5072 ishii@postgresql.org      930         [ +  + ]:            350 :         else if (strcmp(fname, "lo_lseek64") == 0)
                                931                 :             50 :             lobjfuncs->fn_lo_lseek64 = foid;
 5357 peter_e@gmx.net           932         [ +  + ]:            300 :         else if (strcmp(fname, "lo_tell") == 0)
10581 bruce@momjian.us          933                 :             50 :             lobjfuncs->fn_lo_tell = foid;
 5072 ishii@postgresql.org      934         [ +  + ]:            250 :         else if (strcmp(fname, "lo_tell64") == 0)
                                935                 :             50 :             lobjfuncs->fn_lo_tell64 = foid;
 5357 peter_e@gmx.net           936         [ +  + ]:            200 :         else if (strcmp(fname, "lo_truncate") == 0)
 7117 bruce@momjian.us          937                 :             50 :             lobjfuncs->fn_lo_truncate = foid;
 5072 ishii@postgresql.org      938         [ +  + ]:            150 :         else if (strcmp(fname, "lo_truncate64") == 0)
                                939                 :             50 :             lobjfuncs->fn_lo_truncate64 = foid;
 5357 peter_e@gmx.net           940         [ +  + ]:            100 :         else if (strcmp(fname, "loread") == 0)
10581 bruce@momjian.us          941                 :             50 :             lobjfuncs->fn_lo_read = foid;
 5357 peter_e@gmx.net           942         [ +  - ]:             50 :         else if (strcmp(fname, "lowrite") == 0)
10581 bruce@momjian.us          943                 :             50 :             lobjfuncs->fn_lo_write = foid;
                                944                 :                :     }
                                945                 :                : 
10881 scrappy@hub.org           946                 :             50 :     PQclear(res);
                                947                 :                : 
                                948                 :                :     /*
                                949                 :                :      * Finally check that we got all required large object interface functions
                                950                 :                :      * (ones that have been added later than the stone age are instead checked
                                951                 :                :      * only if used)
                                952                 :                :      */
10581 bruce@momjian.us          953         [ -  + ]:             50 :     if (lobjfuncs->fn_lo_open == 0)
                                954                 :                :     {
 1381 peter@eisentraut.org      955                 :UBC           0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                956                 :                :                                 "lo_open");
10581 bruce@momjian.us          957                 :              0 :         free(lobjfuncs);
                                958                 :              0 :         return -1;
                                959                 :                :     }
10581 bruce@momjian.us          960         [ -  + ]:CBC          50 :     if (lobjfuncs->fn_lo_close == 0)
                                961                 :                :     {
 1381 peter@eisentraut.org      962                 :UBC           0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                963                 :                :                                 "lo_close");
10581 bruce@momjian.us          964                 :              0 :         free(lobjfuncs);
                                965                 :              0 :         return -1;
                                966                 :                :     }
10581 bruce@momjian.us          967         [ -  + ]:CBC          50 :     if (lobjfuncs->fn_lo_creat == 0)
                                968                 :                :     {
 1381 peter@eisentraut.org      969                 :UBC           0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                970                 :                :                                 "lo_creat");
10581 bruce@momjian.us          971                 :              0 :         free(lobjfuncs);
                                972                 :              0 :         return -1;
                                973                 :                :     }
10581 bruce@momjian.us          974         [ -  + ]:CBC          50 :     if (lobjfuncs->fn_lo_unlink == 0)
                                975                 :                :     {
 1381 peter@eisentraut.org      976                 :UBC           0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                977                 :                :                                 "lo_unlink");
10581 bruce@momjian.us          978                 :              0 :         free(lobjfuncs);
                                979                 :              0 :         return -1;
                                980                 :                :     }
10581 bruce@momjian.us          981         [ -  + ]:CBC          50 :     if (lobjfuncs->fn_lo_lseek == 0)
                                982                 :                :     {
 1381 peter@eisentraut.org      983                 :UBC           0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                984                 :                :                                 "lo_lseek");
10581 bruce@momjian.us          985                 :              0 :         free(lobjfuncs);
                                986                 :              0 :         return -1;
                                987                 :                :     }
10581 bruce@momjian.us          988         [ -  + ]:CBC          50 :     if (lobjfuncs->fn_lo_tell == 0)
                                989                 :                :     {
 1381 peter@eisentraut.org      990                 :UBC           0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                991                 :                :                                 "lo_tell");
10581 bruce@momjian.us          992                 :              0 :         free(lobjfuncs);
                                993                 :              0 :         return -1;
                                994                 :                :     }
10581 bruce@momjian.us          995         [ -  + ]:CBC          50 :     if (lobjfuncs->fn_lo_read == 0)
                                996                 :                :     {
 1381 peter@eisentraut.org      997                 :UBC           0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                                998                 :                :                                 "loread");
10581 bruce@momjian.us          999                 :              0 :         free(lobjfuncs);
                               1000                 :              0 :         return -1;
                               1001                 :                :     }
10581 bruce@momjian.us         1002         [ -  + ]:CBC          50 :     if (lobjfuncs->fn_lo_write == 0)
                               1003                 :                :     {
 1381 peter@eisentraut.org     1004                 :UBC           0 :         libpq_append_conn_error(conn, "cannot determine OID of function %s",
                               1005                 :                :                                 "lowrite");
10581 bruce@momjian.us         1006                 :              0 :         free(lobjfuncs);
                               1007                 :              0 :         return -1;
                               1008                 :                :     }
                               1009                 :                : 
                               1010                 :                :     /*
                               1011                 :                :      * Put the structure into the connection control
                               1012                 :                :      */
10581 bruce@momjian.us         1013                 :CBC          50 :     conn->lobjfuncs = lobjfuncs;
                               1014                 :             50 :     return 0;
                               1015                 :                : }
                               1016                 :                : /* /home/coverage/diff-cov-workdir/pg-current/src/interfaces/libpq/fe-lobj.c not long enough */
                               1017                 :                : /* (content generated from coverage data) */
                               1018                 :                : /* ... */
                               1019                 :                : /* ... */
                               1020                 :                : /* ... */
                               1021                 :                : /* ... */
                               1022                 :                : /* ... */
                               1023                 :                : /* BEGIN: function "lo_hton64" */
                               1024                 :                : /* ... */
                               1025                 :                : /* ... */
                               1026                 :                : /* ... */
                               1027                 :                : /* ... */
                               1028                 :                : /* ... */
                               1029                 :                : /* ... */
                               1030                 :                : /* ... */
                               1031                 :                : /* ... */
                               1032                 :                : /* ... */
                               1033                 :                : /* ... */
                               1034                 :                : /* ... */
                               1035                 :                : /* ... */
                               1036                 :                : /* /home/coverage/diff-cov-workdir/pg-current/src/interfaces/libpq/fe-lobj.c not long enough */
                               1037                 :                : /* (content generated from coverage data) */
                               1038                 :                : /* ... */
                               1039                 :                : /* ... */
                               1040                 :                : /* ... */
                               1041                 :                : /* ... */
                               1042                 :                : /* ... */
                               1043                 :                : /* ... */
                               1044                 :                : /* ... */
                               1045                 :                : /* ... */
                               1046                 :                : /* ... */
                               1047                 :                : /* ... */
                               1048                 :                : /* BEGIN: function "lo_ntoh64" */
        

Generated by: LCOV version 2.0-1