1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
41 // Helper functions for creating file-system hierarchies and cleaning up.
43 #ifndef QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED
44 #define QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED
47 #include <QStringList>
51 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
54 #ifndef IO_REPARSE_TAG_MOUNT_POINT
55 #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
57 #define REPARSE_MOUNTPOINT_HEADER_SIZE 8
58 #ifndef FSCTL_SET_REPARSE_POINT
59 #define FSCTL_SET_REPARSE_POINT CTL_CODE(FILE_DEVICE_FILE_SYSTEM, 41, METHOD_BUFFERED, FILE_SPECIAL_ACCESS)
67 Q_FOREACH(QString fileName, createdFiles)
68 QFile::remove(fileName);
70 Q_FOREACH(QString dirName, createdDirectories)
71 currentDir.rmdir(dirName);
74 bool createDirectory(const QString &dirName)
76 if (currentDir.mkdir(dirName)) {
77 createdDirectories.prepend(dirName);
83 bool createFile(const QString &fileName)
86 if (file.open(QIODevice::WriteOnly)) {
87 createdFiles << fileName;
93 qint64 createFileWithContent(const QString &fileName)
96 if (file.open(QIODevice::WriteOnly)) {
97 createdFiles << fileName;
98 return file.write(fileName.toUtf8());
103 bool createLink(const QString &destination, const QString &linkName)
105 if (QFile::link(destination, linkName)) {
106 createdFiles << linkName;
111 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
112 static void createNtfsJunction(QString target, QString linkName)
116 DWORD ReparseDataLength;
118 WORD ReparseTargetLength;
119 WORD ReparseTargetMaximumLength;
121 WCHAR ReparseTarget[1];
122 } REPARSE_MOUNTPOINT_DATA_BUFFER, *PREPARSE_MOUNTPOINT_DATA_BUFFER;
124 char reparseBuffer[MAX_PATH*3];
126 DWORD returnedLength;
127 wchar_t fileSystem[MAX_PATH] = L"";
128 PREPARSE_MOUNTPOINT_DATA_BUFFER reparseInfo = (PREPARSE_MOUNTPOINT_DATA_BUFFER) reparseBuffer;
130 QFileInfo junctionInfo(linkName);
131 linkName = QDir::toNativeSeparators(junctionInfo.absoluteFilePath());
133 GetVolumeInformationW( (wchar_t*)linkName.left(3).utf16(), NULL, 0, NULL, NULL, NULL,
134 fileSystem, sizeof(fileSystem)/sizeof(WCHAR));
135 if(QString().fromWCharArray(fileSystem) != "NTFS")
136 QSKIP("This seems not to be an NTFS volume. Junctions are not allowed.");
138 if (!target.startsWith("\\??\\") && !target.startsWith("\\\\?\\")) {
139 QFileInfo targetInfo(target);
140 target = QDir::toNativeSeparators(targetInfo.absoluteFilePath());
141 target.prepend("\\??\\");
142 if(target.endsWith('\\') && target.at(target.length()-2) != ':')
145 QDir().mkdir(linkName);
146 hFile = CreateFileW( (wchar_t*)linkName.utf16(), GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
147 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, NULL );
148 QVERIFY(hFile != INVALID_HANDLE_VALUE );
150 memset( reparseInfo, 0, sizeof( *reparseInfo ));
151 reparseInfo->ReparseTag = IO_REPARSE_TAG_MOUNT_POINT;
152 reparseInfo->ReparseTargetLength = DWORD(target.size() * sizeof(wchar_t));
153 reparseInfo->ReparseTargetMaximumLength = reparseInfo->ReparseTargetLength + sizeof(wchar_t);
154 target.toWCharArray(reparseInfo->ReparseTarget);
155 reparseInfo->ReparseDataLength = reparseInfo->ReparseTargetLength + 12;
157 bool ioc = DeviceIoControl(hFile, FSCTL_SET_REPARSE_POINT, reparseInfo,
158 reparseInfo->ReparseDataLength + REPARSE_MOUNTPOINT_HEADER_SIZE,
159 NULL, 0, &returnedLength, NULL);
160 CloseHandle( hFile );
168 QStringList createdDirectories;
169 QStringList createdFiles;
172 #endif // include guard