Skip to content

Commit 74367e6

Browse files
committed
rework file stat handling to simplify and cross-platform-ability
- don't use full `struct stat` since it differs between platforms - store file kind separately from mode in pb specific way - add pioFilesAreSame as replacement of fio_is_same_file since it needs st_ino and st_dev, therefore had to run on localdrive always.
1 parent af80680 commit 74367e6

File tree

14 files changed

+356
-176
lines changed

14 files changed

+356
-176
lines changed

src/archive.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ push_file_internal(const char *wal_file_name, const char *pg_xlog_dir,
388388
char to_fullpath[MAXPGPATH];
389389
char to_fullpath_part[MAXPGPATH];
390390
/* partial handling */
391-
struct stat st;
391+
pio_stat_t st;
392392
int partial_try_count = 0;
393393
ssize_t partial_file_size = 0;
394394
bool partial_is_stale = true;
@@ -466,11 +466,11 @@ push_file_internal(const char *wal_file_name, const char *pg_xlog_dir,
466466
elog(LOG,
467467
"Temp WAL file already exists, waiting on it %u seconds: \"%s\"",
468468
archive_timeout, to_fullpath_part);
469-
partial_file_size = st.st_size;
469+
partial_file_size = st.pst_size;
470470
}
471471

472472
/* file size is changing */
473-
if (st.st_size != partial_file_size)
473+
if (st.pst_size != partial_file_size)
474474
partial_is_stale = false;
475475

476476
sleep(1);

src/backup.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
392392
pgFile *file = (pgFile *) parray_get(backup_files_list, i);
393393

394394
/* if the entry was a directory, create it in the backup */
395-
if (S_ISDIR(file->mode))
395+
if (file->kind == PIO_KIND_DIRECTORY)
396396
{
397397
char dirpath[MAXPGPATH];
398398

@@ -569,7 +569,7 @@ do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
569569
pgFile *file = (pgFile *) parray_get(backup_files_list, i);
570570

571571
/* TODO: sync directory ? */
572-
if (S_ISDIR(file->mode))
572+
if (file->kind == PIO_KIND_DIRECTORY)
573573
continue;
574574

575575
if (file->write_size <= 0)
@@ -1837,7 +1837,7 @@ pg_stop_backup_write_file_helper(const char *path, const char *filename, const c
18371837
file = pgFileNew(full_filename, filename, true, 0,
18381838
FIO_BACKUP_HOST);
18391839

1840-
if (S_ISREG(file->mode))
1840+
if (file->kind == PIO_KIND_REGULAR)
18411841
{
18421842
file->crc = pgFileGetCRC32C(full_filename, false);
18431843

@@ -1991,7 +1991,7 @@ backup_files(void *arg)
19911991
pgFile *prev_file = NULL;
19921992

19931993
/* We have already copied all directories */
1994-
if (S_ISDIR(file->mode))
1994+
if (file->kind == PIO_KIND_DIRECTORY)
19951995
continue;
19961996

19971997
if (arguments->thread_num == 1)
@@ -2046,9 +2046,9 @@ backup_files(void *arg)
20462046
}
20472047

20482048
/* Encountered some strange beast */
2049-
if (!S_ISREG(file->mode))
2050-
elog(WARNING, "Unexpected type %d of file \"%s\", skipping",
2051-
file->mode, from_fullpath);
2049+
if (file->kind != PIO_KIND_REGULAR)
2050+
elog(WARNING, "Unexpected type %s of file \"%s\", skipping",
2051+
pio_file_kind2str(file->kind, from_fullpath), from_fullpath);
20522052

20532053
/* Check that file exist in previous backup */
20542054
if (current.backup_mode != BACKUP_MODE_FULL)
@@ -2121,7 +2121,7 @@ parse_filelist_filenames(parray *files, const char *root)
21212121
pgFile *file = (pgFile *) parray_get(files, i);
21222122
int sscanf_result;
21232123

2124-
if (S_ISREG(file->mode) &&
2124+
if (file->kind == PIO_KIND_REGULAR &&
21252125
path_is_prefix_of_path(PG_TBLSPC_DIR, file->rel_path))
21262126
{
21272127
/*
@@ -2148,7 +2148,7 @@ parse_filelist_filenames(parray *files, const char *root)
21482148
}
21492149
}
21502150

2151-
if (S_ISREG(file->mode) && file->tblspcOid != 0 &&
2151+
if (file->kind == PIO_KIND_REGULAR && file->tblspcOid != 0 &&
21522152
file->name && file->name[0])
21532153
{
21542154
if (file->forkName == init)
@@ -2218,7 +2218,7 @@ set_cfs_datafiles(parray *files, const char *root, char *relative, size_t i)
22182218

22192219
if (strstr(prev_file->rel_path, cfs_tblspc_path) != NULL)
22202220
{
2221-
if (S_ISREG(prev_file->mode) && prev_file->is_datafile)
2221+
if (prev_file->kind == PIO_KIND_REGULAR && prev_file->is_datafile)
22222222
{
22232223
elog(LOG, "Setting 'is_cfs' on file %s, name %s",
22242224
prev_file->rel_path, prev_file->name);
@@ -2375,7 +2375,7 @@ calculate_datasize_of_filelist(parray *filelist)
23752375
if (file->external_dir_num != 0 || file->excluded)
23762376
continue;
23772377

2378-
if (S_ISDIR(file->mode))
2378+
if (file->kind == PIO_KIND_DIRECTORY)
23792379
{
23802380
// TODO is a dir always 4K?
23812381
bytes += 4096;

src/catalog.c

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -847,12 +847,22 @@ pgBackupGetBackupMode(pgBackup *backup, bool show_color)
847847
static bool
848848
IsDir(const char *dirpath, const char *entry, fio_location location)
849849
{
850+
FOBJ_FUNC_ARP();
850851
char path[MAXPGPATH];
851-
struct stat st;
852+
pio_stat_t st;
853+
err_i err;
852854

853855
join_path_components(path, dirpath, entry);
854856

855-
return fio_stat(location, path, &st, false) == 0 && S_ISDIR(st.st_mode);
857+
st = $i(pioStat, pioDriveForLocation(location),
858+
.path = path, .follow_symlink = false, .err = &err);
859+
if ($haserr(err))
860+
{
861+
ft_logerr(FT_WARNING, $errmsg(err), "IsDir");
862+
return false;
863+
}
864+
865+
return st.pst_kind == PIO_KIND_DIRECTORY;
856866
}
857867

858868
/*
@@ -1074,6 +1084,7 @@ get_backup_filelist(pgBackup *backup, bool strict)
10741084
char path[MAXPGPATH];
10751085
char linked[MAXPGPATH];
10761086
char compress_alg_string[MAXPGPATH];
1087+
char kind[16];
10771088
int64 write_size,
10781089
uncompressed_size,
10791090
mode, /* bit length of mode_t depends on platforms */
@@ -1115,6 +1126,11 @@ get_backup_filelist(pgBackup *backup, bool strict)
11151126
/*
11161127
* Optional fields
11171128
*/
1129+
if (get_control_value_str(buf, "kind", kind, sizeof(kind), false))
1130+
file->kind = pio_str2file_kind(kind, path);
1131+
else /* fallback to mode for old backups */
1132+
file->kind = pio_statmode2file_kind(file->mode, path);
1133+
11181134
if (get_control_value_str(buf, "linked", linked, sizeof(linked), false) && linked[0])
11191135
{
11201136
file->linked = pgut_strdup(linked);
@@ -1146,7 +1162,7 @@ get_backup_filelist(pgBackup *backup, bool strict)
11461162
if (!file->is_datafile || file->is_cfs)
11471163
file->size = file->uncompressed_size;
11481164

1149-
if (file->external_dir_num == 0 && S_ISREG(file->mode))
1165+
if (file->external_dir_num == 0 && file->kind == PIO_KIND_REGULAR)
11501166
{
11511167
bool is_datafile = file->is_datafile;
11521168
set_forkname(file);
@@ -2564,14 +2580,14 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
25642580
if (file->write_size == FILE_NOT_FOUND)
25652581
continue;
25662582

2567-
if (S_ISDIR(file->mode))
2583+
if (file->kind == PIO_KIND_DIRECTORY)
25682584
{
25692585
backup_size_on_disk += 4096;
25702586
uncompressed_size_on_disk += 4096;
25712587
}
25722588

25732589
/* Count the amount of the data actually copied */
2574-
if (S_ISREG(file->mode) && file->write_size > 0)
2590+
if (file->kind == PIO_KIND_REGULAR && file->write_size > 0)
25752591
{
25762592
/*
25772593
* Size of WAL files in 'pg_wal' is counted separately
@@ -2587,11 +2603,13 @@ write_backup_filelist(pgBackup *backup, parray *files, const char *root,
25872603
}
25882604

25892605
len = sprintf(line, "{\"path\":\"%s\", \"size\":\"" INT64_FORMAT "\", "
2590-
"\"mode\":\"%u\", \"is_datafile\":\"%u\", "
2606+
"\"kind\":\"%s\", \"mode\":\"%u\", \"is_datafile\":\"%u\", "
25912607
"\"is_cfs\":\"%u\", \"crc\":\"%u\", "
25922608
"\"compress_alg\":\"%s\", \"external_dir_num\":\"%d\", "
25932609
"\"dbOid\":\"%u\"",
2594-
file->rel_path, file->write_size, file->mode,
2610+
file->rel_path, file->write_size,
2611+
pio_file_kind2str(file->kind, file->rel_path),
2612+
file->mode,
25952613
file->is_datafile ? 1 : 0,
25962614
file->is_cfs ? 1 : 0,
25972615
file->crc,

src/catchup.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ catchup_thread_runner(void *arg)
378378
pgFile *dest_file = NULL;
379379

380380
/* We have already copied all directories */
381-
if (S_ISDIR(file->mode))
381+
if (file->kind == PIO_KIND_DIRECTORY)
382382
continue;
383383

384384
if (file->excluded)
@@ -400,9 +400,9 @@ catchup_thread_runner(void *arg)
400400
join_path_components(to_fullpath, arguments->to_root, file->rel_path);
401401

402402
/* Encountered some strange beast */
403-
if (!S_ISREG(file->mode))
404-
elog(WARNING, "Unexpected type %d of file \"%s\", skipping",
405-
file->mode, from_fullpath);
403+
if (file->kind != PIO_KIND_REGULAR)
404+
elog(WARNING, "Unexpected kind %s of file \"%s\", skipping",
405+
pio_file_kind2str(file->kind, from_fullpath), from_fullpath);
406406

407407
/* Check that file exist in dest pgdata */
408408
if (arguments->backup_mode != BACKUP_MODE_FULL)
@@ -546,7 +546,7 @@ catchup_sync_destination_files(const char* pgdata_path, fio_location location, p
546546
* - but PG itself is not relying on fs, its durable_sync
547547
* includes directory sync
548548
*/
549-
if (S_ISDIR(file->mode) || file->excluded)
549+
if (file->kind == PIO_KIND_DIRECTORY || file->excluded)
550550
continue;
551551

552552
Assert(file->external_dir_num == 0);
@@ -807,7 +807,7 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
807807
pgFile *file = (pgFile *) parray_get(source_filelist, i);
808808
char parent_dir[MAXPGPATH];
809809

810-
if (!S_ISDIR(file->mode) || file->excluded)
810+
if (file->kind != PIO_KIND_DIRECTORY || file->excluded)
811811
continue;
812812

813813
/*

src/checkdb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ check_files(void *arg)
147147
elog(ERROR, "interrupted during checkdb");
148148

149149
/* No need to check directories */
150-
if (S_ISDIR(file->mode))
150+
if (file->kind == PIO_KIND_DIRECTORY)
151151
continue;
152152

153153
if (!pg_atomic_test_set_flag(&file->lock))
@@ -161,7 +161,7 @@ check_files(void *arg)
161161
elog(INFO, "Progress: (%d/%d). Process file \"%s\"",
162162
i + 1, n_files_list, from_fullpath);
163163

164-
if (S_ISREG(file->mode))
164+
if (file->kind == PIO_KIND_REGULAR)
165165
{
166166
/* check only uncompressed by cfs datafiles */
167167
if (file->is_datafile && !file->is_cfs)

0 commit comments

Comments
 (0)