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 int emufs_fsc_agregar(EMUFS *emu, int num_bloque, int fs)
47 strcpy(name_f_fsc,emu->nombre);
48 strcat(name_f_fsc, EMUFS_FSC_EXT);
51 reg.block = num_bloque;
53 /*lo guardo en el archivo al final "a+"*/
54 if ( (f_fsc = fopen(name_f_fsc,"a+"))==NULL ) return -1;
55 fwrite(®,sizeof(EMUFS_FSC),1,f_fsc);
60 /* busca el bloque y le resta fs de espacio libre */
61 int emufs_fsc_actualizar(EMUFS *emu, int num_bloque, int fs)
67 strcpy(name_f_fsc,emu->nombre);
68 strcat(name_f_fsc, EMUFS_FSC_EXT);
70 /*busco el bloque que modifique*/
71 if ( (f_fsc = fopen(name_f_fsc,"r+")) == NULL) return -1;
72 while ( !feof(f_fsc) ){
73 if ( fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue;
74 if ( reg.block == num_bloque ){
76 fseek(f_fsc,-sizeof(EMUFS_FSC),SEEK_CUR);
77 fwrite(®,sizeof(EMUFS_FSC),1,f_fsc);
85 /* me devuelve el ID del bloque donde quepa un registro, y guarda en fs el espacio libre que queda en el bloque */
86 int emufs_fsc_buscar_lugar(EMUFS *emu, unsigned long tam, int *fs)
92 strcpy(name_f_fsc,emu->nombre);
93 strcat(name_f_fsc, EMUFS_FSC_EXT);
95 if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return -1;
97 /* Inicializo la estructura para evitar que si el archivo esta vacio
98 * el resultado sea correcto
101 *fs = emu->tam_bloque;
102 while( !feof(f_fsc) ){
103 if (fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue;
104 if (reg.free_space >= tam) {
108 *fs = emu->tam_bloque;
114 *fs = reg.free_space;
118 int emufs_fsc_get_fs(EMUFS *emu, int num_bloque)
122 char name_f_fsc[255];
124 strcpy(name_f_fsc,emu->nombre);
125 strcat(name_f_fsc, EMUFS_FSC_EXT);
127 if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return -1;
129 while ( !feof(f_fsc) ){
130 if ( fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1 ) continue;
131 if ( reg.block == num_bloque )
136 return reg.free_space;