static errcode_t open_and_check(o2fsck_state *ost, char *filename, int open_flags, uint64_t blkno, uint64_t blksize) { errcode_t ret; ret = ocfs2_open(filename, open_flags, blkno, blksize, &ost->ost_fs); if (ret) { com_err(whoami, ret, "while opening \"%s\"", filename); goto out; } ret = check_superblock(ost); if (ret) { printf("fsck saw unrecoverable errors in the super block and " "will not continue.\n"); goto out; } out: return ret; }
int main(int argc, char *argv[]) { int fd; int i = 0, c = 0; struct stat filestat; int inode_number, blk_grp_number; int inode_grp_offset; int superblock_present; int offset; int filesize; uint32_t data32; uint16_t data16; // Setting up buffers unsigned char blk[MAX_BLOCK_SIZE]; unsigned char blk_bitmap[MAX_BLOCK_SIZE]; unsigned char inode_bitmap[MAX_BLOCK_SIZE]; unsigned char inode[MAX_INODE_SIZE]; if (argc < 3) { printf("\nUsage : fileblock [filename] [/dev/sda1]\n\n"); return 1; } // Open data file fd = open(argv[1], O_RDONLY); if (fd < 0) { printf("Error opening file\n"); return 1; } // Open file system fs = open(argv[2], O_RDONLY); if (fs < 0) { printf("Error opening file system\n"); close(fd); return 1; } // Initialize all the file system specific structures if (init_fs() != 0) { printf("Error reading extfs information from super block\n"); close(fs); close(fd); return 1; } // Read inode number of file if (fstat(fd, &filestat) < 0) { printf("Error reading inode number for a file\n"); close(fs); close(fd); return 1; } inode_number = filestat.st_ino; blk_grp_number = (inode_number / INODE_PER_GROUP); inode_grp_offset = ((inode_number - 1) % INODE_PER_GROUP) * INODE_SIZE; // Super block copy present in block number 0, 1 and power of 3, 5, 7 superblock_present = check_superblock(blk_grp_number); printf("Inode number: %d (offset = %d)\n", inode_number, inode_grp_offset); printf("Block number: %d (super block = %d)\n", blk_grp_number, superblock_present); close(fd); if (read_gdt(blk_grp_number) < 0) { close(fs); return 1; } offset = (inode_table_addr * BLOCK_SIZE) + inode_grp_offset; printf("Inode table entry at : %d\n", offset); if (lseek(fs, offset, 0) < 0) { printf("Failed to seek to inode table entry\n"); close(fs); return 1; } // Read inode table read(fs, inode, INODE_SIZE); // Decoding inode data which in is LE format // http://wiki.osdev.org/Ext2 // User id data16 = inode[3]; data16 = (data16 << 8) | inode[2]; printf("User ID : %d\n", data16); // File size data32 = inode[7]; data32 = (data32 << 8) | inode[6]; data32 = (data32 << 8) | inode[5]; data32 = (data32 << 8) | inode[4]; filesize = data32; printf("Size : %d\n", filesize); // Address of direct block 0 data32 = inode[43]; data32 = (data32 << 8) | inode[42]; data32 = (data32 << 8) | inode[41]; data32 = (data32 << 8) | inode[40]; printf("Direct Block 0 : %d\n", data32); // Reading inode data if (lseek(fs, data32 * BLOCK_SIZE, 0) < 0) { printf("Failed to seek to inode data block 0\n"); close(fs); return 1; } read(fs, blk, BLOCK_SIZE); // Showing upto max first 1024 bytes by adding \0 in the data buffer if (filesize < 1024) { blk[filesize] = '\0'; } else { blk[1024] = '\0'; } printf("Data 0 (max 1024 bytes) :\n%s\n", blk); close(fs); return 0; }