read_fat.c
Vai alla documentazione di questo file.00001
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016
00017 #include "Include/fat.h"
00018 #include "Include/util.h"
00019
00020 int fat_read(FILE *fs, struct fat_ctrl *f_ctrl, char *path, int start,
00021 int data_len, char *data){
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
00031 if ((letti = parsing(fs, f_ctrl, path, RDFILE))!=0) return letti;
00032
00033
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
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
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 }