I've got a problem about creating a directory within a linux kernel module.
What I want: Creating a directory within a kernel module.
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":
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
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!
这不会创建目录,但会打开像opendir
那样的目录(该目录应该存在)。
something that worked for me: its also similar to how they create a dir in devtmpfs
int mkdir_(const char *dirPath, umode_t mode) {
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.