PostgreSQL Source Code  git master
bbstreamer_tar.c File Reference
#include "postgres_fe.h"
#include <time.h>
#include "bbstreamer.h"
#include "common/logging.h"
#include "pgtar.h"
Include dependency graph for bbstreamer_tar.c:

Go to the source code of this file.

Data Structures

struct  bbstreamer_tar_parser
 
struct  bbstreamer_tar_archiver
 

Typedefs

typedef struct bbstreamer_tar_parser bbstreamer_tar_parser
 
typedef struct bbstreamer_tar_archiver bbstreamer_tar_archiver
 

Functions

static void bbstreamer_tar_parser_content (bbstreamer *streamer, bbstreamer_member *member, const char *data, int len, bbstreamer_archive_context context)
 
static void bbstreamer_tar_parser_finalize (bbstreamer *streamer)
 
static void bbstreamer_tar_parser_free (bbstreamer *streamer)
 
static bool bbstreamer_tar_header (bbstreamer_tar_parser *mystreamer)
 
static void bbstreamer_tar_archiver_content (bbstreamer *streamer, bbstreamer_member *member, const char *data, int len, bbstreamer_archive_context context)
 
static void bbstreamer_tar_archiver_finalize (bbstreamer *streamer)
 
static void bbstreamer_tar_archiver_free (bbstreamer *streamer)
 
static void bbstreamer_tar_terminator_content (bbstreamer *streamer, bbstreamer_member *member, const char *data, int len, bbstreamer_archive_context context)
 
static void bbstreamer_tar_terminator_finalize (bbstreamer *streamer)
 
static void bbstreamer_tar_terminator_free (bbstreamer *streamer)
 
bbstreamerbbstreamer_tar_parser_new (bbstreamer *next)
 
bbstreamerbbstreamer_tar_archiver_new (bbstreamer *next)
 
bbstreamerbbstreamer_tar_terminator_new (bbstreamer *next)
 

Variables

const bbstreamer_ops bbstreamer_tar_parser_ops
 
const bbstreamer_ops bbstreamer_tar_archiver_ops
 
const bbstreamer_ops bbstreamer_tar_terminator_ops
 

Typedef Documentation

◆ bbstreamer_tar_archiver

◆ bbstreamer_tar_parser

Function Documentation

◆ bbstreamer_tar_archiver_content()

static void bbstreamer_tar_archiver_content ( bbstreamer streamer,
bbstreamer_member member,
const char *  data,
int  len,
bbstreamer_archive_context  context 
)
static

Definition at line 392 of file bbstreamer_tar.c.

396 {
397  bbstreamer_tar_archiver *mystreamer = (bbstreamer_tar_archiver *) streamer;
398  char buffer[2 * TAR_BLOCK_SIZE];
399 
400  Assert(context != BBSTREAMER_UNKNOWN);
401 
402  if (context == BBSTREAMER_MEMBER_HEADER && len != TAR_BLOCK_SIZE)
403  {
404  Assert(len == 0);
405 
406  /* Replace zero-length tar header with a newly constructed one. */
407  tarCreateHeader(buffer, member->pathname, NULL,
408  member->size, member->mode, member->uid, member->gid,
409  time(NULL));
410  data = buffer;
412 
413  /* Also make a note to replace padding, in case size changed. */
414  mystreamer->rearchive_member = true;
415  }
416  else if (context == BBSTREAMER_MEMBER_TRAILER &&
417  mystreamer->rearchive_member)
418  {
419  int pad_bytes = tarPaddingBytesRequired(member->size);
420 
421  /* Also replace padding, if we regenerated the header. */
422  memset(buffer, 0, pad_bytes);
423  data = buffer;
424  len = pad_bytes;
425 
426  /* Don't do this again unless we replace another header. */
427  mystreamer->rearchive_member = false;
428  }
429  else if (context == BBSTREAMER_ARCHIVE_TRAILER)
430  {
431  /* Trailer should always be two blocks of zero bytes. */
432  memset(buffer, 0, 2 * TAR_BLOCK_SIZE);
433  data = buffer;
434  len = 2 * TAR_BLOCK_SIZE;
435  }
436 
437  bbstreamer_content(streamer->bbs_next, member, data, len, context);
438 }
static void bbstreamer_content(bbstreamer *streamer, bbstreamer_member *member, const char *data, int len, bbstreamer_archive_context context)
Definition: bbstreamer.h:126
@ BBSTREAMER_ARCHIVE_TRAILER
Definition: bbstreamer.h:59
@ BBSTREAMER_MEMBER_HEADER
Definition: bbstreamer.h:56
@ BBSTREAMER_MEMBER_TRAILER
Definition: bbstreamer.h:58
@ BBSTREAMER_UNKNOWN
Definition: bbstreamer.h:55
Assert(fmt[strlen(fmt) - 1] !='\n')
const void size_t len
const void * data
static size_t tarPaddingBytesRequired(size_t len)
Definition: pgtar.h:40
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
#define TAR_BLOCK_SIZE
Definition: pgtar.h:17
char pathname[MAXPGPATH]
Definition: bbstreamer.h:72
bbstreamer * bbs_next
Definition: bbstreamer.h:101

References Assert(), bbstreamer::bbs_next, BBSTREAMER_ARCHIVE_TRAILER, bbstreamer_content(), BBSTREAMER_MEMBER_HEADER, BBSTREAMER_MEMBER_TRAILER, BBSTREAMER_UNKNOWN, data, bbstreamer_member::gid, len, bbstreamer_member::mode, bbstreamer_member::pathname, bbstreamer_tar_archiver::rearchive_member, bbstreamer_member::size, TAR_BLOCK_SIZE, tarCreateHeader(), tarPaddingBytesRequired(), and bbstreamer_member::uid.

◆ bbstreamer_tar_archiver_finalize()

static void bbstreamer_tar_archiver_finalize ( bbstreamer streamer)
static

Definition at line 444 of file bbstreamer_tar.c.

445 {
446  bbstreamer_finalize(streamer->bbs_next);
447 }
static void bbstreamer_finalize(bbstreamer *streamer)
Definition: bbstreamer.h:136

References bbstreamer::bbs_next, and bbstreamer_finalize().

◆ bbstreamer_tar_archiver_free()

static void bbstreamer_tar_archiver_free ( bbstreamer streamer)
static

Definition at line 453 of file bbstreamer_tar.c.

454 {
455  bbstreamer_free(streamer->bbs_next);
456  pfree(streamer);
457 }
static void bbstreamer_free(bbstreamer *streamer)
Definition: bbstreamer.h:144
void pfree(void *pointer)
Definition: mcxt.c:1252

References bbstreamer::bbs_next, bbstreamer_free(), and pfree().

◆ bbstreamer_tar_archiver_new()

bbstreamer* bbstreamer_tar_archiver_new ( bbstreamer next)

Definition at line 358 of file bbstreamer_tar.c.

359 {
360  bbstreamer_tar_archiver *streamer;
361 
362  streamer = palloc0(sizeof(bbstreamer_tar_archiver));
363  *((const bbstreamer_ops **) &streamer->base.bbs_ops) =
365  streamer->base.bbs_next = next;
366 
367  return &streamer->base;
368 }
const bbstreamer_ops bbstreamer_tar_archiver_ops
static int32 next
Definition: blutils.c:219
void * palloc0(Size size)
Definition: mcxt.c:1176
const bbstreamer_ops * bbs_ops
Definition: bbstreamer.h:100

References bbstreamer_tar_archiver::base, bbstreamer::bbs_next, bbstreamer::bbs_ops, bbstreamer_tar_archiver_ops, next, and palloc0().

Referenced by CreateBackupStreamer().

◆ bbstreamer_tar_header()

static bool bbstreamer_tar_header ( bbstreamer_tar_parser mystreamer)
static

Definition at line 261 of file bbstreamer_tar.c.

262 {
263  bool has_nonzero_byte = false;
264  int i;
265  bbstreamer_member *member = &mystreamer->member;
266  char *buffer = mystreamer->base.bbs_buffer.data;
267 
268  Assert(mystreamer->base.bbs_buffer.len == TAR_BLOCK_SIZE);
269 
270  /* Check whether we've got a block of all zero bytes. */
271  for (i = 0; i < TAR_BLOCK_SIZE; ++i)
272  {
273  if (buffer[i] != '\0')
274  {
275  has_nonzero_byte = true;
276  break;
277  }
278  }
279 
280  /*
281  * If the entire block was zeros, this is the end of the archive, not the
282  * start of the next file.
283  */
284  if (!has_nonzero_byte)
285  return false;
286 
287  /*
288  * Parse key fields out of the header.
289  *
290  * FIXME: It's terrible that we use hard-coded values here instead of some
291  * more principled approach. It's been like this for a long time, but we
292  * ought to do better.
293  */
294  strlcpy(member->pathname, &buffer[0], MAXPGPATH);
295  if (member->pathname[0] == '\0')
296  pg_fatal("tar member has empty name");
297  member->size = read_tar_number(&buffer[124], 12);
298  member->mode = read_tar_number(&buffer[100], 8);
299  member->uid = read_tar_number(&buffer[108], 8);
300  member->gid = read_tar_number(&buffer[116], 8);
301  member->is_directory = (buffer[156] == '5');
302  member->is_link = (buffer[156] == '2');
303  if (member->is_link)
304  strlcpy(member->linktarget, &buffer[157], 100);
305 
306  /* Compute number of padding bytes. */
307  mystreamer->pad_bytes_expected = tarPaddingBytesRequired(member->size);
308 
309  /* Forward the entire header to the next bbstreamer. */
310  bbstreamer_content(mystreamer->base.bbs_next, member,
311  buffer, TAR_BLOCK_SIZE,
313 
314  return true;
315 }
int i
Definition: isn.c:73
#define pg_fatal(...)
#define MAXPGPATH
uint64 read_tar_number(const char *s, int len)
Definition: tar.c:58
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
char linktarget[MAXPGPATH]
Definition: bbstreamer.h:79
bbstreamer_member member
StringInfoData bbs_buffer
Definition: bbstreamer.h:102

References Assert(), bbstreamer_tar_parser::base, bbstreamer::bbs_buffer, bbstreamer::bbs_next, bbstreamer_content(), BBSTREAMER_MEMBER_HEADER, StringInfoData::data, bbstreamer_member::gid, i, bbstreamer_member::is_directory, bbstreamer_member::is_link, StringInfoData::len, bbstreamer_member::linktarget, MAXPGPATH, bbstreamer_tar_parser::member, bbstreamer_member::mode, bbstreamer_tar_parser::pad_bytes_expected, bbstreamer_member::pathname, pg_fatal, read_tar_number(), bbstreamer_member::size, strlcpy(), TAR_BLOCK_SIZE, tarPaddingBytesRequired(), and bbstreamer_member::uid.

Referenced by bbstreamer_tar_parser_content().

◆ bbstreamer_tar_parser_content()

static void bbstreamer_tar_parser_content ( bbstreamer streamer,
bbstreamer_member member,
const char *  data,
int  len,
bbstreamer_archive_context  context 
)
static

Definition at line 111 of file bbstreamer_tar.c.

114 {
115  bbstreamer_tar_parser *mystreamer = (bbstreamer_tar_parser *) streamer;
116  size_t nbytes;
117 
118  /* Expect unparsed input. */
119  Assert(member == NULL);
120  Assert(context == BBSTREAMER_UNKNOWN);
121 
122  while (len > 0)
123  {
124  switch (mystreamer->next_context)
125  {
127 
128  /*
129  * If we're expecting an archive member header, accumulate a
130  * full block of data before doing anything further.
131  */
132  if (!bbstreamer_buffer_until(streamer, &data, &len,
134  return;
135 
136  /*
137  * Now we can process the header and get ready to process the
138  * file contents; however, we might find out that what we
139  * thought was the next file header is actually the start of
140  * the archive trailer. Switch modes accordingly.
141  */
142  if (bbstreamer_tar_header(mystreamer))
143  {
144  if (mystreamer->member.size == 0)
145  {
146  /* No content; trailer is zero-length. */
147  bbstreamer_content(mystreamer->base.bbs_next,
148  &mystreamer->member,
149  NULL, 0,
151 
152  /* Expect next header. */
154  }
155  else
156  {
157  /* Expect contents. */
159  }
160  mystreamer->base.bbs_buffer.len = 0;
161  mystreamer->file_bytes_sent = 0;
162  }
163  else
165  break;
166 
168 
169  /*
170  * Send as much content as we have, but not more than the
171  * remaining file length.
172  */
173  Assert(mystreamer->file_bytes_sent < mystreamer->member.size);
174  nbytes = mystreamer->member.size - mystreamer->file_bytes_sent;
175  nbytes = Min(nbytes, len);
176  Assert(nbytes > 0);
177  bbstreamer_content(mystreamer->base.bbs_next,
178  &mystreamer->member,
179  data, nbytes,
181  mystreamer->file_bytes_sent += nbytes;
182  data += nbytes;
183  len -= nbytes;
184 
185  /*
186  * If we've not yet sent the whole file, then there's more
187  * content to come; otherwise, it's time to expect the file
188  * trailer.
189  */
190  Assert(mystreamer->file_bytes_sent <= mystreamer->member.size);
191  if (mystreamer->file_bytes_sent == mystreamer->member.size)
192  {
193  if (mystreamer->pad_bytes_expected == 0)
194  {
195  /* Trailer is zero-length. */
196  bbstreamer_content(mystreamer->base.bbs_next,
197  &mystreamer->member,
198  NULL, 0,
200 
201  /* Expect next header. */
203  }
204  else
205  {
206  /* Trailer is not zero-length. */
208  }
209  mystreamer->base.bbs_buffer.len = 0;
210  }
211  break;
212 
214 
215  /*
216  * If we're expecting an archive member trailer, accumulate
217  * the expected number of padding bytes before sending
218  * anything onward.
219  */
220  if (!bbstreamer_buffer_until(streamer, &data, &len,
221  mystreamer->pad_bytes_expected))
222  return;
223 
224  /* OK, now we can send it. */
225  bbstreamer_content(mystreamer->base.bbs_next,
226  &mystreamer->member,
227  data, mystreamer->pad_bytes_expected,
229 
230  /* Expect next file header. */
232  mystreamer->base.bbs_buffer.len = 0;
233  break;
234 
236 
237  /*
238  * We've seen an end-of-archive indicator, so anything more is
239  * buffered and sent as part of the archive trailer. But we
240  * don't expect more than 2 blocks.
241  */
242  bbstreamer_buffer_bytes(streamer, &data, &len, len);
243  if (len > 2 * TAR_BLOCK_SIZE)
244  pg_fatal("tar file trailer exceeds 2 blocks");
245  return;
246 
247  default:
248  /* Shouldn't happen. */
249  pg_fatal("unexpected state while parsing tar archive");
250  }
251  }
252 }
static void bbstreamer_buffer_bytes(bbstreamer *streamer, const char **data, int *len, int nbytes)
Definition: bbstreamer.h:157
static bool bbstreamer_buffer_until(bbstreamer *streamer, const char **data, int *len, int target_bytes)
Definition: bbstreamer.h:175
@ BBSTREAMER_MEMBER_CONTENTS
Definition: bbstreamer.h:57
static bool bbstreamer_tar_header(bbstreamer_tar_parser *mystreamer)
#define Min(x, y)
Definition: c.h:976
bbstreamer_archive_context next_context

References Assert(), bbstreamer_tar_parser::base, bbstreamer::bbs_buffer, bbstreamer::bbs_next, BBSTREAMER_ARCHIVE_TRAILER, bbstreamer_buffer_bytes(), bbstreamer_buffer_until(), bbstreamer_content(), BBSTREAMER_MEMBER_CONTENTS, BBSTREAMER_MEMBER_HEADER, BBSTREAMER_MEMBER_TRAILER, bbstreamer_tar_header(), BBSTREAMER_UNKNOWN, data, bbstreamer_tar_parser::file_bytes_sent, StringInfoData::len, len, bbstreamer_tar_parser::member, Min, bbstreamer_tar_parser::next_context, bbstreamer_tar_parser::pad_bytes_expected, pg_fatal, bbstreamer_member::size, and TAR_BLOCK_SIZE.

◆ bbstreamer_tar_parser_finalize()

static void bbstreamer_tar_parser_finalize ( bbstreamer streamer)
static

Definition at line 321 of file bbstreamer_tar.c.

322 {
323  bbstreamer_tar_parser *mystreamer = (bbstreamer_tar_parser *) streamer;
324 
325  if (mystreamer->next_context != BBSTREAMER_ARCHIVE_TRAILER &&
326  (mystreamer->next_context != BBSTREAMER_MEMBER_HEADER ||
327  mystreamer->base.bbs_buffer.len > 0))
328  pg_fatal("COPY stream ended before last file was finished");
329 
330  /* Send the archive trailer, even if empty. */
331  bbstreamer_content(streamer->bbs_next, NULL,
332  streamer->bbs_buffer.data, streamer->bbs_buffer.len,
334 
335  /* Now finalize successor. */
336  bbstreamer_finalize(streamer->bbs_next);
337 }

References bbstreamer_tar_parser::base, bbstreamer::bbs_buffer, bbstreamer::bbs_next, BBSTREAMER_ARCHIVE_TRAILER, bbstreamer_content(), bbstreamer_finalize(), BBSTREAMER_MEMBER_HEADER, StringInfoData::data, StringInfoData::len, bbstreamer_tar_parser::next_context, and pg_fatal.

◆ bbstreamer_tar_parser_free()

static void bbstreamer_tar_parser_free ( bbstreamer streamer)
static

Definition at line 343 of file bbstreamer_tar.c.

344 {
345  pfree(streamer->bbs_buffer.data);
346  bbstreamer_free(streamer->bbs_next);
347 }

References bbstreamer::bbs_buffer, bbstreamer::bbs_next, bbstreamer_free(), StringInfoData::data, and pfree().

◆ bbstreamer_tar_parser_new()

bbstreamer* bbstreamer_tar_parser_new ( bbstreamer next)

Definition at line 93 of file bbstreamer_tar.c.

94 {
95  bbstreamer_tar_parser *streamer;
96 
97  streamer = palloc0(sizeof(bbstreamer_tar_parser));
98  *((const bbstreamer_ops **) &streamer->base.bbs_ops) =
100  streamer->base.bbs_next = next;
101  initStringInfo(&streamer->base.bbs_buffer);
103 
104  return &streamer->base;
105 }
const bbstreamer_ops bbstreamer_tar_parser_ops
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59

References bbstreamer_tar_parser::base, bbstreamer::bbs_buffer, bbstreamer::bbs_next, bbstreamer::bbs_ops, BBSTREAMER_MEMBER_HEADER, bbstreamer_tar_parser_ops, initStringInfo(), next, bbstreamer_tar_parser::next_context, and palloc0().

Referenced by CreateBackupStreamer().

◆ bbstreamer_tar_terminator_content()

static void bbstreamer_tar_terminator_content ( bbstreamer streamer,
bbstreamer_member member,
const char *  data,
int  len,
bbstreamer_archive_context  context 
)
static

Definition at line 480 of file bbstreamer_tar.c.

484 {
485  /* Expect unparsed input. */
486  Assert(member == NULL);
487  Assert(context == BBSTREAMER_UNKNOWN);
488 
489  /* Just forward it. */
490  bbstreamer_content(streamer->bbs_next, member, data, len, context);
491 }

References Assert(), bbstreamer::bbs_next, bbstreamer_content(), BBSTREAMER_UNKNOWN, data, and len.

◆ bbstreamer_tar_terminator_finalize()

static void bbstreamer_tar_terminator_finalize ( bbstreamer streamer)
static

Definition at line 498 of file bbstreamer_tar.c.

499 {
500  char buffer[2 * TAR_BLOCK_SIZE];
501 
502  memset(buffer, 0, 2 * TAR_BLOCK_SIZE);
503  bbstreamer_content(streamer->bbs_next, NULL, buffer,
505  bbstreamer_finalize(streamer->bbs_next);
506 }

References bbstreamer::bbs_next, bbstreamer_content(), bbstreamer_finalize(), BBSTREAMER_UNKNOWN, and TAR_BLOCK_SIZE.

◆ bbstreamer_tar_terminator_free()

static void bbstreamer_tar_terminator_free ( bbstreamer streamer)
static

Definition at line 512 of file bbstreamer_tar.c.

513 {
514  bbstreamer_free(streamer->bbs_next);
515  pfree(streamer);
516 }

References bbstreamer::bbs_next, bbstreamer_free(), and pfree().

◆ bbstreamer_tar_terminator_new()

bbstreamer* bbstreamer_tar_terminator_new ( bbstreamer next)

Definition at line 464 of file bbstreamer_tar.c.

465 {
466  bbstreamer *streamer;
467 
468  streamer = palloc0(sizeof(bbstreamer));
469  *((const bbstreamer_ops **) &streamer->bbs_ops) =
471  streamer->bbs_next = next;
472 
473  return streamer;
474 }
const bbstreamer_ops bbstreamer_tar_terminator_ops

References bbstreamer::bbs_next, bbstreamer::bbs_ops, bbstreamer_tar_terminator_ops, next, and palloc0().

Referenced by CreateBackupStreamer().

Variable Documentation

◆ bbstreamer_tar_archiver_ops

const bbstreamer_ops bbstreamer_tar_archiver_ops
Initial value:
= {
}
static void bbstreamer_tar_archiver_finalize(bbstreamer *streamer)
static void bbstreamer_tar_archiver_free(bbstreamer *streamer)
static void bbstreamer_tar_archiver_content(bbstreamer *streamer, bbstreamer_member *member, const char *data, int len, bbstreamer_archive_context context)

Definition at line 66 of file bbstreamer_tar.c.

Referenced by bbstreamer_tar_archiver_new().

◆ bbstreamer_tar_parser_ops

const bbstreamer_ops bbstreamer_tar_parser_ops
Initial value:
= {
}
static void bbstreamer_tar_parser_content(bbstreamer *streamer, bbstreamer_member *member, const char *data, int len, bbstreamer_archive_context context)
static void bbstreamer_tar_parser_finalize(bbstreamer *streamer)
static void bbstreamer_tar_parser_free(bbstreamer *streamer)

Definition at line 53 of file bbstreamer_tar.c.

Referenced by bbstreamer_tar_parser_new().

◆ bbstreamer_tar_terminator_ops

const bbstreamer_ops bbstreamer_tar_terminator_ops
Initial value:
= {
}
static void bbstreamer_tar_terminator_free(bbstreamer *streamer)
static void bbstreamer_tar_terminator_content(bbstreamer *streamer, bbstreamer_member *member, const char *data, int len, bbstreamer_archive_context context)
static void bbstreamer_tar_terminator_finalize(bbstreamer *streamer)

Definition at line 79 of file bbstreamer_tar.c.

Referenced by bbstreamer_tar_terminator_new().