X-Git-Url: https://git.llucax.com/z.facultad/75.06/emufs.git/blobdiff_plain/c407055e1cc6ff4c435114091c3a5430486aa659..b1be8a89626ce30127758ad5e01a5c6069516fae:/emufs/idx.c?ds=inline diff --git a/emufs/idx.c b/emufs/idx.c index c05c34c..6ee1b87 100644 --- a/emufs/idx.c +++ b/emufs/idx.c @@ -68,26 +68,31 @@ int emufs_idx_crear(EMUFS *efs) /* Devuelve el mayor id de registro utilizado so far en el archivo de datos, revisando el indice. */ EMUFS_REG_ID emufs_idx_buscar_mayor_id(EMUFS *emu) { - EMUFS_REG_ID n_IdReg, max = -1; + EMUFS_REG_ID n_idreg, max = 0; FILE *f_idx; EMUFS_IDX reg; char name_f_idx[255]; /* TODO usar malloc para no limitar el tamaƱo de nombre de archivo */ + unsigned short int b_found = 0; strcpy(name_f_idx,emu->nombre); strcat(name_f_idx, EMUFS_IDX_EXT); if ( (f_idx = fopen(name_f_idx,"r")) == NULL) return -1; /*ERROR*/ - n_IdReg = -1; + n_idreg = -1; while ( !feof(f_idx) ){ /* Me aseguro de leer la cantidad de bytes correcta */ if (fread(®,sizeof(EMUFS_IDX),1,f_idx) != 1) continue; - if ( reg.n_IdReg >= max ) - max = reg.n_IdReg; + if ( reg.n_idreg >= max ) { + max = reg.n_idreg; + b_found = 1; + } } - n_IdReg = max+1; fclose(f_idx); - - return n_IdReg; + + if (!b_found) + return (0); + else + return(max+1); } /* busca el registro ID en el archivo ".idx" y devuelve el nro de bloque en el que se encuentra */ @@ -105,18 +110,20 @@ EMUFS_BLOCK_ID emufs_idx_buscar_registro(EMUFS *emu, EMUFS_REG_ID n_IdReg) while (!feof(f_idx) && !b_Found){ if (fread(®,sizeof(EMUFS_IDX),1,f_idx) != 1) continue; - if (reg.n_IdReg == n_IdReg) b_Found = 1; + if (reg.n_idreg == n_IdReg) b_Found = 1; } fclose(f_idx); /* Sino lo encontre devuelvo uno, otherwise el offset o bloque */ - if (!b_Found) return(-1); - else return(reg.n_Location); + if (!b_Found) + return(-1); + else + return(reg.n_location); } /* agrega un registro al final del archivo */ -int emufs_idx_agregar(EMUFS *emu, EMUFS_BLOCK_ID n_Location, EMUFS_REG_ID n_IdReg) +int emufs_idx_agregar(EMUFS *emu, EMUFS_REG_ID n_idreg, EMUFS_BLOCK_ID n_location) { FILE *f_idx; EMUFS_IDX reg; @@ -128,8 +135,8 @@ int emufs_idx_agregar(EMUFS *emu, EMUFS_BLOCK_ID n_Location, EMUFS_REG_ID n_IdRe if ( (f_idx = fopen(name_f_idx,"a+"))==NULL ) return -1; /* Note: Location = Bloque para Tipo 1 y 3, Offset para Tipo 2 */ - reg.n_Location = n_Location; - reg.n_IdReg = n_IdReg; + reg.n_idreg = n_idreg; + reg.n_location = n_location; fwrite(®,sizeof(EMUFS_IDX),1,f_idx); fclose(f_idx); return 0; @@ -151,7 +158,7 @@ int emufs_idx_borrar(EMUFS *emu, EMUFS_REG_ID n_IdReg) while ( !feof(f_idx) ){ /*busco cual tengo que borrar*/ if ( fread(®, sizeof(EMUFS_IDX), 1, f_idx) != 1 ) continue; - if ( reg.n_IdReg == n_IdReg ) + if ( reg.n_idreg == n_IdReg ) break; }