PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
pg_backup_tar.c File Reference
#include "postgres_fe.h"
#include "pg_backup_archiver.h"
#include "pg_backup_tar.h"
#include "pg_backup_utils.h"
#include "pgtar.h"
#include "common/file_utils.h"
#include "fe_utils/string_utils.h"
#include <sys/stat.h>
#include <ctype.h>
#include <limits.h>
#include <unistd.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 (ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt,...) pg_attribute_printf(3
 
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)
 

Variables

static const char * modulename = gettext_noop("tar archiver")
 

Macro Definition Documentation

#define K_STD_BUF_SIZE   1024

Definition at line 63 of file pg_backup_tar.c.

Referenced by _ArchiveEntry(), and _StartBlobs().

Function Documentation

static void _ArchiveEntry ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 257 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(), and lclTocEntry::TH.

Referenced by InitArchiveFmt_Tar().

258 {
259  lclTocEntry *ctx;
260  char fn[K_STD_BUF_SIZE];
261 
262  ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
263  if (te->dataDumper != NULL)
264  {
265 #ifdef HAVE_LIBZ
266  if (AH->compression == 0)
267  sprintf(fn, "%d.dat", te->dumpId);
268  else
269  sprintf(fn, "%d.dat.gz", te->dumpId);
270 #else
271  sprintf(fn, "%d.dat", te->dumpId);
272 #endif
273  ctx->filename = pg_strdup(fn);
274  }
275  else
276  {
277  ctx->filename = NULL;
278  ctx->TH = NULL;
279  }
280  te->formatData = (void *) ctx;
281 }
DataDumperPtr dataDumper
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
static void _CloseArchive ( ArchiveHandle AH)
static

Definition at line 838 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

839 {
840  lclContext *ctx = (lclContext *) AH->formatData;
841  TAR_MEMBER *th;
842  RestoreOptions *ropt;
843  RestoreOptions *savRopt;
844  DumpOptions *savDopt;
845  int savVerbose,
846  i;
847 
848  if (AH->mode == archModeWrite)
849  {
850  /*
851  * Write the Header & TOC to the archive FIRST
852  */
853  th = tarOpen(AH, "toc.dat", 'w');
854  ctx->FH = th;
855  WriteHead(AH);
856  WriteToc(AH);
857  tarClose(AH, th); /* Not needed any more */
858 
859  /*
860  * Now send the data (tables & blobs)
861  */
862  WriteDataChunks(AH, NULL);
863 
864  /*
865  * Now this format wants to append a script which does a full restore
866  * if the files have been extracted.
867  */
868  th = tarOpen(AH, "restore.sql", 'w');
869 
870  tarPrintf(AH, th, "--\n"
871  "-- NOTE:\n"
872  "--\n"
873  "-- File paths need to be edited. Search for $$PATH$$ and\n"
874  "-- replace it with the path to the directory containing\n"
875  "-- the extracted data files.\n"
876  "--\n");
877 
878  AH->CustomOutPtr = _scriptOut;
879 
880  ctx->isSpecialScript = 1;
881  ctx->scriptTH = th;
882 
883  ropt = NewRestoreOptions();
884  memcpy(ropt, AH->public.ropt, sizeof(RestoreOptions));
885  ropt->filename = NULL;
886  ropt->dropSchema = 1;
887  ropt->compression = 0;
888  ropt->superuser = NULL;
889  ropt->suppressDumpWarnings = true;
890 
891  savDopt = AH->public.dopt;
892  savRopt = AH->public.ropt;
893 
894  SetArchiveOptions((Archive *) AH, NULL, ropt);
895 
896  savVerbose = AH->public.verbose;
897  AH->public.verbose = 0;
898 
899  RestoreArchive((Archive *) AH);
900 
901  SetArchiveOptions((Archive *) AH, savDopt, savRopt);
902 
903  AH->public.verbose = savVerbose;
904 
905  tarClose(AH, th);
906 
907  ctx->isSpecialScript = 0;
908 
909  /*
910  * EOF marker for tar files is two blocks of NULLs.
911  */
912  for (i = 0; i < 512 * 2; i++)
913  {
914  if (fputc(0, ctx->tarFH) == EOF)
916  }
917 
918  /* Sync the output file if one is defined */
919  if (AH->dosync && AH->fSpec)
920  (void) fsync_fname(AH->fSpec, false, progname);
921  }
922 
923  AH->FH = NULL;
924 }
static int tarPrintf(ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt,...) pg_attribute_printf(3
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
TAR_MEMBER * scriptTH
Definition: pg_backup_tar.c:93
RestoreOptions * ropt
Definition: pg_backup.h:182
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:572
void WriteToc(ArchiveHandle *AH)
const char * progname
Definition: pg_standby.c:37
RestoreOptions * NewRestoreOptions(void)
int isSpecialScript
Definition: pg_backup_tar.c:92
DumpOptions * dopt
Definition: pg_backup.h:181
FILE * tarFH
Definition: pg_backup_tar.c:88
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
int verbose
Definition: pg_backup.h:184
static size_t _scriptOut(ArchiveHandle *AH, const void *buf, size_t len)
CustomOutPtrType CustomOutPtr
void WriteHead(ArchiveHandle *AH)
#define WRITE_ERROR_EXIT
void RestoreArchive(Archive *AH)
int i
void SetArchiveOptions(Archive *AH, DumpOptions *dopt, RestoreOptions *ropt)
static void _EndBlob ( ArchiveHandle AH,
TocEntry te,
Oid  oid 
)
static

Definition at line 995 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

996 {
997  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
998 
999  tarClose(AH, tctx->TH);
1000 }
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
TAR_MEMBER * TH
Definition: pg_backup_tar.c:98
static void _EndBlobs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 1009 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

1010 {
1011  lclContext *ctx = (lclContext *) AH->formatData;
1012 
1013  /* Write out a fake zero OID to mark end-of-blobs. */
1014  /* WriteInt(AH, 0); */
1015 
1016  tarClose(AH, ctx->blobToc);
1017 }
TAR_MEMBER * blobToc
Definition: pg_backup_tar.c:87
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
static void _EndData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 633 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

634 {
635  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
636 
637  /* Close the file */
638  tarClose(AH, tctx->TH);
639  tctx->TH = NULL;
640 }
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
TAR_MEMBER * TH
Definition: pg_backup_tar.c:98
static void _LoadBlobs ( ArchiveHandle AH)
static

Definition at line 728 of file pg_backup_tar.c.

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

Referenced by _PrintTocData().

729 {
730  Oid oid;
731  lclContext *ctx = (lclContext *) AH->formatData;
732  TAR_MEMBER *th;
733  size_t cnt;
734  bool foundBlob = false;
735  char buf[4096];
736 
737  StartRestoreBlobs(AH);
738 
739  th = tarOpen(AH, NULL, 'r'); /* Open next file */
740  while (th != NULL)
741  {
742  ctx->FH = th;
743 
744  if (strncmp(th->targetFile, "blob_", 5) == 0)
745  {
746  oid = atooid(&th->targetFile[5]);
747  if (oid != 0)
748  {
749  ahlog(AH, 1, "restoring large object with OID %u\n", oid);
750 
751  StartRestoreBlob(AH, oid, AH->public.ropt->dropSchema);
752 
753  while ((cnt = tarRead(buf, 4095, th)) > 0)
754  {
755  buf[cnt] = '\0';
756  ahwrite(buf, 1, cnt, AH);
757  }
758  EndRestoreBlob(AH, oid);
759  foundBlob = true;
760  }
761  tarClose(AH, th);
762  }
763  else
764  {
765  tarClose(AH, th);
766 
767  /*
768  * Once we have found the first blob, stop at the first non-blob
769  * entry (which will be 'blobs.toc'). This coding would eat all
770  * the rest of the archive if there are no blobs ... but this
771  * function shouldn't be called at all in that case.
772  */
773  if (foundBlob)
774  break;
775  }
776 
777  th = tarOpen(AH, NULL, 'r');
778  }
779  EndRestoreBlobs(AH);
780 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
RestoreOptions * ropt
Definition: pg_backup.h:182
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:67
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
#define atooid(x)
Definition: postgres_ext.h:42
void EndRestoreBlobs(ArchiveHandle *AH)
void ahlog(ArchiveHandle *AH, int level, const char *fmt,...)
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)
static void _PrintExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 315 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

316 {
317  lclTocEntry *ctx = (lclTocEntry *) te->formatData;
318 
319  if (AH->public.verbose && ctx->filename != NULL)
320  ahprintf(AH, "-- File: %s\n", ctx->filename);
321 }
int ahprintf(ArchiveHandle *AH, const char *fmt,...)
int verbose
Definition: pg_backup.h:184
static void _PrintFileData ( ArchiveHandle AH,
char *  filename 
)
static

Definition at line 646 of file pg_backup_tar.c.

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

Referenced by _PrintTocData().

647 {
648  lclContext *ctx = (lclContext *) AH->formatData;
649  char buf[4096];
650  size_t cnt;
651  TAR_MEMBER *th;
652 
653  if (!filename)
654  return;
655 
656  th = tarOpen(AH, filename, 'r');
657  ctx->FH = th;
658 
659  while ((cnt = tarRead(buf, 4095, th)) > 0)
660  {
661  buf[cnt] = '\0';
662  ahwrite(buf, 1, cnt, AH);
663  }
664 
665  tarClose(AH, th);
666 }
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:67
static void tarClose(ArchiveHandle *AH, TAR_MEMBER *TH)
static char * filename
Definition: pg_dumpall.c:90
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)
static void _PrintTocData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 673 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

674 {
675  lclContext *ctx = (lclContext *) AH->formatData;
676  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
677  int pos1;
678 
679  if (!tctx->filename)
680  return;
681 
682  /*
683  * If we're writing the special restore.sql script, emit a suitable
684  * command to include each table's data from the corresponding file.
685  *
686  * In the COPY case this is a bit klugy because the regular COPY command
687  * was already printed before we get control.
688  */
689  if (ctx->isSpecialScript)
690  {
691  if (te->copyStmt)
692  {
693  /* Abort the COPY FROM stdin */
694  ahprintf(AH, "\\.\n");
695 
696  /*
697  * The COPY statement should look like "COPY ... FROM stdin;\n",
698  * see dumpTableData().
699  */
700  pos1 = (int) strlen(te->copyStmt) - 13;
701  if (pos1 < 6 || strncmp(te->copyStmt, "COPY ", 5) != 0 ||
702  strcmp(te->copyStmt + pos1, " FROM stdin;\n") != 0)
704  "unexpected COPY statement syntax: \"%s\"\n",
705  te->copyStmt);
706 
707  /* Emit all but the FROM part ... */
708  ahwrite(te->copyStmt, 1, pos1, AH);
709  /* ... and insert modified FROM */
710  ahprintf(AH, " FROM '$$PATH$$/%s';\n\n", tctx->filename);
711  }
712  else
713  {
714  /* --inserts mode, no worries, just include the data file */
715  ahprintf(AH, "\\i $$PATH$$/%s\n\n", tctx->filename);
716  }
717 
718  return;
719  }
720 
721  if (strcmp(te->desc, "BLOBS") == 0)
722  _LoadBlobs(AH);
723  else
724  _PrintFileData(AH, tctx->filename);
725 }
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,...)
static const char * modulename
void exit_horribly(const char *modulename, const char *fmt,...)
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
static void _ReadBuf ( ArchiveHandle AH,
void *  buf,
size_t  len 
)
static

Definition at line 824 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

825 {
826  lclContext *ctx = (lclContext *) AH->formatData;
827 
828  if (tarRead(buf, len, ctx->FH) != len)
829  /* We already would have exited for errors on reads, must be EOF */
831  "could not read from input file: end of file\n");
832 
833  ctx->filePos += len;
834  return;
835 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
static char * buf
Definition: pg_test_fsync.c:67
static const char * modulename
void exit_horribly(const char *modulename, const char *fmt,...)
static size_t tarRead(void *buf, size_t len, TAR_MEMBER *th)
pgoff_t filePos
static int _ReadByte ( ArchiveHandle AH)
static

Definition at line 797 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

798 {
799  lclContext *ctx = (lclContext *) AH->formatData;
800  size_t res;
801  unsigned char c;
802 
803  res = tarRead(&c, 1, ctx->FH);
804  if (res != 1)
805  /* We already would have exited for errors on reads, must be EOF */
807  "could not read from input file: end of file\n");
808  ctx->filePos += 1;
809  return c;
810 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
char * c
static const char * modulename
void exit_horribly(const char *modulename, const char *fmt,...)
static size_t tarRead(void *buf, size_t len, TAR_MEMBER *th)
pgoff_t filePos
static void _ReadExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 295 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

296 {
297  lclTocEntry *ctx = (lclTocEntry *) te->formatData;
298 
299  if (ctx == NULL)
300  {
301  ctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
302  te->formatData = (void *) ctx;
303  }
304 
305  ctx->filename = ReadStr(AH);
306  if (strlen(ctx->filename) == 0)
307  {
308  free(ctx->filename);
309  ctx->filename = NULL;
310  }
311  ctx->TH = NULL;
312 }
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
static size_t _scriptOut ( ArchiveHandle AH,
const void *  buf,
size_t  len 
)
static

Definition at line 927 of file pg_backup_tar.c.

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

Referenced by _CloseArchive().

928 {
929  lclContext *ctx = (lclContext *) AH->formatData;
930 
931  return tarWrite(buf, len, ctx->scriptTH);
932 }
TAR_MEMBER * scriptTH
Definition: pg_backup_tar.c:93
static char * buf
Definition: pg_test_fsync.c:67
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th)
static void _StartBlob ( ArchiveHandle AH,
TocEntry te,
Oid  oid 
)
static

Definition at line 966 of file pg_backup_tar.c.

References lclContext::blobToc, _archiveHandle::compression, exit_horribly(), _archiveHandle::formatData, _tocEntry::formatData, modulename, tarOpen(), and tarPrintf().

Referenced by InitArchiveFmt_Tar().

967 {
968  lclContext *ctx = (lclContext *) AH->formatData;
969  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
970  char fname[255];
971  char *sfx;
972 
973  if (oid == 0)
974  exit_horribly(modulename, "invalid OID for large object (%u)\n", oid);
975 
976  if (AH->compression != 0)
977  sfx = ".gz";
978  else
979  sfx = "";
980 
981  sprintf(fname, "blob_%u.dat%s", oid, sfx);
982 
983  tarPrintf(AH, ctx->blobToc, "%u %s\n", oid, fname);
984 
985  tctx->TH = tarOpen(AH, fname, 'w');
986 }
static int tarPrintf(ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt,...) pg_attribute_printf(3
TAR_MEMBER * blobToc
Definition: pg_backup_tar.c:87
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
static const char * modulename
void exit_horribly(const char *modulename, const char *fmt,...)
static void _StartBlobs ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 949 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

950 {
951  lclContext *ctx = (lclContext *) AH->formatData;
952  char fname[K_STD_BUF_SIZE];
953 
954  sprintf(fname, "blobs.toc");
955  ctx->blobToc = tarOpen(AH, fname, 'w');
956 }
TAR_MEMBER * blobToc
Definition: pg_backup_tar.c:87
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
#define K_STD_BUF_SIZE
Definition: pg_backup_tar.c:63
static void _StartData ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 324 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

325 {
326  lclTocEntry *tctx = (lclTocEntry *) te->formatData;
327 
328  tctx->TH = tarOpen(AH, tctx->filename, 'w');
329 }
static TAR_MEMBER * tarOpen(ArchiveHandle *AH, const char *filename, char mode)
TAR_MEMBER * TH
Definition: pg_backup_tar.c:98
static void _tarAddFile ( ArchiveHandle AH,
TAR_MEMBER th 
)
static

Definition at line 1085 of file pg_backup_tar.c.

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

Referenced by tarClose().

1086 {
1087  lclContext *ctx = (lclContext *) AH->formatData;
1088  FILE *tmp = th->tmpFH; /* Grab it for convenience */
1089  char buf[32768];
1090  size_t cnt;
1091  pgoff_t len = 0;
1092  size_t res;
1093  size_t i,
1094  pad;
1095 
1096  /*
1097  * Find file len & go back to start.
1098  */
1099  fseeko(tmp, 0, SEEK_END);
1100  th->fileLen = ftello(tmp);
1101  if (th->fileLen < 0)
1102  exit_horribly(modulename, "could not determine seek position in archive file: %s\n",
1103  strerror(errno));
1104  fseeko(tmp, 0, SEEK_SET);
1105 
1106  _tarWriteHeader(th);
1107 
1108  while ((cnt = fread(buf, 1, sizeof(buf), tmp)) > 0)
1109  {
1110  if ((res = fwrite(buf, 1, cnt, th->tarFH)) != cnt)
1112  len += res;
1113  }
1114  if (!feof(tmp))
1115  READ_ERROR_EXIT(tmp);
1116 
1117  if (fclose(tmp) != 0) /* This *should* delete it... */
1118  exit_horribly(modulename, "could not close temporary file: %s\n",
1119  strerror(errno));
1120 
1121  if (len != th->fileLen)
1122  {
1123  char buf1[32],
1124  buf2[32];
1125 
1126  snprintf(buf1, sizeof(buf1), INT64_FORMAT, (int64) len);
1127  snprintf(buf2, sizeof(buf2), INT64_FORMAT, (int64) th->fileLen);
1128  exit_horribly(modulename, "actual file length (%s) does not match expected (%s)\n",
1129  buf1, buf2);
1130  }
1131 
1132  pad = ((len + 511) & ~511) - len;
1133  for (i = 0; i < pad; i++)
1134  {
1135  if (fputc('\0', th->tarFH) == EOF)
1137  }
1138 
1139  ctx->tarFHpos += len + pad;
1140 }
pgoff_t tarFHpos
Definition: pg_backup_tar.c:89
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define fseeko(stream, offset, origin)
Definition: win32.h:237
static char * buf
Definition: pg_test_fsync.c:67
#define pgoff_t
Definition: win32.h:231
static const char * modulename
static void _tarWriteHeader(TAR_MEMBER *th)
FILE * tmpFH
Definition: pg_backup_tar.c:75
pgoff_t fileLen
Definition: pg_backup_tar.c:79
#define ftello(stream)
Definition: win32.h:240
#define INT64_FORMAT
Definition: c.h:300
#define WRITE_ERROR_EXIT
void exit_horribly(const char *modulename, const char *fmt,...)
int i
const char * strerror(int errnum)
Definition: strerror.c:19
#define READ_ERROR_EXIT(fd)
FILE * tarFH
Definition: pg_backup_tar.c:74
static int _tarGetHeader ( ArchiveHandle AH,
TAR_MEMBER th 
)
static

Definition at line 1226 of file pg_backup_tar.c.

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

Referenced by _tarPositionTo().

1227 {
1228  lclContext *ctx = (lclContext *) AH->formatData;
1229  char h[512];
1230  char tag[100 + 1];
1231  int sum,
1232  chk;
1233  pgoff_t len;
1234  pgoff_t hPos;
1235  bool gotBlock = false;
1236 
1237  while (!gotBlock)
1238  {
1239  /* Save the pos for reporting purposes */
1240  hPos = ctx->tarFHpos;
1241 
1242  /* Read a 512 byte block, return EOF, exit if short */
1243  len = _tarReadRaw(AH, h, 512, NULL, ctx->tarFH);
1244  if (len == 0) /* EOF */
1245  return 0;
1246 
1247  if (len != 512)
1249  ngettext("incomplete tar header found (%lu byte)\n",
1250  "incomplete tar header found (%lu bytes)\n",
1251  len),
1252  (unsigned long) len);
1253 
1254  /* Calc checksum */
1255  chk = tarChecksum(h);
1256  sum = read_tar_number(&h[148], 8);
1257 
1258  /*
1259  * If the checksum failed, see if it is a null block. If so, silently
1260  * continue to the next block.
1261  */
1262  if (chk == sum)
1263  gotBlock = true;
1264  else
1265  {
1266  int i;
1267 
1268  for (i = 0; i < 512; i++)
1269  {
1270  if (h[i] != 0)
1271  {
1272  gotBlock = true;
1273  break;
1274  }
1275  }
1276  }
1277  }
1278 
1279  /* Name field is 100 bytes, might not be null-terminated */
1280  strlcpy(tag, &h[0], 100 + 1);
1281 
1282  len = read_tar_number(&h[124], 12);
1283 
1284  {
1285  char posbuf[32];
1286  char lenbuf[32];
1287 
1288  snprintf(posbuf, sizeof(posbuf), UINT64_FORMAT, (uint64) hPos);
1289  snprintf(lenbuf, sizeof(lenbuf), UINT64_FORMAT, (uint64) len);
1290  ahlog(AH, 3, "TOC Entry %s at %s (length %s, checksum %d)\n",
1291  tag, posbuf, lenbuf, sum);
1292  }
1293 
1294  if (chk != sum)
1295  {
1296  char posbuf[32];
1297 
1298  snprintf(posbuf, sizeof(posbuf), UINT64_FORMAT,
1299  (uint64) ftello(ctx->tarFH));
1301  "corrupt tar header found in %s "
1302  "(expected %d, computed %d) file position %s\n",
1303  tag, sum, chk, posbuf);
1304  }
1305 
1306  th->targetFile = pg_strdup(tag);
1307  th->fileLen = len;
1308 
1309  return 1;
1310 }
uint64 read_tar_number(const char *s, int len)
Definition: tar.c:56
pgoff_t tarFHpos
Definition: pg_backup_tar.c:89
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
FILE * tarFH
Definition: pg_backup_tar.c:88
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define pgoff_t
Definition: win32.h:231
#define ngettext(s, p, n)
Definition: c.h:127
static const char * modulename
int tarChecksum(char *header)
Definition: tar.c:88
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 ftello(stream)
Definition: win32.h:240
void exit_horribly(const char *modulename, const char *fmt,...)
void ahlog(ArchiveHandle *AH, int level, const char *fmt,...)
int i
#define UINT64_FORMAT
Definition: c.h:301
char * targetFile
Definition: pg_backup_tar.c:76
static size_t _tarReadRaw(ArchiveHandle *AH, void *buf, size_t len, TAR_MEMBER *th, FILE *fh)
static TAR_MEMBER * _tarPositionTo ( ArchiveHandle AH,
const char *  filename 
)
static

Definition at line 1144 of file pg_backup_tar.c.

References _tarGetHeader(), _tarReadRaw(), TAR_MEMBER::AH, ahlog(), exit_horribly(), TAR_MEMBER::fileLen, _archiveHandle::formatData, free, header(), i, INT64_FORMAT, modulename, pg_malloc0(), TAR_MEMBER::pos, REQ_DATA, snprintf(), lclContext::tarFH, lclContext::tarFHpos, TAR_MEMBER::targetFile, lclContext::tarNextMember, and TocIDRequired().

Referenced by tarOpen().

1145 {
1146  lclContext *ctx = (lclContext *) AH->formatData;
1147  TAR_MEMBER *th = pg_malloc0(sizeof(TAR_MEMBER));
1148  char c;
1149  char header[512];
1150  size_t i,
1151  len,
1152  blks;
1153  int id;
1154 
1155  th->AH = AH;
1156 
1157  /* Go to end of current file, if any */
1158  if (ctx->tarFHpos != 0)
1159  {
1160  char buf1[100],
1161  buf2[100];
1162 
1163  snprintf(buf1, sizeof(buf1), INT64_FORMAT, (int64) ctx->tarFHpos);
1164  snprintf(buf2, sizeof(buf2), INT64_FORMAT, (int64) ctx->tarNextMember);
1165  ahlog(AH, 4, "moving from position %s to next member at file position %s\n",
1166  buf1, buf2);
1167 
1168  while (ctx->tarFHpos < ctx->tarNextMember)
1169  _tarReadRaw(AH, &c, 1, NULL, ctx->tarFH);
1170  }
1171 
1172  {
1173  char buf[100];
1174 
1175  snprintf(buf, sizeof(buf), INT64_FORMAT, (int64) ctx->tarFHpos);
1176  ahlog(AH, 4, "now at file position %s\n", buf);
1177  }
1178 
1179  /* We are at the start of the file, or at the next member */
1180 
1181  /* Get the header */
1182  if (!_tarGetHeader(AH, th))
1183  {
1184  if (filename)
1185  exit_horribly(modulename, "could not find header for file \"%s\" in tar archive\n", filename);
1186  else
1187  {
1188  /*
1189  * We're just scanning the archive for the next file, so return
1190  * null
1191  */
1192  free(th);
1193  return NULL;
1194  }
1195  }
1196 
1197  while (filename != NULL && strcmp(th->targetFile, filename) != 0)
1198  {
1199  ahlog(AH, 4, "skipping tar member %s\n", th->targetFile);
1200 
1201  id = atoi(th->targetFile);
1202  if ((TocIDRequired(AH, id) & REQ_DATA) != 0)
1203  exit_horribly(modulename, "restoring data out of order is not supported in this archive format: "
1204  "\"%s\" is required, but comes before \"%s\" in the archive file.\n",
1205  th->targetFile, filename);
1206 
1207  /* Header doesn't match, so read to next header */
1208  len = ((th->fileLen + 511) & ~511); /* Padded length */
1209  blks = len >> 9; /* # of 512 byte blocks */
1210 
1211  for (i = 0; i < blks; i++)
1212  _tarReadRaw(AH, &header[0], 512, NULL, ctx->tarFH);
1213 
1214  if (!_tarGetHeader(AH, th))
1215  exit_horribly(modulename, "could not find header for file \"%s\" in tar archive\n", filename);
1216  }
1217 
1218  ctx->tarNextMember = ctx->tarFHpos + ((th->fileLen + 511) & ~511);
1219  th->pos = 0;
1220 
1221  return th;
1222 }
pgoff_t tarFHpos
Definition: pg_backup_tar.c:89
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
pgoff_t tarNextMember
Definition: pg_backup_tar.c:90
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
FILE * tarFH
Definition: pg_backup_tar.c:88
char * c
static char * buf
Definition: pg_test_fsync.c:67
static const char * modulename
#define free(a)
Definition: header.h:65
teReqs TocIDRequired(ArchiveHandle *AH, DumpId id)
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
#define INT64_FORMAT
Definition: c.h:300
void exit_horribly(const char *modulename, const char *fmt,...)
static char * filename
Definition: pg_dumpall.c:90
void ahlog(ArchiveHandle *AH, int level, const char *fmt,...)
int i
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 _tarReadRaw ( ArchiveHandle AH,
void *  buf,
size_t  len,
TAR_MEMBER th,
FILE *  fh 
)
static

Definition at line 519 of file pg_backup_tar.c.

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

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

520 {
521  lclContext *ctx = (lclContext *) AH->formatData;
522  size_t avail;
523  size_t used = 0;
524  size_t res = 0;
525 
526  avail = AH->lookaheadLen - AH->lookaheadPos;
527  if (avail > 0)
528  {
529  /* We have some lookahead bytes to use */
530  if (avail >= len) /* Just use the lookahead buffer */
531  used = len;
532  else
533  used = avail;
534 
535  /* Copy, and adjust buffer pos */
536  memcpy(buf, AH->lookahead + AH->lookaheadPos, used);
537  AH->lookaheadPos += used;
538 
539  /* Adjust required length */
540  len -= used;
541  }
542 
543  /* Read the file if len > 0 */
544  if (len > 0)
545  {
546  if (fh)
547  {
548  res = fread(&((char *) buf)[used], 1, len, fh);
549  if (res != len && !feof(fh))
550  READ_ERROR_EXIT(fh);
551  }
552  else if (th)
553  {
554  if (th->zFH)
555  {
556  res = GZREAD(&((char *) buf)[used], 1, len, th->zFH);
557  if (res != len && !GZEOF(th->zFH))
558  {
559 #ifdef HAVE_LIBZ
560  int errnum;
561  const char *errmsg = gzerror(th->zFH, &errnum);
562 
564  "could not read from input file: %s\n",
565  errnum == Z_ERRNO ? strerror(errno) : errmsg);
566 #else
568  "could not read from input file: %s\n",
569  strerror(errno));
570 #endif
571  }
572  }
573  else
574  {
575  res = fread(&((char *) buf)[used], 1, len, th->nFH);
576  if (res != len && !feof(th->nFH))
577  READ_ERROR_EXIT(th->nFH);
578  }
579  }
580  else
581  exit_horribly(modulename, "internal error -- neither th nor fh specified in tarReadRaw()\n");
582  }
583 
584  ctx->tarFHpos += res + used;
585 
586  return (res + used);
587 }
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:67
#define GZEOF(fh)
FILE * nFH
Definition: pg_backup_tar.c:73
static const char * modulename
#define GZREAD(p, s, n, fh)
void exit_horribly(const char *modulename, const char *fmt,...)
int errmsg(const char *fmt,...)
Definition: elog.c:797
const char * strerror(int errnum)
Definition: strerror.c:19
#define READ_ERROR_EXIT(fd)
static void _tarWriteHeader ( TAR_MEMBER th)
static

Definition at line 1314 of file pg_backup_tar.c.

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

Referenced by _tarAddFile().

1315 {
1316  char h[512];
1317 
1318  tarCreateHeader(h, th->targetFile, NULL, th->fileLen,
1319  0600, 04000, 02000, time(NULL));
1320 
1321  /* Now write the completed header. */
1322  if (fwrite(h, 1, 512, th->tarFH) != 512)
1324 }
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:112
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
static void _WriteBuf ( ArchiveHandle AH,
const void *  buf,
size_t  len 
)
static

Definition at line 813 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

814 {
815  lclContext *ctx = (lclContext *) AH->formatData;
816 
817  if (tarWrite(buf, len, ctx->FH) != len)
819 
820  ctx->filePos += len;
821 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
static char * buf
Definition: pg_test_fsync.c:67
#define WRITE_ERROR_EXIT
pgoff_t filePos
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th)
static int _WriteByte ( ArchiveHandle AH,
const int  i 
)
static

Definition at line 784 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

785 {
786  lclContext *ctx = (lclContext *) AH->formatData;
787  char b = i; /* Avoid endian problems */
788 
789  if (tarWrite(&b, 1, ctx->FH) != 1)
791 
792  ctx->filePos += 1;
793  return 1;
794 }
TAR_MEMBER * FH
Definition: pg_backup_tar.c:91
#define WRITE_ERROR_EXIT
int i
pgoff_t filePos
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th)
static void _WriteData ( ArchiveHandle AH,
const void *  data,
size_t  dLen 
)
static

Definition at line 622 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

623 {
624  lclTocEntry *tctx = (lclTocEntry *) AH->currToc->formatData;
625 
626  if (tarWrite(data, dLen, tctx->TH) != dLen)
628 
629  return;
630 }
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)
static void _WriteExtraToc ( ArchiveHandle AH,
TocEntry te 
)
static

Definition at line 284 of file pg_backup_tar.c.

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

Referenced by InitArchiveFmt_Tar().

285 {
286  lclTocEntry *ctx = (lclTocEntry *) te->formatData;
287 
288  if (ctx->filename)
289  WriteStr(AH, ctx->filename);
290  else
291  WriteStr(AH, "");
292 }
size_t WriteStr(ArchiveHandle *AH, const char *c)
void InitArchiveFmt_Tar ( ArchiveHandle AH)

Definition at line 129 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(), exit_horribly(), lclContext::FH, lclContext::filePos, lclContext::hasSeek, lclContext::isSpecialScript, LOBBUFSIZE, modulename, PG_BINARY_R, PG_BINARY_W, pg_malloc(), pg_malloc0(), ReadHead(), ReadToc(), strerror(), tarClose(), lclContext::tarFH, lclContext::tarFHpos, and tarOpen().

Referenced by _allocAH().

130 {
131  lclContext *ctx;
132 
133  /* Assuming static functions, this can be copied for each format. */
135  AH->StartDataPtr = _StartData;
136  AH->WriteDataPtr = _WriteData;
137  AH->EndDataPtr = _EndData;
138  AH->WriteBytePtr = _WriteByte;
139  AH->ReadBytePtr = _ReadByte;
140  AH->WriteBufPtr = _WriteBuf;
141  AH->ReadBufPtr = _ReadBuf;
142  AH->ClosePtr = _CloseArchive;
143  AH->ReopenPtr = NULL;
148 
150  AH->StartBlobPtr = _StartBlob;
151  AH->EndBlobPtr = _EndBlob;
152  AH->EndBlobsPtr = _EndBlobs;
153  AH->ClonePtr = NULL;
154  AH->DeClonePtr = NULL;
155 
156  AH->WorkerJobDumpPtr = NULL;
157  AH->WorkerJobRestorePtr = NULL;
158 
159  /*
160  * Set up some special context used in compressing data.
161  */
162  ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
163  AH->formatData = (void *) ctx;
164  ctx->filePos = 0;
165  ctx->isSpecialScript = 0;
166 
167  /* Initialize LO buffering */
168  AH->lo_buf_size = LOBBUFSIZE;
169  AH->lo_buf = (void *) pg_malloc(LOBBUFSIZE);
170 
171  /*
172  * Now open the tar file, and load the TOC if we're in read mode.
173  */
174  if (AH->mode == archModeWrite)
175  {
176  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
177  {
178  ctx->tarFH = fopen(AH->fSpec, PG_BINARY_W);
179  if (ctx->tarFH == NULL)
181  "could not open TOC file \"%s\" for output: %s\n",
182  AH->fSpec, strerror(errno));
183  }
184  else
185  {
186  ctx->tarFH = stdout;
187  if (ctx->tarFH == NULL)
189  "could not open TOC file for output: %s\n",
190  strerror(errno));
191  }
192 
193  ctx->tarFHpos = 0;
194 
195  /*
196  * Make unbuffered since we will dup() it, and the buffers screw each
197  * other
198  */
199  /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */
200 
201  ctx->hasSeek = checkSeek(ctx->tarFH);
202 
203  /*
204  * We don't support compression because reading the files back is not
205  * possible since gzdopen uses buffered IO which totally screws file
206  * positioning.
207  */
208  if (AH->compression != 0)
210  "compression is not supported by tar archive format\n");
211  }
212  else
213  { /* Read Mode */
214  if (AH->fSpec && strcmp(AH->fSpec, "") != 0)
215  {
216  ctx->tarFH = fopen(AH->fSpec, PG_BINARY_R);
217  if (ctx->tarFH == NULL)
218  exit_horribly(modulename, "could not open TOC file \"%s\" for input: %s\n",
219  AH->fSpec, strerror(errno));
220  }
221  else
222  {
223  ctx->tarFH = stdin;
224  if (ctx->tarFH == NULL)
225  exit_horribly(modulename, "could not open TOC file for input: %s\n",
226  strerror(errno));
227  }
228 
229  /*
230  * Make unbuffered since we will dup() it, and the buffers screw each
231  * other
232  */
233  /* setvbuf(ctx->tarFH, NULL, _IONBF, 0); */
234 
235  ctx->tarFHpos = 0;
236 
237  ctx->hasSeek = checkSeek(ctx->tarFH);
238 
239  /*
240  * Forcibly unmark the header as read since we use the lookahead
241  * buffer
242  */
243  AH->readHeader = 0;
244 
245  ctx->FH = (void *) tarOpen(AH, "toc.dat", 'r');
246  ReadHead(AH);
247  ReadToc(AH);
248  tarClose(AH, ctx->FH); /* Nothing else in the file... */
249  }
250 }
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:1047
static void _EndBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
StartBlobsPtrType StartBlobsPtr
#define PG_BINARY_R
Definition: c.h:1046
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)
static const char * modulename
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
static void _StartBlob(ArchiveHandle *AH, TocEntry *te, Oid oid)
void exit_horribly(const char *modulename, const char *fmt,...)
ClonePtrType ClonePtr
static void _CloseArchive(ArchiveHandle *AH)
static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
const char * strerror(int errnum)
Definition: strerror.c:19
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
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
bool isValidTarHeader ( char *  header)

Definition at line 1059 of file pg_backup_tar.c.

References read_tar_number(), and tarChecksum().

Referenced by _discoverArchiveFormat().

1060 {
1061  int sum;
1062  int chk = tarChecksum(header);
1063 
1064  sum = read_tar_number(&header[148], 8);
1065 
1066  if (sum != chk)
1067  return false;
1068 
1069  /* POSIX tar format */
1070  if (memcmp(&header[257], "ustar\0", 6) == 0 &&
1071  memcmp(&header[263], "00", 2) == 0)
1072  return true;
1073  /* GNU tar format */
1074  if (memcmp(&header[257], "ustar \0", 8) == 0)
1075  return true;
1076  /* not-quite-POSIX format written by pre-9.3 pg_dump */
1077  if (memcmp(&header[257], "ustar00\0", 8) == 0)
1078  return true;
1079 
1080  return false;
1081 }
uint64 read_tar_number(const char *s, int len)
Definition: tar.c:56
int tarChecksum(char *header)
Definition: tar.c:88
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
static void tarClose ( ArchiveHandle AH,
TAR_MEMBER TH 
)
static

Definition at line 449 of file pg_backup_tar.c.

References _tarAddFile(), _archiveHandle::compression, exit_horribly(), free, GZCLOSE, TAR_MEMBER::mode, modulename, TAR_MEMBER::nFH, TAR_MEMBER::targetFile, and TAR_MEMBER::zFH.

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

450 {
451  /*
452  * Close the GZ file since we dup'd. This will flush the buffers.
453  */
454  if (AH->compression != 0)
455  if (GZCLOSE(th->zFH) != 0)
456  exit_horribly(modulename, "could not close tar member\n");
457 
458  if (th->mode == 'w')
459  _tarAddFile(AH, th); /* This will close the temp file */
460 
461  /*
462  * else Nothing to do for normal read since we don't dup() normal file
463  * handle, and we don't use temp files.
464  */
465 
466  if (th->targetFile)
467  free(th->targetFile);
468 
469  th->nFH = NULL;
470  th->zFH = NULL;
471 }
static const char * modulename
#define free(a)
Definition: header.h:65
#define GZCLOSE(fh)
static int static void _tarAddFile(ArchiveHandle *AH, TAR_MEMBER *th)
void exit_horribly(const char *modulename, const char *fmt,...)
static TAR_MEMBER * tarOpen ( ArchiveHandle AH,
const char *  filename,
char  mode 
)
static

Definition at line 332 of file pg_backup_tar.c.

References _tarPositionTo(), _archiveHandle::compression, exit_horribly(), fd(), _archiveHandle::formatData, free, modulename, name, pg_malloc0(), pg_strdup(), S_IRWXG, S_IRWXO, strerror(), lclContext::tarFH, and tm.

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

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

Definition at line 1027 of file pg_backup_tar.c.

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

Referenced by _CloseArchive(), and _StartBlob().

1028 {
1029  char *p;
1030  size_t len = 128; /* initial assumption about buffer size */
1031  size_t cnt;
1032 
1033  for (;;)
1034  {
1035  va_list args;
1036 
1037  /* Allocate work buffer. */
1038  p = (char *) pg_malloc(len);
1039 
1040  /* Try to format the data. */
1041  va_start(args, fmt);
1042  cnt = pvsnprintf(p, len, fmt, args);
1043  va_end(args);
1044 
1045  if (cnt < len)
1046  break; /* success */
1047 
1048  /* Release buffer and loop around to try again with larger len. */
1049  free(p);
1050  len = cnt;
1051  }
1052 
1053  cnt = tarWrite(p, cnt, th);
1054  free(p);
1055  return (int) cnt;
1056 }
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:104
#define free(a)
Definition: header.h:65
static size_t tarWrite(const void *buf, size_t len, TAR_MEMBER *th)
static size_t tarRead ( void *  buf,
size_t  len,
TAR_MEMBER th 
)
static

Definition at line 590 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().

591 {
592  size_t res;
593 
594  if (th->pos + len > th->fileLen)
595  len = th->fileLen - th->pos;
596 
597  if (len <= 0)
598  return 0;
599 
600  res = _tarReadRaw(th->AH, buf, len, th, NULL);
601 
602  th->pos += res;
603 
604  return res;
605 }
ArchiveHandle * AH
Definition: pg_backup_tar.c:80
static char * buf
Definition: pg_test_fsync.c:67
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)
static size_t tarWrite ( const void *  buf,
size_t  len,
TAR_MEMBER th 
)
static

Definition at line 608 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().

609 {
610  size_t res;
611 
612  if (th->zFH != NULL)
613  res = GZWRITE(buf, 1, len, th->zFH);
614  else
615  res = fwrite(buf, 1, len, th->nFH);
616 
617  th->pos += res;
618  return res;
619 }
FILE * zFH
Definition: pg_backup_tar.c:71
static char * buf
Definition: pg_test_fsync.c:67
#define GZWRITE(p, s, n, fh)
FILE * nFH
Definition: pg_backup_tar.c:73
pgoff_t pos
Definition: pg_backup_tar.c:78

Variable Documentation

const char* modulename = gettext_noop("tar archiver")
static