Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Posix how is type dev_t getting used?

Tags:

c

posix

stat

What I am after is the meaning of such type and what interface can use it.

It is explained in Posix spec that dev_t is used for device IDs. However, what device id means for any object described by a path, which can be a file, a directy, a fifo or a physical device?

For example, calling stat() shall give you a struct including a member of such type; and you can stat any kinds of object in your file system. The device id should have different meanings for different file types then.

like image 570
Mengfei Murphy Avatar asked Mar 09 '12 14:03

Mengfei Murphy


2 Answers

The only use of dev_t in the vast majority of programs (ones which are portable and not connected to a single OS) is to determine that two file names or file descriptors refer to the same underlying file. This is true if and only if the st_ino and st_dev entries for the two files' stat structures match one another.

Basically, st_dev tells which "device" (e.g. mounted partition, network share, etc.) the file resides on, and st_ino is a unique identifier of the file within the context of a single device.

like image 194
R.. GitHub STOP HELPING ICE Avatar answered Oct 07 '22 23:10

R.. GitHub STOP HELPING ICE


Actually, there are two dev_t-typed fields in struct stat:

  • st_dev is the "[d]evice ID of device containing file", so if two files have the same st_dev, they're on the same filesystem.
  • st_rdev is the device ID of the device denoted by a character or block special file, i.e. the files commonly encountered in /dev. It has no meaning for other types of files.
like image 38
Fred Foo Avatar answered Oct 07 '22 22:10

Fred Foo