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

Generated by: LCOV version 2.0-1