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/qdir.h>
43 #include <QtCore/qfileinfo.h>
44 #include <QtCore/qvarlengtharray.h>
45 
46 #include "qfilesystementry_p.h"
47 
48 #include <qt_windows.h>
49 
50 QT_BEGIN_NAMESPACE
51 
52 static const int defaultBufferSize = MAX_PATH + 1;
53 
canonicalPath(const QString & rootPath)54 static QString canonicalPath(const QString &rootPath)
55 {
56     QString path = QDir::toNativeSeparators(QFileInfo(rootPath).canonicalFilePath());
57     if (path.isEmpty())
58         return path;
59 
60     if (path.startsWith(QLatin1String("\\\\?\\")))
61         path.remove(0, 4);
62     if (path.length() < 2 || path.at(1) != QLatin1Char(':'))
63         return QString();
64 
65     path[0] = path[0].toUpper();
66     if (!(path.at(0).unicode() >= 'A' && path.at(0).unicode() <= 'Z'))
67         return QString();
68     if (!path.endsWith(QLatin1Char('\\')))
69         path.append(QLatin1Char('\\'));
70     return path;
71 }
72 
initRootPath()73 void QStorageInfoPrivate::initRootPath()
74 {
75     // Do not unnecessarily call QFileInfo::canonicalFilePath() if the path is
76     // already a drive root since it may hang on network drives.
77     const QString path = QFileSystemEntry::isDriveRootPath(rootPath)
78         ? QDir::toNativeSeparators(rootPath)
79         : canonicalPath(rootPath);
80 
81     if (path.isEmpty()) {
82         valid = ready = false;
83         return;
84     }
85 
86     // ### test if disk mounted to folder on other disk
87     wchar_t buffer[defaultBufferSize];
88     if (::GetVolumePathName(reinterpret_cast<const wchar_t *>(path.utf16()), buffer, defaultBufferSize))
89         rootPath = QDir::fromNativeSeparators(QString::fromWCharArray(buffer));
90     else
91         valid = ready = false;
92 }
93 
getDevice(const QString & rootPath)94 static inline QByteArray getDevice(const QString &rootPath)
95 {
96     const QString path = QDir::toNativeSeparators(rootPath);
97     const UINT type = ::GetDriveType(reinterpret_cast<const wchar_t *>(path.utf16()));
98     if (type == DRIVE_REMOTE) {
99         QVarLengthArray<char, 256> buffer(256);
100         DWORD bufferLength = buffer.size();
101         DWORD result;
102         UNIVERSAL_NAME_INFO *remoteNameInfo;
103         do {
104             buffer.resize(bufferLength);
105             remoteNameInfo = reinterpret_cast<UNIVERSAL_NAME_INFO *>(buffer.data());
106             result = ::WNetGetUniversalName(reinterpret_cast<const wchar_t *>(path.utf16()),
107                                             UNIVERSAL_NAME_INFO_LEVEL,
108                                             remoteNameInfo,
109                                             &bufferLength);
110         } while (result == ERROR_MORE_DATA);
111         if (result == NO_ERROR)
112             return QString::fromWCharArray(remoteNameInfo->lpUniversalName).toUtf8();
113         return QByteArray();
114     }
115 
116     wchar_t deviceBuffer[51];
117     if (::GetVolumeNameForVolumeMountPoint(reinterpret_cast<const wchar_t *>(path.utf16()),
118                                            deviceBuffer,
119                                            sizeof(deviceBuffer) / sizeof(wchar_t))) {
120         return QString::fromWCharArray(deviceBuffer).toLatin1();
121     }
122     return QByteArray();
123 }
124 
doStat()125 void QStorageInfoPrivate::doStat()
126 {
127     valid = ready = true;
128     initRootPath();
129     if (!valid || !ready)
130         return;
131 
132     retrieveVolumeInfo();
133     if (!valid || !ready)
134         return;
135     device = getDevice(rootPath);
136     retrieveDiskFreeSpace();
137 }
138 
retrieveVolumeInfo()139 void QStorageInfoPrivate::retrieveVolumeInfo()
140 {
141     const UINT oldmode = ::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
142 
143     const QString path = QDir::toNativeSeparators(rootPath);
144     wchar_t nameBuffer[defaultBufferSize];
145     wchar_t fileSystemTypeBuffer[defaultBufferSize];
146     DWORD fileSystemFlags = 0;
147     const bool result = ::GetVolumeInformation(reinterpret_cast<const wchar_t *>(path.utf16()),
148                                                nameBuffer,
149                                                defaultBufferSize,
150                                                nullptr,
151                                                nullptr,
152                                                &fileSystemFlags,
153                                                fileSystemTypeBuffer,
154                                                defaultBufferSize);
155     if (!result) {
156         ready = false;
157         valid = ::GetLastError() == ERROR_NOT_READY;
158     } else {
159         fileSystemType = QString::fromWCharArray(fileSystemTypeBuffer).toLatin1();
160         name = QString::fromWCharArray(nameBuffer);
161 
162         readOnly = (fileSystemFlags & FILE_READ_ONLY_VOLUME) != 0;
163     }
164 
165     ::SetErrorMode(oldmode);
166 }
167 
retrieveDiskFreeSpace()168 void QStorageInfoPrivate::retrieveDiskFreeSpace()
169 {
170     const UINT oldmode = ::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
171 
172     const QString path = QDir::toNativeSeparators(rootPath);
173     ready = ::GetDiskFreeSpaceEx(reinterpret_cast<const wchar_t *>(path.utf16()),
174                                  PULARGE_INTEGER(&bytesAvailable),
175                                  PULARGE_INTEGER(&bytesTotal),
176                                  PULARGE_INTEGER(&bytesFree));
177 
178     ::SetErrorMode(oldmode);
179 }
180 
mountedVolumes()181 QList<QStorageInfo> QStorageInfoPrivate::mountedVolumes()
182 {
183     QList<QStorageInfo> volumes;
184 
185     QString driveName = QStringLiteral("A:/");
186     const UINT oldmode = ::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
187     quint32 driveBits = quint32(::GetLogicalDrives()) & 0x3ffffff;
188     ::SetErrorMode(oldmode);
189     while (driveBits) {
190         if (driveBits & 1) {
191             QStorageInfo drive(driveName);
192             if (!drive.rootPath().isEmpty()) // drive exists, but not mounted
193                 volumes.append(drive);
194         }
195         driveName[0] = driveName[0].unicode() + 1;
196         driveBits = driveBits >> 1;
197     }
198 
199     return volumes;
200 }
201 
root()202 QStorageInfo QStorageInfoPrivate::root()
203 {
204     return QStorageInfo(QDir::fromNativeSeparators(QFile::decodeName(qgetenv("SystemDrive"))));
205 }
206 
207 QT_END_NAMESPACE
208