PostgreSQL Source Code  git master
pg_backup_tar.c File Reference
#include "postgres_fe.h"
#include <sys/stat.h>
#include <ctype.h>
#include <limits.h>
#include <unistd.h>
#include "common/file_utils.h"
#include "fe_utils/string_utils.h"
#include "pg_backup_archiver.h"
#include "pg_backup_tar.h"
#include "pg_backup_utils.h"
#include "pgtar.h"
Include dependency graph for pg_backup_tar.c:

Go to the source code of this file.

Data Structures

struct  TAR_MEMBER
 
struct  lclContext
 
struct  lclTocEntry
 

Macros

#define K_STD_BUF_SIZE   1024
 

Functions

static void _ArchiveEntry (ArchiveHandle *AH, TocEntry *te)
 
static void _StartData (ArchiveHandle *AH, TocEntry *te)
 
static void _WriteData (ArchiveHandle *AH, const void *data, size_t dLen)
 
static void _EndData (ArchiveHandle *AH, TocEntry *te)
 
static int _WriteByte (ArchiveHandle *AH, const int i)
 
static int _ReadByte (ArchiveHandle *)
 
static void _WriteBuf (ArchiveHandle *AH, const void *buf, size_t len)
 
static void _ReadBuf (ArchiveHandle *AH, void *buf, size_t len)
 
static void _CloseArchive (ArchiveHandle *AH)
 
static void _PrintTocData (ArchiveHandle *AH, TocEntry *te)
 
static void _WriteExtraToc (ArchiveHandle *AH, TocEntry *te)
 
static void _ReadExtraToc (ArchiveHandle *AH, TocEntry *te)
 
static void _PrintExtraToc (ArchiveHandle *AH, TocEntry *te)
 
static void _StartBlobs (ArchiveHandle *AH, TocEntry *te)
 
static void _StartBlob (ArchiveHandle *AH, TocEntry *te, Oid oid)
 
static void _EndBlob (ArchiveHandle *AH, TocEntry *te, Oid oid)
 
static void _EndBlobs (ArchiveHandle *AH, TocEntry *te)
 
static void _LoadBlobs (ArchiveHandle *AH)
 
static TAR_MEMBERtarOpen (ArchiveHandle *AH, const char *filename, char mode)
 
static void tarClose (ArchiveHandle *AH, TAR_MEMBER *TH)
 
static int tarPrintf (TAR_MEMBER *th, const char *fmt,...) pg_attribute_printf(2
 
static int static void _tarAddFile (ArchiveHandle *AH, TAR_MEMBER *th)
 
static TAR_MEMBER_tarPositionTo (ArchiveHandle *AH, const char *filename)
 
static size_t tarRead (void *buf, size_t len, TAR_MEMBER *th)
 
static size_t tarWrite (const void *buf, size_t len, TAR_MEMBER *th)
 
static void _tarWriteHeader (TAR_MEMBER *th)
 
static int _tarGetHeader (ArchiveHandle *AH, TAR_MEMBER *th)
 
static size_t _tarReadRaw (ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)
 
static size_t _scriptOut (ArchiveHandle *AH, const void *buf, size_t len)
 
void InitArchiveFmt_Tar (ArchiveHandle *AH)
 
static void _PrintFileData (ArchiveHandle *AH, char *filename)
 
bool isValidTarHeader (char *header)
 

Macro Definition Documentation

◆ K_STD_BUF_SIZE

#define K_STD_BUF_SIZE   1024

Definition at line 63 of file pg_backup_tar.c.

Referenced by _ArchiveEntry(), and _StartBlobs().

Function Documentation

◆ _ArchiveEntry()

static void _ArchiveEntry ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 243 of file pg_backup_tar.c.

References _archiveHandle::compression, _tocEntry::dataDumper, _tocEntry::dumpId, lclTocEntry::filename, fn(), _tocEntry::formatData, K_STD_BUF_SIZE, pg_malloc0(), pg_strdup(), sprintf, and lclTocEntry::TH.

Referenced by InitArchiveFmt_Tar().

244 {
245  lclTocEntry *ctx;
246  char fn[K_STD_BUF_SIZE];
247 
248  ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
249  if (te->dataDumper != NULL)
250  {
251 #ifdef HAVE_LIBZ
252  if (AH->compression == 0)
253  sprintf(fn, "%d.dat", te->dumpId);
254  else
255  sprintf(fn, "%d.dat.gz", te->dumpId);
256 #else
257  sprintf(fn, "%d.dat", te->dumpId);
258 #endif
259  ctx->filename = pg_strdup(fn);
260  }
261  else
262  {
263  ctx->filename = NULL;
264  ctx->TH = NULL;
265  }
266  te->formatData = (void *) ctx;
267 }
DataDumperPtr dataDumper
#define sprintf
Definition: port.h:218
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static void * fn(void *arg)
#define K_STD_BUF_SIZE
Definition: pg_backup_tar.c:63
TAR_MEMBER * TH
Definition: pg_backup_tar.c:98

◆ _CloseArchive()

static void _CloseArchive ( ArchiveHandle AH)
static

Definition at line 816 of file pg_backup_tar.c.

References _scriptOut(), archModeWrite, _archiveHandle::CustomOutPtr, Archive::dopt, lclContext::FH, _archiveHandle::FH, _archiveHandle::formatData, fsync_fname(), i, lclContext::isSpecialScript, _archiveHandle::mode, NewRestoreOptions(), _archiveHandle::public, RestoreArchive(), Archive::ropt, lclContext::scriptTH, SetArchiveOptions(), TAR_BLOCK_SIZE, tarClose(), lclContext::tarFH, tarOpen(), tarPrintf(), Archive::verbose, WRITE_ERROR_EXIT, WriteDataChunks(), WriteHead(), and WriteToc().

Referenced by InitArchiveFmt_Tar().

817 {
818  lclContext *ctx = (lclContext *) AH->formatData;
819  TAR_MEMBER *th;
820  RestoreOptions *ropt;
821  RestoreOptions *savRopt;
822  DumpOptions *savDopt;
823  int savVerbose,
824  i;
825 
826  if (AH->mode == archModeWrite)
827  {
828  /*
829  * Write the Header & TOC to the archive FIRST
830  */
831  th = tarOpen(AH, "toc.dat", 'w');
832  ctx->FH = th;
833  WriteHead(AH);
834  WriteToc(AH);
835  tarClose(AH, th); /* Not needed any more */
836 
837  /*
838  * Now send the data (tables & blobs)
839  */
840  WriteDataChunks(AH, NULL);
841 
842  /*
843  * Now this format wants to append a script which does a full restore
844  * if the files have been extracted.
845  */
846  th = tarOpen(AH, "restore.sql", 'w');
847 
848  tarPrintf(th, "--\n"
849  "-- NOTE:\n"
850  "--\n"
851  "-- File paths need to be edited. Search for $$PATH$$ and\n"
852  "-- replace it with the path to the directory containing\n"
853  "-- the extracted data files.\n"
854  "--\n");
855 
856  AH->CustomOutPtr = _scriptOut;
857 
858  ctx->isSpecialScript = 1;
859  ctx->scriptTH = th;
860 
861  ropt = NewRestoreOptions();
862  memcpy(ropt, AH->public.ropt, sizeof(RestoreOptions));
863  ropt->filename = NULL;
864  ropt->dropSchema = 1;
865  ropt->compression = 0;
866  ropt->superuser = NULL;
867  ropt->suppressDumpWarnings = true;
868 
869  savDopt = AH->public.dopt;
870  savRopt = AH->public.ropt;
871 
872  SetArchiveOptions((Archive *) AH, NULL, ropt);
873 
874  savVerbose = AH->public.verbose;
875  AH->public.verbose = 0;
876 
877  RestoreArchive((Archive *) AH);
878 
879  SetArchiveOptions((Archive *) AH, savDopt, savRopt);
880 
881  AH->public.verbose = savVerbose;
882 
883  tarClose(AH, th);
884 
885  ctx->isSpecialScript = 0;
886 
887  /*
888  * EOF marker for tar files is two blocks of NULLs.
889  */
890  for (i = 0; i < TAR_BLOCK_SIZE * 2; i++)
891  {
892  if (fputc(0, ctx->tarFH) == EOF)
894  }
895 
896  /* Sync the output file if one is defined */
897  if (AH->dosync && AH->fSpec)
898  (void) fsync_fname(AH->fSpec, false);
899  }
900 
901  AH->FH = NULL;
902 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
TAR_MEMBER * scriptTH
Definition: pg_backup_tar.c:93
RestoreOptions * ropt
Definition: pg_backup.h:192
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
void WriteDataChunks(ArchiveHandle *AH, ParallelState *pstate)
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:666
void WriteToc(ArchiveHandle *AH)
RestoreOptions * NewRestoreOptions(void)
int isSpecialScript
Definition: pg_backup_tar.c:92
DumpOptions * dopt
Definition: pg_backup.h:191
FILE * tarFH
Definition: pg_backup_tar.c:88
#define TAR_BLOCK_SIZE
Definition: pgtar.h:17
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
int verbose
Definition: pg_backup.h:194
static size_t _scriptOut(ArchiveHandle *AH, const void *buf, size_t len)
static int tarPrintf(TAR_MEMBER *th, const char *fmt,...) pg_attribute_printf(2
CustomOutPtrType CustomOutPtr
void WriteHead(ArchiveHandle *AH)
#define WRITE_ERROR_EXIT
void RestoreArchive(Archive *AH)
int i
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)

◆ _EndBlob()

static void _EndBlob ( ArchiveHandle AH,
TocEntry te,
Oid  oid 
)
static

Definition at line 973 of file pg_backup_tar.c.

References _tocEntry::formatData, tarClose(), and lclTocEntry::TH.

Referenced by InitArchiveFmt_Tar().

974 {
975  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
976 
977  tarClose(AH, tctx->TH);
978 }
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
TAR_MEMBER * TH
Definition: pg_backup_tar.c:98

◆ _EndBlobs()

static void _EndBlobs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 987 of file pg_backup_tar.c.

References lclContext::blobToc, _archiveHandle::formatData, and tarClose().

Referenced by InitArchiveFmt_Tar().

988 {
989  lclContext *ctx = (lclContext *) AH->formatData;
990 
991  /* Write out a fake zero OID to mark end-of-blobs. */
992  /* WriteInt(AH, 0); */
993 
994  tarClose(AH, ctx->blobToc);
995 }
TAR_MEMBER * blobToc
Definition: pg_backup_tar.c:87
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)

◆ _EndData()

static void _EndData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 615 of file pg_backup_tar.c.

References _tocEntry::formatData, tarClose(), and lclTocEntry::TH.

Referenced by InitArchiveFmt_Tar().

616 {
617  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
618 
619  /* Close the file */
620  tarClose(AH, tctx->TH);
621  tctx->TH = NULL;
622 }
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
TAR_MEMBER * TH
Definition: pg_backup_tar.c:98

◆ _LoadBlobs()

static void _LoadBlobs ( ArchiveHandle AH)
static

Definition at line 709 of file pg_backup_tar.c.

References ahwrite(), atooid, buf, _restoreOptions::dropSchema, EndRestoreBlob(), EndRestoreBlobs(), lclContext::FH, _archiveHandle::formatData, pg_log_info, _archiveHandle::public, Archive::ropt, StartRestoreBlob(), StartRestoreBlobs(), tarClose(), TAR_MEMBER::targetFile, tarOpen(), and tarRead().

Referenced by _PrintTocData().

710 {
711  Oid oid;
712  lclContext *ctx = (lclContext *) AH->formatData;
713  TAR_MEMBER *th;
714  size_t cnt;
715  bool foundBlob = false;
716  char buf[4096];
717 
718  StartRestoreBlobs(AH);
719 
720  th = tarOpen(AH, NULL, 'r'); /* Open next file */
721  while (th != NULL)
722  {
723  ctx->FH = th;
724 
725  if (strncmp(th->targetFile, "blob_", 5) == 0)
726  {
727  oid = atooid(&th->targetFile[5]);
728  if (oid != 0)
729  {
730  pg_log_info("restoring large object with OID %u", oid);
731 
732  StartRestoreBlob(AH, oid, AH->public.ropt->dropSchema);
733 
734  while ((cnt = tarRead(buf, 4095, th)) > 0)
735  {
736  buf[cnt] = '\0';
737  ahwrite(buf, 1, cnt, AH);
738  }
739  EndRestoreBlob(AH, oid);
740  foundBlob = true;
741  }
742  tarClose(AH, th);
743  }
744  else
745  {
746  tarClose(AH, th);
747 
748  /*
749  * Once we have found the first blob, stop at the first non-blob
750  * entry (which will be 'blobs.toc'). This coding would eat all
751  * the rest of the archive if there are no blobs ... but this
752  * function shouldn't be called at all in that case.
753  */
754  if (foundBlob)
755  break;
756  }
757 
758  th = tarOpen(AH, NULL, 'r');
759  }
760  EndRestoreBlobs(AH);
761 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
RestoreOptions * ropt
Definition: pg_backup.h:192
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
unsigned int Oid
Definition: postgres_ext.h:31
void StartRestoreBlobs(ArchiveHandle *AH)
void StartRestoreBlob(ArchiveHandle *AH, Oid oid, bool drop)
static char * buf
Definition: pg_test_fsync.c:68
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
#define atooid(x)
Definition: postgres_ext.h:42
void EndRestoreBlobs(ArchiveHandle *AH)
void EndRestoreBlob(ArchiveHandle *AH, Oid oid)
static size_t tarRead(void *buf, size_t len, TAR_MEMBER *th)
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
#define pg_log_info(...)
Definition: logging.h:88

◆ _PrintExtraToc()

static void _PrintExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 301 of file pg_backup_tar.c.

References ahprintf(), lclTocEntry::filename, _tocEntry::formatData, _archiveHandle::public, and Archive::verbose.

Referenced by InitArchiveFmt_Tar().

302 {
303  lclTocEntry *ctx = (lclTocEntry *) te->formatData;
304 
305  if (AH->public.verbose && ctx->filename != NULL)
306  ahprintf(AH, "-- File: %s\n", ctx->filename);
307 }
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
int verbose
Definition: pg_backup.h:194

◆ _PrintFileData()

static void _PrintFileData ( ArchiveHandle AH,
char *  filename 
)
static

Definition at line 628 of file pg_backup_tar.c.

References ahwrite(), buf, lclContext::FH, _archiveHandle::formatData, tarClose(), tarOpen(), and tarRead().

Referenced by _PrintTocData().

629 {
630  lclContext *ctx = (lclContext *) AH->formatData;
631  char buf[4096];
632  size_t cnt;
633  TAR_MEMBER *th;
634 
635  if (!filename)
636  return;
637 
638  th = tarOpen(AH, filename, 'r');
639  ctx->FH = th;
640 
641  while ((cnt = tarRead(buf, 4095, th)) > 0)
642  {
643  buf[cnt] = '\0';
644  ahwrite(buf, 1, cnt, AH);
645  }
646 
647  tarClose(AH, th);
648 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
static char * buf
Definition: pg_test_fsync.c:68
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
static char * filename
Definition: pg_dumpall.c:92
static size_t tarRead(void *buf, size_t len, TAR_MEMBER *th)
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)

◆ _PrintTocData()

static void _PrintTocData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 655 of file pg_backup_tar.c.

References _LoadBlobs(), _PrintFileData(), ahprintf(), ahwrite(), _tocEntry::copyStmt, _tocEntry::desc, fatal, _archiveHandle::formatData, _tocEntry::formatData, and lclContext::isSpecialScript.

Referenced by InitArchiveFmt_Tar().

656 {
657  lclContext *ctx = (lclContext *) AH->formatData;
658  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
659  int pos1;
660 
661  if (!tctx->filename)
662  return;
663 
664  /*
665  * If we're writing the special restore.sql script, emit a suitable
666  * command to include each table's data from the corresponding file.
667  *
668  * In the COPY case this is a bit klugy because the regular COPY command
669  * was already printed before we get control.
670  */
671  if (ctx->isSpecialScript)
672  {
673  if (te->copyStmt)
674  {
675  /* Abort the COPY FROM stdin */
676  ahprintf(AH, "\\.\n");
677 
678  /*
679  * The COPY statement should look like "COPY ... FROM stdin;\n",
680  * see dumpTableData().
681  */
682  pos1 = (int) strlen(te->copyStmt) - 13;
683  if (pos1 < 6 || strncmp(te->copyStmt, "COPY ", 5) != 0 ||
684  strcmp(te->copyStmt + pos1, " FROM stdin;\n") != 0)
685  fatal("unexpected COPY statement syntax: \"%s\"",
686  te->copyStmt);
687 
688  /* Emit all but the FROM part ... */
689  ahwrite(te->copyStmt, 1, pos1, AH);
690  /* ... and insert modified FROM */
691  ahprintf(AH, " FROM '$$PATH$$/%s';\n\n", tctx->filename);
692  }
693  else
694  {
695  /* --inserts mode, no worries, just include the data file */
696  ahprintf(AH, "\\i $$PATH$$/%s\n\n", tctx->filename);
697  }
698 
699  return;
700  }
701 
702  if (strcmp(te->desc, "BLOBS") == 0)
703  _LoadBlobs(AH);
704  else
705  _PrintFileData(AH, tctx->filename);
706 }
static void _LoadBlobs(ArchiveHandle *AH)
static void _PrintFileData(ArchiveHandle *AH, char *filename)
int isSpecialScript
Definition: pg_backup_tar.c:92
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
#define fatal(...)
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)

◆ _ReadBuf()

static void _ReadBuf ( ArchiveHandle AH,
void *  buf,
size_t  len 
)
static

Definition at line 804 of file pg_backup_tar.c.

References fatal, lclContext::FH, lclContext::filePos, _archiveHandle::formatData, and tarRead().

Referenced by InitArchiveFmt_Tar().

805 {
806  lclContext *ctx = (lclContext *) AH->formatData;
807 
808  if (tarRead(buf, len, ctx->FH) != len)
809  /* We already would have exited for errors on reads, must be EOF */
810  fatal("could not read from input file: end of file");
811 
812  ctx->filePos += len;
813 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
static char * buf
Definition: pg_test_fsync.c:68
#define fatal(...)
static size_t tarRead(void *buf, size_t len, TAR_MEMBER *th)
pgoff_t filePos
Definition: pg_backup_tar.c:86

◆ _ReadByte()

static int _ReadByte ( ArchiveHandle AH)
static

Definition at line 778 of file pg_backup_tar.c.

References fatal, lclContext::FH, lclContext::filePos, _archiveHandle::formatData, and tarRead().

Referenced by InitArchiveFmt_Tar().

779 {
780  lclContext *ctx = (lclContext *) AH->formatData;
781  size_t res;
782  unsigned char c;
783 
784  res = tarRead(&c, 1, ctx->FH);
785  if (res != 1)
786  /* We already would have exited for errors on reads, must be EOF */
787  fatal("could not read from input file: end of file");
788  ctx->filePos += 1;
789  return c;
790 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
char * c
#define fatal(...)
static size_t tarRead(void *buf, size_t len, TAR_MEMBER *th)
pgoff_t filePos
Definition: pg_backup_tar.c:86

◆ _ReadExtraToc()

static void _ReadExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 281 of file pg_backup_tar.c.

References lclTocEntry::filename, _tocEntry::formatData, free, pg_malloc0(), ReadStr(), and lclTocEntry::TH.

Referenced by InitArchiveFmt_Tar().

282 {
283  lclTocEntry *ctx = (lclTocEntry *) te->formatData;
284 
285  if (ctx == NULL)
286  {
287  ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
288  te->formatData = (void *) ctx;
289  }
290 
291  ctx->filename = ReadStr(AH);
292  if (strlen(ctx->filename) == 0)
293  {
294  free(ctx->filename);
295  ctx->filename = NULL;
296  }
297  ctx->TH = NULL;
298 }
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define free(a)
Definition: header.h:65
char * ReadStr(ArchiveHandle *AH)
TAR_MEMBER * TH
Definition: pg_backup_tar.c:98

◆ _scriptOut()

static size_t _scriptOut ( ArchiveHandle AH,
const void *  buf,
size_t  len 
)
static

Definition at line 905 of file pg_backup_tar.c.

References _archiveHandle::formatData, lclContext::scriptTH, and tarWrite().

Referenced by _CloseArchive().

906 {
907  lclContext *ctx = (lclContext *) AH->formatData;
908 
909  return tarWrite(buf, len, ctx->scriptTH);
910 }
TAR_MEMBER * scriptTH
Definition: pg_backup_tar.c:93
static char * buf
Definition: pg_test_fsync.c:68
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th)

◆ _StartBlob()

static void _StartBlob ( ArchiveHandle AH,
TocEntry te,
Oid  oid 
)
static

Definition at line 944 of file pg_backup_tar.c.

References lclContext::blobToc, _archiveHandle::compression, fatal, _archiveHandle::formatData, _tocEntry::formatData, sprintf, tarOpen(), and tarPrintf().

Referenced by InitArchiveFmt_Tar().

945 {
946  lclContext *ctx = (lclContext *) AH->formatData;
947  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
948  char fname[255];
949  char *sfx;
950 
951  if (oid == 0)
952  fatal("invalid OID for large object (%u)", oid);
953 
954  if (AH->compression != 0)
955  sfx = ".gz";
956  else
957  sfx = "";
958 
959  sprintf(fname, "blob_%u.dat%s", oid, sfx);
960 
961  tarPrintf(ctx->blobToc, "%u %s\n", oid, fname);
962 
963  tctx->TH = tarOpen(AH, fname, 'w');
964 }
TAR_MEMBER * blobToc
Definition: pg_backup_tar.c:87
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
#define sprintf
Definition: port.h:218
static int tarPrintf(TAR_MEMBER *th, const char *fmt,...) pg_attribute_printf(2
#define fatal(...)

◆ _StartBlobs()

static void _StartBlobs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 927 of file pg_backup_tar.c.

References lclContext::blobToc, _archiveHandle::formatData, K_STD_BUF_SIZE, sprintf, and tarOpen().

Referenced by InitArchiveFmt_Tar().

928 {
929  lclContext *ctx = (lclContext *) AH->formatData;
930  char fname[K_STD_BUF_SIZE];
931 
932  sprintf(fname, "blobs.toc");
933  ctx->blobToc = tarOpen(AH, fname, 'w');
934 }
TAR_MEMBER * blobToc
Definition: pg_backup_tar.c:87
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
#define sprintf
Definition: port.h:218
#define K_STD_BUF_SIZE
Definition: pg_backup_tar.c:63

◆ _StartData()

static void _StartData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 310 of file pg_backup_tar.c.

References lclTocEntry::filename, _tocEntry::formatData, tarOpen(), and lclTocEntry::TH.

Referenced by InitArchiveFmt_Tar().

311 {
312  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
313 
314  tctx->TH = tarOpen(AH, tctx->filename, 'w');
315 }
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
TAR_MEMBER * TH
Definition: pg_backup_tar.c:98

◆ _tarAddFile()

static void _tarAddFile ( ArchiveHandle AH,
TAR_MEMBER th 
)
static

Definition at line 1065 of file pg_backup_tar.c.

References _tarWriteHeader(), buf, fatal, TAR_MEMBER::fileLen, _archiveHandle::formatData, fseeko, ftello, i, INT64_FORMAT, pgoff_t, READ_ERROR_EXIT, snprintf, TAR_MEMBER::tarFH, lclContext::tarFHpos, tarPaddingBytesRequired(), TAR_MEMBER::tmpFH, and WRITE_ERROR_EXIT.

Referenced by tarClose().

1066 {
1067  lclContext *ctx = (lclContext *) AH->formatData;
1068  FILE *tmp = th->tmpFH; /* Grab it for convenience */
1069  char buf[32768];
1070  size_t cnt;
1071  pgoff_t len = 0;
1072  size_t res;
1073  size_t i,
1074  pad;
1075 
1076  /*
1077  * Find file len & go back to start.
1078  */
1079  if (fseeko(tmp, 0, SEEK_END) != 0)
1080  fatal("error during file seek: %m");
1081  th->fileLen = ftello(tmp);
1082  if (th->fileLen < 0)
1083  fatal("could not determine seek position in archive file: %m");
1084  if (fseeko(tmp, 0, SEEK_SET) != 0)
1085  fatal("error during file seek: %m");
1086 
1087  _tarWriteHeader(th);
1088 
1089  while ((cnt = fread(buf, 1, sizeof(buf), tmp)) > 0)
1090  {
1091  if ((res = fwrite(buf, 1, cnt, th->tarFH)) != cnt)
1093  len += res;
1094  }
1095  if (!feof(tmp))
1096  READ_ERROR_EXIT(tmp);
1097 
1098  if (fclose(tmp) != 0) /* This *should* delete it... */
1099  fatal("could not close temporary file: %m");
1100 
1101  if (len != th->fileLen)
1102  {
1103  char buf1[32],
1104  buf2[32];
1105 
1106  snprintf(buf1, sizeof(buf1), INT64_FORMAT, (int64) len);
1107  snprintf(buf2, sizeof(buf2), INT64_FORMAT, (int64) th->fileLen);
1108  fatal("actual file length (%s) does not match expected (%s)",
1109  buf1, buf2);
1110  }
1111 
1112  pad = tarPaddingBytesRequired(len);
1113  for (i = 0; i < pad; i++)
1114  {
1115  if (fputc('\0', th->tarFH) == EOF)
1117  }
1118 
1119  ctx->tarFHpos += len + pad;
1120 }
pgoff_t tarFHpos
Definition: pg_backup_tar.c:89
#define pgoff_t
Definition: win32_port.h:200
#define ftello(stream)
Definition: win32_port.h:210
static char * buf
Definition: pg_test_fsync.c:68
static size_t tarPaddingBytesRequired(size_t len)
Definition: pgtar.h:40
#define fseeko(stream, offset, origin)
Definition: win32_port.h:207
static void _tarWriteHeader(TAR_MEMBER *th)
FILE * tmpFH
Definition: pg_backup_tar.c:75
pgoff_t fileLen
Definition: pg_backup_tar.c:79
#define fatal(...)
#define INT64_FORMAT
Definition: c.h:483
#define WRITE_ERROR_EXIT
int i
#define READ_ERROR_EXIT(fd)
FILE * tarFH
Definition: pg_backup_tar.c:74
#define snprintf
Definition: port.h:216

◆ _tarGetHeader()

static int _tarGetHeader ( ArchiveHandle AH,
TAR_MEMBER th 
)
static

Definition at line 1208 of file pg_backup_tar.c.

References _tarReadRaw(), fatal, TAR_MEMBER::fileLen, _archiveHandle::formatData, ftello, i, ngettext, pg_log_debug, pg_strdup(), pgoff_t, read_tar_number(), snprintf, strlcpy(), TAR_BLOCK_SIZE, tarChecksum(), lclContext::tarFH, lclContext::tarFHpos, TAR_MEMBER::targetFile, and UINT64_FORMAT.

Referenced by _tarPositionTo().

1209 {
1210  lclContext *ctx = (lclContext *) AH->formatData;
1211  char h[TAR_BLOCK_SIZE];
1212  char tag[100 + 1];
1213  int sum,
1214  chk;
1215  pgoff_t len;
1216  pgoff_t hPos;
1217  bool gotBlock = false;
1218 
1219  while (!gotBlock)
1220  {
1221  /* Save the pos for reporting purposes */
1222  hPos = ctx->tarFHpos;
1223 
1224  /* Read the next tar block, return EOF, exit if short */
1225  len = _tarReadRaw(AH, h, TAR_BLOCK_SIZE, NULL, ctx->tarFH);
1226  if (len == 0) /* EOF */
1227  return 0;
1228 
1229  if (len != TAR_BLOCK_SIZE)
1230  fatal(ngettext("incomplete tar header found (%lu byte)",
1231  "incomplete tar header found (%lu bytes)",
1232  len),
1233  (unsigned long) len);
1234 
1235  /* Calc checksum */
1236  chk = tarChecksum(h);
1237  sum = read_tar_number(&h[148], 8);
1238 
1239  /*
1240  * If the checksum failed, see if it is a null block. If so, silently
1241  * continue to the next block.
1242  */
1243  if (chk == sum)
1244  gotBlock = true;
1245  else
1246  {
1247  int i;
1248 
1249  for (i = 0; i < TAR_BLOCK_SIZE; i++)
1250  {
1251  if (h[i] != 0)
1252  {
1253  gotBlock = true;
1254  break;
1255  }
1256  }
1257  }
1258  }
1259 
1260  /* Name field is 100 bytes, might not be null-terminated */
1261  strlcpy(tag, &h[0], 100 + 1);
1262 
1263  len = read_tar_number(&h[124], 12);
1264 
1265  {
1266  char posbuf[32];
1267  char lenbuf[32];
1268 
1269  snprintf(posbuf, sizeof(posbuf), UINT64_FORMAT, (uint64) hPos);
1270  snprintf(lenbuf, sizeof(lenbuf), UINT64_FORMAT, (uint64) len);
1271  pg_log_debug("TOC Entry %s at %s (length %s, checksum %d)",
1272  tag, posbuf, lenbuf, sum);
1273  }
1274 
1275  if (chk != sum)
1276  {
1277  char posbuf[32];
1278 
1279  snprintf(posbuf, sizeof(posbuf), UINT64_FORMAT,
1280  (uint64) ftello(ctx->tarFH));
1281  fatal("corrupt tar header found in %s (expected %d, computed %d) file position %s",
1282  tag, sum, chk, posbuf);
1283  }
1284 
1285  th->targetFile = pg_strdup(tag);
1286  th->fileLen = len;
1287 
1288  return 1;
1289 }
uint64 read_tar_number(const char *s, int len)
Definition: tar.c:58
pgoff_t tarFHpos
Definition: pg_backup_tar.c:89
#define pgoff_t
Definition: win32_port.h:200
#define ftello(stream)
Definition: win32_port.h:210
#define pg_log_debug(...)
Definition: logging.h:92
FILE * tarFH
Definition: pg_backup_tar.c:88
#define TAR_BLOCK_SIZE
Definition: pgtar.h:17
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define ngettext(s, p, n)
Definition: c.h:1182
int tarChecksum(char *header)
Definition: tar.c:90
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
pgoff_t fileLen
Definition: pg_backup_tar.c:79
#define fatal(...)
int i
#define snprintf
Definition: port.h:216
#define UINT64_FORMAT
Definition: c.h:484
char * targetFile
Definition: pg_backup_tar.c:76
static size_t _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)

◆ _tarPositionTo()

static TAR_MEMBER * _tarPositionTo ( ArchiveHandle AH,
const char *  filename 
)
static

Definition at line 1124 of file pg_backup_tar.c.

References _tarGetHeader(), _tarReadRaw(), TAR_MEMBER::AH, buf, fatal, TAR_MEMBER::fileLen, _archiveHandle::formatData, free, header(), i, INT64_FORMAT, pg_log_debug, pg_malloc0(), TAR_MEMBER::pos, REQ_DATA, snprintf, TAR_BLOCK_SIZE, lclContext::tarFH, lclContext::tarFHpos, TAR_MEMBER::targetFile, lclContext::tarNextMember, tarPaddingBytesRequired(), and TocIDRequired().

Referenced by tarOpen().

1125 {
1126  lclContext *ctx = (lclContext *) AH->formatData;
1127  TAR_MEMBER *th = pg_malloc0(sizeof(TAR_MEMBER));
1128  char c;
1129  char header[TAR_BLOCK_SIZE];
1130  size_t i,
1131  len,
1132  blks;
1133  int id;
1134 
1135  th->AH = AH;
1136 
1137  /* Go to end of current file, if any */
1138  if (ctx->tarFHpos != 0)
1139  {
1140  char buf1[100],
1141  buf2[100];
1142 
1143  snprintf(buf1, sizeof(buf1), INT64_FORMAT, (int64) ctx->tarFHpos);
1144  snprintf(buf2, sizeof(buf2), INT64_FORMAT, (int64) ctx->tarNextMember);
1145  pg_log_debug("moving from position %s to next member at file position %s",
1146  buf1, buf2);
1147 
1148  while (ctx->tarFHpos < ctx->tarNextMember)
1149  _tarReadRaw(AH, &c, 1, NULL, ctx->tarFH);
1150  }
1151 
1152  {
1153  char buf[100];
1154 
1155  snprintf(buf, sizeof(buf), INT64_FORMAT, (int64) ctx->tarFHpos);
1156  pg_log_debug("now at file position %s", buf);
1157  }
1158 
1159  /* We are at the start of the file, or at the next member */
1160 
1161  /* Get the header */
1162  if (!_tarGetHeader(AH, th))
1163  {
1164  if (filename)
1165  fatal("could not find header for file \"%s\" in tar archive", filename);
1166  else
1167  {
1168  /*
1169  * We're just scanning the archive for the next file, so return
1170  * null
1171  */
1172  free(th);
1173  return NULL;
1174  }
1175  }
1176 
1177  while (filename != NULL && strcmp(th->targetFile, filename) != 0)
1178  {
1179  pg_log_debug("skipping tar member %s", th->targetFile);
1180 
1181  id = atoi(th->targetFile);
1182  if ((TocIDRequired(AH, id) & REQ_DATA) != 0)
1183  fatal("restoring data out of order is not supported in this archive format: "
1184  "\"%s\" is required, but comes before \"%s\" in the archive file.",
1185  th->targetFile, filename);
1186 
1187  /* Header doesn't match, so read to next header */
1188  len = th->fileLen;
1189  len += tarPaddingBytesRequired(th->fileLen);
1190  blks = len / TAR_BLOCK_SIZE; /* # of tar blocks */
1191 
1192  for (i = 0; i < blks; i++)
1193  _tarReadRaw(AH, &header[0], TAR_BLOCK_SIZE, NULL, ctx->tarFH);
1194 
1195  if (!_tarGetHeader(AH, th))
1196  fatal("could not find header for file \"%s\" in tar archive", filename);
1197  }
1198 
1199  ctx->tarNextMember = ctx->tarFHpos + th->fileLen
1200  + tarPaddingBytesRequired(th->fileLen);
1201  th->pos = 0;
1202 
1203  return th;
1204 }
#define REQ_DATA
pgoff_t tarFHpos
Definition: pg_backup_tar.c:89
pgoff_t tarNextMember
Definition: pg_backup_tar.c:90
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
#define pg_log_debug(...)
Definition: logging.h:92
FILE * tarFH
Definition: pg_backup_tar.c:88
#define TAR_BLOCK_SIZE
Definition: pgtar.h:17
char * c
static char * buf
Definition: pg_test_fsync.c:68
static size_t tarPaddingBytesRequired(size_t len)
Definition: pgtar.h:40
#define free(a)
Definition: header.h:65
#define fatal(...)
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210
#define INT64_FORMAT
Definition: c.h:483
static char * filename
Definition: pg_dumpall.c:92
int i
static int _tarGetHeader(ArchiveHandle *AH, TAR_MEMBER *th)
int TocIDRequired(ArchiveHandle *AH, DumpId id)
#define snprintf
Definition: port.h:216
static size_t _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)

◆ _tarReadRaw()

static size_t _tarReadRaw ( ArchiveHandle AH,
void *  buf,
size_t  len,
TAR_MEMBER th,
FILE *  fh 
)
static

Definition at line 505 of file pg_backup_tar.c.

References Assert, errmsg(), fatal, _archiveHandle::formatData, GZEOF, GZREAD, _archiveHandle::lookahead, _archiveHandle::lookaheadLen, _archiveHandle::lookaheadPos, TAR_MEMBER::nFH, READ_ERROR_EXIT, strerror, lclContext::tarFHpos, and TAR_MEMBER::zFH.

Referenced by _tarGetHeader(), _tarPositionTo(), tarClose(), and tarRead().

506 {
507  lclContext *ctx = (lclContext *) AH->formatData;
508  size_t avail;
509  size_t used = 0;
510  size_t res = 0;
511 
512  Assert(th || fh);
513 
514  avail = AH->lookaheadLen - AH->lookaheadPos;
515  if (avail > 0)
516  {
517  /* We have some lookahead bytes to use */
518  if (avail >= len) /* Just use the lookahead buffer */
519  used = len;
520  else
521  used = avail;
522 
523  /* Copy, and adjust buffer pos */
524  memcpy(buf, AH->lookahead + AH->lookaheadPos, used);
525  AH->lookaheadPos += used;
526 
527  /* Adjust required length */
528  len -= used;
529  }
530 
531  /* Read the file if len > 0 */
532  if (len > 0)
533  {
534  if (fh)
535  {
536  res = fread(&((char *) buf)[used], 1, len, fh);
537  if (res != len && !feof(fh))
538  READ_ERROR_EXIT(fh);
539  }
540  else if (th)
541  {
542  if (th->zFH)
543  {
544  res = GZREAD(&((char *) buf)[used], 1, len, th->zFH);
545  if (res != len && !GZEOF(th->zFH))
546  {
547 #ifdef HAVE_LIBZ
548  int errnum;
549  const char *errmsg = gzerror(th->zFH, &errnum);
550 
551  fatal("could not read from input file: %s",
552  errnum == Z_ERRNO ? strerror(errno) : errmsg);
553 #else
554  fatal("could not read from input file: %s",
555  strerror(errno));
556 #endif
557  }
558  }
559  else
560  {
561  res = fread(&((char *) buf)[used], 1, len, th->nFH);
562  if (res != len && !feof(th->nFH))
563  READ_ERROR_EXIT(th->nFH);
564  }
565  }
566  }
567 
568  ctx->tarFHpos += res + used;
569 
570  return (res + used);
571 }
FILE * zFH
Definition: pg_backup_tar.c:71
pgoff_t tarFHpos
Definition: pg_backup_tar.c:89
static char * buf
Definition: pg_test_fsync.c:68
#define GZEOF(fh)
FILE * nFH
Definition: pg_backup_tar.c:73
#define Assert(condition)
Definition: c.h:804
#define strerror
Definition: port.h:229
#define fatal(...)
#define GZREAD(p, s, n, fh)
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define READ_ERROR_EXIT(fd)

◆ _tarWriteHeader()

static void _tarWriteHeader ( TAR_MEMBER th)
static

Definition at line 1293 of file pg_backup_tar.c.

References TAR_MEMBER::fileLen, TAR_BLOCK_SIZE, tarCreateHeader(), TAR_MEMBER::tarFH, TAR_MEMBER::targetFile, and WRITE_ERROR_EXIT.

Referenced by _tarAddFile().

1294 {
1295  char h[TAR_BLOCK_SIZE];
1296 
1297  tarCreateHeader(h, th->targetFile, NULL, th->fileLen,
1298  0600, 04000, 02000, time(NULL));
1299 
1300  /* Now write the completed header. */
1301  if (fwrite(h, 1, TAR_BLOCK_SIZE, th->tarFH) != TAR_BLOCK_SIZE)
1303 }
#define TAR_BLOCK_SIZE
Definition: pgtar.h:17
enum tarError tarCreateHeader(char *h, const char *filename, const char *linktarget, pgoff_t size, mode_t mode, uid_t uid, gid_t gid, time_t mtime)
Definition: tar.c:114
pgoff_t fileLen
Definition: pg_backup_tar.c:79
#define WRITE_ERROR_EXIT
FILE * tarFH
Definition: pg_backup_tar.c:74
char * targetFile
Definition: pg_backup_tar.c:76

◆ _WriteBuf()

static void _WriteBuf ( ArchiveHandle AH,
const void *  buf,
size_t  len 
)
static

Definition at line 793 of file pg_backup_tar.c.

References lclContext::FH, lclContext::filePos, _archiveHandle::formatData, tarWrite(), and WRITE_ERROR_EXIT.

Referenced by InitArchiveFmt_Tar().

794 {
795  lclContext *ctx = (lclContext *) AH->formatData;
796 
797  if (tarWrite(buf, len, ctx->FH) != len)
799 
800  ctx->filePos += len;
801 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
static char * buf
Definition: pg_test_fsync.c:68
#define WRITE_ERROR_EXIT
pgoff_t filePos
Definition: pg_backup_tar.c:86
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th)

◆ _WriteByte()

static int _WriteByte ( ArchiveHandle AH,
const int  i 
)
static

Definition at line 765 of file pg_backup_tar.c.

References lclContext::FH, lclContext::filePos, _archiveHandle::formatData, tarWrite(), and WRITE_ERROR_EXIT.

Referenced by InitArchiveFmt_Tar().

766 {
767  lclContext *ctx = (lclContext *) AH->formatData;
768  char b = i; /* Avoid endian problems */
769 
770  if (tarWrite(&b, 1, ctx->FH) != 1)
772 
773  ctx->filePos += 1;
774  return 1;
775 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
#define WRITE_ERROR_EXIT
int i
pgoff_t filePos
Definition: pg_backup_tar.c:86
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th)

◆ _WriteData()

static void _WriteData ( ArchiveHandle AH,
const void *  data,
size_t  dLen 
)
static

Definition at line 606 of file pg_backup_tar.c.

References _archiveHandle::currToc, _tocEntry::formatData, tarWrite(), lclTocEntry::TH, and WRITE_ERROR_EXIT.

Referenced by InitArchiveFmt_Tar().

607 {
608  lclTocEntry *tctx = (lclTocEntry *) AH->currToc->formatData;
609 
610  if (tarWrite(data, dLen, tctx->TH) != dLen)
612 }
struct _tocEntry * currToc
#define WRITE_ERROR_EXIT
TAR_MEMBER * TH
Definition: pg_backup_tar.c:98
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th)

◆ _WriteExtraToc()

static void _WriteExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 270 of file pg_backup_tar.c.

References lclTocEntry::filename, _tocEntry::formatData, and WriteStr().

Referenced by InitArchiveFmt_Tar().

271 {
272  lclTocEntry *ctx = (lclTocEntry *) te->formatData;
273 
274  if (ctx->filename)
275  WriteStr(AH, ctx->filename);
276  else
277  WriteStr(AH, "");
278 }
size_t WriteStr(ArchiveHandle *AH, const char *c)

◆ InitArchiveFmt_Tar()

void InitArchiveFmt_Tar ( ArchiveHandle AH)

Definition at line 126 of file pg_backup_tar.c.

References _ArchiveEntry(), _CloseArchive(), _EndBlob(), _EndBlobs(), _EndData(), _PrintExtraToc(), _PrintTocData(), _ReadBuf(), _ReadByte(), _ReadExtraToc(), _StartBlob(), _StartBlobs(), _StartData(), _WriteBuf(), _WriteByte(), _WriteData(), _WriteExtraToc(), archModeWrite, checkSeek(), fatal, lclContext::FH, lclContext::filePos, lclContext::hasSeek, lclContext::isSpecialScript, LOBBUFSIZE, PG_BINARY_R, PG_BINARY_W, pg_malloc(), pg_malloc0(), ReadHead(), ReadToc(), generate_unaccent_rules::stdout, tarClose(), lclContext::tarFH, lclContext::tarFHpos, and tarOpen().

Referenced by _allocAH().

127 {
128  lclContext *ctx;
129 
130  /* Assuming static functions, this can be copied for each format. */
132  AH->StartDataPtr = _StartData;
133  AH->WriteDataPtr = _WriteData;
134  AH->EndDataPtr = _EndData;
135  AH->WriteBytePtr = _WriteByte;
136  AH->ReadBytePtr = _ReadByte;
137  AH->WriteBufPtr = _WriteBuf;
138  AH->ReadBufPtr = _ReadBuf;
139  AH->ClosePtr = _CloseArchive;
140  AH->ReopenPtr = NULL;
145 
147  AH->StartBlobPtr = _StartBlob;
148  AH->EndBlobPtr = _EndBlob;
149  AH->EndBlobsPtr = _EndBlobs;
150  AH->ClonePtr = NULL;
151  AH->DeClonePtr = NULL;
152 
153  AH->WorkerJobDumpPtr = NULL;
154  AH->WorkerJobRestorePtr = NULL;
155 
156  /*
157  * Set up some special context used in compressing data.
158  */
159  ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
160  AH->formatData = (void *) ctx;
161  ctx->filePos = 0;
162  ctx->isSpecialScript = 0;
163 
164  /* Initialize LO buffering */
165  AH->lo_buf_size = LOBBUFSIZE;
166  AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
167 
168  /*
169  * Now open the tar file, and load the TOC if we're in read mode.
170  */
171  if (AH->mode == archModeWrite)
172  {
173  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
174  {
175  ctx->tarFH = fopen(AH->fSpec, PG_BINARY_W);
176  if (ctx->tarFH == NULL)
177  fatal("could not open TOC file \"%s\" for output: %m",
178  AH->fSpec);
179  }
180  else
181  {
182  ctx->tarFH = stdout;
183  if (ctx->tarFH == NULL)
184  fatal("could not open TOC file for output: %m");
185  }
186 
187  ctx->tarFHpos = 0;
188 
189  /*
190  * Make unbuffered since we will dup() it, and the buffers screw each
191  * other
192  */
193  /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */
194 
195  ctx->hasSeek = checkSeek(ctx->tarFH);
196 
197  /*
198  * We don't support compression because reading the files back is not
199  * possible since gzdopen uses buffered IO which totally screws file
200  * positioning.
201  */
202  if (AH->compression != 0)
203  fatal("compression is not supported by tar archive format");
204  }
205  else
206  { /* Read Mode */
207  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
208  {
209  ctx->tarFH = fopen(AH->fSpec, PG_BINARY_R);
210  if (ctx->tarFH == NULL)
211  fatal("could not open TOC file \"%s\" for input: %m",
212  AH->fSpec);
213  }
214  else
215  {
216  ctx->tarFH = stdin;
217  if (ctx->tarFH == NULL)
218  fatal("could not open TOC file for input: %m");
219  }
220 
221  /*
222  * Make unbuffered since we will dup() it, and the buffers screw each
223  * other
224  */
225  /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */
226 
227  ctx->tarFHpos = 0;
228 
229  ctx->hasSeek = checkSeek(ctx->tarFH);
230 
231  ctx->FH = (void *) tarOpen(AH, "toc.dat", 'r');
232  ReadHead(AH);
233  ReadToc(AH);
234  tarClose(AH, ctx->FH); /* Nothing else in the file... */
235  }
236 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
ReopenPtrType ReopenPtr
ReadBufPtrType ReadBufPtr
void ReadToc(ArchiveHandle *AH)
static void _StartData(ArchiveHandle *AH, TocEntry *te)
void ReadHead(ArchiveHandle *AH)
DeClonePtrType DeClonePtr
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
WriteBufPtrType WriteBufPtr
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
pgoff_t tarFHpos
Definition: pg_backup_tar.c:89
StartBlobPtrType StartBlobPtr
EndDataPtrType EndDataPtr
#define PG_BINARY_W
Definition: c.h:1274
static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
StartBlobsPtrType StartBlobsPtr
#define PG_BINARY_R
Definition: c.h:1273
ReadBytePtrType ReadBytePtr
EndBlobPtrType EndBlobPtr
int isSpecialScript
Definition: pg_backup_tar.c:92
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
FILE * tarFH
Definition: pg_backup_tar.c:88
static void _StartBlobs(ArchiveHandle *AH, TocEntry *te)
static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
StartDataPtrType StartDataPtr
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
WriteBytePtrType WriteBytePtr
static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
static void _EndBlobs(ArchiveHandle *AH, TocEntry *te)
PrintTocDataPtrType PrintTocDataPtr
WriteDataPtrType WriteDataPtr
static void _EndData(ArchiveHandle *AH, TocEntry *te)
EndBlobsPtrType EndBlobsPtr
static void _PrintTocData(ArchiveHandle *AH, TocEntry *te)
static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
ArchiveEntryPtrType ArchiveEntryPtr
#define fatal(...)
static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
ClonePtrType ClonePtr
static void _CloseArchive(ArchiveHandle *AH)
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
WorkerJobRestorePtrType WorkerJobRestorePtr
bool checkSeek(FILE *fp)
ClosePtrType ClosePtr
PrintExtraTocPtrType PrintExtraTocPtr
static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
pgoff_t filePos
Definition: pg_backup_tar.c:86
static int _ReadByte(ArchiveHandle *)
WriteExtraTocPtrType WriteExtraTocPtr
#define LOBBUFSIZE
static int _WriteByte(ArchiveHandle *AH, const int i)
ReadExtraTocPtrType ReadExtraTocPtr
static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
WorkerJobDumpPtrType WorkerJobDumpPtr

◆ isValidTarHeader()

bool isValidTarHeader ( char *  header)

Definition at line 1039 of file pg_backup_tar.c.

References read_tar_number(), and tarChecksum().

Referenced by _discoverArchiveFormat().

1040 {
1041  int sum;
1042  int chk = tarChecksum(header);
1043 
1044  sum = read_tar_number(&header[148], 8);
1045 
1046  if (sum != chk)
1047  return false;
1048 
1049  /* POSIX tar format */
1050  if (memcmp(&header[257], "ustar\0", 6) == 0 &&
1051  memcmp(&header[263], "00", 2) == 0)
1052  return true;
1053  /* GNU tar format */
1054  if (memcmp(&header[257], "ustar \0", 8) == 0)
1055  return true;
1056  /* not-quite-POSIX format written by pre-9.3 pg_dump */
1057  if (memcmp(&header[257], "ustar00\0", 8) == 0)
1058  return true;
1059 
1060  return false;
1061 }
uint64 read_tar_number(const char *s, int len)
Definition: tar.c:58
int tarChecksum(char *header)
Definition: tar.c:90
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210

◆ tarClose()

static void tarClose ( ArchiveHandle AH,
TAR_MEMBER TH 
)
static

Definition at line 435 of file pg_backup_tar.c.

References _tarAddFile(), _tarReadRaw(), TAR_MEMBER::AH, buf, _archiveHandle::compression, fatal, TAR_MEMBER::fileLen, free, GZCLOSE, TAR_MEMBER::mode, TAR_MEMBER::nFH, TAR_MEMBER::pos, TAR_MEMBER::targetFile, and TAR_MEMBER::zFH.

Referenced by _CloseArchive(), _EndBlob(), _EndBlobs(), _EndData(), _LoadBlobs(), _PrintFileData(), and InitArchiveFmt_Tar().

436 {
437  /*
438  * Close the GZ file since we dup'd. This will flush the buffers.
439  */
440  if (AH->compression != 0)
441  if (GZCLOSE(th->zFH) != 0)
442  fatal("could not close tar member");
443 
444  if (th->mode == 'w')
445  _tarAddFile(AH, th); /* This will close the temp file */
446 
447  /*
448  * else Nothing to do for normal read since we don't dup() normal file
449  * handle, and we don't use temp files.
450  */
451 
452  if (th->targetFile)
453  free(th->targetFile);
454 
455  th->nFH = NULL;
456  th->zFH = NULL;
457 }
#define free(a)
Definition: header.h:65
#define fatal(...)
#define GZCLOSE(fh)
static int static void _tarAddFile(ArchiveHandle *AH, TAR_MEMBER *th)

◆ tarOpen()

static TAR_MEMBER * tarOpen ( ArchiveHandle AH,
const char *  filename,
char  mode 
)
static

Definition at line 318 of file pg_backup_tar.c.

References _tarPositionTo(), _archiveHandle::compression, fatal, fd(), _archiveHandle::formatData, free, mode, name, pg_malloc0(), pg_strdup(), S_IRUSR, S_IRWXG, S_IRWXO, S_IWUSR, sprintf, lclContext::tarFH, and tm.

Referenced by _CloseArchive(), _LoadBlobs(), _PrintFileData(), _StartBlob(), _StartBlobs(), _StartData(), and InitArchiveFmt_Tar().

319 {
320  lclContext *ctx = (lclContext *) AH->formatData;
321  TAR_MEMBER *tm;
322 
323 #ifdef HAVE_LIBZ
324  char fmode[14];
325 #endif
326 
327  if (mode == 'r')
328  {
329  tm = _tarPositionTo(AH, filename);
330  if (!tm) /* Not found */
331  {
332  if (filename)
333  {
334  /*
335  * Couldn't find the requested file. Future: do SEEK(0) and
336  * retry.
337  */
338  fatal("could not find file \"%s\" in archive", filename);
339  }
340  else
341  {
342  /* Any file OK, none left, so return NULL */
343  return NULL;
344  }
345  }
346 
347 #ifdef HAVE_LIBZ
348 
349  if (AH->compression == 0)
350  tm->nFH = ctx->tarFH;
351  else
352  fatal("compression is not supported by tar archive format");
353  /* tm->zFH = gzdopen(dup(fileno(ctx->tarFH)), "rb"); */
354 #else
355  tm->nFH = ctx->tarFH;
356 #endif
357  }
358  else
359  {
360  int old_umask;
361 
362  tm = pg_malloc0(sizeof(TAR_MEMBER));
363 
364  /*
365  * POSIX does not require, but permits, tmpfile() to restrict file
366  * permissions. Given an OS crash after we write data, the filesystem
367  * might retain the data but forget tmpfile()'s unlink(). If so, the
368  * file mode protects confidentiality of the data written.
369  */
370  old_umask = umask(S_IRWXG | S_IRWXO);
371 
372 #ifndef WIN32
373  tm->tmpFH = tmpfile();
374 #else
375 
376  /*
377  * On WIN32, tmpfile() generates a filename in the root directory,
378  * which requires administrative permissions on certain systems. Loop
379  * until we find a unique file name we can create.
380  */
381  while (1)
382  {
383  char *name;
384  int fd;
385 
386  name = _tempnam(NULL, "pg_temp_");
387  if (name == NULL)
388  break;
389  fd = open(name, O_RDWR | O_CREAT | O_EXCL | O_BINARY |
390  O_TEMPORARY, S_IRUSR | S_IWUSR);
391  free(name);
392 
393  if (fd != -1) /* created a file */
394  {
395  tm->tmpFH = fdopen(fd, "w+b");
396  break;
397  }
398  else if (errno != EEXIST) /* failure other than file exists */
399  break;
400  }
401 #endif
402 
403  if (tm->tmpFH == NULL)
404  fatal("could not generate temporary file name: %m");
405 
406  umask(old_umask);
407 
408 #ifdef HAVE_LIBZ
409 
410  if (AH->compression != 0)
411  {
412  sprintf(fmode, "wb%d", AH->compression);
413  tm->zFH = gzdopen(dup(fileno(tm->tmpFH)), fmode);
414  if (tm->zFH == NULL)
415  fatal("could not open temporary file");
416  }
417  else
418  tm->nFH = tm->tmpFH;
419 #else
420 
421  tm->nFH = tm->tmpFH;
422 #endif
423 
424  tm->AH = AH;
425  tm->targetFile = pg_strdup(filename);
426  }
427 
428  tm->mode = mode;
429  tm->tarFH = ctx->tarFH;
430 
431  return tm;
432 }
static PgChecksumMode mode
Definition: pg_checksums.c:65
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static struct pg_tm tm
Definition: localtime.c:102
#define sprintf
Definition: port.h:218
#define S_IWUSR
Definition: win32_port.h:283
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
FILE * tarFH
Definition: pg_backup_tar.c:88
static TAR_MEMBER * _tarPositionTo(ArchiveHandle *AH, const char *filename)
#define S_IRWXG
Definition: win32_port.h:301
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define free(a)
Definition: header.h:65
#define fatal(...)
const char * name
Definition: encode.c:515
#define S_IRUSR
Definition: win32_port.h:280
static char * filename
Definition: pg_dumpall.c:92
#define S_IRWXO
Definition: win32_port.h:313

◆ tarPrintf()

static int tarPrintf ( TAR_MEMBER th,
const char *  fmt,
  ... 
)
static

Definition at line 1005 of file pg_backup_tar.c.

References generate_unaccent_rules::args, free, pg_malloc(), pvsnprintf(), and tarWrite().

Referenced by _CloseArchive(), and _StartBlob().

1006 {
1007  int save_errno = errno;
1008  char *p;
1009  size_t len = 128; /* initial assumption about buffer size */
1010  size_t cnt;
1011 
1012  for (;;)
1013  {
1014  va_list args;
1015 
1016  /* Allocate work buffer. */
1017  p = (char *) pg_malloc(len);
1018 
1019  /* Try to format the data. */
1020  errno = save_errno;
1021  va_start(args, fmt);
1022  cnt = pvsnprintf(p, len, fmt, args);
1023  va_end(args);
1024 
1025  if (cnt < len)
1026  break; /* success */
1027 
1028  /* Release buffer and loop around to try again with larger len. */
1029  free(p);
1030  len = cnt;
1031  }
1032 
1033  cnt = tarWrite(p, cnt, th);
1034  free(p);
1035  return (int) cnt;
1036 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
size_t pvsnprintf(char *buf, size_t len, const char *fmt, va_list args)
Definition: psprintf.c:106
#define free(a)
Definition: header.h:65
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th)

◆ tarRead()

static size_t tarRead ( void *  buf,
size_t  len,
TAR_MEMBER th 
)
static

Definition at line 574 of file pg_backup_tar.c.

References _tarReadRaw(), TAR_MEMBER::AH, TAR_MEMBER::fileLen, and TAR_MEMBER::pos.

Referenced by _LoadBlobs(), _PrintFileData(), _ReadBuf(), and _ReadByte().

575 {
576  size_t res;
577 
578  if (th->pos + len > th->fileLen)
579  len = th->fileLen - th->pos;
580 
581  if (len <= 0)
582  return 0;
583 
584  res = _tarReadRaw(th->AH, buf, len, th, NULL);
585 
586  th->pos += res;
587 
588  return res;
589 }
ArchiveHandle * AH
Definition: pg_backup_tar.c:80
static char * buf
Definition: pg_test_fsync.c:68
pgoff_t pos
Definition: pg_backup_tar.c:78
pgoff_t fileLen
Definition: pg_backup_tar.c:79
static size_t _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)

◆ tarWrite()

static size_t tarWrite ( const void *  buf,
size_t  len,
TAR_MEMBER th 
)
static

Definition at line 592 of file pg_backup_tar.c.

References GZWRITE, TAR_MEMBER::nFH, TAR_MEMBER::pos, and TAR_MEMBER::zFH.

Referenced by _scriptOut(), _WriteBuf(), _WriteByte(), _WriteData(), and tarPrintf().

593 {
594  size_t res;
595 
596  if (th->zFH != NULL)
597  res = GZWRITE(buf, 1, len, th->zFH);
598  else
599  res = fwrite(buf, 1, len, th->nFH);
600 
601  th->pos += res;
602  return res;
603 }
FILE * zFH
Definition: pg_backup_tar.c:71
static char * buf
Definition: pg_test_fsync.c:68
#define GZWRITE(p, s, n, fh)
FILE * nFH
Definition: pg_backup_tar.c:73
pgoff_t pos
Definition: pg_backup_tar.c:78