X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/dab746f5bcfd9154d9a8656071d6a2436fd8a765..409b3712f9aafe1d8a9c2da8c5284ac0afebcfb0:/emufs/idx.c diff --git a/emufs/idx.c b/emufs/idx.c index 5fcec35..f465d20 100644 --- a/emufs/idx.c +++ b/emufs/idx.c @@ -78,7 +78,6 @@ EMUFS_REG_ID emufs_idx_buscar_mayor_id_libre(EMUFS* emu, int* err) strcpy(name_f_idx, emu->nombre); strcat(name_f_idx, EMUFS_IDX_EXT); - (*err) = 0; if ((f_idx = fopen(name_f_idx, "rb")) == NULL) { PERR("No se puede abrir archivo"); *err = 4; /* EMUFS_ERROR_CANT_OPEN_FILE */ @@ -92,8 +91,8 @@ EMUFS_REG_ID emufs_idx_buscar_mayor_id_libre(EMUFS* emu, int* err) *err = 3; /* EMUFS_ERROR_FILE_READ */ return EMUFS_NOT_FOUND; } - if (reg.n_idreg >= max) { - max = reg.n_idreg; + if (reg.id_reg >= max) { + max = reg.id_reg; found = 1; } } @@ -129,9 +128,9 @@ EMUFS_BLOCK_ID emufs_idx_buscar_registro(EMUFS *emu, EMUFS_REG_ID reg_id) /* *err = 3; * EMUFS_ERROR_FILE_READ */ return EMUFS_NOT_FOUND; } - if (reg.n_idreg == reg_id) { + if (reg.id_reg == reg_id) { fclose(f_idx); - return reg.n_location; + return reg.location; } } fclose(f_idx); @@ -140,7 +139,7 @@ EMUFS_BLOCK_ID emufs_idx_buscar_registro(EMUFS *emu, EMUFS_REG_ID reg_id) } /* agrega un registro al final del archivo */ -int emufs_idx_agregar(EMUFS *emu, EMUFS_REG_ID n_idreg, EMUFS_BLOCK_ID n_location) +int emufs_idx_agregar(EMUFS *emu, EMUFS_REG_ID id_reg, EMUFS_BLOCK_ID location) { FILE *f_idx; EMUFS_IDX reg; @@ -152,8 +151,8 @@ int emufs_idx_agregar(EMUFS *emu, EMUFS_REG_ID n_idreg, EMUFS_BLOCK_ID n_locatio if ( (f_idx = fopen(name_f_idx,"a+"))==NULL ) return -1; /* Note: Location = Bloque para Tipo 1 y 3, Offset para Tipo 2 */ - reg.n_idreg = n_idreg; - reg.n_location = n_location; + reg.id_reg = id_reg; + reg.location = location; fwrite(®,sizeof(EMUFS_IDX),1,f_idx); fclose(f_idx); return 0; @@ -165,25 +164,24 @@ int emufs_idx_borrar(EMUFS *emu, EMUFS_REG_ID idreg) FILE *f_idx; EMUFS_IDX reg; char name_f_idx[255]; - long actual, final, cant, i, tam, a; + long actual, final, cant, i, tam; strcpy(name_f_idx,emu->nombre); strcat(name_f_idx, EMUFS_IDX_EXT); - if ( (f_idx = fopen(name_f_idx,"a+"))==NULL ) return -1; - - + if ( (f_idx = fopen(name_f_idx,"r+"))==NULL ) return -1; + while ( !feof(f_idx) ){ /*busco cual tengo que borrar*/ if ( fread(®, sizeof(EMUFS_IDX), 1, f_idx) != 1 ) continue; - if ( reg.n_idreg == idreg ) + if ( reg.id_reg == idreg ) { break; + } } /* me paro en el que tengo que borrar */ fseek(f_idx, -sizeof(EMUFS_IDX), SEEK_CUR); actual = ftell(f_idx); /* Guardo la posicion actual */ - printf(" actual = %ld\n",actual); fseek(f_idx, 0, SEEK_END); /* me voy al final */ final = ftell(f_idx); /* veo cuando ocupa el archivo */ fseek(f_idx, actual, SEEK_SET);/* vuelvo al lugar desde donde quiero justificar */ @@ -202,9 +200,7 @@ int emufs_idx_borrar(EMUFS *emu, EMUFS_REG_ID idreg) /* Aca deberia estar al final del archivo , no hago seek */ tam = ftell(f_idx); fclose(f_idx); - if ( truncate(name_f_idx, tam-sizeof(EMUFS_IDX))!= 0) - printf("no pude truncar\n"); - else printf("TRUNKE!!!!\n"); + truncate(name_f_idx, tam-sizeof(EMUFS_IDX)); return 0; } @@ -215,12 +211,53 @@ EMUFS_REG_ID emufs_idx_get_new_id(EMUFS* efs, int* err) 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 id; + return EMUFS_NOT_FOUND; } } return id; } +EMUFS_REG_ID emufs_idx_get_count(EMUFS *emu) +{ + FILE *fp; + char name_f_idx[255]; + EMUFS_REG_ID tam; + + strcpy(name_f_idx,emu->nombre); + strcat(name_f_idx, EMUFS_IDX_EXT); + + fp = fopen(name_f_idx, "rb"); + if (fp == NULL) return 0; + + fseek(fp, 0l, SEEK_END); + tam = ftell(fp); + fclose(fp); + + return tam/sizeof(EMUFS_IDX); +} + +EMUFS_REG_ID emufs_idx_get_id_at(EMUFS *emu, long pos) +{ + FILE *fp; + char name_f_idx[255]; + EMUFS_IDX id; + + strcpy(name_f_idx,emu->nombre); + strcat(name_f_idx, EMUFS_IDX_EXT); + + fp = fopen(name_f_idx, "rb"); + if (fp == NULL) return EMUFS_NOT_FOUND; + + fseek(fp, pos*sizeof(EMUFS_IDX), SEEK_SET); + fread(&id, sizeof(EMUFS_IDX), 1, fp); + fclose(fp); + + return id.id_reg; +}