简体   繁体   English

获取块设备Linux / Windows的大小

[英]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? 我有一个任务来获取块设备的大小,它必须是跨平台应用程序(Linux / Windows),所以我使用Qt,能否使用Qt标准类(例如QFile和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? 它显示“ / dev / sda5 isReadable”并且大小等于零,您能帮我解决这个问题吗?

You have to use statfs on Linux and GetDiskFreeSpaceEx on Windows. 您必须在Linux上使用statfs ,在Windows上使用GetDiskFreeSpaceEx It does not seem that Qt was wrapping that function, but it's rather short piece of code. Qt似乎没有包装该函数,但这只是一小段代码。

Device nodes report size of zero via usual stat call. 设备节点通过常规stat调用报告大小为零。 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. 无论如何,这并不有趣,因为要找出在某个路径上安装了哪个设备是很多特定于Unix的工作。

On the Linux side, there are two utilities that will help with enumerating attached storage partitions, depending on your distro: blkid and vol_id . 在Linux方面,有两个实用程序将帮助枚举附加的存储分区,具体取决于您的发行版: blkidvol_id I've used these to identify possible partitions (and their type) for a Samba server setup script. 我已经使用它们来识别Samba服务器设置脚本的可能分区(及其类型)。 One could redirect their output to a text file and parse the result in your C code. 可以将其输出重定向到文本文件,然后将结果解析为C代码。 HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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