Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xv6-public
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
OS Lab Group
奖励实验
xv6-public
提交
2186f88c
提交
2186f88c
8月 14, 2007
创建
作者:
rsc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
formatting, cleanup
上级
cce27ba9
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
54 行增加
和
56 行删除
+54
-56
file.c
file.c
+54
-56
没有找到文件。
file.c
浏览文件 @
2186f88c
...
...
@@ -41,75 +41,55 @@ filealloc(void)
return
0
;
}
//
Write to file f. Addr is kernel address
.
int
file
write
(
struct
file
*
f
,
char
*
addr
,
int
n
)
//
Increment ref count for file f
.
void
file
incref
(
struct
file
*
f
)
{
if
(
f
->
writable
==
0
)
return
-
1
;
if
(
f
->
type
==
FD_PIPE
){
return
pipe_write
(
f
->
pipe
,
addr
,
n
);
}
else
if
(
f
->
type
==
FD_FILE
)
{
ilock
(
f
->
ip
);
int
r
=
writei
(
f
->
ip
,
addr
,
f
->
off
,
n
);
if
(
r
>
0
)
{
f
->
off
+=
r
;
}
iunlock
(
f
->
ip
);
return
r
;
}
else
{
panic
(
"filewrite"
);
return
-
1
;
}
acquire
(
&
file_table_lock
);
if
(
f
->
ref
<
1
||
f
->
type
==
FD_CLOSED
)
panic
(
"fileincref"
);
f
->
ref
++
;
release
(
&
file_table_lock
);
}
// Read from file f. Addr is kernel address.
int
fileread
(
struct
file
*
f
,
char
*
addr
,
int
n
)
{
int
r
;
if
(
f
->
readable
==
0
)
return
-
1
;
if
(
f
->
type
==
FD_PIPE
)
{
if
(
f
->
type
==
FD_PIPE
)
return
pipe_read
(
f
->
pipe
,
addr
,
n
);
}
else
if
(
f
->
type
==
FD_FILE
){
if
(
f
->
type
==
FD_FILE
){
ilock
(
f
->
ip
);
int
cc
=
readi
(
f
->
ip
,
addr
,
f
->
off
,
n
);
if
(
cc
>
0
)
f
->
off
+=
cc
;
if
((
r
=
readi
(
f
->
ip
,
addr
,
f
->
off
,
n
))
>
0
)
f
->
off
+=
r
;
iunlock
(
f
->
ip
);
return
cc
;
}
else
{
panic
(
"fileread"
);
return
-
1
;
return
r
;
}
panic
(
"fileread"
);
}
//
Close file f. (Decrement ref count, close when reaches 0.)
void
file
close
(
struct
file
*
f
)
//
Write to file f. Addr is kernel address.
int
file
write
(
struct
file
*
f
,
char
*
addr
,
int
n
)
{
acquire
(
&
file_table_lock
);
if
(
f
->
ref
<
1
||
f
->
type
==
FD_CLOSED
)
panic
(
"fileclose"
);
if
(
--
f
->
ref
==
0
){
struct
file
dummy
=
*
f
;
f
->
ref
=
0
;
f
->
type
=
FD_CLOSED
;
release
(
&
file_table_lock
);
int
r
;
if
(
dummy
.
type
==
FD_PIPE
){
pipe_close
(
dummy
.
pipe
,
dummy
.
writable
);
}
else
if
(
dummy
.
type
==
FD_FILE
){
idecref
(
dummy
.
ip
);
}
else
{
panic
(
"fileclose"
);
}
}
else
{
release
(
&
file_table_lock
);
if
(
f
->
writable
==
0
)
return
-
1
;
if
(
f
->
type
==
FD_PIPE
)
return
pipe_write
(
f
->
pipe
,
addr
,
n
);
if
(
f
->
type
==
FD_FILE
){
ilock
(
f
->
ip
);
if
((
r
=
writei
(
f
->
ip
,
addr
,
f
->
off
,
n
))
>
0
)
f
->
off
+=
r
;
iunlock
(
f
->
ip
);
return
r
;
}
panic
(
"filewrite"
);
}
// Get metadata about file f.
...
...
@@ -121,17 +101,35 @@ filestat(struct file *f, struct stat *st)
stati
(
f
->
ip
,
st
);
iunlock
(
f
->
ip
);
return
0
;
}
else
}
return
-
1
;
}
//
Increment ref count for file f.
//
Close file f. (Decrement ref count, close when reaches 0.)
void
file
incref
(
struct
file
*
f
)
file
close
(
struct
file
*
f
)
{
struct
file
ff
;
acquire
(
&
file_table_lock
);
if
(
f
->
ref
<
1
||
f
->
type
==
FD_CLOSED
)
panic
(
"fileincref"
);
f
->
ref
++
;
panic
(
"fileclose"
);
if
(
--
f
->
ref
>
0
){
release
(
&
file_table_lock
);
return
;
}
ff
=
*
f
;
f
->
ref
=
0
;
f
->
type
=
FD_CLOSED
;
release
(
&
file_table_lock
);
if
(
ff
.
type
==
FD_PIPE
)
pipe_close
(
ff
.
pipe
,
ff
.
writable
);
else
if
(
ff
.
type
==
FD_FILE
)
idecref
(
ff
.
ip
);
else
panic
(
"fileclose"
);
}
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论