Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "qstorageinfo_p.h"
5
6#include <QtCore/qfileinfo.h>
7#include <QtCore/private/qcore_mac_p.h>
8
9#include <CoreFoundation/CoreFoundation.h>
10#include <CoreFoundation/CFURLEnumerator.h>
11
12#include <sys/mount.h>
13
14#define QT_STATFSBUF struct statfs
15#define QT_STATFS ::statfs
16
18
19void QStorageInfoPrivate::initRootPath()
20{
21 rootPath = QFileInfo(rootPath).canonicalFilePath();
22
23 if (rootPath.isEmpty())
24 return;
25
26 retrieveUrlProperties(true);
27}
28
29void QStorageInfoPrivate::doStat()
30{
31 initRootPath();
32
33 if (rootPath.isEmpty())
34 return;
35
37 retrievePosixInfo();
38 retrieveUrlProperties();
39}
40
41void QStorageInfoPrivate::retrievePosixInfo()
42{
43 QT_STATFSBUF statfs_buf;
44 int result = QT_STATFS(QFile::encodeName(rootPath).constData(), &statfs_buf);
45 if (result == 0) {
46 device.assign(statfs_buf.f_mntfromname);
47 readOnly = (statfs_buf.f_flags & MNT_RDONLY) != 0;
48 fileSystemType.assign(statfs_buf.f_fstypename);
49 blockSize = statfs_buf.f_bsize;
50 }
51}
52
53static inline qint64 CFDictionaryGetInt64(CFDictionaryRef dictionary, const void *key)
54{
55 CFNumberRef cfNumber = (CFNumberRef)CFDictionaryGetValue(dictionary, key);
56 if (!cfNumber)
57 return -1;
59 bool ok = CFNumberGetValue(cfNumber, kCFNumberSInt64Type, &result);
60 if (!ok)
61 return -1;
62 return result;
63}
64
65void QStorageInfoPrivate::retrieveUrlProperties(bool initRootPath)
66{
67 static const void *rootPathKeys[] = { kCFURLVolumeURLKey };
68 static const void *propertyKeys[] = {
69 // kCFURLVolumeNameKey, // 10.7
70 // kCFURLVolumeLocalizedNameKey, // 10.7
71 kCFURLVolumeTotalCapacityKey,
72 kCFURLVolumeAvailableCapacityKey,
73 // kCFURLVolumeIsReadOnlyKey // 10.7
74 };
75 size_t size = (initRootPath ? sizeof(rootPathKeys) : sizeof(propertyKeys)) / sizeof(void*);
76 QCFType<CFArrayRef> keys = CFArrayCreate(kCFAllocatorDefault,
77 initRootPath ? rootPathKeys : propertyKeys,
78 size,
79 nullptr);
80
81 if (!keys)
82 return;
83
84 const QCFString cfPath = rootPath;
85 if (initRootPath)
86 rootPath.clear();
87
88 QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
89 cfPath,
90 kCFURLPOSIXPathStyle,
91 true);
92 if (!url)
93 return;
94
95 CFErrorRef error;
96 QCFType<CFDictionaryRef> map = CFURLCopyResourcePropertiesForKeys(url, keys, &error);
97
98 if (!map)
99 return;
100
101 if (initRootPath) {
102 const CFURLRef rootUrl = (CFURLRef)CFDictionaryGetValue(map, kCFURLVolumeURLKey);
103 if (!rootUrl)
104 return;
105
106 rootPath = QCFString(CFURLCopyFileSystemPath(rootUrl, kCFURLPOSIXPathStyle));
107 valid = true;
108 ready = true;
109
110 return;
111 }
112
113 bytesTotal = CFDictionaryGetInt64(map, kCFURLVolumeTotalCapacityKey);
114 bytesAvailable = CFDictionaryGetInt64(map, kCFURLVolumeAvailableCapacityKey);
115 bytesFree = bytesAvailable;
116}
117
118void QStorageInfoPrivate::retrieveLabel()
119{
120 QCFString path = CFStringCreateWithFileSystemRepresentation(0,
121 QFile::encodeName(rootPath).constData());
122 if (!path)
123 return;
124
125 QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, path, kCFURLPOSIXPathStyle, true);
126 if (!url)
127 return;
128
129 QCFType<CFURLRef> volumeUrl;
130 if (!CFURLCopyResourcePropertyForKey(url, kCFURLVolumeURLKey, &volumeUrl, NULL))
131 return;
132
133 QCFString volumeName;
134 if (!CFURLCopyResourcePropertyForKey(url, kCFURLNameKey, &volumeName, NULL))
135 return;
136
137 name = volumeName;
138}
139
140QList<QStorageInfo> QStorageInfoPrivate::mountedVolumes()
141{
142 QList<QStorageInfo> volumes;
143
144 QCFType<CFURLEnumeratorRef> enumerator;
145 enumerator = CFURLEnumeratorCreateForMountedVolumes(nullptr,
146 kCFURLEnumeratorSkipInvisibles,
147 nullptr);
148
149 CFURLEnumeratorResult result = kCFURLEnumeratorSuccess;
150 do {
151 CFURLRef url;
152 CFErrorRef error;
153 result = CFURLEnumeratorGetNextURL(enumerator, &url, &error);
154 if (result == kCFURLEnumeratorSuccess) {
155 const QCFString urlString = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
156 volumes.append(QStorageInfo(urlString));
157 }
158 } while (result != kCFURLEnumeratorEnd);
159
160 return volumes;
161}
162
IOBluetoothDevice * device
QString canonicalFilePath() const
Returns the file system entry's canonical path, including the entry's name, that is,...
static QByteArray encodeName(const QString &fileName)
Converts fileName to an 8-bit encoding that you can use in native APIs.
Definition qfile.h:158
\inmodule QtCore
QMap< QString, QString > map
[6]
Combined button and popup list for selecting options.
const int blockSize
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
DBusConnection const char DBusError * error
GLuint64 key
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint name
GLsizei const GLchar *const * path
GLuint64EXT * result
[6]
static QString retrieveLabel(const QStorageInfoPrivate &d, quint64 deviceId)
static qint64 CFDictionaryGetInt64(CFDictionaryRef dictionary, const void *key)
#define QT_STATFS
#define QT_STATFSBUF
long long qint64
Definition qtypes.h:60
QFuture< QSet< QString > > dictionary
QStringList keys
QUrl url("example.com")
[constructor-url-reference]
QObject::connect nullptr