LCOV - code coverage report
Current view: top level - src/bin/pg_dump - pg_backup_null.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 71 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 12 0
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * pg_backup_null.c
       4              :  *
       5              :  *      Implementation of an archive that is never saved; it is used by
       6              :  *      pg_dump to output a plain text SQL script instead of saving
       7              :  *      a real archive.
       8              :  *
       9              :  *      See the headers to pg_restore for more details.
      10              :  *
      11              :  * Copyright (c) 2000, Philip Warner
      12              :  *              Rights are granted to use this software in any way so long
      13              :  *              as this notice is not removed.
      14              :  *
      15              :  *      The author is not responsible for loss or damages that may
      16              :  *      result from its use.
      17              :  *
      18              :  *
      19              :  * IDENTIFICATION
      20              :  *              src/bin/pg_dump/pg_backup_null.c
      21              :  *
      22              :  *-------------------------------------------------------------------------
      23              :  */
      24              : #include "postgres_fe.h"
      25              : 
      26              : #include "fe_utils/string_utils.h"
      27              : #include "libpq/libpq-fs.h"
      28              : #include "pg_backup_archiver.h"
      29              : #include "pg_backup_utils.h"
      30              : 
      31              : static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
      32              : static void _WriteLOData(ArchiveHandle *AH, const void *data, size_t dLen);
      33              : static void _EndData(ArchiveHandle *AH, TocEntry *te);
      34              : static int      _WriteByte(ArchiveHandle *AH, const int i);
      35              : static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
      36              : static void _CloseArchive(ArchiveHandle *AH);
      37              : static void _PrintTocData(ArchiveHandle *AH, TocEntry *te);
      38              : static void _StartLOs(ArchiveHandle *AH, TocEntry *te);
      39              : static void _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid);
      40              : static void _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid);
      41              : static void _EndLOs(ArchiveHandle *AH, TocEntry *te);
      42              : 
      43              : 
      44              : /*
      45              :  *      Initializer
      46              :  */
      47              : void
      48            0 : InitArchiveFmt_Null(ArchiveHandle *AH)
      49              : {
      50              :         /* Assuming static functions, this can be copied for each format. */
      51            0 :         AH->WriteDataPtr = _WriteData;
      52            0 :         AH->EndDataPtr = _EndData;
      53            0 :         AH->WriteBytePtr = _WriteByte;
      54            0 :         AH->WriteBufPtr = _WriteBuf;
      55            0 :         AH->ClosePtr = _CloseArchive;
      56            0 :         AH->ReopenPtr = NULL;
      57            0 :         AH->PrintTocDataPtr = _PrintTocData;
      58              : 
      59            0 :         AH->StartLOsPtr = _StartLOs;
      60            0 :         AH->StartLOPtr = _StartLO;
      61            0 :         AH->EndLOPtr = _EndLO;
      62            0 :         AH->EndLOsPtr = _EndLOs;
      63            0 :         AH->ClonePtr = NULL;
      64            0 :         AH->DeClonePtr = NULL;
      65              : 
      66              :         /*
      67              :          * Now prevent reading...
      68              :          */
      69            0 :         if (AH->mode == archModeRead)
      70            0 :                 pg_fatal("this format cannot be read");
      71            0 : }
      72              : 
      73              : /*
      74              :  * - Start a new TOC entry
      75              :  */
      76              : 
      77              : /*
      78              :  * Called by dumper via archiver from within a data dump routine
      79              :  */
      80              : static void
      81            0 : _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
      82              : {
      83              :         /* Just send it to output, ahwrite() already errors on failure */
      84            0 :         ahwrite(data, 1, dLen, AH);
      85            0 : }
      86              : 
      87              : /*
      88              :  * Called by dumper via archiver from within a data dump routine
      89              :  * We substitute this for _WriteData while emitting a LO
      90              :  */
      91              : static void
      92            0 : _WriteLOData(ArchiveHandle *AH, const void *data, size_t dLen)
      93              : {
      94            0 :         if (dLen > 0)
      95              :         {
      96            0 :                 PQExpBuffer buf = createPQExpBuffer();
      97              : 
      98            0 :                 appendByteaLiteralAHX(buf,
      99              :                                                           (const unsigned char *) data,
     100              :                                                           dLen,
     101              :                                                           AH);
     102              : 
     103            0 :                 ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data);
     104              : 
     105            0 :                 destroyPQExpBuffer(buf);
     106            0 :         }
     107            0 : }
     108              : 
     109              : static void
     110            0 : _EndData(ArchiveHandle *AH, TocEntry *te)
     111              : {
     112            0 :         ahprintf(AH, "\n\n");
     113            0 : }
     114              : 
     115              : /*
     116              :  * Called by the archiver when starting to save BLOB DATA (not schema).
     117              :  * This routine should save whatever format-specific information is needed
     118              :  * to read the LOs back into memory.
     119              :  *
     120              :  * It is called just prior to the dumper's DataDumper routine.
     121              :  *
     122              :  * Optional, but strongly recommended.
     123              :  */
     124              : static void
     125            0 : _StartLOs(ArchiveHandle *AH, TocEntry *te)
     126              : {
     127            0 :         ahprintf(AH, "BEGIN;\n\n");
     128            0 : }
     129              : 
     130              : /*
     131              :  * Called by the archiver when the dumper calls StartLO.
     132              :  *
     133              :  * Mandatory.
     134              :  *
     135              :  * Must save the passed OID for retrieval at restore-time.
     136              :  */
     137              : static void
     138            0 : _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
     139              : {
     140            0 :         bool            old_lo_style = (AH->version < K_VERS_1_12);
     141              : 
     142            0 :         if (oid == 0)
     143            0 :                 pg_fatal("invalid OID for large object");
     144              : 
     145              :         /* With an old archive we must do drop and create logic here */
     146            0 :         if (old_lo_style && AH->public.ropt->dropSchema)
     147            0 :                 DropLOIfExists(AH, oid);
     148              : 
     149            0 :         if (old_lo_style)
     150            0 :                 ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
     151            0 :                                  oid, INV_WRITE);
     152              :         else
     153            0 :                 ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n",
     154            0 :                                  oid, INV_WRITE);
     155              : 
     156            0 :         AH->WriteDataPtr = _WriteLOData;
     157            0 : }
     158              : 
     159              : /*
     160              :  * Called by the archiver when the dumper calls EndLO.
     161              :  *
     162              :  * Optional.
     163              :  */
     164              : static void
     165            0 : _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
     166              : {
     167            0 :         AH->WriteDataPtr = _WriteData;
     168              : 
     169            0 :         ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n");
     170            0 : }
     171              : 
     172              : /*
     173              :  * Called by the archiver when finishing saving BLOB DATA.
     174              :  *
     175              :  * Optional.
     176              :  */
     177              : static void
     178            0 : _EndLOs(ArchiveHandle *AH, TocEntry *te)
     179              : {
     180            0 :         ahprintf(AH, "COMMIT;\n\n");
     181            0 : }
     182              : 
     183              : /*------
     184              :  * Called as part of a RestoreArchive call; for the NULL archive, this
     185              :  * just sends the data for a given TOC entry to the output.
     186              :  *------
     187              :  */
     188              : static void
     189            0 : _PrintTocData(ArchiveHandle *AH, TocEntry *te)
     190              : {
     191            0 :         if (te->dataDumper)
     192              :         {
     193            0 :                 AH->currToc = te;
     194              : 
     195            0 :                 if (strcmp(te->desc, "BLOBS") == 0)
     196            0 :                         _StartLOs(AH, te);
     197              : 
     198            0 :                 te->dataDumper((Archive *) AH, te->dataDumperArg);
     199              : 
     200            0 :                 if (strcmp(te->desc, "BLOBS") == 0)
     201            0 :                         _EndLOs(AH, te);
     202              : 
     203            0 :                 AH->currToc = NULL;
     204            0 :         }
     205            0 : }
     206              : 
     207              : static int
     208            0 : _WriteByte(ArchiveHandle *AH, const int i)
     209              : {
     210              :         /* Don't do anything */
     211            0 :         return 0;
     212              : }
     213              : 
     214              : static void
     215            0 : _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
     216              : {
     217              :         /* Don't do anything */
     218            0 : }
     219              : 
     220              : static void
     221            0 : _CloseArchive(ArchiveHandle *AH)
     222              : {
     223              :         /* Nothing to do */
     224            0 : }
        

Generated by: LCOV version 2.3.2-1