blob: be502485eec83f32bbadc86fab742e324aa8c35e [file] [log] [blame]
Alistair Delvabeaee832021-02-24 11:27:23 -08001/* 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
30static int is_initialized = 0;
31static Stream_t *fss[256]; /* open drives */
32
33static 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
45static 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
Yi Kong39bbd962022-01-09 19:41:38 +080057Stream_t *open_root_dir(char drive, int flags, int *isRop)
Alistair Delvabeaee832021-02-24 11:27:23 -080058{
59 Stream_t *Fs;
60
61 init_streamcache();
62
Yi Kong39bbd962022-01-09 19:41:38 +080063 drive = (char)toupper(drive);
64
Alistair Delvabeaee832021-02-24 11:27:23 -080065 /* open the drive */
Yi Kong39bbd962022-01-09 19:41:38 +080066 if(fss[(unsigned char)drive])
67 Fs = fss[(unsigned char)drive];
Alistair Delvabeaee832021-02-24 11:27:23 -080068 else {
69 Fs = fs_init(drive, flags, isRop);
70 if (!Fs){
71 fprintf(stderr, "Cannot initialize '%c:'\n", drive);
72 return NULL;
73 }
74
Yi Kong39bbd962022-01-09 19:41:38 +080075 fss[(unsigned char)drive] = Fs;
Alistair Delvabeaee832021-02-24 11:27:23 -080076 }
77
78 return OpenRoot(Fs);
79}