code:open1 - ikarishinjieva/unixV6-code-analyze-chs GitHub Wiki

Table of Contents

Source

  • open creat 调用,为这两个函数的公共函数
  • 共传入3个参数 ip,mode,trf
    • ip : 指向文件的INODE块
    • mode : 读/写模式
      • FREAD : 读模式
      • FWRITE : 写模式
    • trf : 这是一个特殊的参数,标志这个函数被调用的位置
      • 0 : 由open调用
      • 1 : 由creat 5795调用,
      • 2 : 由creat 5793调用,
  • 这个函数 是从 open creat 公共部分抽取而来,为了减少冗余
但是不是很好理解,因为功能很难说明 并且有很重的修改痕迹,对此LIONS代码分析中也提出了相近的评论
5798

5799 /*

5800  * common code for open and creat.

5801  * Check permissions, allocate an open file structure,

5802  * and call the device open routine if any.

5803  */

5804 open1(ip, mode, trf)

5805 int *ip;

5806 {

5807     register struct file *fp;

5808     register *rip, m;

5809     int i;

5810

5811     rip = ip;

5812     m = mode;

5813     if(trf != 2) {

5814          if(m&FREAD)

5815           access(rip, IREAD);

5816          if(m&FWRITE) {

5817           access(rip, IWRITE);

5818           if((rip->i_mode&IFMT) == IFDIR)

5819                u.u_error = EISDIR;
5820          }

5821     }

5822     if(u.u_error)

5823          goto out;

  • 此处若检到错误,是
    • access抛出
    • 由5819抛出
5824     if(trf)

5825          itrunc(rip);

  • LIONS 代码分析 在此提出将测试条件改为 trf==1,会改变性能
  • 个人认为 LIONS代码分析 出现了错误,应当与5794行(creat)笔误有关
5826     prele(rip);
5827     if ((fp = falloc()) == NULL)

5828          goto out;

  • falloc 中 置u.u_ar0[R0],将在5831使用
5829     fp->f_flag = m&(FREAD|FWRITE);

5830     fp->f_inode = rip;

5831     i = u.u_ar0[R0];

5832     openi(rip, m&FWRITE);

5833     if(u.u_error == 0)

5834          return;

5835     u.u_ofile[i] = NULL;

5836     fp->f_count--;

5837

5838 out:

5839     iput(rip);

5840 }

5841 /* ------------------------- */

Extend

附图

Ref

Caller

⚠️ **GitHub.com Fallback** ⚠️