int delete_inode(unsigned int inumber) { struct inode_s inode; read_inode(inumber, &inode); free_blocs(inode.blocs_directs, NB_DIRECTS); unsigned int bloc[NB_BLOCS_BY_BLOC]; /* A voir si utile */ int i; if(inode.blocs_indirect) { read_bloc(current_volume, inumber, (unsigned char *)inode.blocs_indirect); free_blocs(inode.blocs_directs, NB_BLOCS_BY_BLOC); free_bloc(inode.blocs_indirect); } if(inode.blocs_double_indirect) { read_bloc(current_volume, inumber, (unsigned char *)inode.blocs_double_indirect); for(i = 0; i < NB_BLOCS_BY_BLOC; i++) { unsigned int tmp[NB_BLOCS_BY_BLOC]; read_bloc(current_volume, inode.blocs_double_indirect, (unsigned char *)tmp); free_blocs(tmp, NB_BLOCS_BY_BLOC); free_bloc(inode.blocs_double_indirect); } free_blocs(bloc, NB_BLOCS_BY_BLOC); } free_bloc(inode.blocs_double_indirect); free_bloc(inumber); return inumber; }
int open_ifile(file_desc_t *fd, unsigned int inumber) { unsigned int first_bloc; struct inode_s inode; /* we are opening the designed file! */ fd->fds_inumber = inumber; read_inode (inumber, &inode); /* other trivial init */ fd->fds_size = inode.ind_size; fd->fds_pos = 0; /* the buffer is full of zeros if the first bloc is zero, loaded with this first bloc otherwise */ first_bloc = vbloc_of_fbloc(inumber, 0, FALSE); if (! first_bloc) memset(fd->fds_buf, 0, DATA_BLOC_SIZE); else read_bloc(current_volume, first_bloc, fd->fds_buf); /* last trivial */ fd->fds_dirty = FALSE; return RETURN_SUCCESS; }
/* move the cursor of offset positions. */ void seek_ifile(file_desc_t *fd, int offset) { unsigned int old_pos = fd->fds_pos; unsigned int fbloc, vbloc; /* update the position */ fd->fds_pos += offset; /* does the seek imply a jump in another bloc? */ if (bloc_of_pos(fd->fds_pos) != bloc_of_pos(old_pos)) { /* flush */ flush_ifile(fd); /* the bloc index of the new buffer */ fbloc = bloc_of_pos(fd->fds_pos); vbloc = vbloc_of_fbloc(fd->fds_inumber, fbloc, FALSE); if (! vbloc) /* the bloc #0 is full of zeros */ memset(fd->fds_buf, 0, BLOC_SIZE); else /* load the bloc */ read_bloc(current_volume, vbloc, fd->fds_buf); } }
void read_inode(unsigned int inumber, struct inode_s* inode) { unsigned char buffer[SECTOR_SIZE]; PRINT_ASSERT_ERROR_MSG(inumber != BLOC_NULL, "Null inode (read_inode)"); read_bloc(current_vol, inumber, buffer); memcpy(inode, buffer, sizeof(struct inode_s)); }
unsigned initialize_bloc(unsigned address){ int new_bloc[NBPB]; int i; read_bloc(current_volume, address, (unsigned char*)new_bloc); for (i = 0; i < NBPB; i++){ new_bloc[i] = 0; } write_bloc(current_volume, address, (unsigned char*)new_bloc); return address; }
int delete_inode(unsigned int inumber) { struct inode_s inode; unsigned int buffer_indirect_1[SECTOR_SIZE]; unsigned int buffer_indirect_2[SECTOR_SIZE]; unsigned int i; PRINT_ASSERT_ERROR_MSG(inumber != BLOC_NULL, "Null inode (delete_inode)"); read_inode(inumber, &inode); /* Direct */ free_bloc_array(inode.inode_direct_bloc, NB_DIRECT_BLOC); /* Indirect 1 */ if(inode.inode_indirect_1 != BLOC_NULL) { read_bloc(current_vol, inode.inode_indirect_1, (unsigned char*)buffer_indirect_1); free_bloc_array(buffer_indirect_1, NB_INDIRECT_BLOC); free_bloc(inode.inode_indirect_1); } /* Indirect 2 */ if(inode.inode_indirect_2 != BLOC_NULL) { read_bloc(current_vol, inode.inode_indirect_2, (unsigned char*)buffer_indirect_2); for(i=0; i<NB_INDIRECT_BLOC; i++) { if(buffer_indirect_2[i] != BLOC_NULL) { read_bloc(current_vol, buffer_indirect_2[i], (unsigned char*)buffer_indirect_1); free_bloc_array(buffer_indirect_1, NB_INDIRECT_BLOC); free_bloc(buffer_indirect_2[i]); } } free_bloc(inode.inode_indirect_2); } free_bloc(inumber); return RETURN_SUCCESS; }
/* return the pos in the file ; RETURN_FAILURE in case of error */ int writec_ifile(file_desc_t *fd, char c) { unsigned int ibloc; /* write the char in the buffer */ fd->fds_buf[ibloc_of_pos(fd->fds_pos)] = c; /* first write in the bloc ? ensure the data bloc allocation */ if (! fd->fds_dirty) { ibloc = vbloc_of_fbloc(fd->fds_inumber, bloc_of_pos(fd->fds_pos), TRUE); if (! ibloc) { return RETURN_FAILURE; } fd->fds_dirty = TRUE; } /* is the buffer full? */ if (ibloc_of_pos(fd->fds_pos) == BLOC_SIZE-1) { /* write the buffer */ ibloc = vbloc_of_fbloc(fd->fds_inumber, bloc_of_pos(fd->fds_pos), FALSE); write_bloc(current_vol, ibloc, fd->fds_buf); /* read the new buffer */ ibloc = vbloc_of_fbloc(fd->fds_inumber, bloc_of_pos(fd->fds_pos+1), FALSE); if (! ibloc) memset(fd->fds_buf, 0, BLOC_SIZE); else read_bloc(current_vol, ibloc, fd->fds_buf); fd->fds_dirty = FALSE; } /* update the file cursor and size */ if (fd->fds_size < fd->fds_pos) fd->fds_size = fd->fds_pos; fd->fds_pos++; /* the position of the written char */ return fd->fds_pos - 1; }
unsigned int new_bloc(){ struct freeBloc_s *fb; unsigned char buffer[HDA_SECTORSIZE]; unsigned res; if(current_super.super_nb_free==0) return 0; assert(current_super.super_first_free_bloc); read_bloc(current_vol, current_super.super_first_free_bloc, buffer); fb = (struct freeBloc_s*) buffer; res = current_super.super_first_free_bloc; if(fb->freebloc_nb_free > 1){ current_super.super_nb_free --; fb->freebloc_nb_free--; write_bloc(current_vol, current_super.super_first_free_bloc, buffer); } else { current_super.super_first_free_bloc = fb->freebloc_next; } //current_super.super_first_free_bloc--; save_super(); return res; }
// Recopié depuis le cahier, les noms sont pas forcément les bons unsigned int vbloc_of_fbloc(unsigned int inumber, unsigned int fbloc,bool_t do_allocate){ struct inode_s inode; unsigned int bloc_index = fbloc; read_inode(inumber, &inode); //direct if (bloc_index < NDIRECT){ if (inode.direct[bloc_index] == 0){ if(do_allocate){ inode.direct[bloc_index] = initialize_bloc(new_bloc()); write_inode(inumber,&inode); }else{return BLOC_NULL;} } return inode.direct[bloc_index]; } bloc_index -= NDIRECT; //indirect simple if (bloc_index < NBPB){ // if the indirect entry in the inode is not allocated yet if (inode.direct == 0){ if(do_allocate){ inode.indirect = initialize_bloc(new_bloc());; write_inode(inumber,&inode); }else{return BLOC_NULL;} } int indirect[NBPB]; read_bloc(current_volume, inode.indirect, (unsigned char *)indirect); if (indirect[bloc_index] == 0){ if(do_allocate){ indirect[bloc_index] = initialize_bloc(new_bloc());; write_bloc(current_volume, inode.indirect, (unsigned char*)indirect); }else{return BLOC_NULL;} } return indirect[bloc_index]; } bloc_index -= NBPB; //indirect double if(bloc_index < NBPB*NBPB){ if (inode.two_indirect == 0){ if(do_allocate){ inode.two_indirect = initialize_bloc(new_bloc());; write_inode(inumber,&inode); }else{return BLOC_NULL;} } int db_indirect_index = bloc_index / NBPB; int indirect_index = bloc_index % NBPB; int db_indirect[NBPB]; read_bloc(current_volume, inode.two_indirect, (unsigned char*)db_indirect); if (db_indirect[db_indirect_index] == 0){ if(do_allocate){ db_indirect[db_indirect_index] = initialize_bloc(new_bloc()); write_bloc(current_volume, inode.two_indirect, (unsigned char*)db_indirect); }else{return BLOC_NULL;} } int indirect[NBPB]; read_bloc(current_volume, db_indirect[db_indirect_index], (unsigned char*)indirect); if (indirect[indirect_index] == 0){ if(do_allocate){ //printf("allocate indirect[%d]\n",indirect_index); indirect[indirect_index] = initialize_bloc(new_bloc());; write_bloc(current_volume, db_indirect[db_indirect_index], (unsigned char*)indirect); }else{return BLOC_NULL;} } return indirect[indirect_index]; } //fprintf(stderr,"fbloc is too big.\n\tfbloc provided: %d\n\tfbloc max size: %d",fbloc, NDIRECT+NBPB+NBPB*NBPB); return -1; }
void read_inode (unsigned int inumber, struct inode_s* inode) { assert(BLOC_SIZE == sizeof(struct inode_s)); read_bloc(current_volume, inumber, (unsigned char *)inode); }
unsigned int vbloc_of_fbloc(unsigned int inumber, unsigned int fbloc, bool_t do_allocate) { struct inode_s inode; unsigned int buffer_indirect[SECTOR_SIZE]; unsigned int buffer_indirect2[SECTOR_SIZE]; PRINT_ASSERT_ERROR_MSG(inumber != BLOC_NULL, "Null inode (vbloc_of_fbloc)"); read_inode(inumber, &inode); if(fbloc < NB_DIRECT_BLOC) { /* Direct */ if(inode.inode_direct_bloc[fbloc] == BLOC_NULL) { if(do_allocate) { inode.inode_direct_bloc[fbloc] = new_bloc(); write_inode(inumber, &inode); } else { return BLOC_NULL; } } return inode.inode_direct_bloc[fbloc]; } else if(fbloc < NB_INDIRECT_BLOC + NB_DIRECT_BLOC) { /* Indirect 1 */ fbloc -= NB_DIRECT_BLOC; unsigned int vbloc; /* Level 0 */ if(inode.inode_indirect_1 == BLOC_NULL) { if(do_allocate) { inode.inode_indirect_1 = new_bloc(); if(inode.inode_indirect_1 == BLOC_NULL) { return BLOC_NULL; } /* set 0 to new bloc */ memset(buffer_indirect, 0, SECTOR_SIZE); write_bloc(current_vol, inode.inode_indirect_1, (unsigned char*)buffer_indirect); write_inode(inumber, &inode); } else { return BLOC_NULL; } } /* Level 1 */ read_bloc(current_vol, inode.inode_indirect_1, (unsigned char*)buffer_indirect); vbloc = buffer_indirect[fbloc]; if(vbloc == BLOC_NULL) { if(do_allocate) { buffer_indirect[fbloc] = new_bloc(); if(buffer_indirect[fbloc] == BLOC_NULL) { return BLOC_NULL; } write_bloc(current_vol, inode.inode_indirect_1, (unsigned char*)buffer_indirect); vbloc = buffer_indirect[fbloc]; } else { return BLOC_NULL; } } return vbloc; } else if (fbloc < NB_INDIRECT_BLOC + NB_DIRECT_BLOC + NB_INDIRECT_BLOC*NB_INDIRECT_BLOC) { /* Indirect 2 */ unsigned int vbloc_1, vbloc_2; fbloc -= NB_DIRECT_BLOC+NB_INDIRECT_BLOC; /* Level 0 */ if(inode.inode_indirect_2 == BLOC_NULL) { if(do_allocate) { inode.inode_indirect_2 = new_bloc(); if(inode.inode_indirect_2 == BLOC_NULL) { return BLOC_NULL; } /* set 0 to new bloc */ memset(buffer_indirect, 0, SECTOR_SIZE); write_bloc(current_vol, inode.inode_indirect_2, (unsigned char*)buffer_indirect); write_inode(inumber, &inode); } else { return BLOC_NULL; } } /* Level 1 */ read_bloc(current_vol, inode.inode_indirect_2, (unsigned char*)buffer_indirect); vbloc_1 = buffer_indirect[fbloc/NB_INDIRECT_BLOC]; if(vbloc_1 == BLOC_NULL) { if(do_allocate) { buffer_indirect[fbloc/NB_INDIRECT_BLOC] = new_bloc(); if(buffer_indirect[fbloc/NB_INDIRECT_BLOC] == BLOC_NULL) { return BLOC_NULL; }; /* set 0 to new bloc */ memset(buffer_indirect2, 0, SECTOR_SIZE); write_bloc(current_vol, buffer_indirect[fbloc/NB_INDIRECT_BLOC], (unsigned char*)buffer_indirect2); write_bloc(current_vol, inode.inode_indirect_2, (unsigned char*)buffer_indirect); vbloc_1 = buffer_indirect[fbloc/NB_INDIRECT_BLOC]; } else { return BLOC_NULL; } } /* Level 2 */ read_bloc(current_vol, vbloc_1, (unsigned char*)buffer_indirect); vbloc_2 = buffer_indirect[fbloc%NB_INDIRECT_BLOC]; if(vbloc_2 == BLOC_NULL) { if(do_allocate) { buffer_indirect[fbloc%NB_INDIRECT_BLOC] = new_bloc(); if(buffer_indirect[fbloc%NB_INDIRECT_BLOC] == BLOC_NULL) { return BLOC_NULL; } write_bloc(current_vol, vbloc_1, (unsigned char*)buffer_indirect); vbloc_2 = buffer_indirect[fbloc%NB_INDIRECT_BLOC]; } else { return BLOC_NULL; } } return vbloc_2; } else return BLOC_NULL; }