7#include <QtCore/qfileinfo.h>
8#include <QtCore/private/qcore_mac_p.h>
10#include <CoreFoundation/CoreFoundation.h>
11#include <CoreFoundation/CFURLEnumerator.h>
15#define QT_STATFSBUF struct statfs
16#define QT_STATFS ::statfs
20void QStorageInfoPrivate::initRootPath()
22 rootPath = QFileInfo(rootPath).canonicalFilePath();
24 if (rootPath.isEmpty())
27 retrieveUrlProperties(
true);
30void QStorageInfoPrivate::doStat()
34 if (rootPath.isEmpty())
39 retrieveUrlProperties();
42void QStorageInfoPrivate::retrievePosixInfo()
45 int result =
QT_STATFS(QFile::encodeName(rootPath).constData(), &statfs_buf);
47 device.assign(statfs_buf.f_mntfromname);
48 readOnly = (statfs_buf.f_flags & MNT_RDONLY) != 0;
49 fileSystemType.assign(statfs_buf.f_fstypename);
50 blockSize = statfs_buf.f_bsize;
56 CFNumberRef cfNumber = (CFNumberRef)CFDictionaryGetValue(dictionary, key);
60 bool ok = CFNumberGetValue(cfNumber, kCFNumberSInt64Type, &result);
66void QStorageInfoPrivate::retrieveUrlProperties(
bool initRootPath)
68 static const void *rootPathKeys[] = { kCFURLVolumeURLKey };
69 static const void *propertyKeys[] = {
72 kCFURLVolumeTotalCapacityKey,
73 kCFURLVolumeAvailableCapacityKey,
76 size_t size = (initRootPath ?
sizeof(rootPathKeys) :
sizeof(propertyKeys)) /
sizeof(
void*);
77 QCFType<CFArrayRef> keys = CFArrayCreate(kCFAllocatorDefault,
78 initRootPath ? rootPathKeys : propertyKeys,
85 const QCFString cfPath = rootPath;
89 QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
97 QCFType<CFDictionaryRef> map = CFURLCopyResourcePropertiesForKeys(url, keys, &error);
103 const CFURLRef rootUrl = (CFURLRef)CFDictionaryGetValue(map, kCFURLVolumeURLKey);
107 rootPath = QCFString(CFURLCopyFileSystemPath(rootUrl, kCFURLPOSIXPathStyle));
114 bytesTotal = CFDictionaryGetInt64(map, kCFURLVolumeTotalCapacityKey);
115 bytesAvailable = CFDictionaryGetInt64(map, kCFURLVolumeAvailableCapacityKey);
116 bytesFree = bytesAvailable;
119void QStorageInfoPrivate::retrieveLabel()
121 QCFString path = CFStringCreateWithFileSystemRepresentation(0,
122 QFile::encodeName(rootPath).constData());
126 QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, path, kCFURLPOSIXPathStyle,
true);
130 QCFType<CFURLRef> volumeUrl;
131 if (!CFURLCopyResourcePropertyForKey(url, kCFURLVolumeURLKey, &volumeUrl, NULL))
134 QCFString volumeName;
135 if (!CFURLCopyResourcePropertyForKey(url, kCFURLNameKey, &volumeName, NULL))
141QList<QStorageInfo> QStorageInfoPrivate::mountedVolumes()
143 QList<QStorageInfo> volumes;
145 QCFType<CFURLEnumeratorRef> enumerator;
146 enumerator = CFURLEnumeratorCreateForMountedVolumes(
nullptr,
147 kCFURLEnumeratorSkipInvisibles,
150 CFURLEnumeratorResult result = kCFURLEnumeratorSuccess;
154 result = CFURLEnumeratorGetNextURL(enumerator, &url, &error);
155 if (result == kCFURLEnumeratorSuccess) {
156 const QCFString urlString = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
157 volumes.append(QStorageInfo(urlString));
159 }
while (result != kCFURLEnumeratorEnd);
static qint64 CFDictionaryGetInt64(CFDictionaryRef dictionary, const void *key)