[技术| 编程·课件·Linux] [暑假补课]OS第四次实验资料-文件系统验证
callmesb
· 发布于 2012-08-15 19:55
· 3710 次阅读
转载文章时务必注明原作者及原始链接,并注明「发表于 软院网 RuanYuan.Net 」,并不得对作品进行修改。
本帖最后由 小木公 于 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.去年的代码,需要略作修改~ |
ing........ |
thank u for sharing. |
看看!谢谢楼主啦! |
LZ赞一个 |
看看啊,好好学习学习 |
看看是不是好东西 |
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq |