Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3f74992

Browse files
committed
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.
1 parent e26b0ab commit 3f74992

File tree

10 files changed

+88
-223
lines changed

10 files changed

+88
-223
lines changed

contrib/dbsize/dbsize.c

+4-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Copyright (c) 2002-2005, PostgreSQL Global Development Group
66
*
77
* IDENTIFICATION
8-
* $PostgreSQL: pgsql/contrib/dbsize/dbsize.c,v 1.17 2005/05/27 00:57:48 neilc Exp $
8+
* $PostgreSQL: pgsql/contrib/dbsize/dbsize.c,v 1.18 2005/06/19 21:34:00 tgl Exp $
99
*
1010
*/
1111

@@ -58,7 +58,7 @@ db_dir_size(const char *path)
5858
if (!dirdesc)
5959
return 0;
6060

61-
while ((direntry = readdir(dirdesc)) != NULL)
61+
while ((direntry = ReadDir(dirdesc, path)) != NULL)
6262
{
6363
struct stat fst;
6464

@@ -97,13 +97,8 @@ calculate_database_size(Oid dbOid)
9797
/* Scan the non-default tablespaces */
9898
snprintf(pathname, MAXPGPATH, "%s/pg_tblspc", DataDir);
9999
dirdesc = AllocateDir(pathname);
100-
if (!dirdesc)
101-
ereport(ERROR,
102-
(errcode_for_file_access(),
103-
errmsg("could not open tablespace directory \"%s\": %m",
104-
pathname)));
105100

106-
while ((direntry = readdir(dirdesc)) != NULL)
101+
while ((direntry = ReadDir(dirdesc, pathname)) != NULL)
107102
{
108103
if (strcmp(direntry->d_name, ".") == 0 ||
109104
strcmp(direntry->d_name, "..") == 0)
@@ -147,13 +142,7 @@ pg_tablespace_size(PG_FUNCTION_ARGS)
147142

148143
dirdesc = AllocateDir(tblspcPath);
149144

150-
if (!dirdesc)
151-
ereport(ERROR,
152-
(errcode_for_file_access(),
153-
errmsg("could not open tablespace directory \"%s\": %m",
154-
tblspcPath)));
155-
156-
while ((direntry = readdir(dirdesc)) != NULL)
145+
while ((direntry = ReadDir(dirdesc, tblspcPath)) != NULL)
157146
{
158147
struct stat fst;
159148

src/backend/access/transam/slru.c

+2-23
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
4949
* Portions Copyright (c) 1994, Regents of the University of California
5050
*
51-
* $PostgreSQL: pgsql/src/backend/access/transam/slru.c,v 1.24 2005/02/12 23:53:37 momjian Exp $
51+
* $PostgreSQL: pgsql/src/backend/access/transam/slru.c,v 1.25 2005/06/19 21:34:01 tgl Exp $
5252
*
5353
*-------------------------------------------------------------------------
5454
*/
@@ -924,14 +924,7 @@ SlruScanDirectory(SlruCtl ctl, int cutoffPage, bool doDeletions)
924924
cutoffPage -= cutoffPage % SLRU_PAGES_PER_SEGMENT;
925925

926926
cldir = AllocateDir(ctl->Dir);
927-
if (cldir == NULL)
928-
ereport(ERROR,
929-
(errcode_for_file_access(),
930-
errmsg("could not open directory \"%s\": %m",
931-
ctl->Dir)));
932-
933-
errno = 0;
934-
while ((clde = readdir(cldir)) != NULL)
927+
while ((clde = ReadDir(cldir, ctl->Dir)) != NULL)
935928
{
936929
if (strlen(clde->d_name) == 4 &&
937930
strspn(clde->d_name, "0123456789ABCDEF") == 4)
@@ -950,21 +943,7 @@ SlruScanDirectory(SlruCtl ctl, int cutoffPage, bool doDeletions)
950943
}
951944
}
952945
}
953-
errno = 0;
954946
}
955-
#ifdef WIN32
956-
957-
/*
958-
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
959-
* not in released version
960-
*/
961-
if (GetLastError() == ERROR_NO_MORE_FILES)
962-
errno = 0;
963-
#endif
964-
if (errno)
965-
ereport(ERROR,
966-
(errcode_for_file_access(),
967-
errmsg("could not read directory \"%s\": %m", ctl->Dir)));
968947
FreeDir(cldir);
969948

970949
return found;

src/backend/access/transam/twophase.c

+3-50
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/access/transam/twophase.c,v 1.4 2005/06/19 20:00:38 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/twophase.c,v 1.5 2005/06/19 21:34:01 tgl Exp $
1111
*
1212
* NOTES
1313
* Each global transaction is associated with a global transaction
@@ -1440,13 +1440,7 @@ PrescanPreparedTransactions(void)
14401440
snprintf(dir, MAXPGPATH, "%s/%s", DataDir, TWOPHASE_DIR);
14411441

14421442
cldir = AllocateDir(dir);
1443-
if (cldir == NULL)
1444-
ereport(ERROR,
1445-
(errcode_for_file_access(),
1446-
errmsg("could not open directory \"%s\": %m", dir)));
1447-
1448-
errno = 0;
1449-
while ((clde = readdir(cldir)) != NULL)
1443+
while ((clde = ReadDir(cldir, dir)) != NULL)
14501444
{
14511445
if (strlen(clde->d_name) == 8 &&
14521446
strspn(clde->d_name, "0123456789ABCDEF") == 8)
@@ -1466,7 +1460,6 @@ PrescanPreparedTransactions(void)
14661460
(errmsg("removing future twophase state file \"%s\"",
14671461
clde->d_name)));
14681462
RemoveTwoPhaseFile(xid, true);
1469-
errno = 0;
14701463
continue;
14711464
}
14721465

@@ -1483,7 +1476,6 @@ PrescanPreparedTransactions(void)
14831476
(errmsg("removing corrupt twophase state file \"%s\"",
14841477
clde->d_name)));
14851478
RemoveTwoPhaseFile(xid, true);
1486-
errno = 0;
14871479
continue;
14881480
}
14891481

@@ -1496,7 +1488,6 @@ PrescanPreparedTransactions(void)
14961488
clde->d_name)));
14971489
RemoveTwoPhaseFile(xid, true);
14981490
pfree(buf);
1499-
errno = 0;
15001491
continue;
15011492
}
15021493

@@ -1528,22 +1519,7 @@ PrescanPreparedTransactions(void)
15281519

15291520
pfree(buf);
15301521
}
1531-
errno = 0;
15321522
}
1533-
#ifdef WIN32
1534-
1535-
/*
1536-
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
1537-
* not in released version
1538-
*/
1539-
if (GetLastError() == ERROR_NO_MORE_FILES)
1540-
errno = 0;
1541-
#endif
1542-
if (errno)
1543-
ereport(ERROR,
1544-
(errcode_for_file_access(),
1545-
errmsg("could not read directory \"%s\": %m", dir)));
1546-
15471523
FreeDir(cldir);
15481524

15491525
return result;
@@ -1566,13 +1542,7 @@ RecoverPreparedTransactions(void)
15661542
snprintf(dir, MAXPGPATH, "%s/%s", DataDir, TWOPHASE_DIR);
15671543

15681544
cldir = AllocateDir(dir);
1569-
if (cldir == NULL)
1570-
ereport(ERROR,
1571-
(errcode_for_file_access(),
1572-
errmsg("could not open directory \"%s\": %m", dir)));
1573-
1574-
errno = 0;
1575-
while ((clde = readdir(cldir)) != NULL)
1545+
while ((clde = ReadDir(cldir, dir)) != NULL)
15761546
{
15771547
if (strlen(clde->d_name) == 8 &&
15781548
strspn(clde->d_name, "0123456789ABCDEF") == 8)
@@ -1594,7 +1564,6 @@ RecoverPreparedTransactions(void)
15941564
(errmsg("removing stale twophase state file \"%s\"",
15951565
clde->d_name)));
15961566
RemoveTwoPhaseFile(xid, true);
1597-
errno = 0;
15981567
continue;
15991568
}
16001569

@@ -1606,7 +1575,6 @@ RecoverPreparedTransactions(void)
16061575
(errmsg("removing corrupt twophase state file \"%s\"",
16071576
clde->d_name)));
16081577
RemoveTwoPhaseFile(xid, true);
1609-
errno = 0;
16101578
continue;
16111579
}
16121580

@@ -1655,22 +1623,7 @@ RecoverPreparedTransactions(void)
16551623

16561624
pfree(buf);
16571625
}
1658-
errno = 0;
16591626
}
1660-
#ifdef WIN32
1661-
1662-
/*
1663-
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
1664-
* not in released version
1665-
*/
1666-
if (GetLastError() == ERROR_NO_MORE_FILES)
1667-
errno = 0;
1668-
#endif
1669-
if (errno)
1670-
ereport(ERROR,
1671-
(errcode_for_file_access(),
1672-
errmsg("could not read directory \"%s\": %m", dir)));
1673-
16741627
FreeDir(cldir);
16751628
}
16761629

src/backend/access/transam/xlog.c

+3-33
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.202 2005/06/19 20:00:38 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.203 2005/06/19 21:34:01 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -2265,8 +2265,7 @@ MoveOfflineLogs(uint32 log, uint32 seg, XLogRecPtr endptr,
22652265

22662266
XLogFileName(lastoff, ThisTimeLineID, log, seg);
22672267

2268-
errno = 0;
2269-
while ((xlde = readdir(xldir)) != NULL)
2268+
while ((xlde = ReadDir(xldir, XLogDir)) != NULL)
22702269
{
22712270
/*
22722271
* We ignore the timeline part of the XLOG segment identifiers in
@@ -2326,22 +2325,8 @@ MoveOfflineLogs(uint32 log, uint32 seg, XLogRecPtr endptr,
23262325
XLogArchiveCleanup(xlde->d_name);
23272326
}
23282327
}
2329-
errno = 0;
23302328
}
2331-
#ifdef WIN32
23322329

2333-
/*
2334-
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
2335-
* not in released version
2336-
*/
2337-
if (GetLastError() == ERROR_NO_MORE_FILES)
2338-
errno = 0;
2339-
#endif
2340-
if (errno)
2341-
ereport(ERROR,
2342-
(errcode_for_file_access(),
2343-
errmsg("could not read transaction log directory \"%s\": %m",
2344-
XLogDir)));
23452330
FreeDir(xldir);
23462331
}
23472332

@@ -2362,8 +2347,7 @@ RemoveOldBackupHistory(void)
23622347
errmsg("could not open transaction log directory \"%s\": %m",
23632348
XLogDir)));
23642349

2365-
errno = 0;
2366-
while ((xlde = readdir(xldir)) != NULL)
2350+
while ((xlde = ReadDir(xldir, XLogDir)) != NULL)
23672351
{
23682352
if (strlen(xlde->d_name) > 24 &&
23692353
strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
@@ -2381,22 +2365,8 @@ RemoveOldBackupHistory(void)
23812365
XLogArchiveCleanup(xlde->d_name);
23822366
}
23832367
}
2384-
errno = 0;
23852368
}
2386-
#ifdef WIN32
23872369

2388-
/*
2389-
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
2390-
* not in released version
2391-
*/
2392-
if (GetLastError() == ERROR_NO_MORE_FILES)
2393-
errno = 0;
2394-
#endif
2395-
if (errno)
2396-
ereport(ERROR,
2397-
(errcode_for_file_access(),
2398-
errmsg("could not read transaction log directory \"%s\": %m",
2399-
XLogDir)));
24002370
FreeDir(xldir);
24012371
}
24022372

src/backend/commands/tablespace.c

+4-47
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.21 2005/06/06 20:22:57 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/commands/tablespace.c,v 1.22 2005/06/19 21:34:01 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -519,23 +519,16 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo)
519519
pfree(location);
520520
return true;
521521
}
522-
ereport(ERROR,
523-
(errcode_for_file_access(),
524-
errmsg("could not open directory \"%s\": %m",
525-
location)));
522+
/* else let ReadDir report the error */
526523
}
527524

528-
errno = 0;
529-
while ((de = readdir(dirdesc)) != NULL)
525+
while ((de = ReadDir(dirdesc, location)) != NULL)
530526
{
531527
/* Note we ignore PG_VERSION for the nonce */
532528
if (strcmp(de->d_name, ".") == 0 ||
533529
strcmp(de->d_name, "..") == 0 ||
534530
strcmp(de->d_name, "PG_VERSION") == 0)
535-
{
536-
errno = 0;
537531
continue;
538-
}
539532

540533
subfile = palloc(strlen(location) + 1 + strlen(de->d_name) + 1);
541534
sprintf(subfile, "%s/%s", location, de->d_name);
@@ -555,22 +548,8 @@ remove_tablespace_directories(Oid tablespaceoid, bool redo)
555548
subfile)));
556549

557550
pfree(subfile);
558-
errno = 0;
559551
}
560-
#ifdef WIN32
561552

562-
/*
563-
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
564-
* not in released version
565-
*/
566-
if (GetLastError() == ERROR_NO_MORE_FILES)
567-
errno = 0;
568-
#endif
569-
if (errno)
570-
ereport(ERROR,
571-
(errcode_for_file_access(),
572-
errmsg("could not read directory \"%s\": %m",
573-
location)));
574553
FreeDir(dirdesc);
575554

576555
/*
@@ -685,38 +664,16 @@ directory_is_empty(const char *path)
685664
struct dirent *de;
686665

687666
dirdesc = AllocateDir(path);
688-
if (dirdesc == NULL)
689-
ereport(ERROR,
690-
(errcode_for_file_access(),
691-
errmsg("could not open directory \"%s\": %m",
692-
path)));
693667

694-
errno = 0;
695-
while ((de = readdir(dirdesc)) != NULL)
668+
while ((de = ReadDir(dirdesc, path)) != NULL)
696669
{
697670
if (strcmp(de->d_name, ".") == 0 ||
698671
strcmp(de->d_name, "..") == 0)
699-
{
700-
errno = 0;
701672
continue;
702-
}
703673
FreeDir(dirdesc);
704674
return false;
705675
}
706-
#ifdef WIN32
707676

708-
/*
709-
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but
710-
* not in released version
711-
*/
712-
if (GetLastError() == ERROR_NO_MORE_FILES)
713-
errno = 0;
714-
#endif
715-
if (errno)
716-
ereport(ERROR,
717-
(errcode_for_file_access(),
718-
errmsg("could not read directory \"%s\": %m",
719-
path)));
720677
FreeDir(dirdesc);
721678
return true;
722679
}

0 commit comments

Comments
 (0)