From cca48e1cf29c643e026a21edc677f544b585c18f Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 31 Mar 2008 01:32:17 +0000 Subject: [PATCH] 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.) --- src/backend/utils/adt/dbsize.c | 4 ++-- src/backend/utils/adt/genfile.c | 4 ++-- src/port/copydir.c | 6 +++--- src/port/exec.c | 10 +++++----- src/test/regress/pg_regress.c | 4 ++-- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c index dc27e249f3f..24467d866bb 100644 --- a/src/backend/utils/adt/dbsize.c +++ b/src/backend/utils/adt/dbsize.c @@ -5,7 +5,7 @@ * Copyright (c) 2002-2006, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.9.2.1 2007/03/11 06:44:11 alvherre Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.9.2.2 2008/03/31 01:32:17 tgl Exp $ * */ @@ -187,7 +187,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 38a129783ea..05ed12dd269 100644 --- a/src/backend/utils/adt/genfile.c +++ b/src/backend/utils/adt/genfile.c @@ -9,7 +9,7 @@ * Author: Andreas Pflug * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.13 2006/11/24 21:18:42 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.13.2.1 2008/03/31 01:32:17 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -201,7 +201,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 ef441e5ad3f..f12732cf212 100644 --- a/src/port/copydir.c +++ b/src/port/copydir.c @@ -11,7 +11,7 @@ * as a service. * * IDENTIFICATION - * $PostgreSQL: pgsql/src/port/copydir.c,v 1.18 2006/07/18 22:36:46 tgl Exp $ + * $PostgreSQL: pgsql/src/port/copydir.c,v 1.18.2.1 2008/03/31 01:32:17 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -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 1fc0dffaa13..5e7fb3e5b48 100644 --- a/src/port/exec.c +++ b/src/port/exec.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/port/exec.c,v 1.43.2.1 2008/02/29 15:31:37 mha Exp $ + * $PostgreSQL: pgsql/src/port/exec.c,v 1.43.2.2 2008/03/31 01:32:17 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -79,8 +79,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() */ @@ -102,7 +102,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; /* @@ -330,7 +330,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 5daf28a666d..be22c4b9815 100644 --- a/src/test/regress/pg_regress.c +++ b/src/test/regress/pg_regress.c @@ -11,7 +11,7 @@ * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.23 2006/10/04 00:30:14 momjian Exp $ + * $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.23.2.1 2008/03/31 01:32:17 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -784,7 +784,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; } -- 2.39.5