Skip to content

Commit e5fb869

Browse files
committed
[refactoring] remove dir_create_dir() calls and use fio_mkdir() instead
1 parent 6b3aa65 commit e5fb869

File tree

10 files changed

+89
-75
lines changed

10 files changed

+89
-75
lines changed

src/backup.c

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

262262
join_path_components(stream_xlog_path, current.database_dir, PG_XLOG_DIR);
263-
fio_mkdir(FIO_BACKUP_HOST, stream_xlog_path, DIR_PERMISSION);
263+
fio_mkdir(FIO_BACKUP_HOST, stream_xlog_path, DIR_PERMISSION, false);
264264

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

415415
elog(LOG, "Create directory '%s'", dirpath);
416-
fio_mkdir(FIO_BACKUP_HOST, dirpath, DIR_PERMISSION);
416+
fio_mkdir(FIO_BACKUP_HOST, dirpath, DIR_PERMISSION, false);
417417
}
418418

419419
}

src/catalog.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,6 @@ get_multi_timeline_parent(parray *backup_list, parray *tli_list,
14501450
* It may be ok or maybe not, so it's up to the caller
14511451
* to fix it or let it be.
14521452
*/
1453-
14541453
void
14551454
pgBackupInitDir(pgBackup *backup, const char *backup_instance_path)
14561455
{
@@ -1495,7 +1494,7 @@ pgBackupInitDir(pgBackup *backup, const char *backup_instance_path)
14951494
for (i = 0; i < parray_num(subdirs); i++)
14961495
{
14971496
join_path_components(temp, backup->root_dir, parray_get(subdirs, i));
1498-
fio_mkdir(FIO_BACKUP_HOST, temp, DIR_PERMISSION);
1497+
fio_mkdir(FIO_BACKUP_HOST, temp, DIR_PERMISSION, false);
14991498
}
15001499

15011500
free_dir_list(subdirs);
@@ -1515,9 +1514,7 @@ create_backup_dir(pgBackup *backup, const char *backup_instance_path)
15151514
char path[MAXPGPATH];
15161515

15171516
join_path_components(path, backup_instance_path, backup_id_of(backup));
1518-
1519-
/* TODO: add wrapper for remote mode */
1520-
rc = dir_create_dir(path, DIR_PERMISSION, true);
1517+
rc = fio_mkdir(FIO_BACKUP_HOST, path, DIR_PERMISSION, true);
15211518

15221519
if (rc == 0)
15231520
backup->root_dir = pgut_strdup(path);

src/catchup.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
712712
join_path_components(dest_xlog_path, dest_pgdata, PG_XLOG_DIR);
713713
if (!dry_run)
714714
{
715-
fio_mkdir(FIO_LOCAL_HOST, dest_xlog_path, DIR_PERMISSION);
715+
fio_mkdir(FIO_LOCAL_HOST, dest_xlog_path, DIR_PERMISSION, false);
716716
start_WAL_streaming(source_conn, dest_xlog_path, &instance_config.conn_opt,
717717
current.start_lsn, current.tli, false);
718718
}
@@ -832,7 +832,7 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
832832

833833
elog(LOG, "Create directory '%s'", dirpath);
834834
if (!dry_run)
835-
fio_mkdir(FIO_LOCAL_HOST, dirpath, DIR_PERMISSION);
835+
fio_mkdir(FIO_LOCAL_HOST, dirpath, DIR_PERMISSION, false);
836836
}
837837
else
838838
{
@@ -866,7 +866,7 @@ do_catchup(const char *source_pgdata, const char *dest_pgdata, int num_threads,
866866
if (!dry_run)
867867
{
868868
/* create tablespace directory */
869-
if (fio_mkdir(FIO_LOCAL_HOST, linked_path, file->mode) != 0)
869+
if (fio_mkdir(FIO_LOCAL_HOST, linked_path, file->mode, false) != 0)
870870
elog(ERROR, "Could not create tablespace directory \"%s\": %s",
871871
linked_path, strerror(errno));
872872

src/dir.c

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

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

960930
/* create tablespace directory */
961-
fio_mkdir(location, linked_path, pg_tablespace_mode);
931+
fio_mkdir(location, linked_path, pg_tablespace_mode, false);
962932

963933
/* create link to linked_path */
964934
if (fio_symlink(location, linked_path, to_path, incremental) < 0)
@@ -976,7 +946,7 @@ create_data_directories(parray *dest_files, const char *data_dir, const char *ba
976946
join_path_components(to_path, data_dir, dir->rel_path);
977947

978948
// TODO check exit code
979-
fio_mkdir(location, to_path, dir->mode);
949+
fio_mkdir(location, to_path, dir->mode, false);
980950
}
981951

982952
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
@@ -635,7 +635,7 @@ merge_chain(InstanceState *instanceState,
635635
makeExternalDirPathByNum(new_container, full_external_prefix,
636636
file->external_dir_num);
637637
join_path_components(dirpath, new_container, file->rel_path);
638-
dir_create_dir(dirpath, DIR_PERMISSION, false);
638+
fio_mkdir(FIO_BACKUP_HOST, dirpath, DIR_PERMISSION, false);
639639
}
640640

641641
pg_atomic_init_flag(&file->lock);

src/pg_probackup.h

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

1070-
extern int dir_create_dir(const char *path, mode_t mode, bool strict);
10711070
extern bool dir_is_empty(const char *path, fio_location location);
10721071

10731072
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
@@ -813,7 +813,7 @@ restore_chain(pgBackup *dest_backup, parray *parent_chain,
813813

814814
for (i = 0; i < parray_num(external_dirs); i++)
815815
fio_mkdir(FIO_DB_HOST, parray_get(external_dirs, i),
816-
DIR_PERMISSION);
816+
DIR_PERMISSION, false);
817817
}
818818

819819
/*
@@ -839,7 +839,7 @@ restore_chain(pgBackup *dest_backup, parray *parent_chain,
839839
join_path_components(dirpath, external_path, file->rel_path);
840840

841841
elog(LOG, "Create external directory \"%s\"", dirpath);
842-
fio_mkdir(FIO_DB_HOST, dirpath, file->mode);
842+
fio_mkdir(FIO_DB_HOST, dirpath, file->mode, false);
843843
}
844844
}
845845

src/utils/file.c

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,15 +1144,15 @@ fio_access(fio_location location, const char* path, int mode)
11441144
{
11451145
if (fio_is_remote(location))
11461146
{
1147-
fio_header hdr;
1148-
size_t path_len = strlen(path) + 1;
1149-
hdr.cop = FIO_ACCESS;
1150-
hdr.handle = -1;
1151-
hdr.size = path_len;
1152-
hdr.arg = mode;
1147+
fio_header hdr = {
1148+
.cop = FIO_ACCESS,
1149+
.handle = -1,
1150+
.size = strlen(path) + 1,
1151+
.arg = mode,
1152+
};
11531153

11541154
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
1155-
IO_CHECK(fio_write_all(fio_stdout, path, path_len), path_len);
1155+
IO_CHECK(fio_write_all(fio_stdout, path, hdr.size), hdr.size);
11561156

11571157
IO_CHECK(fio_read_all(fio_stdin, &hdr, sizeof(hdr)), sizeof(hdr));
11581158
Assert(hdr.cop == FIO_ACCESS);
@@ -1410,7 +1410,6 @@ fio_remove(fio_location location, const char* path, bool missing_ok)
14101410
return result;
14111411
}
14121412

1413-
14141413
static void
14151414
fio_remove_impl(const char* path, bool missing_ok, int out)
14161415
{
@@ -1430,35 +1429,86 @@ fio_remove_impl(const char* path, bool missing_ok, int out)
14301429
IO_CHECK(fio_write_all(out, &hdr, sizeof(hdr)), sizeof(hdr));
14311430
}
14321431

1433-
/* Create directory
1434-
* TODO: add strict flag
1432+
/*
1433+
* Create directory, also create parent directories if necessary.
1434+
* In strict mode treat already existing directory as error.
1435+
* Return values:
1436+
* 0 - ok
1437+
* -1 - error (check errno)
1438+
*/
1439+
static int
1440+
dir_create_dir(const char *dir, mode_t mode, bool strict)
1441+
{
1442+
char parent[MAXPGPATH];
1443+
1444+
strncpy(parent, dir, MAXPGPATH);
1445+
get_parent_directory(parent);
1446+
1447+
/* Create parent first */
1448+
if (access(parent, F_OK) == -1)
1449+
dir_create_dir(parent, mode, false);
1450+
1451+
/* Create directory */
1452+
if (mkdir(dir, mode) == -1)
1453+
{
1454+
if (errno == EEXIST && !strict) /* already exist */
1455+
return 0;
1456+
return -1;
1457+
}
1458+
1459+
return 0;
1460+
}
1461+
1462+
/*
1463+
* Create directory
14351464
*/
14361465
int
1437-
fio_mkdir(fio_location location, const char* path, int mode)
1466+
fio_mkdir(fio_location location, const char* path, int mode, bool strict)
14381467
{
14391468
if (fio_is_remote(location))
14401469
{
1441-
fio_header hdr;
1442-
size_t path_len = strlen(path) + 1;
1443-
hdr.cop = FIO_MKDIR;
1444-
hdr.handle = -1;
1445-
hdr.size = path_len;
1446-
hdr.arg = mode;
1470+
fio_header hdr = {
1471+
.cop = FIO_MKDIR,
1472+
.handle = strict ? 1 : 0, /* ugly "hack" to pass more params*/
1473+
.size = strlen(path) + 1,
1474+
.arg = mode,
1475+
};
14471476

14481477
IO_CHECK(fio_write_all(fio_stdout, &hdr, sizeof(hdr)), sizeof(hdr));
1449-
IO_CHECK(fio_write_all(fio_stdout, path, path_len), path_len);
1478+
IO_CHECK(fio_write_all(fio_stdout, path, hdr.size), hdr.size);
14501479

14511480
IO_CHECK(fio_read_all(fio_stdin, &hdr, sizeof(hdr)), sizeof(hdr));
14521481
Assert(hdr.cop == FIO_MKDIR);
14531482

1454-
return hdr.arg;
1483+
if (hdr.arg != 0)
1484+
{
1485+
errno = hdr.arg;
1486+
return -1;
1487+
}
1488+
return 0;
14551489
}
14561490
else
14571491
{
1458-
return dir_create_dir(path, mode, false);
1492+
return dir_create_dir(path, mode, strict);
14591493
}
14601494
}
14611495

1496+
static void
1497+
fio_mkdir_impl(const char* path, int mode, bool strict, int out)
1498+
{
1499+
fio_header hdr = {
1500+
.cop = FIO_MKDIR,
1501+
.handle = -1,
1502+
.size = 0,
1503+
.arg = 0,
1504+
};
1505+
1506+
if (dir_create_dir(path, mode, strict) != 0)
1507+
hdr.arg = errno;
1508+
1509+
IO_CHECK(fio_write_all(out, &hdr, sizeof(hdr)), sizeof(hdr));
1510+
}
1511+
14621512
/* Change file mode */
14631513
int
14641514
fio_chmod(fio_location location, const char* path, int mode)
@@ -3835,9 +3885,7 @@ fio_communicate(int in, int out)
38353885
fio_remove_impl(buf, hdr.arg == 1, out);
38363886
break;
38373887
case FIO_MKDIR: /* Create directory */
3838-
hdr.size = 0;
3839-
hdr.arg = dir_create_dir(buf, hdr.arg, false);
3840-
IO_CHECK(fio_write_all(out, &hdr, sizeof(hdr)), sizeof(hdr));
3888+
fio_mkdir_impl(buf, hdr.arg, hdr.handle == 1, out);
38413889
break;
38423890
case FIO_CHMOD: /* Change file mode */
38433891
SYS_CHECK(chmod(buf, hdr.arg));

src/utils/file.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ extern pg_crc32 fio_get_crc32_truncated(fio_location location, const char *file_
162162
extern int fio_rename(fio_location location, const char* old_path, const char* new_path);
163163
extern int fio_symlink(fio_location location, const char* target, const char* link_path, bool overwrite);
164164
extern int fio_remove(fio_location location, const char* path, bool missing_ok);
165-
extern int fio_mkdir(fio_location location, const char* path, int mode);
165+
extern int fio_mkdir(fio_location location, const char* path, int mode, bool strict);
166166
extern int fio_chmod(fio_location location, const char* path, int mode);
167167
extern int fio_access(fio_location location, const char* path, int mode);
168168
extern int fio_stat(fio_location location, const char* path, struct stat* st, bool follow_symlinks);

0 commit comments

Comments
 (0)