~ubuntu-branches/ubuntu/karmic/firebird2.1/karmic

« back to all changes in this revision

Viewing changes to src/utilities/install/registry.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Damyan Ivanov
  • Date: 2008-05-26 23:59:25 UTC
  • Revision ID: james.westby@ubuntu.com-20080526235925-2pnqj6nxpppoeaer
Tags: upstream-2.1.0.17798-0.ds2
ImportĀ upstreamĀ versionĀ 2.1.0.17798-0.ds2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *      PROGRAM:        Windows NT registry installation program
 
3
 *      MODULE:         registry.cpp
 
4
 *      DESCRIPTION:    Registry update routines
 
5
 *
 
6
 * The contents of this file are subject to the Interbase Public
 
7
 * License Version 1.0 (the "License"); you may not use this file
 
8
 * except in compliance with the License. You may obtain a copy
 
9
 * of the License at http://www.Inprise.com/IPL.html
 
10
 *
 
11
 * Software distributed under the License is distributed on an
 
12
 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
 
13
 * or implied. See the License for the specific language governing
 
14
 * rights and limitations under the License.
 
15
 *
 
16
 * The Original Code was created by Inprise Corporation
 
17
 * and its predecessors. Portions created by Inprise Corporation are
 
18
 * Copyright (C) Inprise Corporation.
 
19
 *
 
20
 * All Rights Reserved.
 
21
 * Contributor(s): ______________________________________.
 
22
 *
 
23
 * 01-Feb-2002 Paul Reeves: Removed hard-coded registry path
 
24
 *
 
25
 */
 
26
 
 
27
#include "firebird.h"
 
28
#include "../jrd/common.h"
 
29
#include <stdio.h>
 
30
#include <stdlib.h>
 
31
#include <windows.h>
 
32
//#include "../jrd/license.h"
 
33
#include "../utilities/install/install_nt.h"
 
34
#include "../utilities/install/regis_proto.h"
 
35
#include "../utilities/install/registry.h"
 
36
 
 
37
static void cleanup_key(HKEY, const char*);
 
38
#ifdef THIS_CODE_IS_TEMPORARILY_NOT_USED_ANYMORE
 
39
static USHORT remove_subkeys(HKEY, bool, pfnRegError);
 
40
#endif
 
41
 
 
42
USHORT REGISTRY_install(HKEY hkey_rootnode,
 
43
                                                const TEXT* directory,
 
44
                                                pfnRegError err_handler)
 
45
{
 
46
/**************************************
 
47
 *
 
48
 *      R E G I S T R Y _ i n s t a l l
 
49
 *
 
50
 **************************************
 
51
 *
 
52
 * Functional description
 
53
 *      Install Firebird in the registry.
 
54
 *
 
55
 **************************************/
 
56
        HKEY hkey_instances;
 
57
        DWORD disp;
 
58
        SLONG status = RegCreateKeyEx(hkey_rootnode,
 
59
                        REG_KEY_ROOT_INSTANCES,
 
60
                        0,
 
61
                        "",
 
62
                        REG_OPTION_NON_VOLATILE,
 
63
                        KEY_WRITE,
 
64
                        NULL, &hkey_instances, &disp);
 
65
        if (status != ERROR_SUCCESS) {
 
66
                return (*err_handler) (status, "RegCreateKeyEx", NULL);
 
67
        }
 
68
 
 
69
        TEXT path_name[MAXPATHLEN];
 
70
        TEXT* p;
 
71
        USHORT len = GetFullPathName(directory, sizeof(path_name), path_name, &p);
 
72
        if (len && path_name[len - 1] != '/' && path_name[len - 1] != '\\') {
 
73
                path_name[len++] = '\\';
 
74
                path_name[len] = 0;
 
75
        }
 
76
 
 
77
        if ((status = RegSetValueEx(hkey_instances, FB_DEFAULT_INSTANCE, 0,
 
78
                        REG_SZ, reinterpret_cast<const BYTE*>(path_name),
 
79
                        (DWORD) (len + 1))) != ERROR_SUCCESS)
 
80
        {
 
81
                (*err_handler) (status, "RegSetValueEx", hkey_instances);
 
82
 
 
83
                // Removes the "Instances" key if we just created it.
 
84
                // Else, keep it, because we don't want to trash other instances.
 
85
                if (disp == REG_CREATED_NEW_KEY)
 
86
                {
 
87
                        RegDeleteKey(hkey_rootnode, REG_KEY_ROOT_INSTANCES);
 
88
                        cleanup_key(hkey_rootnode, REG_KEY_ROOT_PRODUCT);
 
89
                        cleanup_key(hkey_rootnode, REG_KEY_ROOT_COMPANY);
 
90
                }
 
91
 
 
92
                return FB_FAILURE;
 
93
        }
 
94
 
 
95
        RegCloseKey(hkey_instances);
 
96
        return FB_SUCCESS;
 
97
}
 
98
 
 
99
USHORT REGISTRY_remove(HKEY hkey_rootnode,
 
100
                                           bool silent_flag,
 
101
                                           pfnRegError err_handler)
 
102
{
 
103
/**************************************
 
104
 *
 
105
 *      R E G I S T R Y _ r e m o v e
 
106
 *
 
107
 **************************************
 
108
 *
 
109
 * Functional description
 
110
 *      Remove Firebird from the registry.
 
111
 *
 
112
 **************************************/
 
113
        HKEY hkey_instances;
 
114
 
 
115
        SLONG status = RegOpenKeyEx(hkey_rootnode, REG_KEY_ROOT_INSTANCES, 0,
 
116
                KEY_READ | KEY_WRITE, &hkey_instances);
 
117
        if (status != ERROR_SUCCESS)
 
118
        {
 
119
                if (silent_flag)
 
120
                        return FB_FAILURE;
 
121
                return (*err_handler) (status, "RegOpenKeyEx", NULL);
 
122
        }
 
123
 
 
124
        // Remove the FB_DEFAULT_INSTANCE value
 
125
        if ((status = RegDeleteValue(hkey_instances, FB_DEFAULT_INSTANCE))
 
126
                != ERROR_SUCCESS)
 
127
        {
 
128
                RegCloseKey(hkey_instances);
 
129
                if (silent_flag)
 
130
                        return FB_FAILURE;
 
131
                return (*err_handler) (status, "RegDeleteValue", NULL);
 
132
        }
 
133
 
 
134
        RegCloseKey(hkey_instances);
 
135
        cleanup_key(hkey_rootnode, REG_KEY_ROOT_INSTANCES);
 
136
        cleanup_key(hkey_rootnode, REG_KEY_ROOT_PRODUCT);
 
137
        cleanup_key(hkey_rootnode, REG_KEY_ROOT_COMPANY);
 
138
 
 
139
        return FB_SUCCESS;
 
140
}
 
141
 
 
142
static void cleanup_key(HKEY hkey_rootnode, const char* key)
 
143
{
 
144
/**************************************
 
145
 *
 
146
 * c l e a n u p _ k e y
 
147
 *
 
148
 **************************************
 
149
 *
 
150
 * Functional description
 
151
 *      Remove a key, if found unused (no subkeys and no values).
 
152
 *      This function is silent.
 
153
 *
 
154
 **************************************/
 
155
 
 
156
        HKEY hkey;
 
157
 
 
158
        if (RegOpenKeyEx(hkey_rootnode, key, 0,
 
159
                        KEY_READ | KEY_WRITE, &hkey) == ERROR_SUCCESS)
 
160
        {
 
161
                DWORD subkeys_count, values_count;
 
162
                if (RegQueryInfoKey(hkey, NULL, 0, 0,
 
163
                        &subkeys_count, NULL, NULL,
 
164
                        &values_count, 0, 0, NULL, NULL) == ERROR_SUCCESS &&
 
165
                                subkeys_count == 0 && values_count == 0)
 
166
                {
 
167
                        RegCloseKey(hkey);
 
168
                        RegDeleteKey(hkey_rootnode, key);
 
169
                }
 
170
                else
 
171
                        RegCloseKey(hkey);
 
172
        }
 
173
        return;
 
174
}
 
175
 
 
176
#ifdef THIS_CODE_IS_TEMPORARILY_NOT_USED_ANYMORE
 
177
// I keep it here for possible re-use after FB 1.5 release. OM, sept 30, 2003.
 
178
static USHORT remove_subkeys(
 
179
                                                         HKEY hkey,
 
180
                                                         bool silent_flag,
 
181
                                                         pfnRegError err_handler)
 
182
{
 
183
/**************************************
 
184
 *
 
185
 *      r e m o v e _ s u b k e y s
 
186
 *
 
187
 **************************************
 
188
 *
 
189
 * Functional description
 
190
 *      Remove all sub-keys of an Firebird key from the registry.
 
191
 *
 
192
 **************************************/
 
193
        TEXT buffer[MAXPATHLEN];
 
194
        DWORD n_sub_keys, max_sub_key;
 
195
        FILETIME last_write_time;
 
196
 
 
197
        DWORD buf_len = sizeof(buffer);
 
198
        DWORD status = RegQueryInfoKey(hkey,
 
199
                                                         buffer, &buf_len,
 
200
                                                         NULL,
 
201
                                                         &n_sub_keys,
 
202
                                                         &max_sub_key,
 
203
                                                         &i, &i, &i, &i, &i, &last_write_time);
 
204
        if (status != ERROR_SUCCESS && status != ERROR_MORE_DATA) {
 
205
                if (silent_flag)
 
206
                        return FB_FAILURE;
 
207
                return (*err_handler) (status, "RegQueryInfoKey", NULL);
 
208
        }
 
209
 
 
210
        TEXT* sub_key = (++max_sub_key > sizeof(buffer)) ?
 
211
                (TEXT*) malloc((SLONG) max_sub_key) : buffer;
 
212
 
 
213
        const TEXT* p = NULL;
 
214
        for (DWORD i = 0; i < n_sub_keys; i++) {
 
215
                DWORD sub_key_len = max_sub_key;
 
216
                if ((status = RegEnumKeyEx(hkey, i, sub_key, &sub_key_len,
 
217
                                                                   NULL, NULL, NULL,
 
218
                                                                   &last_write_time)) != ERROR_SUCCESS)
 
219
                {
 
220
                        p = "RegEnumKeyEx";
 
221
                        break;
 
222
                }
 
223
 
 
224
                HKEY hkey_sub;
 
225
                if ((status = RegOpenKeyEx(hkey, sub_key,
 
226
                                                                   0,
 
227
                                                                   KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE |
 
228
                                                                   KEY_WRITE, &hkey_sub)) != ERROR_SUCCESS)
 
229
                {
 
230
                        p = "RegOpenKeyEx";
 
231
                        break;
 
232
                }
 
233
                const USHORT ret = remove_subkeys(hkey_sub, silent_flag, err_handler);
 
234
                RegCloseKey(hkey_sub);
 
235
                if (ret == FB_FAILURE)
 
236
                        return FB_FAILURE;
 
237
                if ((status = RegDeleteKey(hkey, sub_key)) != ERROR_SUCCESS) {
 
238
                        p = "RegDeleteKey";
 
239
                        break;
 
240
                }
 
241
        }
 
242
 
 
243
        if (buffer != sub_key)
 
244
                free(sub_key);
 
245
 
 
246
        if (p) {
 
247
                if (silent_flag)
 
248
                        return FB_FAILURE;
 
249
                return (*err_handler) (status, p, NULL);
 
250
        }
 
251
 
 
252
        return FB_SUCCESS;
 
253
}
 
254
#endif
 
255