Warning: That file was not part of the compilation database. It may have many parsing errors.

1/****************************************************************************
2**
3** Copyright (C) 2014 Ivan Komissarov <ABBAPOH@gmail.com>
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qstorageinfo_p.h"
41
42#include <QtCore/qfileinfo.h>
43#include <QtCore/private/qcore_mac_p.h>
44
45#include <CoreFoundation/CoreFoundation.h>
46#include <CoreFoundation/CFURLEnumerator.h>
47
48#include <sys/mount.h>
49
50#define QT_STATFSBUF struct statfs
51#define QT_STATFS ::statfs
52
53QT_BEGIN_NAMESPACE
54
55void QStorageInfoPrivate::initRootPath()
56{
57 rootPath = QFileInfo(rootPath).canonicalFilePath();
58
59 if (rootPath.isEmpty())
60 return;
61
62 retrieveUrlProperties(true);
63}
64
65void QStorageInfoPrivate::doStat()
66{
67 initRootPath();
68
69 if (rootPath.isEmpty())
70 return;
71
72 retrieveLabel();
73 retrievePosixInfo();
74 retrieveUrlProperties();
75}
76
77void QStorageInfoPrivate::retrievePosixInfo()
78{
79 QT_STATFSBUF statfs_buf;
80 int result = QT_STATFS(QFile::encodeName(rootPath).constData(), &statfs_buf);
81 if (result == 0) {
82 device = QByteArray(statfs_buf.f_mntfromname);
83 readOnly = (statfs_buf.f_flags & MNT_RDONLY) != 0;
84 fileSystemType = QByteArray(statfs_buf.f_fstypename);
85 blockSize = statfs_buf.f_bsize;
86 }
87}
88
89static inline qint64 CFDictionaryGetInt64(CFDictionaryRef dictionary, const void *key)
90{
91 CFNumberRef cfNumber = (CFNumberRef)CFDictionaryGetValue(dictionary, key);
92 if (!cfNumber)
93 return -1;
94 qint64 result;
95 bool ok = CFNumberGetValue(cfNumber, kCFNumberSInt64Type, &result);
96 if (!ok)
97 return -1;
98 return result;
99}
100
101void QStorageInfoPrivate::retrieveUrlProperties(bool initRootPath)
102{
103 static const void *rootPathKeys[] = { kCFURLVolumeURLKey };
104 static const void *propertyKeys[] = {
105 // kCFURLVolumeNameKey, // 10.7
106 // kCFURLVolumeLocalizedNameKey, // 10.7
107 kCFURLVolumeTotalCapacityKey,
108 kCFURLVolumeAvailableCapacityKey,
109 // kCFURLVolumeIsReadOnlyKey // 10.7
110 };
111 size_t size = (initRootPath ? sizeof(rootPathKeys) : sizeof(propertyKeys)) / sizeof(void*);
112 QCFType<CFArrayRef> keys = CFArrayCreate(kCFAllocatorDefault,
113 initRootPath ? rootPathKeys : propertyKeys,
114 size,
115 nullptr);
116
117 if (!keys)
118 return;
119
120 const QCFString cfPath = rootPath;
121 if (initRootPath)
122 rootPath.clear();
123
124 QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
125 cfPath,
126 kCFURLPOSIXPathStyle,
127 true);
128 if (!url)
129 return;
130
131 CFErrorRef error;
132 QCFType<CFDictionaryRef> map = CFURLCopyResourcePropertiesForKeys(url, keys, &error);
133
134 if (!map)
135 return;
136
137 if (initRootPath) {
138 const CFURLRef rootUrl = (CFURLRef)CFDictionaryGetValue(map, kCFURLVolumeURLKey);
139 if (!rootUrl)
140 return;
141
142 rootPath = QCFString(CFURLCopyFileSystemPath(rootUrl, kCFURLPOSIXPathStyle));
143 valid = true;
144 ready = true;
145
146 return;
147 }
148
149 bytesTotal = CFDictionaryGetInt64(map, kCFURLVolumeTotalCapacityKey);
150 bytesAvailable = CFDictionaryGetInt64(map, kCFURLVolumeAvailableCapacityKey);
151 bytesFree = bytesAvailable;
152}
153
154void QStorageInfoPrivate::retrieveLabel()
155{
156 QCFString path = CFStringCreateWithFileSystemRepresentation(0,
157 QFile::encodeName(rootPath).constData());
158 if (!path)
159 return;
160
161 QCFType<CFURLRef> url = CFURLCreateWithFileSystemPath(0, path, kCFURLPOSIXPathStyle, true);
162 if (!url)
163 return;
164
165 QCFType<CFURLRef> volumeUrl;
166 if (!CFURLCopyResourcePropertyForKey(url, kCFURLVolumeURLKey, &volumeUrl, NULL))
167 return;
168
169 QCFString volumeName;
170 if (!CFURLCopyResourcePropertyForKey(url, kCFURLNameKey, &volumeName, NULL))
171 return;
172
173 name = volumeName;
174}
175
176QList<QStorageInfo> QStorageInfoPrivate::mountedVolumes()
177{
178 QList<QStorageInfo> volumes;
179
180 QCFType<CFURLEnumeratorRef> enumerator;
181 enumerator = CFURLEnumeratorCreateForMountedVolumes(nullptr,
182 kCFURLEnumeratorSkipInvisibles,
183 nullptr);
184
185 CFURLEnumeratorResult result = kCFURLEnumeratorSuccess;
186 do {
187 CFURLRef url;
188 CFErrorRef error;
189 result = CFURLEnumeratorGetNextURL(enumerator, &url, &error);
190 if (result == kCFURLEnumeratorSuccess) {
191 const QCFString urlString = CFURLCopyFileSystemPath(url, kCFURLPOSIXPathStyle);
192 volumes.append(QStorageInfo(urlString));
193 }
194 } while (result != kCFURLEnumeratorEnd);
195
196 return volumes;
197}
198
199QStorageInfo QStorageInfoPrivate::root()
200{
201 return QStorageInfo(QStringLiteral("/"));
202}
203
204QT_END_NAMESPACE
205

Warning: That file was not part of the compilation database. It may have many parsing errors.