Skip to content

Commit 2961c97

Browse files
committed
Assorted cleanup of tar-related code.
Introduce TAR_BLOCK_SIZE and replace many instances of 512 with the new constant. Introduce function tarPaddingBytesRequired and use it to replace numerous repetitions of (x + 511) & ~511. Add preprocessor guards against multiple inclusion to pgtar.h. Reformat the prototype for tarCreateHeader so it doesn't extend beyond 80 characters. Discussion: http://postgr.es/m/CA+TgmobWbfReO9-XFk8urR1K4wTNwqoHx_v56t7=T8KaiEoKNw@mail.gmail.com
1 parent e532b1d commit 2961c97

File tree

5 files changed

+94
-59
lines changed

5 files changed

+94
-59
lines changed

src/backend/replication/basebackup.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,11 @@ perform_base_backup(basebackup_options *opt)
665665
errmsg("unexpected WAL file size \"%s\"", walFileName)));
666666
}
667667

668-
/* wal_segment_size is a multiple of 512, so no need for padding */
668+
/*
669+
* wal_segment_size is a multiple of TAR_BLOCK_SIZE, so no need
670+
* for padding.
671+
*/
672+
Assert(wal_segment_size % TAR_BLOCK_SIZE == 0);
669673

670674
FreeFile(fp);
671675

@@ -1118,11 +1122,11 @@ sendFileWithContent(const char *filename, const char *content,
11181122
pq_putmessage('d', content, len);
11191123
update_basebackup_progress(len);
11201124

1121-
/* Pad to 512 byte boundary, per tar format requirements */
1122-
pad = ((len + 511) & ~511) - len;
1125+
/* Pad to a multiple of the tar block size. */
1126+
pad = tarPaddingBytesRequired(len);
11231127
if (pad > 0)
11241128
{
1125-
char buf[512];
1129+
char buf[TAR_BLOCK_SIZE];
11261130

11271131
MemSet(buf, 0, pad);
11281132
pq_putmessage('d', buf, pad);
@@ -1491,9 +1495,14 @@ sendDir(const char *path, int basepathlen, bool sizeonly, List *tablespaces,
14911495

14921496
if (sent || sizeonly)
14931497
{
1494-
/* Add size, rounded up to 512byte block */
1495-
size += ((statbuf.st_size + 511) & ~511);
1496-
size += 512; /* Size of the header of the file */
1498+
/* Add size. */
1499+
size += statbuf.st_size;
1500+
1501+
/* Pad to a multiple of the tar block size. */
1502+
size += tarPaddingBytesRequired(statbuf.st_size);
1503+
1504+
/* Size of the header for the file. */
1505+
size += TAR_BLOCK_SIZE;
14971506
}
14981507
}
14991508
else
@@ -1784,11 +1793,11 @@ sendFile(const char *readfilename, const char *tarfilename,
17841793
}
17851794

17861795
/*
1787-
* Pad to 512 byte boundary, per tar format requirements. (This small
1796+
* Pad to a block boundary, per tar format requirements. (This small
17881797
* piece of data is probably not worth throttling, and is not checksummed
17891798
* because it's not actually part of the file.)
17901799
*/
1791-
pad = ((len + 511) & ~511) - len;
1800+
pad = tarPaddingBytesRequired(len);
17921801
if (pad > 0)
17931802
{
17941803
MemSet(buf, 0, pad);
@@ -1822,7 +1831,7 @@ static int64
18221831
_tarWriteHeader(const char *filename, const char *linktarget,
18231832
struct stat *statbuf, bool sizeonly)
18241833
{
1825-
char h[512];
1834+
char h[TAR_BLOCK_SIZE];
18261835
enum tarError rc;
18271836

18281837
if (!sizeonly)

src/bin/pg_basebackup/pg_basebackup.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ typedef struct WriteTarState
6262
int tablespacenum;
6363
char filename[MAXPGPATH];
6464
FILE *tarfile;
65-
char tarhdr[512];
65+
char tarhdr[TAR_BLOCK_SIZE];
6666
bool basetablespace;
6767
bool in_tarhdr;
6868
bool skip_file;
@@ -1024,7 +1024,7 @@ writeTarData(WriteTarState *state, char *buf, int r)
10241024
static void
10251025
ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
10261026
{
1027-
char zerobuf[1024];
1027+
char zerobuf[TAR_BLOCK_SIZE * 2];
10281028
WriteTarState state;
10291029

10301030
memset(&state, 0, sizeof(state));
@@ -1169,7 +1169,7 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
11691169

11701170
if (state.basetablespace && writerecoveryconf)
11711171
{
1172-
char header[512];
1172+
char header[TAR_BLOCK_SIZE];
11731173

11741174
/*
11751175
* If postgresql.auto.conf has not been found in the streamed data,
@@ -1188,7 +1188,7 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
11881188
pg_file_create_mode, 04000, 02000,
11891189
time(NULL));
11901190

1191-
padding = ((recoveryconfcontents->len + 511) & ~511) - recoveryconfcontents->len;
1191+
padding = tarPaddingBytesRequired(recoveryconfcontents->len);
11921192

11931193
writeTarData(&state, header, sizeof(header));
11941194
writeTarData(&state, recoveryconfcontents->data,
@@ -1224,7 +1224,7 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
12241224
*/
12251225
if (strcmp(basedir, "-") == 0 && manifest)
12261226
{
1227-
char header[512];
1227+
char header[TAR_BLOCK_SIZE];
12281228
PQExpBufferData buf;
12291229

12301230
initPQExpBuffer(&buf);
@@ -1242,7 +1242,7 @@ ReceiveTarFile(PGconn *conn, PGresult *res, int rownum)
12421242
termPQExpBuffer(&buf);
12431243
}
12441244

1245-
/* 2 * 512 bytes empty data at end of file */
1245+
/* 2 * TAR_BLOCK_SIZE bytes empty data at end of file */
12461246
writeTarData(&state, zerobuf, sizeof(zerobuf));
12471247

12481248
#ifdef HAVE_LIBZ
@@ -1303,9 +1303,9 @@ ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data)
13031303
*
13041304
* To do this, we have to process the individual files inside the TAR
13051305
* stream. The stream consists of a header and zero or more chunks,
1306-
* all 512 bytes long. The stream from the server is broken up into
1307-
* smaller pieces, so we have to track the size of the files to find
1308-
* the next header structure.
1306+
* each with a length equal to TAR_BLOCK_SIZE. The stream from the
1307+
* server is broken up into smaller pieces, so we have to track the
1308+
* size of the files to find the next header structure.
13091309
*/
13101310
int rr = r;
13111311
int pos = 0;
@@ -1318,17 +1318,17 @@ ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data)
13181318
* We're currently reading a header structure inside the TAR
13191319
* stream, i.e. the file metadata.
13201320
*/
1321-
if (state->tarhdrsz < 512)
1321+
if (state->tarhdrsz < TAR_BLOCK_SIZE)
13221322
{
13231323
/*
13241324
* Copy the header structure into tarhdr in case the
1325-
* header is not aligned to 512 bytes or it's not returned
1326-
* in whole by the last PQgetCopyData call.
1325+
* header is not aligned properly or it's not returned in
1326+
* whole by the last PQgetCopyData call.
13271327
*/
13281328
int hdrleft;
13291329
int bytes2copy;
13301330

1331-
hdrleft = 512 - state->tarhdrsz;
1331+
hdrleft = TAR_BLOCK_SIZE - state->tarhdrsz;
13321332
bytes2copy = (rr > hdrleft ? hdrleft : rr);
13331333

13341334
memcpy(&state->tarhdr[state->tarhdrsz], copybuf + pos,
@@ -1361,14 +1361,14 @@ ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data)
13611361

13621362
state->filesz = read_tar_number(&state->tarhdr[124], 12);
13631363
state->file_padding_len =
1364-
((state->filesz + 511) & ~511) - state->filesz;
1364+
tarPaddingBytesRequired(state->filesz);
13651365

13661366
if (state->is_recovery_guc_supported &&
13671367
state->is_postgresql_auto_conf &&
13681368
writerecoveryconf)
13691369
{
13701370
/* replace tar header */
1371-
char header[512];
1371+
char header[TAR_BLOCK_SIZE];
13721372

13731373
tarCreateHeader(header, "postgresql.auto.conf", NULL,
13741374
state->filesz + recoveryconfcontents->len,
@@ -1388,7 +1388,7 @@ ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data)
13881388
* If we're not skipping the file, write the tar
13891389
* header unmodified.
13901390
*/
1391-
writeTarData(state, state->tarhdr, 512);
1391+
writeTarData(state, state->tarhdr, TAR_BLOCK_SIZE);
13921392
}
13931393
}
13941394

@@ -1425,15 +1425,15 @@ ReceiveTarCopyChunk(size_t r, char *copybuf, void *callback_data)
14251425
int padding;
14261426
int tailsize;
14271427

1428-
tailsize = (512 - state->file_padding_len) + recoveryconfcontents->len;
1429-
padding = ((tailsize + 511) & ~511) - tailsize;
1428+
tailsize = (TAR_BLOCK_SIZE - state->file_padding_len) + recoveryconfcontents->len;
1429+
padding = tarPaddingBytesRequired(tailsize);
14301430

14311431
writeTarData(state, recoveryconfcontents->data,
14321432
recoveryconfcontents->len);
14331433

14341434
if (padding)
14351435
{
1436-
char zerobuf[512];
1436+
char zerobuf[TAR_BLOCK_SIZE];
14371437

14381438
MemSet(zerobuf, 0, sizeof(zerobuf));
14391439
writeTarData(state, zerobuf, padding);
@@ -1551,12 +1551,12 @@ ReceiveTarAndUnpackCopyChunk(size_t r, char *copybuf, void *callback_data)
15511551
/*
15521552
* No current file, so this must be the header for a new file
15531553
*/
1554-
if (r != 512)
1554+
if (r != TAR_BLOCK_SIZE)
15551555
{
15561556
pg_log_error("invalid tar block header size: %zu", r);
15571557
exit(1);
15581558
}
1559-
totaldone += 512;
1559+
totaldone += TAR_BLOCK_SIZE;
15601560

15611561
state->current_len_left = read_tar_number(&copybuf[124], 12);
15621562

@@ -1566,10 +1566,10 @@ ReceiveTarAndUnpackCopyChunk(size_t r, char *copybuf, void *callback_data)
15661566
#endif
15671567

15681568
/*
1569-
* All files are padded up to 512 bytes
1569+
* All files are padded up to a multiple of TAR_BLOCK_SIZE
15701570
*/
15711571
state->current_padding =
1572-
((state->current_len_left + 511) & ~511) - state->current_len_left;
1572+
tarPaddingBytesRequired(state->current_len_left);
15731573

15741574
/*
15751575
* First part of header is zero terminated filename

src/bin/pg_basebackup/walmethods.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ typedef struct TarMethodFile
386386
{
387387
off_t ofs_start; /* Where does the *header* for this file start */
388388
off_t currpos;
389-
char header[512];
389+
char header[TAR_BLOCK_SIZE];
390390
char *pathname;
391391
size_t pad_to_size;
392392
} TarMethodFile;
@@ -625,7 +625,8 @@ tar_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_
625625
if (!tar_data->compression)
626626
{
627627
errno = 0;
628-
if (write(tar_data->fd, tar_data->currentfile->header, 512) != 512)
628+
if (write(tar_data->fd, tar_data->currentfile->header,
629+
TAR_BLOCK_SIZE) != TAR_BLOCK_SIZE)
629630
{
630631
save_errno = errno;
631632
pg_free(tar_data->currentfile);
@@ -639,7 +640,8 @@ tar_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_
639640
else
640641
{
641642
/* Write header through the zlib APIs but with no compression */
642-
if (!tar_write_compressed_data(tar_data->currentfile->header, 512, true))
643+
if (!tar_write_compressed_data(tar_data->currentfile->header,
644+
TAR_BLOCK_SIZE, true))
643645
return NULL;
644646

645647
/* Re-enable compression for the rest of the file */
@@ -665,7 +667,9 @@ tar_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_
665667
/* Uncompressed, so pad now */
666668
tar_write_padding_data(tar_data->currentfile, pad_to_size);
667669
/* Seek back to start */
668-
if (lseek(tar_data->fd, tar_data->currentfile->ofs_start + 512, SEEK_SET) != tar_data->currentfile->ofs_start + 512)
670+
if (lseek(tar_data->fd,
671+
tar_data->currentfile->ofs_start + TAR_BLOCK_SIZE,
672+
SEEK_SET) != tar_data->currentfile->ofs_start + TAR_BLOCK_SIZE)
669673
return NULL;
670674

671675
tar_data->currentfile->currpos = 0;
@@ -778,14 +782,14 @@ tar_close(Walfile f, WalCloseMethod method)
778782
}
779783

780784
/*
781-
* Get the size of the file, and pad the current data up to the nearest
782-
* 512 byte boundary.
785+
* Get the size of the file, and pad out to a multiple of the tar block
786+
* size.
783787
*/
784788
filesize = tar_get_current_pos(f);
785-
padding = ((filesize + 511) & ~511) - filesize;
789+
padding = tarPaddingBytesRequired(filesize);
786790
if (padding)
787791
{
788-
char zerobuf[512];
792+
char zerobuf[TAR_BLOCK_SIZE];
789793

790794
MemSet(zerobuf, 0, padding);
791795
if (tar_write(f, zerobuf, padding) != padding)
@@ -826,7 +830,7 @@ tar_close(Walfile f, WalCloseMethod method)
826830
if (!tar_data->compression)
827831
{
828832
errno = 0;
829-
if (write(tar_data->fd, tf->header, 512) != 512)
833+
if (write(tar_data->fd, tf->header, TAR_BLOCK_SIZE) != TAR_BLOCK_SIZE)
830834
{
831835
/* if write didn't set errno, assume problem is no disk space */
832836
if (errno == 0)
@@ -845,7 +849,8 @@ tar_close(Walfile f, WalCloseMethod method)
845849
}
846850

847851
/* Overwrite the header, assuming the size will be the same */
848-
if (!tar_write_compressed_data(tar_data->currentfile->header, 512, true))
852+
if (!tar_write_compressed_data(tar_data->currentfile->header,
853+
TAR_BLOCK_SIZE, true))
849854
return -1;
850855

851856
/* Turn compression back on */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy