summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2008-03-31 01:33:13 +0000
committerTom Lane2008-03-31 01:33:13 +0000
commit91813267b2763ea11020f62e7f9603a7b5335100 (patch)
treee18990dbb0469890200902e844606430e7835d6f
parentf93274d69fad753b8616000dd0233b219cff647e (diff)
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.)
-rw-r--r--src/backend/utils/adt/dbsize.c2
-rw-r--r--src/backend/utils/adt/genfile.c2
-rw-r--r--src/port/copydir.c4
-rw-r--r--src/port/exec.c8
-rw-r--r--src/test/regress/pg_regress.c2
5 files changed, 9 insertions, 9 deletions
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index d648d84303..a7ba411760 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -209,7 +209,7 @@ calculate_tablespace_size(Oid tblspcOid)
errmsg("could not stat file \"%s\": %m", pathname)));
}
- if (fst.st_mode & S_IFDIR)
+ if (S_ISDIR(fst.st_mode))
totalsize += db_dir_size(pathname);
totalsize += fst.st_size;
diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c
index 7835a632ba..211e17a27d 100644
--- a/src/backend/utils/adt/genfile.c
+++ b/src/backend/utils/adt/genfile.c
@@ -198,7 +198,7 @@ pg_stat_file(PG_FUNCTION_ARGS)
isnull[3] = true;
values[4] = TimestampTzGetDatum(time_t_to_timestamptz(fst.st_ctime));
#endif
- values[5] = BoolGetDatum(fst.st_mode & S_IFDIR);
+ values[5] = BoolGetDatum(S_ISDIR(fst.st_mode));
tuple = heap_form_tuple(tupdesc, values, isnull);
diff --git a/src/port/copydir.c b/src/port/copydir.c
index 73b227bf57..8d8c24f5bf 100644
--- a/src/port/copydir.c
+++ b/src/port/copydir.c
@@ -80,13 +80,13 @@ copydir(char *fromdir, char *todir, bool recurse)
(errcode_for_file_access(),
errmsg("could not stat file \"%s\": %m", fromfile)));
- if (fst.st_mode & S_IFDIR)
+ if (S_ISDIR(fst.st_mode))
{
/* recurse to handle subdirectories */
if (recurse)
copydir(fromfile, tofile, true);
}
- else if (fst.st_mode & S_IFREG)
+ else if (S_ISREG(fst.st_mode))
copy_file(fromfile, tofile);
}
diff --git a/src/port/exec.c b/src/port/exec.c
index fd8a2fba98..3210686dcb 100644
--- a/src/port/exec.c
+++ b/src/port/exec.c
@@ -80,8 +80,8 @@ validate_exec(const char *path)
#else
char path_exe[MAXPGPATH + sizeof(".exe") - 1];
#endif
- int is_r = 0;
- int is_x = 0;
+ int is_r;
+ int is_x;
#ifdef WIN32
/* Win32 requires a .exe suffix for stat() */
@@ -103,7 +103,7 @@ validate_exec(const char *path)
if (stat(path, &buf) < 0)
return -1;
- if ((buf.st_mode & S_IFMT) != S_IFREG)
+ if (!S_ISREG(buf.st_mode))
return -1;
/*
@@ -331,7 +331,7 @@ resolve_symlinks(char *path)
fname = path;
if (lstat(fname, &buf) < 0 ||
- (buf.st_mode & S_IFMT) != S_IFLNK)
+ !S_ISLNK(buf.st_mode))
break;
rllen = readlink(fname, link_buf, sizeof(link_buf));
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 3264c7e867..b028e343d4 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -1102,7 +1102,7 @@ directory_exists(const char *dir)
if (stat(dir, &st) != 0)
return false;
- if (st.st_mode & S_IFDIR)
+ if (S_ISDIR(st.st_mode))
return true;
return false;
}