make_dir.c

Vai alla documentazione di questo file.
00001 
00012 #include <stdio.h>
00013 #include <libgen.h>
00014 #include <stdlib.h>
00015 #include <string.h>
00016 
00017 #include "Include/fat.h" 
00018 #include "Include/util.h"
00019 
00020 int fat_mkdir(FILE *fs, struct fat_ctrl *f_ctrl, char *path){
00021         struct dir_entry cart_p;
00022         struct dir_entry cart_new;
00023         char *basec, *bname;
00024         int ret_val=0;
00025         unsigned int new_index=0;
00026         long fs_radice;
00027         
00028         if ( !(fs) || !(f_ctrl) || !(path) ) return EBDP;
00029         if (!strcmp("/", path)) return EDAEX;
00030 
00031         /* a) Posizionamento nel filesystem, controllo del percoso ecc. */
00032         if ((ret_val = parsing(fs, f_ctrl, path, MKDIR))!=0) return ret_val;
00033 
00034         /* Reperisco il nome della cartella da creare */
00035         if (!(basec = strdup(path))) return STDLIBERR;
00036         bname = basename(basec);
00037         if (!strcmp(".", bname) || !strcmp("..", bname)){free(basec); return EDAEX;}
00038                 
00039         fs_radice = ftell(fs);
00040         /* Leggo l'entry iniziale '.' della cartella corrente */
00041         if (!(fread(&cart_p, sizeof(struct dir_entry), 1, fs))){free(basec); return ERBD;}
00042 
00043         /* Queste due righe sottostanti vanno sempre insieme!! */
00044         fseek(fs, fs_radice, SEEK_SET);
00045         /*      b) controllo che la cartella non esista gia' */
00046         ret_val = find(fs, f_ctrl, bname, MKDIR);
00047         
00048         /* Cerco un nuovo blocco libero della fat (BLOCK_FREE) che verra' dedicato
00049          alle entry della nuova cartella. se non c'e' ne sono esco con ENMSD */
00050         if (ret_val==0){ 
00051                 for (new_index=0; new_index<f_ctrl->b_sector.num_block; new_index++)
00052                         if (f_ctrl->fat_ptr[new_index] == BLOCK_FREE) break;
00053         }
00054         if (new_index>=f_ctrl->b_sector.num_block || ret_val!=0){
00055                 /* Al fine di annullare i possibili cambiamenti portati dalla funzione
00056                  * 'find' rileggo le informazioni della fat */
00057                 fseek(fs, sizeof(struct boot_sector), SEEK_SET);
00058                 if (!(fread(f_ctrl->fat_ptr, (f_ctrl->b_sector.num_block * sizeof(unsigned int)), 1, fs)))
00059                         ret_val = EWFCD;
00060                 else if (ret_val==0) ret_val = ENMSD; 
00061                 free(basec);
00062                 return ret_val;
00063         }
00064 
00065         /* Il blocco libero trovato della FAT deve essere impostato ad LAST_BLOCK. */
00066         f_ctrl->fat_ptr[new_index] = LAST_BLOCK;
00067 
00068         cart_new.attr = SUB_ENTRY;
00069         cart_new.index = new_index;             /* Indice del blocco delle entry della nuova cartella */
00070         cart_new.len = 2 * sizeof(struct dir_entry);
00071         memset(cart_new.name, 0x0, MAX_LEN_NAME+1);
00072         strncpy(cart_new.name,bname,MAX_LEN_NAME);
00073         cart_new.used = DIR_ENTRY_BUSY;
00074 
00075         /* Scrivo l'entry della cartella che sto creando, nella sua cartella radice */
00076         if (!(fwrite(&cart_new, sizeof(struct dir_entry), 1, fs))){free(basec); return EWFCD;}
00077 
00078         strncpy(cart_new.name,".",MAX_LEN_NAME);
00079         /* Posiziono il file pointer fs all'inizio del blocco che conterra' tutte le entry
00080          * relative alla cartella che sto creando */
00081         fseek(fs, (f_ctrl->blk_base + (cart_new.index * f_ctrl->b_sector.block_size)), SEEK_SET);       
00082         if (!(fwrite(&cart_new, sizeof(struct dir_entry), 1, fs))){free(basec); return EWFCD;}
00083 
00084         cart_new.index = cart_p.index;
00085         strncpy(cart_new.name,"..",MAX_LEN_NAME);
00086         if (!(fwrite(&cart_new, sizeof(struct dir_entry), 1, fs))){free(basec); return EWFCD;}
00087         
00088         /* Devo incrementare il conteggio delle entry nella cartella che contiene
00089         la nuova cartella che sto creando e scrivero' questo nuovo dato nel filesystem */
00090         fseek(fs, fs_radice, SEEK_SET);
00091         cart_p.len += sizeof(struct dir_entry);
00092         if (!(fwrite(&cart_p, sizeof(struct dir_entry), 1, fs))){free(basec); return ERBD;}
00093 
00094         /* Metto il puntatore fs all'inizio della FAT */
00095         fseek(fs, sizeof(struct boot_sector), SEEK_SET);        
00096         /* Scrittura dei nuovi dati della FAT nel file */
00097         if (!(fwrite(f_ctrl->fat_ptr, (f_ctrl->b_sector.num_block * sizeof(unsigned int)), 1, fs)))
00098                 ret_val = EWFCD;
00099         
00100         free(basec);
00101         return ret_val;
00102 }
Generato il Fri Jan 28 22:16:31 2011 per SFAT: Simplified File Allocation Table Project da  doxygen 1.6.3