Select Git revision
minix.h 5.35 KiB
#ifndef _MINIX_H_
#define _MINIX_H_
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#define MINIX_MAGIC 0x137F
#define MINIX_NAME_LEN 14
#define BLOCK_SIZE 1024
#define BITMAP_INODE_FIRST_IDX 2
#define BYTE_BITSIZE 8
struct __attribute__((packed)) minix_inode {
uint16_t i_mode; // file type and permissions for file
uint16_t i_uid; // user id
uint32_t i_size; // file size in bytes
uint32_t i_time; // inode time
uint8_t i_gid; // group id
uint8_t i_nlinks; // number of dir entry pointing to this inode
uint16_t i_zone[7]; // direct pointers
uint16_t i_indir_zone; // indirect pointer
uint16_t i_dbl_indir_zone; // double indirect pointer
};
struct __attribute__((packed)) minix_dir_entry {
uint16_t inode;
char name[MINIX_NAME_LEN];
};
struct __attribute__((packed)) minix_super_block {
uint16_t s_ninodes; // total number of inodes
uint16_t s_nzones; // total number of blocks (including superblock)
uint16_t s_imap_blocks; // inodes bitmap size in blocks
uint16_t s_zmap_blocks; // data blocks bitmap size in blocks
uint16_t s_firstdatazone; // index of first data block
uint16_t s_log_zone_size; // block size in bytes = 1024 * 2^s_log_zone_size
uint32_t s_max_size; // max file size in bytes
uint16_t s_magic; // 0x137f = v1 with 14 characters dir_entry
uint16_t s_state; // was the FS properly unmounted ?
};
/**
* @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 Function that translates a logical block value to a physical block
* value
*
* @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 Function that reads the contents of a block of data into the char *buf
* buffer
*
* @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 *img_filepath,
const size_t phys_num_block);
/**
* @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 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 Function that reads the content of a given file in the FS image
*
* @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);
#endif // !_MINIX_H_