MythTV  master
mythcoreutil.cpp
Go to the documentation of this file.
1 #include "mythcoreutil.h"
2 
3 // POSIX
4 #include <unistd.h>
5 #include <fcntl.h>
6 
7 // System specific C headers
8 #include "compat.h"
9 #include <QtGlobal>
10 
11 #ifdef __linux__
12 #include <sys/vfs.h>
13 #include <sys/sysinfo.h>
14 #endif
15 
16 #ifdef Q_OS_DARWIN
17 #include <mach/mach.h>
18 #endif
19 
20 #ifdef BSD
21 #include <sys/mount.h> // for struct statfs
22 #include <sys/sysctl.h>
23 #endif
24 
25 // Qt headers
26 #include <QByteArray>
27 
33 int64_t getDiskSpace(const QString &file_on_disk,
34  int64_t &total, int64_t &used)
35 {
36  struct statfs statbuf {};
37  int64_t freespace = -1;
38  QByteArray cstr = file_on_disk.toLocal8Bit();
39 
40  total = used = -1;
41 
42  // there are cases where statfs will return 0 (good), but f_blocks and
43  // others are invalid and set to 0 (such as when an automounted directory
44  // is not mounted but still visible because --ghost was used),
45  // so check to make sure we can have a total size > 0
46  if ((statfs(cstr.constData(), &statbuf) == 0) &&
47  (statbuf.f_blocks > 0) &&
48  (statbuf.f_bsize > 0))
49  {
50  total = statbuf.f_blocks;
51  total *= statbuf.f_bsize;
52  total = total >> 10;
53 
54  freespace = statbuf.f_bavail;
55  freespace *= statbuf.f_bsize;
56  freespace = freespace >> 10;
57 
58  used = total - freespace;
59  }
60 
61  return freespace;
62 }
63 
64 /* vim: set expandtab tabstop=4 shiftwidth=4: */
65 
getDiskSpace
int64_t getDiskSpace(const QString &file_on_disk, int64_t &total, int64_t &used)
Definition: mythcoreutil.cpp:33
mythcoreutil.h
statfs
int statfs(const char *path, struct statfs *buffer)
Definition: compat.h:105
compat.h
statfs::f_blocks
long f_blocks
Definition: compat.h:96
statfs
Definition: compat.h:93
statfs::f_bsize
long f_bsize
Definition: compat.h:95
statfs::f_bavail
long f_bavail
Definition: compat.h:98