📄 dir.c
字号:
#include "fs.h"// member furnctions for DirEnt, DirBlock class/*void DirEntInit(struct DirEnt *de){ de->ino=-1;}*/static void get_fname_in_dirent(struct DirEnt *dent, char *buf){ strncpy(buf, dent->fname, dent->name_len); buf[dent->name_len]=0;}void DirBlk_init(struct DirBlock *db){// init db int i; for(i=0;i<BLKSIZE;i++) db->box[i]=0;}static struct DirEnt * next_dent(struct DirEnt *dent){// ret next dir entry of dent return ((struct DirEnt *) ( ((int) dent)+ dent->rec_len));}struct DirEnt * DirBlk_get_free_dir_ent(struct DirBlock *db){// get free entry in directory block d struct DirEnt *dent = (struct DirEnt *) db; for(;;){ // find dir entry whose reclen=0 if (dent->rec_len==0) return dent; dent = next_dent(dent); if (((int)dent)>=BLKSIZE) break; } panic("no more entry in dir_ent");}void DirBlk_report_new_entry(struct DirBlock *dir, int ino, char *fname){// fname is a new file with inode num ino. report it in dir struct DirEnt *x; int quotient, remain,tot_len; printk("DirBlk_report_new_entry for %s\n",fname); x=DirBlk_get_free_dir_ent(dir); printk("In DirBlk_report_new_entry. %x\n",x); x->ino = ino; strcpy(x->fname, fname); x->name_len = strlen(fname); tot_len = 4+ // inode number 4+ // rec_len & name_len + file_type x->name_len; // file name length // we need to round up by multiple of 4 quotient = tot_len / 4; remain = tot_len % 4; if (remain !=0) quotient++; x->rec_len = quotient * 4;}void DirBlk_set_empty(struct DirBlock *db,int me, int parent){// make empty dir block. me is this dir's inode; parent its parent inode DirBlk_init(db); DirBlk_report_new_entry(db, me, "."); DirBlk_report_new_entry(db, parent, "..");}int find_ino_for_file_in_dirblk(char *f, char *blk){ struct DirEnt *dent = (struct DirEnt *) blk; char buf[EXT2_NAME_LEN]; int start = (int) dent; printk("finding ino for file %s in dirblk\n",f); for(;;){ // find dir entry whose fname is f if (dent->rec_len == 0) break; // end of dir entries get_fname_in_dirent(dent,buf); // get file name as a string in buf printk("the next file name we check is %s\n", buf); if (!strcmp(buf, f)) return dent->ino; dent = next_dent(dent); if ((int)(dent-start) >= BLKSIZE) break; // if at end of blk } return -1; // no such file name}int find_ino_for_file_in_dir(char *f, struct Inode *in){// return inode num of file "f" which belongs to a directory "in" // for all blocks in dir, call find_ino_for_file_in_dirblk struct Ext2Inode *inode = in->inode; int nblks = inode->i_blocks; int i,ino; char *blk; printk("finding ino for file %s in dir whose ino is %d\n",f,in->ino); for(i=0;i<nblks;i++){ blk=(char *)FBTable_get_block(inode->i_block[i]); ino=find_ino_for_file_in_dirblk(f, blk); if (ino > 0) break; } return ino;}void dump_directory_file_block(char *blk){// buf is a block of some dir. dump the contents struct DirEnt *dent = (struct DirEnt *) blk; char buf[EXT2_NAME_LEN]; int start=(int)dent; for(;;){ // find dir entry whose fname is f if (dent->rec_len == 0) break; // end of dir entries get_fname_in_dirent(dent, buf); printk("ino %d fname %s ", dent->ino, buf); dent = next_dent(dent); if ((int)(dent-start)>=BLKSIZE) break; } printk("dumping ended for this block\n");}void dump_directory_file(struct Inode *in){// in points to a directoy file. dump the contents struct Ext2Inode *inode = in->inode; int nblks = inode->i_blocks; int i; char *blk; printk("dumping dir file. nblks is %d\n", nblks); for(i=0;i<nblks;i++){ blk=(char *)FBTable_get_block(inode->i_block[i]); dump_directory_file_block(blk); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -