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: mié mar 31 17:26:46 ART 2004
22 * Autores: Nicolás Dimov <sagardua@uolsinectis.com.ar>
23 * Ricardo Markiewicz <rmarkie@fi.uba.ar>
24 * Leandro Lucarella <llucare@fi.uba.ar>
25 *----------------------------------------------------------------------------
27 * $Id: command.cpp 220 2003-11-19 23:10:40Z luca $
33 * Estructura general de un archivo <em>abstracto</em> de emufs.
35 * Implementación de la estructura abstracta que representa cualquiera de los
36 * tipos de archivo implementados. Se incluyen funciones tipo <em>factory</em>
37 * para crear un archivo, abrirlo y destruirlo.
44 /* Defino las extenciones que usan cada tipo de archivo */
45 #define EXT_TIPO3_ID ".idx"
46 #define EXT_TIPO3_DATA ".dat"
47 #define EXT_TIPO3_DISP ".fsc"
48 #define EXT_TIPO3_IDS ".did"
50 char *str_dup(const char *s);
52 char *str_dup(const char *s)
55 if (s == NULL) return NULL;
56 tmp = (char *)malloc(sizeof(char)*(strlen(s)+1));
62 EMUFS *emufs_crear(const char *filename, char tipo, unsigned int tam_bloque, unsigned int tam_reg)
66 EMUFS *tmp = (EMUFS *)malloc(sizeof(EMUFS));
75 tmp->tam_bloque = tam_bloque;
76 tmp->leer_bloque = emufs_tipo3_leer_bloque;
77 tmp->leer_registro = emufs_tipo3_leer_registro;
78 tmp->grabar_registro = emufs_tipo3_grabar_registro;
79 tmp->borrar_registro = emufs_tipo3_borrar_registro;
80 tmp->nombre = str_dup(filename);
82 strcpy(name, filename);
83 strcat(name, EXT_TIPO3_DATA);
84 fp = fopen(name, "w");
91 /* Guardo el Header */
92 fwrite(&tipo, sizeof(char), 1, fp);
93 fwrite(&tam_bloque, sizeof(unsigned int), 1, fp);
94 fwrite(&tam_reg, sizeof(unsigned int), 1, fp);
97 strcpy(name, filename);
98 strcat(name, EXT_TIPO3_ID);
99 fp = fopen(name, "w");
102 strcpy(name, filename);
103 strcat(name, EXT_TIPO3_DISP);
104 fp = fopen(name, "w");
107 strcpy(name, filename);
108 strcat(name, EXT_TIPO3_IDS);
109 fp = fopen(name, "w");
121 EMUFS *emufs_abrir(const char *filename)
128 strcpy(name, filename);
129 strcat(name, EXT_TIPO3_DATA);
131 /* Trato de determinar el tipo de archivo */
132 fp = fopen(name, "r");
133 if (fp == NULL) return NULL;
134 fread(&tipo, sizeof(char), 1, fp);
135 if ((tipo < 0) || (tipo > 2)) {
140 tmp = (EMUFS *)malloc(sizeof(EMUFS));
153 fread(&tmp->tam_bloque, sizeof(int), 1, fp);
154 tmp->leer_bloque = emufs_tipo3_leer_bloque;
155 tmp->leer_registro = emufs_tipo3_leer_registro;
156 tmp->grabar_registro = emufs_tipo3_grabar_registro;
157 tmp->borrar_registro = emufs_tipo3_borrar_registro;
158 tmp->nombre = str_dup(filename);
165 int emufs_destruir(EMUFS *e)
167 if (e == NULL) return 1;
173 int ver_archivo_FS(EMUFS *emu)
177 char name_f_block_free[255];
179 strcpy(name_f_block_free,emu->nombre);
180 strcat(name_f_block_free,".fsc");
182 if ( (f_block_free = fopen(name_f_block_free,"r"))==NULL ){
183 printf("no pude abrir el archivo %s\n",name_f_block_free);
186 fread(®,sizeof(reg),1,f_block_free);
187 while ( !feof(f_block_free) ){
188 printf(" Bloque = %d Espacio libre = %d\n",reg.block, reg.free_space);
189 fread(®,sizeof(reg),1,f_block_free);
192 fclose(f_block_free);
194 /* Imprimo la lista de bloques/registros */
195 strcpy(name_f_block_free,emu->nombre);
196 strcat(name_f_block_free,".idx");
199 f_block_free = fopen(name_f_block_free, "r");
200 fread(&r, sizeof(BLOCK_REG_T), 1, f_block_free);
201 while (!feof(f_block_free)) {
202 printf("ID %ld en bloque %d\n", r.id_reg, r.block);
203 fread(&r, sizeof(BLOCK_REG_T), 1, f_block_free);
205 fclose(f_block_free);