Merge commit 'qt-v5.0.0-alpha1'
[profile/ivi/qtbase.git] / tests / shared / filesystem.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 // Helper functions for creating file-system hierarchies and cleaning up.
42
43 #ifndef QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED
44 #define QT_TESTS_SHARED_FILESYSTEM_H_INCLUDED
45
46 #include <QString>
47 #include <QStringList>
48 #include <QDir>
49 #include <QFile>
50
51 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
52 #include <windows.h>
53 #include <winioctl.h>
54 #ifndef IO_REPARSE_TAG_MOUNT_POINT
55 #define IO_REPARSE_TAG_MOUNT_POINT       (0xA0000003L)
56 #endif
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)
60 #endif
61 #endif
62
63 struct FileSystem
64 {
65     ~FileSystem()
66     {
67         Q_FOREACH(QString fileName, createdFiles)
68             QFile::remove(fileName);
69
70         Q_FOREACH(QString dirName, createdDirectories)
71             currentDir.rmdir(dirName);
72     }
73
74     bool createDirectory(const QString &dirName)
75     {
76         if (currentDir.mkdir(dirName)) {
77             createdDirectories.prepend(dirName);
78             return true;
79         }
80         return false;
81     }
82
83     bool createFile(const QString &fileName)
84     {
85         QFile file(fileName);
86         if (file.open(QIODevice::WriteOnly)) {
87             createdFiles << fileName;
88             return true;
89         }
90         return false;
91     }
92
93     qint64 createFileWithContent(const QString &fileName)
94     {
95         QFile file(fileName);
96         if (file.open(QIODevice::WriteOnly)) {
97             createdFiles << fileName;
98             return file.write(fileName.toUtf8());
99         }
100         return -1;
101     }
102
103     bool createLink(const QString &destination, const QString &linkName)
104     {
105         if (QFile::link(destination, linkName)) {
106             createdFiles << linkName;
107             return true;
108         }
109         return false;
110     }
111 #if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
112     static void createNtfsJunction(QString target, QString linkName)
113     {
114         typedef struct {
115             DWORD   ReparseTag;
116             DWORD   ReparseDataLength;
117             WORD    Reserved;
118             WORD    ReparseTargetLength;
119             WORD    ReparseTargetMaximumLength;
120             WORD    Reserved1;
121             WCHAR   ReparseTarget[1];
122         } REPARSE_MOUNTPOINT_DATA_BUFFER, *PREPARSE_MOUNTPOINT_DATA_BUFFER;
123
124         char    reparseBuffer[MAX_PATH*3];
125         HANDLE  hFile;
126         DWORD   returnedLength;
127         wchar_t fileSystem[MAX_PATH] = L"";
128         PREPARSE_MOUNTPOINT_DATA_BUFFER reparseInfo = (PREPARSE_MOUNTPOINT_DATA_BUFFER) reparseBuffer;
129
130         QFileInfo junctionInfo(linkName);
131         linkName = QDir::toNativeSeparators(junctionInfo.absoluteFilePath());
132
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.");
137
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) != ':')
143                 target.chop(1);
144         }
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 );
149
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;
156
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 );
161         QVERIFY(ioc);
162     }
163 #endif
164
165 private:
166     QDir currentDir;
167
168     QStringList createdDirectories;
169     QStringList createdFiles;
170 };
171
172 #endif // include guard