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
00030 if ((ret_val = parsing(fs, f_ctrl, path, MKFILE))!=0) return ret_val;
00031
00032
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
00039 if (!(fread(&cart_p, sizeof(struct dir_entry), 1, fs))){free(basec); return ERBD;}
00040
00041
00042 fseek(fs, fs_radice, SEEK_SET);
00043
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;
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
00054 if (!(fwrite(&file_new, sizeof(struct dir_entry), 1, fs))){free(basec); return EWFCD;}
00055
00056
00057
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
00063 fseek(fs, sizeof(struct boot_sector), SEEK_SET);
00064
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 }