*/
#include "fsc.h"
-#include <string.h>
+#include "error.h"
#include <unistd.h>
+#include <sys/types.h>
+#include <string.h>
/* Crea un archivo de Gaps o Espacio Libre en Bloque */
int emufs_fsc_crear(EMUFS* efs)
}
/* Agrega un registro al archivo de espacio libre en bloque. */
-int emufs_fsc_agregar(EMUFS *emu, EMUFS_BLOCK_ID n_marker, EMUFS_FREE n_freespace)
+int emufs_fsc_agregar(EMUFS *emu, EMUFS_BLOCK_ID marker, EMUFS_FREE freespace)
{
FILE *f_fsc;
EMUFS_FSC reg;
strcpy(name_f_fsc,emu->nombre);
strcat(name_f_fsc, EMUFS_FSC_EXT);
- /* Cargo el registro */
- reg.n_marker = n_marker;
- reg.n_freespace = n_freespace;
-
/* Lo guardo en el archivo al final "a+"*/
- if ( (f_fsc = fopen(name_f_fsc,"a+"))==NULL ) return -1;
+ 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;
fwrite(®,sizeof(EMUFS_FSC),1,f_fsc);
fclose(f_fsc);
return 0;
}
/* Agrega un GAP en el archivo de Gaps para Filetype 2 */
-int emufs_fsc_agregar_gap(EMUFS *emu, EMUFS_OFFSET n_marker, EMUFS_FREE n_freespace)
+int emufs_fsc_agregar_gap(EMUFS *emu, EMUFS_OFFSET marker, EMUFS_FREE freespace)
{
- FILE *f_fsc;
+ FILE *f_fsc;
EMUFS_FSC gap_aux,gap_before,gap_after,gap_new;
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;
-
+ EMUFS_REG_ID pos_gap_before = 0, pos_gap_after = 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);
-
- gap_before.n_marker = -1;
- gap_after.n_marker = -1;
-
+
+ gap_before.marker = -1;
+ gap_after.marker = -1;
+
/* Busco si hay un GAP por delante y/o por detras del que se esta por crear */
/* para en dicho caso realizar un merge! */
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;
-
+
/* Chequeo si es un gap justo anterior al nuestro */
- if (gap_aux.n_marker+gap_aux.n_freespace == n_marker) {
- gap_before.n_marker = gap_aux.n_marker;
- gap_before.n_freespace = gap_aux.n_freespace;
- n_gap_before = n_reg_count;
+ if (gap_aux.marker+gap_aux.freespace == marker) {
+ gap_before.marker = gap_aux.marker;
+ gap_before.freespace = gap_aux.freespace;
+ pos_gap_before = reg_count;
}
-
+
/* Chequeo si es un gap justo posterior al nuestro */
- if (gap_aux.n_marker == n_marker+n_freespace) {
- gap_after.n_marker = gap_aux.n_marker;
- gap_after.n_freespace = gap_aux.n_freespace;
- n_gap_after = n_reg_count;
+ if (gap_aux.marker == marker+freespace) {
+ gap_after.marker = gap_aux.marker;
+ gap_after.freespace = gap_aux.freespace;
+ pos_gap_after = reg_count;
}
- n_reg_count += 1;
+ reg_count += 1;
}
-
+
/* Si no encontre gaps ni por delante ni por detras */
- if ((gap_before.n_marker == -1) && (gap_after.n_marker == -1)) {
- /* Lo guardo en el archivo al final */
- gap_new.n_marker = n_marker;
- gap_new.n_freespace = n_freespace;
- fseek(f_fsc,0,SEEK_END);
- fwrite(&gap_new,sizeof(EMUFS_FSC),1,f_fsc);
+ if ((gap_before.marker == -1) && (gap_after.marker == -1)) {
+ /* Lo guardo ordenado donde deba ir */
+ gap_new.marker = marker;
+ gap_new.freespace = freespace;
+ /* Busco el gap que sucede a este */
+ fseek(f_fsc,0,SEEK_SET);
+ while (!feof(f_fsc)) {
+ if (fread(&gap_aux,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue;
+ 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);
}
-
+
/* Si encuentro un GAP Justo por delante pero no por detras */
- if ((gap_before.n_marker != -1) && (gap_after.n_marker == -1))
+ if ((gap_before.marker != -1) && (gap_after.marker == -1))
{
- printf("fsc.c >> Found GAP justo por Delante pero no por Detras!!\n");
- printf("fsc.c >> Gap found is reg number %lu in .fsc file\n",n_gap_before);
- /* Me posiciono en el registro que indica dicho gap y lo reescribo con */
- /* la suma de los espacios libres */
- fseek(f_fsc,sizeof(EMUFS_FSC)*n_gap_before,0);
- gap_new.n_marker = gap_before.n_marker;
- gap_new.n_freespace = gap_before.n_freespace + n_freespace;
- fwrite(&gap_new,sizeof(EMUFS_FSC),1,f_fsc);
- fclose(f_fsc);
+ /* Me posiciono en el registro que indica dicho gap y lo reescribo con */
+ /* la suma de los espacios libres */
+ fseek(f_fsc,sizeof(EMUFS_FSC)*pos_gap_before,0);
+ gap_new.marker = gap_before.marker;
+ gap_new.freespace = gap_before.freespace + freespace;
+ fwrite(&gap_new,sizeof(EMUFS_FSC),1,f_fsc);
+ fclose(f_fsc);
}
-
+
/* Si encuentro un GAP Justo por detras pero no por delante */
- if ((gap_before.n_marker == -1) && (gap_after.n_marker != -1))
- {
- printf("fsc.c >> Found GAP justo por Detras pero no por Delante!!\n");
- printf("fsc.c >> Gap found is reg number %lu in .fsc file\n",n_gap_after);
- /* Me posiciono en el registro que indica dicho gap y lo reescribo con */
- /* los datos actualizados de offset y espacio */
- fseek(f_fsc,sizeof(EMUFS_FSC)*n_gap_after,0);
- gap_new.n_marker = gap_after.n_marker - n_freespace;
- gap_new.n_freespace = gap_after.n_freespace + n_freespace;
- fwrite(&gap_new,sizeof(EMUFS_FSC),1,f_fsc);
- fclose(f_fsc);
+ if ((gap_before.marker == -1) && (gap_after.marker != -1))
+ {
+ /* Me posiciono en el registro que indica dicho gap y lo reescribo con */
+ /* los datos actualizados de offset y espacio */
+ fseek(f_fsc,sizeof(EMUFS_FSC)*pos_gap_after,0);
+ gap_new.marker = gap_after.marker - freespace;
+ gap_new.freespace = gap_after.freespace + freespace;
+ fwrite(&gap_new,sizeof(EMUFS_FSC),1,f_fsc);
+ fclose(f_fsc);
}
-
+
/* Finalmente, si encuentro Justo por delante y por detras..*/
- if ((gap_before.n_marker != -1) && (gap_after.n_marker != -1))
- {
- printf("fsc.c >> Found GAP justo por Delante y por Detras!!\n");
- 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);
- 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);
-
- /* Preparo el escenario para la movida de registros */
- n_destination = sizeof(EMUFS_FSC)*n_gap_after;
- n_source = n_destination+sizeof(EMUFS_FSC); /* Salteo el gap_after! */
- 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_new,sizeof(EMUFS_FSC),1,f_fsc);
- fseek(f_fsc,-sizeof(EMUFS_FSC)*2,SEEK_CUR);
+ if ((gap_before.marker != -1) && (gap_after.marker != -1))
+ {
+ /* Guardo el nuevo GAP que posee los tres espacios sumados */
+ if (pos_gap_before < pos_gap_after) {
+ fseek(f_fsc,sizeof(EMUFS_FSC)*pos_gap_before,0);
+ destination = sizeof(EMUFS_FSC)*pos_gap_after;
+ }
+ else {
+ fseek(f_fsc,sizeof(EMUFS_FSC)*pos_gap_after,0);
+ destination = sizeof(EMUFS_FSC)*pos_gap_before;
+ }
+ gap_new.marker = gap_before.marker;
+ gap_new.freespace = gap_before.freespace + freespace + gap_after.freespace;
fwrite(&gap_new,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));
+
+ /* Preparo el escenario para la movida de registros */
+ source = destination+sizeof(EMUFS_FSC); /* Salteo el gap que elimino! */
+ fseek(f_fsc,0,SEEK_END);
+ file_size = ftell(f_fsc);
+ reg_count = (file_size - source) / sizeof(EMUFS_FSC);
+
+ /* Comienzo a mover */
+ while (cant_moved < reg_count) {
+ fseek(f_fsc,source,0);
+ fread(&gap_new,sizeof(EMUFS_FSC),1,f_fsc);
+ fseek(f_fsc,-sizeof(EMUFS_FSC)*2,SEEK_CUR);
+ fwrite(&gap_new,sizeof(EMUFS_FSC),1,f_fsc);
+ source += sizeof(EMUFS_FSC);
+ ++cant_moved;
+ }
+ fclose(f_fsc);
+ truncate(name_f_fsc, file_size - 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)
+int emufs_fsc_remove_gap(EMUFS *emu, EMUFS_OFFSET 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;
+ unsigned long source,destination,file_size,reg_count = 0,cant_moved = 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) ){
if ( fread(&gap_aux,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue;
- if ( gap_aux.n_marker == n_marker ) break;
+ if ( gap_aux.marker == 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! */
+ destination = ftell(f_fsc);
+ source = 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);
+ file_size = ftell(f_fsc);
+ reg_count = (file_size - source) / sizeof(EMUFS_FSC);
/* Comienzo a mover */
- while (n_moved < n_reg_count) {
- fseek(f_fsc,n_source,0);
+ while (cant_moved < reg_count) {
+ fseek(f_fsc,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;
+ source += sizeof(EMUFS_FSC);
+ ++cant_moved;
}
fclose(f_fsc);
- truncate(name_f_fsc, n_filesize - sizeof(EMUFS_FSC));
+ truncate(name_f_fsc, file_size - 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)
+int emufs_fsc_actualizar(EMUFS *emu, EMUFS_BLOCK_ID marker, EMUFS_FREE freespace)
{
FILE *f_fsc;
EMUFS_FSC reg;
if ( (f_fsc = fopen(name_f_fsc,"r+")) == NULL) return -1;
while ( !feof(f_fsc) ){
if ( fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue;
- if ( reg.n_marker == n_marker ){
- reg.n_freespace = n_freespace;
+ if ( reg.marker == marker ){
+ reg.freespace = freespace;
fseek(f_fsc,-sizeof(EMUFS_FSC),SEEK_CUR);
fwrite(®,sizeof(EMUFS_FSC),1,f_fsc);
break;
}
/* 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)
+int emufs_fsc_actualizar_gap(EMUFS *emu, EMUFS_OFFSET marker, EMUFS_FREE freespace)
{
FILE *f_fsc;
EMUFS_FSC gap_aux;
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;
+ if ( gap_aux.marker == marker ){
+ gap_aux.marker = marker + gap_aux.freespace - freespace;
+ gap_aux.freespace = freespace;
fseek(f_fsc,-sizeof(EMUFS_FSC),SEEK_CUR);
fwrite(&gap_aux,sizeof(EMUFS_FSC),1,f_fsc);
break;
}
/* 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 n_regsize, EMUFS_FREE *n_freespace)
+EMUFS_BLOCK_ID emufs_fsc_buscar_lugar(EMUFS *emu, EMUFS_FREE reg_size, EMUFS_FREE *freespace)
{
FILE *f_fsc;
EMUFS_FSC reg;
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)){
if (fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue;
- if (reg.n_freespace >= n_regsize) {
+ if (reg.freespace >= reg_size) {
found = 1;
break;
}
/* Si salio por error o por fin de archivo y no encontro space... */
if (!found) {
- reg.n_marker = EMUFS_NOT_FOUND;
- *n_freespace = emu->tam_bloque;
+ reg.marker = EMUFS_NOT_FOUND;
+ *freespace = emu->tam_bloque;
}
- else *n_freespace = reg.n_freespace;
+ else *freespace = reg.freespace;
fclose(f_fsc);
- return reg.n_marker;
+ 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 = 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 = 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 = 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 = 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 n_marker)
+EMUFS_FREE emufs_fsc_get_fs(EMUFS *emu, EMUFS_BLOCK_ID marker)
{
FILE *f_fsc;
EMUFS_FSC reg;
if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return -1;
while ( !feof(f_fsc) ){
if ( fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1 ) continue;
- if ( reg.n_marker == n_marker )
+ if ( reg.marker == marker )
break;
}
fclose(f_fsc);
- return reg.n_freespace;
+ return reg.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.freespace;
+ }
+ fclose(f_fsc);
+ return total;
+}
+
+int emufs_fsc_get_max_min_fs(EMUFS *emu, EMUFS_FREE *min, EMUFS_FREE *max)
+{
+ FILE *f_fsc;
+ EMUFS_FSC reg;
+ char name_f_fsc[255];
+
+ strcpy(name_f_fsc,emu->nombre);
+ strcat(name_f_fsc, EMUFS_FSC_EXT);
+
+ if ( (f_fsc = fopen(name_f_fsc,"r"))==NULL ) return -1;
+
+ /* 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;
+ }
+}
+
+EMUFS_FREE emufs_fsc_get_media_fs(EMUFS *emu)
+{
+ FILE *f_fsc;
+ EMUFS_FSC reg;
+ char name_f_fsc[255];
+ EMUFS_FREE total_fs = 0;
+ EMUFS_REG_ID gap_count = 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) ){
+ if ( fread(®, sizeof(EMUFS_FSC), 1, f_fsc) != 1) continue;
+ total_fs += reg.freespace;
+ ++gap_count;
+ }
+
+ fclose(f_fsc);
+
+ if (gap_count > 0) return total_fs/gap_count;
+ else return 0;
+}
+
+EMUFS_BLOCK_ID emufs_fsc_get_cant_bloques_vacios(EMUFS *emu)
+{
+ FILE *f_fsc;
+ EMUFS_FSC reg;
+ char name_f_fsc[255];
+ EMUFS_BLOCK_ID 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));
+}
+
+EMUFS_BLOCK_ID emufs_fsc_get_num_blocks(EMUFS* efs)
+{
+ FILE *f_fsc;
+ char name_f_fsc[255];
+ EMUFS_BLOCK_ID cant = 0;
+
+ strcpy(name_f_fsc, efs->nombre);
+ strcat(name_f_fsc, EMUFS_FSC_EXT);
+
+ if (!(f_fsc = fopen(name_f_fsc, "ab"))) {
+ PERR("error al abrir archivo .fsc");
+ return EMUFS_NOT_FOUND;
+ }
+ cant = ftell(f_fsc) / sizeof(EMUFS_FSC);
+ fclose(f_fsc);
+ return cant;
}