Skip to content

Commit 0bed194

Browse files
committed
Merge branch 'master' into try-merge-2.6
2 parents 46c782b + b17669c commit 0bed194

File tree

10 files changed

+1007
-383
lines changed

10 files changed

+1007
-383
lines changed

src/catalog.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ get_backup_filelist(pgBackup *backup, bool strict)
10751075
char linked[MAXPGPATH];
10761076
char compress_alg_string[MAXPGPATH];
10771077
int64 write_size,
1078+
uncompressed_size,
10781079
mode, /* bit length of mode_t depends on platforms */
10791080
is_datafile,
10801081
is_cfs,
@@ -1138,8 +1139,30 @@ get_backup_filelist(pgBackup *backup, bool strict)
11381139
if (get_control_value_int64(buf, "hdr_size", &hdr_size, false))
11391140
file->hdr_size = (int) hdr_size;
11401141

1141-
if (file->external_dir_num == 0)
1142+
if (get_control_value_int64(buf, "full_size", &uncompressed_size, false))
1143+
file->uncompressed_size = uncompressed_size;
1144+
else
1145+
file->uncompressed_size = write_size;
1146+
if (!file->is_datafile || file->is_cfs)
1147+
file->size = file->uncompressed_size;
1148+
1149+
if (file->external_dir_num == 0 && S_ISREG(file->mode))
1150+
{
1151+
bool is_datafile = file->is_datafile;
11421152
set_forkname(file);
1153+
if (is_datafile != file->is_datafile)
1154+
{
1155+
if (is_datafile)
1156+
elog(WARNING, "File '%s' was stored as datafile, but looks like it is not",
1157+
file->rel_path);
1158+
else
1159+
elog(WARNING, "File '%s' was stored as non-datafile, but looks like it is",
1160+
file->rel_path);
1161+
/* Lets fail in tests */
1162+
Assert(file->is_datafile == file->is_datafile);
1163+
file->is_datafile = is_datafile;
1164+
}
1165+
}
11431166

11441167
parray_append(files, file);
11451168
}
@@ -2567,6 +2590,11 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
25672590
file->external_dir_num,
25682591
file->dbOid);
25692592

2593+
if (file->uncompressed_size != 0 &&
2594+
file->uncompressed_size != file->write_size)
2595+
len += sprintf(line+len, ",\"full_size\":\"" INT64_FORMAT "\"",
2596+
file->uncompressed_size);
2597+
25702598
if (file->is_datafile)
25712599
len += sprintf(line+len, ",\"segno\":\"%d\"", file->segno);
25722600

src/data.c

Lines changed: 40 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -801,13 +801,17 @@ backup_non_data_file(pgFile *file, pgFile *prev_file,
801801
* and its mtime is less than parent backup start time ... */
802802
if ((pg_strcasecmp(file->name, RELMAPPER_FILENAME) != 0) &&
803803
(prev_file && file->exists_in_prev &&
804+
file->size == prev_file->size &&
804805
file->mtime <= parent_backup_time))
805806
{
806807
/*
807808
* file could be deleted under our feets.
808809
* But then backup_non_data_file_internal will handle it safely
809810
*/
810-
file->crc = fio_get_crc32(FIO_DB_HOST, from_fullpath, false, true);
811+
if (file->forkName != cfm)
812+
file->crc = fio_get_crc32(FIO_DB_HOST, from_fullpath, false, true);
813+
else
814+
file->crc = fio_get_crc32_truncated(FIO_DB_HOST, from_fullpath, true);
811815

812816
/* ...and checksum is the same... */
813817
if (EQ_CRC32C(file->crc, prev_file->crc))
@@ -1332,8 +1336,12 @@ restore_non_data_file(parray *parent_chain, pgBackup *dest_backup,
13321336
if (already_exists)
13331337
{
13341338
/* compare checksums of already existing file and backup file */
1335-
pg_crc32 file_crc = fio_get_crc32(FIO_DB_HOST, to_fullpath, false,
1336-
false);
1339+
pg_crc32 file_crc;
1340+
if (tmp_file->forkName == cfm &&
1341+
tmp_file->uncompressed_size > tmp_file->write_size)
1342+
file_crc = fio_get_crc32_truncated(FIO_DB_HOST, to_fullpath, false);
1343+
else
1344+
file_crc = fio_get_crc32(FIO_DB_HOST, to_fullpath, false, false);
13371345

13381346
if (file_crc == tmp_file->crc)
13391347
{
@@ -1390,10 +1398,12 @@ backup_non_data_file_internal(const char *from_fullpath,
13901398
const char *to_fullpath, pgFile *file,
13911399
bool missing_ok)
13921400
{
1393-
FILE *in = NULL;
13941401
FILE *out = NULL;
1395-
ssize_t read_len = 0;
1396-
char *buf = NULL;
1402+
char *errmsg = NULL;
1403+
int rc;
1404+
bool cut_zero_tail;
1405+
1406+
cut_zero_tail = file->forkName == cfm;
13971407

13981408
INIT_CRC32C(file->crc);
13991409

@@ -1415,107 +1425,44 @@ backup_non_data_file_internal(const char *from_fullpath,
14151425

14161426
/* backup remote file */
14171427
if (fio_is_remote(FIO_DB_HOST))
1418-
{
1419-
char *errmsg = NULL;
1420-
int rc = fio_send_file(from_fullpath, to_fullpath, out, file, &errmsg);
1428+
rc = fio_send_file(from_fullpath, out, cut_zero_tail, file, &errmsg);
1429+
else
1430+
rc = fio_send_file_local(from_fullpath, out, cut_zero_tail, file, &errmsg);
14211431

1422-
/* handle errors */
1423-
if (rc == FILE_MISSING)
1424-
{
1425-
/* maybe deleted, it's not error in case of backup */
1426-
if (missing_ok)
1427-
{
1428-
elog(LOG, "File \"%s\" is not found", from_fullpath);
1429-
file->write_size = FILE_NOT_FOUND;
1430-
goto cleanup;
1431-
}
1432-
else
1433-
elog(ERROR, "File \"%s\" is not found", from_fullpath);
1434-
}
1435-
else if (rc == WRITE_FAILED)
1436-
elog(ERROR, "Cannot write to \"%s\": %s", to_fullpath, strerror(errno));
1437-
else if (rc != SEND_OK)
1432+
/* handle errors */
1433+
if (rc == FILE_MISSING)
1434+
{
1435+
/* maybe deleted, it's not error in case of backup */
1436+
if (missing_ok)
14381437
{
1439-
if (errmsg)
1440-
elog(ERROR, "%s", errmsg);
1441-
else
1442-
elog(ERROR, "Cannot access remote file \"%s\"", from_fullpath);
1438+
elog(LOG, "File \"%s\" is not found", from_fullpath);
1439+
file->write_size = FILE_NOT_FOUND;
1440+
goto cleanup;
14431441
}
1444-
1445-
pg_free(errmsg);
1442+
else
1443+
elog(ERROR, "File \"%s\" is not found", from_fullpath);
14461444
}
1447-
/* backup local file */
1448-
else
1445+
else if (rc == WRITE_FAILED)
1446+
elog(ERROR, "Cannot write to \"%s\": %s", to_fullpath, strerror(errno));
1447+
else if (rc != SEND_OK)
14491448
{
1450-
/* open source file for read */
1451-
in = fopen(from_fullpath, PG_BINARY_R);
1452-
if (in == NULL)
1453-
{
1454-
/* maybe deleted, it's not error in case of backup */
1455-
if (errno == ENOENT)
1456-
{
1457-
if (missing_ok)
1458-
{
1459-
elog(LOG, "File \"%s\" is not found", from_fullpath);
1460-
file->write_size = FILE_NOT_FOUND;
1461-
goto cleanup;
1462-
}
1463-
else
1464-
elog(ERROR, "File \"%s\" is not found", from_fullpath);
1465-
}
1466-
1467-
elog(ERROR, "Cannot open file \"%s\": %s", from_fullpath,
1468-
strerror(errno));
1469-
}
1470-
1471-
/* disable stdio buffering for local input/output files to avoid triple buffering */
1472-
setvbuf(in, NULL, _IONBF, BUFSIZ);
1473-
setvbuf(out, NULL, _IONBF, BUFSIZ);
1474-
1475-
/* allocate 64kB buffer */
1476-
buf = pgut_malloc(CHUNK_SIZE);
1477-
1478-
/* copy content and calc CRC */
1479-
for (;;)
1480-
{
1481-
read_len = fread(buf, 1, CHUNK_SIZE, in);
1482-
1483-
if (ferror(in))
1484-
elog(ERROR, "Cannot read from file \"%s\": %s",
1485-
from_fullpath, strerror(errno));
1486-
1487-
if (read_len > 0)
1488-
{
1489-
if (fwrite(buf, 1, read_len, out) != read_len)
1490-
elog(ERROR, "Cannot write to file \"%s\": %s", to_fullpath,
1491-
strerror(errno));
1492-
1493-
/* update CRC */
1494-
COMP_CRC32C(file->crc, buf, read_len);
1495-
file->read_size += read_len;
1496-
}
1497-
1498-
if (feof(in))
1499-
break;
1500-
}
1449+
if (errmsg)
1450+
elog(ERROR, "%s", errmsg);
1451+
else
1452+
elog(ERROR, "Cannot access remote file \"%s\"", from_fullpath);
15011453
}
15021454

1503-
file->write_size = (int64) file->read_size;
1504-
1505-
if (file->write_size > 0)
1506-
file->uncompressed_size = file->write_size;
1455+
file->uncompressed_size = file->read_size;
15071456

15081457
cleanup:
1458+
if (errmsg != NULL)
1459+
pg_free(errmsg);
1460+
15091461
/* finish CRC calculation and store into pgFile */
15101462
FIN_CRC32C(file->crc);
15111463

1512-
if (in && fclose(in))
1513-
elog(ERROR, "Cannot close the file \"%s\": %s", from_fullpath, strerror(errno));
1514-
15151464
if (out && fclose(out))
15161465
elog(ERROR, "Cannot close the file \"%s\": %s", to_fullpath, strerror(errno));
1517-
1518-
pg_free(buf);
15191466
}
15201467

15211468
/*

0 commit comments

Comments
 (0)