Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qstorageinfo_mac.cpp
Go to the documentation of this file.
1// Copyright (C) 2014 Ivan Komissarov <ABBAPOH@gmail.com>
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <QtCore/qfileinfo.h>
8#include <QtCore/private/qcore_mac_p.h>
9
10#include <CoreFoundation/CoreFoundation.h>
11#include <CoreFoundation/CFURLEnumerator.h>
12
13#include <sys/mount.h>
14
15#define QT_STATFSBUF struct statfs
16#define QT_STATFS ::statfs
17
18QT_BEGIN_NAMESPACE
19
20void QStorageInfoPrivate::initRootPath()
21{
22 rootPath = QFileInfo(rootPath).canonicalFilePath();
23
24 if (rootPath.isEmpty())
25 return;
26
27 retrieveUrlProperties(true);
28}
29
30void QStorageInfoPrivate::doStat()
31{
32 initRootPath();
33
34 if (rootPath.isEmpty())
35 return;
36
37 retrieveLabel();
38 retrievePosixInfo();
39 retrieveUrlProperties();
40}
41
42void QStorageInfoPrivate::retrievePosixInfo()
43{
44 QT_STATFSBUF statfs_buf;
45 int result = QT_STATFS(QFile::encodeName(rootPath).constData(), &statfs_buf);
46 if (result == 0) {
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;
51 }
52}
53
54static inline qint64 CFDictionaryGetInt64(CFDictionaryRef dictionary, const void *key)
55{
56 CFNumberRef cfNumber = (CFNumberRef)CFDictionaryGetValue(dictionary, key);
57 if (!cfNumber)
58 return -1;
59 qint64 result;
60 bool ok = CFNumberGetValue(cfNumber, kCFNumberSInt64Type, &result);
61 if (!ok)
62 return -1;
63 return result;
64}
65
66void QStorageInfoPrivate::retrieveUrlProperties(bool initRootPath)
67{
68 static const void *rootPathKeys[] = { kCFURLVolumeURLKey };
69 static const void *propertyKeys[] = {
70 // kCFURLVolumeNameKey, // 10.7
71 // kCFURLVolumeLocalizedNameKey, // 10.7
72 kCFURLVolumeTotalCapacityKey,
73 kCFURLVolumeAvailableCapacityKey,
74 // kCFURLVolumeIsReadOnlyKey // 10.7
75 };
76 size_t size = (initRootPath ? sizeof(rootPathKeys) : sizeof(propertyKeys)) / sizeof(void*);
77 QCFType<CFArrayRef> keys = CFArrayCreate(kCFAllocatorDefault,
78 initRootPath ? rootPathKeys : propertyKeys,
79 size,
80 nullptr);
81
82 if (!keys)
83 return;
84
85 const QCFString cfPath = rootPath;
86 if (initRootPath)
87 rootPath.clear();
88
89 QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
90 cfPath,
91 kCFURLPOSIXPathStyle,
92 true);
93 if (!url)
94 return;
95
96 CFErrorRef error;
97 QCFType<CFDictionaryRef> map = CFURLCopyResourcePropertiesForKeys(url, keys, &error);
98
99 if (!map)
100 return;
101
102 if (initRootPath) {
103 const CFURLRef rootUrl = (CFURLRef)CFDictionaryGetValue(map, kCFURLVolumeURLKey);
104 if (!rootUrl)
105 return;
106
107 rootPath = QCFString(CFURLCopyFileSystemPath(rootUrl, kCFURLPOSIXPathStyle));
108 valid = true;
109 ready = true;
110
111 return;
112 }
113
114 bytesTotal = CFDictionaryGetInt64(map, kCFURLVolumeTotalCapacityKey);
115 bytesAvailable = CFDictionaryGetInt64(map, kCFURLVolumeAvailableCapacityKey);
116 bytesFree = bytesAvailable;
117}
118
119void QStorageInfoPrivate::retrieveLabel()
120{
121 QCFString path = CFStringCreateWithFileSystemRepresentation(0,
122 QFile::encodeName(rootPath).constData());
123 if (!path)
124 return;
125
126 QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, path, kCFURLPOSIXPathStyle, true);
127 if (!url)
128 return;
129
130 QCFType<CFURLRef> volumeUrl;
131 if (!CFURLCopyResourcePropertyForKey(url, kCFURLVolumeURLKey, &volumeUrl, NULL))
132 return;
133
134 QCFString volumeName;
135 if (!CFURLCopyResourcePropertyForKey(url, kCFURLNameKey, &volumeName, NULL))
136 return;
137
138 name = volumeName;
139}
140
141QList<QStorageInfo> QStorageInfoPrivate::mountedVolumes()
142{
143 QList<QStorageInfo> volumes;
144
145 QCFType<CFURLEnumeratorRef> enumerator;
146 enumerator = CFURLEnumeratorCreateForMountedVolumes(nullptr,
147 kCFURLEnumeratorSkipInvisibles,
148 nullptr);
149
150 CFURLEnumeratorResult result = kCFURLEnumeratorSuccess;
151 do {
152 CFURLRef url;
153 CFErrorRef error;
154 result = CFURLEnumeratorGetNextURL(enumerator, &url, &error);
155 if (result == kCFURLEnumeratorSuccess) {
156 const QCFString urlString = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
157 volumes.append(QStorageInfo(urlString));
158 }
159 } while (result != kCFURLEnumeratorEnd);
160
161 return volumes;
162}
163
164QT_END_NAMESPACE
static qint64 CFDictionaryGetInt64(CFDictionaryRef dictionary, const void *key)
#define QT_STATFS
#define QT_STATFSBUF