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