X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/80aa04fe7756cf1670b51e01238b166d95eb43e4..f8a441d3986f96851c8201fe82c5ce328b582d05:/emufs/idx.c?ds=inline diff --git a/emufs/idx.c b/emufs/idx.c index cb2ff78..6e33fda 100644 --- a/emufs/idx.c +++ b/emufs/idx.c @@ -37,6 +37,8 @@ #include "idx.h" #include "did.h" +#include "error.h" +#include "common.h" #include #include #include @@ -50,7 +52,6 @@ FILE* emufs_idx_abrir(EMUFS* efs, const char* mode) 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); @@ -80,7 +81,7 @@ EMUFS_REG_ID emufs_idx_buscar_mayor_id_libre(EMUFS* emu, int* err) if ((f_idx = fopen(name_f_idx, "rb")) == NULL) { PERR("No se puede abrir archivo"); - *err = 4; /* EMUFS_ERROR_CANT_OPEN_FILE */ + *err = EMUFS_ERROR_CANT_OPEN_FILE; return EMUFS_NOT_FOUND; } @@ -115,7 +116,7 @@ EMUFS_BLOCK_ID emufs_idx_buscar_registro(EMUFS *emu, EMUFS_REG_ID reg_id) if ((f_idx = fopen(name_f_idx, "rb")) == NULL) { PERR("No se puede abrir archivo"); - /* *err = 4; * EMUFS_ERROR_CANT_OPEN_FILE */ + /* *err = EMUFS_ERROR_CANT_OPEN_FILE; */ return EMUFS_NOT_FOUND; } @@ -131,7 +132,6 @@ EMUFS_BLOCK_ID emufs_idx_buscar_registro(EMUFS *emu, EMUFS_REG_ID reg_id) return EMUFS_NOT_FOUND; } -/* agrega un registro al final del archivo */ int emufs_idx_agregar(EMUFS *emu, EMUFS_REG_ID id_reg, EMUFS_BLOCK_ID location) { FILE *f_idx; @@ -141,7 +141,7 @@ int emufs_idx_agregar(EMUFS *emu, EMUFS_REG_ID id_reg, EMUFS_BLOCK_ID location) strcpy(name_f_idx,emu->nombre); strcat(name_f_idx, EMUFS_IDX_EXT); - if ( (f_idx = fopen(name_f_idx,"a+b"))==NULL ) return -1; + 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; @@ -215,10 +215,11 @@ EMUFS_REG_ID *emufs_idx_get(EMUFS *emu, EMUFS_REG_ID *cant) count = 0; while (!feof(f_idx)) { if (fread(®, sizeof(EMUFS_IDX), 1, f_idx) != 1) continue; - count++; - /* TODO : Verificar errores :-D */ - tmp = realloc(tmp, count); - tmp[count-1] = reg.id_reg; + if (reg.location != EMUFS_NOT_FOUND) { + count++; + tmp = realloc(tmp, count*sizeof(EMUFS_REG_ID)); + tmp[count-1] = reg.id_reg; + } } fclose(f_idx); @@ -226,6 +227,10 @@ EMUFS_REG_ID *emufs_idx_get(EMUFS *emu, EMUFS_REG_ID *cant) 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; @@ -239,7 +244,7 @@ int emufs_idx_existe_id(EMUFS *emu, int ID) 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) { @@ -247,7 +252,38 @@ int emufs_idx_existe_id(EMUFS *emu, int ID) 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 -1; + return 0; } + +long emufs_idx_get_file_size(EMUFS* efs, int* err) +{ + char name[255]; + strcpy(name, efs->nombre); + strcat(name, EMUFS_IDX_EXT); + return emufs_common_get_file_size(name, err); +} +