简体   繁体   中英

Get size of block device Linux/Windows

I have a task to get size of block devices, it must be cross-platform application (Linux/Windows) so I use Qt, can I get file size of block devices usin Qt standard classes such as QFile and QFileInfo? My sketch program doesn't work correct:

#include <QtCore/QCoreApplication>
#include <iostream>
#include <QFileInfo>

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);
   QFileInfo info("/dev/sda5");
   if(info.isReadable())
       std::cout<<"/dev/sda1 isReadable\n";
   else
       std::cout<<"Cant read /dev/sda5";

   std::cout<<info.size()<<"\n";

   return a.exec();
}

It shows "/dev/sda5 isReadable" and size equals zero, can you help me with this problem?

You have to use statfs on Linux and GetDiskFreeSpaceEx on Windows. It does not seem that Qt was wrapping that function, but it's rather short piece of code.

Device nodes report size of zero via usual stat call. That's a fact and you can't do much about it. It's not interesting anyway, because it's quite a lot of unix-specific work to find out which device is mounted on some path.

On the Linux side, there are two utilities that will help with enumerating attached storage partitions, depending on your distro: blkid and vol_id . I've used these to identify possible partitions (and their type) for a Samba server setup script. One could redirect their output to a text file and parse the result in your C code. HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM