-
-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathalice_meta.epp
More file actions
448 lines (354 loc) · 9.49 KB
/
alice_meta.epp
File metadata and controls
448 lines (354 loc) · 9.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
*____________________________________________________________
*
* PROGRAM: Alice (All Else) Utility
* MODULE: alice_meta.epp
* DESCRIPTION: Metadata lookup routines
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
*
*____________________________________________________________
*
*/
#include "firebird.h"
#include <stdio.h>
#include "ibase.h"
#include "../alice/alice.h"
#include "../alice/alice_meta.h"
#include "../yvalve/gds_proto.h"
#include "fb_exception.h"
#include "../common/classes/alloc.h"
#include "../common/classes/array.h"
#include "../common/classes/UserBlob.h"
#include "../alice/alice_proto.h"
#include "../common/utils_proto.h"
#include "../common/isc_f_proto.h"
#include <string.h>
// For service APIs the follow DB handle is a value stored
// in thread data. This is also done for other statics generated by
// GPRE. This is to avoid multiple threading problems with module
// level statics.
DATABASE DB = STATIC FILENAME "yachts.lnk";
#define DB tdgbl->db_handle
#define gds_trans tdgbl->tr_handle
namespace Firebird::Alice {
typedef HalfStaticArray<TEXT, 1024> TextBuffer;
static void get_string(const TEXT**, string&);
static USHORT get_capabilities(ISC_STATUS*);
static tdr* get_description(ISC_QUAD*);
static void parse_fullpath(tdr*);
static bool snarf_blob(ISC_QUAD*, TextBuffer&);
// table used to determine capabilities, checking for specific
// fields in system relations
struct rfr_tab_t
{
const TEXT* relation;
const TEXT* field;
int bit_mask;
};
static const rfr_tab_t rfr_table[] =
{
{ "RDB$TRANSACTIONS", "RDB$TRANSACTION_DESCRIPTION", CAP_transactions },
{ 0, 0, 0 }
};
static inline void return_error(const ISC_STATUS* /*user_status*/)
{
ALICE_print_status(true, gds_status);
LongJump::raise();
}
/*____________________________________________________________
*
* Disable WAL for the database forcibly.
* Drop all the entries from RDB$LOG_FILES
*/
void MET_disable_wal(ISC_STATUS* user_status, isc_db_handle handle)
{
FB_API_HANDLE request = 0;
AliceGlobals* tdgbl = AliceGlobals::getSpecific();
if (!(DB = handle))
return;
START_TRANSACTION
ON_ERROR
return_error(user_status);
END_ERROR;
FOR(REQUEST_HANDLE request)
X IN RDB$LOG_FILES
ERASE X;
END_FOR
COMMIT
ON_ERROR
return_error(user_status);
END_ERROR;
}
/*____________________________________________________________
*
* Get the state of a transaction,
* assuming that the database has
* already been attached.
*/
void MET_get_state(ISC_STATUS* user_status, tdr* trans)
{
FB_API_HANDLE request = 0;
AliceGlobals* tdgbl = AliceGlobals::getSpecific();
if (!(DB = trans->tdr_db_handle) || !(trans->tdr_db_caps & CAP_transactions))
{
trans->tdr_state = TRA_unknown;
return;
}
START_TRANSACTION
ON_ERROR
return_error(user_status);
END_ERROR;
FOR(REQUEST_HANDLE request)
TRA IN RDB$TRANSACTIONS WITH
TRA.RDB$TRANSACTION_ID = trans->tdr_id
trans->tdr_state = TRA.RDB$TRANSACTION_STATE;
END_FOR
ON_ERROR
return_error(user_status);
END_ERROR;
isc_release_request(gds_status, &request);
if (gds_status[1]) {
return_error(user_status);
}
ROLLBACK
ON_ERROR
return_error(user_status);
END_ERROR;
}
/*____________________________________________________________
*
* Get the description of a transaction in
* limbo, including all associated transactions
* in other databases.
*/
tdr* MET_get_transaction(ISC_STATUS* user_status, isc_db_handle handle, TraNumber id)
{
FB_API_HANDLE request = 0;
tdr* trans = NULL;
AliceGlobals* tdgbl = AliceGlobals::getSpecific();
if (!(DB = handle))
return 0;
START_TRANSACTION
ON_ERROR
return_error(user_status);
END_ERROR;
const USHORT capabilities = get_capabilities(user_status);
if (capabilities & CAP_transactions)
{
FOR(REQUEST_HANDLE request)
TRA IN RDB$TRANSACTIONS WITH
TRA.RDB$TRANSACTION_ID = id AND
TRA.RDB$TRANSACTION_DESCRIPTION NOT MISSING
trans = get_description(&TRA.RDB$TRANSACTION_DESCRIPTION);
END_FOR
ON_ERROR
return_error(user_status);
END_ERROR;
isc_release_request(gds_status, &request);
if (gds_status[1]) {
return_error(user_status);
}
}
ROLLBACK
ON_ERROR
return_error(user_status);
END_ERROR;
if (trans)
trans->tdr_db_caps = capabilities;
return trans;
}
/*____________________________________________________________
*
* Get the capabilities associated with
* the database for a particular transaction.
*/
void MET_set_capabilities(ISC_STATUS* user_status, tdr* trans)
{
AliceGlobals* tdgbl = AliceGlobals::getSpecific();
if (!(DB = trans->tdr_db_handle))
return;
START_TRANSACTION
ON_ERROR
return_error(user_status);
END_ERROR;
trans->tdr_db_caps = get_capabilities(user_status);
ROLLBACK
ON_ERROR
return_error(user_status);
END_ERROR;
}
/*____________________________________________________________
*
* Eat a string with a byte-encoded length.
*/
static void get_string(const TEXT** ptr, string& str)
{
AliceGlobals* tdgbl = AliceGlobals::getSpecific();
const TEXT* p = *ptr;
const USHORT length = (USHORT) *p++;
*ptr = p + length;
str.assign(p, length);
}
/*____________________________________________________________
*
* Get the capabilities associated with
* the database for a particular transaction.
*/
static USHORT get_capabilities(ISC_STATUS* user_status)
{
USHORT capabilities = CAP_none;
AliceGlobals* tdgbl = AliceGlobals::getSpecific();
// Look for desired fields in system relations
FB_API_HANDLE req = 0;
for (const rfr_tab_t* rel_field_table = rfr_table; rel_field_table->relation; rel_field_table++)
{
FOR(REQUEST_HANDLE req) x IN DB.RDB$RELATION_FIELDS
WITH x.RDB$RELATION_NAME = rel_field_table->relation
AND x.RDB$FIELD_NAME = rel_field_table->field
capabilities |= rel_field_table->bit_mask;
END_FOR
ON_ERROR
return_error(user_status);
END_ERROR;
}
isc_release_request(gds_status, &req);
if (gds_status[1]) {
return_error(user_status);
}
return capabilities;
}
/*____________________________________________________________
*
* Get the description of a transaction in
* limbo, including all associated transactions
* in other databases.
*/
static tdr* get_description(ISC_QUAD* blob_id)
{
AliceGlobals* tdgbl = AliceGlobals::getSpecific();
TextBuffer buffer;
if (UserBlob::blobIsNull(*blob_id))
return NULL;
if (!snarf_blob(blob_id, buffer))
{
ALICE_print_status(true, gds_status);
return NULL;
}
if (buffer.getCount() < 2)
return NULL;
tdr* trans = NULL;
string host_site;
string database_path;
const TEXT* p = buffer.begin();
MemoryPool& pool(*tdgbl->getDefaultPool());
// skip version number
++p;
tdr* ptr = NULL; // silence uninitialized warning
SLONG id_length;
TraNumber id;
while (*p)
{
switch (*p++)
{
case TDR_HOST_SITE:
get_string(&p, host_site);
break;
case TDR_DATABASE_PATH:
get_string(&p, database_path);
break;
case TDR_TRANSACTION_ID:
id_length = *p++;
id = isc_portable_integer(reinterpret_cast<const UCHAR*>(p), id_length);
p += id_length;
if (!trans) {
trans = ptr = FB_NEW_POOL(pool) tdr(pool);
}
else
{
ptr->tdr_next = FB_NEW_POOL(pool) tdr(pool);
ptr = ptr->tdr_next;
}
ptr->tdr_host_site = host_site;
ptr->tdr_fullpath = database_path;
parse_fullpath(ptr);
ptr->tdr_id = id;
database_path.clear();
break;
default:
ALICE_print(108);
// msg 108: Transaction description item unknown.
return NULL;
}
}
return trans;
}
/*____________________________________________________________
*
* Take apart a full remote path specification,
* finding the filename and the remote site.
*/
static void parse_fullpath(tdr* trans)
{
PathName filename = trans->tdr_fullpath.c_str();
PathName hostname;
// Find the last remote node in the path
while (ISC_analyze_tcp(filename, hostname))
trans->tdr_remote_site = hostname.c_str();
// At this point the filename is clear from any remote nodes
trans->tdr_filename = filename.c_str();
}
/*____________________________________________________________
*
* Get a blob into a buffer, returning false if error happens
*/
static bool snarf_blob(ISC_QUAD* blob_id, TextBuffer& buffer)
{
AliceGlobals* tdgbl = AliceGlobals::getSpecific();
UserBlob blob(gds_status);
if (!blob.open(DB, gds_trans, *blob_id))
return false;
FB_UINT64 blob_size;
if (!getBlobSize(blob, &blob_size, NULL, NULL))
return false;
if (blob_size > MAX_ULONG)
return false;
FB_SIZE_T len = blob_size;
TEXT* ptr = buffer.getBuffer(blob_size + 1);
while (len)
{
FB_SIZE_T real_len;
if (len && blob.getSegment(len, ptr, real_len))
{
len -= real_len;
ptr += real_len;
}
const ISC_STATUS err = blob.getCode();
if (err == 0 || err == isc_segment)
continue;
if (err == isc_segstr_eof)
break;
return false;
}
blob.close();
*ptr = 0;
buffer.resize(ptr - buffer.begin() + 1);
return true;
}
} // namespace Firebird::Alice