-
Notifications
You must be signed in to change notification settings - Fork 0
/
diskinfo.cpp
49 lines (41 loc) · 1.11 KB
/
diskinfo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <QDir>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
#ifdef Q_OS_LINUX
#include <sys/statvfs.h>
#endif
#include "diskinfo.h"
quint64 diskSize(QString path) {
quint64 free_space;
if (path.isEmpty())
return 0;
QDir dir(path);
if (!dir.exists())
return 0;
free_space = 0;
#ifdef Q_OS_WIN
const wchar_t *encodedPath
= reinterpret_cast<const wchar_t *>(path.utf16());
GetDiskFreeSpaceExW(encodedPath, (PULARGE_INTEGER)&free_space, 0, 0);
#endif
#ifdef Q_OS_LINUX
struct statvfs buf;
if (!statvfs(path.toAscii(), &buf)) {
free_space = buf.f_bfree*buf.f_bsize;
}
#endif
return free_space;
}
QString sizeToStr(quint64 size) {
if (size > 10737418240LL) { //10Gb
return QString::number(size/1024.0/1024.0/1024.0,0,2) + " Gb";
}
if (size > 1048576) { //1Mb
return QString::number(size/1024.0/1024.0,0,2) + " Mb";
}
if (size > 10240) { //10Kb
return QString::number(size/1024.0,0,2) + " kb";
}
return QString::number(size) + " b";
}