Improve pg_check_dir code and comments.
authorRobert Haas <[email protected]>
Tue, 17 Feb 2015 15:19:30 +0000 (10:19 -0500)
committerRobert Haas <[email protected]>
Tue, 17 Feb 2015 15:19:30 +0000 (10:19 -0500)
Avoid losing errno if readdir() fails and closedir() works.  Consistently
return 4 rather than 3 if both a lost+found directory and other files are
found, rather than returning one value or the other depending on the
order of the directory listing.  Update comments to match the actual
behavior.

These oversights date to commits 6f03927fce038096f53ca67eeab9adb24938f8a6
and 17f15239325a88581bb4f9cf91d38005f1f52d69.

Marco Nenciarini

src/port/pgcheckdir.c

index 706189369ce4cb8e3cd1cadad883837917970722..7102f2c741f203e1c4c26c91663cd3d4d1138acb 100644 (file)
@@ -22,7 +22,9 @@
  * Returns:
  *     0 if nonexistent
  *     1 if exists and empty
- *     2 if exists and not empty
+ *     2 if exists and contains _only_ dot files
+ *     3 if exists and contains a mount point
+ *     4 if exists and not empty
  *     -1 if trouble accessing directory (errno reflects the error)
  */
 int
@@ -32,6 +34,8 @@ pg_check_dir(const char *dir)
    DIR        *chkdir;
    struct dirent *file;
    bool        dot_found = false;
+   bool        mount_found = false;
+   int         readdir_errno;
 
    chkdir = opendir(dir);
    if (chkdir == NULL)
@@ -51,10 +55,10 @@ pg_check_dir(const char *dir)
        {
            dot_found = true;
        }
+       /* lost+found directory */
        else if (strcmp("lost+found", file->d_name) == 0)
        {
-           result = 3;         /* not empty, mount point */
-           break;
+           mount_found = true;
        }
 #endif
        else
@@ -64,9 +68,20 @@ pg_check_dir(const char *dir)
        }
    }
 
-   if (errno || closedir(chkdir))
+   if (errno)
        result = -1;            /* some kind of I/O error? */
 
+   /* Close chkdir and avoid overwriting the readdir errno on success */
+   readdir_errno = errno;
+   if (closedir(chkdir))
+       result = -1;            /* error executing closedir */
+   else
+       errno = readdir_errno;
+
+   /* We report on mount point if we find a lost+found directory */
+   if (result == 1 && mount_found)
+       result = 3;
+
    /* We report on dot-files if we _only_ find dot files */
    if (result == 1 && dot_found)
        result = 2;