繁体   English   中英

Xv6中的Omode是什么?

[英]What is Omode in Xv6?

在xv6中处理文件时,我可以看到一个称为Omode的整数变量。 它是什么? 它有什么价值?

例如,这是来自Xv6的开放系统调用:

int sys_open(void)
{
  char *path;
  int fd, omode;
  struct file *f;
  struct inode *ip;

  if (argstr(0, &path) < 0 || argint(1, &omode) < 0)
    return -1;

  begin_op();

  if (omode & O_CREATE) {
    ip = create(path, T_FILE, 0, 0);
    if (ip == 0) {
      end_op();
      return -1;
    }
  } else {
    if ((ip = namei(path)) == 0) {
      end_op();
      return -1;
    }
    ilock(ip);
    if (ip->type == T_DIR && omode != O_RDONLY) {
      iunlockput(ip);
      end_op();
      return -1;
    }
  }

  if ((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0) {
    if (f)
      fileclose(f);
    iunlockput(ip);
    end_op();
    return -1;
  }
  iunlock(ip);
  end_op();

  f->type = FD_INODE;
  f->ip = ip;
  f->off = 0;
  f->readable = !(omode & O_WRONLY);
  f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
  return fd;
}

似乎可能是O_WRONLY,O_RDWR或O_CREATE。 这些值代表什么?

omode(代表开放模式),是xv6操作系统中用于开放系统调用的第二个参数,代表打开名称和路径在第一个参数上给出的文件时要使用的模式。

来自xv6的官方书籍

open(文件名,标志)打开文件; 标志指示读/写

该字段的有效选项为(定义位于fcntl.h):

#define O_RDONLY  0x000
#define O_WRONLY  0x001
#define O_RDWR    0x002
#define O_CREATE  0x200

哪里:

  • O_RDONLY-说明文件应以只读模式打开。 不要让写入从open调用返回的文件描述符表示的文件。
  • O_WRONLY-与上面相同,但仅允许写入不读。
  • O_RDWR-允许读取和写入。
  • O_CREATE-允许打开以创建给定的文件(如果尚不存在)。

您还可以进一步遵循该代码,并查看在何处使用可读和可写:

可读块不允许时读取:

// Read from file f.
int
fileread(struct file *f, char *addr, int n)
{
  int r;

  if(f->readable == 0)
    return -1;
...

可写作品类似于写:

// Write to file f.
int
filewrite(struct file *f, char *addr, int n)
{
  int r;

  if(f->writable == 0)
    return -1;
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM