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

2019-09-01 18:57发布

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++?

标签: c++ c linux
1条回答
老娘就宠你
2楼-- · 2019-09-01 19:24

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.

查看更多
登录 后发表回答