Reputation: 121
In C I'm using 'open()' to create and open files:
fd = open(fileName, O_CREAT | O_RDWR, 0644);
if 'fileName' includes a directory the file is created only if the directory exists and the application has permission to write to this directory.
If the directory does not exists 'open()' returns an error and I would like to create the directory.
With makedir (see below) I am able to create the directory.
int mkdir(const char *path, mode_t mode);
I was wondering, are there other ways to create a directory? I believe we can also use ioctl()? Are there more alternatives?
I'm asking this because in our code we are not allowed to use mkdir.
Upvotes: 2
Views: 7382
Reputation: 646
A kernel driver can create a folder using
struct file *fp = (struct file *) NULL;
fp = filp_open("/home/testdir", O_DIRECTORY|O_CREAT, S_IRUSR);
I can imagine some situations when a "make_dir" driver which creates large number of folders will be faster than calling mkdir for every folder.
Upvotes: 0
Reputation: 36630
I was wondering, are there other ways to create a directory? I believe we can also use ioctl()?
At least on Linux and HP-UX, mkdir
is a system call on its own.
So, No, there is no other way to create a directory, at least not on Linux - other systems might implement directory creation differently. But on any reasonable system, I would expect directory creation to be a privileged operation, so you need to go through the kernel.
The only slightly different approach would be to execute the system call directly, by bypassing the C
runtime library. In assembly nasm syntax on an i386 architecture, this would look something like
mkdir:
mov eax, 39 ; System-call "sys_mkdir"
mov ebx, dirName ; directory name to create
mov ecx, 0x755 ; creation mode
int 80H ; call sys_mkdir
However, this is not something you would normally do since it is completely system dependant. Switching to a different CPU would require different register setup and/or different instructions - that is what the C runtime library encapsulates.
See also
Upvotes: 5