No such SHA1 was found

1
/****************************************************************************
2
**
3
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4
** All rights reserved.
5
** Contact: Nokia Corporation (qt-info@nokia.com)
6
**
7
** This file is part of the QtCore module of the Qt Toolkit.
8
**
9
** $QT_BEGIN_LICENSE:LGPL$
10
** GNU Lesser General Public License Usage
11
** This file may be used under the terms of the GNU Lesser General Public
12
** License version 2.1 as published by the Free Software Foundation and
13
** appearing in the file LICENSE.LGPL included in the packaging of this
14
** file. Please review the following information to ensure the GNU Lesser
15
** General Public License version 2.1 requirements will be met:
16
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17
**
18
** In addition, as a special exception, Nokia gives you certain additional
19
** rights. These rights are described in the Nokia Qt LGPL Exception
20
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21
**
22
** GNU General Public License Usage
23
** Alternatively, this file may be used under the terms of the GNU General
24
** Public License version 3.0 as published by the Free Software Foundation
25
** and appearing in the file LICENSE.GPL included in the packaging of this
26
** file. Please review the following information to ensure the GNU General
27
** Public License version 3.0 requirements will be met:
28
** http://www.gnu.org/copyleft/gpl.html.
29
**
30
** Other Usage
31
** Alternatively, this file may be used in accordance with the terms and
32
** conditions contained in a signed written agreement between you and Nokia.
33
**
34
**
35
**
36
**
37
**
38
** $QT_END_LICENSE$
39
**
40
****************************************************************************/
41
42
#include "qsharedmemory.h"
43
#include "qsharedmemory_p.h"
44
45
#include <qdebug.h>
46
47
#ifndef QT_NO_SHAREDMEMORY
48
49
//#define QSHAREDMEMORY_DEBUG
50
51
QT_BEGIN_NAMESPACE
52
53
QSharedMemoryPrivate::QSharedMemoryPrivate()
54
    : QObjectPrivate(), memory(0), size(0), error(QSharedMemory::NoError),
55
#ifndef QT_NO_SYSTEMSEMAPHORE
56
      systemSemaphore(QString()), lockedByMe(false),
57
#endif
58
      hand(0)
59
{
60
}
61
62
void QSharedMemoryPrivate::setErrorString(const QString &function)
63
{
64
    DWORD windowsError = GetLastError();
65
    if (windowsError == 0)
66
        return;
67
68
    switch (windowsError) {
69
    case ERROR_ALREADY_EXISTS:
70
        error = QSharedMemory::AlreadyExists;
71
        errorString = QSharedMemory::tr("%1: already exists").arg(function);
72
    break;
73
    case ERROR_FILE_NOT_FOUND:
74
#ifdef Q_OS_WINCE
75
        // This happens on CE only if no file is present as CreateFileMappingW
76
        // bails out with this error code
77
    case ERROR_INVALID_PARAMETER:
78
#endif
79
        error = QSharedMemory::NotFound;
80
        errorString = QSharedMemory::tr("%1: doesn't exist").arg(function);
81
        break;
82
    case ERROR_COMMITMENT_LIMIT:
83
        error = QSharedMemory::InvalidSize;
84
        errorString = QSharedMemory::tr("%1: invalid size").arg(function);
85
        break;
86
    case ERROR_NO_SYSTEM_RESOURCES:
87
    case ERROR_NOT_ENOUGH_MEMORY:
88
        error = QSharedMemory::OutOfResources;
89
        errorString = QSharedMemory::tr("%1: out of resources").arg(function);
90
        break;
91
    case ERROR_ACCESS_DENIED:
92
        error = QSharedMemory::PermissionDenied;
93
        errorString = QSharedMemory::tr("%1: permission denied").arg(function);
94
        break;
95
    default:
96
        errorString = QSharedMemory::tr("%1: unknown error %2").arg(function).arg(windowsError);
97
        error = QSharedMemory::UnknownError;
98
#ifdef QSHAREDMEMORY_DEBUG
99
        qDebug() << errorString << "key" << key;
100
#endif
101
        break;
102
    }
103
}
104
105
HANDLE QSharedMemoryPrivate::handle()
106
{
107
    if (!hand) {
108
        // don't allow making handles on empty keys
109
        if (nativeKey.isEmpty()) {
110
            error = QSharedMemory::KeyError;
111
            errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::handle"));
112
            return false;
113
        }
114
#ifndef Q_OS_WINCE
115
        hand = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, (wchar_t*)nativeKey.utf16());
116
#else
117
        // This works for opening a mapping too, but always opens it with read/write access in
118
        // attach as it seems.
119
        hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, 0, (wchar_t*)nativeKey.utf16());
120
#endif
121
        if (!hand)
122
            setErrorString(QLatin1String("QSharedMemory::handle"));
123
    }
124
125
    return hand;
126
}
127
128
void QSharedMemoryPrivate::cleanHandle()
129
{
130
    if (hand != 0 && !CloseHandle(hand))
131
        setErrorString(QLatin1String("QSharedMemory::cleanHandle"));
132
    hand = 0;
133
}
134
135
bool QSharedMemoryPrivate::create(int size)
136
{
137
    if (nativeKey.isEmpty()) {
138
        error = QSharedMemory::KeyError;
139
        errorString = QSharedMemory::tr("%1: key is empty").arg(QLatin1String("QSharedMemory::create"));
140
        return false;
141
    }
142
143
    // Create the file mapping.
144
    hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size, (wchar_t*)nativeKey.utf16());
145
    setErrorString(QLatin1String("QSharedMemory::create"));
146
147
    // hand is valid when it already exists unlike unix so explicitly check
148
    return !(error == QSharedMemory::AlreadyExists || !hand);
149
}
150
151
bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
152
{
153
    // Grab a pointer to the memory block
154
    int permissions = (mode == QSharedMemory::ReadOnly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS);
155
    memory = (void *)MapViewOfFile(handle(), permissions, 0, 0, 0);
156
    if (0 == memory) {
157
        setErrorString(QLatin1String("QSharedMemory::attach"));
158
        cleanHandle();
159
        return false;
160
    }
161
162
    // Grab the size of the memory we have been given (a multiple of 4K on windows)
163
    MEMORY_BASIC_INFORMATION info;
164
    if (!VirtualQuery(memory, &info, sizeof(info))) {
165
        // Windows doesn't set an error code on this one,
166
        // it should only be a kernel memory error.
167
        error = QSharedMemory::UnknownError;
168
        errorString = QSharedMemory::tr("%1: size query failed").arg(QLatin1String("QSharedMemory::attach"));
169
        return false;
170
    }
171
    size = info.RegionSize;
172
173
    return true;
174
}
175
176
bool QSharedMemoryPrivate::detach()
177
{
178
    // umap memory
179
    if (!UnmapViewOfFile(memory)) {
180
        setErrorString(QLatin1String("QSharedMemory::detach"));
181
        return false;
182
    }
183
    memory = 0;
184
    size = 0;
185
186
    // close handle
187
    cleanHandle();
188
189
    return true;
190
}
191
192
QT_END_NAMESPACE
193
194
#endif // QT_NO_SHAREDMEMORY