6
6
* Copyright (c) 1994, Regents of the University of California
7
7
*
8
8
* 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 $
10
10
*
11
11
* NOTES:
12
12
*
@@ -124,12 +124,6 @@ static Size SizeVfdCache = 0;
124
124
*/
125
125
static int nfile = 0 ;
126
126
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" ;
133
127
static char Sep_char = '/' ;
134
128
135
129
/*
@@ -312,7 +306,6 @@ LruInsert (File file)
312
306
vfdP = & VfdCache [file ];
313
307
314
308
if (FileIsNotOpen (file )) {
315
- int tmpfd ;
316
309
317
310
if ( nfile >= pg_nofile () )
318
311
AssertLruRoom ();
@@ -324,16 +317,13 @@ LruInsert (File file)
324
317
* should be able to open all the time. If this fails, we
325
318
* assume this is because there's no free file descriptors.
326
319
*/
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 ) ) {
330
323
errno = 0 ;
331
324
AssertLruRoom ();
332
325
goto tryAgain ;
333
- } else {
334
- close (tmpfd );
335
326
}
336
- vfdP -> fd = open (vfdP -> fileName ,vfdP -> fileFlags ,vfdP -> fileMode );
337
327
338
328
if (vfdP -> fd < 0 ) {
339
329
DO_DB (elog (DEBUG , "RE_OPEN FAILED: %d" ,
@@ -530,7 +520,6 @@ fileNameOpenFile(FileName fileName,
530
520
{
531
521
File file ;
532
522
Vfd * vfdP ;
533
- int tmpfd ;
534
523
535
524
DO_DB (elog (DEBUG , "fileNameOpenFile: %s %x %o" ,
536
525
fileName , fileFlags , fileMode ));
@@ -542,18 +531,15 @@ fileNameOpenFile(FileName fileName,
542
531
AssertLruRoom ();
543
532
544
533
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 ) ) {
547
536
DO_DB (elog (DEBUG , "fileNameOpenFile: not enough descs, retry, er= %d" ,
548
537
errno ));
549
538
errno = 0 ;
550
539
AssertLruRoom ();
551
540
goto tryAgain ;
552
- } else {
553
- close (tmpfd );
554
541
}
555
542
556
- vfdP -> fd = open (fileName ,fileFlags ,fileMode );
557
543
vfdP -> fdstate = 0x0 ;
558
544
559
545
if (vfdP -> fd < 0 ) {
@@ -816,42 +802,44 @@ FileNameUnlink(char *filename)
816
802
*/
817
803
static int allocatedFiles = 0 ;
818
804
819
- void
820
- AllocateFile ()
805
+ FILE *
806
+ AllocateFile (char * name , char * mode )
821
807
{
822
- int fd ;
808
+ FILE * file ;
823
809
int fdleft ;
824
810
825
811
DO_DB (elog (DEBUG , "AllocateFile: Allocated %d." , allocatedFiles ));
826
812
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
+ }
835
822
}
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 );
841
828
}
842
-
829
+ return file ;
843
830
}
844
831
845
832
/*
846
833
* XXX What happens if FreeFile() is called without a previous
847
834
* AllocateFile()?
848
835
*/
849
836
void
850
- FreeFile ()
837
+ FreeFile (FILE * file )
851
838
{
852
839
DO_DB (elog (DEBUG , "FreeFile: Allocated %d." , allocatedFiles ));
853
840
854
841
Assert (allocatedFiles > 0 );
842
+ fclose (file );
855
843
-- allocatedFiles ;
856
844
}
857
845
@@ -865,15 +853,3 @@ closeAllVfds()
865
853
LruDelete (i );
866
854
}
867
855
}
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