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
00032 if ((ret_val = parsing(fs, f_ctrl, path, MKDIR))!=0) return ret_val;
00033
00034
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
00041 if (!(fread(&cart_p, sizeof(struct dir_entry), 1, fs))){free(basec); return ERBD;}
00042
00043
00044 fseek(fs, fs_radice, SEEK_SET);
00045
00046 ret_val = find(fs, f_ctrl, bname, MKDIR);
00047
00048
00049
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
00056
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
00066 f_ctrl->fat_ptr[new_index] = LAST_BLOCK;
00067
00068 cart_new.attr = SUB_ENTRY;
00069 cart_new.index = new_index;
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
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
00080
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
00089
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
00095 fseek(fs, sizeof(struct boot_sector), SEEK_SET);
00096
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 }