open_fat.c

Vai alla documentazione di questo file.
00001 
00013 #include <stdio.h> 
00014 #include <stdlib.h>
00015 #include <libgen.h>
00016 #include <string.h>
00017 
00018 #include "Include/fat.h"
00019 #include "Include/util.h"
00020 
00021 int fat_open(FILE *fs, struct fat_ctrl *f_ctrl, char *path){
00022         struct dir_entry cart_p, file_new;
00023         char *basec, *bname;
00024         int ret_val=0;
00025         long fs_radice;
00026                 
00027         if (!(fs) || !(f_ctrl) || !(path)) return EBDP;
00028         if (!strcmp("/", path)) return EDAEX;
00029         /* a) Posizionamento nel filesystem, controllo del percoso ecc. */
00030         if ((ret_val = parsing(fs, f_ctrl, path, MKFILE))!=0) return ret_val;
00031 
00032         /* Reperisco il nome del file da creare */
00033         if (!(basec = strdup(path))) return STDLIBERR;
00034         bname = basename(basec);
00035         if (!strcmp(".", bname) || !strcmp("..", bname)){free(basec); return EDAEX;}
00036         
00037         fs_radice = ftell(fs);
00038         /* Leggo l'entry iniziale '.' della cartella corrente */
00039         if (!(fread(&cart_p, sizeof(struct dir_entry), 1, fs))){free(basec); return ERBD;}
00040 
00041         /* Queste due righe sottostanti vanno sempre insieme!! */
00042         fseek(fs, fs_radice, SEEK_SET);
00043         /* Controllo che il file non esista gia' */
00044         if ((ret_val = find(fs, f_ctrl, bname, MKFILE))!=0){free(basec); return ret_val;}
00045 
00046         file_new.attr = FILE_ENTRY;
00047         file_new.index= BLOCK_FREE;     /* Indice del blocco del nuovo file ?? */
00048         file_new.len = 0;
00049         memset(file_new.name, 0x0, MAX_LEN_NAME+1);
00050         strncpy(file_new.name,bname,MAX_LEN_NAME);
00051         file_new.used = DIR_ENTRY_BUSY;
00052         
00053         /* Scrivo l'entry del file che sto creando, nella sua cartella radice */
00054         if (!(fwrite(&file_new, sizeof(struct dir_entry), 1, fs))){free(basec); return EWFCD;}
00055         
00056         /* Devo incrementare il conteggio delle entry nella cartella che contiene
00057         il nuovo file che sto creando e scrivero' questo nuovo dato nel filesystem */
00058         fseek(fs, fs_radice, SEEK_SET);
00059         cart_p.len += sizeof(struct dir_entry);
00060         if (!(fwrite(&cart_p, sizeof(struct dir_entry), 1, fs))){free(basec); return ERBD;}
00061 
00062         /* Metto il puntatore fs all'inizio della FAT */
00063         fseek(fs, sizeof(struct boot_sector), SEEK_SET);        
00064         /* Scrittura dei nuovi dati della FAT nel file */
00065         if (!(fwrite(f_ctrl->fat_ptr, (f_ctrl->b_sector.num_block * sizeof(unsigned int)), 1, fs)))
00066                 ret_val = EWFCD;
00067         
00068         free(basec);            
00069         return ret_val;
00070 }
Generato il Fri Jan 28 22:16:31 2011 per SFAT: Simplified File Allocation Table Project da  doxygen 1.6.3