In Posix how is type dev_t getting used?

Mengfei Murphy picture Mengfei Murphy · Mar 9, 2012 · Viewed 10.1k times · Source

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.

Answer

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.