在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。这些值代表什么?
发布于 2018-11-26 06:10:55
omode (代表打开模式),是xv6操作系统中打开系统调用的第二个参数,表示打开文件时使用的模式,第一个参数中给出了文件的名称和路径。
来自xv6的官方book
Open (文件名,标志)打开文件;标志表示读/写
此字段的有效选项为(定义位于fcntl.h):
#define O_RDONLY 0x000
#define O_WRONLY 0x001
#define O_RDWR 0x002
#define O_CREATE 0x200其中:
您还可以进一步跟踪代码,看看在哪里使用了readable和writable:
不允许时读取可读数据块:
// Read from file f.
int
fileread(struct file *f, char *addr, int n)
{
int r;
if(f->readable == 0)
return -1;
...writes的工作原理与writes类似:
// Write to file f.
int
filewrite(struct file *f, char *addr, int n)
{
int r;
if(f->writable == 0)
return -1;
...https://stackoverflow.com/questions/53361908
复制相似问题