#include <stdio.h>
#include <libgen.h>
#include <stdlib.h>
#include <string.h>
#include "Include/fat.h"
#include "Include/util.h"
Vai al codice sorgente di questo file.
Funzioni | |
int | fat_mkdir (FILE *fs, struct fat_ctrl *f_ctrl, char *path) |
This program is free software; you can redistribuite it and/or modify it under the terms of the GNU/General Pubblic License as published the Free software Foundation; either version 2 of the License, or (at your opinion) any later version.
Definizione nel file make_dir.c.
int fat_mkdir | ( | FILE * | fs, | |
struct fat_ctrl * | f_ctrl, | |||
char * | path | |||
) |
Questa funzione ha il compito di creare una subdirectory SUBDIR all'interno di una directory DIR. Il pathname con cui si effettua la creazione della directory e' formato da DIR/SUBDIR. Ad esempio il pathname /this/is/a/path/name/example
e' formato dal pathname assoluto della directory DIR= /this/is/a/path/name/
in cui si vuole creare una nuova subdirectory, ed il nome SUBDIR= example
della subdirectory che si vuole creare. Ogni singola componete del pathname DIR (e.g. this
is
a
ecc.) viene riferito con il termine path element. In particolare questa funzione ha il compito di:
fs | il FILE pointer che consente di accedere al file dove il filesystem e' memorizzato. | |
f_ctrl | puntatore alla struttura che mantiene tutte le informazioni di controllo del filesystem. | |
path | il path assoluto in cui creare la nuova directory. L'ultimo elemento del path e' il nome della nuova directory. Ad esempio se path = /home/pippo allora la directory che deve essere creata ha nome pippo e deve essere creata come subdirectory di /home. |
Definizione alla linea 20 del file make_dir.c.
00020 { 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 }