#include #include #include #include #include "superbloc.h" #include "sfile.h" inode_t create_sfile(unsigned int volume, enum file_type_e type) { struct inode_desc_t inode; struct superblock_desc_t sblock; /* Traiter le superbloc */ load_super(volume, &sblock); sblock.first_inode = new_block(); assert(sblock.free_space && "Aucun espace dispo sur le periphérique"); save_super(volume, &sblock); /* Créer l'inoeud */ inode = new_inode(volume, type); inode.size = 0; write_data_to_block(volume, sblock.first_inode, &inode, sizeof(struct inode_desc_t)); return sblock.first_inode; } int open_sfile(unsigned int volume, struct file_desc_t *fd) { struct superblock_desc_t sblock; struct inode_desc_t inode; /* Charger le superblock */ load_super(volume, &sblock); if ( sblock.first_inode == 0 ) return OPEN_FAILED; /* Lire l'inode */ read_data_from_block(volume, sblock.first_inode, &inode, sizeof(struct inode_desc_t)); fd->volume = volume; fd->inode_block = sblock.first_inode; fd->buffer = malloc(block_size()); memset(fd->buffer, 0, block_size()); fd->cur_pos = 0; fd->flags = 0; fd->filesize = inode.size; return OPEN_SUCCESS; } int delete_sfile(unsigned int volume) { struct superblock_desc_t sblock; struct inode_desc_t inode; /* Charger le superblock */ load_super(volume, &sblock); assert(sblock.first_inode && "Sfile inexistant!"); /* Lire l'inode */ read_data_from_block(volume, sblock.first_inode, &inode, sizeof(struct inode_desc_t) ); /* Libérer les blocs */ free_inode_blocks(inode); free_block(inode.volume, sblock.first_inode); /* Mettre à jour le superbloc */ sblock.first_inode = 0; save_super(volume, &sblock); return 0; /* FIXME */ } void close_sfile(struct file_desc_t *fd) { flush_sfile(fd); free(fd->buffer); /* Fermer */ } /* static uint32_t grow_file(struct file_desc_t *fd) */ /* { */ /* struct inode_desc_t inode; */ /* /\* lire l'inode *\/ */ /* read_data_from_block(fd->volume, */ /* fd->inode_block, */ /* &inode, */ /* sizeof(struct inode_desc_t) */ /* ); */ /* /\* TODO: supporter les doubles indirects *\/ */ /* uint32_t block = block_of_pos_alloc(&inode, fd->cur_pos); */ /* /\* Ecrire l'inode *\/ */ /* write_data_to_block(fd->volume, */ /* fd->inode_block, */ /* &inode, */ /* sizeof(struct inode_desc_t) */ /* ); */ /* return block; */ /* } */ void flush_sfile(struct file_desc_t *fd) { flush_ifile(fd); } /* void flush_file(struct file_desc_t *fd) */ /* { */ /* /\* Ecrire le bloc si modifié *\/ */ /* if ( fd->flags & BUFFER_MODIF ) */ /* { */ /* /\* Charger l'inode *\/ */ /* struct inode_desc_t inode; */ /* read_data_from_block(fd->volume, */ /* fd->inode_block, */ /* &inode, */ /* sizeof(struct inode_desc_t)); */ /* uint32_t block = block_of_pos(inode, fd->cur_pos); */ /* /\* Si il faut, faire grossir l'inode d'un bloc *\/ */ /* if ( block == 0 ) */ /* block = grow_file(fd); */ /* write_bloc(fd->volume, */ /* block, */ /* fd->buffer); */ /* fd->flags &= ~BUFFER_MODIF; */ /* } */ /* } */ /* relatif */ void seek_sfile(struct file_desc_t *fd, int r_offset) { seek2_sfile(fd, fd->cur_pos + r_offset); } /* absolu */ void seek2_sfile(struct file_desc_t *fd, unsigned int a_offset) { //assert(a_offset >= 0 && a_offset <= fd->filesize); /* Si on essaye d'aller trop loin */ /* if ( a_offset >= fd->filesize ) */ /* a_offset = fd->filesize - 1; */ assert(a_offset >= 0); unsigned int oldb = fd->cur_pos / block_size(); unsigned int newb = a_offset / block_size(); /* Si on change de bloc */ if ( oldb != newb || fd->filesize == 0 ) { //printf("changement de bloc!\n"); /* Si on a le flag modifié, écrire le bloc */ flush_sfile(fd); fd->cur_pos = a_offset; /* Lire l'inode */ struct inode_desc_t inode; read_data_from_block(fd->volume, fd->inode_block, &inode, sizeof(struct inode_desc_t)); uint32_t b = block_of_pos(inode, fd->cur_pos); printf("lire bloc : %d\n", b); /* Si le prochain bloc existe, le charger, sinon mettre le buffer à zéro */ if ( b != 0 ) { read_bloc(fd->volume, b, fd->buffer); } else memset(fd->buffer, 0, block_size()); } else fd->cur_pos = a_offset; } int readc_sfile(struct file_desc_t *fd) { if ( fd->cur_pos >= fd->filesize ) return READ_EOF; int c = ((char*)fd->buffer)[fd->cur_pos % block_size()]; seek_sfile(fd, 1); return c; } int writec_sfile(struct file_desc_t *fd, char c) { ((char*)fd->buffer)[fd->cur_pos % block_size()] = c; fd->flags |= BUFFER_MODIF; if ( (fd->cur_pos+1) > fd->filesize ) fd->filesize++; seek_sfile(fd, 1); return 1; } int read_sfile(struct file_desc_t *fd, void *buf, unsigned int nbyte) { int i; int count=0; for(i=0;i