#include #include #include #include #include #include "inode.h" #include "file.h" 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) ); uint32_t block = block_of_pos_alloc(&inode, fd->cur_pos, fd->filesize); /* Ecrire l'inode */ write_data_to_block(fd->volume, fd->inode_block, &inode, sizeof(struct inode_desc_t) ); return block; } void flush_ifile(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); /* Mettre à jour la taille de l'inode */ read_data_from_block(fd->volume, fd->inode_block, &inode, sizeof(struct inode_desc_t) ); inode.size = fd->filesize; write_data_to_block(fd->volume, fd->inode_block, &inode, sizeof(struct inode_desc_t) ); /* Ecrire le buffer */ //printf("Write data to block %u\n", block); write_bloc(fd->volume, block, fd->buffer); fd->flags &= ~BUFFER_MODIF; } } /* relatif */ void seek_ifile(struct file_desc_t *fd, int r_offset) { seek2_ifile(fd, fd->cur_pos + r_offset); } /* absolu */ void seek2_ifile(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 ) { //printf("changement de bloc!\n"); /* Si on a le flag modifié, écrire le bloc */ flush_ifile(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_ifile(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_ifile(fd, 1); return c; } int writec_ifile(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_ifile(fd, 1); return 1; } int read_ifile(struct file_desc_t *fd, void *buf, unsigned int nbyte) { int i; int count=0; for(i=0;ibuffer); }