X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/e7e2badf7ebc4b6aeaf03366ad6fe6b919713d54..108d64919c0a9b294bdb26ac44fc433df0065aad:/emufs/idx.c?ds=inline diff --git a/emufs/idx.c b/emufs/idx.c index 52f3b59..bd20b63 100644 --- a/emufs/idx.c +++ b/emufs/idx.c @@ -1,25 +1,280 @@ +/* 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 + * Leandro Lucarella + *---------------------------------------------------------------------------- + * + * $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" +#include "did.h" +#include "error.h" +#include +#include +#include + +/* Objetivo: Realiza una apertura de un archivo indice y devuelve el handler. */ +FILE* emufs_idx_abrir(EMUFS* efs, const char* mode) +{ + FILE* f; + char* filename; + + filename = (char*) malloc(sizeof(char) * (strlen(efs->nombre) + + strlen(EMUFS_IDX_EXT) + 1)); + if (filename == NULL) { + /* TODO Manejo de errores */ + return NULL; + } + strcpy(filename, efs->nombre); + strcat(filename, EMUFS_IDX_EXT); + f = fopen(filename, mode); + free(filename); + return f; +} + +/* Crea un archivo indice de registros */ +int emufs_idx_crear(EMUFS *efs) +{ + return emufs_crear_archivo_auxiliar(efs->nombre, EMUFS_IDX_EXT); +} -int emufs_idx_buscar_mayor_id(EMUFS *emu) +/* Devuelve el mayor id de registro utilizado so far en el archivo de datos, revisando el indice. */ +EMUFS_REG_ID emufs_idx_buscar_mayor_id_libre(EMUFS* emu, int* err) { - int id, max = -1; - FILE *f_idx; - BLOCK_REG_T reg; + EMUFS_REG_ID max = 0; + FILE* f_idx; + EMUFS_IDX reg; + char name_f_idx[255]; /* TODO usar malloc para no limitar el tamaño de nombre de archivo */ + int found = 0; + + strcpy(name_f_idx, emu->nombre); + strcat(name_f_idx, EMUFS_IDX_EXT); + + if ((f_idx = fopen(name_f_idx, "rb")) == NULL) { + PERR("No se puede abrir archivo"); + *err = EMUFS_ERROR_CANT_OPEN_FILE; + return EMUFS_NOT_FOUND; + } + + /* Voy a la ultima entrada */ + if (fseek(f_idx, -sizeof(EMUFS_IDX), SEEK_END) != 0) { + fclose(f_idx); + return 0; + } + + if (fread(®, sizeof(EMUFS_IDX), 1, f_idx) == 1) { + found = 1; + max = reg.id_reg; + } + fclose(f_idx); + + if (found) { + return ++max; + } else { + return 0; + } +} + +/* busca el registro ID en el archivo ".idx" y devuelve el nro de bloque en el que se encuentra */ +EMUFS_BLOCK_ID emufs_idx_buscar_registro(EMUFS *emu, EMUFS_REG_ID reg_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, "rb")) == NULL) { + PERR("No se puede abrir archivo"); + /* *err = EMUFS_ERROR_CANT_OPEN_FILE; */ + return EMUFS_NOT_FOUND; + } + + /* Me muevo a la posicion pedida */ + if (fseek(f_idx, sizeof(EMUFS_IDX)*reg_id, SEEK_SET) == 0) { + if (fread(®, sizeof(EMUFS_IDX), 1, f_idx) == 1) { + fclose(f_idx); + return reg.location; + } + } + fclose(f_idx); + + return EMUFS_NOT_FOUND; +} +int emufs_idx_agregar(EMUFS *emu, EMUFS_REG_ID id_reg, EMUFS_BLOCK_ID location) +{ + FILE *f_idx; + EMUFS_IDX reg; + char name_f_idx[255]; + 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; + + fseek(f_idx, sizeof(EMUFS_IDX)*id_reg, SEEK_SET); + reg.id_reg = id_reg; + reg.location = location; + fwrite(®,sizeof(EMUFS_IDX),1,f_idx); + fclose(f_idx); - if ( (f_idx = fopen(name_f_idx,"r")) == NULL) return -1; /*ERROR*/ - 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 ( reg.id_reg >= max ) - max = reg.id_reg; + return 0; +} + +/* Borra un registro del indice dada la eliminacion fisica de un reg */ +int emufs_idx_borrar(EMUFS *emu, EMUFS_REG_ID idreg) +{ + 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; + + if (fseek(f_idx, sizeof(EMUFS_IDX)*idreg, SEEK_SET) == 0) { + reg.id_reg = idreg; + reg.location = EMUFS_NOT_FOUND; + fwrite(®, sizeof(EMUFS_IDX), 1, f_idx); } - id = max+1; fclose(f_idx); + + return 0; +} + +EMUFS_REG_ID emufs_idx_get_new_id(EMUFS* efs, int* err) +{ + EMUFS_REG_ID id; + + id = emufs_did_get_last(efs, err); + if (id == EMUFS_NOT_FOUND) { + if (*err) { + PERR("error al obtener ultimo id"); + return id; + } + id = emufs_idx_buscar_mayor_id_libre(efs, err); + if (*err) { + PERR("error al obtener id mayor"); + return EMUFS_NOT_FOUND; + } + } return id; } + +EMUFS_REG_ID *emufs_idx_get(EMUFS *emu, EMUFS_REG_ID *cant) +{ + FILE *f_idx; + int count; + char name_f_idx[255]; + EMUFS_REG_ID *tmp; + EMUFS_IDX reg; + + strcpy(name_f_idx,emu->nombre); + strcat(name_f_idx, EMUFS_IDX_EXT); + + if ( (f_idx = fopen(name_f_idx, "rb"))==NULL){ + PERR("No se pudo abrir el archvo"); + (*cant) = EMUFS_NOT_FOUND; + return NULL; + } + + tmp = NULL; + count = 0; + while (!feof(f_idx)) { + if (fread(®, sizeof(EMUFS_IDX), 1, f_idx) != 1) continue; + if (reg.location != EMUFS_NOT_FOUND) { + count++; + tmp = realloc(tmp, count*sizeof(EMUFS_REG_ID)); + tmp[count-1] = reg.id_reg; + } + } + fclose(f_idx); + + (*cant) = count; + return tmp; +} + +/** @todo FIXME borrar cuando se cambie emufs_gui/registros.c que es el unico + * que lo usa. + * @deprecated Usar emufs_idx_buscar_registro. + */ +int emufs_idx_existe_id(EMUFS *emu, int ID) +{ + FILE *f_idx; + char name_f_idx[255]; + EMUFS_IDX reg; + + strcpy(name_f_idx,emu->nombre); + strcat(name_f_idx, EMUFS_IDX_EXT); + + if ( (f_idx = fopen(name_f_idx, "rb")) == NULL){ + PERR("No se pudo abrir el archivo"); + return -1; + } + + if (fseek(f_idx, sizeof(EMUFS_IDX)*ID, SEEK_SET) == 0) { + fread(®, sizeof(EMUFS_IDX), 1, f_idx); + if (reg.location != EMUFS_NOT_FOUND) { + fclose(f_idx); + return 0; + } + } + fclose(f_idx); + return -1;/*no existe el id*/ +} + +int emufs_idx_actualizar(EMUFS *emu, int ID, EMUFS_BLOCK_ID bloque) +{ + FILE *f_idx; + char name_f_idx[255]; + EMUFS_IDX reg; + + strcpy(name_f_idx,emu->nombre); + strcat(name_f_idx, EMUFS_IDX_EXT); + + if ( (f_idx = fopen(name_f_idx, "r+")) == NULL){ + PERR("No se pudo abrir el archivo"); + return -1; + } + fseek(f_idx,0,SEEK_SET); + fseek(f_idx,ID*sizeof(EMUFS_IDX),SEEK_SET); + fread(®,sizeof(EMUFS_IDX),1,f_idx); + reg.location = bloque; + fseek(f_idx,-sizeof(EMUFS_IDX),SEEK_CUR); + fwrite(®,sizeof(EMUFS_IDX),1,f_idx); + fclose(f_idx); + return 0; +}