diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2005-06-19 21:34:03 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2005-06-19 21:34:03 +0000 |
commit | 3f749924f82efd5b2f4b424f6c69a89a2959e4b3 (patch) | |
tree | 3bd4c6588dc45f0b88f394e0dee85f201f54d4d0 /src/backend/access | |
parent | e26b0abda3919448d5ccbcaac0415010022864b7 (diff) | |
download | postgresql-3f749924f82efd5b2f4b424f6c69a89a2959e4b3.tar.gz postgresql-3f749924f82efd5b2f4b424f6c69a89a2959e4b3.zip |
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.
Diffstat (limited to 'src/backend/access')
-rw-r--r-- | src/backend/access/transam/slru.c | 25 | ||||
-rw-r--r-- | src/backend/access/transam/twophase.c | 53 | ||||
-rw-r--r-- | src/backend/access/transam/xlog.c | 36 |
3 files changed, 8 insertions, 106 deletions
diff --git a/src/backend/access/transam/slru.c b/src/backend/access/transam/slru.c index cfc4fd67ea9..24f9a947792 100644 --- a/src/backend/access/transam/slru.c +++ b/src/backend/access/transam/slru.c @@ -48,7 +48,7 @@ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/access/transam/slru.c,v 1.24 2005/02/12 23:53:37 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/access/transam/slru.c,v 1.25 2005/06/19 21:34:01 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -924,14 +924,7 @@ SlruScanDirectory(SlruCtl ctl, int cutoffPage, bool doDeletions) cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT; cldir = AllocateDir(ctl->Dir); - if (cldir == NULL) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not open directory \"%s\": %m", - ctl->Dir))); - - errno = 0; - while ((clde = readdir(cldir)) != NULL) + while ((clde = ReadDir(cldir, ctl->Dir)) != NULL) { if (strlen(clde->d_name) == 4 && strspn(clde->d_name, "0123456789ABCDEF") == 4) @@ -950,21 +943,7 @@ SlruScanDirectory(SlruCtl ctl, int cutoffPage, bool doDeletions) } } } - 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", ctl->Dir))); FreeDir(cldir); return found; diff --git a/src/backend/access/transam/twophase.c b/src/backend/access/transam/twophase.c index 1aa9ce9b025..6b78467ae15 100644 --- a/src/backend/access/transam/twophase.c +++ b/src/backend/access/transam/twophase.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/access/transam/twophase.c,v 1.4 2005/06/19 20:00:38 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/access/transam/twophase.c,v 1.5 2005/06/19 21:34:01 tgl Exp $ * * NOTES * Each global transaction is associated with a global transaction @@ -1440,13 +1440,7 @@ PrescanPreparedTransactions(void) snprintf(dir, MAXPGPATH, "%s/%s", DataDir, TWOPHASE_DIR); cldir = AllocateDir(dir); - if (cldir == NULL) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not open directory \"%s\": %m", dir))); - - errno = 0; - while ((clde = readdir(cldir)) != NULL) + while ((clde = ReadDir(cldir, dir)) != NULL) { if (strlen(clde->d_name) == 8 && strspn(clde->d_name, "0123456789ABCDEF") == 8) @@ -1466,7 +1460,6 @@ PrescanPreparedTransactions(void) (errmsg("removing future twophase state file \"%s\"", clde->d_name))); RemoveTwoPhaseFile(xid, true); - errno = 0; continue; } @@ -1483,7 +1476,6 @@ PrescanPreparedTransactions(void) (errmsg("removing corrupt twophase state file \"%s\"", clde->d_name))); RemoveTwoPhaseFile(xid, true); - errno = 0; continue; } @@ -1496,7 +1488,6 @@ PrescanPreparedTransactions(void) clde->d_name))); RemoveTwoPhaseFile(xid, true); pfree(buf); - errno = 0; continue; } @@ -1528,22 +1519,7 @@ PrescanPreparedTransactions(void) pfree(buf); } - 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", dir))); - FreeDir(cldir); return result; @@ -1566,13 +1542,7 @@ RecoverPreparedTransactions(void) snprintf(dir, MAXPGPATH, "%s/%s", DataDir, TWOPHASE_DIR); cldir = AllocateDir(dir); - if (cldir == NULL) - ereport(ERROR, - (errcode_for_file_access(), - errmsg("could not open directory \"%s\": %m", dir))); - - errno = 0; - while ((clde = readdir(cldir)) != NULL) + while ((clde = ReadDir(cldir, dir)) != NULL) { if (strlen(clde->d_name) == 8 && strspn(clde->d_name, "0123456789ABCDEF") == 8) @@ -1594,7 +1564,6 @@ RecoverPreparedTransactions(void) (errmsg("removing stale twophase state file \"%s\"", clde->d_name))); RemoveTwoPhaseFile(xid, true); - errno = 0; continue; } @@ -1606,7 +1575,6 @@ RecoverPreparedTransactions(void) (errmsg("removing corrupt twophase state file \"%s\"", clde->d_name))); RemoveTwoPhaseFile(xid, true); - errno = 0; continue; } @@ -1655,22 +1623,7 @@ RecoverPreparedTransactions(void) pfree(buf); } - 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", dir))); - FreeDir(cldir); } diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index b15949b2854..3a55a521c17 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -7,7 +7,7 @@ * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.202 2005/06/19 20:00:38 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.203 2005/06/19 21:34:01 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -2265,8 +2265,7 @@ MoveOfflineLogs(uint32 log, uint32 seg, XLogRecPtr endptr, XLogFileName(lastoff, ThisTimeLineID, log, seg); - errno = 0; - while ((xlde = readdir(xldir)) != NULL) + while ((xlde = ReadDir(xldir, XLogDir)) != NULL) { /* * We ignore the timeline part of the XLOG segment identifiers in @@ -2326,22 +2325,8 @@ MoveOfflineLogs(uint32 log, uint32 seg, XLogRecPtr endptr, XLogArchiveCleanup(xlde->d_name); } } - 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 transaction log directory \"%s\": %m", - XLogDir))); FreeDir(xldir); } @@ -2362,8 +2347,7 @@ RemoveOldBackupHistory(void) errmsg("could not open transaction log directory \"%s\": %m", XLogDir))); - errno = 0; - while ((xlde = readdir(xldir)) != NULL) + while ((xlde = ReadDir(xldir, XLogDir)) != NULL) { if (strlen(xlde->d_name) > 24 && strspn(xlde->d_name, "0123456789ABCDEF") == 24 && @@ -2381,22 +2365,8 @@ RemoveOldBackupHistory(void) XLogArchiveCleanup(xlde->d_name); } } - 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 transaction log directory \"%s\": %m", - XLogDir))); FreeDir(xldir); } |