Reputation: 612
Simple questions: in Linux, I stat()
a file that is not a device. what is the expected value of the st_rdev
field? Is it ok for me to run major(stat.st_rdev)
and minor(stat.st_rdev)
? If so, what are the expected return values?
Code example:
struct stat sstat = stat("myfileordevice");
ProcessFileOrDevice("myfileordevice",
sstat.st_mode,
major(stat.st_rdev),
minor(stat.st_rdev));
Upvotes: 2
Views: 8045
Reputation: 39426
What value
stat.st_rdev
should have if Istat()
a non-device filesystem entry?
It depends on the filesystem that the entry is on. The internal structures and functions in Linux (such asfs/stat.c
) allow each filesystem to define the values.
In general, the value should be zero. Quick testing (stat -c '%t:%T' some-files-and-directories
) indicates it tends to be. I just cannot find any guarantees or claims it should be zero.
Is it safe to use
major(stat.st_rdev)
and minor(stat.st_rdev)
Of course; they only apply some arithmetic to the specified value, and return the result.
Personally, I'd be inclined to use something like
unsigned int maj, min;
struct stat info;
/* stat(), fstat(), fstatat() or something
to fill the info buffer */
if (S_ISCHR(info.st_mode) || S_ISBLK(info.st_mode)) {
maj = major(info.st_rdev);
min = minor(info.sr_rdev);
} else {
maj = 0U;
min = 0U;
}
ProcessFileOrDevice("myfileordevice", info.st_mode, maj, min);
just to make sure. Note that the ProcessFileOrDevice()
function could just as well do the check itself, and ignore maj
and min
values unless S_ISCHR(info.st_mode)
or S_ISBLK(info.st_mode)
values are true.
The man 2 stat
man page contains further useful details.
Upvotes: 4