From 3f749924f82efd5b2f4b424f6c69a89a2959e4b3 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sun, 19 Jun 2005 21:34:03 +0000 Subject: Simplify uses of readdir() by creating a function ReadDir() that includes error checking and an appropriate ereport(ERROR) message. This gets rid of rather tedious and error-prone manipulation of errno, as well as a Windows-specific bug workaround, at more than a dozen call sites. After an idea in a recent patch by Heikki Linnakangas. --- src/backend/commands/tablespace.c | 51 +++------------------------------------ 1 file changed, 4 insertions(+), 47 deletions(-) (limited to 'src/backend/commands/tablespace.c') diff --git a/src/backend/commands/tablespace.c b/src/backend/commands/tablespace.c index fac20708c0..a469a8fa34 100644 --- a/src/backend/commands/tablespace.c +++ b/src/backend/commands/tablespace.c @@ -37,7 +37,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.21 2005/06/06 20:22:57 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.22 2005/06/19 21:34:01 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -519,23 +519,16 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo) pfree(location); return true; } - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not open directory \"%s\": %m", - location))); + /* else let ReadDir report the error */ } - errno = 0; - while ((de = readdir(dirdesc)) != NULL) + while ((de = ReadDir(dirdesc, location)) != NULL) { /* Note we ignore PG_VERSION for the nonce */ if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0 || strcmp(de->d_name, "PG_VERSION") == 0) - { - errno = 0; continue; - } subfile = palloc(strlen(location) + 1 + strlen(de->d_name) + 1); sprintf(subfile, "%s/%s", location, de->d_name); @@ -555,22 +548,8 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo) subfile))); pfree(subfile); - errno = 0; } -#ifdef WIN32 - /* - * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but - * not in released version - */ - if (GetLastError() == ERROR_NO_MORE_FILES) - errno = 0; -#endif - if (errno) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not read directory \"%s\": %m", - location))); FreeDir(dirdesc); /* @@ -685,38 +664,16 @@ directory_is_empty(const char *path) struct dirent *de; dirdesc = AllocateDir(path); - if (dirdesc == NULL) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not open directory \"%s\": %m", - path))); - errno = 0; - while ((de = readdir(dirdesc)) != NULL) + while ((de = ReadDir(dirdesc, path)) != NULL) { if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) - { - errno = 0; continue; - } FreeDir(dirdesc); return false; } -#ifdef WIN32 - /* - * This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but - * not in released version - */ - if (GetLastError() == ERROR_NO_MORE_FILES) - errno = 0; -#endif - if (errno) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not read directory \"%s\": %m", - path))); FreeDir(dirdesc); return true; } -- cgit v1.2.1