X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/6effd0769b012b1ac78cfeda307cec54802f9b58..08c5b98cd8a74821b3ef0a72c6385eb6b267fff3:/emufs/fsc.c diff --git a/emufs/fsc.c b/emufs/fsc.c index a3aad0e..4c5008a 100644 --- a/emufs/fsc.c +++ b/emufs/fsc.c @@ -55,12 +55,23 @@ int emufs_fsc_agregar(EMUFS *emu, EMUFS_BLOCK_ID marker, EMUFS_FREE freespace) strcpy(name_f_fsc,emu->nombre); strcat(name_f_fsc, EMUFS_FSC_EXT); + /* Lo guardo en el archivo al final "a+"*/ + if ( (f_fsc = fopen(name_f_fsc,"r+"))==NULL ) return -1; + /* lo busco.. si esta lo modifico y si no lo agrego */ + fseek(f_fsc,0,SEEK_SET); + while ( !feof(f_fsc) ){ + if ( fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue; + if ( reg.marker == marker ){ + fseek(f_fsc,-sizeof(EMUFS_FSC),SEEK_CUR); + reg.freespace = freespace; + fwrite(®,sizeof(EMUFS_FSC),1,f_fsc); + fclose(f_fsc); + return 0; + } + } /* Cargo el registro */ reg.marker = marker; reg.freespace = freespace; - - /* Lo guardo en el archivo al final "a+"*/ - if ( (f_fsc = fopen(name_f_fsc,"a+"))==NULL ) return -1; fwrite(®,sizeof(EMUFS_FSC),1,f_fsc); fclose(f_fsc); return 0; @@ -280,6 +291,17 @@ EMUFS_BLOCK_ID emufs_fsc_buscar_lugar(EMUFS *emu, EMUFS_FREE reg_size, EMUFS_FRE if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return EMUFS_NOT_FOUND; + if ( emu->tam_reg > emu->tam_bloque-sizeof(EMUFS_REG_ID) ){ + fseek(f_fsc,0,SEEK_SET); + while(!feof(f_fsc)){ + if (fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue; + if (reg.freespace == emu->tam_bloque) { + fclose(f_fsc); + *freespace = reg.freespace; + return reg.marker; + } + } + } /* Inicializamos la estructura para devolver algun valor en concreto */ /* en caso de que no se halle un espacio libre apropiado */ while(!feof(f_fsc)){ @@ -390,3 +412,33 @@ EMUFS_FREE emufs_fsc_get_media_fs(EMUFS *emu) fclose(f_fsc); return total_fs/gap_count; } + +int emufs_fsc_get_cant_bloques_vacios(EMUFS *emu) +{ + FILE *f_fsc; + EMUFS_FSC reg; + char name_f_fsc[255]; + int cant=0; + + strcpy(name_f_fsc,emu->nombre); + strcat(name_f_fsc, EMUFS_FSC_EXT); + + if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return -1; + while ( !feof(f_fsc) ){ + fread(®, sizeof(EMUFS_FSC), 1, f_fsc); + if ( reg.freespace == emu->tam_bloque ) + cant++; + } + + fclose(f_fsc); + return cant; +} + +int emufs_fsc_truncate(EMUFS* efs, EMUFS_BLOCK_ID blocks) +{ + char name_f_fsc[255]; + + strcpy(name_f_fsc, efs->nombre); + strcat(name_f_fsc, EMUFS_FSC_EXT); + return truncate(name_f_fsc, blocks * sizeof(EMUFS_FSC)); +}