简体   繁体   English

Linux Kernel 模块 - 创建目录

[英]Linux Kernel Module - Create Directory

I've got a problem about creating a directory within a linux kernel module.我在 linux kernel 模块中创建目录时遇到问题。

What I want: Creating a directory within a kernel module.我想要的:在 kernel 模块中创建一个目录。

Here is my actual code:这是我的实际代码:

struct file *fp = (struct file *) NULL;
fp = filp_open("/home/testdir", O_DIRECTORY|O_CREAT, S_IRUSR);

But it creates a file instead of directory.但它创建一个文件而不是目录。

I tried to same code as above without the flag "O_DIRECTORY":我尝试在没有标志“O_DIRECTORY”的情况下使用与上面相同的代码:

struct file *fp = (struct file *) NULL;
fp = filp_open("/home/testdir", O_CREAT, S_IRUSR);

And the result is similar to the previous result.结果与之前的结果相似。

I don't understand the behaviour.我不明白这种行为。 What am I doing wrong?我究竟做错了什么?

Edit 1: I am coding on a Raspberry PI, Raspbian, kernel version: 4.4.43-v7编辑 1:我在 Raspberry PI,Raspbian,kernel 版本上编码:4.4.43-v7

I got it by myself. 我自己得到了它。 The solution is: 解决方案是:

struct file *fp = (struct file *) NULL;
fp = filp_open("/home/testdir/", O_DIRECTORY|O_CREAT, S_IRUSR);

Note the "/" at the end of the path. 注意路径末尾的“/”。

Thanks @all for the try! 谢谢@all的尝试!

这不会创建目录,但会打开像opendir那样的目录(该目录应该存在)。

something that worked for me: its also similar to how they create a dir in devtmpfs对我有用的东西:它也类似于他们在 devtmpfs 中创建目录的方式

int mkdir_(const char *dirPath, umode_t mode) { int mkdir_(const char *dirPath, umode_t 模式) {

struct path path;
struct dentry *dentry = kern_path_create(AT_FDCWD, dirPath, &path, mode);

int err = PTR_ERR(dentry);
if (IS_ERR(dentry)) {
    return -1;
}

err = vfs_mkdir(path.dentry->d_inode, dentry, mode);
done_path_create(&path, dentry);

return err;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM