Select Git revision
minix.c 11.23 KiB
#include "minix.h"
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
int unmarshal_sb(struct minix_super_block *sb, const char *img_filepath) {
int fd = open(img_filepath, O_RDONLY);
if (fd == -1) {
perror("open");
close(fd);
return -1;
}
lseek(fd, BLOCK_SIZE, SEEK_SET);
if (read(fd, sb, sizeof(struct minix_super_block)) == -1) {
perror("read");
close(fd);
return -1;
}
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);
return -1;
}
close(fd);
return 0;
}
int unmarshal_inode(struct minix_inode *inode, const uint32_t inode_nb,
const char *img_filepath) {
struct minix_super_block sb = {0};
if (unmarshal_sb(&sb, img_filepath) == -1) {
perror("marshal_sb");
return -1;
}
int fd = open(img_filepath, O_RDONLY);
if (fd == -1) {
perror("open");
close(fd);
return -1;
}
lseek(fd, BLOCK_SIZE * BITMAP_INODE_FIRST_IDX, SEEK_SET);
if (inode_nb == 0 || inode_nb > sb.s_ninodes ||
!is_inode_allocated(inode_nb, img_filepath)) {
fprintf(stderr, "Inode isn't allocated or inode value is invalid\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;
lseek(fd,
BLOCK_SIZE * idx_first_block_inode_table +
((inode_nb - 1) * sizeof(struct minix_inode)),
SEEK_SET);