Skip to content

Commit 25ebddb

Browse files
author
Michael Paquier
committed
Update CRC32 algorithm to CRC32C
This is to be compatible with Postgres >= 9.5 that switched to a more performant algorithm. Per reminder from Zhuravlev Uriy.
1 parent 55db450 commit 25ebddb

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

data.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ doDeflate(z_stream *zp, size_t in_size, size_t out_size, void *inbuf,
6868
}
6969

7070
/* update CRC */
71-
COMP_CRC32(*crc, outbuf, out_size - zp->avail_out);
71+
COMP_CRC32C(*crc, outbuf, out_size - zp->avail_out);
7272

7373
*write_size += out_size - zp->avail_out;
7474

@@ -146,7 +146,7 @@ doInflate(z_stream *zp, size_t in_size, size_t out_size,void *inbuf,
146146
}
147147

148148
/* update CRC */
149-
COMP_CRC32(*crc, outbuf, out_size - zp->avail_out);
149+
COMP_CRC32C(*crc, outbuf, out_size - zp->avail_out);
150150

151151
return status;
152152
}
@@ -216,7 +216,7 @@ backup_data_file(const char *from_root, const char *to_root,
216216
z_stream z;
217217
char outbuf[zlibOutSize];
218218
#endif
219-
INIT_CRC32(crc);
219+
INIT_CRC32C(crc);
220220

221221
/* reset size summary */
222222
file->read_size = 0;
@@ -226,7 +226,7 @@ backup_data_file(const char *from_root, const char *to_root,
226226
in = fopen(file->path, "r");
227227
if (in == NULL)
228228
{
229-
FIN_CRC32(crc);
229+
FIN_CRC32C(crc);
230230
file->crc = crc;
231231

232232
/* maybe vanished, it's not error */
@@ -338,9 +338,9 @@ backup_data_file(const char *from_root, const char *to_root,
338338
}
339339

340340
/* update CRC */
341-
COMP_CRC32(crc, &header, sizeof(header));
342-
COMP_CRC32(crc, page.data, header.hole_offset);
343-
COMP_CRC32(crc, page.data + upper_offset, upper_length);
341+
COMP_CRC32C(crc, &header, sizeof(header));
342+
COMP_CRC32C(crc, page.data, header.hole_offset);
343+
COMP_CRC32C(crc, page.data + upper_offset, upper_length);
344344

345345
file->write_size += sizeof(header) + read_len - header.hole_length;
346346
}
@@ -393,7 +393,7 @@ backup_data_file(const char *from_root, const char *to_root,
393393
_("can't write at block %u of \"%s\": %s"),
394394
blknum, to_path, strerror(errno_tmp));
395395
}
396-
COMP_CRC32(crc, &header, sizeof(header));
396+
COMP_CRC32C(crc, &header, sizeof(header));
397397
file->write_size += sizeof(header);
398398
}
399399
}
@@ -418,7 +418,7 @@ backup_data_file(const char *from_root, const char *to_root,
418418
blknum, to_path, strerror(errno_tmp));
419419
}
420420

421-
COMP_CRC32(crc, page.data, read_len);
421+
COMP_CRC32C(crc, page.data, read_len);
422422
file->write_size += read_len;
423423
}
424424

@@ -462,7 +462,7 @@ backup_data_file(const char *from_root, const char *to_root,
462462
fclose(out);
463463

464464
/* finish CRC calculation and store into pgFile */
465-
FIN_CRC32(crc);
465+
FIN_CRC32C(crc);
466466
file->crc = crc;
467467

468468
/* Treat empty file as not-datafile */
@@ -553,7 +553,7 @@ restore_data_file(const char *from_root,
553553
if (inflateInit(&z) != Z_OK)
554554
elog(ERROR_SYSTEM, _("can't initialize compression library: %s"),
555555
z.msg);
556-
INIT_CRC32(crc);
556+
INIT_CRC32C(crc);
557557
read_size = 0;
558558
}
559559
#endif
@@ -700,7 +700,7 @@ copy_file(const char *from_root, const char *to_root, pgFile *file,
700700
char outbuf[zlibOutSize];
701701
char inbuf[zlibInSize];
702702
#endif
703-
INIT_CRC32(crc);
703+
INIT_CRC32C(crc);
704704

705705
/* reset size summary */
706706
file->read_size = 0;
@@ -710,7 +710,7 @@ copy_file(const char *from_root, const char *to_root, pgFile *file,
710710
in = fopen(file->path, "r");
711711
if (in == NULL)
712712
{
713-
FIN_CRC32(crc);
713+
FIN_CRC32C(crc);
714714
file->crc = crc;
715715

716716
/* maybe deleted, it's not error */
@@ -825,7 +825,7 @@ copy_file(const char *from_root, const char *to_root, pgFile *file,
825825
strerror(errno_tmp));
826826
}
827827
/* update CRC */
828-
COMP_CRC32(crc, buf, read_len);
828+
COMP_CRC32C(crc, buf, read_len);
829829

830830
file->write_size += sizeof(buf);
831831
file->read_size += sizeof(buf);
@@ -862,7 +862,7 @@ copy_file(const char *from_root, const char *to_root, pgFile *file,
862862
strerror(errno_tmp));
863863
}
864864
/* update CRC */
865-
COMP_CRC32(crc, buf, read_len);
865+
COMP_CRC32C(crc, buf, read_len);
866866

867867
file->write_size += read_len;
868868
}
@@ -900,7 +900,7 @@ copy_file(const char *from_root, const char *to_root, pgFile *file,
900900

901901
#endif
902902
/* finish CRC calculation and store into pgFile */
903-
FIN_CRC32(crc);
903+
FIN_CRC32C(crc);
904904
file->crc = crc;
905905

906906
/* update file permission */

dir.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,20 @@ pgFileGetCRC(pgFile *file)
141141
file->path, strerror(errno));
142142

143143
/* calc CRC of backup file */
144-
INIT_CRC32(crc);
144+
INIT_CRC32C(crc);
145145
while ((len = fread(buf, 1, sizeof(buf), fp)) == sizeof(buf))
146146
{
147147
if (interrupted)
148148
elog(ERROR_INTERRUPTED, _("interrupted during CRC calculation"));
149-
COMP_CRC32(crc, buf, len);
149+
COMP_CRC32C(crc, buf, len);
150150
}
151151
errno_tmp = errno;
152152
if (!feof(fp))
153153
elog(WARNING, _("can't read \"%s\": %s"), file->path,
154154
strerror(errno_tmp));
155155
if (len > 0)
156-
COMP_CRC32(crc, buf, len);
157-
FIN_CRC32(crc);
156+
COMP_CRC32C(crc, buf, len);
157+
FIN_CRC32C(crc);
158158

159159
fclose(fp);
160160

restore.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,13 @@ get_current_timeline(void)
679679
close(fd);
680680

681681
/* Check the CRC. */
682-
INIT_CRC32(crc);
683-
COMP_CRC32(crc,
682+
INIT_CRC32C(crc);
683+
COMP_CRC32C(crc,
684684
(char *) &ControlFile,
685685
offsetof(ControlFileData, crc));
686-
FIN_CRC32(crc);
686+
FIN_CRC32C(crc);
687687

688-
if (!EQ_CRC32(crc, ControlFile.crc))
688+
if (!EQ_CRC32C(crc, ControlFile.crc))
689689
{
690690
elog(WARNING, _("Calculated CRC checksum does not match value stored in file.\n"
691691
"Either the file is corrupt, or it has a different layout than this program\n"

0 commit comments

Comments
 (0)