Skip to content

Commit 17f1523

Browse files
committed
Warn about initdb using mount-points
Add code to detect and warn about trying to initdb or create pg_xlog on mount points.
1 parent 1bd42cd commit 17f1523

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

src/bin/initdb/initdb.c

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ void setup_signals(void);
257257
void setup_text_search(void);
258258
void create_data_directory(void);
259259
void create_xlog_symlink(void);
260+
void warn_on_mount_point(int error);
260261
void initialize_data_directory(void);
261262

262263

@@ -3144,7 +3145,9 @@ setup_signals(void)
31443145
void
31453146
create_data_directory(void)
31463147
{
3147-
switch (pg_check_dir(pg_data))
3148+
int ret;
3149+
3150+
switch ((ret = pg_check_dir(pg_data)))
31483151
{
31493152
case 0:
31503153
/* PGDATA not there, must create it */
@@ -3179,15 +3182,20 @@ create_data_directory(void)
31793182
break;
31803183

31813184
case 2:
3185+
case 3:
3186+
case 4:
31823187
/* Present and not empty */
31833188
fprintf(stderr,
31843189
_("%s: directory \"%s\" exists but is not empty\n"),
31853190
progname, pg_data);
3186-
fprintf(stderr,
3187-
_("If you want to create a new database system, either remove or empty\n"
3188-
"the directory \"%s\" or run %s\n"
3189-
"with an argument other than \"%s\".\n"),
3190-
pg_data, progname, pg_data);
3191+
if (ret != 4)
3192+
warn_on_mount_point(ret);
3193+
else
3194+
fprintf(stderr,
3195+
_("If you want to create a new database system, either remove or empty\n"
3196+
"the directory \"%s\" or run %s\n"
3197+
"with an argument other than \"%s\".\n"),
3198+
pg_data, progname, pg_data);
31913199
exit(1); /* no further message needed */
31923200

31933201
default:
@@ -3206,6 +3214,7 @@ create_xlog_symlink(void)
32063214
if (strcmp(xlog_dir, "") != 0)
32073215
{
32083216
char *linkloc;
3217+
int ret;
32093218

32103219
/* clean up xlog directory name, check it's absolute */
32113220
canonicalize_path(xlog_dir);
@@ -3216,7 +3225,7 @@ create_xlog_symlink(void)
32163225
}
32173226

32183227
/* check if the specified xlog directory exists/is empty */
3219-
switch (pg_check_dir(xlog_dir))
3228+
switch ((ret = pg_check_dir(xlog_dir)))
32203229
{
32213230
case 0:
32223231
/* xlog directory not there, must create it */
@@ -3255,14 +3264,19 @@ create_xlog_symlink(void)
32553264
break;
32563265

32573266
case 2:
3267+
case 3:
3268+
case 4:
32583269
/* Present and not empty */
32593270
fprintf(stderr,
32603271
_("%s: directory \"%s\" exists but is not empty\n"),
32613272
progname, xlog_dir);
3262-
fprintf(stderr,
3263-
_("If you want to store the transaction log there, either\n"
3264-
"remove or empty the directory \"%s\".\n"),
3265-
xlog_dir);
3273+
if (ret != 4)
3274+
warn_on_mount_point(ret);
3275+
else
3276+
fprintf(stderr,
3277+
_("If you want to store the transaction log there, either\n"
3278+
"remove or empty the directory \"%s\".\n"),
3279+
xlog_dir);
32663280
exit_nicely();
32673281

32683282
default:
@@ -3291,6 +3305,21 @@ create_xlog_symlink(void)
32913305
}
32923306

32933307

3308+
void
3309+
warn_on_mount_point(int error)
3310+
{
3311+
if (error == 2)
3312+
fprintf(stderr,
3313+
_("It contains a dot-prefixed/invisible file, perhaps due to it being a mount point.\n"));
3314+
else if (error == 3)
3315+
fprintf(stderr,
3316+
_("It contains a lost+found directory, perhaps due to it being a mount point.\n"));
3317+
3318+
fprintf(stderr,
3319+
_("Using the top-level directory of a mount point is not recommended.\n"));
3320+
}
3321+
3322+
32943323
void
32953324
initialize_data_directory(void)
32963325
{

src/bin/pg_basebackup/pg_basebackup.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ verify_dir_is_empty_or_create(char *dirname)
371371
*/
372372
return;
373373
case 2:
374+
case 3:
375+
case 4:
374376

375377
/*
376378
* Exists, not empty

src/port/pgcheckdir.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pg_check_dir(const char *dir)
3131
int result = 1;
3232
DIR *chkdir;
3333
struct dirent *file;
34+
bool dot_found = false;
3435

3536
errno = 0;
3637

@@ -47,15 +48,26 @@ pg_check_dir(const char *dir)
4748
/* skip this and parent directory */
4849
continue;
4950
}
51+
#ifndef WIN32
52+
/* file starts with "." */
53+
else if (file->d_name[0] == '.')
54+
{
55+
dot_found = true;
56+
}
57+
else if (strcmp("lost+found", file->d_name) == 0)
58+
{
59+
result = 3; /* not empty, mount point */
60+
break;
61+
}
62+
#endif
5063
else
5164
{
52-
result = 2; /* not empty */
65+
result = 4; /* not empty */
5366
break;
5467
}
5568
}
5669

5770
#ifdef WIN32
58-
5971
/*
6072
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
6173
* released version
@@ -69,5 +81,9 @@ pg_check_dir(const char *dir)
6981
if (errno != 0)
7082
result = -1; /* some kind of I/O error? */
7183

84+
/* We report on dot-files if we _only_ find dot files */
85+
if (result == 1 && dot_found)
86+
result = 2;
87+
7288
return result;
7389
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy