?? igetput.c
字號:
#include <stdio.h>
#include <stdlib.h>
#include "filesys.h"
struct inode * iget(unsigned int dinodeid) /* iget() */
{
int existed=0,inodeid;
long addr;
struct inode *temp, *newinode;
inodeid=dinodeid % NHINO;
if(hinode[inodeid].i_forw==NULL) //hinode為查找內存i節點的hash表
existed=0;
else
{
temp=hinode[inodeid].i_forw;
while(temp)
{
if(temp->i_ino==inodeid)//existed ,i_ino是磁盤i節點標號
{
existed=1;
temp->i_count++;
return temp;
}
else /*not existed*/
temp=temp->i_forw;
};
}
// not existed 因為前面有個return沒有返回
addr=DINODESTART+dinodeid*DINODESIZ; //1. calculate the addr of the dinode in the file sys column
newinode=(struct inode *)malloc (sizeof(struct inode));//2. malloc the new inode
fseek(fd,addr,SEEK_SET); //3. read the dinode to the inode
fread(&(newinode->di_number),DINODESIZ,1,fd);
newinode->i_forw=hinode[inodeid].i_forw;//4. put it into hinode [inodeid]queue
newinode->i_back=newinode;
if(newinode->i_forw!=NULL)
newinode->i_forw->i_back=newinode;
hinode[inodeid].i_forw=newinode;
newinode->i_count=1; //5. initialize the inode ,i_count引用計數
newinode->i_flag=0; //f1ag for not update
newinode->i_ino=dinodeid;
newinode->di_size=3*(DIRSIZ+2);
if(dinodeid == 3)
newinode->di_size=BLOCKSIZ;
return newinode;
}
iput(struct inode *pinode) /*iput()*/
{
long addr;
unsigned int block_num;
int i;
if(pinode->i_count>1)
{
pinode->i_count--;//引用計數減1后返回
return;
}
else
{
if(pinode->di_number!=0)
{
/* write back the inode */
addr=DINODESTART+pinode->i_ino*DINODESIZ;
fseek(fd,addr,SEEK_SET);
fwrite(&pinode->di_number,DINODESIZ,1,fd);
}
else //關聯文件數為0。刪除該文件
{
/* rm the inode & the block of the file in the disk */
block_num=pinode->di_size/BLOCKSIZ;
for(i=0;i<block_num;i++)
{
bfree(pinode->di_addr[i]);
}
ifree(pinode->i_ino);
};
/* free the inode in the memory */
if(pinode->i_forw==NULL)
pinode->i_back->i_forw=NULL;
else
{
pinode->i_forw->i_back=pinode->i_back;
pinode->i_back->i_forw=pinode->i_forw;
};
ifree(pinode);
};
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -