Skip to content

Commit f533955

Browse files
Refactored calc_file_checksum()
1 parent 6d0cbfa commit f533955

File tree

6 files changed

+40
-81
lines changed

6 files changed

+40
-81
lines changed

src/data.c

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,75 +1418,13 @@ get_wal_file(const char *from_path, const char *to_path)
14181418
* but created in process of backup, such as stream XLOG files,
14191419
* PG_TABLESPACE_MAP_FILE and PG_BACKUP_LABEL_FILE.
14201420
*/
1421-
bool
1421+
void
14221422
calc_file_checksum(pgFile *file)
14231423
{
1424-
FILE *in;
1425-
size_t read_len = 0;
1426-
int errno_tmp;
1427-
char buf[BLCKSZ];
1428-
struct stat st;
1429-
pg_crc32 crc;
1430-
14311424
Assert(S_ISREG(file->mode));
1432-
INIT_TRADITIONAL_CRC32(crc);
1433-
1434-
/* reset size summary */
1435-
file->read_size = 0;
1436-
file->write_size = 0;
1437-
1438-
/* open backup mode file for read */
1439-
in = fopen(file->path, PG_BINARY_R);
1440-
if (in == NULL)
1441-
{
1442-
FIN_TRADITIONAL_CRC32(crc);
1443-
file->crc = crc;
1444-
1445-
/* maybe deleted, it's not error */
1446-
if (errno == ENOENT)
1447-
return false;
1448-
1449-
elog(ERROR, "cannot open source file \"%s\": %s", file->path,
1450-
strerror(errno));
1451-
}
1452-
1453-
/* stat source file to change mode of destination file */
1454-
if (fstat(fileno(in), &st) == -1)
1455-
{
1456-
fclose(in);
1457-
elog(ERROR, "cannot stat \"%s\": %s", file->path,
1458-
strerror(errno));
1459-
}
1460-
1461-
for (;;)
1462-
{
1463-
read_len = fread(buf, 1, sizeof(buf), in);
1464-
1465-
if(read_len == 0)
1466-
break;
1467-
1468-
/* update CRC */
1469-
COMP_TRADITIONAL_CRC32(crc, buf, read_len);
1470-
1471-
file->write_size += read_len;
1472-
file->read_size += read_len;
1473-
}
1474-
1475-
errno_tmp = errno;
1476-
if (!feof(in))
1477-
{
1478-
fclose(in);
1479-
elog(ERROR, "cannot read backup mode file \"%s\": %s",
1480-
file->path, strerror(errno_tmp));
1481-
}
1482-
1483-
/* finish CRC calculation and store into pgFile */
1484-
FIN_TRADITIONAL_CRC32(crc);
1485-
file->crc = crc;
1486-
1487-
fclose(in);
14881425

1489-
return true;
1426+
file->crc = pgFileGetCRC(file->path, false, false, &file->read_size);
1427+
file->write_size = file->read_size;
14901428
}
14911429

14921430
/*
@@ -1779,11 +1717,11 @@ fileEqualCRC(const char *path1, const char *path2, bool path2_is_compressed)
17791717
else
17801718
#endif
17811719
{
1782-
crc2 = pgFileGetCRC(path2);
1720+
crc2 = pgFileGetCRC(path2, false, true, NULL);
17831721
}
17841722

17851723
/* Get checksum of original file */
1786-
crc1 = pgFileGetCRC(path1);
1724+
crc1 = pgFileGetCRC(path1, false, true, NULL);
17871725

17881726
return EQ_CRC32C(crc1, crc2);
17891727
}

src/dir.c

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,36 +259,55 @@ pgFileDelete(pgFile *file)
259259
}
260260

261261
pg_crc32
262-
pgFileGetCRC(const char *file_path, bool use_crc32c)
262+
pgFileGetCRC(const char *file_path, bool use_crc32c, bool raise_on_deleted,
263+
size_t *bytes_read)
263264
{
264265
FILE *fp;
265266
pg_crc32 crc = 0;
266267
char buf[1024];
267268
size_t len;
269+
size_t total = 0;
268270
int errno_tmp;
269271

272+
INIT_FILE_CRC32(use_crc32c, crc);
273+
270274
/* open file in binary read mode */
271275
fp = fopen(file_path, PG_BINARY_R);
272276
if (fp == NULL)
273-
elog(ERROR, "cannot open file \"%s\": %s",
274-
file_path, strerror(errno));
277+
{
278+
if (!raise_on_deleted && errno == ENOENT)
279+
{
280+
FIN_FILE_CRC32(use_crc32c, crc);
281+
return crc;
282+
}
283+
else
284+
elog(ERROR, "cannot open file \"%s\": %s",
285+
file_path, strerror(errno));
286+
}
275287

276-
/* calc CRC of backup file */
277-
INIT_FILE_CRC32(use_crc32c, crc);
278-
while ((len = fread(buf, 1, sizeof(buf), fp)) == sizeof(buf))
288+
/* calc CRC of file */
289+
for (;;)
279290
{
280291
if (interrupted)
281292
elog(ERROR, "interrupted during CRC calculation");
293+
294+
len = fread(buf, 1, sizeof(buf), fp);
295+
if(len == 0)
296+
break;
297+
/* update CRC */
282298
COMP_FILE_CRC32(use_crc32c, crc, buf, len);
299+
total += len;
283300
}
301+
302+
if (bytes_read)
303+
*bytes_read = total;
304+
284305
errno_tmp = errno;
285306
if (!feof(fp))
286307
elog(WARNING, "cannot read \"%s\": %s", file_path,
287308
strerror(errno_tmp));
288-
if (len > 0)
289-
COMP_FILE_CRC32(use_crc32c, crc, buf, len);
290-
FIN_FILE_CRC32(use_crc32c, crc);
291309

310+
FIN_FILE_CRC32(use_crc32c, crc);
292311
fclose(fp);
293312

294313
return crc;

src/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ merge_files(void *arg)
524524
* do that.
525525
*/
526526
file->write_size = pgFileSize(to_path_tmp);
527-
file->crc = pgFileGetCRC(to_path_tmp, false);
527+
file->crc = pgFileGetCRC(to_path_tmp, false, true, NULL);
528528
}
529529
}
530530
else

src/pg_probackup.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ extern pgFile *pgFileNew(const char *path, bool omit_symlink);
531531
extern pgFile *pgFileInit(const char *path);
532532
extern void pgFileDelete(pgFile *file);
533533
extern void pgFileFree(void *file);
534-
extern pg_crc32 pgFileGetCRC(const char *file_path, bool use_crc32c);
534+
extern pg_crc32 pgFileGetCRC(const char *file_path, bool use_crc32c,
535+
bool raise_on_deleted, size_t *bytes_read);
535536
extern int pgFileComparePath(const void *f1, const void *f2);
536537
extern int pgFileComparePathDesc(const void *f1, const void *f2);
537538
extern int pgFileCompareLinked(const void *f1, const void *f2);
@@ -552,7 +553,7 @@ extern void push_wal_file(const char *from_path, const char *to_path,
552553
bool is_compress, bool overwrite);
553554
extern void get_wal_file(const char *from_path, const char *to_path);
554555

555-
extern bool calc_file_checksum(pgFile *file);
556+
extern void calc_file_checksum(pgFile *file);
556557

557558
extern bool check_file_pages(pgFile* file,
558559
XLogRecPtr stop_lsn,

src/util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ set_min_recovery_point(pgFile *file, const char *backup_path, XLogRecPtr stop_ba
334334
writeControlFile(&ControlFile, fullpath);
335335

336336
/* Update pg_control checksum in backup_list */
337-
file->crc = pgFileGetCRC(fullpath, false);
337+
file->crc = pgFileGetCRC(fullpath, false, true, NULL);
338338

339339
pg_free(buffer);
340340
}

src/validate.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ pgBackupValidateFiles(void *arg)
224224
* To avoid this problem we need to use different algorithm, CRC-32 in
225225
* this case.
226226
*/
227-
crc = pgFileGetCRC(file->path, arguments->backup_version <= 20021);
227+
crc = pgFileGetCRC(file->path, arguments->backup_version <= 20021,
228+
true, NULL);
228229
if (crc != file->crc)
229230
{
230231
elog(WARNING, "Invalid CRC of backup file \"%s\" : %X. Expected %X",

0 commit comments

Comments
 (0)