8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-27 20:03:03 +01:00
firebird-mirror/src/alice/exe.cpp

405 lines
10 KiB
C++
Raw Normal View History

2001-05-23 15:26:42 +02:00
//____________________________________________________________
//
2001-05-23 15:26:42 +02:00
// PROGRAM: Alice (All Else) Utility
// MODULE: exe.cpp
// DESCRIPTION: Does the database calls
//
2001-05-23 15:26:42 +02:00
// 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
//
2001-05-23 15:26:42 +02:00
// 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.
//
2001-05-23 15:26:42 +02:00
// The Original Code was created by Inprise Corporation
// and its predecessors. Portions created by Inprise Corporation are
// Copyright (C) Inprise Corporation.
//
2001-05-23 15:26:42 +02:00
// All Rights Reserved.
// Contributor(s): ______________________________________.
//
2001-05-23 15:26:42 +02:00
//
//____________________________________________________________
//
// $Id: exe.cpp,v 1.21 2003-10-29 10:52:59 robocop Exp $
//
// 2001.07.06 Sean Leyne - Code Cleanup, removed "#ifdef READONLY_DATABASE"
// conditionals, as the engine now fully supports
// readonly databases.
2001-05-23 15:26:42 +02:00
//
// 2002.10.30 Sean Leyne - Removed obsolete "PC_PLATFORM" define
//
2001-05-23 15:26:42 +02:00
#include "firebird.h"
2001-05-23 15:26:42 +02:00
#include "../jrd/ib_stdio.h"
#include <stdlib.h>
#include <string.h>
#include "../jrd/gds.h"
2001-05-23 15:26:42 +02:00
#include "../jrd/common.h"
#include "../jrd/ibsetjmp.h"
#include "../alice/alice.h"
#include "../alice/alice_proto.h"
2001-05-23 15:26:42 +02:00
#include "../alice/aliceswi.h"
#include "../alice/all.h"
#include "../alice/all_proto.h"
#include "../alice/alice_meta.h"
#include "../alice/tdr_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/thd_proto.h"
static USHORT build_dpb(UCHAR *, ULONG);
static void extract_db_info(UCHAR *);
static const TEXT val_errors[] =
2001-05-23 15:26:42 +02:00
{
isc_info_page_errors, isc_info_record_errors, isc_info_bpage_errors,
isc_info_dpage_errors, isc_info_ipage_errors, isc_info_ppage_errors,
isc_info_tpage_errors, gds_info_end
};
static inline void stuff_dpb(UCHAR** d, int blr)
{
UCHAR *ptr = *d;
2003-09-22 10:49:45 +02:00
*ptr++ = (UCHAR)blr;
*d = ptr;
}
2003-09-22 10:49:45 +02:00
static inline void stuff_dpb_long(UCHAR** d, int blr)
{
stuff_dpb(d, blr);
2003-09-22 10:49:45 +02:00
stuff_dpb(d, blr >> 8);
stuff_dpb(d, blr >> 16);
stuff_dpb(d, blr >> 24);
}
2001-05-23 15:26:42 +02:00
//____________________________________________________________
//
//
2001-05-23 15:26:42 +02:00
int EXE_action(const TEXT* database, ULONG switches)
2001-05-23 15:26:42 +02:00
{
UCHAR dpb[128];
TGBL tdgbl = GET_THREAD_DATA;
2001-05-23 15:26:42 +02:00
ALLA_init();
for (USHORT i = 0; i < MAX_VAL_ERRORS; i++)
2001-05-23 15:26:42 +02:00
tdgbl->ALICE_data.ua_val_errors[i] = 0;
// generate the database parameter block for the attach,
// based on the various switches
2001-05-23 15:26:42 +02:00
const USHORT dpb_length = build_dpb(dpb, switches);
2001-05-23 15:26:42 +02:00
bool error = false;
FRBRD* handle = NULL;
2003-08-30 04:02:36 +02:00
gds__attach_database(tdgbl->status, 0, database, &handle, dpb_length,
reinterpret_cast<SCHAR*>(dpb));
2001-05-23 15:26:42 +02:00
SVC_STARTED(tdgbl->service_blk);
if (tdgbl->status[1])
2003-09-10 19:52:12 +02:00
error = true;
2001-05-23 15:26:42 +02:00
if (tdgbl->status[2] == isc_arg_warning)
ALICE_print_status(tdgbl->status);
if (handle != NULL) {
UCHAR error_string[128];
2001-05-23 15:26:42 +02:00
if ((switches & sw_validate) && (tdgbl->status[1] != isc_bug_check)) {
2003-08-30 04:02:36 +02:00
gds__database_info(tdgbl->status, &handle, sizeof(val_errors),
val_errors, sizeof(error_string),
reinterpret_cast<char*>(error_string));
2001-05-23 15:26:42 +02:00
extract_db_info(error_string);
}
if (switches & sw_disable)
MET_disable_wal(tdgbl->status, handle);
2003-08-30 04:02:36 +02:00
gds__detach_database(tdgbl->status, &handle);
2001-05-23 15:26:42 +02:00
}
ALLA_fini();
return ((error) ? FINI_ERROR : FINI_OK);
}
//____________________________________________________________
//
//
2001-05-23 15:26:42 +02:00
int EXE_two_phase(const TEXT* database, ULONG switches)
2001-05-23 15:26:42 +02:00
{
UCHAR dpb[128];
TGBL tdgbl = GET_THREAD_DATA;
2001-05-23 15:26:42 +02:00
ALLA_init();
for (USHORT i = 0; i < MAX_VAL_ERRORS; i++)
2001-05-23 15:26:42 +02:00
tdgbl->ALICE_data.ua_val_errors[i] = 0;
// generate the database parameter block for the attach,
// based on the various switches
2001-05-23 15:26:42 +02:00
const USHORT dpb_length = build_dpb(dpb, switches);
2001-05-23 15:26:42 +02:00
bool error = false;
FRBRD* handle = NULL;
2003-08-30 04:02:36 +02:00
gds__attach_database(tdgbl->status, 0, database, &handle,
dpb_length, reinterpret_cast<char*>(dpb));
2001-05-23 15:26:42 +02:00
SVC_STARTED(tdgbl->service_blk);
if (tdgbl->status[1])
2003-09-10 19:52:12 +02:00
error = true;
2001-05-23 15:26:42 +02:00
else if (switches & sw_list)
TDR_list_limbo((handle), database, switches);
2001-05-23 15:26:42 +02:00
else if (switches & (sw_commit | sw_rollback | sw_two_phase))
2003-09-10 19:52:12 +02:00
error = TDR_reconnect_multiple((handle),
tdgbl->ALICE_data.ua_transaction, database,
switches);
2001-05-23 15:26:42 +02:00
if (handle)
2003-08-30 04:02:36 +02:00
gds__detach_database(tdgbl->status, &handle);
2001-05-23 15:26:42 +02:00
ALLA_fini();
return ((error) ? FINI_ERROR : FINI_OK);
}
//____________________________________________________________
//
//
// generate the database parameter block for the attach,
// based on the various switches
//
2001-05-23 15:26:42 +02:00
2003-10-16 10:51:06 +02:00
static USHORT build_dpb(UCHAR* dpb, ULONG switches)
2001-05-23 15:26:42 +02:00
{
TGBL tdgbl = GET_THREAD_DATA;
2001-05-23 15:26:42 +02:00
UCHAR* dpb2 = dpb;
*dpb2++ = gds_dpb_version1;
*dpb2++ = isc_dpb_gfix_attach;
*dpb2++ = 0;
2001-05-23 15:26:42 +02:00
if (switches & sw_sweep) {
*dpb2++ = gds_dpb_sweep;
*dpb2++ = 1;
*dpb2++ = gds_dpb_records;
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_activate) {
*dpb2++ = gds_dpb_activate_shadow;
*dpb2++ = 0;
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_validate) {
*dpb2++ = gds_dpb_verify;
*dpb2++ = 1;
*dpb2 = gds_dpb_pages;
2001-05-23 15:26:42 +02:00
if (switches & sw_full)
*dpb2 |= gds_dpb_records;
2001-05-23 15:26:42 +02:00
if (switches & sw_no_update)
*dpb2 |= gds_dpb_no_update;
2001-05-23 15:26:42 +02:00
if (switches & sw_mend)
*dpb2 |= gds_dpb_repair;
2001-05-23 15:26:42 +02:00
if (switches & sw_ignore)
*dpb2 |= gds_dpb_ignore;
dpb2++;
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_housekeeping) {
*dpb2++ = gds_dpb_sweep_interval;
*dpb2++ = 4;
for (int i = 0; i < 4; i++, (tdgbl->ALICE_data.ua_sweep_interval >>= 8))
{
// TMN: Here we should really have the following assert
// assert(tdgbl->ALICE_data.ua_sweep_interval <= MAX_UCHAR);
*dpb2++ = (UCHAR) tdgbl->ALICE_data.ua_sweep_interval;
}
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_begin_log) {
*dpb2++ = gds_dpb_begin_log;
*dpb2++ = strlen(tdgbl->ALICE_data.ua_log_file);
2003-10-16 10:51:06 +02:00
for (const char* q = tdgbl->ALICE_data.ua_log_file; *q;)
*dpb2++ = *q++;
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_buffers) {
*dpb2++ = isc_dpb_set_page_buffers;
*dpb2++ = 4;
for (int i = 0; i < 4; i++, (tdgbl->ALICE_data.ua_page_buffers >>= 8))
{
// TMN: Here we should really have the following assert
// assert(tdgbl->ALICE_data.ua_page_buffers <= MAX_UCHAR);
*dpb2++ = (UCHAR) tdgbl->ALICE_data.ua_page_buffers;
}
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_quit_log) {
*dpb2++ = gds_dpb_quit_log;
*dpb2++ = 0;
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_kill) {
*dpb2++ = gds_dpb_delete_shadow;
*dpb2++ = 0;
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_write) {
*dpb2++ = gds_dpb_force_write;
*dpb2++ = 1;
*dpb2++ = tdgbl->ALICE_data.ua_force ? 1 : 0;
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_use) {
*dpb2++ = gds_dpb_no_reserve;
*dpb2++ = 1;
*dpb2++ = tdgbl->ALICE_data.ua_use ? 1 : 0;
2001-05-23 15:26:42 +02:00
}
2001-05-23 15:26:42 +02:00
else if (switches & sw_mode) {
*dpb2++ = isc_dpb_set_db_readonly;
*dpb2++ = 1;
*dpb2++ = (tdgbl->ALICE_data.ua_read_only) ? 1 : 0;
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_shut) {
*dpb2++ = gds_dpb_shutdown;
*dpb2++ = 1;
*dpb2 = 0;
2001-05-23 15:26:42 +02:00
if (switches & sw_attach)
*dpb2 |= gds_dpb_shut_attachment;
2001-05-23 15:26:42 +02:00
else if (switches & sw_cache)
*dpb2 |= gds_dpb_shut_cache;
2001-05-23 15:26:42 +02:00
else if (switches & sw_force)
*dpb2 |= gds_dpb_shut_force;
2001-05-23 15:26:42 +02:00
else if (switches & sw_tran)
*dpb2 |= gds_dpb_shut_transaction;
dpb2++;
*dpb2++ = gds_dpb_shutdown_delay;
*dpb2++ = 2; // Build room for shutdown delay
// TMN: Here we should really have the following assert
// assert(tdgbl->ALICE_data.ua_page_buffers <= MAX_USHORT);
// or maybe even compare with MAX_SSHORT
*dpb2++ = (UCHAR) tdgbl->ALICE_data.ua_shutdown_delay;
*dpb2++ = (UCHAR) (tdgbl->ALICE_data.ua_shutdown_delay >> 8);
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_online) {
*dpb2++ = gds_dpb_online;
*dpb2++ = 0;
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_disable) {
*dpb2++ = isc_dpb_disable_wal;
*dpb2++ = 0;
2001-05-23 15:26:42 +02:00
}
else if (switches & (sw_list | sw_commit | sw_rollback | sw_two_phase)) {
*dpb2++ = gds_dpb_no_garbage_collect;
*dpb2++ = 0;
2001-05-23 15:26:42 +02:00
}
else if (switches & sw_set_db_dialect) {
stuff_dpb(&dpb2, isc_dpb_set_db_sql_dialect);
stuff_dpb(&dpb2, 4);
2003-09-22 10:49:45 +02:00
stuff_dpb_long(&dpb2, tdgbl->ALICE_data.ua_db_SQL_dialect);
2001-05-23 15:26:42 +02:00
}
if (tdgbl->ALICE_data.ua_user) {
*dpb2++ = gds_dpb_user_name;
*dpb2++ = strlen(reinterpret_cast<const char*>(tdgbl->ALICE_data.ua_user));
2003-10-16 10:51:06 +02:00
for (const UCHAR* q = tdgbl->ALICE_data.ua_user; *q;)
*dpb2++ = *q++;
2001-05-23 15:26:42 +02:00
}
if (tdgbl->ALICE_data.ua_password) {
if (!tdgbl->sw_service)
*dpb2++ = gds_dpb_password;
2001-05-23 15:26:42 +02:00
else
*dpb2++ = gds_dpb_password_enc;
*dpb2++ = strlen(reinterpret_cast<const char*>(tdgbl->ALICE_data.ua_password));
2003-10-16 10:51:06 +02:00
for (const UCHAR* q = tdgbl->ALICE_data.ua_password; *q;)
*dpb2++ = *q++;
2001-05-23 15:26:42 +02:00
}
USHORT dpb_length = dpb2 - dpb;
2001-05-23 15:26:42 +02:00
if (dpb_length == 1)
dpb_length = 0;
return dpb_length;
}
//____________________________________________________________
//
2001-05-23 15:26:42 +02:00
// Extract database info from string
//
2001-05-23 15:26:42 +02:00
static void extract_db_info(UCHAR* db_info_buffer)
2001-05-23 15:26:42 +02:00
{
UCHAR item;
TGBL tdgbl = GET_THREAD_DATA;
2001-05-23 15:26:42 +02:00
UCHAR* p = db_info_buffer;
2001-05-23 15:26:42 +02:00
while ((item = *p++) != gds_info_end) {
const SLONG length = gds__vax_integer(p, 2);
2001-05-23 15:26:42 +02:00
p += 2;
// TMN: Here we should really have the following assert
// assert(length <= MAX_SSHORT);
// for all cases that use 'length' as input to 'gds__vax_integer'
2001-05-23 15:26:42 +02:00
switch (item) {
case isc_info_page_errors:
tdgbl->ALICE_data.ua_val_errors[VAL_PAGE_ERRORS] =
gds__vax_integer(p, (SSHORT) length);
p += length;
break;
case isc_info_record_errors:
tdgbl->ALICE_data.ua_val_errors[VAL_RECORD_ERRORS] =
gds__vax_integer(p, (SSHORT) length);
p += length;
break;
case isc_info_bpage_errors:
tdgbl->ALICE_data.ua_val_errors[VAL_BLOB_PAGE_ERRORS] =
gds__vax_integer(p, (SSHORT) length);
p += length;
break;
case isc_info_dpage_errors:
tdgbl->ALICE_data.ua_val_errors[VAL_DATA_PAGE_ERRORS] =
gds__vax_integer(p, (SSHORT) length);
p += length;
break;
case isc_info_ipage_errors:
tdgbl->ALICE_data.ua_val_errors[VAL_INDEX_PAGE_ERRORS] =
gds__vax_integer(p, (SSHORT) length);
p += length;
break;
case isc_info_ppage_errors:
tdgbl->ALICE_data.ua_val_errors[VAL_POINTER_PAGE_ERRORS] =
gds__vax_integer(p, (SSHORT) length);
p += length;
break;
case isc_info_tpage_errors:
tdgbl->ALICE_data.ua_val_errors[VAL_TIP_PAGE_ERRORS] =
gds__vax_integer(p, (SSHORT) length);
p += length;
break;
case isc_info_error:
/* has to be a < V4 database. */
tdgbl->ALICE_data.ua_val_errors[VAL_INVALID_DB_VERSION] = 1;
return;
default:
;
}
}
}