X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/7a86d06abb03ced86951067cf341f228acfc9f30..eb5e623c9e1334da07faa5f4b8b6c3ebf431503f:/emufs/fsc.c?ds=sidebyside diff --git a/emufs/fsc.c b/emufs/fsc.c index b8e0db4..d6ec6a0 100644 --- a/emufs/fsc.c +++ b/emufs/fsc.c @@ -1,45 +1,145 @@ +/* vim: set noexpandtab tabstop=4 shiftwidth=4: + *---------------------------------------------------------------------------- + * emufs + *---------------------------------------------------------------------------- + * This file is part of emufs. + * + * emufs is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * emufs is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along + * with emufs; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + *---------------------------------------------------------------------------- + * Creado: vie abr 9 16:17:50 ART 2004 + * Autores: Nicolás Dimov + * Leandro Lucarella + *---------------------------------------------------------------------------- + * + * $Id$ + * + */ + +/** \file + * + * Archivo para administrar el espacio libre disponible. + * + * Implementación del archivo para administrar el espacio libre disponible. + * + */ + #include "fsc.h" -#include "tipo3.h" +#include +#include + +/* Crea un archivo de Gaps o Espacio Libre en Bloque */ +int emufs_fsc_crear(EMUFS* efs) +{ + return emufs_crear_archivo_auxiliar(efs->nombre, EMUFS_FSC_EXT); +} -int emufs_fsc_agregar(EMUFS *emu, int num_bloque, int fs) +/* Agrega un registro al archivo de espacios libres. */ +int emufs_fsc_agregar(EMUFS *emu, EMUFS_BLOCK_ID n_marker, EMUFS_FREE n_freespace) { FILE *f_fsc; - BLOCK_FREE_T reg; + EMUFS_FSC reg; char name_f_fsc[255]; strcpy(name_f_fsc,emu->nombre); - strcat(name_f_fsc,".fsc"); + strcat(name_f_fsc, EMUFS_FSC_EXT); - /*cargo el registro*/ - reg.block = num_bloque; /*no incremento cant, porque grabe el nuevo bloque antes y no lo conte!!*/ - reg.free_space = fs; - /*lo guardo en el archivo al final "a+"*/ + /* 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; - fwrite(®,sizeof(BLOCK_FREE_T),1,f_fsc); + fwrite(®,sizeof(EMUFS_FSC),1,f_fsc); fclose(f_fsc); return 0; } -int emufs_fsc_actualizar(EMUFS *emu, int num_bloque, int fs) +/* 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) { FILE *f_fsc; - BLOCK_FREE_T reg; + EMUFS_FSC reg; char name_f_fsc[255]; strcpy(name_f_fsc,emu->nombre); - strcat(name_f_fsc,".fsc"); + strcat(name_f_fsc, EMUFS_FSC_EXT); - /*busco el bloque que modifique*/ + /*busco el bloque o gap que modifique*/ if ( (f_fsc = fopen(name_f_fsc,"r+")) == NULL) return -1; while ( !feof(f_fsc) ){ - if ( fread(®,sizeof(BLOCK_FREE_T),1,f_fsc) != 1) continue; - if ( reg.block == num_bloque ){ - reg.free_space -= fs; - fseek(f_fsc,-sizeof(BLOCK_FREE_T),SEEK_CUR); - fwrite(®,sizeof(BLOCK_FREE_T),1,f_fsc); + if ( fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue; + if ( reg.n_marker == n_marker ){ + reg.n_freespace = n_freespace; + fseek(f_fsc,-sizeof(EMUFS_FSC),SEEK_CUR); + fwrite(®,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 n_RegSize, EMUFS_FREE *n_freespace) +{ + FILE *f_fsc; + EMUFS_FSC reg; + char name_f_fsc[255]; + unsigned short int b_Found = 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; + + /* Inicializamos la estructura para devolver algun valor en concreto */ + /* en caso de que no se halle un espacio libre apropiado */ + while(!feof(f_fsc) && !b_Found){ + if (fread(®,sizeof(EMUFS_FSC),1,f_fsc) != 1) continue; + if (reg.n_freespace >= n_RegSize) b_Found = 1; + } + + /* Si salio por error o por fin de archivo y no encontro space... */ + if (!b_Found) { + reg.n_marker = -1; + *n_freespace = emu->tam_bloque; + } + else *n_freespace = reg.n_freespace; + + fclose(f_fsc); + return reg.n_marker; +} + +/* Devuelve el espacio libre de un Bloque o Gap dado */ +EMUFS_FREE emufs_fsc_get_fs(EMUFS *emu, EMUFS_BLOCK_ID n_marker) +{ + FILE *f_fsc; + EMUFS_FSC reg; + char name_f_fsc[255]; + + strcpy(name_f_fsc,emu->nombre); + strcat(name_f_fsc, EMUFS_FSC_EXT); + + /* Busco el Bloque o Gap pedido y obtengo su espacio libre */ + 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 ) + break; + } + + fclose(f_fsc); + return reg.n_freespace; +}