-
Notifications
You must be signed in to change notification settings - Fork 344
/
mythcoreutil.cpp
65 lines (51 loc) · 1.58 KB
/
mythcoreutil.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "mythcoreutil.h"
// POSIX
#include <unistd.h>
#include <fcntl.h>
// System specific C headers
#include "compat.h"
#include <QtGlobal>
#ifdef __linux__
#include <sys/vfs.h>
#include <sys/sysinfo.h>
#endif
#ifdef Q_OS_DARWIN
#include <mach/mach.h>
#endif
#ifdef BSD
#include <sys/mount.h> // for struct statfs
#include <sys/sysctl.h>
#endif
// Qt headers
#include <QByteArray>
/** \fn getDiskSpace(const QString&,long long&,long long&)
* \brief Returns free space on disk containing file in KiB,
* or -1 if it does not succeed.
* \param file_on_disk file on the file system we wish to stat.
*/
int64_t getDiskSpace(const QString &file_on_disk,
int64_t &total, int64_t &used)
{
struct statfs statbuf {};
int64_t freespace = -1;
QByteArray cstr = file_on_disk.toLocal8Bit();
total = used = -1;
// there are cases where statfs will return 0 (good), but f_blocks and
// others are invalid and set to 0 (such as when an automounted directory
// is not mounted but still visible because --ghost was used),
// so check to make sure we can have a total size > 0
if ((statfs(cstr.constData(), &statbuf) == 0) &&
(statbuf.f_blocks > 0) &&
(statbuf.f_bsize > 0))
{
total = statbuf.f_blocks;
total *= statbuf.f_bsize;
total = total >> 10;
freespace = statbuf.f_bavail;
freespace *= statbuf.f_bsize;
freespace = freespace >> 10;
used = total - freespace;
}
return freespace;
}
/* vim: set expandtab tabstop=4 shiftwidth=4: */