Skip to content

Commit 022903f

Browse files
committed
Reduce open() calls. Replace fopen() calls with calls to fd.c functions.
1 parent eaae21f commit 022903f

File tree

11 files changed

+74
-106
lines changed

11 files changed

+74
-106
lines changed

src/backend/commands/copy.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.24 1997/06/12 15:39:44 vadim Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.25 1997/08/18 02:14:34 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -34,6 +34,7 @@
3434
#include <catalog/catname.h>
3535
#include <catalog/pg_user.h>
3636
#include <commands/copy.h>
37+
#include <storage/fd.h>
3738

3839
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
3940
#define VALUE(c) ((c) - '0')
@@ -127,7 +128,7 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
127128
fp = Pfin;
128129
} else fp = stdin;
129130
} else {
130-
fp = fopen(filename, "r");
131+
fp = AllocateFile(filename, "r");
131132
if (fp == NULL)
132133
elog(WARN, "COPY command, running in backend with "
133134
"effective uid %d, could not open file '%s' for "
@@ -145,7 +146,7 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
145146
} else {
146147
mode_t oumask; /* Pre-existing umask value */
147148
oumask = umask((mode_t) 0);
148-
fp = fopen(filename, "w");
149+
fp = AllocateFile(filename, "w");
149150
umask(oumask);
150151
if (fp == NULL)
151152
elog(WARN, "COPY command, running in backend with "
@@ -156,7 +157,8 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
156157
}
157158
CopyTo(rel, binary, oids, fp, delim);
158159
}
159-
if (!pipe) fclose(fp);
160+
if (!pipe)
161+
FreeFile(fp);
160162
else if (!from && !binary) {
161163
fputs("\\.\n", fp);
162164
if (IsUnderPostmaster) fflush(Pfout);

src/backend/libpq/hba.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.17 1997/08/12 22:52:52 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.18 1997/08/18 02:14:37 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -28,7 +28,7 @@
2828
#include <libpq/pqcomm.h>
2929
#include <libpq/hba.h>
3030
#include <port/inet_aton.h> /* For inet_aton() */
31-
31+
#include <storage/fd.h>
3232

3333
/* Some standard C libraries, including GNU, have an isblank() function.
3434
Others, including Solaris, do not. So we have our own.
@@ -334,8 +334,8 @@ find_hba_entry(const char DataDir[], const struct in_addr ip_addr,
334334
strlen(CONF_FILE)+2)*sizeof(char));
335335
sprintf(conf_file, "%s/%s", DataDir, CONF_FILE);
336336

337-
file = fopen(conf_file, "r");
338-
if (file == 0) {
337+
file = AllocateFile(conf_file, "r");
338+
if (file == NULL) {
339339
/* The open of the config file failed. */
340340

341341
*host_ok_p = false;
@@ -350,7 +350,7 @@ find_hba_entry(const char DataDir[], const struct in_addr ip_addr,
350350
} else {
351351
process_open_config_file(file, ip_addr, database, host_ok_p, userauth_p,
352352
usermap_name, find_password_entries);
353-
fclose(file);
353+
FreeFile(file);
354354
}
355355
free(conf_file);
356356
}
@@ -636,8 +636,8 @@ verify_against_usermap(const char DataDir[],
636636
strlen(MAP_FILE)+2)*sizeof(char));
637637
sprintf(map_file, "%s/%s", DataDir, MAP_FILE);
638638

639-
file = fopen(map_file, "r");
640-
if (file == 0) {
639+
file = AllocateFile(map_file, "r");
640+
if (file == NULL) {
641641
/* The open of the map file failed. */
642642

643643
*checks_out_p = false;
@@ -654,7 +654,7 @@ verify_against_usermap(const char DataDir[],
654654
verify_against_open_usermap(file,
655655
pguser, ident_username, usermap_name,
656656
checks_out_p);
657-
fclose(file);
657+
FreeFile(file);
658658
}
659659
free(map_file);
660660

src/backend/libpq/password.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <libpq/password.h>
33
#include <libpq/hba.h>
44
#include <libpq/libpq.h>
5+
#include <storage/fd.h>
56
#include <string.h>
67
#include <unistd.h>
78
#ifdef HAVE_CRYPT_H
@@ -56,7 +57,7 @@ verify_password(char *user, char *password, Port *port,
5657
strcat(pw_file_fullname, "/");
5758
strcat(pw_file_fullname, pw_file_name);
5859

59-
pw_file = fopen(pw_file_fullname, "r");
60+
pw_file = AllocateFile(pw_file_fullname, "r");
6061
if(!pw_file) {
6162
sprintf(PQerrormsg,
6263
"verify_password: couldn't open password file '%s'\n",
@@ -84,7 +85,7 @@ verify_password(char *user, char *password, Port *port,
8485

8586
if(strcmp(user, test_user) == 0) {
8687
/* we're outta here one way or the other. */
87-
fclose(pw_file);
88+
FreeFile(pw_file);
8889

8990
if(strcmp(crypt(password, salt), test_pw) == 0) {
9091
/* it matched. */

src/backend/optimizer/geqo/geqo_params.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* Copyright (c) 1994, Regents of the University of California
77
*
8-
* $Id: geqo_params.c,v 1.4 1997/08/12 22:53:09 momjian Exp $
8+
* $Id: geqo_params.c,v 1.5 1997/08/18 02:14:41 momjian Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -43,6 +43,8 @@
4343
#include "optimizer/geqo_gene.h"
4444
#include "optimizer/geqo.h"
4545

46+
#include "storage/fd.h"
47+
4648
#define POOL_TAG "Pool_Size"
4749
#define TRIAL_TAG "Generations"
4850
#define RAND_TAG "Random_Seed"
@@ -89,7 +91,7 @@ geqo_params(int string_length)
8991
sprintf(conf_file, "%s/%s", DataDir, GEQO_FILE);
9092

9193
/* open the config file */
92-
file = fopen(conf_file, "r");
94+
file = AllocateFile(conf_file, "r");
9395
if (file)
9496
{
9597
/*
@@ -187,7 +189,7 @@ geqo_params(int string_length)
187189
}
188190
}
189191

190-
fclose(file);
192+
FreeFile(file);
191193

192194
pfree(conf_file);
193195
}

src/backend/parser/dbcommands.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/parser/Attic/dbcommands.c,v 1.3 1997/01/10 20:18:20 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/parser/Attic/dbcommands.c,v 1.4 1997/08/18 02:14:44 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -31,6 +31,7 @@
3131
#include "tcop/tcopprot.h"
3232
#include "storage/bufmgr.h"
3333
#include "storage/lmgr.h"
34+
#include "storage/fd.h"
3435

3536

3637
/* non-export function prototypes */
@@ -249,9 +250,9 @@ stop_vacuum(char *dbname)
249250

250251
sprintf(filename, "%s%cbase%c%s%c%s.vacuum", DataDir, SEP_CHAR, SEP_CHAR,
251252
dbname, SEP_CHAR, dbname);
252-
if ((fp = fopen(filename, "r")) != (FILE *) NULL) {
253+
if ((fp = AllocateFile(filename, "r")) != NULL) {
253254
fscanf(fp, "%d", &pid);
254-
fclose(fp);
255+
FreeFile(fp);
255256
if (kill(pid, SIGKILLDAEMON1) < 0) {
256257
elog(WARN, "can't kill vacuum daemon (pid %d) on %s",
257258
pid, dbname);

src/backend/storage/buffer/bufmgr.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.16 1997/08/12 22:53:46 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.17 1997/08/18 02:14:49 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -1663,7 +1663,7 @@ _bm_die(Oid dbId, Oid relId, int blkNo, int bufNo,
16631663

16641664
tb = &TraceBuf[cur];
16651665

1666-
if ((fp = fopen("/tmp/death_notice", "w")) == (FILE *) NULL)
1666+
if ((fp = AllocateFile("/tmp/death_notice", "w")) == NULL)
16671667
elog(FATAL, "buffer alloc trace error and can't open log file");
16681668

16691669
fprintf(fp, "buffer alloc trace detected the following error:\n\n");
@@ -1728,7 +1728,7 @@ _bm_die(Oid dbId, Oid relId, int blkNo, int bufNo,
17281728
break;
17291729
}
17301730

1731-
fclose(fp);
1731+
FreeFile(fp);
17321732

17331733
kill(getpid(), SIGILL);
17341734
}

src/backend/storage/file/fd.c

Lines changed: 26 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright (c) 1994, Regents of the University of California
77
*
88
* IDENTIFICATION
9-
* $Id: fd.c,v 1.20 1997/08/12 22:53:51 momjian Exp $
9+
* $Id: fd.c,v 1.21 1997/08/18 02:14:50 momjian Exp $
1010
*
1111
* NOTES:
1212
*
@@ -124,12 +124,6 @@ static Size SizeVfdCache = 0;
124124
*/
125125
static int nfile = 0;
126126

127-
/*
128-
* we use the name of the null device in various places, mostly so
129-
* that we can open it and find out if we really have any descriptors
130-
* available or not.
131-
*/
132-
static char *Nulldev = "/dev/null";
133127
static char Sep_char = '/';
134128

135129
/*
@@ -312,7 +306,6 @@ LruInsert (File file)
312306
vfdP = &VfdCache[file];
313307

314308
if (FileIsNotOpen(file)) {
315-
int tmpfd;
316309

317310
if ( nfile >= pg_nofile() )
318311
AssertLruRoom();
@@ -324,16 +317,13 @@ LruInsert (File file)
324317
* should be able to open all the time. If this fails, we
325318
* assume this is because there's no free file descriptors.
326319
*/
327-
tryAgain:
328-
tmpfd = open(Nulldev, O_CREAT|O_RDWR, 0666);
329-
if (tmpfd < 0) {
320+
tryAgain:
321+
vfdP->fd = open(vfdP->fileName,vfdP->fileFlags,vfdP->fileMode);
322+
if (vfdP->fd < 0 && (errno == EMFILE || errno == ENFILE)) {
330323
errno = 0;
331324
AssertLruRoom();
332325
goto tryAgain;
333-
} else {
334-
close(tmpfd);
335326
}
336-
vfdP->fd = open(vfdP->fileName,vfdP->fileFlags,vfdP->fileMode);
337327

338328
if (vfdP->fd < 0) {
339329
DO_DB(elog(DEBUG, "RE_OPEN FAILED: %d",
@@ -530,7 +520,6 @@ fileNameOpenFile(FileName fileName,
530520
{
531521
File file;
532522
Vfd *vfdP;
533-
int tmpfd;
534523

535524
DO_DB(elog(DEBUG, "fileNameOpenFile: %s %x %o",
536525
fileName, fileFlags, fileMode));
@@ -542,18 +531,15 @@ fileNameOpenFile(FileName fileName,
542531
AssertLruRoom();
543532

544533
tryAgain:
545-
tmpfd = open(Nulldev, O_CREAT|O_RDWR, 0666);
546-
if (tmpfd < 0) {
534+
vfdP->fd = open(fileName,fileFlags,fileMode);
535+
if (vfdP->fd < 0 && (errno == EMFILE || errno == ENFILE)) {
547536
DO_DB(elog(DEBUG, "fileNameOpenFile: not enough descs, retry, er= %d",
548537
errno));
549538
errno = 0;
550539
AssertLruRoom();
551540
goto tryAgain;
552-
} else {
553-
close(tmpfd);
554541
}
555542

556-
vfdP->fd = open(fileName,fileFlags,fileMode);
557543
vfdP->fdstate = 0x0;
558544

559545
if (vfdP->fd < 0) {
@@ -816,42 +802,44 @@ FileNameUnlink(char *filename)
816802
*/
817803
static int allocatedFiles = 0;
818804

819-
void
820-
AllocateFile()
805+
FILE *
806+
AllocateFile(char *name, char *mode)
821807
{
822-
int fd;
808+
FILE *file;
823809
int fdleft;
824810

825811
DO_DB(elog(DEBUG, "AllocateFile: Allocated %d.", allocatedFiles));
826812

827-
while ((fd = open(Nulldev,O_WRONLY,0)) < 0) {
828-
if (errno == EMFILE) {
829-
errno = 0;
830-
AssertLruRoom();
831-
} else {
832-
elog(WARN,"Open: %s in %s line %d, %s", Nulldev,
833-
__FILE__, __LINE__, strerror(errno));
834-
}
813+
TryAgain:
814+
if ((file = fopen(name, mode)) == NULL) {
815+
if (errno == EMFILE || errno == ENFILE) {
816+
DO_DB(elog(DEBUG, "AllocateFile: not enough descs, retry, er= %d",
817+
errno));
818+
errno = 0;
819+
AssertLruRoom();
820+
goto TryAgain;
821+
}
835822
}
836-
close(fd);
837-
++allocatedFiles;
838-
fdleft = pg_nofile() - allocatedFiles;
839-
if (fdleft < 6) {
840-
elog(NOTICE,"warning: few usable file descriptors left (%d)", fdleft);
823+
else {
824+
++allocatedFiles;
825+
fdleft = pg_nofile() - allocatedFiles;
826+
if (fdleft < 6)
827+
elog(NOTICE,"warning: few usable file descriptors left (%d)", fdleft);
841828
}
842-
829+
return file;
843830
}
844831

845832
/*
846833
* XXX What happens if FreeFile() is called without a previous
847834
* AllocateFile()?
848835
*/
849836
void
850-
FreeFile()
837+
FreeFile(FILE *file)
851838
{
852839
DO_DB(elog(DEBUG, "FreeFile: Allocated %d.", allocatedFiles));
853840

854841
Assert(allocatedFiles > 0);
842+
fclose(file);
855843
--allocatedFiles;
856844
}
857845

@@ -865,15 +853,3 @@ closeAllVfds()
865853
LruDelete(i);
866854
}
867855
}
868-
869-
void
870-
closeOneVfd()
871-
{
872-
int tmpfd;
873-
874-
tmpfd = open(Nulldev, O_CREAT | O_RDWR, 0666);
875-
if (tmpfd < 0)
876-
AssertLruRoom();
877-
else
878-
close(tmpfd);
879-
}

0 commit comments

Comments
 (0)