#include #include #include #include #include "superbloc.h" #include "file.h" #include "ifile.h" #include "inode.h" #include "entry.h" int new_entry(struct file_desc_t *fd) { assert(fd->type == FILET_DIR); struct entry_t ent; /* Revenir au début */ seek2_ifile(fd, 0); /* Parcourir toutes les entrées */ int num=0; while ( read_ifile(fd, &ent, sizeof(struct entry_t)) ) { /* Emplacement trouvé */ if ( ent.ent_inumber == 0 ) return num; num++; } /* Créer une nouvelle entrée */ memset(&ent, 0, sizeof(struct entry_t)); write_ifile(fd, &ent, sizeof(struct entry_t)); return num; } inode_t find_entry(struct file_desc_t *fd, const char *basename) { assert(fd->type == FILET_DIR); struct entry_t ent; /* Revenir au début */ seek2_ifile(fd, 0); /* Parcourir toutes les entrées */ while ( read_ifile(fd, &ent, sizeof(struct entry_t)) ) { /* Emplacement trouvé */ if ( strcmp(ent.ent_basename, basename) == 0 ) return ent.ent_inumber; } return 0; } int add_entry(struct file_desc_t *dirfd, inode_t inode_no, const char *basename) { assert(strlen(basename) < ENT_LEN); int no = new_entry(dirfd); if ( no < 0 ) return -1; struct entry_t ent; ent.ent_inumber = inode_no; strcpy(ent.ent_basename, basename); seek2_ifile(dirfd, no*sizeof(struct entry_t)); write_ifile(dirfd, &ent, sizeof(struct entry_t)); return 0; } void del_entry(struct file_desc_t *dirfd, inode_t inode_no) { assert(dirfd->type == FILET_DIR); struct entry_t ent; /* Revenir au début */ seek2_ifile(dirfd, 0); /* Parcourir toutes les entrées */ while ( read_ifile(dirfd, &ent, sizeof(struct entry_t)) ) { /* Emplacement trouvé */ if ( ent.ent_inumber == inode_no ) { memset(&ent, 0, sizeof(struct entry_t)); seek_ifile(dirfd, -sizeof(struct entry_t)); write_ifile(dirfd, &ent, sizeof(struct entry_t)); break; } } } inode_t inumber_of_basename(unsigned int volume, inode_t idir, const char *basename) { struct file_desc_t fd; open_ifile(volume, idir, &fd); inode_t no = find_entry(&fd, basename); close_ifile(&fd); return no; } inode_t inumber_of_path(unsigned int volume, const char *pathname) { assert(strlen(pathname) && pathname[0] == '/'); struct superblock_desc_t sblock; char *tmp = strdup(pathname); char *s, *e; load_super(volume, &sblock); inode_t no=sblock.first_inode; /* pour '/' */ if ( strlen(pathname) == 1 ) return sblock.first_inode; do { s = strchr(pathname, '/'); e = strchr(s+1, '/'); /* Plusieurs '/' consécutifs */ if ( e-s == 1 ) { pathname++; continue; } if ( e == NULL ) strcpy(tmp, s+1); else { strncpy(tmp, s+1, (e-s)-1); tmp[(e-s)-1] = '\0'; } no = inumber_of_basename(volume, no, tmp); if ( no == 0 ) { free(tmp); return 0; } pathname = s+1; } while ( e != NULL ); free(tmp); return no; } inode_t dinumber_of_path(unsigned int volume, const char *pathname, const char **basename) { assert(strlen(pathname) && pathname[0] == '/'); *basename = strrchr(pathname, '/')+1; return inumber_of_path(volume, pathname); }