Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xv6-public
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
OS Lab Group
奖励实验
xv6-public
提交
2cbb4b18
提交
2cbb4b18
9月 08, 2006
创建
作者:
rsc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
stop using fd to name files
上级
5692823b
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
60 行增加
和
60 行删除
+60
-60
file.c
file.c
+42
-42
pipe.c
pipe.c
+18
-18
没有找到文件。
file.c
浏览文件 @
2cbb4b18
...
@@ -11,7 +11,7 @@
...
@@ -11,7 +11,7 @@
#include "fs.h"
#include "fs.h"
#include "fsvar.h"
#include "fsvar.h"
struct
spinlock
f
d
_table_lock
;
struct
spinlock
f
ile
_table_lock
;
struct
devsw
devsw
[
NDEV
];
struct
devsw
devsw
[
NDEV
];
struct
file
file
[
NFILE
];
struct
file
file
[
NFILE
];
...
@@ -19,7 +19,7 @@ struct file file[NFILE];
...
@@ -19,7 +19,7 @@ struct file file[NFILE];
void
void
fileinit
(
void
)
fileinit
(
void
)
{
{
initlock
(
&
f
d_table_lock
,
"fd
_table"
);
initlock
(
&
f
ile_table_lock
,
"file
_table"
);
}
}
// Allocate a file structure
// Allocate a file structure
...
@@ -28,34 +28,34 @@ filealloc(void)
...
@@ -28,34 +28,34 @@ filealloc(void)
{
{
int
i
;
int
i
;
acquire
(
&
f
d
_table_lock
);
acquire
(
&
f
ile
_table_lock
);
for
(
i
=
0
;
i
<
NFILE
;
i
++
){
for
(
i
=
0
;
i
<
NFILE
;
i
++
){
if
(
file
[
i
].
type
==
FD_CLOSED
){
if
(
file
[
i
].
type
==
FD_CLOSED
){
file
[
i
].
type
=
FD_NONE
;
file
[
i
].
type
=
FD_NONE
;
file
[
i
].
ref
=
1
;
file
[
i
].
ref
=
1
;
release
(
&
f
d
_table_lock
);
release
(
&
f
ile
_table_lock
);
return
file
+
i
;
return
file
+
i
;
}
}
}
}
release
(
&
f
d
_table_lock
);
release
(
&
f
ile
_table_lock
);
return
0
;
return
0
;
}
}
// Write to file f. Addr is kernel address.
// Write to file f. Addr is kernel address.
int
int
filewrite
(
struct
file
*
f
d
,
char
*
addr
,
int
n
)
filewrite
(
struct
file
*
f
,
char
*
addr
,
int
n
)
{
{
if
(
f
d
->
writable
==
0
)
if
(
f
->
writable
==
0
)
return
-
1
;
return
-
1
;
if
(
f
d
->
type
==
FD_PIPE
){
if
(
f
->
type
==
FD_PIPE
){
return
pipe_write
(
f
d
->
pipe
,
addr
,
n
);
return
pipe_write
(
f
->
pipe
,
addr
,
n
);
}
else
if
(
f
d
->
type
==
FD_FILE
)
{
}
else
if
(
f
->
type
==
FD_FILE
)
{
ilock
(
f
d
->
ip
);
ilock
(
f
->
ip
);
int
r
=
writei
(
f
d
->
ip
,
addr
,
fd
->
off
,
n
);
int
r
=
writei
(
f
->
ip
,
addr
,
f
->
off
,
n
);
if
(
r
>
0
)
{
if
(
r
>
0
)
{
f
d
->
off
+=
r
;
f
->
off
+=
r
;
}
}
iunlock
(
f
d
->
ip
);
iunlock
(
f
->
ip
);
return
r
;
return
r
;
}
else
{
}
else
{
panic
(
"filewrite"
);
panic
(
"filewrite"
);
...
@@ -65,18 +65,18 @@ filewrite(struct file *fd, char *addr, int n)
...
@@ -65,18 +65,18 @@ filewrite(struct file *fd, char *addr, int n)
// Read from file f. Addr is kernel address.
// Read from file f. Addr is kernel address.
int
int
fileread
(
struct
file
*
f
d
,
char
*
addr
,
int
n
)
fileread
(
struct
file
*
f
,
char
*
addr
,
int
n
)
{
{
if
(
f
d
->
readable
==
0
)
if
(
f
->
readable
==
0
)
return
-
1
;
return
-
1
;
if
(
f
d
->
type
==
FD_PIPE
){
if
(
f
->
type
==
FD_PIPE
){
return
pipe_read
(
f
d
->
pipe
,
addr
,
n
);
return
pipe_read
(
f
->
pipe
,
addr
,
n
);
}
else
if
(
f
d
->
type
==
FD_FILE
){
}
else
if
(
f
->
type
==
FD_FILE
){
ilock
(
f
d
->
ip
);
ilock
(
f
->
ip
);
int
cc
=
readi
(
f
d
->
ip
,
addr
,
fd
->
off
,
n
);
int
cc
=
readi
(
f
->
ip
,
addr
,
f
->
off
,
n
);
if
(
cc
>
0
)
if
(
cc
>
0
)
f
d
->
off
+=
cc
;
f
->
off
+=
cc
;
iunlock
(
f
d
->
ip
);
iunlock
(
f
->
ip
);
return
cc
;
return
cc
;
}
else
{
}
else
{
panic
(
"fileread"
);
panic
(
"fileread"
);
...
@@ -86,19 +86,19 @@ fileread(struct file *fd, char *addr, int n)
...
@@ -86,19 +86,19 @@ fileread(struct file *fd, char *addr, int n)
// Close file f. (Decrement ref count, close when reaches 0.)
// Close file f. (Decrement ref count, close when reaches 0.)
void
void
fileclose
(
struct
file
*
f
d
)
fileclose
(
struct
file
*
f
)
{
{
acquire
(
&
f
d
_table_lock
);
acquire
(
&
f
ile
_table_lock
);
if
(
f
d
->
ref
<
1
||
fd
->
type
==
FD_CLOSED
)
if
(
f
->
ref
<
1
||
f
->
type
==
FD_CLOSED
)
panic
(
"fileclose"
);
panic
(
"fileclose"
);
if
(
--
f
d
->
ref
==
0
){
if
(
--
f
->
ref
==
0
){
struct
file
dummy
=
*
f
d
;
struct
file
dummy
=
*
f
;
f
d
->
ref
=
0
;
f
->
ref
=
0
;
f
d
->
type
=
FD_CLOSED
;
f
->
type
=
FD_CLOSED
;
release
(
&
f
d
_table_lock
);
release
(
&
f
ile
_table_lock
);
if
(
dummy
.
type
==
FD_PIPE
){
if
(
dummy
.
type
==
FD_PIPE
){
pipe_close
(
dummy
.
pipe
,
dummy
.
writable
);
pipe_close
(
dummy
.
pipe
,
dummy
.
writable
);
...
@@ -108,18 +108,18 @@ fileclose(struct file *fd)
...
@@ -108,18 +108,18 @@ fileclose(struct file *fd)
panic
(
"fileclose"
);
panic
(
"fileclose"
);
}
}
}
else
{
}
else
{
release
(
&
f
d
_table_lock
);
release
(
&
f
ile
_table_lock
);
}
}
}
}
// Get metadata about file f.
// Get metadata about file f.
int
int
filestat
(
struct
file
*
f
d
,
struct
stat
*
st
)
filestat
(
struct
file
*
f
,
struct
stat
*
st
)
{
{
if
(
f
d
->
type
==
FD_FILE
){
if
(
f
->
type
==
FD_FILE
){
ilock
(
f
d
->
ip
);
ilock
(
f
->
ip
);
stati
(
f
d
->
ip
,
st
);
stati
(
f
->
ip
,
st
);
iunlock
(
f
d
->
ip
);
iunlock
(
f
->
ip
);
return
0
;
return
0
;
}
else
}
else
return
-
1
;
return
-
1
;
...
@@ -127,11 +127,11 @@ filestat(struct file *fd, struct stat *st)
...
@@ -127,11 +127,11 @@ filestat(struct file *fd, struct stat *st)
// Increment ref count for file f.
// Increment ref count for file f.
void
void
fileincref
(
struct
file
*
f
d
)
fileincref
(
struct
file
*
f
)
{
{
acquire
(
&
f
d
_table_lock
);
acquire
(
&
f
ile
_table_lock
);
if
(
f
d
->
ref
<
1
||
fd
->
type
==
FD_CLOSED
)
if
(
f
->
ref
<
1
||
f
->
type
==
FD_CLOSED
)
panic
(
"fileincref"
);
panic
(
"fileincref"
);
f
d
->
ref
++
;
f
->
ref
++
;
release
(
&
f
d
_table_lock
);
release
(
&
f
ile
_table_lock
);
}
}
pipe.c
浏览文件 @
2cbb4b18
...
@@ -19,14 +19,14 @@ struct pipe {
...
@@ -19,14 +19,14 @@ struct pipe {
};
};
int
int
pipe_alloc
(
struct
file
**
f
d1
,
struct
file
**
fd2
)
pipe_alloc
(
struct
file
**
f
0
,
struct
file
**
f1
)
{
{
*
f
d1
=
*
fd2
=
0
;
*
f
0
=
*
f1
=
0
;
struct
pipe
*
p
=
0
;
struct
pipe
*
p
=
0
;
if
((
*
f
d1
=
filealloc
())
==
0
)
if
((
*
f
0
=
filealloc
())
==
0
)
goto
oops
;
goto
oops
;
if
((
*
f
d2
=
filealloc
())
==
0
)
if
((
*
f
1
=
filealloc
())
==
0
)
goto
oops
;
goto
oops
;
if
((
p
=
(
struct
pipe
*
)
kalloc
(
PAGE
))
==
0
)
if
((
p
=
(
struct
pipe
*
)
kalloc
(
PAGE
))
==
0
)
goto
oops
;
goto
oops
;
...
@@ -35,25 +35,25 @@ pipe_alloc(struct file **fd1, struct file **fd2)
...
@@ -35,25 +35,25 @@ pipe_alloc(struct file **fd1, struct file **fd2)
p
->
writep
=
0
;
p
->
writep
=
0
;
p
->
readp
=
0
;
p
->
readp
=
0
;
initlock
(
&
p
->
lock
,
"pipe"
);
initlock
(
&
p
->
lock
,
"pipe"
);
(
*
f
d1
)
->
type
=
FD_PIPE
;
(
*
f
0
)
->
type
=
FD_PIPE
;
(
*
f
d1
)
->
readable
=
1
;
(
*
f
0
)
->
readable
=
1
;
(
*
f
d1
)
->
writable
=
0
;
(
*
f
0
)
->
writable
=
0
;
(
*
f
d1
)
->
pipe
=
p
;
(
*
f
0
)
->
pipe
=
p
;
(
*
f
d2
)
->
type
=
FD_PIPE
;
(
*
f
1
)
->
type
=
FD_PIPE
;
(
*
f
d2
)
->
readable
=
0
;
(
*
f
1
)
->
readable
=
0
;
(
*
f
d2
)
->
writable
=
1
;
(
*
f
1
)
->
writable
=
1
;
(
*
f
d2
)
->
pipe
=
p
;
(
*
f
1
)
->
pipe
=
p
;
return
0
;
return
0
;
oops:
oops:
if
(
p
)
if
(
p
)
kfree
((
char
*
)
p
,
PAGE
);
kfree
((
char
*
)
p
,
PAGE
);
if
(
*
f
d1
){
if
(
*
f
0
){
(
*
f
d1
)
->
type
=
FD_NONE
;
(
*
f
0
)
->
type
=
FD_NONE
;
fileclose
(
*
f
d1
);
fileclose
(
*
f
0
);
}
}
if
(
*
f
d2
){
if
(
*
f
1
){
(
*
f
d2
)
->
type
=
FD_NONE
;
(
*
f
1
)
->
type
=
FD_NONE
;
fileclose
(
*
f
d2
);
fileclose
(
*
f
1
);
}
}
return
-
1
;
return
-
1
;
}
}
...
...
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论