Sumit Sumit - 1 year ago 63
C Question

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)



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.

Answer Source

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.