X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/e7e2badf7ebc4b6aeaf03366ad6fe6b919713d54..6c5fe1e15e441c27ba01b22b91bf1e5843ae53f2:/emufs/idx.c diff --git a/emufs/idx.c b/emufs/idx.c index 52f3b59..093c2ac 100644 --- a/emufs/idx.c +++ b/emufs/idx.c @@ -1,20 +1,63 @@ +/* vim: set noexpandtab tabstop=4 shiftwidth=4: + *---------------------------------------------------------------------------- + * emufs + *---------------------------------------------------------------------------- + * This file is part of emufs. + * + * emufs is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * emufs is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along + * with emufs; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + *---------------------------------------------------------------------------- + * Creado: jue abr 8 18:10:35 ART 2004 + * Autores: Nicolás Dimov + *---------------------------------------------------------------------------- + * + * $Id$ + * + */ + +/** \file + * + * Manejo de archivos de índice de registros. + * + * Implementación del manejo de archivos de índice de registros. + * + */ + #include "idx.h" -#include "tipo3.h" + +#define EMUFS_IDX_EXT ".idx" + +typedef struct emufs_idx_t { + int block; + long int id_reg; +} EMUFS_IDX; int emufs_idx_buscar_mayor_id(EMUFS *emu) { int id, max = -1; FILE *f_idx; - BLOCK_REG_T reg; - char name_f_idx[255]; + EMUFS_IDX reg; + char name_f_idx[255]; /* TODO usar malloc para no limitar el tamaño de nombre de archivo */ strcpy(name_f_idx,emu->nombre); - strcat(name_f_idx,".idx"); + strcat(name_f_idx, EMUFS_IDX_EXT); if ( (f_idx = fopen(name_f_idx,"r")) == NULL) return -1; /*ERROR*/ + id = -1; while ( !feof(f_idx) ){ /* Me aseguro de leer la cantidad de bytes correcta */ - if (fread(®,sizeof(BLOCK_REG_T),1,f_idx) != 1) continue; + if (fread(®,sizeof(EMUFS_IDX),1,f_idx) != 1) continue; if ( reg.id_reg >= max ) max = reg.id_reg; } @@ -23,3 +66,44 @@ int emufs_idx_buscar_mayor_id(EMUFS *emu) return id; } + +/*busca el registro ID en el archivo "block_reg.dat" y devuelve el nro de bloque en el que se encuentra*/ +int emufs_idx_buscar_registro(EMUFS *emu, int ID) +{ + FILE* f_idx; + EMUFS_IDX reg; + char name_f_idx[255]; + strcpy(name_f_idx,emu->nombre); + strcat(name_f_idx, EMUFS_IDX_EXT); + + if ( (f_idx = fopen(name_f_idx,"r")) == NULL) return -1; /*ERROR*/ + while ( !feof(f_idx) ){ + if (fread(®,sizeof(EMUFS_IDX),1,f_idx) != 1) continue; + if ( reg.id_reg == ID ){ + fclose(f_idx); + return reg.block; + } + } + + fclose(f_idx); + return -1; /*no existe el registro*/ +} + +/* agrega un registro al final del archivo */ +int emufs_idx_agregar(EMUFS *emu, int num_bloque, int ID_aux) +{ + FILE *f_idx; + EMUFS_IDX reg; + char name_f_idx[255]; + + strcpy(name_f_idx,emu->nombre); + strcat(name_f_idx, EMUFS_IDX_EXT); + + if ( (f_idx = fopen(name_f_idx,"ab+"))==NULL ) return -1; + + reg.block = num_bloque; + reg.id_reg = ID_aux; + fwrite(®,sizeof(EMUFS_IDX),1,f_idx); + fclose(f_idx); + return 0; +}