callmesb 发表于 2012-8-15 19:55:21

[暑假补课]OS第四次实验资料-文件系统验证

这次是个验证性的实验,PPT里有详细的步骤了~
哦。。对了。。有个能实现实验步骤的程序老师似乎木有给。
**** Hidden Message *****

小木公 发表于 2012-8-15 20:45:50

本帖最后由 小木公 于 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;
}

小木公 发表于 2012-8-15 20:49:05

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.去年的代码,需要略作修改~

admin 发表于 2012-8-15 20:28:24

没补课的同学可以参考~

小木公 发表于 2012-8-15 20:29:30

{:5_156:}似乎是好东西啊

beckham467 发表于 2012-8-15 20:29:41

ing........

0x710dddd 发表于 2012-8-15 20:32:24

看看看看学习学习学习

rocklee 发表于 2012-8-15 20:32:39

看到楼主名字,顿觉霸气侧漏。。。

sunhongbo 发表于 2012-8-15 20:38:24

顶一个,必须的。。

andylove 发表于 2012-8-15 20:40:30

没补课的路过

刘世伟 发表于 2012-8-15 20:49:02

感激不尽!

m68300981 发表于 2012-8-15 21:00:39

顶一下啊啊啊啊啊啊啊啊啊啊啊啊啊啊

冰封飞飞 发表于 2012-8-15 21:21:55

学习一下~~

fghhslk 发表于 2012-8-15 21:59:31

{:6_208:}。。。

callmesb 发表于 2012-8-15 22:47:53

fghhslk 发表于 2012-8-15 21:59 static/image/common/back.gif
。。。

{:6_187:}.........

ngddjl 发表于 2012-8-15 23:49:17

学习一下~   

镕羽 发表于 2012-8-16 00:19:37

看看学习了

wkmatt 发表于 2012-8-16 01:34:09

木补课的瞅瞅!
来自:软院网·中科大 Android客户端

fwsir 发表于 2012-8-16 07:05:49

seeeeeeeeee

tutuhuagong 发表于 2012-8-16 09:40:24

thank u for sharing.

wjx45007 发表于 2012-8-16 10:11:30

{:10_455:}THX~

o小酒窝o 发表于 2012-8-16 10:53:41

楼主辛苦啦

Miller 发表于 2012-8-16 22:32:15

什么东东{:10_471:}

mumufengling 发表于 2012-8-16 22:49:06

看看!谢谢楼主啦!

ssc 发表于 2012-8-17 17:29:41

以前没弄过。。。。

汪晨阳189 发表于 2012-8-25 21:46:20

我来看看哦

mcoursework 发表于 2012-8-26 12:26:33

{:5_134:}         LZ赞一个

renzhefengshen 发表于 2012-8-29 21:40:48

看看啊,好好学习学习

kimitaozi 发表于 2012-9-8 19:21:22

thank you!

在下鸠摩智 发表于 2013-7-4 21:56:14

多谢楼主!

sky8023 发表于 2013-8-3 22:42:34

好东西,顶啊,谢谢

异见天空 发表于 2013-8-24 23:01:11

撒旦法反倒是是的{:5_147:}

leeger8580 发表于 2013-8-24 23:40:17

看看是不是好东西

sxxzjm 发表于 2017-7-18 10:19:35

kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk

贝壳 发表于 2017-7-27 07:17:15

看看快快快坎坎坷坷啦啦啦

夏小乔 发表于 2017-8-2 20:03:17

顶一个。。。

心如止水2017 发表于 2017-8-2 20:21:23

谢谢分享!

车欠耳完 发表于 2017-8-2 20:49:42

法律程序、今年第三次

exexeet 发表于 2017-8-13 11:20:37

反反复复付付付付付付付付付付付付付付付付

Ymengchun 发表于 2018-5-15 10:55:36


看看看看学习学习学习

ruanyuan 发表于 2018-5-28 22:48:54

谢谢谢谢谢谢

zch 发表于 2018-7-3 10:40:11

厉害了,路过看看

heyang5188 发表于 2019-1-10 16:57:41

qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq

yt1997 发表于 2019-1-11 19:34:06

顶一下啦啦啦啦啦啦啦啦啦
页: [1]
查看完整版本: [暑假补课]OS第四次实验资料-文件系统验证