]> git.llucax.com Git - z.facultad/75.06/emufs.git/blobdiff - emufs/fsc.c
* BUGFIX en idx_get . No filtraba los ids no utilizados.
[z.facultad/75.06/emufs.git] / emufs / fsc.c
index 562ae32fa2bd905b597718761a15cd1a6733365e..cc634c51f136bee9fe01f21df6f978c1fb756871 100644 (file)
@@ -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);          
        }