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 *----------------------------------------------------------------------------
31 * Archivo para administrar el espacio libre disponible.
33 * Implementación del archivo para administrar el espacio libre disponible.
41 #define EMUFS_FSC_EXT ".fsc"
43 typedef struct emufs_fsc_t {
48 int emufs_fsc_agregar(EMUFS *emu, int num_bloque, int fs)
54 strcpy(name_f_fsc,emu->nombre);
55 strcat(name_f_fsc, EMUFS_FSC_EXT);
58 reg.block = num_bloque;
60 /*lo guardo en el archivo al final "a+"*/
61 if ( (f_fsc = fopen(name_f_fsc,"a+"))==NULL ) return -1;
62 fwrite(®,sizeof(EMUFS_FSC),1,f_fsc);
67 /* busca el bloque y le resta fs de espacio libre */
68 int emufs_fsc_actualizar(EMUFS *emu, int num_bloque, int fs)
74 strcpy(name_f_fsc,emu->nombre);
75 strcat(name_f_fsc, EMUFS_FSC_EXT);
77 /*busco el bloque que modifique*/
78 if ( (f_fsc = fopen(name_f_fsc,"r+")) == NULL) return -1;
79 while ( !feof(f_fsc) ){
80 if ( fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue;
81 if ( reg.block == num_bloque ){
83 fseek(f_fsc,-sizeof(EMUFS_FSC),SEEK_CUR);
84 fwrite(®,sizeof(EMUFS_FSC),1,f_fsc);
92 /* me devuelve el ID del bloque donde quepa un registro, y guarda en fs el espacio libre que queda en el bloque */
93 int emufs_fsc_buscar_lugar(EMUFS *emu, unsigned long tam, int *fs)
99 strcpy(name_f_fsc,emu->nombre);
100 strcat(name_f_fsc, EMUFS_FSC_EXT);
102 if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return -1;
104 /* Inicializo la estructura para evitar que si el archivo esta vacio
105 * el resultado sea correcto
108 *fs = emu->tam_bloque;
109 while( !feof(f_fsc) ){
110 if (fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue;
111 if ( reg.free_space >= tam+sizeof(int))
115 *fs = emu->tam_bloque;
121 *fs = reg.free_space;
125 int emufs_fsc_get_fs(EMUFS *emu, int num_bloque)
129 char name_f_fsc[255];
131 strcpy(name_f_fsc,emu->nombre);
132 strcat(name_f_fsc, EMUFS_FSC_EXT);
134 if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return -1;
136 while ( !feof(f_fsc) ){
137 if ( fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1 ) continue;
138 if ( reg.block == num_bloque )
143 return reg.free_space;