1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
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 General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | #ifndef QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED |
30 | #define QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED |
31 | |
32 | #include <QString> |
33 | #include <QStringList> |
34 | #include <QTemporaryDir> |
35 | #include <QScopedPointer> |
36 | #include <QDir> |
37 | #include <QFile> |
38 | |
39 | #if defined(Q_OS_WIN) |
40 | #include <windows.h> |
41 | #include <winioctl.h> |
42 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
43 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) |
44 | #endif |
45 | #define REPARSE_MOUNTPOINT_HEADER_SIZE 8 |
46 | #ifndef FSCTL_SET_REPARSE_POINT |
47 | #define FSCTL_SET_REPARSE_POINT CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 41, METHOD_BUFFERED, FILE_SPECIAL_ACCESS) |
48 | #endif |
49 | #endif |
50 | |
51 | // QTemporaryDir-based helper class for creating file-system hierarchies and cleaning up. |
52 | class FileSystem |
53 | { |
54 | Q_DISABLE_COPY(FileSystem) |
55 | public: |
56 | FileSystem() : m_temporaryDir(FileSystem::tempFilePattern()) {} |
57 | |
58 | QString path() const { return m_temporaryDir.path(); } |
59 | QString absoluteFilePath(const QString &fileName) const { return path() + QLatin1Char('/') + fileName; } |
60 | |
61 | bool createDirectory(const QString &relativeDirName) |
62 | { |
63 | if (m_temporaryDir.isValid()) { |
64 | QDir dir(m_temporaryDir.path()); |
65 | return dir.mkpath(dirPath: relativeDirName); |
66 | } |
67 | return false; |
68 | } |
69 | |
70 | bool createFile(const QString &relativeFileName) |
71 | { |
72 | QScopedPointer<QFile> file(openFileForWrite(fileName: relativeFileName)); |
73 | return !file.isNull(); |
74 | } |
75 | |
76 | qint64 createFileWithContent(const QString &relativeFileName) |
77 | { |
78 | QScopedPointer<QFile> file(openFileForWrite(fileName: relativeFileName)); |
79 | return file.isNull() ? qint64(-1) : file->write(data: relativeFileName.toUtf8()); |
80 | } |
81 | |
82 | #if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) |
83 | static DWORD createNtfsJunction(QString target, QString linkName, QString *errorMessage) |
84 | { |
85 | typedef struct { |
86 | DWORD ReparseTag; |
87 | DWORD ReparseDataLength; |
88 | WORD Reserved; |
89 | WORD ReparseTargetLength; |
90 | WORD ReparseTargetMaximumLength; |
91 | WORD Reserved1; |
92 | WCHAR ReparseTarget[1]; |
93 | } REPARSE_MOUNTPOINT_DATA_BUFFER, *PREPARSE_MOUNTPOINT_DATA_BUFFER; |
94 | |
95 | char reparseBuffer[MAX_PATH*3]; |
96 | HANDLE hFile; |
97 | DWORD returnedLength; |
98 | wchar_t fileSystem[MAX_PATH] = L"" ; |
99 | PREPARSE_MOUNTPOINT_DATA_BUFFER reparseInfo = (PREPARSE_MOUNTPOINT_DATA_BUFFER) reparseBuffer; |
100 | DWORD result = ERROR_SUCCESS; |
101 | |
102 | QFileInfo junctionInfo(linkName); |
103 | linkName = QDir::toNativeSeparators(junctionInfo.absoluteFilePath()); |
104 | const QString drive = linkName.left(3); |
105 | if (GetVolumeInformationW(reinterpret_cast<const wchar_t *>(drive.utf16()), |
106 | NULL, 0, NULL, NULL, NULL, |
107 | fileSystem, sizeof(fileSystem)/sizeof(WCHAR)) == FALSE) { |
108 | result = GetLastError(); |
109 | *errorMessage = "GetVolumeInformationW() failed: " + qt_error_string(int(result)); |
110 | return result; |
111 | } |
112 | if (QString::fromWCharArray(fileSystem) != "NTFS" ) { |
113 | *errorMessage = "This seems not to be an NTFS volume. Junctions are not allowed." ; |
114 | return ERROR_NOT_SUPPORTED; |
115 | } |
116 | |
117 | if (!target.startsWith("\\??\\" ) && !target.startsWith("\\\\?\\" )) { |
118 | QFileInfo targetInfo(target); |
119 | target = QDir::toNativeSeparators(targetInfo.absoluteFilePath()); |
120 | target.prepend("\\??\\" ); |
121 | if(target.endsWith('\\') && target.at(target.length()-2) != ':') |
122 | target.chop(1); |
123 | } |
124 | QDir().mkdir(linkName); |
125 | hFile = CreateFileW( (wchar_t*)linkName.utf16(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING, |
126 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL ); |
127 | if (hFile == INVALID_HANDLE_VALUE) { |
128 | result = GetLastError(); |
129 | *errorMessage = "CreateFileW(" + linkName + ") failed: " + qt_error_string(int(result)); |
130 | return result; |
131 | } |
132 | |
133 | memset( reparseInfo, 0, sizeof( *reparseInfo )); |
134 | reparseInfo->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT; |
135 | reparseInfo->ReparseTargetLength = WORD(target.size()) * WORD(sizeof(wchar_t)); |
136 | reparseInfo->ReparseTargetMaximumLength = reparseInfo->ReparseTargetLength + sizeof(wchar_t); |
137 | target.toWCharArray(reparseInfo->ReparseTarget); |
138 | reparseInfo->ReparseDataLength = reparseInfo->ReparseTargetLength + 12; |
139 | |
140 | bool ioc = DeviceIoControl(hFile, FSCTL_SET_REPARSE_POINT, reparseInfo, |
141 | reparseInfo->ReparseDataLength + REPARSE_MOUNTPOINT_HEADER_SIZE, |
142 | NULL, 0, &returnedLength, NULL); |
143 | if (!ioc) { |
144 | result = GetLastError(); |
145 | *errorMessage = "DeviceIoControl() failed: " + qt_error_string(int(result)); |
146 | } |
147 | CloseHandle( hFile ); |
148 | return result; |
149 | } |
150 | #endif |
151 | |
152 | private: |
153 | static QString tempFilePattern() |
154 | { |
155 | QString result = QDir::tempPath(); |
156 | if (!result.endsWith(c: QLatin1Char('/'))) |
157 | result.append(c: QLatin1Char('/')); |
158 | result += QStringLiteral("qt-test-filesystem-" ); |
159 | result += QCoreApplication::applicationName(); |
160 | result += QStringLiteral("-XXXXXX" ); |
161 | return result; |
162 | } |
163 | |
164 | QFile *openFileForWrite(const QString &fileName) const |
165 | { |
166 | if (m_temporaryDir.isValid()) { |
167 | const QString absName = absoluteFilePath(fileName); |
168 | QScopedPointer<QFile> file(new QFile(absName)); |
169 | if (file->open(flags: QIODevice::WriteOnly)) |
170 | return file.take(); |
171 | qWarning(msg: "Cannot open '%s' for writing: %s" , qPrintable(absName), qPrintable(file->errorString())); |
172 | } |
173 | return 0; |
174 | } |
175 | |
176 | QTemporaryDir m_temporaryDir; |
177 | }; |
178 | |
179 | #endif // include guard |
180 | |