Skip to content

Commit efedbc2

Browse files
committed
[refactoring] remove dir_create_dir() calls and use fio_mkdir() instead
1 parent 9e9e86f commit efedbc2

File tree

10 files changed

+89
-74
lines changed

10 files changed

+89
-74
lines changed

src/backup.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
261261
char stream_xlog_path[MAXPGPATH];
262262

263263
join_path_components(stream_xlog_path, current.database_dir, PG_XLOG_DIR);
264-
fio_mkdir(FIO_BACKUP_HOST, stream_xlog_path, DIR_PERMISSION);
264+
fio_mkdir(FIO_BACKUP_HOST, stream_xlog_path, DIR_PERMISSION, false);
265265

266266
start_WAL_streaming(backup_conn, stream_xlog_path, &instance_config.conn_opt,
267267
current.start_lsn, current.tli, true);
@@ -414,7 +414,7 @@ do_backup_pg(InstanceState *instanceState, PGconn *backup_conn,
414414
join_path_components(dirpath, current.database_dir, file->rel_path);
415415

416416
elog(VERBOSE, "Create directory '%s'", dirpath);
417-
fio_mkdir(FIO_BACKUP_HOST, dirpath, DIR_PERMISSION);
417+
fio_mkdir(FIO_BACKUP_HOST, dirpath, DIR_PERMISSION, false);
418418
}
419419

420420
}

src/catalog.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,6 @@ get_multi_timeline_parent(parray *backup_list, parray *tli_list,
14241424
* It may be ok or maybe not, so it's up to the caller
14251425
* to fix it or let it be.
14261426
*/
1427-
14281427
void
14291428
pgBackupCreateDir(pgBackup *backup, const char *backup_instance_path)
14301429
{
@@ -1467,7 +1466,7 @@ pgBackupCreateDir(pgBackup *backup, const char *backup_instance_path)
14671466
char path[MAXPGPATH];
14681467

14691468
join_path_components(path, backup->root_dir, parray_get(subdirs, i));
1470-
fio_mkdir(FIO_BACKUP_HOST, path, DIR_PERMISSION);
1469+
fio_mkdir(FIO_BACKUP_HOST, path, DIR_PERMISSION, false);
14711470
}
14721471

14731472
free_dir_list(subdirs);
@@ -1490,8 +1489,7 @@ create_backup_dir(pgBackup *backup, const char *backup_instance_path)
14901489

14911490
join_path_components(path, backup_instance_path, base36enc(backup_id));
14921491

1493-
/* TODO: add wrapper for remote mode */
1494-
rc = dir_create_dir(path, DIR_PERMISSION, true);
1492+
rc = fio_mkdir(FIO_BACKUP_HOST, path, DIR_PERMISSION, true);
14951493

14961494
if (rc == 0)
14971495
{

src/catchup.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
707707

708708
/* Start stream replication */
709709
join_path_components(dest_xlog_path, dest_pgdata, PG_XLOG_DIR);
710-
fio_mkdir(FIO_LOCAL_HOST, dest_xlog_path, DIR_PERMISSION);
710+
fio_mkdir(FIO_LOCAL_HOST, dest_xlog_path, DIR_PERMISSION, false);
711711
start_WAL_streaming(source_conn, dest_xlog_path, &instance_config.conn_opt,
712712
current.start_lsn, current.tli, false);
713713

@@ -823,7 +823,7 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
823823
join_path_components(dirpath, dest_pgdata, file->rel_path);
824824

825825
elog(VERBOSE, "Create directory '%s'", dirpath);
826-
fio_mkdir(FIO_LOCAL_HOST, dirpath, DIR_PERMISSION);
826+
fio_mkdir(FIO_LOCAL_HOST, dirpath, DIR_PERMISSION, false);
827827
}
828828
else
829829
{
@@ -855,7 +855,7 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
855855
linked_path, to_path);
856856

857857
/* create tablespace directory */
858-
if (fio_mkdir(FIO_LOCAL_HOST, linked_path, file->mode) != 0)
858+
if (fio_mkdir(FIO_LOCAL_HOST, linked_path, file->mode, false) != 0)
859859
elog(ERROR, "Could not create tablespace directory \"%s\": %s",
860860
linked_path, strerror(errno));
861861

src/dir.c

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -135,36 +135,6 @@ static TablespaceList tablespace_dirs = {NULL, NULL};
135135
/* Extra directories mapping */
136136
static TablespaceList external_remap_list = {NULL, NULL};
137137

138-
/*
139-
* Create directory, also create parent directories if necessary.
140-
* In strict mode treat already existing directory as error.
141-
* Return values:
142-
* 0 - ok
143-
* -1 - error (check errno)
144-
*/
145-
int
146-
dir_create_dir(const char *dir, mode_t mode, bool strict)
147-
{
148-
char parent[MAXPGPATH];
149-
150-
strncpy(parent, dir, MAXPGPATH);
151-
get_parent_directory(parent);
152-
153-
/* Create parent first */
154-
if (access(parent, F_OK) == -1)
155-
dir_create_dir(parent, mode, false);
156-
157-
/* Create directory */
158-
if (mkdir(dir, mode) == -1)
159-
{
160-
if (errno == EEXIST && !strict) /* already exist */
161-
return 0;
162-
return -1;
163-
}
164-
165-
return 0;
166-
}
167-
168138
pgFile *
169139
pgFileNew(const char *path, const char *rel_path, bool follow_symlink,
170140
int external_dir_num, fio_location location)
@@ -1106,7 +1076,7 @@ create_data_directories(parray *dest_files, const char *data_dir, const char *ba
11061076
linked_path, to_path);
11071077

11081078
/* create tablespace directory */
1109-
fio_mkdir(location, linked_path, pg_tablespace_mode);
1079+
fio_mkdir(location, linked_path, pg_tablespace_mode, false);
11101080

11111081
/* create link to linked_path */
11121082
if (fio_symlink(location, linked_path, to_path, incremental) < 0)
@@ -1124,7 +1094,7 @@ create_data_directories(parray *dest_files, const char *data_dir, const char *ba
11241094
join_path_components(to_path, data_dir, dir->rel_path);
11251095

11261096
// TODO check exit code
1127-
fio_mkdir(location, to_path, dir->mode);
1097+
fio_mkdir(location, to_path, dir->mode, false);
11281098
}
11291099

11301100
if (extract_tablespaces)

src/init.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ do_init(CatalogState *catalogState)
3333
}
3434

3535
/* create backup catalog root directory */
36-
dir_create_dir(catalogState->catalog_path, DIR_PERMISSION, false);
36+
fio_mkdir(FIO_BACKUP_HOST, catalogState->catalog_path, DIR_PERMISSION, false);
3737

3838
/* create backup catalog data directory */
39-
dir_create_dir(catalogState->backup_subdir_path, DIR_PERMISSION, false);
39+
fio_mkdir(FIO_BACKUP_HOST, catalogState->backup_subdir_path, DIR_PERMISSION, false);
4040

4141
/* create backup catalog wal directory */
42-
dir_create_dir(catalogState->wal_subdir_path, DIR_PERMISSION, false);
42+
fio_mkdir(FIO_BACKUP_HOST, catalogState->wal_subdir_path, DIR_PERMISSION, false);
4343

4444
elog(INFO, "Backup catalog '%s' successfully inited", catalogState->catalog_path);
4545
return 0;
@@ -86,8 +86,8 @@ do_add_instance(InstanceState *instanceState, InstanceConfig *instance)
8686
instanceState->instance_name, instanceState->instance_wal_subdir_path);
8787

8888
/* Create directory for data files of this specific instance */
89-
dir_create_dir(instanceState->instance_backup_subdir_path, DIR_PERMISSION, false);
90-
dir_create_dir(instanceState->instance_wal_subdir_path, DIR_PERMISSION, false);
89+
fio_mkdir(FIO_BACKUP_HOST, instanceState->instance_backup_subdir_path, DIR_PERMISSION, false);
90+
fio_mkdir(FIO_BACKUP_HOST, instanceState->instance_wal_subdir_path, DIR_PERMISSION, false);
9191

9292
/*
9393
* Write initial configuration file.

src/merge.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ merge_chain(InstanceState *instanceState,
646646
makeExternalDirPathByNum(new_container, full_external_prefix,
647647
file->external_dir_num);
648648
join_path_components(dirpath, new_container, file->rel_path);
649-
dir_create_dir(dirpath, DIR_PERMISSION, false);
649+
fio_mkdir(FIO_BACKUP_HOST, dirpath, DIR_PERMISSION, false);
650650
}
651651

652652
pg_atomic_init_flag(&file->lock);

src/pg_probackup.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,6 @@ extern void makeExternalDirPathByNum(char *ret_path, const char *pattern_path,
10441044
const int dir_num);
10451045
extern bool backup_contains_external(const char *dir, parray *dirs_list);
10461046

1047-
extern int dir_create_dir(const char *path, mode_t mode, bool strict);
10481047
extern bool dir_is_empty(const char *path, fio_location location);
10491048

10501049
extern bool fileExists(const char *path, fio_location location);

src/restore.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ restore_chain(pgBackup *dest_backup, parray *parent_chain,
818818

819819
for (i = 0; i < parray_num(external_dirs); i++)
820820
fio_mkdir(FIO_DB_HOST, parray_get(external_dirs, i),
821-
DIR_PERMISSION);
821+
DIR_PERMISSION, false);
822822
}
823823

824824
/*
@@ -844,7 +844,7 @@ restore_chain(pgBackup *dest_backup, parray *parent_chain,
844844
join_path_components(dirpath, external_path, file->rel_path);
845845

846846
elog(VERBOSE, "Create external directory \"%s\"", dirpath);
847-
fio_mkdir(FIO_DB_HOST, dirpath, file->mode);
847+
fio_mkdir(FIO_DB_HOST, dirpath, file->mode, false);
848848
}
849849
}
850850

src/utils/file.c

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,15 +1224,15 @@ fio_access(fio_location location, const char* path, int mode)
12241224
{
12251225
if (fio_is_remote(location))
12261226
{
1227-
fio_header hdr;
1228-
size_t path_len = strlen(path) + 1;
1229-
hdr.cop = FIO_ACCESS;
1230-
hdr.handle = -1;
1231-
hdr.size = path_len;
1232-
hdr.arg = mode;
1227+
fio_header hdr = {
1228+
.cop = FIO_ACCESS,
1229+
.handle = -1,
1230+
.size = strlen(path) + 1,
1231+
.arg = mode,
1232+
};
12331233

12341234
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
1235-
IO_CHECK(fio_write_all(fio_stdout, path, path_len), path_len);
1235+
IO_CHECK(fio_write_all(fio_stdout, path, hdr.size), hdr.size);
12361236

12371237
IO_CHECK(fio_read_all(fio_stdin, &hdr, sizeof(hdr)), sizeof(hdr));
12381238
Assert(hdr.cop == FIO_ACCESS);
@@ -1460,7 +1460,6 @@ fio_remove(fio_location location, const char* path, bool missing_ok)
14601460
return result;
14611461
}
14621462

1463-
14641463
static void
14651464
fio_remove_impl(const char* path, bool missing_ok, int out)
14661465
{
@@ -1480,35 +1479,86 @@ fio_remove_impl(const char* path, bool missing_ok, int out)
14801479
IO_CHECK(fio_write_all(out, &hdr, sizeof(hdr)), sizeof(hdr));
14811480
}
14821481

1483-
/* Create directory
1484-
* TODO: add strict flag
1482+
/*
1483+
* Create directory, also create parent directories if necessary.
1484+
* In strict mode treat already existing directory as error.
1485+
* Return values:
1486+
* 0 - ok
1487+
* -1 - error (check errno)
1488+
*/
1489+
static int
1490+
dir_create_dir(const char *dir, mode_t mode, bool strict)
1491+
{
1492+
char parent[MAXPGPATH];
1493+
1494+
strncpy(parent, dir, MAXPGPATH);
1495+
get_parent_directory(parent);
1496+
1497+
/* Create parent first */
1498+
if (access(parent, F_OK) == -1)
1499+
dir_create_dir(parent, mode, false);
1500+
1501+
/* Create directory */
1502+
if (mkdir(dir, mode) == -1)
1503+
{
1504+
if (errno == EEXIST && !strict) /* already exist */
1505+
return 0;
1506+
return -1;
1507+
}
1508+
1509+
return 0;
1510+
}
1511+
1512+
/*
1513+
* Create directory
14851514
*/
14861515
int
1487-
fio_mkdir(fio_location location, const char* path, int mode)
1516+
fio_mkdir(fio_location location, const char* path, int mode, bool strict)
14881517
{
14891518
if (fio_is_remote(location))
14901519
{
1491-
fio_header hdr;
1492-
size_t path_len = strlen(path) + 1;
1493-
hdr.cop = FIO_MKDIR;
1494-
hdr.handle = -1;
1495-
hdr.size = path_len;
1496-
hdr.arg = mode;
1520+
fio_header hdr = {
1521+
.cop = FIO_MKDIR,
1522+
.handle = strict ? 1 : 0, /* ugly "hack" to pass more params*/
1523+
.size = strlen(path) + 1,
1524+
.arg = mode,
1525+
};
14971526

14981527
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
1499-
IO_CHECK(fio_write_all(fio_stdout, path, path_len), path_len);
1528+
IO_CHECK(fio_write_all(fio_stdout, path, hdr.size), hdr.size);
15001529

15011530
IO_CHECK(fio_read_all(fio_stdin, &hdr, sizeof(hdr)), sizeof(hdr));
15021531
Assert(hdr.cop == FIO_MKDIR);
15031532

1504-
return hdr.arg;
1533+
if (hdr.arg != 0)
1534+
{
1535+
errno = hdr.arg;
1536+
return -1;
1537+
}
1538+
return 0;
15051539
}
15061540
else
15071541
{
1508-
return dir_create_dir(path, mode, false);
1542+
return dir_create_dir(path, mode, strict);
15091543
}
15101544
}
15111545

1546+
static void
1547+
fio_mkdir_impl(const char* path, int mode, bool strict, int out)
1548+
{
1549+
fio_header hdr = {
1550+
.cop = FIO_MKDIR,
1551+
.handle = -1,
1552+
.size = 0,
1553+
.arg = 0,
1554+
};
1555+
1556+
if (dir_create_dir(path, mode, strict) != 0)
1557+
hdr.arg = errno;
1558+
1559+
IO_CHECK(fio_write_all(out, &hdr, sizeof(hdr)), sizeof(hdr));
1560+
}
1561+
15121562
/* Change file mode */
15131563
int
15141564
fio_chmod(fio_location location, const char* path, int mode)
@@ -3384,9 +3434,7 @@ fio_communicate(int in, int out)
33843434
fio_remove_impl(buf, hdr.arg == 1, out);
33853435
break;
33863436
case FIO_MKDIR: /* Create directory */
3387-
hdr.size = 0;
3388-
hdr.arg = dir_create_dir(buf, hdr.arg, false);
3389-
IO_CHECK(fio_write_all(out, &hdr, sizeof(hdr)), sizeof(hdr));
3437+
fio_mkdir_impl(buf, hdr.arg, hdr.handle == 1, out);
33903438
break;
33913439
case FIO_CHMOD: /* Change file mode */
33923440
SYS_CHECK(chmod(buf, hdr.arg));

src/utils/file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ extern pg_crc32 fio_get_crc32(fio_location location, const char *file_path, bool
158158
extern int fio_rename(fio_location location, const char* old_path, const char* new_path);
159159
extern int fio_symlink(fio_location location, const char* target, const char* link_path, bool overwrite);
160160
extern int fio_remove(fio_location location, const char* path, bool missing_ok);
161-
extern int fio_mkdir(fio_location location, const char* path, int mode);
161+
extern int fio_mkdir(fio_location location, const char* path, int mode, bool strict);
162162
extern int fio_chmod(fio_location location, const char* path, int mode);
163163
extern int fio_access(fio_location location, const char* path, int mode);
164164
extern int fio_stat(fio_location location, const char* path, struct stat* st, bool follow_symlinks);

0 commit comments

Comments
 (0)