c - Xv6 中的 Omode 是什么?

标签 c linux xv6

在 xv6 中处理文件时,我可以看到一个名为 Omode 的整型变量。它是什么?它有什么值(value)?

例如,这是来自 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的官方book :

open(filename, flags) Open a file; the flags indicate read/write

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

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

地点:

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

您还可以进一步查看代码,看看在哪里使用了 readable 和 writable:

可读 block 在不允许时读取:

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

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

writable 的工作原理与写入类似:

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

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

关于c - Xv6 中的 Omode 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53361908/

相关文章:

c - 不同架构的集群应该指定哪个AVX和march?

linux - 从 Linux Shell 递归解压?

c - 在 xv6 中实现系统调用

c - xv6 - switch 语句中的 '+' 是什么意思

c - 如何在 "if"比较中使用#define mask

c - 奇怪的 for 循环条件

c - 在 C 中取消数组中的结构

linux - 在centos5上安装nxlog(依赖)

linux - 如何加速 Linux 内核编译?

c - 如何将值传递给 XV6 中的系统调用函数?