📄 readext2fs.c
字号:
} printf("file name %s\t\tinode: %u\n",name,inum); printf("file mode :%o\n",inode->i_mode); printf("file size :%u\n",inode->i_size); printf("data block count :%u\n",inode->i_blocks); show_inode_data_block(inode); printf("creation time :%s",ctime((time_t*)&(inode->i_ctime))); printf("access time :%s",ctime((time_t*)&(inode->i_atime))); return 0;}int show_inode_from_inum(int fd,unsigned long inum,char* pblock){ struct ext2_inode inode; read_inode(fd,inum,(struct ext2_inode*)&inode); if(inum == ROOT_INODE) printf("This is root inode of device: %s\t \ volume name of the device: %s\n", device,ebi.volume_name); show_inode_info(inum,NULL,&inode); if(IS_DIR(inode.i_mode)) { printf("%lu is a directory:\n",inum); ls(&inode,fd,pblock); } return 0;}//===========================================================================unsigned long match_dir(struct ext2_dir_entry_2* dir,char* name){ int count = 0; char buf[255],*pblock; struct ext2_dir_entry_2* cur_dir = dir; while(count < ebi.block_size) { memset(buf,0,255); strncpy(buf,cur_dir->name,cur_dir->name_len); if(!strcmp(buf,name)) return cur_dir->inode; count += cur_dir->rec_len;// printf("inode:%7u\tname:%s\n",dir->inode,buf); pblock = (char*)dir; pblock += count; cur_dir = (struct ext2_dir_entry_2*)pblock; } return 0;}int show_inode_from_name(int fd,char* name,char* pblock){ char sub[CMD_LEN],*pos = NULL,*pos1 = NULL; unsigned long dir_block,inum; struct ext2_inode inode; if(name[0] != '/') { printf("Invalid Path!\n"); return -1; } if(!strcmp(name,"/")) { printf("This is the root directory of device: %s\t \ volume name of the device: %s\n", device,ebi.volume_name); read_inode(fd,ROOT_INODE,&inode); show_inode_info(ROOT_INODE,"/",&inode); if(IS_DIR(inode.i_mode)) { printf("%s is a directory:\n",name); ls(&inode,fd,pblock); } return 0; } pos = name + 1; dir_block = ebi.root_dir_block; while(1) { memset(pblock,0,BUFFER_SIZE); if(lseek64(fd,(ull)dir_block*(ull)ebi.block_size,SEEK_SET) < 0) { fprintf(stderr,"seek error:%s\n",strerror(errno)); return -1; } read(fd,pblock,BUFFER_SIZE); memset(sub,0,CMD_LEN); if((pos1 = strchr(pos,'/'))) { strncpy(sub,pos,pos1 - pos); if((inum = match_dir((struct ext2_dir_entry_2*)pblock,sub)) == 0) { fprintf(stderr,"Invalid directory:%s\n",name); return -1; } read_inode(fd,inum,&inode); dir_block = inode.i_block[0]; pos = pos1 + 1; } else { strcpy(sub,pos); if((inum = match_dir((struct ext2_dir_entry_2*)pblock,sub)) == 0) { fprintf(stderr,"error: invalid path %s\n",name); return -1; } read_inode(fd,inum,&inode); show_inode_info(inum,name,&inode); if(IS_DIR(inode.i_mode)) { printf("%s is a directory:\n",name); ls(&inode,fd,pblock); } break; } } return 0;}void show_help(){ printf("Command and it`s format:\n"); printf("cmd: block N <N is the number of block of the device>\n"); printf("cmd: super\n"); printf("cmd: desc_table\n"); printf("cmd: group_desc N <N is the number of group of the device>\n"); printf("cmd: block_bitmap N <N is the number of block of the device>\n"); printf("cmd: inode_bitmap N <N is the number of block of the device>\n"); printf("cmd: inode_table N [M ] <N is the number of block of the device ;M is the block number of the inode table of group N>\n"); printf("cmd: inode N <N is the number of inode>\n"); printf("cmd: namei name <name is a real path, for example: /boot>\n"); printf("cmd: help\n"); printf("cmd: quit\n");}//===========================================================================int get_base_info(int fd,char* pblk){ struct ext2_super_block* sb = NULL; memset(pblk,0,BUFFER_SIZE); lseek(fd,1024,SEEK_SET); read(fd,pblk,1024); sb = (struct ext2_super_block*)pblk; if(sb->s_magic != 0xef53) { fprintf(stderr,"This device %s is not a ext2 filesystem!\n",device); return -1; } memset(&ebi,0,sizeof(struct ext2_base_info)); ebi.blocks_count = sb->s_blocks_count; //= ebi.block_size = 1<<(sb->s_log_block_size + 10); ebi.device_size = (ebi.blocks_count * (ebi.block_size/1024))/1024; strncpy(ebi.volume_name,sb->s_volume_name,16); //= ebi.first_data_block = sb->s_first_data_block; //= ebi.blocks_per_group = sb->s_blocks_per_group; //= ebi.group_size = sb->s_blocks_per_group*(ebi.block_size/1024)/1024; ebi.groups_count = (sb->s_blocks_count - sb->s_first_data_block - 1)/sb->s_blocks_per_group +1; ebi.inodes_per_group = sb->s_inodes_per_group; //= ebi.inode_blocks_per_group = sb->s_inodes_per_group/(ebi.block_size/sizeof(struct ext2_inode)); read_inode(fd,ROOT_INODE,(struct ext2_inode*)pblk); ebi.root_dir_block = ((struct ext2_inode*)pblk)->i_block[0];// printf("root dir block: %lu\n",ebi.root_dir_block); return 0;}/********************************************************************************************************************************************************/char* left_align(char* pTemp){ int i,len; len = strlen(pTemp); for(i = 0; i<len && pTemp[i] == ' '; i++); if(i == len) return NULL; return pTemp + i;}int parse_cmd(int fd,char* pblock,char* cmd){ char* pPos = NULL; char type[CMD_LEN],param[CMD_LEN],name[CMD_LEN]; int i = 0; int num = -1,off = 0; unsigned long inum = 0; memset(type,0,CMD_LEN); memset(param,0,CMD_LEN); memset(name,0,CMD_LEN); //parse command; if((pPos = left_align(cmd)) == NULL) { fprintf(stderr,"command error!\n"); return -1; } while(pPos[i] != ' ' && pPos[i] != 0) { type[i] = pPos[i]; i++; } if((pPos = left_align(pPos + i))) { i = 0; while(pPos[i] != ' '&&pPos[i] != 0) { param[i] = pPos[i]; i++; } strcpy(name,param); num = atoi(param); inum = strtoul(param,NULL,10); memset(param,0,CMD_LEN); if((pPos = left_align(pPos + i))) { i = 0; while(pPos[i] != ' '&&pPos[i] != 0) { param[i] = pPos[i]; i++; } off = atoi(param); } } printf("==============================>\n"); //execve command; if(!strcmp(type,"block")) { show_block(fd,num,pblock); } else if(!strcmp(type,"super")) { show_super_info(fd,pblock); } else if(!strcmp(type,"desc_table")) { show_group_desc_table(fd,pblock); } else if(!strcmp(type,"group_desc")) { show_group_desc(fd,num,pblock); } else if(!strcmp(type,"block_bitmap")) { show_block_bitmap(fd,num,pblock); } else if(!strcmp(type,"inode_bitmap")) { show_inode_bitmap(fd,num,pblock); } else if(!strcmp(type,"inode_table")) { show_inode_table(fd,num,off,pblock); } else if(!strcmp(type,"inode")) { show_inode_from_inum(fd,inum,pblock); } else if(!strcmp(type,"namei")) { show_inode_from_name(fd,name,pblock); } else if(!strcmp(type,"help")) { show_help(); } else { fprintf(stderr,"unknowned command! \n\n"); show_help(); return -1; } return 0; }void shell(int fd,char* pblock,char* cmd){ int len; printf("please input command to get information of etx2 file system or input \"help\" to get help infomation\n"); while(1) { printf("(cmd)"); memset(cmd,0,CMD_LEN); fgets(cmd,CMD_LEN,stdin); len = strlen(cmd); cmd[len -1] = 0; if(!strcmp(cmd,"quit")) break; parse_cmd(fd,pblock,cmd); }}void usage(char* name){ fprintf(stderr,"usage:\n\t%s [device name]\n",name); fprintf(stderr,"for example: %s /dev/hda4\n\n",name);// fprintf(stderr,"\n"); show_help(); exit(1);}int main(int argc,char** argv){ char cmd[CMD_LEN]; char* pblock = NULL; int fd,ret; if(argc == 1) usage(argv[0]); strcpy(device,argv[1]); if((fd = open(device,O_RDONLY)) < 0) { fprintf(stderr,"open %s error: %s\n",device,strerror(errno)); exit(1); } if((pblock = (char*)malloc(BUFFER_SIZE)) == NULL) { fprintf(stderr,"malloc buffer error:%s\n",strerror(errno)); close(fd); exit(1); } if((ret = get_base_info(fd,pblock)) < 0) { if(ret != -1) fprintf(stderr,"read base infomation from %s error!\n",device); close(fd); free(pblock); exit(1); } shell(fd,pblock,cmd); close(fd); free(pblock); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -