提交 bfdbe916 创建 作者: Nickolai Zeldovich's avatar Nickolai Zeldovich

sequential du

上级 9cc8199d
UPROGS= \ UPROGS= \
cat \ cat \
du \
echo \ echo \
init \ init \
forkexectree \ forkexectree \
......
#include "types.h"
#include "stat.h"
#include "user.h"
#include "fs.h"
static int
du(int fd)
{
struct stat st;
if (fstat(fd, &st) < 0) {
fprintf(2, "du: cannot stat\n");
close(fd);
return 0;
}
int size = st.size;
if (st.type == T_DIR) {
struct dirent de;
while (read(fd, &de, sizeof(de)) == sizeof(de)) {
if (de.inum == 0)
continue;
char buf[DIRSIZ+1];
memmove(buf, de.name, DIRSIZ);
buf[DIRSIZ] = 0;
if (!strcmp(buf, ".") || !strcmp(buf, ".."))
continue;
int nfd = openat(fd, buf, 0);
if (nfd >= 0)
size += du(nfd); // should go into work queue
}
}
close(fd);
return size;
}
int
main(int ac, char **av)
{
printf("%d\n", du(open(".", 0)));
exit();
}
...@@ -15,6 +15,7 @@ filealloc(void) ...@@ -15,6 +15,7 @@ filealloc(void)
{ {
struct file *f = (file*) kmalloc(sizeof(struct file)); struct file *f = (file*) kmalloc(sizeof(struct file));
f->ref = 1; f->ref = 1;
f->type = file::FD_NONE;
return f; return f;
} }
...@@ -40,7 +41,7 @@ fileclose(struct file *f) ...@@ -40,7 +41,7 @@ fileclose(struct file *f)
iput(f->ip); iput(f->ip);
else if(f->type == file::FD_SOCKET) else if(f->type == file::FD_SOCKET)
netclose(f->socket); netclose(f->socket);
else else if(f->type != file::FD_NONE)
panic("fileclose bad type"); panic("fileclose bad type");
kmfree(f, sizeof(struct file)); kmfree(f, sizeof(struct file));
} }
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论