Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xv6-public
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
问题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
银宸时代
OS Lab Group
奖励实验
xv6-public
提交
ab5c2dbb
提交
ab5c2dbb
8月 09, 2007
创建
作者:
rsc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Clearer namei
上级
806f4c11
显示空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
106 行增加
和
69 行删除
+106
-69
fs.c
fs.c
+106
-69
没有找到文件。
fs.c
浏览文件 @
ab5c2dbb
...
...
@@ -476,6 +476,73 @@ writei(struct inode *ip, char *addr, uint off, uint n)
}
}
// Skip over the next path element in path,
// saving it in *name and its length in *len.
// Return a pointer to the element after that
// (after any trailing slashes).
// Thus the caller can check whether *path=='\0'
// to see whether the name just removed was
// the last one.
// If there is no name to remove, return 0.
//
// Examples:
// skipelem("a/bb/c") = "bb/c", with *name = "a/bb/c", len=1
// skipelem("///a/bb") = "b", with *name="a/bb", len=1
// skipelem("") = skipelem("////") = 0
//
static
char
*
skipelem
(
char
*
path
,
char
**
name
,
int
*
len
)
{
while
(
*
path
==
'/'
)
path
++
;
if
(
*
path
==
0
)
return
0
;
*
name
=
path
;
while
(
*
path
!=
'/'
&&
*
path
!=
0
)
path
++
;
*
len
=
path
-
*
name
;
while
(
*
path
==
'/'
)
path
++
;
return
path
;
}
// Look for a directory entry in a directory.
// If not found, return -1.
// If found:
// set *poff to the byte offset of the directory entry
// set *pinum to the inode number
// return 0.
static
int
lookup
(
struct
inode
*
dp
,
char
*
name
,
int
namelen
,
uint
*
poff
,
uint
*
pinum
)
{
uint
off
;
struct
buf
*
bp
;
struct
dirent
*
de
;
if
(
dp
->
type
!=
T_DIR
)
return
-
1
;
for
(
off
=
0
;
off
<
dp
->
size
;
off
+=
BSIZE
){
bp
=
bread
(
dp
->
dev
,
bmap
(
dp
,
off
/
BSIZE
));
for
(
de
=
(
struct
dirent
*
)
bp
->
data
;
de
<
(
struct
dirent
*
)
(
bp
->
data
+
BSIZE
);
de
++
){
if
(
de
->
inum
==
0
)
continue
;
if
(
memcmp
(
name
,
de
->
name
,
namelen
)
==
0
&&
(
namelen
==
DIRSIZ
||
de
->
name
[
namelen
]
==
0
)){
// entry matches path element
*
poff
=
off
+
(
uchar
*
)
de
-
bp
->
data
;
*
pinum
=
de
->
inum
;
brelse
(
bp
);
return
0
;
}
}
brelse
(
bp
);
}
return
-
1
;
}
// look up a path name, in one of three modes.
// NAMEI_LOOKUP: return locked target inode.
// NAMEI_CREATE: return locked parent inode.
...
...
@@ -491,12 +558,10 @@ namei(char *path, int mode, uint *ret_off,
{
struct
inode
*
dp
;
struct
proc
*
p
=
curproc
[
cpu
()];
char
*
cp
=
path
,
*
cp1
;
char
*
name
;
int
namelen
;
uint
off
,
dev
;
struct
buf
*
bp
;
struct
dirent
*
ep
;
int
i
,
l
,
atend
;
uint
ninum
;
uint
inum
;
if
(
ret_off
)
*
ret_off
=
0xffffffff
;
...
...
@@ -505,91 +570,57 @@ namei(char *path, int mode, uint *ret_off,
if
(
ret_ip
)
*
ret_ip
=
0
;
if
(
*
cp
==
'/'
)
if
(
*
path
==
'/'
)
dp
=
iget
(
rootdev
,
1
);
else
{
dp
=
iincref
(
p
->
cwd
);
ilock
(
dp
);
}
for
(;;){
while
(
*
cp
==
'/'
)
cp
++
;
if
(
*
cp
==
'\0'
){
if
(
mode
==
NAMEI_LOOKUP
)
return
dp
;
if
(
mode
==
NAMEI_CREATE
&&
ret_ip
){
*
ret_ip
=
dp
;
return
0
;
}
iput
(
dp
);
return
0
;
}
if
(
dp
->
type
!=
T_DIR
){
iput
(
dp
);
return
0
;
}
while
((
path
=
skipelem
(
path
,
&
name
,
&
namelen
))
!=
0
){
// Truncate names in path to DIRSIZ chars.
if
(
namelen
>
DIRSIZ
)
namelen
=
DIRSIZ
;
for
(
i
=
0
;
cp
[
i
]
!=
0
&&
cp
[
i
]
!=
'/'
;
i
++
)
;
l
=
i
;
if
(
i
>
DIRSIZ
)
l
=
DIRSIZ
;
if
(
dp
->
type
!=
T_DIR
)
goto
fail
;
for
(
off
=
0
;
off
<
dp
->
size
;
off
+=
BSIZE
){
bp
=
bread
(
dp
->
dev
,
bmap
(
dp
,
off
/
BSIZE
));
for
(
ep
=
(
struct
dirent
*
)
bp
->
data
;
ep
<
(
struct
dirent
*
)
(
bp
->
data
+
BSIZE
);
ep
++
){
if
(
ep
->
inum
==
0
)
continue
;
if
(
memcmp
(
cp
,
ep
->
name
,
l
)
==
0
&&
(
l
==
DIRSIZ
||
ep
->
name
[
l
]
==
0
)){
// entry matches path element
off
+=
(
uchar
*
)
ep
-
bp
->
data
;
ninum
=
ep
->
inum
;
brelse
(
bp
);
cp
+=
i
;
goto
found
;
}
}
brelse
(
bp
);
}
atend
=
1
;
for
(
cp1
=
cp
;
*
cp1
;
cp1
++
)
if
(
*
cp1
==
'/'
)
atend
=
0
;
if
(
mode
==
NAMEI_CREATE
&&
atend
){
if
(
*
cp
==
'\0'
){
iput
(
dp
);
return
0
;
}
*
ret_last
=
cp
;
if
(
lookup
(
dp
,
name
,
namelen
,
&
off
,
&
inum
)
<
0
){
if
(
mode
==
NAMEI_CREATE
&&
*
path
==
'\0'
){
*
ret_last
=
name
;
return
dp
;
}
goto
fail
;
}
iput
(
dp
);
return
0
;
found:
if
(
mode
==
NAMEI_DELETE
&&
*
cp
==
'\0'
){
if
(
mode
==
NAMEI_DELETE
&&
*
path
==
'\0'
){
// can't unlink . and ..
if
((
i
==
1
&&
memcmp
(
cp
-
1
,
"."
,
1
)
==
0
)
||
(
i
==
2
&&
memcmp
(
cp
-
2
,
".."
,
2
)
==
0
)){
iput
(
dp
);
return
0
;
if
((
namelen
==
1
&&
memcmp
(
name
,
"."
,
1
)
==
0
)
||
(
namelen
==
2
&&
memcmp
(
name
,
".."
,
2
)
==
0
)){
goto
fail
;
}
*
ret_off
=
off
;
return
dp
;
}
dev
=
dp
->
dev
;
iput
(
dp
);
dp
=
iget
(
dev
,
n
inum
);
dp
=
iget
(
dev
,
inum
);
if
(
dp
->
type
==
0
||
dp
->
nlink
<
1
)
panic
(
"namei"
);
}
if
(
mode
==
NAMEI_LOOKUP
)
return
dp
;
if
(
mode
==
NAMEI_CREATE
&&
ret_ip
){
*
ret_ip
=
dp
;
return
0
;
}
goto
fail
;
fail:
iput
(
dp
);
return
0
;
}
// Write a new directory entry (name, ino) into the directory dp.
...
...
@@ -676,6 +707,12 @@ unlink(char *cp)
if
(
readi
(
dp
,
(
char
*
)
&
de
,
off
,
sizeof
(
de
))
!=
sizeof
(
de
)
||
de
.
inum
==
0
)
panic
(
"unlink no entry"
);
// Cannot remove "." or ".." - the 2 and 3 count the trailing NUL.
if
(
memcmp
(
de
.
name
,
"."
,
2
)
==
0
||
memcmp
(
de
.
name
,
".."
,
3
)
==
0
){
iput
(
dp
);
return
-
1
;
}
inum
=
de
.
inum
;
memset
(
&
de
,
0
,
sizeof
(
de
));
...
...
编写
预览
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论