diff --git a/systeme_fichiers/libminix/inode_info.c b/systeme_fichiers/libminix/inode_info.c index b2bdf911cb7563386cf4095fdca87267b7ae4ab0..b98c32273a288e789461b51f77a6b657ba25ad0e 100644 --- a/systeme_fichiers/libminix/inode_info.c +++ b/systeme_fichiers/libminix/inode_info.c @@ -13,7 +13,7 @@ int main(int argc, char *argv[]) { struct minix_inode inode = {0}; int inode_nb = atoi(argv[2]); - if (marshal_inode(&inode, inode_nb, argv[1]) == -1) { + if (unmarshal_inode(&inode, inode_nb, argv[1]) == -1) { fprintf(stdout, "marshal_inode has failed\n"); exit(EXIT_FAILURE); } diff --git a/systeme_fichiers/libminix/list_inodes.c b/systeme_fichiers/libminix/list_inodes.c index a628c0b698f4cdf136dbe40e1607090c26394a74..3b075104df1cc7a689ca07985b9baacbd04972b6 100644 --- a/systeme_fichiers/libminix/list_inodes.c +++ b/systeme_fichiers/libminix/list_inodes.c @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) { struct minix_super_block sb = {0}; - if (marshal_sb(&sb, argv[1]) == -1) { + if (unmarshal_sb(&sb, argv[1]) == -1) { fprintf(stderr, "Failed to marshal minix_super_block structure\n"); fprintf(stderr, "Exiting...\n"); exit(EXIT_FAILURE); @@ -24,7 +24,7 @@ int main(int argc, char *argv[]) { if (is_inode_allocated(i, argv[1])) { nb_allocated_inodes++; struct minix_inode inode = {0}; - if (marshal_inode(&inode, i, argv[1]) == -1) { + if (unmarshal_inode(&inode, i, argv[1]) == -1) { fprintf(stderr, "Failed to marshal minix_inode structure\n"); fprintf(stderr, "Exiting...\n"); exit(EXIT_FAILURE); diff --git a/systeme_fichiers/libminix/read_dir.c b/systeme_fichiers/libminix/read_dir.c index 769f55cdf9818eb546ba6f4261807f0bfbb8c504..5700d019a5603207335d53d0a4259c43b3bc8a8f 100644 --- a/systeme_fichiers/libminix/read_dir.c +++ b/systeme_fichiers/libminix/read_dir.c @@ -14,7 +14,7 @@ int main(int argc, char *argv[]) { struct minix_inode inode = {0}; int inode_nb = atoi(argv[2]); - marshal_inode(&inode, inode_nb, argv[1]); + unmarshal_inode(&inode, inode_nb, argv[1]); int nb_blocks_used = inode.i_size / BLOCK_SIZE; if (inode.i_size % BLOCK_SIZE != 0) { diff --git a/systeme_fichiers/libminix/read_file.c b/systeme_fichiers/libminix/read_file.c index dc15963f4227f924a776ef0abeb2022c6d55c50b..100d10b9b9f6ba03903de36aac4404f4fa32a57e 100644 --- a/systeme_fichiers/libminix/read_file.c +++ b/systeme_fichiers/libminix/read_file.c @@ -13,7 +13,7 @@ int main(int argc, char *argv[]) { struct minix_inode inode = {0}; int inode_nb = atoi(argv[2]); - marshal_inode(&inode, inode_nb, argv[1]); + unmarshal_inode(&inode, inode_nb, argv[1]); int nb_blocks_used = inode.i_size / BLOCK_SIZE; if (inode.i_size % BLOCK_SIZE != 0) { diff --git a/systeme_fichiers/libminix/sb_info.c b/systeme_fichiers/libminix/sb_info.c index 8cc5fbf3fd7abf81b656c6d1a6a419c0fbeae98e..f4a32df9745f99bc3bc6724027ddb45d999fbd25 100644 --- a/systeme_fichiers/libminix/sb_info.c +++ b/systeme_fichiers/libminix/sb_info.c @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) { struct minix_super_block m_sb = {0}; - marshal_sb(&m_sb, argv[1]); + unmarshal_sb(&m_sb, argv[1]); read_sb(&m_sb); return EXIT_SUCCESS; diff --git a/systeme_fichiers/libminix/struct/minix.c b/systeme_fichiers/libminix/struct/minix.c index 3875f1c34a53777dad7b262bf5d808581e922226..09ab07f64035ad255dbd6d4847a0139e5087386f 100644 --- a/systeme_fichiers/libminix/struct/minix.c +++ b/systeme_fichiers/libminix/struct/minix.c @@ -6,7 +6,7 @@ #include <sys/stat.h> #include <unistd.h> -int marshal_sb(struct minix_super_block *sb, const char *img_filepath) { +int unmarshal_sb(struct minix_super_block *sb, const char *img_filepath) { int fd = open(img_filepath, O_RDONLY); if (fd == -1) { @@ -23,7 +23,7 @@ int marshal_sb(struct minix_super_block *sb, const char *img_filepath) { return -1; } - if (sb->s_magic != MAGIC) { + if (sb->s_magic != MINIX_MAGIC) { fprintf(stderr, "The provided image isn't a minix-FS image whose magic " "value is 0x137F\n"); close(fd); @@ -34,44 +34,11 @@ int marshal_sb(struct minix_super_block *sb, const char *img_filepath) { return 0; } -bool is_inode_allocated(const uint32_t inode_nb, const char *img_filepath) { - int fd = open(img_filepath, O_RDONLY); - if (fd == -1) { - perror("open"); - close(fd); - return -1; - } - - char buf[BLOCK_SIZE] = {0}; - // int inode_bitmap_block = inode_nb / (BLOCK_SIZE * BYTE_BITSIZE); - - lseek(fd, BLOCK_SIZE * BITMAP_INODE_FIRST_IDX, SEEK_SET); - - // Checking if we've read an entire block or not - ssize_t bytes_read = read(fd, buf, BLOCK_SIZE); - if (bytes_read == -1) { - perror("read"); - close(fd); - return false; - } - - if (bytes_read == BLOCK_SIZE) { - uint16_t byte_idx = inode_nb / BYTE_BITSIZE; - uint8_t bit_idx = inode_nb % BYTE_BITSIZE; - - close(fd); - return buf[byte_idx] & (0x01 << bit_idx); - } - - close(fd); - return false; -} - -int marshal_inode(struct minix_inode *inode, const uint32_t inode_nb, - const char *img_filepath) { +int unmarshal_inode(struct minix_inode *inode, const uint32_t inode_nb, + const char *img_filepath) { struct minix_super_block sb = {0}; - if (marshal_sb(&sb, img_filepath) == -1) { + if (unmarshal_sb(&sb, img_filepath) == -1) { perror("marshal_sb"); return -1; } @@ -111,7 +78,50 @@ int marshal_inode(struct minix_inode *inode, const uint32_t inode_nb, return 0; } +bool is_inode_allocated(const uint32_t inode_nb, const char *img_filepath) { + int fd = open(img_filepath, O_RDONLY); + + if (fd == -1) { + perror("open"); + close(fd); + return -1; + } + + // Computes in which block of the inode's bitmap is the bit associated + // to the given inode number + int inode_bitmap_block = (inode_nb / (BLOCK_SIZE * BYTE_BITSIZE)) + 1; + + char buf[BLOCK_SIZE] = {0}; + + lseek(fd, BLOCK_SIZE * BITMAP_INODE_FIRST_IDX * inode_bitmap_block, + SEEK_SET); + + // Checking if we've read an entire block or not + ssize_t bytes_read = read(fd, buf, BLOCK_SIZE); + if (bytes_read == -1) { + perror("read"); + close(fd); + return false; + } + + if (bytes_read == BLOCK_SIZE) { + uint16_t byte_idx = inode_nb / BYTE_BITSIZE; + uint8_t bit_idx = inode_nb % BYTE_BITSIZE; + + close(fd); + return buf[byte_idx] & (0x01 << bit_idx); + } + + close(fd); + return false; +} + int read_sb(const struct minix_super_block *sb) { + if (sb->s_magic != MINIX_MAGIC) { + fprintf(stdout, "Wrong magic value\n"); + return -1; + } + uint16_t inode_table_size = sb->s_ninodes / sizeof(struct minix_inode); uint16_t idx_first_block_inode_table = sb->s_firstdatazone - inode_table_size; @@ -248,13 +258,17 @@ int read_indir_block(uint16_t *buf, const char *img_filepath, ssize_t bmap(const struct minix_inode *inode, const char *img_filepath, size_t logical_block) { - if (logical_block < sizeof(inode->i_zone) / sizeof(uint16_t)) { + + size_t direct_zone_len = sizeof(inode->i_zone) / sizeof(inode->i_zone[0]); + size_t ptr_by_block = BLOCK_SIZE / sizeof(inode->i_indir_zone); + + if (logical_block < direct_zone_len) { return inode->i_zone[logical_block]; } - logical_block = logical_block - sizeof(inode->i_zone) / sizeof(uint16_t); + logical_block -= direct_zone_len; - if (logical_block < BLOCK_SIZE / sizeof(uint16_t)) { + if (logical_block < ptr_by_block) { uint16_t buf[BLOCK_SIZE] = {0}; if (read_indir_block(buf, img_filepath, inode->i_indir_zone) == -1) { @@ -265,10 +279,9 @@ ssize_t bmap(const struct minix_inode *inode, const char *img_filepath, return buf[logical_block]; } - logical_block = logical_block - BLOCK_SIZE / sizeof(uint16_t); + logical_block -= ptr_by_block; - if (logical_block < - BLOCK_SIZE / sizeof(uint16_t) * BLOCK_SIZE / sizeof(uint16_t)) { + if (logical_block < (ptr_by_block * ptr_by_block)) { uint16_t buf1[BLOCK_SIZE] = {0}; uint16_t buf2[BLOCK_SIZE] = {0}; @@ -278,9 +291,8 @@ ssize_t bmap(const struct minix_inode *inode, const char *img_filepath, return -1; } - if (read_indir_block( - buf2, img_filepath, - buf1[logical_block / BLOCK_SIZE * sizeof(uint16_t)]) == -1) { + if (read_indir_block(buf2, img_filepath, + buf1[logical_block / ptr_by_block]) == -1) { fprintf(stderr, "read_indir_block failed\n"); return -1; } @@ -291,8 +303,9 @@ ssize_t bmap(const struct minix_inode *inode, const char *img_filepath, return 0; } -int read_data_block(char *buf, const char *filepath, size_t phys_num_block) { - int fd = open(filepath, O_RDONLY); +int read_data_block(char *buf, const char *img_filepath, + size_t phys_num_block) { + int fd = open(img_filepath, O_RDONLY); if (fd == -1) { perror("open"); @@ -358,7 +371,7 @@ int lookup_entry(const struct minix_inode *inode, const char *token, int namei(const char *img_filepath, char *filepath) { struct minix_inode inode = {0}; - if (marshal_inode(&inode, 1, img_filepath) == -1) { + if (unmarshal_inode(&inode, 1, img_filepath) == -1) { fprintf(stderr, "marshal_inode has failed\n"); return -1; } @@ -381,7 +394,7 @@ int namei(const char *img_filepath, char *filepath) { token = strtok(NULL, "/"); memset(&inode, 0, sizeof(struct minix_inode)); - if (marshal_inode(&inode, next_inode, img_filepath) == -1) { + if (unmarshal_inode(&inode, next_inode, img_filepath) == -1) { fprintf(stderr, "marshal_inode has failed\n"); return -1; } diff --git a/systeme_fichiers/libminix/struct/minix.h b/systeme_fichiers/libminix/struct/minix.h index bcff347a28106c7d1eb5b5768210e2a653b455c6..ac0c580adbae3e281cc90142faa6e0ad3dcc6486 100644 --- a/systeme_fichiers/libminix/struct/minix.h +++ b/systeme_fichiers/libminix/struct/minix.h @@ -5,16 +5,10 @@ #include <stdint.h> #include <stdio.h> -// Superblock magic 0x137f -#define MAGIC 0x137F +#define MINIX_MAGIC 0x137F #define MINIX_NAME_LEN 14 -// 1KB block size #define BLOCK_SIZE 1024 - #define BITMAP_INODE_FIRST_IDX 2 - -#define IS_BIT_SET(value, pos) ((value >> pos) & 0x1) - #define BYTE_BITSIZE 8 struct __attribute__((packed)) minix_inode { @@ -46,54 +40,111 @@ struct __attribute__((packed)) minix_super_block { uint16_t s_state; // was the FS properly unmounted ? }; -extern int marshal_sb(struct minix_super_block *sb, const char *img_filepath); -extern int marshal_inode(struct minix_inode *inode, const uint32_t inode_nb, - const char *img_filepath); +/** + * @brief Function that deserializes a byte stream into a minix_super_block + * structure + * + * @param sb MINIX Superblock structure to populate + * @param img_filepath Path to the FS image + * @return -1 in case of an error, 0 otherwise + */ +extern int unmarshal_sb(struct minix_super_block *sb, const char *img_filepath); + +/** + * @brief Function that deserializes a byte stream into a minix_inode structure + * + * @param inode MINIX Inode structure to populate + * @param inode_nb Number of the inode + * @param img_filepath Path to the FS image + * @return -1 in case of an error, 0 otherwise + */ +extern int unmarshal_inode(struct minix_inode *inode, const uint32_t inode_nb, + const char *img_filepath); + +/** + * @brief Function that determines if an inode is allocated or not based on the + * value of its corresponding bit in the inode's bitmap + * + * @param inode_nb Number of the inode + * @param img_filepath Path to the FS image + * @return true if inode is allocated, false otherwise + */ extern bool is_inode_allocated(const uint32_t inode_nb, const char *img_filepath); + +/** + * @brief Function that displays the contents the minix_super_block structure + * + * @param sb MINIX Superblock structure to display + * @return -1 in case of an error, 0 otherwise + */ extern int read_sb(const struct minix_super_block *sb); + +/** + * @brief Function that displays the contents of a minix_inode structure + * + * @param inode MINIX Inode structure to display + * @return -1 in case of an error, 0 otherwise + */ extern int read_inode(const struct minix_inode *inode); + +/** + * @brief Function that reads the contents of indirect block (aka one that + * contains pointers to other blocks of data) + * + * @param buf Allocated buffer of size BLOCKSIZE = 1024 whose entries are + * pointers to blocks of data + * @param img_filepath Path to the FS image + * @param block_number Block number that is going to be read + * @return -1 in case of an error, 0 otherwise + */ extern int read_indir_block(uint16_t *buf, const char *img_filepath, const size_t block_number); + /** - * @brief Returns physical number of data block based on its logical - * representation + * @brief Function that translates a logical block value to a physical block + * value * - * @param inode - * @param filepath - * @param logical_block_idx - * @return + * @param inode MINIX Inode structure + * @param img_filepath Path to the FS image + * @param logical_block Value of the logical block to translated + * @return -1 in case of an error, number of the physical block otherwise */ extern ssize_t bmap(const struct minix_inode *inode, const char *img_filepath, size_t logical_block_idx); /** - * @brief + * @brief Function that reads the contents of a block of data into the char *buf + * buffer * - * @param buf Buffer should allocated in such way that it accounts for a '\0' - * being placed at the end of it - * @param filepath - * @param phys_num_block - * @return + * @param buf Allocated buffer of size BLOCKSIZE = 1024 + 1 for the '\0' + * character + * @param img_filepath Path to the FS image + * @param phys_num_block Number of the physical data block to read + * @return -1 in case of an error, 0 otherwise */ -extern int read_data_block(char *buf, const char *filepath, +extern int read_data_block(char *buf, const char *img_filepath, const size_t phys_num_block); /** - * @brief + * @brief Function that looks up the entries in a given inode and looks for an + * entry whose name matches the value the const char *token string. If it + * matches the name then the value of the corresponding inode shall be returned * - * @param dir_entry - * @param token - * @return -1 in case of an error or if entry hasn't been found + * @param inode MINIX Inode structure + * @param token Token that will be searched for in the given inode + * @param img_filepath + * @return -1 in case of an error, value of the corresponding inode otherwise */ extern int lookup_entry(const struct minix_inode *inode, const char *token, const char *img_filepath); /** - * @brief Finding file's inode based on the absolute filepath + * @brief Function that reads the content of a given file in the FS image * - * @param filepath - * @return + * @param img_filepath Path to the FS image + * @param filepath Absolute path to the file in the FS image + * @return -1 in case of an error, 0 otherwise */ extern int namei(const char *img_filepath, char *filepath);