]> granicus.if.org Git - postgresql/commitdiff
Fix a number of places that were making file-type tests infelicitously.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 31 Mar 2008 01:32:01 +0000 (01:32 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 31 Mar 2008 01:32:01 +0000 (01:32 +0000)
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
src/backend/utils/adt/genfile.c
src/port/copydir.c
src/port/exec.c
src/test/regress/pg_regress.c

index 00ac3b0e094796db118975f2c7f516b82eb3c758..c5c3314c4d3220aca502fa9d6d7f5ca379d90b8a 100644 (file)
@@ -5,7 +5,7 @@
  * Copyright (c) 2002-2008, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.16 2008/01/01 19:45:52 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.16.2.1 2008/03/31 01:32:01 tgl Exp $
  *
  */
 
@@ -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;
index a3a16bcfa50c488230ad71302b3103be52496fad..6c664c8887a33041e6940167042ebc75f1008561 100644 (file)
@@ -9,7 +9,7 @@
  * Author: Andreas Pflug <pgadmin@pse-consulting.de>
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.17 2008/01/01 19:45:52 momjian Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/adt/genfile.c,v 1.17.2.1 2008/03/31 01:32:01 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);
 
index 540384597cae5f7ed3ba6df2b15ca4b4fbfde9b1..ccad931d76ae5b5806aabf58c1646e9b074ccac8 100644 (file)
@@ -11,7 +11,7 @@
  *     as a service.
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/port/copydir.c,v 1.21 2008/01/01 19:46:00 momjian Exp $
+ *       $PostgreSQL: pgsql/src/port/copydir.c,v 1.21.2.1 2008/03/31 01:32:01 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);
        }
 
index 0de2c353ad735d275a06f073c4da18051ab57eb8..c37dd33d85bc470b3e8ca61861b4b880a71b1307 100644 (file)
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/port/exec.c,v 1.57.2.1 2008/02/29 15:31:41 mha Exp $
+ *       $PostgreSQL: pgsql/src/port/exec.c,v 1.57.2.2 2008/03/31 01:32:01 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -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));
index 7c4ee404f545bfee1bc0908656cd5caa149a943c..75b0ea53422968ecadc48392fc4bb51ef363426d 100644 (file)
@@ -11,7 +11,7 @@
  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.41.2.1 2008/03/04 15:38:33 mha Exp $
+ * $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.41.2.2 2008/03/31 01:32:01 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -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;
 }