8
0
mirror of https://github.com/FirebirdSQL/firebird.git synced 2025-01-23 05:23:03 +01:00
firebird-mirror/src/burp/burp.cpp

2176 lines
55 KiB
C++
Raw Normal View History

2001-05-23 15:26:42 +02:00
/*
* PROGRAM: JRD Backup and Restore Program
* MODULE: burp.cpp
2001-05-23 15:26:42 +02:00
* DESCRIPTION: Command line interpreter for backup/restore
*
* 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): ______________________________________.
* 2001.07.06 Sean Leyne - Code Cleanup, removed "#ifdef READONLY_DATABASE"
* conditionals, as the engine now fully supports
* readonly databases.
2002-02-16 03:21:35 +01:00
*
* 2002.02.15 Sean Leyne - Code Cleanup, removed obsolete "EPSON" defines
*
2002-10-30 07:40:58 +01:00
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
*
* 2002.10.30 Sean Leyne - Removed support for obsolete "PC_PLATFORM" define
2001-05-23 15:26:42 +02:00
*/
#include "firebird.h"
2004-04-29 00:00:03 +02:00
#include <stdio.h>
2001-05-23 15:26:42 +02:00
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "../jrd/common.h"
#include "../jrd/ibase.h"
#include <stdarg.h>
#include "../jrd/ibsetjmp.h"
#include "../jrd/msg_encode.h"
#include "../jrd/thd.h"
2001-05-23 15:26:42 +02:00
#include "../jrd/ods.h" // to get MAX_PAGE_SIZE
2004-05-03 23:43:56 +02:00
#include "../jrd/svc.h"
2001-05-23 15:26:42 +02:00
#include "../burp/burp.h"
#include "../burp/burpswi.h"
#include "../burp/std_desc.h"
2001-05-23 15:26:42 +02:00
#include "../jrd/license.h"
#include "../common/classes/timestamp.h"
2001-05-23 15:26:42 +02:00
#include "../burp/burp_proto.h"
#include "../burp/backu_proto.h"
#include "../burp/mvol_proto.h"
#include "../burp/resto_proto.h"
#include "../jrd/gds_proto.h"
#include "../jrd/why_proto.h"
#include "../jrd/gdsassert.h"
#include "../common/classes/ClumpletWriter.h"
2001-05-23 15:26:42 +02:00
2001-07-12 07:46:06 +02:00
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#ifdef SERVICE_THREAD
2003-07-15 04:43:36 +02:00
#include "../utilities/common/cmd_util_proto.h"
2001-05-23 15:26:42 +02:00
#endif
#include "../common/utils_proto.h"
2001-05-23 15:26:42 +02:00
#ifdef UNIX
#include <unistd.h>
#endif
#ifdef VMS
#include <descrip.h>
#include <iodef.h>
#include <types.h>
#include <file.h>
#endif
#ifndef VMS
#include <fcntl.h>
#endif
#ifdef WIN_NT
#include <io.h>
#endif
2001-05-23 15:26:42 +02:00
#ifndef O_CREAT
#include <sys/types.h>
#include <sys/file.h>
#endif
// The following structure in only needed if we are building a local exe
// I've commented it out to make it clear since this global variable is
// defined in burp.cpp as well, and is not relevant for SERVICE_THREAD
// MOD 23-July-2002
const char* fopen_write_type = "w";
const char* fopen_read_type = "r";
2001-05-23 15:26:42 +02:00
const int open_mask = 0666;
2001-05-23 15:26:42 +02:00
2003-09-18 23:56:26 +02:00
#ifdef VMS
const char* switch_char = "/";
2003-09-18 23:56:26 +02:00
#else
const char* switch_char = "-";
2001-05-23 15:26:42 +02:00
#endif
const char* const output_suppress = "SUPPRESS";
const int burp_msg_fac = 12;
2001-05-23 15:26:42 +02:00
enum gbak_action
{
QUIT = 0,
BACKUP = 1 ,
RESTORE = 2,
FDESC = 3
};
static void close_out_transaction(gbak_action, isc_tr_handle*);
//static void enable_signals(void);
//static void excp_handler(void);
2004-01-13 10:52:19 +01:00
static SLONG get_number(const SCHAR*);
static ULONG get_size(const SCHAR*, burp_fil*);
static gbak_action open_files(const TEXT *, const TEXT**, bool, USHORT,
const Firebird::ClumpletWriter&);
static int common_main(int, char**, Jrd::pfn_svc_output, Jrd::Service*);
#ifndef SERVICE_THREAD
2004-07-06 07:54:13 +02:00
BurpGlobals* gdgbl;
static int output_main(Jrd::Service*, const UCHAR*);
2003-09-10 19:52:12 +02:00
static int api_gbak(int, char**, USHORT, TEXT*, TEXT*, TEXT *, bool, bool);
2001-05-23 15:26:42 +02:00
#endif
static void burp_output(const SCHAR*, ...) ATTRIBUTE_FORMAT(1,2);
static void burp_usage();
2001-05-23 15:26:42 +02:00
// fil.fil_length is ULONG
const ULONG KBYTE = 1024;
const ULONG MBYTE = KBYTE * KBYTE;
const ULONG GBYTE = MBYTE * KBYTE;
2001-05-23 15:26:42 +02:00
2003-02-11 21:17:56 +01:00
#if defined (WIN95)
2003-09-10 19:52:12 +02:00
static bool fAnsiCP = false;
static inline void translate_cp(SCHAR* a)
{
2003-09-18 23:56:26 +02:00
if (!fAnsiCP)
2003-09-22 16:13:54 +02:00
AnsiToOem(a, a);
2003-09-18 23:56:26 +02:00
}
2001-05-23 15:26:42 +02:00
#else
static inline void translate_cp(SCHAR* a)
{
2003-09-18 23:56:26 +02:00
}
2001-05-23 15:26:42 +02:00
#endif
static int output_svc(Jrd::Service* output_data, const UCHAR* output_buf)
{
/**************************************
*
* o u t p u t _ s v c
*
**************************************
*
* Functional description
* Routine which is passed to GBAK for calling back when there is output
* if gbak is run as a service
*
**************************************/
2004-04-29 00:00:03 +02:00
fprintf(stdout, "%s", output_buf);
return 0;
}
#ifdef SERVICE_THREAD
THREAD_ENTRY_DECLARE BURP_main(THREAD_ENTRY_PARAM arg)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* m a i n _ g b a k
*
**************************************
*
* Functional description
* Entrypoint for GBAK via services manager.
2001-05-23 15:26:42 +02:00
*
**************************************/
Jrd::Service* service = (Jrd::Service*)arg;
2003-12-22 11:00:59 +01:00
const int exit_code = common_main(service->svc_argc, service->svc_argv,
SVC_output, service);
2001-05-23 15:26:42 +02:00
// Mark service thread as finished.
// If service is detached, cleanup memory being used by service.
2004-05-03 23:43:56 +02:00
SVC_finish(service, Jrd::SVC_finished);
2001-05-23 15:26:42 +02:00
2006-01-16 16:39:55 +01:00
return (THREAD_ENTRY_RETURN)(IPTR)(exit_code);
2001-05-23 15:26:42 +02:00
}
#else // SERVICE_THREAD
2001-05-23 15:26:42 +02:00
int CLIB_ROUTINE main(int argc, char* argv[])
{
/**************************************
*
* m a i n
*
**************************************
*
* Functional description
* Parse and interpret command line, then "do the right thing."
*
**************************************/
2003-12-22 11:00:59 +01:00
// If a "-service" switch is specified then use Admin APIs
// The code will alter the command line.
TEXT** argvp = argv;
const TEXT* const* const end = argvp + argc;
2001-05-23 15:26:42 +02:00
argvp++;
// Initialize data
USHORT total = 0;
bool flag_restore, flag_verbose, err;
flag_restore = flag_verbose = err = false;
2003-12-22 11:00:59 +01:00
TEXT *sw_user, *sw_password, *sw_service;
TEXT *d_user, *d_password, *d_service;
2001-05-23 15:26:42 +02:00
sw_user = sw_password = sw_service = d_user = d_password = d_service = NULL;
/* Parse the command line for the -USER, -PASSWORD, -SERVICE,
and -VERBOSE options. Calculate a length for the new command line to be
passed to a server using services APIs */
// This is to avoid overwriting literal strings later. Read the warning near
// the end of this function.
char none[] = "-*NONE*";
2001-05-23 15:26:42 +02:00
while (argvp < end && !err)
{
2003-12-22 11:00:59 +01:00
TEXT* string = *argvp++;
USHORT len = strlen(string);
if (total > 0 && len > 0)
++len; // space to separate the current argument from the previous
if (*string != *switch_char) {
total += len;
2001-05-23 15:26:42 +02:00
continue;
}
if (!string[1])
string = none;
const in_sw_tab_t* in_sw_tab = burp_in_sw_table;
const TEXT* q;
for (; q = in_sw_tab->in_sw_name; in_sw_tab++)
2001-05-23 15:26:42 +02:00
{
TEXT c;
for (const TEXT* p = string + 1; c = *p++;) {
2001-05-23 15:26:42 +02:00
if (UPPER(c) != *q++)
break;
2004-01-13 10:52:19 +01:00
}
2001-05-23 15:26:42 +02:00
if (!c)
break;
}
switch (in_sw_tab->in_sw)
{
case IN_SW_BURP_C: // create database
case IN_SW_BURP_R: // replace database
case IN_SW_BURP_RECREATE: // recreate database
total += len;
2003-09-10 19:52:12 +02:00
flag_restore = true;
2001-05-23 15:26:42 +02:00
break;
case IN_SW_BURP_USER: // default user name
2001-05-23 15:26:42 +02:00
if (argvp >= end)
2003-09-10 19:52:12 +02:00
err = true;
2001-05-23 15:26:42 +02:00
else {
sw_user = string;
d_user = *argvp++;
}
break;
case IN_SW_BURP_PASS: // default password
2001-05-23 15:26:42 +02:00
if (argvp >= end)
2003-09-10 19:52:12 +02:00
err = true;
2001-05-23 15:26:42 +02:00
else {
sw_password = string;
d_password = *argvp++;
}
break;
case IN_SW_BURP_SE: // service name
2001-05-23 15:26:42 +02:00
if (argvp >= end) {
2003-09-10 19:52:12 +02:00
err = true;
2003-12-22 11:00:59 +01:00
}
else {
2001-05-23 15:26:42 +02:00
sw_service = string;
d_service = *argvp++;
}
break;
case IN_SW_BURP_V: // verify actions
total += len;
2003-09-10 19:52:12 +02:00
flag_verbose = true;
2001-05-23 15:26:42 +02:00
break;
default:
total += len;
2001-05-23 15:26:42 +02:00
break;
}
}
int exit_code;
2001-05-23 15:26:42 +02:00
if (sw_service && !err)
{
/* Backup/restore operations will be running as a service thread.
* To make this more efficiently the isc_spb_options is used.
* This allows us to skip a conversion from the gbak command line
* switches to service parameter block in here as well as vice versa
2003-12-22 11:00:59 +01:00
* conversion within svc.cpp
2001-05-23 15:26:42 +02:00
*
* If -USER and -PASSWORD switches are used by the user within
* the gbak command line then we have to eliminate them from there. The
* password will be encrypted and added along with the user name
2001-05-23 15:26:42 +02:00
* within SVC_start function later on. We shall also eliminate
* the -SERVER switch because the switch has already been processed.
*/
2003-12-22 11:00:59 +01:00
// Warning: altering command line.
2001-05-23 15:26:42 +02:00
if (sw_user)
*sw_user = '\0';
if (sw_password)
*sw_password = '\0';
if (sw_service)
*sw_service = '\0';
exit_code = api_gbak(argc, argv, total, d_password,
d_user, d_service, flag_restore, flag_verbose);
}
else
exit_code = common_main(argc, argv, output_main, NULL);
2001-05-23 15:26:42 +02:00
return exit_code;
}
static int output_main(Jrd::Service* output_data, const UCHAR* output_buf)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* o u t p u t _ m a i n
*
**************************************
*
* Functional description
* Routine which is passed to GBAK for calling back when there is output.
*
**************************************/
2004-04-29 00:00:03 +02:00
fprintf(stderr, "%s", output_buf);
2001-05-23 15:26:42 +02:00
return 0;
}
static int api_gbak(int argc,
2003-12-22 11:00:59 +01:00
char* argv[],
USHORT length,
2003-12-22 11:00:59 +01:00
TEXT* password,
TEXT* user,
TEXT* service,
bool restore,
bool verbose)
{
/**********************************************
*
* a p i _ g b a k
*
**********************************************
*
* Functional description
* Run gbak using services APIs
*
**********************************************/
2004-07-03 02:13:46 +02:00
BurpGlobals ldgbl;
BurpGlobals* tdgbl = &ldgbl;
BurpGlobals::putSpecific(tdgbl);
tdgbl->output_proc = output_main;
Firebird::string usr;
if (!user)
fb_utils::readenv("ISC_USER", usr);
else
usr = user;
Firebird::string pswd;
if (!password)
fb_utils::readenv("ISC_PASSWORD", pswd);
else
pswd = password;
char *const spb = (char *) gds__alloc((SLONG) (2 + 2 + usr.length() +
2 + pswd.length() +
2 + length));
/* 'isc_spb_version'
'isc_spb_current_version'
'isc_spb_user_name'
'length'
"usr"
'isc_spb_password'
'length'
"pswd"
'isc_spb_options'
'length'
"options" */
2003-12-22 11:00:59 +01:00
ISC_STATUS_ARRAY status;
if (spb == NULL) {
status[0] = isc_arg_gds;
status[1] = isc_virmemexh;
status[2] = isc_arg_end;
BURP_print_status(status);
BURP_print(83, 0, 0, 0, 0, 0); // msg 83 Exiting before completion due to errors
return FINI_ERROR;
}
2003-12-22 11:00:59 +01:00
char* spb_ptr = spb;
*spb_ptr++ = isc_spb_version;
*spb_ptr++ = isc_spb_current_version;
if (usr.length()) {
*spb_ptr++ = isc_spb_user_name;
*spb_ptr++ = usr.length();
MEMMOVE(usr.c_str(), spb_ptr, usr.length());
spb_ptr += usr.length();
if (user)
*user = '\0';
}
if (pswd.length()) {
*spb_ptr++ = isc_spb_password;
*spb_ptr++ = pswd.length();
MEMMOVE(pswd.c_str(), spb_ptr, pswd.length());
spb_ptr += pswd.length();
if (password)
*password = '\0';
}
2004-01-13 10:52:19 +01:00
char* const svc_name = (char *) gds__alloc((SLONG) (strlen(service) + 1));
if (svc_name == NULL) {
status[0] = isc_arg_gds;
status[1] = isc_virmemexh;
status[2] = isc_arg_end;
BURP_print_status(status);
gds__free(spb);
BURP_print(83, 0, 0, 0, 0, 0); // msg 83 Exiting before completion due to errors
return FINI_ERROR;
}
if (service) {
strcpy(svc_name, service);
*service = '\0';
}
// Fill command line options
*spb_ptr++ = isc_spb_command_line;
2003-12-31 06:36:12 +01:00
const TEXT* const* const end = argv + argc;
argv++;
*spb_ptr++ = length;
const char* cmdline_begin = spb_ptr;
while (argv < end) {
if (**argv && spb_ptr > cmdline_begin)
*spb_ptr++ = ' ';
2003-12-31 06:36:12 +01:00
for (const TEXT* x = *argv++; *x;)
*spb_ptr++ = *x++;
}
USHORT spblen = spb_ptr - spb;
2004-05-03 01:06:37 +02:00
FB_API_HANDLE svc_handle = 0;
if (isc_service_attach(status, 0, svc_name, (&svc_handle), spblen, spb))
{
BURP_print_status(status);
gds__free(spb);
gds__free(svc_name);
BURP_print(83, 0, 0, 0, 0, 0);
// msg 83 Exiting before completion due to errors
return FINI_ERROR;
}
char *const thd = (char *) gds__alloc((SLONG) (2));
// 'isc_action_svc_restore/isc_action_svc_backup'
// 'isc_spb_verbose'
if (thd == NULL) {
status[0] = isc_arg_gds;
status[1] = isc_virmemexh;
status[2] = isc_arg_end;
BURP_print_status(status);
isc_service_detach(status, (&svc_handle));
gds__free(spb);
gds__free(svc_name);
BURP_print(83, 0, 0, 0, 0, 0);
// msg 83 Exiting before completion due to errors
return FINI_ERROR;
}
char *thd_ptr = thd;
if (restore)
*thd_ptr++ = isc_action_svc_restore;
else
*thd_ptr++ = isc_action_svc_backup;
if (verbose)
*thd_ptr++ = isc_spb_verbose;
USHORT thdlen = thd_ptr - thd;
if (isc_service_start(status, (&svc_handle), NULL, thdlen, thd))
{
BURP_print_status(status);
gds__free(spb);
gds__free(svc_name);
gds__free(thd);
isc_service_detach(status, (&svc_handle));
BURP_print(83, 0, 0, 0, 0, 0); // msg 83 Exiting before completion due to errors
return FINI_ERROR;
}
2003-12-22 11:00:59 +01:00
const char sendbuf[] = { isc_info_svc_line };
char respbuf[1024];
const char* sl;
do {
if (isc_service_query(status, (&svc_handle), NULL, 0, NULL,
sizeof(sendbuf), sendbuf,
sizeof(respbuf), respbuf))
{
BURP_print_status(status);
gds__free(spb);
gds__free(svc_name);
gds__free(thd);
isc_service_detach(status, (&svc_handle));
BURP_print(83, 0, 0, 0, 0, 0); // msg 83 Exiting before completion due to errors
return FINI_ERROR;
}
2003-12-22 11:00:59 +01:00
char* p = respbuf;
sl = p;
if (*p++ == isc_info_svc_line)
{
const ISC_USHORT len = (ISC_USHORT) isc_vax_integer(p, sizeof(ISC_USHORT));
p += sizeof(ISC_USHORT);
if (!len)
{
if (*p == isc_info_data_not_ready)
continue;
else if (*p == isc_info_end)
break;
}
p[len] = '\0';
burp_output("%s\n", p);
}
} while (*sl == isc_info_svc_line);
gds__free(spb);
gds__free(svc_name);
gds__free(thd);
isc_service_detach(status, (&svc_handle));
return FINI_OK;
}
#endif // SERVICE_THREAD
2001-05-23 15:26:42 +02:00
int common_main(int argc,
char* argv[],
Jrd::pfn_svc_output output_proc,
Jrd::Service* output_data)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* B U R P _ g b a k
*
**************************************
*
* Functional description
* Routine called by command line utility, services API, and server manager.
*
**************************************/
const TEXT* file2 = NULL;
2001-05-23 15:26:42 +02:00
// TMN: This variable should probably be removed, but I left it in
// in case some platform should redefine the BURP BurpGlobals::putSpecific.
2004-07-03 02:13:46 +02:00
//BurpGlobals thd_context;
gbak_action action = QUIT;
BurpGlobals sgbl;
BurpGlobals *tdgbl = &sgbl;
2001-05-23 15:26:42 +02:00
BurpGlobals::putSpecific(tdgbl);
tdgbl->burp_throw = true;
2001-05-23 15:26:42 +02:00
tdgbl->file_desc = INVALID_HANDLE_VALUE;
tdgbl->output_proc = output_proc;
tdgbl->output_data = output_data;
2003-12-31 06:36:12 +01:00
in_sw_tab_t* in_sw_tab; // used in several parts below.
// Initialize static data.
2001-12-24 03:51:06 +01:00
for (in_sw_tab = burp_in_sw_table; in_sw_tab->in_sw_name; in_sw_tab++) {
2001-05-23 15:26:42 +02:00
in_sw_tab->in_sw_state = FALSE;
}
2001-12-24 03:51:06 +01:00
try {
2001-05-23 15:26:42 +02:00
#ifdef VMS
argc = VMS_parse(&argv, argc);
#endif
/* Perform some special handling when run as an Interbase service. The
first switch can be "-svc" (lower case!) or it can be "-svc_re" followed
by 3 file descriptors to use in re-directing stdin, stdout, and stderr.
If this utility is started as a thread in the engine, then the first switch
will be "-svc_thd".
*/
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_service_gbak = false;
tdgbl->gbl_sw_service_thd = false;
2001-05-23 15:26:42 +02:00
tdgbl->service_blk = NULL;
tdgbl->status = tdgbl->status_vector;
2001-05-23 15:26:42 +02:00
if (argc > 1 && !strcmp(argv[1], "-svc")) {
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_service_gbak = true;
2001-05-23 15:26:42 +02:00
argv++;
argc--;
}
else if (argc > 1 && !strcmp(argv[1], "-svc_thd")) {
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_service_gbak = true;
tdgbl->gbl_sw_service_thd = true;
tdgbl->service_blk = (Jrd::Service*) output_data;
2001-05-23 15:26:42 +02:00
tdgbl->status = tdgbl->service_blk->svc_status;
tdgbl->service_blk->svc_started();
2001-05-23 15:26:42 +02:00
argv++;
argc--;
}
else if (argc > 4 && !strcmp(argv[1], "-svc_re")) {
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_service_gbak = true;
2001-05-23 15:26:42 +02:00
tdgbl->output_proc = output_svc;
long redir_in = atol(argv[2]);
long redir_out = atol(argv[3]);
long redir_err = atol(argv[4]);
2001-05-23 15:26:42 +02:00
#ifdef WIN_NT
2003-02-11 21:17:56 +01:00
#if defined (WIN95)
2003-09-10 19:52:12 +02:00
fAnsiCP = true;
2001-05-23 15:26:42 +02:00
#endif
redir_in = _open_osfhandle(redir_in, 0);
redir_out = _open_osfhandle(redir_out, 0);
redir_err = _open_osfhandle(redir_err, 0);
#endif
if (redir_in != 0)
if (dup2((int) redir_in, 0))
close((int) redir_in);
if (redir_out != 1)
if (dup2((int) redir_out, 1))
close((int) redir_out);
if (redir_err != 2)
if (dup2((int) redir_err, 2))
close((int) redir_err);
argv += 4;
argc -= 4;
}
2003-02-11 21:17:56 +01:00
#if defined (WIN95)
2001-05-23 15:26:42 +02:00
if (!fAnsiCP)
fAnsiCP = (GetConsoleCP() == GetACP());
#endif
if (argc <= 1) {
burp_usage();
BURP_exit_local(FINI_ERROR, tdgbl);
}
2001-05-23 15:26:42 +02:00
USHORT sw_replace = FALSE;
2001-05-23 15:26:42 +02:00
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_compress = true;
tdgbl->gbl_sw_convert_ext_tables = false;
tdgbl->gbl_sw_transportable = true;
tdgbl->gbl_sw_ignore_limbo = false;
2001-05-23 15:26:42 +02:00
tdgbl->gbl_sw_blk_factor = 0;
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_no_reserve = false;
tdgbl->gbl_sw_old_descriptions = false;
tdgbl->gbl_sw_mode = false;
2001-05-23 15:26:42 +02:00
tdgbl->gbl_sw_skip_count = 0;
tdgbl->action = NULL;
2004-01-13 10:52:19 +01:00
burp_fil* file = NULL;
burp_fil* file_list = NULL;
2001-05-23 15:26:42 +02:00
tdgbl->io_buffer_size = GBAK_IO_BUFFER_SIZE;
// Avoid overwriting literal strings.
char none[] = "-*NONE*";
const TEXT* const* const end = argv + argc;
2001-05-23 15:26:42 +02:00
++argv;
while (argv < end) {
2003-12-22 11:00:59 +01:00
TEXT* string = *argv;
int temp = strlen(string) - 1;
2001-05-23 15:26:42 +02:00
if (string[temp] == ',')
2003-12-22 11:00:59 +01:00
string[temp] = '\0'; // Modifying argv elements
2001-05-23 15:26:42 +02:00
if (*string != *switch_char) {
2001-05-23 15:26:42 +02:00
if (!file || file->fil_length || !get_size(*argv, file)) {
/* Miserable thing must be a filename
(dummy in a length for the backup file */
2004-01-13 10:52:19 +01:00
file = (burp_fil*) BURP_alloc_zero(FIL_LEN);
2001-05-23 15:26:42 +02:00
file->fil_name = string;
file->fil_fd = INVALID_HANDLE_VALUE;
if (!file_list)
file->fil_length = MAX_LENGTH;
else
file->fil_length = 0;
file->fil_next = file_list;
file_list = file;
}
argv++;
}
else {
++argv;
if (!string[1])
string = none;
2003-12-31 06:36:12 +01:00
const TEXT* q;
2001-05-23 15:26:42 +02:00
for (in_sw_tab = burp_in_sw_table; q = in_sw_tab->in_sw_name;
in_sw_tab++)
{
TEXT c;
for (const TEXT *p = string + 1; c = *p++;)
2001-05-23 15:26:42 +02:00
if (UPPER(c) != *q++)
break;
if (!c)
break;
}
in_sw_tab->in_sw_state = TRUE;
if (!in_sw_tab->in_sw) {
2003-09-14 03:08:18 +02:00
BURP_print(137, string + 1, 0, 0, 0, 0);
// msg 137 unknown switch %s
2001-05-23 15:26:42 +02:00
burp_usage();
BURP_error(1, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 1: found unknown switch
2001-05-23 15:26:42 +02:00
}
else if (in_sw_tab->in_sw == IN_SW_BURP_RECREATE) {
int real_sw = IN_SW_BURP_C;
if ((argv < end) && (**argv != *switch_char) ) {
// find optional BURP_SW_OVERWRITE parameter
TEXT c;
const TEXT* q = BURP_SW_OVERWRITE;
for (const TEXT *p = *argv; c = *p++;)
if (UPPER(c) != *q++)
break;
if (!c) {
real_sw = IN_SW_BURP_R;
argv++;
}
}
// replace IN_SW_BURP_RECREATE by IN_SW_BURP_R or IN_SW_BURP_C
in_sw_tab->in_sw_state = FALSE;
in_sw_tab_t* real_sw_tab = burp_in_sw_table;
for (; real_sw_tab->in_sw != real_sw; real_sw_tab++)
;
real_sw_tab->in_sw_state = TRUE;
}
2001-05-23 15:26:42 +02:00
else if (in_sw_tab->in_sw == IN_SW_BURP_S) {
if (argv >= end)
BURP_error(200, true, 0, 0, 0, 0, 0);
// msg 200: missing parameter for the number of bytes to be skipped
2001-05-23 15:26:42 +02:00
tdgbl->gbl_sw_skip_count = get_number(*argv);
if (!tdgbl->gbl_sw_skip_count)
BURP_error(201, true, *argv, 0, 0, 0, 0);
// msg 201: expected number of bytes to be skipped, encountered "%s"
2001-05-23 15:26:42 +02:00
argv++;
}
else if (in_sw_tab->in_sw == IN_SW_BURP_P) {
if (argv >= end)
BURP_error(2, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 2 page size parameter missing
2001-05-23 15:26:42 +02:00
tdgbl->gbl_sw_page_size = (USHORT) get_number(*argv);
if (!tdgbl->gbl_sw_page_size)
BURP_error(12, true, *argv, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 12 expected page size, encountered "%s"
2001-05-23 15:26:42 +02:00
argv++;
}
else if (in_sw_tab->in_sw == IN_SW_BURP_BU) {
if (argv >= end)
BURP_error(258, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 258 page buffers parameter missing
2001-05-23 15:26:42 +02:00
tdgbl->gbl_sw_page_buffers = get_number(*argv);
if (!tdgbl->gbl_sw_page_buffers)
BURP_error(259, true, *argv, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 259 expected page buffers, encountered "%s"
2001-05-23 15:26:42 +02:00
argv++;
}
else if (in_sw_tab->in_sw == IN_SW_BURP_MODE) {
if (argv >= end)
BURP_error(279, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 279: "read_only" or "read_write" required
2001-05-23 15:26:42 +02:00
string = *argv++;
if (!strcmp(string, BURP_SW_MODE_RO))
tdgbl->gbl_sw_mode_val = true;
2001-05-23 15:26:42 +02:00
else if (!strcmp(string, BURP_SW_MODE_RW))
tdgbl->gbl_sw_mode_val = false;
2001-05-23 15:26:42 +02:00
else
BURP_error(279, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 279: "read_only" or "read_write" required
tdgbl->gbl_sw_mode = true;
2001-05-23 15:26:42 +02:00
}
else if (in_sw_tab->in_sw == IN_SW_BURP_PASS) {
if (argv >= end)
BURP_error(189, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// password parameter missing
2001-05-23 15:26:42 +02:00
tdgbl->gbl_sw_password = *argv++;
}
else if (in_sw_tab->in_sw == IN_SW_BURP_USER) {
if (argv >= end)
BURP_error(188, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// user name parameter missing
2001-05-23 15:26:42 +02:00
tdgbl->gbl_sw_user = *argv++;
}
else if (in_sw_tab->in_sw == IN_SW_BURP_ROLE) {
if (argv >= end)
BURP_error(253, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// SQL role parameter missing
2001-05-23 15:26:42 +02:00
tdgbl->gbl_sw_sql_role = *argv++;
}
else if (in_sw_tab->in_sw == IN_SW_BURP_FA) {
if (argv >= end)
BURP_error(182, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 182 blocking factor parameter missing
tdgbl->gbl_sw_blk_factor = get_number(*argv);
2001-05-23 15:26:42 +02:00
if (!tdgbl->gbl_sw_blk_factor)
BURP_error(183, true, *argv, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 183 expected blocking factor, encountered "%s"
2001-05-23 15:26:42 +02:00
argv++;
}
else if (in_sw_tab->in_sw == IN_SW_BURP_SE) {
if (argv >= end) {
BURP_error(273, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 273: service name parameter missing
2001-05-23 15:26:42 +02:00
}
in_sw_tab->in_sw_state = FALSE;
++argv; // skip a service specification
2001-05-23 15:26:42 +02:00
}
// want to do output redirect handling now instead of waiting
2001-05-23 15:26:42 +02:00
else if (in_sw_tab->in_sw == IN_SW_BURP_Y) {
const TEXT* redirect = *argv;
if (argv >= end) // redirect may equal NULL
2001-05-23 15:26:42 +02:00
redirect = NULL;
else if (*redirect == *switch_char)
2001-05-23 15:26:42 +02:00
redirect = NULL;
else
++argv;
if (!redirect)
BURP_error(4, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 4 redirect location for output is not specified
2001-05-23 15:26:42 +02:00
const TEXT *p = redirect;
TEXT c;
2004-05-12 23:47:36 +02:00
const TEXT* q2 = output_suppress;
2001-05-23 15:26:42 +02:00
tdgbl->sw_redirect = NOOUTPUT;
while (c = *p++) {
2004-05-12 23:47:36 +02:00
if (UPPER(c) != *q2++) {
2003-09-18 23:56:26 +02:00
tdgbl->sw_redirect = REDIRECT;
2001-05-23 15:26:42 +02:00
break;
}
}
2003-09-18 23:56:26 +02:00
if (tdgbl->sw_redirect == REDIRECT) { // not NOREDIRECT, and not NOOUTPUT
2001-05-23 15:26:42 +02:00
// Make sure the status file doesn't already exist
2004-04-29 00:00:03 +02:00
FILE* tmp_outfile = fopen(redirect, fopen_read_type);
if (tmp_outfile) {
2001-05-23 15:26:42 +02:00
BURP_print(66, redirect, 0, 0, 0, 0);
// msg 66 can't open status and error output file %s
2004-04-29 00:00:03 +02:00
fclose(tmp_outfile);
2004-03-07 08:58:55 +01:00
BURP_exit_local(FINI_ERROR, tdgbl);
2001-05-23 15:26:42 +02:00
}
if (!
(tdgbl->output_file =
fopen(redirect, fopen_write_type)))
{
2001-05-23 15:26:42 +02:00
BURP_print(66, redirect, 0, 0, 0, 0);
// msg 66 can't open status and error output file %s
2004-03-07 08:58:55 +01:00
BURP_exit_local(FINI_ERROR, tdgbl);
2001-05-23 15:26:42 +02:00
}
}
} //else if (in_sw_tab->in_sw == IN_SW_BURP_Y)
} // else
} // while (argv < end)
2001-05-23 15:26:42 +02:00
// reverse the linked list of file blocks
2001-05-23 15:26:42 +02:00
tdgbl->gbl_sw_files = NULL;
2004-01-13 10:52:19 +01:00
burp_fil* next_file = NULL;
2001-05-23 15:26:42 +02:00
for (file = file_list; file; file = next_file) {
next_file = file->fil_next;
file->fil_next = tdgbl->gbl_sw_files;
tdgbl->gbl_sw_files = file;
}
// pop off the obviously boring ones, plus do some checking
2001-05-23 15:26:42 +02:00
const TEXT* file1 = NULL;
2001-05-23 15:26:42 +02:00
for (file = tdgbl->gbl_sw_files; file; file = file->fil_next) {
if (!file1)
file1 = file->fil_name;
else if (!file2)
file2 = file->fil_name;
for (file_list = file->fil_next; file_list;
2003-09-14 03:08:18 +02:00
file_list = file_list->fil_next)
{
2001-05-23 15:26:42 +02:00
if (!strcmp(file->fil_name, file_list->fil_name))
BURP_error(9, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 9 mutiple sources or destinations specified
2001-05-23 15:26:42 +02:00
}
}
// Initialize 'dpb'
Firebird::ClumpletWriter dpb(Firebird::ClumpletReader::Tagged, MAX_DPB_SIZE, isc_dpb_version1);
dpb.insertString(isc_dpb_gbak_attach, GDS_VERSION, strlen(GDS_VERSION));
dpb.insertByte(isc_dpb_gsec_attach, 1); // make it possible to have local security backups
2001-05-23 15:26:42 +02:00
for (in_sw_tab = burp_in_sw_table; in_sw_tab->in_sw_name; in_sw_tab++) {
if (in_sw_tab->in_sw_state) {
switch (in_sw_tab->in_sw) {
case (IN_SW_BURP_B):
if (sw_replace)
BURP_error(5, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 5 conflicting switches for backup/restore
2001-05-23 15:26:42 +02:00
sw_replace = IN_SW_BURP_B;
break;
case (IN_SW_BURP_C):
if (sw_replace == IN_SW_BURP_B)
BURP_error(5, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 5 conflicting switches for backup/restore
2001-05-23 15:26:42 +02:00
if (sw_replace != IN_SW_BURP_R)
sw_replace = IN_SW_BURP_C;
break;
case (IN_SW_BURP_CO):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_convert_ext_tables = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_E):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_compress = false;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_G):
dpb.insertTag(isc_dpb_no_garbage_collect);
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_I):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_deactivate_indexes = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_IG):
dpb.insertByte(isc_dpb_damaged, 1);
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_K):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_kill = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_L):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_ignore_limbo = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_M):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_meta = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_MODE):
tdgbl->gbl_sw_mode = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_N):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_novalidity = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_NT): // Backup non-transportable format
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_transportable = false;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_O):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_incremental = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_OL):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_old_descriptions = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_PASS):
dpb.insertString(tdgbl->gbl_sw_service_gbak ?
isc_dpb_password_enc : isc_dpb_password,
tdgbl->gbl_sw_password,
strlen(tdgbl->gbl_sw_password));
break;
2001-05-23 15:26:42 +02:00
case (IN_SW_BURP_R):
if (sw_replace == IN_SW_BURP_B)
BURP_error(5, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 5 conflicting switches for backup/restore
2001-05-23 15:26:42 +02:00
sw_replace = IN_SW_BURP_R;
break;
case (IN_SW_BURP_T):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_transportable = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_U):
BURP_error(7, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 7 protection isn't there yet
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_US):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_no_reserve = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_ROLE):
dpb.insertString(isc_dpb_sql_role_name,
tdgbl->gbl_sw_sql_role,
strlen(tdgbl->gbl_sw_sql_role));
break;
2001-05-23 15:26:42 +02:00
case (IN_SW_BURP_USER):
dpb.insertString(isc_dpb_user_name,
tdgbl->gbl_sw_user,
strlen(tdgbl->gbl_sw_user));
break;
2001-05-23 15:26:42 +02:00
case (IN_SW_BURP_V):
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_verbose = true;
2001-05-23 15:26:42 +02:00
break;
case (IN_SW_BURP_Z):
2003-09-14 03:08:18 +02:00
BURP_print(91, (void*) GDS_VERSION, 0, 0, 0, 0);
// msg 91 gbak version %s
2004-01-13 10:52:19 +01:00
tdgbl->gbl_sw_version = true;
2001-05-23 15:26:42 +02:00
break;
default:
break;
}
}
}
if (!sw_replace)
sw_replace = IN_SW_BURP_B;
if (tdgbl->gbl_sw_page_size)
{
if (sw_replace == IN_SW_BURP_B)
BURP_error(8, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 8 page size is allowed only on restore or create
int temp = tdgbl->gbl_sw_page_size;
2001-05-23 15:26:42 +02:00
{
int curr_pg_size = 1024;
while (curr_pg_size <= MAX_PAGE_SIZE) {
if (temp <= curr_pg_size) {
temp = curr_pg_size;
break;
}
curr_pg_size <<= 1;
}
}
if (temp > MAX_PAGE_SIZE)
{
BURP_error(3, true, isc_arg_number,
2004-03-07 08:58:55 +01:00
(void*)(IPTR) tdgbl->gbl_sw_page_size,
2001-05-23 15:26:42 +02:00
0, NULL, 0, NULL,
0, NULL, 0, NULL);
// msg 3 Page size specified (%ld) greater than limit (MAX_PAGE_SIZE bytes)
2001-05-23 15:26:42 +02:00
}
if (temp != tdgbl->gbl_sw_page_size) {
BURP_print(103, (void*)(IPTR) tdgbl->gbl_sw_page_size,
(void*)(IPTR) temp, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 103 page size specified (%ld bytes) rounded up to %ld bytes
2001-05-23 15:26:42 +02:00
tdgbl->gbl_sw_page_size = temp;
}
}
if (tdgbl->gbl_sw_page_buffers) {
if (sw_replace == IN_SW_BURP_B)
BURP_error(260, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 260 page buffers is allowed only on restore or create
2001-05-23 15:26:42 +02:00
}
if (!tdgbl->gbl_sw_blk_factor || sw_replace != IN_SW_BURP_B)
tdgbl->gbl_sw_blk_factor = 1;
if (!file2)
BURP_error(10, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 10 requires both input and output filenames
2001-05-23 15:26:42 +02:00
if (!strcmp(file1, file2))
BURP_error(11, true, 0, 0, 0, 0, 0);
2003-09-14 03:08:18 +02:00
// msg 11 input and output have the same name. Disallowed.
2001-05-23 15:26:42 +02:00
2004-10-30 07:41:25 +02:00
const time_t clock = time(NULL);
2004-03-07 08:58:55 +01:00
strcpy(tdgbl->gbl_backup_start_time, ctime(&clock));
TEXT* nlp = tdgbl->gbl_backup_start_time +
strlen(tdgbl->gbl_backup_start_time) - 1;
if (*nlp == '\n')
*nlp = 0;
2001-05-23 15:26:42 +02:00
2003-09-18 23:56:26 +02:00
tdgbl->action = (ACT) BURP_alloc_zero(ACT_LEN);
2001-05-23 15:26:42 +02:00
tdgbl->action->act_total = 0;
tdgbl->action->act_file = NULL;
tdgbl->action->act_action = ACT_unknown;
action = open_files(file1, &file2, tdgbl->gbl_sw_verbose,
sw_replace, dpb);
2001-05-23 15:26:42 +02:00
MVOL_init(tdgbl->io_buffer_size);
int result;
2001-05-23 15:26:42 +02:00
tdgbl->service_blk->svc_started();
2001-05-23 15:26:42 +02:00
switch (action) {
case (RESTORE):
tdgbl->gbl_sw_overwrite = (sw_replace == IN_SW_BURP_R);
2001-05-23 15:26:42 +02:00
result = RESTORE_restore(file1, file2);
break;
case (BACKUP):
result = BACKUP_backup(file1, file2);
break;
case (QUIT):
BURP_abort();
2004-08-26 21:09:07 +02:00
return 0;
default:
// result undefined
fb_assert(false);
return 0;
2001-05-23 15:26:42 +02:00
}
if (result != FINI_OK && result != FINI_DB_NOT_ONLINE)
BURP_abort();
2004-03-07 08:58:55 +01:00
BURP_exit_local(result, tdgbl);
2001-05-23 15:26:42 +02:00
return result;
2001-12-24 03:51:06 +01:00
} // try
2003-02-13 13:01:28 +01:00
catch (const std::exception&)
2001-12-24 03:51:06 +01:00
{
2003-09-18 23:56:26 +02:00
// All calls to exit_local(), normal and error exits, wind up here
2001-12-24 03:51:06 +01:00
tdgbl->burp_throw = false;
2004-01-13 10:52:19 +01:00
const int exit_code = tdgbl->exit_code;
2001-12-24 03:51:06 +01:00
// Close the gbak file handles if they still open
2004-01-13 10:52:19 +01:00
for (burp_fil* file = tdgbl->gbl_sw_backup_files; file; file = file->fil_next)
{
2001-12-24 03:51:06 +01:00
if (file->fil_fd != INVALID_HANDLE_VALUE)
2003-09-21 01:28:38 +02:00
close_platf(file->fil_fd);
if (exit_code != FINI_OK
2001-12-24 03:51:06 +01:00
&& (tdgbl->action->act_action == ACT_backup_split
|| tdgbl->action->act_action == ACT_backup))
{
2003-09-21 01:28:38 +02:00
unlink_platf(file->fil_name);
}
2001-12-24 03:51:06 +01:00
}
// Detach from database to release system resources
2001-12-24 03:51:06 +01:00
if (tdgbl->db_handle != 0) {
2004-03-07 08:58:55 +01:00
close_out_transaction(action, &tdgbl->tr_handle);
close_out_transaction(action, &tdgbl->global_trans);
if (isc_detach_database(tdgbl->status_vector, &tdgbl->db_handle))
2001-12-24 03:51:06 +01:00
{
2004-03-07 08:58:55 +01:00
BURP_print_status(tdgbl->status_vector);
2001-12-24 03:51:06 +01:00
}
}
// Close the status output file
2003-09-18 23:56:26 +02:00
if (tdgbl->sw_redirect == REDIRECT && tdgbl->output_file != NULL) {
2004-04-29 00:00:03 +02:00
fclose(tdgbl->output_file);
2001-12-24 03:51:06 +01:00
tdgbl->output_file = NULL;
}
// Free all unfreed memory used by Gbak itself
2001-12-24 03:51:06 +01:00
while (tdgbl->head_of_mem_list != NULL) {
2004-03-07 08:58:55 +01:00
UCHAR* mem = tdgbl->head_of_mem_list;
2001-12-24 03:51:06 +01:00
tdgbl->head_of_mem_list = *((UCHAR **) tdgbl->head_of_mem_list);
gds__free(mem);
}
2004-07-03 02:13:46 +02:00
BurpGlobals::restoreSpecific();
2001-12-24 03:51:06 +01:00
#if defined(DEBUG_GDS_ALLOC) && !defined(SERVICE_THREAD)
2001-12-24 03:51:06 +01:00
gds_alloc_report(0, __FILE__, __LINE__);
#endif
// All returns occur from this point - even normal returns
2001-12-24 03:51:06 +01:00
return exit_code;
}
2001-05-23 15:26:42 +02:00
}
2001-12-24 03:51:06 +01:00
2001-05-23 15:26:42 +02:00
void BURP_abort(void)
{
/**************************************
*
* B U R P _ a b o r t
*
**************************************
*
* Functional description
* Abandon a failed operation.
*
**************************************/
2004-07-03 02:13:46 +02:00
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
2001-05-23 15:26:42 +02:00
2003-09-14 03:08:18 +02:00
BURP_print(83, 0, 0, 0, 0, 0);
// msg 83 Exiting before completion due to errors
2001-05-23 15:26:42 +02:00
tdgbl->service_blk->svc_started();
2001-05-23 15:26:42 +02:00
2004-01-13 10:52:19 +01:00
BURP_exit_local(FINI_ERROR, tdgbl);
2001-05-23 15:26:42 +02:00
}
void BURP_error(USHORT errcode, bool abort,
USHORT arg1_t,
const void *arg1,
USHORT arg2_t,
const void *arg2,
USHORT arg3_t,
const void *arg3,
USHORT arg4_t,
const void *arg4,
USHORT arg5_t,
const void *arg5)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* B U R P _ e r r o r
2001-05-23 15:26:42 +02:00
*
**************************************
*
* Functional description
*
**************************************/
#ifdef SERVICE_THREAD
2004-07-03 02:13:46 +02:00
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
ISC_STATUS *status = tdgbl->service_blk->svc_status;
2001-05-23 15:26:42 +02:00
CMD_UTIL_put_svc_status(status, burp_msg_fac, errcode,
arg1_t, arg1, arg2_t, arg2, arg3_t, arg3,
arg4_t, arg4, arg5_t, arg5);
tdgbl->service_blk->svc_started();
2001-05-23 15:26:42 +02:00
#endif
BURP_msg_partial(256, 0, 0, 0, 0, 0); // msg 256: gbak: ERROR:
2001-05-23 15:26:42 +02:00
BURP_msg_put(errcode, arg1, arg2, arg3, arg4, arg5);
if (abort)
BURP_abort();
}
void BURP_error(USHORT errcode, bool abort,
const void* arg1,
const void* arg2,
const void* arg3,
const void* arg4,
const void* arg5)
{
/**************************************
*
* B U R P _ e r r o r
*
**************************************
*
* Functional description
* Format and print an error message, then punt.
*
**************************************/
BURP_error(errcode, abort, isc_arg_string, arg1, isc_arg_string, arg2,
isc_arg_string, arg3, isc_arg_string, arg4,
isc_arg_string, arg5);
2001-05-23 15:26:42 +02:00
}
void BURP_error_redirect(const ISC_STATUS* status_vector,
2001-05-23 15:26:42 +02:00
USHORT errcode,
const void* arg1,
const void* arg2)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* B U R P _ e r r o r _ r e d i r e c t
*
**************************************
*
* Functional description
* Issue error message. Output messages then abort.
*
**************************************/
BURP_print_status(status_vector);
BURP_error(errcode, true, arg1, arg2, NULL, NULL, NULL);
2001-05-23 15:26:42 +02:00
}
2004-01-13 10:52:19 +01:00
// Raises an exception when the old SEH system would jump to another place.
2004-07-03 02:13:46 +02:00
void BURP_exit_local(int code, BurpGlobals* tdgbl)
2004-01-13 10:52:19 +01:00
{
2004-03-01 05:57:43 +01:00
tdgbl->exit_code = code;
if (tdgbl->burp_throw)
2004-03-01 05:57:43 +01:00
throw std::exception();
2004-01-13 10:52:19 +01:00
}
2001-05-23 15:26:42 +02:00
void BURP_msg_partial( USHORT number,
const void* arg1,
const void* arg2,
const void* arg3,
const void* arg4,
const void* arg5)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* B U R P _ m s g _ p a r t i a l
*
**************************************
*
* Functional description
* Retrieve a message from the error file,
2001-05-23 15:26:42 +02:00
* format it, and print it without a newline.
*
**************************************/
TEXT buffer[256];
gds__msg_format(NULL, burp_msg_fac, number, sizeof(buffer), buffer,
static_cast<const char*>(arg1),
static_cast<const char*>(arg2),
static_cast<const char*>(arg3),
static_cast<const char*>(arg4),
static_cast<const char*>(arg5));
2001-05-23 15:26:42 +02:00
burp_output("%s", buffer);
}
void BURP_msg_put( USHORT number,
const void* arg1,
const void* arg2,
const void* arg3,
const void* arg4,
const void* arg5)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* B U R P _ m s g _ p u t
*
**************************************
*
* Functional description
* Retrieve a message from the error file, format it, and print it.
*
**************************************/
TEXT buffer[256];
gds__msg_format(NULL, burp_msg_fac, number, sizeof(buffer), buffer,
static_cast<const char*>(arg1),
static_cast<const char*>(arg2),
static_cast<const char*>(arg3),
static_cast<const char*>(arg4),
static_cast<const char*>(arg5));
2003-09-18 23:56:26 +02:00
translate_cp(buffer);
2001-05-23 15:26:42 +02:00
burp_output("%s\n", buffer);
}
void BURP_msg_get( USHORT number,
TEXT* output_msg,
const void* arg1,
const void* arg2,
const void* arg3,
const void* arg4,
const void* arg5)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* B U R P _ m s g _ g e t
*
**************************************
*
* Functional description
* Retrieve a message from the error file, format it and copy it to the buffer
*
**************************************/
2004-10-30 07:41:25 +02:00
TEXT buffer[BURP_MSG_GET_SIZE];
2001-05-23 15:26:42 +02:00
2003-09-28 02:35:48 +02:00
gds__msg_format(NULL, burp_msg_fac, number, sizeof(buffer), buffer,
static_cast<const char*>(arg1),
static_cast<const char*>(arg2),
static_cast<const char*>(arg3),
static_cast<const char*>(arg4),
static_cast<const char*>(arg5));
strcpy(output_msg, buffer);
2001-05-23 15:26:42 +02:00
}
2003-12-22 11:00:59 +01:00
void BURP_output_version(void* arg1, const TEXT* arg2)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* B U R P _ o u t p u t _ v e r s i o n
*
**************************************
*
* Functional description
* Callback routine for access method
* printing (specifically show version);
* will accept.
*
**************************************/
burp_output(static_cast<const char*>(arg1), arg2);
2001-05-23 15:26:42 +02:00
}
void BURP_print(USHORT number,
const void* arg1,
const void* arg2,
const void* arg3,
const void* arg4,
const void* arg5)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* B U R P _ p r i n t
*
**************************************
*
* Functional description
* Display a formatted error message
* in a way that VMS or civilized systems
* will accept.
*
**************************************/
BURP_msg_partial(169, 0, 0, 0, 0, 0); // msg 169: gbak:
2001-05-23 15:26:42 +02:00
BURP_msg_put(number, arg1, arg2, arg3, arg4, arg5);
}
void BURP_print_status(const ISC_STATUS* status_vector)
{
/**************************************
*
* B U R P _ p r i n t _ s t a t u s
*
**************************************
*
* Functional description
* Print error message. Use fb_interpret
* to allow redirecting output.
*
**************************************/
if (status_vector) {
2003-12-22 11:00:59 +01:00
const ISC_STATUS* vector = status_vector;
#ifdef SERVICE_THREAD
2004-07-03 02:13:46 +02:00
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
2003-12-22 11:00:59 +01:00
ISC_STATUS* status = tdgbl->service_blk->svc_status;
if (status != status_vector) {
int i = 0;
if (status[1]) {
while (*status && (++i < ISC_STATUS_LENGTH))
status++;
}
for (int j = 0; status_vector[j] && (i < ISC_STATUS_LENGTH); j++, i++)
*status++ = status_vector[j];
}
#endif
SCHAR s[1024];
if (fb_interpret(s, sizeof(s), &vector)) {
2003-09-18 23:56:26 +02:00
translate_cp(s);
2003-09-14 03:08:18 +02:00
BURP_msg_partial(256, 0, 0, 0, 0, 0); // msg 256: gbak: ERROR:
burp_output("%s\n", s);
while (fb_interpret(s, sizeof(s), &vector)) {
2003-09-18 23:56:26 +02:00
translate_cp(s);
2003-09-14 03:08:18 +02:00
BURP_msg_partial(256, 0, 0, 0, 0, 0); // msg 256: gbak: ERROR:
burp_output(" %s\n", s);
}
}
}
}
void BURP_print_warning(const ISC_STATUS* status_vector)
{
/**************************************
*
* B U R P _ p r i n t _ w a r n i n g
*
**************************************
*
* Functional description
* Print warning message. Use fb_interpret
* to allow redirecting output.
*
**************************************/
if (status_vector) {
// skip the error, assert that one does not exist
2003-11-08 00:15:33 +01:00
fb_assert(status_vector[0] == isc_arg_gds);
2003-11-04 00:59:24 +01:00
fb_assert(status_vector[1] == 0);
// print the warning message
const ISC_STATUS* vector = &status_vector[2];
SCHAR s[1024];
if (fb_interpret(s, sizeof(s), &vector)) {
2003-09-18 23:56:26 +02:00
translate_cp(s);
2003-09-14 03:08:18 +02:00
BURP_msg_partial(255, 0, 0, 0, 0, 0); // msg 255: gbak: WARNING:
burp_output("%s\n", s);
while (fb_interpret(s, sizeof(s), &vector)) {
2003-09-18 23:56:26 +02:00
translate_cp(s);
2003-09-14 03:08:18 +02:00
BURP_msg_partial(255, 0, 0, 0, 0, 0); // msg 255: gbak: WARNING:
burp_output(" %s\n", s);
}
}
}
}
2001-05-23 15:26:42 +02:00
void BURP_verbose(USHORT number,
const void* arg1,
const void* arg2,
const void* arg3,
const void* arg4,
const void* arg5)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* B U R P _ v e r b o s e
*
**************************************
*
* Functional description
* Calls BURP_print for displaying a formatted error message
* but only for verbose output. If not verbose then calls
* user defined yieding function.
*
**************************************/
2004-07-03 02:13:46 +02:00
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
2001-05-23 15:26:42 +02:00
if (tdgbl->gbl_sw_verbose)
BURP_print(number, arg1, arg2, arg3, arg4, arg5);
else
burp_output("%s", "");
2001-05-23 15:26:42 +02:00
}
2004-03-11 06:44:32 +01:00
static void close_out_transaction(gbak_action action,
2001-05-23 15:26:42 +02:00
isc_tr_handle* handle)
{
/**************************************
*
* c l o s e _ o u t _ t r a n s a c t i o n
*
**************************************
*
* Functional description
* Make a transaction go away. This is
* important as we need to detach from the
* database so all engine allocated memory is
* returned to the system.
*
**************************************/
2003-04-16 12:18:51 +02:00
ISC_STATUS_ARRAY status_vector;
2001-05-23 15:26:42 +02:00
if (*handle != 0) {
if (action == RESTORE) {
/* Even if the restore failed, commit the transaction so that
* a partial database is at least recovered.
*/
isc_commit_transaction(status_vector, handle);
if (status_vector[1]) {
/* If we can't commit - have to roll it back, as
* we need to close all outstanding transactions before
* we can detach from the database.
*/
isc_rollback_transaction(status_vector, handle);
if (status_vector[1])
BURP_print_status(status_vector);
}
}
else
/* A backup shouldn't touch any data - we ensure that
2001-05-23 15:26:42 +02:00
* by never writing data during a backup, but let's double
* ensure it by doing a rollback
*/
if (isc_rollback_transaction(status_vector, handle))
BURP_print_status(status_vector);
}
}
2003-12-22 11:00:59 +01:00
static SLONG get_number( const SCHAR* string)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* g e t _ n u m b e r
*
**************************************
*
* Functional description
* Convert a string to binary, complaining bitterly if
* the string is bum.
* CVC: where does it complain? It does return zero, nothing else.
2001-05-23 15:26:42 +02:00
**************************************/
SCHAR c;
SLONG value = 0;
2001-05-23 15:26:42 +02:00
2003-12-22 11:00:59 +01:00
for (const SCHAR* p = string; c = *p++;) {
2001-05-23 15:26:42 +02:00
if (c < '0' || c > '9')
return 0;
value *= 10;
value += c - '0';
}
return value;
}
static gbak_action open_files(const TEXT* file1,
const TEXT** file2,
2004-01-13 10:52:19 +01:00
bool sw_verbose,
USHORT sw_replace,
const Firebird::ClumpletWriter& dpb)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* o p e n _ f i l e s
*
**************************************
*
* Functional description
* from the input file names and
* positions, guess what the users
* intention was. Return the type
* of the first file, plus open file
* and db handle.
*
**************************************/
2004-07-03 02:13:46 +02:00
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
ISC_STATUS_ARRAY temp_status;
ISC_STATUS* status_vector = temp_status;
2001-05-23 15:26:42 +02:00
// try to attach the database using the first file_name
2001-05-23 15:26:42 +02:00
if (sw_replace != IN_SW_BURP_C && sw_replace != IN_SW_BURP_R)
if (!(isc_attach_database(status_vector,
(SSHORT) 0,
file1,
2001-05-23 15:26:42 +02:00
&tdgbl->db_handle,
dpb.getBufferLength(),
reinterpret_cast<const char*>(dpb.getBuffer()))))
2001-05-23 15:26:42 +02:00
{
if (sw_replace != IN_SW_BURP_B) {
// msg 13 REPLACE specified, but the first file %s is a database
BURP_error(13, true, file1, 0, 0, 0, 0);
2001-05-23 15:26:42 +02:00
if (isc_detach_database(status_vector, &tdgbl->db_handle)) {
BURP_print_status(status_vector);
}
return QUIT;
}
if (tdgbl->gbl_sw_version) {
// msg 139 Version(s) for database "%s"
2001-05-23 15:26:42 +02:00
BURP_print(139, file1, 0, 0, 0, 0);
isc_version(&tdgbl->db_handle, BURP_output_version, (void*) "\t%s\n");
2001-05-23 15:26:42 +02:00
}
if (sw_verbose)
BURP_print(166, file1, 0, 0, 0, 0);
// msg 166: readied database %s for backup
2001-05-23 15:26:42 +02:00
}
else if (sw_replace == IN_SW_BURP_B ||
2003-11-08 00:15:33 +01:00
(status_vector[1] != isc_io_error
2004-12-24 09:52:39 +01:00
&& status_vector[1] != isc_bad_db_format))
{
2001-05-23 15:26:42 +02:00
BURP_print_status(status_vector);
return QUIT;
}
2004-01-13 10:52:19 +01:00
burp_fil* fil = 0;
2001-05-23 15:26:42 +02:00
if (sw_replace == IN_SW_BURP_B) {
// Now it is safe to skip a db file
2001-05-23 15:26:42 +02:00
tdgbl->gbl_sw_backup_files = tdgbl->gbl_sw_files->fil_next;
tdgbl->gbl_sw_files = tdgbl->gbl_sw_files->fil_next;
2003-11-04 00:59:24 +01:00
fb_assert(strcmp(tdgbl->gbl_sw_files->fil_name, *file2) == 0);
2001-05-23 15:26:42 +02:00
gbak_action flag = BACKUP;
2001-05-23 15:26:42 +02:00
tdgbl->action->act_action = ACT_backup;
for (fil = tdgbl->gbl_sw_files; fil; fil = fil->fil_next)
{
// adjust the file size first
2001-05-23 15:26:42 +02:00
switch (fil->fil_size_code)
{
case size_n:
break;
case size_k:
fil->fil_length *= KBYTE;
break;
case size_m:
fil->fil_length *= MBYTE;
break;
case size_g:
fil->fil_length *= GBYTE;
break;
case size_e:
BURP_error(262, true, fil->fil_name, 0, 0, 0, 0);
// msg 262 size specification either missing or incorrect for file %s
2001-05-23 15:26:42 +02:00
break;
default:
2003-11-04 00:59:24 +01:00
fb_assert(FALSE);
2001-05-23 15:26:42 +02:00
break;
}
2004-12-26 05:09:06 +01:00
if ((fil->fil_seq = ++tdgbl->action->act_total) >= 2)
2001-05-23 15:26:42 +02:00
{
tdgbl->action->act_action = ACT_backup_split;
}
if (sw_verbose)
{
BURP_print(75, fil->fil_name, 0, 0, 0, 0); // msg 75 creating file %s
2001-05-23 15:26:42 +02:00
}
if (!strcmp(fil->fil_name, "stdout"))
{
if (tdgbl->action->act_total >= 2 || fil->fil_next)
{
BURP_error(266, true, 0, 0, 0, 0, 0);
// msg 266 standard output is not supported when using split operation
2001-05-23 15:26:42 +02:00
flag = QUIT;
break;
}
/* We ignore SIGPIPE so that we can report an IO error when we
* try to write to the broken pipe.
*/
#ifndef WIN_NT
signal(SIGPIPE, SIG_IGN);
#endif
fil->fil_fd = GBAK_STDOUT_DESC();
2001-05-23 15:26:42 +02:00
break;
}
else
{
#ifdef WIN_NT
if ((fil->fil_fd = MVOL_open(fil->fil_name, MODE_WRITE,
CREATE_ALWAYS)) == INVALID_HANDLE_VALUE)
#else
if ((fil->fil_fd = open(fil->fil_name, MODE_WRITE, open_mask)) == -1)
#endif // WIN_NT
2001-05-23 15:26:42 +02:00
{
BURP_error(65, false, isc_arg_string, fil->fil_name,
2001-05-23 15:26:42 +02:00
0, NULL, 0, NULL, 0, NULL, 0, NULL);
// msg 65 can't open backup file %s
2001-05-23 15:26:42 +02:00
flag = QUIT;
break;
}
}
if (fil->fil_length == 0)
{
if (fil->fil_next)
{
BURP_error(262, true, fil->fil_name, 0, 0, 0, 0);
// msg 262 size specification either missing or incorrect for file %s
2001-05-23 15:26:42 +02:00
flag = QUIT;
break;
}
else
{
fil->fil_length = MAX_LENGTH;
// Write as much as possible to the last file
2001-05-23 15:26:42 +02:00
}
}
if (fil->fil_length < MIN_SPLIT_SIZE)
{
BURP_error(271, true,
2001-05-23 15:26:42 +02:00
isc_arg_number,
reinterpret_cast<void*>(fil->fil_length),
isc_arg_number,
reinterpret_cast<void*>(MIN_SPLIT_SIZE),
0, NULL, 0, NULL, 0, NULL);
// msg file size given (%d) is less than minimum allowed (%d)
2001-05-23 15:26:42 +02:00
flag = QUIT;
break;
}
}
if (flag == BACKUP)
{
tdgbl->action->act_file = tdgbl->gbl_sw_files;
tdgbl->file_desc = tdgbl->gbl_sw_files->fil_fd;
}
else
{
if (isc_detach_database(status_vector, &tdgbl->db_handle))
{
BURP_print_status(status_vector);
}
}
return flag;
}
/*
* If we got to here, then we're really not backing up a database,
* so open a backup file.
*/
/* There are four possible cases such as:
*
* 1. restore single backup file to single db file
* 2. restore single backup file to multiple db files
* 3. restore multiple backup files (join operation) to single db file
* 4. restore multiple backup files (join operation) to multiple db files
*
* Just looking at the command line, we can't say for sure whether it is a
* specification of the last file to be join or it is a specification of the
* primary db file (case 4), for example:
*
* gbak -c gbk1 gbk2 gbk3 db1 200 db2 500 db3 -v
* ^^^
* db1 could be either the last file to be join or primary db file
*
* Since 'gbk' and 'gsplit' formats are different (gsplit file has its own
* header record) hence we can use it as follows:
*
* - open first file
* - read & check a header record
*
* If a header is identified as a 'gsplit' one then we know exactly how
* many files need to be join and in which order. We keep opening a file by
* file till we reach the last one to be join. During this step we check
* that the files are accessible and are in proper order. It gives us
* possibility to let silly customer know about an error as soon as possible.
* Besides we have to find out which file is going to be a db file.
*
* If header is not identified as a 'gsplit' record then we assume that
* we got a single backup file.
*/
fil = tdgbl->gbl_sw_files;
tdgbl->gbl_sw_backup_files = tdgbl->gbl_sw_files;
tdgbl->action->act_action = ACT_restore;
if (!strcmp(fil->fil_name, "stdin")) {
fil->fil_fd = GBAK_STDIN_DESC();
2001-05-23 15:26:42 +02:00
tdgbl->file_desc = fil->fil_fd;
tdgbl->gbl_sw_files = fil->fil_next;
}
else {
// open first file
2001-05-23 15:26:42 +02:00
#ifdef WIN_NT
if ((fil->fil_fd = MVOL_open(fil->fil_name, MODE_READ, OPEN_EXISTING))
== INVALID_HANDLE_VALUE)
#else
if ((fil->fil_fd = open(fil->fil_name, MODE_READ)) ==
INVALID_HANDLE_VALUE)
#endif
{
BURP_error(65, true, fil->fil_name, 0, 0, 0, 0);
// msg 65 can't open backup file %s
2001-05-23 15:26:42 +02:00
return QUIT;
}
if (sw_verbose)
BURP_print(100, fil->fil_name, 0, 0, 0, 0);
// msg 100 opened file %s
// read and check a header record
2001-05-23 15:26:42 +02:00
tdgbl->action->act_file = fil;
int seq = 1;
2001-05-23 15:26:42 +02:00
if (MVOL_split_hdr_read() == TRUE) {
tdgbl->action->act_action = ACT_restore_join;
// number of files to be join
2004-10-30 07:41:25 +02:00
const int total = tdgbl->action->act_total;
2001-05-23 15:26:42 +02:00
if (fil->fil_seq != seq || seq > total) {
BURP_error(263, true, fil->fil_name, 0, 0, 0, 0);
// msg 263 file %s out of sequence
2001-05-23 15:26:42 +02:00
return QUIT;
}
for (++seq, fil = fil->fil_next; seq <= total;
fil = fil->fil_next, seq++)
{
2001-05-23 15:26:42 +02:00
if (!fil) {
BURP_error(264, true, 0, 0, 0, 0, 0);
// msg 264 can't join -- one of the files missing
2001-05-23 15:26:42 +02:00
return QUIT;
}
if (!strcmp(fil->fil_name, "stdin")) {
BURP_error(265, true, 0, 0, 0, 0, 0);
// msg 265 standard input is not supported when using join operation
2001-05-23 15:26:42 +02:00
return QUIT;
}
tdgbl->action->act_file = fil;
#ifdef WIN_NT
if ((fil->fil_fd = MVOL_open(fil->fil_name, MODE_READ,
OPEN_EXISTING)) ==
INVALID_HANDLE_VALUE)
#else
if ((fil->fil_fd = open(fil->fil_name, MODE_READ))
== INVALID_HANDLE_VALUE)
#endif
{
BURP_error(65, false, isc_arg_string, fil->fil_name,
2001-05-23 15:26:42 +02:00
0, NULL, 0, NULL, 0, NULL, 0, NULL);
// msg 65 can't open backup file %s
2001-05-23 15:26:42 +02:00
return QUIT;
}
if (sw_verbose)
BURP_print(100, fil->fil_name, 0, 0, 0, 0);
// msg 100 opened file %s
2001-05-23 15:26:42 +02:00
if (MVOL_split_hdr_read() == TRUE) {
if ((total != tdgbl->action->act_total) ||
(seq != fil->fil_seq) || (seq > total))
{
BURP_error(263, true, fil->fil_name, 0, 0, 0, 0);
// msg 263 file %s out of sequence
2001-05-23 15:26:42 +02:00
return QUIT;
}
}
else {
BURP_error(267, true, fil->fil_name, 0, 0, 0, 0);
// msg 267 backup file %s might be corrupt
2001-05-23 15:26:42 +02:00
return QUIT;
}
}
tdgbl->action->act_file = tdgbl->gbl_sw_files;
tdgbl->file_desc = tdgbl->action->act_file->fil_fd;
if ((tdgbl->gbl_sw_files = fil) == NULL) {
BURP_error(268, true, 0, 0, 0, 0, 0);
// msg 268 database file specification missing
2001-05-23 15:26:42 +02:00
return QUIT;
}
}
else {
// Move pointer to the begining of the file. At this point we
// assume -- this is a single backup file because we were
// not able to read a split header record.
2001-05-23 15:26:42 +02:00
#ifdef WIN_NT
if (strnicmp(fil->fil_name, "\\\\.\\tape", 8))
SetFilePointer(fil->fil_fd, 0, NULL, FILE_BEGIN);
else
SetTapePosition(fil->fil_fd, TAPE_REWIND, 0, 0, 0, FALSE);
#else
lseek(fil->fil_fd, 0, SEEK_SET);
#endif
tdgbl->file_desc = fil->fil_fd;
tdgbl->gbl_sw_files = fil->fil_next;
}
}
// If we got here, we've opened a backup file, and we're
// thinking about creating or replacing a database.
2001-05-23 15:26:42 +02:00
*file2 = tdgbl->gbl_sw_files->fil_name;
if (tdgbl->gbl_sw_files->fil_size_code != size_n)
BURP_error(262, true, *file2, 0, 0, 0, 0);
// msg 262 size specificati on either missing or incorrect for file %s
2001-05-23 15:26:42 +02:00
if ((sw_replace == IN_SW_BURP_C || sw_replace == IN_SW_BURP_R) &&
!isc_attach_database(status_vector,
(SSHORT) 0,
*file2,
&tdgbl->db_handle,
dpb.getBufferLength(),
reinterpret_cast<const char*>(dpb.getBuffer())))
2001-05-23 15:26:42 +02:00
{
if (sw_replace == IN_SW_BURP_C) {
if (isc_detach_database(status_vector, &tdgbl->db_handle)) {
BURP_print_status(status_vector);
}
BURP_error(14, true, *file2, 0, 0, 0, 0);
// msg 14 database %s already exists. To replace it, use the -R switch
2001-05-23 15:26:42 +02:00
}
2005-04-18 05:24:34 +02:00
else {
2001-05-23 15:26:42 +02:00
isc_drop_database(status_vector, &tdgbl->db_handle);
if (tdgbl->db_handle) {
2003-04-16 12:18:51 +02:00
ISC_STATUS_ARRAY status_vector2;
2001-05-23 15:26:42 +02:00
if (isc_detach_database(status_vector2, &tdgbl->db_handle)) {
BURP_print_status(status_vector2);
}
// Complain only if the drop database entrypoint is available.
// If it isn't, the database will simply be overwritten.
2001-05-23 15:26:42 +02:00
2003-11-08 00:15:33 +01:00
if (status_vector[1] != isc_unavailable)
BURP_error(233, true, *file2, 0, 0, 0, 0);
// msg 233 Cannot drop database %s, might be in use
2001-05-23 15:26:42 +02:00
}
}
}
if (sw_replace == IN_SW_BURP_R && status_vector[1] == isc_adm_task_denied) {
/* if we got an error from attach database and we have replace switch set
* then look for error from attach returned due to not owner, if we are
* not owner then return the error status back up
2001-05-23 15:26:42 +02:00
*/
BURP_error(274, true, 0, 0, 0, 0, 0);
2001-05-23 15:26:42 +02:00
/* msg # 274 : Cannot restore over current database, must be sysdba
* or owner of the existing database.
2001-05-23 15:26:42 +02:00
*/
}
/* if we got here, then all is well, remove any error condition from the
* status vector when running as a service thread. If we don't then the
* service will think that there is an error if isc_attach_database failed
* like it should have (if creating a database).
*/
if (tdgbl->gbl_sw_service_thd)
2003-04-10 08:32:58 +02:00
memset(tdgbl->status, 0, ISC_STATUS_LENGTH * sizeof(ISC_STATUS));
2001-05-23 15:26:42 +02:00
// check the file size specification
2001-05-23 15:26:42 +02:00
for (fil = tdgbl->gbl_sw_files; fil; fil = fil->fil_next) {
if (fil->fil_size_code != size_n)
BURP_error(262, true, fil->fil_name, 0, 0, 0, 0);
// msg 262 size specification either missing or incorrect for file %s
2001-05-23 15:26:42 +02:00
}
return RESTORE;
}
static void burp_output( const SCHAR* format, ...)
2001-05-23 15:26:42 +02:00
{
/**************************************
*
* b u r p _ o u t p u t
*
**************************************
*
* Functional description
* Platform independent output routine.
*
**************************************/
va_list arglist;
UCHAR buf[1000];
int exit_code;
2004-07-03 02:13:46 +02:00
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
2001-05-23 15:26:42 +02:00
if (tdgbl->sw_redirect == NOOUTPUT || format[0] == '\0') {
exit_code =
tdgbl->output_proc(tdgbl->output_data,
2001-07-12 07:46:06 +02:00
(UCHAR*)(""));
2001-05-23 15:26:42 +02:00
}
2003-09-18 23:56:26 +02:00
else if (tdgbl->sw_redirect == REDIRECT && tdgbl->output_file != NULL) {
va_start(arglist, format);
2004-04-29 00:00:03 +02:00
vfprintf(tdgbl->output_file, format, arglist);
2001-05-23 15:26:42 +02:00
va_end(arglist);
exit_code =
tdgbl->output_proc(tdgbl->output_data,
2001-07-12 07:46:06 +02:00
(UCHAR*)(""));
2001-05-23 15:26:42 +02:00
}
else {
va_start(arglist, format);
2001-05-23 15:26:42 +02:00
vsprintf((char *) buf, format, arglist);
va_end(arglist);
exit_code = tdgbl->output_proc(tdgbl->output_data, buf);
}
if (exit_code != 0)
2004-01-13 10:52:19 +01:00
BURP_exit_local(exit_code, tdgbl);
2001-05-23 15:26:42 +02:00
}
static void burp_usage()
{
/**********************************************
*
* b u r p _ u s a g e
*
**********************************************
*
* Functional description
* print usage information
*
**********************************************/
BURP_print(95, 0, 0, 0, 0, 0);
// msg 95 legal switches are
2005-04-18 05:24:34 +02:00
const in_sw_tab_t* in_sw_tab = burp_in_sw_table;
for (; in_sw_tab->in_sw; in_sw_tab++)
if (in_sw_tab->in_sw_msg) {
BURP_msg_put(in_sw_tab->in_sw_msg, (void*)switch_char, 0, 0,
0, 0);
}
BURP_print(132, 0, 0, 0, 0, 0);
// msg 132 switches can be abbreviated to one character
}
2004-01-13 10:52:19 +01:00
static ULONG get_size( const SCHAR* string, burp_fil* file)
2001-05-23 15:26:42 +02:00
{
/**********************************************
*
* g e t _ s i z e
*
**********************************************
*
* Functional description
* Get size specification for either splitting or
* restoring to multiple files
*
**********************************************/
SCHAR c;
ULONG size = 0;
bool digit = false;
2001-05-23 15:26:42 +02:00
file->fil_size_code = size_n;
for (const SCHAR *num = string; c = *num++;) {
2001-05-23 15:26:42 +02:00
if (isdigit(c)) {
size = size * 10 + (c - '0');
2003-09-10 19:52:12 +02:00
digit = true;
2001-05-23 15:26:42 +02:00
}
else {
if (isalpha(c)) {
if (!digit) {
file->fil_size_code = size_e;
size = 0;
break;
}
switch (UPPER(c)) {
case 'K':
file->fil_size_code = size_k;
break;
case 'M':
file->fil_size_code = size_m;
break;
case 'G':
file->fil_size_code = size_g;
break;
default:
file->fil_size_code = size_e;
size = 0;
break;
}
if (*num) {
file->fil_size_code = size_e;
size = 0;
}
break;
}
}
}
file->fil_length = size;
return (size);
2001-05-23 15:26:42 +02:00
}