Reduce open() calls. Replace fopen() calls with calls to fd.c functions.

This commit is contained in:
Bruce Momjian 1997-08-18 02:15:04 +00:00
parent eaae21fb4d
commit 022903f22e
11 changed files with 74 additions and 106 deletions

View File

@ -6,7 +6,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.24 1997/06/12 15:39:44 vadim Exp $
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.25 1997/08/18 02:14:34 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -34,6 +34,7 @@
#include <catalog/catname.h>
#include <catalog/pg_user.h>
#include <commands/copy.h>
#include <storage/fd.h>
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
#define VALUE(c) ((c) - '0')
@ -127,7 +128,7 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
fp = Pfin;
} else fp = stdin;
} else {
fp = fopen(filename, "r");
fp = AllocateFile(filename, "r");
if (fp == NULL)
elog(WARN, "COPY command, running in backend with "
"effective uid %d, could not open file '%s' for "
@ -145,7 +146,7 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
} else {
mode_t oumask; /* Pre-existing umask value */
oumask = umask((mode_t) 0);
fp = fopen(filename, "w");
fp = AllocateFile(filename, "w");
umask(oumask);
if (fp == NULL)
elog(WARN, "COPY command, running in backend with "
@ -156,7 +157,8 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
}
CopyTo(rel, binary, oids, fp, delim);
}
if (!pipe) fclose(fp);
if (!pipe)
FreeFile(fp);
else if (!from && !binary) {
fputs("\\.\n", fp);
if (IsUnderPostmaster) fflush(Pfout);

View File

@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.17 1997/08/12 22:52:52 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/libpq/hba.c,v 1.18 1997/08/18 02:14:37 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -28,7 +28,7 @@
#include <libpq/pqcomm.h>
#include <libpq/hba.h>
#include <port/inet_aton.h> /* For inet_aton() */
#include <storage/fd.h>
/* Some standard C libraries, including GNU, have an isblank() function.
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,
strlen(CONF_FILE)+2)*sizeof(char));
sprintf(conf_file, "%s/%s", DataDir, CONF_FILE);
file = fopen(conf_file, "r");
if (file == 0) {
file = AllocateFile(conf_file, "r");
if (file == NULL) {
/* The open of the config file failed. */
*host_ok_p = false;
@ -350,7 +350,7 @@ find_hba_entry(const char DataDir[], const struct in_addr ip_addr,
} else {
process_open_config_file(file, ip_addr, database, host_ok_p, userauth_p,
usermap_name, find_password_entries);
fclose(file);
FreeFile(file);
}
free(conf_file);
}
@ -636,8 +636,8 @@ verify_against_usermap(const char DataDir[],
strlen(MAP_FILE)+2)*sizeof(char));
sprintf(map_file, "%s/%s", DataDir, MAP_FILE);
file = fopen(map_file, "r");
if (file == 0) {
file = AllocateFile(map_file, "r");
if (file == NULL) {
/* The open of the map file failed. */
*checks_out_p = false;
@ -654,7 +654,7 @@ verify_against_usermap(const char DataDir[],
verify_against_open_usermap(file,
pguser, ident_username, usermap_name,
checks_out_p);
fclose(file);
FreeFile(file);
}
free(map_file);

View File

@ -2,6 +2,7 @@
#include <libpq/password.h>
#include <libpq/hba.h>
#include <libpq/libpq.h>
#include <storage/fd.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_CRYPT_H
@ -56,7 +57,7 @@ verify_password(char *user, char *password, Port *port,
strcat(pw_file_fullname, "/");
strcat(pw_file_fullname, pw_file_name);
pw_file = fopen(pw_file_fullname, "r");
pw_file = AllocateFile(pw_file_fullname, "r");
if(!pw_file) {
sprintf(PQerrormsg,
"verify_password: couldn't open password file '%s'\n",
@ -84,7 +85,7 @@ verify_password(char *user, char *password, Port *port,
if(strcmp(user, test_user) == 0) {
/* we're outta here one way or the other. */
fclose(pw_file);
FreeFile(pw_file);
if(strcmp(crypt(password, salt), test_pw) == 0) {
/* it matched. */

View File

@ -5,7 +5,7 @@
*
* Copyright (c) 1994, Regents of the University of California
*
* $Id: geqo_params.c,v 1.4 1997/08/12 22:53:09 momjian Exp $
* $Id: geqo_params.c,v 1.5 1997/08/18 02:14:41 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -43,6 +43,8 @@
#include "optimizer/geqo_gene.h"
#include "optimizer/geqo.h"
#include "storage/fd.h"
#define POOL_TAG "Pool_Size"
#define TRIAL_TAG "Generations"
#define RAND_TAG "Random_Seed"
@ -89,7 +91,7 @@ geqo_params(int string_length)
sprintf(conf_file, "%s/%s", DataDir, GEQO_FILE);
/* open the config file */
file = fopen(conf_file, "r");
file = AllocateFile(conf_file, "r");
if (file)
{
/*
@ -187,7 +189,7 @@ geqo_params(int string_length)
}
}
fclose(file);
FreeFile(file);
pfree(conf_file);
}

View File

@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/parser/Attic/dbcommands.c,v 1.3 1997/01/10 20:18:20 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/parser/Attic/dbcommands.c,v 1.4 1997/08/18 02:14:44 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -31,6 +31,7 @@
#include "tcop/tcopprot.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
#include "storage/fd.h"
/* non-export function prototypes */
@ -249,9 +250,9 @@ stop_vacuum(char *dbname)
sprintf(filename, "%s%cbase%c%s%c%s.vacuum", DataDir, SEP_CHAR, SEP_CHAR,
dbname, SEP_CHAR, dbname);
if ((fp = fopen(filename, "r")) != (FILE *) NULL) {
if ((fp = AllocateFile(filename, "r")) != NULL) {
fscanf(fp, "%d", &pid);
fclose(fp);
FreeFile(fp);
if (kill(pid, SIGKILLDAEMON1) < 0) {
elog(WARN, "can't kill vacuum daemon (pid %d) on %s",
pid, dbname);

View File

@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.16 1997/08/12 22:53:46 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v 1.17 1997/08/18 02:14:49 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -1663,7 +1663,7 @@ _bm_die(Oid dbId, Oid relId, int blkNo, int bufNo,
tb = &TraceBuf[cur];
if ((fp = fopen("/tmp/death_notice", "w")) == (FILE *) NULL)
if ((fp = AllocateFile("/tmp/death_notice", "w")) == NULL)
elog(FATAL, "buffer alloc trace error and can't open log file");
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,
break;
}
fclose(fp);
FreeFile(fp);
kill(getpid(), SIGILL);
}

View File

@ -6,7 +6,7 @@
* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* $Id: fd.c,v 1.20 1997/08/12 22:53:51 momjian Exp $
* $Id: fd.c,v 1.21 1997/08/18 02:14:50 momjian Exp $
*
* NOTES:
*
@ -124,12 +124,6 @@ static Size SizeVfdCache = 0;
*/
static int nfile = 0;
/*
* we use the name of the null device in various places, mostly so
* that we can open it and find out if we really have any descriptors
* available or not.
*/
static char *Nulldev = "/dev/null";
static char Sep_char = '/';
/*
@ -312,7 +306,6 @@ LruInsert (File file)
vfdP = &VfdCache[file];
if (FileIsNotOpen(file)) {
int tmpfd;
if ( nfile >= pg_nofile() )
AssertLruRoom();
@ -324,16 +317,13 @@ LruInsert (File file)
* should be able to open all the time. If this fails, we
* assume this is because there's no free file descriptors.
*/
tryAgain:
tmpfd = open(Nulldev, O_CREAT|O_RDWR, 0666);
if (tmpfd < 0) {
tryAgain:
vfdP->fd = open(vfdP->fileName,vfdP->fileFlags,vfdP->fileMode);
if (vfdP->fd < 0 && (errno == EMFILE || errno == ENFILE)) {
errno = 0;
AssertLruRoom();
goto tryAgain;
} else {
close(tmpfd);
}
vfdP->fd = open(vfdP->fileName,vfdP->fileFlags,vfdP->fileMode);
if (vfdP->fd < 0) {
DO_DB(elog(DEBUG, "RE_OPEN FAILED: %d",
@ -530,7 +520,6 @@ fileNameOpenFile(FileName fileName,
{
File file;
Vfd *vfdP;
int tmpfd;
DO_DB(elog(DEBUG, "fileNameOpenFile: %s %x %o",
fileName, fileFlags, fileMode));
@ -542,18 +531,15 @@ fileNameOpenFile(FileName fileName,
AssertLruRoom();
tryAgain:
tmpfd = open(Nulldev, O_CREAT|O_RDWR, 0666);
if (tmpfd < 0) {
vfdP->fd = open(fileName,fileFlags,fileMode);
if (vfdP->fd < 0 && (errno == EMFILE || errno == ENFILE)) {
DO_DB(elog(DEBUG, "fileNameOpenFile: not enough descs, retry, er= %d",
errno));
errno = 0;
AssertLruRoom();
goto tryAgain;
} else {
close(tmpfd);
}
vfdP->fd = open(fileName,fileFlags,fileMode);
vfdP->fdstate = 0x0;
if (vfdP->fd < 0) {
@ -816,30 +802,31 @@ FileNameUnlink(char *filename)
*/
static int allocatedFiles = 0;
void
AllocateFile()
FILE *
AllocateFile(char *name, char *mode)
{
int fd;
FILE *file;
int fdleft;
DO_DB(elog(DEBUG, "AllocateFile: Allocated %d.", allocatedFiles));
while ((fd = open(Nulldev,O_WRONLY,0)) < 0) {
if (errno == EMFILE) {
errno = 0;
AssertLruRoom();
} else {
elog(WARN,"Open: %s in %s line %d, %s", Nulldev,
__FILE__, __LINE__, strerror(errno));
}
TryAgain:
if ((file = fopen(name, mode)) == NULL) {
if (errno == EMFILE || errno == ENFILE) {
DO_DB(elog(DEBUG, "AllocateFile: not enough descs, retry, er= %d",
errno));
errno = 0;
AssertLruRoom();
goto TryAgain;
}
}
close(fd);
++allocatedFiles;
fdleft = pg_nofile() - allocatedFiles;
if (fdleft < 6) {
elog(NOTICE,"warning: few usable file descriptors left (%d)", fdleft);
else {
++allocatedFiles;
fdleft = pg_nofile() - allocatedFiles;
if (fdleft < 6)
elog(NOTICE,"warning: few usable file descriptors left (%d)", fdleft);
}
return file;
}
/*
@ -847,11 +834,12 @@ AllocateFile()
* AllocateFile()?
*/
void
FreeFile()
FreeFile(FILE *file)
{
DO_DB(elog(DEBUG, "FreeFile: Allocated %d.", allocatedFiles));
Assert(allocatedFiles > 0);
fclose(file);
--allocatedFiles;
}
@ -865,15 +853,3 @@ closeAllVfds()
LruDelete(i);
}
}
void
closeOneVfd()
{
int tmpfd;
tmpfd = open(Nulldev, O_CREAT | O_RDWR, 0666);
if (tmpfd < 0)
AssertLruRoom();
else
close(tmpfd);
}

View File

@ -9,7 +9,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.18 1997/07/24 20:15:18 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/tcop/utility.c,v 1.19 1997/08/18 02:14:52 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -47,6 +47,7 @@
#include "tcop/variable.h"
#include "tcop/utility.h"
#include "fmgr.h" /* For load_file() */
#include "storage/fd.h"
#ifndef NO_SECURITY
#include "miscadmin.h"
@ -218,9 +219,6 @@ ProcessUtility(Node *parsetree,
commandTag = "COPY";
CHECK_IF_ABORTED();
/* Free up file descriptors - going to do a read... */
closeOneVfd();
DoCopy(stmt->relname,
stmt->binary,
stmt->oids,
@ -594,9 +592,9 @@ ProcessUtility(Node *parsetree,
filename = stmt->filename;
closeAllVfds();
if ((fp = fopen(filename, "r")) == NULL)
if ((fp = AllocateFile(filename, "r")) == NULL)
elog(WARN, "LOAD: could not open file %s", filename);
fclose(fp);
FreeFile(fp);
load_file(filename);
}
break;

View File

@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.13 1997/08/12 22:54:24 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.14 1997/08/18 02:14:54 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -23,7 +23,7 @@
#include "utils/syscache.h"
#include "utils/memutils.h"
#include "storage/fd.h" /* for SEEK_ */
#include "storage/fd.h"
#include "fmgr.h"
#include "utils/array.h"
@ -463,11 +463,12 @@ _ReadLOArray(char *str,
if ( accessfile ) {
FILE *afd;
if ((afd = fopen (accessfile, "r")) == NULL)
if ((afd = AllocateFile(accessfile, "r")) == NULL)
elog(WARN, "unable to open access pattern file");
*chunkFlag = true;
retStr = _ChunkArray(*fd, afd, ndim, dim, baseSize, nbytes,
chunkfile);
FreeFile(afd);
}
return(retStr);
}

View File

@ -7,7 +7,7 @@
*
*
* IDENTIFICATION
* $Header: /cvsroot/pgsql/src/backend/utils/sort/Attic/psort.c,v 1.15 1997/08/14 16:11:28 momjian Exp $
* $Header: /cvsroot/pgsql/src/backend/utils/sort/Attic/psort.c,v 1.16 1997/08/18 02:14:56 momjian Exp $
*
* NOTES
* Sorts the first relation into the second relation.
@ -763,16 +763,10 @@ gettape()
memmove(tp->tl_name, uniqueName, strlen(uniqueName));
AllocateFile();
file = fopen(tp->tl_name, "w+");
if (file == NULL) {
elog(NOTICE, "psort: gettape: fopen returned error code %i", errno);
/* XXX this should not happen */
FreeFile();
FREE(tp->tl_name);
FREE(tp);
return(NULL);
}
file = AllocateFile(tp->tl_name, "w+");
if (file == NULL)
elog(WARN,"Open: %s in %s line %d, %s", tp->tl_name,
__FILE__, __LINE__, strerror(errno));
tp->tl_fd = fileno(file);
tp->tl_next = Tapes;
@ -823,8 +817,7 @@ destroytape(FILE *file)
if ((fd = fileno(file)) == tp->tl_fd) {
Tapes = tp->tl_next;
fclose(file);
FreeFile();
FreeFile(file);
unlink(tp->tl_name);
FREE(tp->tl_name);
FREE(tp);
@ -833,8 +826,7 @@ destroytape(FILE *file)
if (tp->tl_next == NULL)
elog(FATAL, "destroytape: tape not found");
if (tp->tl_next->tl_fd == fd) {
fclose(file);
FreeFile();
FreeFile(file);
tq = tp->tl_next;
tp->tl_next = tq->tl_next;
unlink(tq->tl_name);

View File

@ -6,7 +6,7 @@
*
* Copyright (c) 1994, Regents of the University of California
*
* $Id: fd.h,v 1.6 1997/02/14 04:18:42 momjian Exp $
* $Id: fd.h,v 1.7 1997/08/18 02:15:04 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -34,11 +34,7 @@
#ifndef FD_H
#define FD_H
/*
* FileOpen uses the standard UNIX open(2) flags.
*/
#ifndef O_RDONLY
#endif /* O_RDONLY */
#include <stdio.h>
/*
* FileSeek uses the standard UNIX lseek(2) flags.
@ -76,10 +72,9 @@ extern long FileTell(File file);
extern int FileTruncate(File file, int offset);
extern int FileSync(File file);
extern int FileNameUnlink(char *filename);
extern void AllocateFile(void);
extern void FreeFile(void);
extern FILE *AllocateFile(char *name, char *mode);
extern void FreeFile(FILE *);
extern void closeAllVfds(void);
extern void closeOneVfd(void);
extern int pg_fsync(int fd);
#endif /* FD_H */