Lettura da file regolare. Continua...
#include "fat.h"
Vai al codice sorgente di questo file.
Funzioni | |
int | fat_read (FILE *fs, struct fat_ctrl *f_ctrl, char *path, int start, int data_len, char *data) |
Lettura da file regolare.
Definizione nel file read_fat.h.
int fat_read | ( | FILE * | fs, | |
struct fat_ctrl * | f_ctrl, | |||
char * | path, | |||
int | start, | |||
int | data_len, | |||
char * | data | |||
) |
Questa funzione ha il compito di leggere una sequenza di byte di un file memorizzato all'interno di un filesystem SFAT. La sequenza viene specificata tramite due parametri:
La funzione deve:
fs | il FILE pointer che consente di acceddre al file dove il filesystem e' memorizzato. | |
f_ctrl | puntatore alla struttura che mantiene tutte le informazioni di controllo del filesystem. | |
path | la stringa che contiene il path assoluto del file che deve essere letto. | |
start | la posizione del byte da cui cominciare la lettura. | |
data_len | il numero di byte da leggere a partire dal byte in posizone start . | |
data | il buffer in cui copiare i dati letti dal file. Il buffer deve essere allocato opportunamente. |
data_len
nel caso in cui si incontri la fine del file prima di leggere data_len
bytes. Definizione alla linea 20 del file read_fat.c.
00021 { 00022 struct dir_entry file; 00023 int letti=0, to_read=0, my_start=start, len=data_len, byte_da_leggere=0; 00024 unsigned int index=0; 00025 char* dati = data; 00026 long fs_pos=0; 00027 00028 if (!(fs) || !(f_ctrl) || !(path)) return EBDP; 00029 00030 /* a) Posizionamento nel filesystem, controllo del percoso, ecc. */ 00031 if ((letti = parsing(fs, f_ctrl, path, RDFILE))!=0) return letti; 00032 00033 /* Leggo l'entry del file */ 00034 if (!(fread(&file, sizeof(struct dir_entry), 1, fs))) return ERBD; 00035 if ((file.len==0) || (start>file.len) || (data_len==0)) return 0; 00036 if (data_len>file.len) len=file.len; 00037 if ((start + len) > file.len) 00038 len -= ((start + len) - file.len); 00039 00040 /* Calcolo la giusta posizione di partenza per 'fs' */ 00041 index = file.index; 00042 while(my_start>=f_ctrl->b_sector.block_size){ 00043 my_start -= f_ctrl->b_sector.block_size; 00044 if ((my_start>f_ctrl->b_sector.block_size) && (f_ctrl->fat_ptr[index]==LAST_BLOCK)) 00045 return ERFCD; 00046 index = f_ctrl->fat_ptr[index]; 00047 } 00048 fs_pos = f_ctrl->blk_base + my_start + (index * f_ctrl->b_sector.block_size); 00049 fseek(fs, fs_pos, SEEK_SET); 00050 00051 byte_da_leggere = len; 00052 while(len){ 00053 if ((my_start+len) > f_ctrl->b_sector.block_size){ 00054 to_read = f_ctrl->b_sector.block_size - my_start; 00055 len -= to_read; 00056 my_start = 0; 00057 } 00058 else if ((my_start+len) <= f_ctrl->b_sector.block_size){ 00059 to_read = len; 00060 } 00061 if (!(fread(dati, to_read, 1, fs))) return ERBD; 00062 dati += to_read; letti += to_read; 00063 if (letti == byte_da_leggere) break; 00064 /* Altrimenti spostati nel blocco successivo della FAT */ 00065 if (f_ctrl->fat_ptr[index]==BLOCK_FREE) return ERFCD; 00066 fs_pos = f_ctrl->blk_base + (f_ctrl->fat_ptr[index] * f_ctrl->b_sector.block_size); 00067 fseek(fs, fs_pos, SEEK_SET); 00068 index = f_ctrl->fat_ptr[index]; 00069 } 00070 return letti; 00071 }