Skip to content

Commit bb1bd32

Browse files
committed
Adjust mkdir_p to do stat() before trying mkdir(). Avoids problems on
Solaris and should be a little faster anyway, since in most scenarios all but perhaps the last path component will already exist.
1 parent 5ae5e3b commit bb1bd32

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

src/bin/initdb/initdb.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* Portions Copyright (c) 1994, Regents of the University of California
4040
* Portions taken from FreeBSD.
4141
*
42-
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.73 2005/01/08 22:51:12 tgl Exp $
42+
* $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.74 2005/01/28 00:34:32 tgl Exp $
4343
*
4444
*-------------------------------------------------------------------------
4545
*/
@@ -476,6 +476,9 @@ popen_check(const char *command, const char *mode)
476476
* this tries to build all the elements of a path to a directory a la mkdir -p
477477
* we assume the path is in canonical form, i.e. uses / as the separator
478478
* we also assume it isn't null.
479+
*
480+
* note that on failure, the path arg has been modified to show the particular
481+
* directory level we had problems with.
479482
*/
480483
static int
481484
mkdir_p(char *path, mode_t omode)
@@ -544,31 +547,25 @@ mkdir_p(char *path, mode_t omode)
544547
}
545548
if (last)
546549
(void) umask(oumask);
547-
if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0)
550+
551+
/* check for pre-existing directory; ok if it's a parent */
552+
if (stat(path, &sb) == 0)
548553
{
549-
if (errno == EEXIST || errno == EISDIR)
550-
{
551-
if (stat(path, &sb) < 0)
552-
{
553-
retval = 1;
554-
break;
555-
}
556-
else if (!S_ISDIR(sb.st_mode))
557-
{
558-
if (last)
559-
errno = EEXIST;
560-
else
561-
errno = ENOTDIR;
562-
retval = 1;
563-
break;
564-
}
565-
}
566-
else
554+
if (!S_ISDIR(sb.st_mode))
567555
{
556+
if (last)
557+
errno = EEXIST;
558+
else
559+
errno = ENOTDIR;
568560
retval = 1;
569561
break;
570562
}
571563
}
564+
else if (mkdir(path, last ? omode : S_IRWXU | S_IRWXG | S_IRWXO) < 0)
565+
{
566+
retval = 1;
567+
break;
568+
}
572569
if (!last)
573570
*p = '/';
574571
}

0 commit comments

Comments
 (0)