*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
#include
#include
#include
+#include
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
#define VALUE(c) ((c) - '0')
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 "
} 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 "
}
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);
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
#include
#include
#include
/* For inet_aton() */
-
+#include
/* Some standard C libraries, including GNU, have an isblank() function.
Others, including Solaris, do not. So we have our own.
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;
} 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);
}
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;
verify_against_open_usermap(file,
pguser, ident_username, usermap_name,
checks_out_p);
- fclose(file);
+ FreeFile(file);
}
free(map_file);
#include
#include
#include
+#include
#include
#include
#ifdef HAVE_CRYPT_H
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",
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. */
*
* 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 $
*
*-------------------------------------------------------------------------
*/
#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"
sprintf(conf_file, "%s/%s", DataDir, GEQO_FILE);
/* open the config file */
- file = fopen(conf_file, "r");
+ file = AllocateFile(conf_file, "r");
if (file)
{
/*
}
}
- fclose(file);
+ FreeFile(file);
pfree(conf_file);
}
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
#include "tcop/tcopprot.h"
#include "storage/bufmgr.h"
#include "storage/lmgr.h"
+#include "storage/fd.h"
/* non-export function prototypes */
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);
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
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");
break;
}
- fclose(fp);
+ FreeFile(fp);
kill(getpid(), SIGILL);
}
* 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:
*
*/
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 = '/';
/*
vfdP = &VfdCache[file];
if (FileIsNotOpen(file)) {
- int tmpfd;
if ( nfile >= pg_nofile() )
AssertLruRoom();
* 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",
{
File file;
Vfd *vfdP;
- int tmpfd;
DO_DB(elog(DEBUG, "fileNameOpenFile: %s %x %o",
fileName, fileFlags, fileMode));
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) {
*/
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;
}
/*
* AllocateFile()?
*/
void
-FreeFile()
+FreeFile(FILE *file)
{
DO_DB(elog(DEBUG, "FreeFile: Allocated %d.", allocatedFiles));
Assert(allocatedFiles > 0);
+ fclose(file);
--allocatedFiles;
}
LruDelete(i);
}
}
-
-void
-closeOneVfd()
-{
- int tmpfd;
-
- tmpfd = open(Nulldev, O_CREAT | O_RDWR, 0666);
- if (tmpfd < 0)
- AssertLruRoom();
- else
- close(tmpfd);
-}
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
#include "tcop/variable.h"
#include "tcop/utility.h"
#include "fmgr.h" /* For load_file() */
+#include "storage/fd.h"
#ifndef NO_SECURITY
#include "miscadmin.h"
commandTag = "COPY";
CHECK_IF_ABORTED();
- /* Free up file descriptors - going to do a read... */
- closeOneVfd();
-
DoCopy(stmt->relname,
stmt->binary,
stmt->oids,
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;
*
*
* 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 $
*
*-------------------------------------------------------------------------
*/
#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"
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);
}
*
*
* 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.
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;
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);
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);
*
* 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 $
*
*-------------------------------------------------------------------------
*/
#ifndef FD_H
#define FD_H
-/*
- * FileOpen uses the standard UNIX open(2) flags.
- */
-#ifndef O_RDONLY
-#endif /* O_RDONLY */
+#include
/*
* FileSeek uses the standard UNIX lseek(2) flags.
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 */