LCOV - code coverage report
Current view: top level - src/bin/pg_dump - pg_backup_null.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 58 69 84.1 %
Date: 2019-11-15 23:07:02 Functions: 9 12 75.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 _WriteBlobData(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 _StartBlobs(ArchiveHandle *AH, TocEntry *te);
      39             : static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
      40             : static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid);
      41             : static void _EndBlobs(ArchiveHandle *AH, TocEntry *te);
      42             : 
      43             : 
      44             : /*
      45             :  *  Initializer
      46             :  */
      47             : void
      48         134 : InitArchiveFmt_Null(ArchiveHandle *AH)
      49             : {
      50             :     /* Assuming static functions, this can be copied for each format. */
      51         134 :     AH->WriteDataPtr = _WriteData;
      52         134 :     AH->EndDataPtr = _EndData;
      53         134 :     AH->WriteBytePtr = _WriteByte;
      54         134 :     AH->WriteBufPtr = _WriteBuf;
      55         134 :     AH->ClosePtr = _CloseArchive;
      56         134 :     AH->ReopenPtr = NULL;
      57         134 :     AH->PrintTocDataPtr = _PrintTocData;
      58             : 
      59         134 :     AH->StartBlobsPtr = _StartBlobs;
      60         134 :     AH->StartBlobPtr = _StartBlob;
      61         134 :     AH->EndBlobPtr = _EndBlob;
      62         134 :     AH->EndBlobsPtr = _EndBlobs;
      63         134 :     AH->ClonePtr = NULL;
      64         134 :     AH->DeClonePtr = NULL;
      65             : 
      66             :     /* Initialize LO buffering */
      67         134 :     AH->lo_buf_size = LOBBUFSIZE;
      68         134 :     AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
      69             : 
      70             :     /*
      71             :      * Now prevent reading...
      72             :      */
      73         134 :     if (AH->mode == archModeRead)
      74           0 :         fatal("this format cannot be read");
      75         134 : }
      76             : 
      77             : /*
      78             :  * - Start a new TOC entry
      79             :  */
      80             : 
      81             : /*
      82             :  * Called by dumper via archiver from within a data dump routine
      83             :  */
      84             : static void
      85     1455742 : _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
      86             : {
      87             :     /* Just send it to output, ahwrite() already errors on failure */
      88     1455742 :     ahwrite(data, 1, dLen, AH);
      89     1455742 :     return;
      90             : }
      91             : 
      92             : /*
      93             :  * Called by dumper via archiver from within a data dump routine
      94             :  * We substitute this for _WriteData while emitting a BLOB
      95             :  */
      96             : static void
      97          80 : _WriteBlobData(ArchiveHandle *AH, const void *data, size_t dLen)
      98             : {
      99          80 :     if (dLen > 0)
     100             :     {
     101          38 :         PQExpBuffer buf = createPQExpBuffer();
     102             : 
     103          38 :         appendByteaLiteralAHX(buf,
     104             :                               (const unsigned char *) data,
     105             :                               dLen,
     106             :                               AH);
     107             : 
     108          38 :         ahprintf(AH, "SELECT pg_catalog.lowrite(0, %s);\n", buf->data);
     109             : 
     110          38 :         destroyPQExpBuffer(buf);
     111             :     }
     112          80 :     return;
     113             : }
     114             : 
     115             : static void
     116           0 : _EndData(ArchiveHandle *AH, TocEntry *te)
     117             : {
     118           0 :     ahprintf(AH, "\n\n");
     119           0 : }
     120             : 
     121             : /*
     122             :  * Called by the archiver when starting to save all BLOB DATA (not schema).
     123             :  * This routine should save whatever format-specific information is needed
     124             :  * to read the BLOBs back into memory.
     125             :  *
     126             :  * It is called just prior to the dumper's DataDumper routine.
     127             :  *
     128             :  * Optional, but strongly recommended.
     129             :  */
     130             : static void
     131          34 : _StartBlobs(ArchiveHandle *AH, TocEntry *te)
     132             : {
     133          34 :     ahprintf(AH, "BEGIN;\n\n");
     134          34 : }
     135             : 
     136             : /*
     137             :  * Called by the archiver when the dumper calls StartBlob.
     138             :  *
     139             :  * Mandatory.
     140             :  *
     141             :  * Must save the passed OID for retrieval at restore-time.
     142             :  */
     143             : static void
     144          42 : _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
     145             : {
     146          42 :     bool        old_blob_style = (AH->version < K_VERS_1_12);
     147             : 
     148          42 :     if (oid == 0)
     149           0 :         fatal("invalid OID for large object");
     150             : 
     151             :     /* With an old archive we must do drop and create logic here */
     152          42 :     if (old_blob_style && AH->public.ropt->dropSchema)
     153           0 :         DropBlobIfExists(AH, oid);
     154             : 
     155          42 :     if (old_blob_style)
     156           0 :         ahprintf(AH, "SELECT pg_catalog.lo_open(pg_catalog.lo_create('%u'), %d);\n",
     157             :                  oid, INV_WRITE);
     158             :     else
     159          42 :         ahprintf(AH, "SELECT pg_catalog.lo_open('%u', %d);\n",
     160             :                  oid, INV_WRITE);
     161             : 
     162          42 :     AH->WriteDataPtr = _WriteBlobData;
     163          42 : }
     164             : 
     165             : /*
     166             :  * Called by the archiver when the dumper calls EndBlob.
     167             :  *
     168             :  * Optional.
     169             :  */
     170             : static void
     171          42 : _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
     172             : {
     173          42 :     AH->WriteDataPtr = _WriteData;
     174             : 
     175          42 :     ahprintf(AH, "SELECT pg_catalog.lo_close(0);\n\n");
     176          42 : }
     177             : 
     178             : /*
     179             :  * Called by the archiver when finishing saving all BLOB DATA.
     180             :  *
     181             :  * Optional.
     182             :  */
     183             : static void
     184          34 : _EndBlobs(ArchiveHandle *AH, TocEntry *te)
     185             : {
     186          34 :     ahprintf(AH, "COMMIT;\n\n");
     187          34 : }
     188             : 
     189             : /*------
     190             :  * Called as part of a RestoreArchive call; for the NULL archive, this
     191             :  * just sends the data for a given TOC entry to the output.
     192             :  *------
     193             :  */
     194             : static void
     195        2372 : _PrintTocData(ArchiveHandle *AH, TocEntry *te)
     196             : {
     197        2372 :     if (te->dataDumper)
     198             :     {
     199        2372 :         AH->currToc = te;
     200             : 
     201        2372 :         if (strcmp(te->desc, "BLOBS") == 0)
     202          34 :             _StartBlobs(AH, te);
     203             : 
     204        2372 :         te->dataDumper((Archive *) AH, te->dataDumperArg);
     205             : 
     206        2372 :         if (strcmp(te->desc, "BLOBS") == 0)
     207          34 :             _EndBlobs(AH, te);
     208             : 
     209        2372 :         AH->currToc = NULL;
     210             :     }
     211        2372 : }
     212             : 
     213             : static int
     214           0 : _WriteByte(ArchiveHandle *AH, const int i)
     215             : {
     216             :     /* Don't do anything */
     217           0 :     return 0;
     218             : }
     219             : 
     220             : static void
     221           0 : _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
     222             : {
     223             :     /* Don't do anything */
     224           0 :     return;
     225             : }
     226             : 
     227             : static void
     228         122 : _CloseArchive(ArchiveHandle *AH)
     229             : {
     230             :     /* Nothing to do */
     231         122 : }

Generated by: LCOV version 1.13