File mode flags S_IRUSR, S_IRUSR.... definitions - c

Where can I find the definitions for file mode flags such as S_IRUSR, S_IRUSR, S_IROTH.. etc...?
I looked on sys/stat.h but couldn't find the same?

On my system they are defined in:
/usr/include/fcntl.h
/usr/include/linux/stat.h
/usr/include/x86_64-linux-gnu/sys/stat.h

Related

O_PATH equivalent in MacOS?

Linux has the O_PATH flag to open() which allows one to get a fd to be used in fstat, fcntl and others without actually opening the file for reading (or having permissions to do so). However the O_PATH flag is Linux specific.
Is there an equivalent to the O_PATH flag to open() in MacOS? For example, how can I use fstat() on a file I don't have read permissions for?
macOS doesn't have an equivalent to O_PATH, so it's impossible to have a reference to a file without opening it. Regarding the one bit of functionality that you mentioned, you can call stat with a given file path as long as you have "execution" rights to its parent directory, regardless of whether you have any rights to that file.

What's the connection between flags and mode in open file function in C

I'm a beginner in C, have a question about the flags and mode paramaters in open file function in C
so C's open function is :
int open(char *filename, int flags, mode_t mode);
and some macros for the flags are:
O_RDONLY: Reading only
O_WRONLY: Writing only
O_RDWR: Reading and writing
and the mode bit is something like:
What I don't understand is,
let say we have a open function as:
fd = Open("foo.txt", O_RDONLY, S_IWOTH);
so O_RDONLY specifies that we can only read the file, but S_IWOTH specifies that anyone can write this file, isn't that they contradict to each other?
The flags decide the properties to be applied during opening of this file at this time (let's call this the "session") - this affects what you can do with the file while it's open (or, more correctly, what you can do with the file descriptor).
The mode decide the properties of the file should it be created as part of the opening process - this affects how anyone can open the file in future.
Your specific example (albeit with the correct open rather than Open):
fd = open("foo.txt", O_RDONLY, S_IWOTH);
is not really relevant since the file won't be created without the O_CREAT flag(a).
However, had you supplied O_CREAT, it's perfectly acceptable to create the file allowing anyone to write to it, but have it opened for this session in read-only mode.
(a) Some systems have other flags which may create the file under some circumstances. For example, Linux has the O_TMPFILE flag.

uv_fs_open: flags and mode on Windows

From the official documentation we have the following signature for uv_fs_open:
int uv_fs_open(uv_loop_t* loop, uv_fs_t* req, const char* path, int flags, int mode, uv_fs_cb cb);
And it is said that it is equivalent to open(2).
From the most accredited tutorial I've found on the web, we have this (emphasis mine):
flags and mode are standard Unix flags. libuv takes care of converting to the appropriate Windows flags.
Because of that, I thought that the following statement would have worked both on Linux and Windows:
uv_fs_open(my_loop, my_req, my_filename, O_RDWR | O_CREAT, S_IRWXU, my_callback);
Actually, it works just fine on Linux.
Anyway, on Windows I receive the following errors:
'O_RDWR': undeclared identifier
'O_CREAT': undeclared identifier
'S_IRWXU': undeclared identifier
Is it the expected result (and thus the tutorial is wrong)?
What should I do to have a call to uv_fs_open that just works on both the platforms?
What are the values for flags and mode to be used on Windows?
To be able to use uv_fs_open on Windows, users have to:
include fcntl.h explicitly, because uv-win.h doesn't include it (see this issue for further details)
use _O_CREAT, _O_RDWR_ and so on instead of O_CREAT, O_RDWR and the others (see the official documentation for further details)
Something similar applies to the mode and details on the available constants can be found in the linked documentation.

Cooking file mode to represent directory in Linux

In Linux we have 'mode' for each file/folder
if we do:
struct stat buf;
fstat("file_or_folder_name", &buf);
Then fstat function will fill the buf with lots of info including st_mode;
As I know this st_mode contains information about permission and the file type(tells if it is a file or a folder)
So I want to know If I have to generate/make/cook an st_mode from scratch, how can I make sure that it will tell it's
a file and some other time it will tell that it is a folder? That type I will know by doing: S_ISDIR(buf.st_mode)
ex:
mode_t my_file_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
but that only tells about permission.
I want to make it to tell the file type also.
Also I have to fill other members of: struct stat
So what data I can assign to st_dev and st_ino etc any idea?
Actually the file does not exist on disk, I will be reading the objects from cloud, and I will tell the caller if its a file or a folder along with other info.
I think you are looking for S_IFMT
Its available in mode_t.
Predefined values are:
S_IFBLK Block special.
S_IFCHR Character special.
S_IFIFO FIFO special.
S_IFREG Regular.
S_IFDIR Directory.
S_IFLNK Symbolic link.
S_IFSOCK Socket.
Reference

In which header file are EINVAL, ENOMEM, etc. defined in Linux?

It’s said that the error numbers like EINVAL, ENOMEM, etc. are defined in errno.h, but I can’t find them in errno.h, I also searched some directories under /usr/include, still can’t find them. I can use these macros without any issue in my C code. Anyone can tell me where are them?
It is defined either directly in errno.h or in a file included (directly or indirectly) by errno.h.
I searched for it using the following command:
find /usr/include | xargs grep ENOMEM | grep '#define'
and I found a match in /usr/include/asm-generic/errno-base.h in my linux (RHEL 6).
It's up to the implementation of the standard C library.
All that is certain is that <errno.h> is the top-level header that application code should use.
One way of figuring out is to trace an invocation of the compiler.
You can run locate errno.h | xargs grep EINVAL to find the location
On my Ubuntu 12.04 machine, its in /usr/lib/syslinux/com32/include/errno.h

Resources