X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/9b6d59d2a6f3ed7e0d2413c3730c29f1041dab2c..771d5d90ad3fa04217d2f517a79bea62f2c4e9e1:/emufs/fsc.c diff --git a/emufs/fsc.c b/emufs/fsc.c index 044ef33..9063c81 100644 --- a/emufs/fsc.c +++ b/emufs/fsc.c @@ -84,7 +84,8 @@ int emufs_fsc_agregar_gap(EMUFS *emu, EMUFS_OFFSET marker, EMUFS_FREE freespace) EMUFS_FSC gap_aux,gap_before,gap_after,gap_new; char name_f_fsc[255]; EMUFS_REG_ID pos_gap_before = 0, pos_gap_after = 0; - unsigned long source,destination,file_size,reg_count = 0,cant_moved = 0; + unsigned long source,destination,limit,file_size,reg_count = 0,cant_moved = 0; + char found = 0; strcpy(name_f_fsc,emu->nombre); strcat(name_f_fsc, EMUFS_FSC_EXT); @@ -114,13 +115,44 @@ int emufs_fsc_agregar_gap(EMUFS *emu, EMUFS_OFFSET marker, EMUFS_FREE freespace) reg_count += 1; } - /* Si no encontre gaps ni por delante ni por detras */ +/* Si no encontre gaps ni por delante ni por detras */ if ((gap_before.marker == -1) && (gap_after.marker == -1)) { - /* Lo guardo en el archivo al final */ + /* Lo guardo ordenado donde deba ir */ gap_new.marker = marker; gap_new.freespace = freespace; - fseek(f_fsc,0,SEEK_END); - fwrite(&gap_new,sizeof(EMUFS_FSC),1,f_fsc); + /* Busco el gap que sucede a este */ + fseek(f_fsc,0,SEEK_SET); + while (!feof(f_fsc)) { + fread(&gap_aux,sizeof(EMUFS_FSC),1,f_fsc); + if (gap_aux.marker > gap_new.marker) { + found = 1; + break; + } + } + if (found == 1) { + /* Movemos todos los gaps desde el sucesor hasta el final, una pos adelante */ + limit = ftell(f_fsc) - sizeof(EMUFS_FSC); + fseek(f_fsc,0,SEEK_END); + reg_count = (ftell(f_fsc) - limit) / sizeof(EMUFS_FSC); + source = ftell(f_fsc) - sizeof(EMUFS_FSC); + + while (cant_moved < reg_count) + { + fseek(f_fsc,source,SEEK_SET); + fread(&gap_aux,sizeof(EMUFS_FSC),1,f_fsc); + fwrite(&gap_aux,sizeof(EMUFS_FSC),1,f_fsc); + source -= sizeof(EMUFS_FSC); + ++cant_moved; + } + /* Agrego el nuevo registro */ + fseek(f_fsc,limit,SEEK_SET); + fwrite(&gap_new,sizeof(EMUFS_FSC),1,f_fsc); + } + else { + fseek(f_fsc,0,SEEK_END); + fwrite(&gap_new,sizeof(EMUFS_FSC),1,f_fsc); + } + fclose(f_fsc); } @@ -323,6 +355,66 @@ EMUFS_BLOCK_ID emufs_fsc_buscar_lugar(EMUFS *emu, EMUFS_FREE reg_size, EMUFS_FRE return reg.marker; } +/** Busca n lugares consecutivos devolviendo el id del primer bloque. */ +EMUFS_BLOCK_ID emufs_fsc_buscar_n_lugares(EMUFS* efs, size_t n, + EMUFS_FREE reg_size, EMUFS_FREE *freespace, int* err) +{ + FILE *f_fsc; + EMUFS_FSC reg; + char name_f_fsc[255]; + + /* chequeo que al menos se busque un lugar */ + if (!n) { + PERR("Se debe buscar al menos un lugar"); + *err = 13; /* EMUFS_ERROR_WRONG_ARGUMENT */ + return EMUFS_NOT_FOUND; + } + + /* abre archivo */ + strcpy(name_f_fsc, efs->nombre); + strcat(name_f_fsc, EMUFS_FSC_EXT); + if (!(f_fsc = fopen(name_f_fsc, "rb"))) { + PERR("No se puede abrir archivo"); + *err = 4; /* EMUFS_ERROR_CANT_OPEN_FILE */ + return EMUFS_NOT_FOUND; + } + + /* busca el espacio libre */ + while(!feof(f_fsc)) { + if ((fread(®, sizeof(EMUFS_FSC), 1, f_fsc) != 1)) { + if (feof(f_fsc)) break; + PERR("No se puede leer el archivo"); + *err = 3; /* EMUFS_ERROR_FILE_READ */ + return EMUFS_NOT_FOUND; + } + if (reg.freespace >= reg_size) { + int found = 1; + EMUFS_BLOCK_ID first_id = reg.marker; + *freespace = reg.freespace; + while (--n) { + if (fread(®, sizeof(EMUFS_FSC), 1, f_fsc) != 1) { + if (feof(f_fsc)) break; + PERR("No se puede leer el archivo"); + *err = 3; /* EMUFS_ERROR_FILE_READ */ + return EMUFS_NOT_FOUND; + } + /* no hay otro lugar consecutivo */ + if (reg.freespace < reg_size) { + found = 0; + break; + } + } + if (found) { + fclose(f_fsc); + return first_id; + } + } + } + /* no se encontrĂ³ espacio libre */ + *freespace = efs->tam_bloque; + return EMUFS_NOT_FOUND; +} + /* Devuelve el espacio libre de un Bloque o Gap dado */ EMUFS_FREE emufs_fsc_get_fs(EMUFS *emu, EMUFS_BLOCK_ID marker) { @@ -374,20 +466,31 @@ int emufs_fsc_get_max_min_fs(EMUFS *emu, EMUFS_FREE *min, EMUFS_FREE *max) strcpy(name_f_fsc,emu->nombre); strcat(name_f_fsc, EMUFS_FSC_EXT); - *min = ULONG_MAX; - *max = 0; 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 < *min ) - *min = reg.freespace; - if ( reg.freespace > *max ) - *max = reg.freespace; + + /* Si el file esta vacio, devuelvo valores nulos */ + fseek(f_fsc,0,SEEK_END); + if (ftell(f_fsc) == 0) { + *min = 0; + *max = 0; + return 0; + } + else + { + /* Busco Min y Max */ + *min = ULONG_MAX; + *max = 0; + fseek(f_fsc,0,SEEK_SET); + while ( !feof(f_fsc) ){ + if ( fread(®, sizeof(EMUFS_FSC), 1, f_fsc) != 1) continue; + if ( reg.freespace < *min ) + *min = reg.freespace; + if ( reg.freespace > *max ) + *max = reg.freespace; + } + fclose(f_fsc); + return 0; } - - fclose(f_fsc); - return 0; } EMUFS_FREE emufs_fsc_get_media_fs(EMUFS *emu) @@ -404,11 +507,43 @@ EMUFS_FREE emufs_fsc_get_media_fs(EMUFS *emu) if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return -1; while ( !feof(f_fsc) ){ - fread(®, sizeof(EMUFS_FSC), 1, f_fsc); + if ( fread(®, sizeof(EMUFS_FSC), 1, f_fsc) != 1) continue; total_fs += reg.freespace; ++gap_count; } fclose(f_fsc); - return total_fs/gap_count; + + if (gap_count > 0) return total_fs/gap_count; + else return 0; +} + +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)); }