xref: /aosp_15_r20/external/mtools/streamcache.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes /*  Copyright 1996,1997,2001,2002,2007,2009 Alain Knaff.
2*d5c9a868SElliott Hughes  *  This file is part of mtools.
3*d5c9a868SElliott Hughes  *
4*d5c9a868SElliott Hughes  *  Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes  *  it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes  *  the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes  *  (at your option) any later version.
8*d5c9a868SElliott Hughes  *
9*d5c9a868SElliott Hughes  *  Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*d5c9a868SElliott Hughes  *  GNU General Public License for more details.
13*d5c9a868SElliott Hughes  *
14*d5c9a868SElliott Hughes  *  You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes  *
17*d5c9a868SElliott Hughes  * streamcache.c
18*d5c9a868SElliott Hughes  * Managing a cache of open disks
19*d5c9a868SElliott Hughes  */
20*d5c9a868SElliott Hughes 
21*d5c9a868SElliott Hughes #include "sysincludes.h"
22*d5c9a868SElliott Hughes #include "msdos.h"
23*d5c9a868SElliott Hughes #include "mtools.h"
24*d5c9a868SElliott Hughes #include "vfat.h"
25*d5c9a868SElliott Hughes #include "fs.h"
26*d5c9a868SElliott Hughes #include "mainloop.h"
27*d5c9a868SElliott Hughes #include "plain_io.h"
28*d5c9a868SElliott Hughes #include "file.h"
29*d5c9a868SElliott Hughes 
30*d5c9a868SElliott Hughes static int is_initialized = 0;
31*d5c9a868SElliott Hughes static Stream_t *fss[256]; /* open drives */
32*d5c9a868SElliott Hughes 
finish_sc(void)33*d5c9a868SElliott Hughes static void finish_sc(void)
34*d5c9a868SElliott Hughes {
35*d5c9a868SElliott Hughes 	int i;
36*d5c9a868SElliott Hughes 
37*d5c9a868SElliott Hughes 	for(i=0; i<256; i++){
38*d5c9a868SElliott Hughes 		if(fss[i] && fss[i]->refs != 1 )
39*d5c9a868SElliott Hughes 			fprintf(stderr,"Streamcache allocation problem:%c %d\n",
40*d5c9a868SElliott Hughes 				i, fss[i]->refs);
41*d5c9a868SElliott Hughes 		FREE(&(fss[i]));
42*d5c9a868SElliott Hughes 	}
43*d5c9a868SElliott Hughes }
44*d5c9a868SElliott Hughes 
init_streamcache(void)45*d5c9a868SElliott Hughes static void init_streamcache(void)
46*d5c9a868SElliott Hughes {
47*d5c9a868SElliott Hughes 	int i;
48*d5c9a868SElliott Hughes 
49*d5c9a868SElliott Hughes 	if(is_initialized)
50*d5c9a868SElliott Hughes 		return;
51*d5c9a868SElliott Hughes 	is_initialized = 1;
52*d5c9a868SElliott Hughes 	for(i=0; i<256; i++)
53*d5c9a868SElliott Hughes 		fss[i]=0;
54*d5c9a868SElliott Hughes 	atexit(finish_sc);
55*d5c9a868SElliott Hughes }
56*d5c9a868SElliott Hughes 
open_root_dir(char drive,int flags,int * isRop)57*d5c9a868SElliott Hughes Stream_t *open_root_dir(char drive, int flags, int *isRop)
58*d5c9a868SElliott Hughes {
59*d5c9a868SElliott Hughes 	Stream_t *Fs;
60*d5c9a868SElliott Hughes 
61*d5c9a868SElliott Hughes 	init_streamcache();
62*d5c9a868SElliott Hughes 
63*d5c9a868SElliott Hughes 	drive = (char)toupper(drive);
64*d5c9a868SElliott Hughes 
65*d5c9a868SElliott Hughes 	/* open the drive */
66*d5c9a868SElliott Hughes 	if(fss[(unsigned char)drive])
67*d5c9a868SElliott Hughes 		Fs = fss[(unsigned char)drive];
68*d5c9a868SElliott Hughes 	else {
69*d5c9a868SElliott Hughes 		Fs = fs_init(drive, flags, isRop);
70*d5c9a868SElliott Hughes 		if (!Fs){
71*d5c9a868SElliott Hughes 			fprintf(stderr, "Cannot initialize '%c:'\n", drive);
72*d5c9a868SElliott Hughes 			return NULL;
73*d5c9a868SElliott Hughes 		}
74*d5c9a868SElliott Hughes 
75*d5c9a868SElliott Hughes 		fss[(unsigned char)drive] = Fs;
76*d5c9a868SElliott Hughes 	}
77*d5c9a868SElliott Hughes 
78*d5c9a868SElliott Hughes 	return OpenRoot(Fs);
79*d5c9a868SElliott Hughes }
80