Getting disk label in Linux in C/C++ [duplicate]

2019-09-01 18:51发布

问题:

Possible Duplicate:
How to get drive label in Linux using C from userspace

How can I obtain label of a disk by its file name (/dev/sda1, e.g.) in a program written in C/C++?

回答1:

You can code the C or C++ equivalent of this command:

find -L /dev/disk/by-label -inum $(stat -c %i /dev/sda1) -print

That is, stat() the device file you care about and remember its inode number. Iterate over all of the files in /dev/disk/by-label, and stat() each of them. When the inode number matches, then the name of the matched file is the label of that disk.

If it were me, I'd code the above algorithm in C++, using Boost.Filesystem.



标签: c++ c linux