📄 dir.c
字号:
#include <linux/fs.h>
#include <linux/buffer_head.h>
#include "xbfs.h"
int dir_sanity_check(ino_t ino, struct xbfs_inode_disk *id)
{
if (!S_ISDIR(id->mode)) {
DPRINT("inode %ld is fucked. mode is not dir\n", ino);
return 1;
}
return 0;
}
static int xbfs_readdir(struct file * filp, void * dirent, filldir_t filldir)
{
struct inode *inode;
struct xbfs_inode_disk *id;
struct buffer_head *bh;
struct xbfs_dirent *ent;
unsigned long pos = filp->f_pos;
unsigned long i;
int err;
DPRINT("pos %ld\n", pos);
inode = filp->f_dentry->d_inode;
bh = sb_bread(inode->i_sb, inode->i_ino);
if (!bh) {
DPRINT("sb_bread inode %ld error\n", inode->i_ino);
return -EINVAL;
}
err = -EINVAL;
id = (typeof(id))bh->b_data;
if (dir_sanity_check(inode->i_ino, id))
goto out_relse;
i = pos;
pos = 0;
for_each_dirent(id, ent, {
int len;
if (pos < i)
continue;
len = strnlen(ent->name, sizeof(ent->name) - 1);
ent->name[len] = 0;
DPRINT("filldir name %s ino %d\n", ent->name, ent->block);
err = filldir(dirent, ent->name, len, pos, ent->block, DT_UNKNOWN);
if (err)
goto done;
pos++;
});
err = 0;
done:
filp->f_pos = pos;
out_relse:
brelse(bh);
return err;
}
const struct file_operations xbfs_dir_operations = {
.read = generic_read_dir,
.readdir = xbfs_readdir
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -