[暑假补课]OS第四次实验资料-文件系统验证
这次是个验证性的实验,PPT里有详细的步骤了~哦。。对了。。有个能实现实验步骤的程序老师似乎木有给。
**** Hidden Message ***** 本帖最后由 小木公 于 2012-8-15 20:47 编辑
代码在此-opengroup
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/ext2_fs.h>
#define BASE_OFFSET 1024
#define USB_DEVICE "/dev/sda1"
#define BLOCK_OFFSET(block) (BASE_OFFSET + (block - 1) * block_size)
#define FILTER_BIT(byte, i) ((byte >> i) & 0x01)
static unsigned int block_size = 0;
/*
* Please IMPLEMENT THIS: this function will print out a portion of the group
* descriptor pointed to by group to the console. This function is called in
* open_usb with the first group descriptor.
*/
void print_group_desc(struct ext2_group_desc* group);
/*
* The actual function to open and read raw bytes off the USB stick.
* Takes a pointer to a superblock structure, fills it with the bytes
* from the USB drive and returns the file descriptor (which is actually
* just an int)
*/
int open_usb(struct ext2_super_block* super);
int main(void){
struct ext2_super_block usb_block;
int file_descriptor;
file_descriptor = open_usb(&usb_block);
return 0;
}
/*
* Implement this function.
*/
void print_group_desc(struct ext2_group_desc* group){
/*
* Print out: blocks bitmap block, inodes bitmap block, inodes table block,
* free blocks count, free inodes count, and the directories count. Your
* output should be "<field>\t:<value>\n".
*/
}
int open_usb(struct ext2_super_block* super){
int fd;
struct ext2_group_desc* group = malloc(sizeof(struct ext2_group_desc));
/* open USB device */
fd = open(USB_DEVICE, O_RDONLY); //opening the device for reading
if(fd < 0){ //some kind of error occurred
perror(USB_DEVICE);
exit(1); //we give up at this point
}
/* Now we read in Mr. Superblock */
/* seeking across the 'disk' to the superblock location */
lseek(fd, BASE_OFFSET, SEEK_SET);
/*actually reading in the bytes */
read(fd, super, sizeof(struct ext2_super_block));
/* Some sanity checks */
/* Make sure we're reading an EXT2 filesystem */
if(super->s_magic != EXT2_SUPER_MAGIC){
fprintf(stderr, "Not an Ext2 filesystem!\n");
exit(1);
}
block_size = 1024 << super->s_log_block_size;
/*Now on to reading the group descriptors */
int num_groups =
(super->s_blocks_count + super->s_blocks_per_group - 1) /
super->s_blocks_per_group;
/* seek to the first descriptor */
lseek(fd,
BASE_OFFSET + block_size,
SEEK_SET);
/* read it in */
read(fd, group, sizeof(struct ext2_group_desc));
/* print it! */
print_group_desc(group);
/* closing the USB device */
close(fd);
return fd;
}
opensuper
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/ext2_fs.h>
#define BASE_OFFSET 1024
#define USB_DEVICE "/dev/sda1"
#define BLOCK_OFFSET(block) (BASE_OFFSET + (block - 1) * block_size)
#define FILTER_BIT(byte, i) ((byte >> i) & 0x01)
static unsigned int block_size = 0;
/*
* Please IMPLEMENT THIS! This function will print out a portion of the
* superblock to the console. It is called in main after reading in the super
* block from the disk.
*/
void print_super_block(struct ext2_super_block* super);
/*
* The actual function to open and read raw bytes off the USB stick.
* Takes a pointer to a superblock structure, fills it with the bytes
* from the USB drive and returns the file descriptor (which is actually
* just an int)
*/
int open_usb(struct ext2_super_block* super);
int main(void){
struct ext2_super_block usb_block;
int file_descriptor;
file_descriptor = open_usb(&usb_block);
print_super_block(&usb_block);
return 0;
}
/*
* Implement this method!
*/
void print_super_block(struct ext2_super_block* super){
/*
* Print out the inodes count, blocks count, free blocks count, free inodes
* count, the first data block, block size, blocks per group, inodes per
* group, and the size of the inode structure. The output should be
* "<field>\t:<value>\n".
*/
}
int open_usb(struct ext2_super_block* super){
int fd;
struct ext2_group_desc* group = malloc(sizeof(struct ext2_group_desc));
/* open USB device */
fd = open(USB_DEVICE, O_RDONLY); //opening the device for reading
if(fd < 0){ //some kind of error occurred
perror(USB_DEVICE);
exit(1); //we give up at this point
}
/* Now we read in Mr. Superblock */
/* seeking across the 'disk' to the superblock location */
lseek(fd, BASE_OFFSET, SEEK_SET);
/*actually reading in the bytes */
read(fd, super, sizeof(struct ext2_super_block));
/* Some sanity checks */
/* Make sure we're reading an EXT2 filesystem */
if(super->s_magic != EXT2_SUPER_MAGIC){
fprintf(stderr, "Not an Ext2 filesystem!\n");
exit(1);
}
block_size = 1024 << super->s_log_block_size;
/* closing the USB device */
close(fd);
return fd;
}
P.S.去年的代码,需要略作修改~ 没补课的同学可以参考~ {:5_156:}似乎是好东西啊 ing........ 看看看看学习学习学习 看到楼主名字,顿觉霸气侧漏。。。 顶一个,必须的。。 没补课的路过 感激不尽! 顶一下啊啊啊啊啊啊啊啊啊啊啊啊啊啊 学习一下~~ {:6_208:}。。。 fghhslk 发表于 2012-8-15 21:59 static/image/common/back.gif
。。。
{:6_187:}......... 学习一下~ 看看学习了 木补课的瞅瞅!
来自:软院网·中科大 Android客户端 seeeeeeeeee thank u for sharing. {:10_455:}THX~ 楼主辛苦啦
什么东东{:10_471:} 看看!谢谢楼主啦! 以前没弄过。。。。 我来看看哦 {:5_134:} LZ赞一个 看看啊,好好学习学习 thank you!
多谢楼主! 好东西,顶啊,谢谢 撒旦法反倒是是的{:5_147:} 看看是不是好东西 kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
看看快快快坎坎坷坷啦啦啦
顶一个。。。 谢谢分享! 法律程序、今年第三次 反反复复付付付付付付付付付付付付付付付付
看看看看学习学习学习 谢谢谢谢谢谢 厉害了,路过看看
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 顶一下啦啦啦啦啦啦啦啦啦
页:
[1]