#include #include #include #include #include #include "files.h" static int mounted=-1; int mount(unsigned int volume) { if ( mounted != -1 ) return -1; struct mbr_desc_t mbr; load_mbr(&mbr); if ( mbr.vol_count <= volume ) return -1; mounted = volume; return 0; } int umount() { if ( mounted == -1 ) return -1; mounted = -1; return 0; } struct fs_stats_t fs_info() { struct superblock_desc_t sblock; load_super(mounted, &sblock); struct fs_stats_t info; info.serial = sblock.serial; strcpy(info.label, sblock.label); info.root_inode = sblock.first_inode; info.free_space = sblock.free_space; return info; } int create_file(const char *pathname, enum file_type_e type) { assert(mounted != -1); assert(strlen(pathname) && pathname[0] == '/'); const char *filename; inode_t no = dinumber_of_path(mounted, pathname, &filename); /* Le fichier existe déjà */ if ( no > 0 ) return -1; /* Supprimer le nom du fichier en fin de chaine */ char *buf = strdup(pathname); strncpy(buf, pathname, (filename-pathname)-1); /* '/' ? */ if ( (filename - 1) == pathname ) buf[(filename-pathname)] = '\0'; else buf[(filename-pathname)-1] = '\0'; inode_t parent_no; if ( strcmp(pathname, "/") != 0 ) { /* Vérifier que le parent existe */ printf("%s\n", buf); parent_no = inumber_of_path(mounted, buf); if ( parent_no <= 0 ) { //printf("Le répertoire parent (%s) n'existe pas!\n", buf); free(buf); return -1; } } inode_t my_no = create_ifile(mounted, type); /* Ajouter '.' et '..' si c'est un répertoire */ if ( type == FILET_DIR ) { struct file_desc_t fd; open_ifile(mounted, my_no, &fd); add_entry(&fd, my_no, "."); if ( strcmp(pathname, "/") == 0 ) add_entry(&fd, my_no, ".."); else add_entry(&fd, parent_no, ".."); close_ifile(&fd); } if ( strcmp(pathname, "/") != 0 ) { /* Ajouter l'inode au répertoire parent */ struct file_desc_t par_fd; open_ifile(mounted, parent_no, &par_fd); add_entry(&par_fd, my_no, filename); close_ifile(&par_fd); } free(buf); return my_no; } int delete_file(const char *pathname) { assert(mounted != -1); assert(strlen(pathname) && pathname[0] == '/'); const char *filename; inode_t no = dinumber_of_path(mounted, pathname, &filename); /* Le fichier n'existe pas */ if ( no == 0 ) return -1; /* Supprimer le nom du fichier en fin de chaine */ char *buf = strdup(pathname); strncpy(buf, pathname, (filename-pathname)-1); /* '/' ? */ if ( (filename - 1) == pathname ) buf[(filename-pathname)] = '\0'; else buf[(filename-pathname)-1] = '\0'; inode_t parent_no; if ( strcmp(pathname, "/") != 0 ) { /* Vérifier que le parent existe */ parent_no = inumber_of_path(mounted, buf); if ( parent_no <= 0 ) { //printf("Le répertoire parent (%s) n'existe pas!\n", buf); free(buf); return -1; } struct file_desc_t par_fd; open_ifile(mounted, parent_no, &par_fd); del_entry(&par_fd, no); close_ifile(&par_fd); } return delete_ifile(mounted, no); } int open_file(struct file_desc_t *fd, const char *pathname) { assert(mounted != -1); assert(strlen(pathname) && pathname[0] == '/'); const char *filename; inode_t no = dinumber_of_path(mounted, pathname, &filename); /* Le fichier n'existe pas */ if ( no == 0 ) return -1; return open_ifile(mounted, no, fd); } void close_file(struct file_desc_t *fd) { assert(mounted != -1); close_ifile(fd); } void flush_file(struct file_desc_t *fd) { assert(mounted != -1); flush_ifile(fd); } void seek_file(struct file_desc_t *fd, int offset) { assert(mounted != -1); seek_ifile(fd, offset); } void seek2_file(struct file_desc_t *fd, int offset) { assert(mounted != -1); seek2_ifile(fd, offset); } int readc_file(struct file_desc_t *fd) { assert(mounted != -1); return readc_ifile(fd); } int writec_file(struct file_desc_t *fd, char c) { assert(mounted != -1); /* FIXME: Vérifier la place restante */ return writec_ifile(fd, c); } int read_file(struct file_desc_t *fd, void *buf, unsigned int nbyte) { assert(mounted != -1); if ( fd->cur_pos == fd->filesize ) return READ_EOF; return read_ifile(fd, buf, nbyte); } int write_file(struct file_desc_t *fd, const void *buf, unsigned int nbyte) { assert(mounted != -1); /* FIXME: Vérifier la place restante */ return write_ifile(fd, buf, nbyte); }