1 /* vim: set noexpandtab tabstop=4 shiftwidth=4:
2 *----------------------------------------------------------------------------
4 *----------------------------------------------------------------------------
5 * This file is part of emufs.
7 * emufs is free software; you can redistribute it and/or modify it under the
8 * terms of the GNU General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option) any later
12 * emufs is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along
18 * with emufs; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
20 *----------------------------------------------------------------------------
21 * Creado: vie abr 9 16:17:50 ART 2004
22 * Autores: Nicolás Dimov <sagardua@uolsinectis.com.ar>
23 * Leandro Lucarella <llucare@fi.uba.ar>
24 *----------------------------------------------------------------------------
32 * Archivo para administrar el espacio libre disponible.
34 * Implementación del archivo para administrar el espacio libre disponible.
42 int emufs_fsc_crear(EMUFS* efs)
44 return emufs_crear_archivo_auxiliar(efs->nombre, EMUFS_FSC_EXT);
47 int emufs_fsc_agregar(EMUFS *emu, EMUFS_BLOCK_ID num_bloque, EMUFS_FREE fs)
53 strcpy(name_f_fsc,emu->nombre);
54 strcat(name_f_fsc, EMUFS_FSC_EXT);
57 reg.block = num_bloque;
59 /*lo guardo en el archivo al final "a+"*/
60 if ( (f_fsc = fopen(name_f_fsc,"a+"))==NULL ) return -1;
61 fwrite(®,sizeof(EMUFS_FSC),1,f_fsc);
66 /* busca el bloque y le resta fs de espacio libre */
67 int emufs_fsc_actualizar(EMUFS *emu, EMUFS_BLOCK_ID num_bloque, EMUFS_FREE fs)
73 strcpy(name_f_fsc,emu->nombre);
74 strcat(name_f_fsc, EMUFS_FSC_EXT);
76 /*busco el bloque que modifique*/
77 if ( (f_fsc = fopen(name_f_fsc,"r+")) == NULL) return -1;
78 while ( !feof(f_fsc) ){
79 if ( fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue;
80 if ( reg.block == num_bloque ){
82 fseek(f_fsc,-sizeof(EMUFS_FSC),SEEK_CUR);
83 fwrite(®,sizeof(EMUFS_FSC),1,f_fsc);
91 /* me devuelve el ID del bloque donde quepa un registro, y guarda en fs el espacio libre que queda en el bloque */
92 EMUFS_BLOCK_ID emufs_fsc_buscar_lugar(EMUFS *emu, EMUFS_FREE tam, EMUFS_FREE *fs)
98 strcpy(name_f_fsc,emu->nombre);
99 strcat(name_f_fsc, EMUFS_FSC_EXT);
101 if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return -1;
103 /* Inicializo la estructura para evitar que si el archivo esta vacio
104 * el resultado sea correcto
107 *fs = emu->tam_bloque;
108 while( !feof(f_fsc) ){
109 if (fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue;
110 if (reg.free_space >= tam) {
114 *fs = emu->tam_bloque;
120 *fs = reg.free_space;
124 EMUFS_FREE emufs_fsc_get_fs(EMUFS *emu, EMUFS_BLOCK_ID num_bloque)
128 char name_f_fsc[255];
130 strcpy(name_f_fsc,emu->nombre);
131 strcat(name_f_fsc, EMUFS_FSC_EXT);
133 if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return -1;
135 while ( !feof(f_fsc) ){
136 if ( fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1 ) continue;
137 if ( reg.block == num_bloque )
142 return reg.free_space;