Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c5f11f9

Browse files
committed
Fix a number of places that were making file-type tests infelicitously.
The places that did, eg, (statbuf.st_mode & S_IFMT) == S_IFDIR were correct, but there is no good reason not to use S_ISDIR() instead, especially when that's what the other 90% of our code does. The places that did, eg, (statbuf.st_mode & S_IFDIR) were flat out *wrong* and would fail in various platform-specific ways, eg a symlink could be mistaken for a regular file on most Unixen. The actual impact of this is probably small, since the problem cases seem to always involve symlinks or sockets, which are unlikely to be found in the directories that PG code might be scanning. But it's clearly trouble waiting to happen, so patch all the way back anyway. (There seem to be no occurrences of the mistake in 7.4.)
1 parent b65a509 commit c5f11f9

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

src/backend/utils/adt/dbsize.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Copyright (c) 2002-2008, PostgreSQL Global Development Group
66
*
77
* IDENTIFICATION
8-
* $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.17 2008/03/25 22:42:44 tgl Exp $
8+
* $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.18 2008/03/31 01:31:43 tgl Exp $
99
*
1010
*/
1111

@@ -209,7 +209,7 @@ calculate_tablespace_size(Oid tblspcOid)
209209
errmsg("could not stat file \"%s\": %m", pathname)));
210210
}
211211

212-
if (fst.st_mode & S_IFDIR)
212+
if (S_ISDIR(fst.st_mode))
213213
totalsize += db_dir_size(pathname);
214214

215215
totalsize += fst.st_size;

src/backend/utils/adt/genfile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Author: Andreas Pflug <pgadmin@pse-consulting.de>
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.18 2008/03/25 22:42:44 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.19 2008/03/31 01:31:43 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -198,7 +198,7 @@ pg_stat_file(PG_FUNCTION_ARGS)
198198
isnull[3] = true;
199199
values[4] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_ctime));
200200
#endif
201-
values[5] = BoolGetDatum(fst.st_mode & S_IFDIR);
201+
values[5] = BoolGetDatum(S_ISDIR(fst.st_mode));
202202

203203
tuple = heap_form_tuple(tupdesc, values, isnull);
204204

src/port/copydir.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* as a service.
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/port/copydir.c,v 1.21 2008/01/01 19:46:00 momjian Exp $
14+
* $PostgreSQL: pgsql/src/port/copydir.c,v 1.22 2008/03/31 01:31:43 tgl Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -80,13 +80,13 @@ copydir(char *fromdir, char *todir, bool recurse)
8080
(errcode_for_file_access(),
8181
errmsg("could not stat file \"%s\": %m", fromfile)));
8282

83-
if (fst.st_mode & S_IFDIR)
83+
if (S_ISDIR(fst.st_mode))
8484
{
8585
/* recurse to handle subdirectories */
8686
if (recurse)
8787
copydir(fromfile, tofile, true);
8888
}
89-
else if (fst.st_mode & S_IFREG)
89+
else if (S_ISREG(fst.st_mode))
9090
copy_file(fromfile, tofile);
9191
}
9292

src/port/exec.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/port/exec.c,v 1.58 2008/02/29 15:31:33 mha Exp $
12+
* $PostgreSQL: pgsql/src/port/exec.c,v 1.59 2008/03/31 01:31:43 tgl Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -80,8 +80,8 @@ validate_exec(const char *path)
8080
#else
8181
char path_exe[MAXPGPATH + sizeof(".exe") - 1];
8282
#endif
83-
int is_r = 0;
84-
int is_x = 0;
83+
int is_r;
84+
int is_x;
8585

8686
#ifdef WIN32
8787
/* Win32 requires a .exe suffix for stat() */
@@ -103,7 +103,7 @@ validate_exec(const char *path)
103103
if (stat(path, &buf) < 0)
104104
return -1;
105105

106-
if ((buf.st_mode & S_IFMT) != S_IFREG)
106+
if (!S_ISREG(buf.st_mode))
107107
return -1;
108108

109109
/*
@@ -331,7 +331,7 @@ resolve_symlinks(char *path)
331331
fname = path;
332332

333333
if (lstat(fname, &buf) < 0 ||
334-
(buf.st_mode & S_IFMT) != S_IFLNK)
334+
!S_ISLNK(buf.st_mode))
335335
break;
336336

337337
rllen = readlink(fname, link_buf, sizeof(link_buf));

src/test/regress/pg_regress.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
14-
* $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.43 2008/03/04 15:38:31 mha Exp $
14+
* $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.44 2008/03/31 01:31:43 tgl Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -1102,7 +1102,7 @@ directory_exists(const char *dir)
11021102

11031103
if (stat(dir, &st) != 0)
11041104
return false;
1105-
if (st.st_mode & S_IFDIR)
1105+
if (S_ISDIR(st.st_mode))
11061106
return true;
11071107
return false;
11081108
}

0 commit comments

Comments
 (0)