summaryrefslogtreecommitdiff
path: root/src/backend/utils
diff options
context:
space:
mode:
authorTom Lane2017-12-04 22:02:52 +0000
committerTom Lane2017-12-04 22:02:56 +0000
commit2069e6faa0f72eba968714b2260cd65436d0ef3a (patch)
tree4ae75d85a0fe33c9386641ac08ca55915e99cc95 /src/backend/utils
parentab6eaee88420db58a948849d5a735997728d73a9 (diff)
Clean up assorted messiness around AllocateDir() usage.
This patch fixes a couple of low-probability bugs that could lead to reporting an irrelevant errno value (and hence possibly a wrong SQLSTATE) concerning directory-open or file-open failures. It also fixes places where we took shortcuts in reporting such errors, either by using elog instead of ereport or by using ereport but forgetting to specify an errcode. And it eliminates a lot of just plain redundant error-handling code. In service of all this, export fd.c's formerly-static function ReadDirExtended, so that external callers can make use of the coding pattern dir = AllocateDir(path); while ((de = ReadDirExtended(dir, path, LOG)) != NULL) if they'd like to treat directory-open failures as mere LOG conditions rather than errors. Also fix FreeDir to be a no-op if we reach it with dir == NULL, as such a coding pattern would cause. Then, remove code at many call sites that was throwing an error or log message for AllocateDir failure, as ReadDir or ReadDirExtended can handle that job just fine. Aside from being a net code savings, this gets rid of a lot of not-quite-up-to-snuff reports, as mentioned above. (In some places these changes result in replacing a custom error message such as "could not open tablespace directory" with more generic wording "could not open directory", but it was agreed that the custom wording buys little as long as we report the directory name.) In some other call sites where we can't just remove code, change the error reports to be fully project-style-compliant. Also reorder code in restoreTwoPhaseData that was acquiring a lock between AllocateDir and ReadDir; in the unlikely but surely not impossible case that LWLockAcquire changes errno, AllocateDir failures would be misreported. There is no great value in opening the directory before acquiring TwoPhaseStateLock, so just do it in the other order. Also fix CheckXLogRemoved to guarantee that it preserves errno, as quite a number of call sites are implicitly assuming. (Again, it's unlikely but I think not impossible that errno could change during a SpinLockAcquire. If so, this function was broken for its own purposes as well as breaking callers.) And change a few places that were using not-per-project-style messages, such as "could not read directory" when "could not open directory" is more correct. Back-patch the exporting of ReadDirExtended, in case we have occasion to back-patch some fix that makes use of it; it's not needed right now but surely making it global is pretty harmless. Also back-patch the restoreTwoPhaseData and CheckXLogRemoved fixes. The rest of this is essentially cosmetic and need not get back-patched. Michael Paquier, with a bit of additional work by me Discussion: https://postgr.es/m/CAB7nPqRpOCxjiirHmebEFhXVTK7V5Jvw4bz82p7Oimtsm3TyZA@mail.gmail.com
Diffstat (limited to 'src/backend/utils')
-rw-r--r--src/backend/utils/adt/dbsize.c5
-rw-r--r--src/backend/utils/adt/genfile.c2
-rw-r--r--src/backend/utils/adt/misc.c15
-rw-r--r--src/backend/utils/cache/relcache.c16
-rw-r--r--src/backend/utils/time/snapmgr.c26
5 files changed, 20 insertions, 44 deletions
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index 515e30d1777..58c0b01bdc8 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -110,11 +110,6 @@ calculate_database_size(Oid dbOid)
/* Scan the non-default tablespaces */
snprintf(dirpath, MAXPGPATH, "pg_tblspc");
dirdesc = AllocateDir(dirpath);
- if (!dirdesc)
- ereport(ERROR,
- (errcode_for_file_access(),
- errmsg("could not open tablespace directory \"%s\": %m",
- dirpath)));
while ((direntry = ReadDir(dirdesc, dirpath)) != NULL)
{
diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c
index b3b9fc522d3..04f1efbe4bc 100644
--- a/src/backend/utils/adt/genfile.c
+++ b/src/backend/utils/adt/genfile.c
@@ -508,7 +508,7 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char *dir)
if (!fctx->dirdesc)
ereport(ERROR,
(errcode_for_file_access(),
- errmsg("could not read directory \"%s\": %m",
+ errmsg("could not open directory \"%s\": %m",
fctx->location)));
funcctx->user_fctx = fctx;
diff --git a/src/backend/utils/adt/misc.c b/src/backend/utils/adt/misc.c
index 1980ff5ac72..f53d411ad1d 100644
--- a/src/backend/utils/adt/misc.c
+++ b/src/backend/utils/adt/misc.c
@@ -26,6 +26,7 @@
#include "catalog/pg_tablespace.h"
#include "catalog/pg_type.h"
#include "commands/dbcommands.h"
+#include "commands/tablespace.h"
#include "common/keywords.h"
#include "funcapi.h"
#include "miscadmin.h"
@@ -425,9 +426,9 @@ pg_tablespace_databases(PG_FUNCTION_ARGS)
while ((de = ReadDir(fctx->dirdesc, fctx->location)) != NULL)
{
- char *subdir;
- DIR *dirdesc;
Oid datOid = atooid(de->d_name);
+ char *subdir;
+ bool isempty;
/* this test skips . and .., but is awfully weak */
if (!datOid)
@@ -436,16 +437,10 @@ pg_tablespace_databases(PG_FUNCTION_ARGS)
/* if database subdir is empty, don't report tablespace as used */
subdir = psprintf("%s/%s", fctx->location, de->d_name);
- dirdesc = AllocateDir(subdir);
- while ((de = ReadDir(dirdesc, subdir)) != NULL)
- {
- if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
- break;
- }
- FreeDir(dirdesc);
+ isempty = directory_is_empty(subdir);
pfree(subdir);
- if (!de)
+ if (isempty)
continue; /* indeed, nothing in it */
SRF_RETURN_NEXT(funcctx, ObjectIdGetDatum(datOid));
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 1908420d827..12a5f157c0a 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -6119,14 +6119,8 @@ RelationCacheInitFileRemove(void)
/* Scan the tablespace link directory to find non-default tablespaces */
dir = AllocateDir(tblspcdir);
- if (dir == NULL)
- {
- elog(LOG, "could not open tablespace link directory \"%s\": %m",
- tblspcdir);
- return;
- }
- while ((de = ReadDir(dir, tblspcdir)) != NULL)
+ while ((de = ReadDirExtended(dir, tblspcdir, LOG)) != NULL)
{
if (strspn(de->d_name, "0123456789") == strlen(de->d_name))
{
@@ -6150,14 +6144,8 @@ RelationCacheInitFileRemoveInDir(const char *tblspcpath)
/* Scan the tablespace directory to find per-database directories */
dir = AllocateDir(tblspcpath);
- if (dir == NULL)
- {
- elog(LOG, "could not open tablespace directory \"%s\": %m",
- tblspcpath);
- return;
- }
- while ((de = ReadDir(dir, tblspcpath)) != NULL)
+ while ((de = ReadDirExtended(dir, tblspcpath, LOG)) != NULL)
{
if (strspn(de->d_name, "0123456789") == strlen(de->d_name))
{
diff --git a/src/backend/utils/time/snapmgr.c b/src/backend/utils/time/snapmgr.c
index addf87dc3bb..0b032905a5e 100644
--- a/src/backend/utils/time/snapmgr.c
+++ b/src/backend/utils/time/snapmgr.c
@@ -1619,27 +1619,25 @@ DeleteAllExportedSnapshotFiles(void)
DIR *s_dir;
struct dirent *s_de;
- if (!(s_dir = AllocateDir(SNAPSHOT_EXPORT_DIR)))
- {
- /*
- * We really should have that directory in a sane cluster setup. But
- * then again if we don't, it's not fatal enough to make it FATAL.
- * Since we're running in the postmaster, LOG is our best bet.
- */
- elog(LOG, "could not open directory \"%s\": %m", SNAPSHOT_EXPORT_DIR);
- return;
- }
+ /*
+ * Problems in reading the directory, or unlinking files, are reported at
+ * LOG level. Since we're running in the startup process, ERROR level
+ * would prevent database start, and it's not important enough for that.
+ */
+ s_dir = AllocateDir(SNAPSHOT_EXPORT_DIR);
- while ((s_de = ReadDir(s_dir, SNAPSHOT_EXPORT_DIR)) != NULL)
+ while ((s_de = ReadDirExtended(s_dir, SNAPSHOT_EXPORT_DIR, LOG)) != NULL)
{
if (strcmp(s_de->d_name, ".") == 0 ||
strcmp(s_de->d_name, "..") == 0)
continue;
snprintf(buf, sizeof(buf), SNAPSHOT_EXPORT_DIR "/%s", s_de->d_name);
- /* Again, unlink failure is not worthy of FATAL */
- if (unlink(buf))
- elog(LOG, "could not unlink file \"%s\": %m", buf);
+
+ if (unlink(buf) != 0)
+ ereport(LOG,
+ (errcode_for_file_access(),
+ errmsg("could not remove file \"%s\": %m", buf)));
}
FreeDir(s_dir);