X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/885b60b3ea2aed49543ea87490ff971a68a5c2f1..7932b1f7bfc70a8e2d33416e2797b1e74144c21d:/tipo3/param_cte.c diff --git a/tipo3/param_cte.c b/tipo3/param_cte.c index 149ece6..ab02873 100644 --- a/tipo3/param_cte.c +++ b/tipo3/param_cte.c @@ -167,7 +167,10 @@ int grabar_registro(EMUFS *emu, void *ptr, unsigned long tam) /*printf("FS = %d\n", fs);*/ reg.free_space = emu->tam_bloque - tam-sizeof(int); /*lo guardo en el archivo al final "a+"*/ - if ( (f_block_free = fopen(name_f_free,"a+"))==NULL ) return -1; /*ERROR*/ + if ( (f_block_free = fopen(name_f_free,"a+"))==NULL ) { + free(bloque); + return -1; /*ERROR*/ + } fwrite(®,sizeof(BLOCK_FREE_T),1,f_block_free); fclose(f_block_free); } else { @@ -189,7 +192,10 @@ int grabar_registro(EMUFS *emu, void *ptr, unsigned long tam) } /*actualizo el archivo de espacios libres*/ /*busco el bloque que modifique*/ - if ( (f_block_free = fopen(name_f_free,"r+")) == NULL) return -1; /*ERROR*/ + if ( (f_block_free = fopen(name_f_free,"r+")) == NULL) { + free(bloque); + return -1; /*ERROR*/ + } while ( !feof(f_block_free) ){ fread(®,sizeof(BLOCK_FREE_T),1,f_block_free); if ( reg.block == num_bloque ){ @@ -203,7 +209,10 @@ int grabar_registro(EMUFS *emu, void *ptr, unsigned long tam) } /*actualizo el archivo de bloques y registros*/ - if ( (f_block_reg = fopen(name_f_block_reg,"ab+"))==NULL ) return -1; /*ERROR*/ + if ( (f_block_reg = fopen(name_f_block_reg,"ab+"))==NULL ) { + free(bloque); + return -1; /*ERROR*/ + } reg_b.block = reg.block; reg_b.id_reg = ID_aux; fwrite(®_b,sizeof(BLOCK_REG_T),1,f_block_reg); @@ -310,3 +319,107 @@ int get_id(EMUFS *emu) fclose(f_reg_exist); return id; } + +/*borra un registro de un bloque y acomoda los registros que quedan*/ +int borrar_registro(EMUFS *emu, int ID, int tam_reg) +{ + int num_bloque, ptr_elim, ptr_mov, ID_aux, cant, i; + char *bloque; + FILE *f_reg_exist, *f_block_reg, *f_block_free; + BLOCK_REG_T reg_b; + BLOCK_FREE_T reg_f; + BLOCK_REG_T buffer[10]; + char name_f_reg_exist[255]; + char name_f_block_reg[255]; + char name_f_block_free[255]; + + strcpy(name_f_block_reg,emu->nombre); + strcat(name_f_block_reg,".idx"); + + strcpy(name_f_reg_exist,emu->nombre); + strcat(name_f_reg_exist,".did"); + + strcpy(name_f_block_free,emu->nombre); + strcat(name_f_block_free,".fsc"); + + num_bloque = buscar_registro(emu, ID); + bloque = (char*)malloc(emu->tam_bloque); + if ( leer_bloque(emu,num_bloque, bloque) == -1 ){ + printf("No se encontro el bloque\n"); + return -1; + } + + /*apunto al registro que voy a eliminar*/ + ptr_elim = 0; + while ( ptr_elim < emu->tam_bloque ){ + memcpy(&ID_aux, bloque+ptr_elim, sizeof(int)); + if ( ID_aux == ID ) + break; + ptr_elim += tam_reg + sizeof(int); + } + + /*apunto al registro que voy a mover*/ + ptr_mov = ptr_elim + tam_reg + sizeof(int); + + while ( ptr_mov < emu->tam_bloque ){ + memcpy(bloque+ptr_elim, bloque+ptr_mov, sizeof(int)+tam_reg); + ptr_elim = ptr_mov; + ptr_mov += sizeof(int) + tam_reg; + } + + /*grabo el bloque en el archivo*/ + if ( grabar_bloque(emu, bloque, num_bloque) == -1 ){ + printf("No se pudo grabar el bloque\n"); + return -1; + } + + /*actualizo archivo .fsc*/ + if ( (f_block_free = fopen(name_f_block_free,"r+")) == NULL ) return -1; + fread(®_f,sizeof(BLOCK_FREE_T),1,f_block_free); + while ( !feof(f_block_free) ){ + if ( reg_f.block == num_bloque ){ + reg_f.free_space += tam_reg + sizeof(int); + fseek(f_block_free,-sizeof(BLOCK_FREE_T),SEEK_CUR); + fwrite(®_f,sizeof(BLOCK_FREE_T),1,f_block_free); + } + fread(®_f,sizeof(BLOCK_FREE_T),1,f_block_free); + } + fclose(f_block_free); + + /*actualizo archivo .did*/ + if ( (f_reg_exist = fopen(name_f_reg_exist,"a+")) == NULL) return -1; + fwrite(&ID, sizeof(int), 1, f_reg_exist); + fclose(f_reg_exist); + + printf("pase %d\n",__LINE__); + /*actualizo archivo .idx*/ + /*busco el registro que tengo que eliminar*/ + if ( (f_block_reg = fopen(name_f_block_reg,"r+")) == NULL ) return -1; + while ( !feof(f_block_reg) ){ + if ( fread(®_b,sizeof(BLOCK_REG_T),1,f_block_reg) != 1 ) continue; + if ( reg_b.id_reg == ID ) + break; + } + printf("registro borrado= %ld en bloque = %d\n",reg_b.id_reg,reg_b.block); + /*justifico en archivo a la izquieda*/ + while ( !feof(f_block_reg) ){ + cant = fread(&buffer,sizeof(BLOCK_REG_T),10,f_block_reg); + fseek(f_block_reg, -cant*sizeof(BLOCK_REG_T)-1, SEEK_CUR); + for (i=0; i