views:

43

answers:

2

I am porting from Linux to FreeBSD and have run into ::mknod() failing with errno:

[EINVAL]           Creating anything else than a block or character spe-
                   cial file (or a whiteout) is not supported.

But I also see it states earlier on the man page:

 The mknod() system call requires super-user privileges.

So what would be a good replacement call to use that will work on both Linux and FreeBSD?

My code snippet where this occurs:

mode_t mode
  = S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;

if (::mknod(_resolvedName, mode, 0)) {

My objective is to create an empty file with the correct permissions.

+1  A: 

My objective is to create an empty file with the correct permissions.

Why not:

std::fstream(_resolvedName); // Temporary object opens and closes in the
                             // same statement thus creating empty file.
Martin York
+2  A: 

According to the Linux man page for mknod(2):

POSIX.1-2001 says: "The only portable use of mknod() is to create a FIFO-special file. If mode is not S_IFIFO or dev is not 0, the behavior of mknod() is unspecified."

So your use of it in this manner is non-portable and not recommended.

open(2), however, seems to have the functionality you need, and will create a file with zero size, portably. It accepts a third parameter of type mode_t, with which you can set permissions.

greyfade