#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "Include/fat.h"
#include "fat_defs.h"
Vai al codice sorgente di questo file.
Funzioni | |
int | fat_format (FILE *fs, int block_size, unsigned int num_block) |
This program is free software; you can redistribuite it and/or modify it under the terms of the GNU/General Pubblic License as published the Free software Foundation; either version 2 of the License, or (at your opinion) any later version.
Definizione nel file format.c.
int fat_format | ( | FILE * | fs, | |
int | block_size, | |||
unsigned int | num_block | |||
) |
Questa funzione ha il compito di formattare un file con il formato SFAT. In particolare deve realizzare le seguenti operazioni:
fs | puntatore al file che conterra' il file system e che deve quindi essere formattato | |
block_size | size di ciascun blocco (in byte) | |
num_block | numero di blocchi disponibili |
Definizione alla linea 20 del file format.c.
00020 { 00021 struct fat_ctrl myfat; 00022 void * data_r_block; 00023 struct dir_entry cart_p; 00024 int i, conta=0; 00025 00026 if ( !(fs) || !(block_size) || !(num_block)) return EBDP; 00027 if (!((block_size==128) || (block_size==1024) || (block_size==2048) 00028 || (block_size==4096))) 00029 return EWFCD; 00030 00031 /* - Inizializzazione del boot sector. */ 00032 myfat.b_sector.block_size = block_size; 00033 myfat.b_sector.fs_type = FAT_TYPE; 00034 myfat.b_sector.num_block = num_block; 00035 myfat.blk_base = sizeof(struct boot_sector) + sizeof(unsigned int) * num_block; 00036 00037 /* - Scrittura dei dati nel boot sector. */ 00038 if (!(fwrite(&myfat.b_sector, sizeof(struct boot_sector), 1, fs))) 00039 return EWFCD; 00040 00041 /* - Inizializzazione della File Allocation Table. 00042 * (sarebbe bastata un calloc, ma se a BLOCK_FREE si cambia valore, 00043 * l'errore sarebbe piuttosto evidente) 00044 * */ 00045 if (!(myfat.fat_ptr = malloc(num_block * sizeof(unsigned int)))) 00046 return EWFCD; 00047 00048 for (i=1; i<myfat.b_sector.num_block; i++) 00049 myfat.fat_ptr[i] = BLOCK_FREE; 00050 00051 myfat.fat_ptr[ROOT_IDX] = LAST_BLOCK; 00052 00053 /* Scrittura dei dati della FAT nel file */ 00054 if (!(fwrite(myfat.fat_ptr, (num_block * sizeof(unsigned int)), 1, fs))) 00055 return EWFCD; 00056 00057 free(myfat.fat_ptr); 00058 00059 /* Creazione del primo blocco della DATA REGION */ 00060 if (!(data_r_block = malloc(block_size))) return EWFCD; 00061 00062 /* Inizializzazione del blocco della Data Region */ 00063 memset(data_r_block, INIT_BLOCK, block_size); 00064 00065 /* Scrittura dei dati della DATA REGION nel file */ 00066 for (i=0; i<num_block; i++) 00067 conta += fwrite(data_r_block, block_size, 1, fs); 00068 if (conta != num_block) return EWFCD; 00069 free(data_r_block); 00070 00071 /* Torno alla root (inizio data region)*/ 00072 fseek(fs, myfat.blk_base, SEEK_SET); 00073 00074 memset(cart_p.name, 0x0, MAX_LEN_NAME+1); 00075 /* Il primo blocco della data region conterra' la Directory Table della radice */ 00076 /* fill info about dir entry '.' and '..' */ 00077 cart_p.attr = SUB_ENTRY; 00078 cart_p.index = ROOT_IDX; 00079 cart_p.len = 2 * sizeof(struct dir_entry); 00080 strncpy(cart_p.name,".",MAX_LEN_NAME); 00081 cart_p.used = DIR_ENTRY_BUSY; 00082 00083 if (!(fwrite(&cart_p, sizeof(struct dir_entry), 1, fs))) return EWFCD; 00084 00085 strncpy(cart_p.name,"..",MAX_LEN_NAME+1); 00086 if (!(fwrite(&cart_p, sizeof(struct dir_entry), 1, fs))) return EWFCD; 00087 00088 return 0; 00089 }