X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/46453eaa7a7cbd8a43d9bbebda683814b13a832c..fbef24da0ffefaa1ff6d6116d6c03197e862b9a5:/emufs/fsc.c?ds=inline diff --git a/emufs/fsc.c b/emufs/fsc.c index d9b26bb..e71d13d 100644 --- a/emufs/fsc.c +++ b/emufs/fsc.c @@ -74,8 +74,7 @@ int emufs_fsc_agregar_gap(EMUFS *emu, EMUFS_OFFSET n_marker, EMUFS_FREE n_freesp char name_f_fsc[255]; EMUFS_REG_ID n_gap_before = 0, n_gap_after = 0; unsigned long n_source,n_destination,n_filesize,n_reg_count = 0,n_moved = 0; - - + strcpy(name_f_fsc,emu->nombre); strcat(name_f_fsc, EMUFS_FSC_EXT); @@ -149,15 +148,20 @@ int emufs_fsc_agregar_gap(EMUFS *emu, EMUFS_OFFSET n_marker, EMUFS_FREE n_freesp printf("fsc.c >> Pre Gap found is reg number %lu in .fsc file\n",n_gap_before); printf("fsc.c >> Post Gap found is reg number %lu in .fsc file\n",n_gap_after); /* Guardo el nuevo GAP que posee los tres espacios sumados */ - fseek(f_fsc,sizeof(EMUFS_FSC)*n_gap_before,0); + if (n_gap_before < n_gap_after) { + fseek(f_fsc,sizeof(EMUFS_FSC)*n_gap_before,0); + n_destination = sizeof(EMUFS_FSC)*n_gap_after; + } + else { + fseek(f_fsc,sizeof(EMUFS_FSC)*n_gap_after,0); + n_destination = sizeof(EMUFS_FSC)*n_gap_before; + } gap_new.n_marker = gap_before.n_marker; gap_new.n_freespace = gap_before.n_freespace + n_freespace + gap_after.n_freespace; fwrite(&gap_new,sizeof(EMUFS_FSC),1,f_fsc); - /* Guardamos la posicion actual del fpi pues sera en donde comenzaremos */ - /* a mover los registros a recompactar. Ademas, calculo cuantos regs mover */ - n_destination = sizeof(EMUFS_FSC)*n_gap_after; - n_source = n_destination+sizeof(EMUFS_FSC); /* Salteo el gap_after! */ + /* Preparo el escenario para la movida de registros */ + n_source = n_destination+sizeof(EMUFS_FSC); /* Salteo el gap que elimino! */ fseek(f_fsc,0,SEEK_END); n_filesize = ftell(f_fsc); n_reg_count = (n_filesize - n_source) / sizeof(EMUFS_FSC); @@ -173,12 +177,54 @@ int emufs_fsc_agregar_gap(EMUFS *emu, EMUFS_OFFSET n_marker, EMUFS_FREE n_freesp ++n_moved; } fclose(f_fsc); - truncate(name_f_fsc, n_filesize - sizeof(EMUFS_FSC)); + truncate(name_f_fsc, n_filesize - sizeof(EMUFS_FSC)); } return 0; } +/* Elimina un registro GAP del archivo de espacios libres (gaps) */ +int emufs_fsc_remove_gap(EMUFS *emu, EMUFS_OFFSET n_marker) +{ + FILE *f_fsc; + EMUFS_FSC gap_aux; + char name_f_fsc[255]; + unsigned long n_source,n_destination,n_filesize,n_reg_count = 0,n_moved = 0; + + strcpy(name_f_fsc,emu->nombre); + strcat(name_f_fsc, EMUFS_FSC_EXT); + + /* Busco el Gap en el .fsc */ + if ((f_fsc = fopen(name_f_fsc,"r+")) == NULL) return -1; + while ( !feof(f_fsc) ){ + if ( fread(&gap_aux,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue; + if ( gap_aux.n_marker == n_marker ) break; + } + + /* Preparo el escenario para la movida de registros */ + fseek(f_fsc,-sizeof(EMUFS_FSC),SEEK_CUR); + n_destination = ftell(f_fsc); + n_source = n_destination+sizeof(EMUFS_FSC); /* Salteo el gap a eliminar! */ + fseek(f_fsc,0,SEEK_END); + n_filesize = ftell(f_fsc); + n_reg_count = (n_filesize - n_source) / sizeof(EMUFS_FSC); + printf("Destination: %lu Source: %lu Cant Regs a Mover: %lu\n",n_destination,n_source,n_reg_count); + + /* Comienzo a mover */ + while (n_moved < n_reg_count) { + fseek(f_fsc,n_source,0); + fread(&gap_aux,sizeof(EMUFS_FSC),1,f_fsc); + fseek(f_fsc,-sizeof(EMUFS_FSC)*2,SEEK_CUR); + fwrite(&gap_aux,sizeof(EMUFS_FSC),1,f_fsc); + n_source += sizeof(EMUFS_FSC); + ++n_moved; + } + fclose(f_fsc); + truncate(name_f_fsc, n_filesize - sizeof(EMUFS_FSC)); + + return 0; +} + /* Objetivo: Actualiza un registro de espacio libre de acorde al FType */ int emufs_fsc_actualizar(EMUFS *emu, EMUFS_BLOCK_ID n_marker, EMUFS_FREE n_freespace) { @@ -204,8 +250,34 @@ int emufs_fsc_actualizar(EMUFS *emu, EMUFS_BLOCK_ID n_marker, EMUFS_FREE n_frees return 0; } +/* Actualiza un registro de gap, en el archivo de Gaps en Disco */ +int emufs_fsc_actualizar_gap(EMUFS *emu, EMUFS_OFFSET n_marker, EMUFS_FREE n_freespace) +{ + FILE *f_fsc; + EMUFS_FSC gap_aux; + char name_f_fsc[255]; + + strcpy(name_f_fsc,emu->nombre); + strcat(name_f_fsc, EMUFS_FSC_EXT); + + /*busco el bloque o gap que modifique*/ + if ( (f_fsc = fopen(name_f_fsc,"r+")) == NULL) return -1; + while ( !feof(f_fsc) ){ + if ( fread(&gap_aux,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue; + if ( gap_aux.n_marker == n_marker ){ + gap_aux.n_marker = n_marker + gap_aux.n_freespace - n_freespace; + gap_aux.n_freespace = n_freespace; + fseek(f_fsc,-sizeof(EMUFS_FSC),SEEK_CUR); + fwrite(&gap_aux,sizeof(EMUFS_FSC),1,f_fsc); + break; + } + } + fclose(f_fsc); + return 0; +} + /* Me devuelve el ID del bloque u Offset del Gap donde quepa un registro, y guarda en n_freespace el espacio libre actualizado */ -EMUFS_BLOCK_ID emufs_fsc_buscar_lugar(EMUFS *emu, EMUFS_FREE regsize, EMUFS_FREE *freespace) +EMUFS_BLOCK_ID emufs_fsc_buscar_lugar(EMUFS *emu, EMUFS_FREE n_regsize, EMUFS_FREE *n_freespace) { FILE *f_fsc; EMUFS_FSC reg; @@ -221,7 +293,7 @@ EMUFS_BLOCK_ID emufs_fsc_buscar_lugar(EMUFS *emu, EMUFS_FREE regsize, EMUFS_FREE /* en caso de que no se halle un espacio libre apropiado */ while(!feof(f_fsc)){ if (fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue; - if (reg.n_freespace >= regsize) { + if (reg.n_freespace >= n_regsize) { found = 1; break; } @@ -230,9 +302,9 @@ EMUFS_BLOCK_ID emufs_fsc_buscar_lugar(EMUFS *emu, EMUFS_FREE regsize, EMUFS_FREE /* Si salio por error o por fin de archivo y no encontro space... */ if (!found) { reg.n_marker = EMUFS_NOT_FOUND; - *freespace = emu->tam_bloque; + *n_freespace = emu->tam_bloque; } - else *freespace = reg.n_freespace; + else *n_freespace = reg.n_freespace; fclose(f_fsc); return reg.n_marker; @@ -259,3 +331,23 @@ EMUFS_FREE emufs_fsc_get_fs(EMUFS *emu, EMUFS_BLOCK_ID n_marker) fclose(f_fsc); return reg.n_freespace; } + +EMUFS_FREE emufs_fsc_get_total_fs(EMUFS *emu) +{ + FILE *f_fsc; + EMUFS_FSC reg; + char name_f_fsc[255]; + EMUFS_FREE total; + + strcpy(name_f_fsc,emu->nombre); + strcat(name_f_fsc, EMUFS_FSC_EXT); + + if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return -1; + total = 0; + while ( !feof(f_fsc) ){ + if ( fread(®, sizeof(EMUFS_FSC), 1, f_fsc) != 1) continue; + total += reg.n_freespace; + } + fclose(f_fsc); + return total; +}