~drizzle-trunk/drizzle/development

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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
/* Copyright (c) 2009 PrimeBase Technologies GmbH, Germany
 *
 * PrimeBase Media Stream for MySQL
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Barry Leslie
 *
 * 2009-10-27
 *
 * System backup info table for repository backups.
 */
#include "CSConfig.h"
#include <inttypes.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>

#ifdef DRIZZLED
#include <drizzled/server_includes.h>
#endif

//#include "mysql_priv.h"
#include "CSGlobal.h"
#include "CSStrUtil.h"
#include "CSLog.h"
#include "CSPath.h"
#include "CSDirectory.h"

#include "ha_pbms.h"
//#include <plugin.h>

#include "ms_mysql.h"
#include "Database_ms.h"
#include "OpenTable_ms.h"
#include "Util_ms.h"
#include "Discover_ms.h"
#include "SysTab_util.h"
#include "backup_ms.h"

#include "SysTab_backup.h"


DT_FIELD_INFO pbms_backup_info[]=
{
	{"Id",				NULL,	NULL, MYSQL_TYPE_LONG,		NULL,			NOT_NULL_FLAG,	"The backup reference ID"},
	{"Database_Name",	64,		NULL, MYSQL_TYPE_VARCHAR,	&UTF8_CHARSET,	NOT_NULL_FLAG,	"The database name"},
	{"Database_Id",		NULL,	NULL, MYSQL_TYPE_LONG,		NULL,			0,	"The database ID"},
	{"Started",			32,		NULL, MYSQL_TYPE_VARCHAR,	&UTF8_CHARSET,	0,	"The start time"},
	{"Completed",		32,		NULL, MYSQL_TYPE_VARCHAR,	&UTF8_CHARSET,	0,	"The completion time"},
	{"IsRunning",		3,		NULL, MYSQL_TYPE_VARCHAR,	&UTF8_CHARSET,	0,	"Is the backup still running"},
	{"IsDump",			3,		NULL, MYSQL_TYPE_VARCHAR,	&UTF8_CHARSET,	0,	"Is the backup the result of a dump"},
	{"Location",		1024,	NULL, MYSQL_TYPE_VARCHAR,	&UTF8_CHARSET,	0,	"The backup location"},
	{"Cloud_Ref",		NULL,	NULL, MYSQL_TYPE_LONG,		NULL,			0,	"The S3 cloud reference number refering to the pbms.pbms_cloud table."},
	{"Cloud_Backup_No",	NULL,	NULL, MYSQL_TYPE_LONG,		NULL,			0,	"The cloud backup number"},
	{NULL,NULL, NULL, MYSQL_TYPE_STRING,NULL, 0, NULL}
};

DT_KEY_INFO pbms_backup_keys[]=
{
	{"pbms_backup_pk", PRI_KEY_FLAG, {"Id", NULL}},
	{NULL, 0, {NULL}}
};

#define MIN_BACKUP_TABLE_FILE_SIZE 4


//----------------------------
void MSBackupTable::startUp()
{
	MSBackupInfo::startUp();
}

//----------------------------
void MSBackupTable::shutDown()
{
	MSBackupInfo::shutDown();
}

//----------------------------
void MSBackupTable::loadTable(MSDatabase *db)
{

	enter_();
	
	push_(db);
	lock_(MSBackupInfo::gBackupInfo);
	
	if (MSBackupInfo::gMaxInfoRef == 0) {
		CSPath	*path;
		path = getSysFile(getPBMSPath(RETAIN(db->myDatabasePath)), BACKUP_TABLE_NAME, MIN_BACKUP_TABLE_FILE_SIZE);
		push_(path);

		if (path->exists()) {
			CSFile		*file;
			SysTabRec	*backupData;
			const char	*name, *location;
			uint32_t		info_id, db_id, start, end, cloud_ref, cloud_backup_no;
			bool		isDump;
			MSBackupInfo	*info;
			size_t		size;
			
			new_(backupData, SysTabRec("pbms", BACKUP_TABLE_NAME".dat", BACKUP_TABLE_NAME));
			push_(backupData);

			file = path->openFile(CSFile::READONLY);
			push_(file);
			size = file->getEOF();
			backupData->setLength(size);
			file->read(backupData->getBuffer(0), 0, size, size);
			release_(file);
			
			backupData->firstRecord();
			MSBackupInfo::gMaxInfoRef = backupData->getInt4Field();
			
			if (! backupData->isValidRecord()) 
				MSBackupInfo::gMaxInfoRef = 1;
			
			while (backupData->nextRecord()) {
				info_id = backupData->getInt4Field();
				name = backupData->getStringField();
				db_id = backupData->getInt4Field();
				start = backupData->getInt4Field();
				end = backupData->getInt4Field();
				isDump = backupData->getInt1Field();
				location = backupData->getStringField();
				cloud_ref = backupData->getInt4Field();
				cloud_backup_no = backupData->getInt4Field();
				
				if (backupData->isValidRecord()) {
					if (info_id > MSBackupInfo::gMaxInfoRef) {
						char msg[80];
						snprintf(msg, 80, "backup info id (%"PRIu32") larger than expected (%"PRIu32")\n", info_id, MSBackupInfo::gMaxInfoRef);
						CSL.log(self, CSLog::Warning, "pbms "BACKUP_TABLE_NAME".dat :possible damaged file or record. ");
						CSL.log(self, CSLog::Warning, msg);
						MSBackupInfo::gMaxInfoRef = info_id +1;
					}
					if ( MSBackupInfo::gBackupInfo->get(info_id)) {
						char msg[80];
						snprintf(msg, 80, "Duplicate Backup info id (%"PRIu32") being ignored\n", info_id);
						CSL.log(self, CSLog::Warning, "pbms "BACKUP_TABLE_NAME".dat :possible damaged file or record. ");
						CSL.log(self, CSLog::Warning, msg);
					} else {
						new_(info, MSBackupInfo(info_id, name, db_id, start, end, isDump, location, cloud_ref, cloud_backup_no));
						MSBackupInfo::gBackupInfo->set(info_id, info);
					}
				}
			}
			release_(backupData); backupData = NULL;
			
		} else
			MSBackupInfo::gMaxInfoRef = 1;
		
		release_(path);
		
	}
	unlock_(MSBackupInfo::gBackupInfo);

	release_(db);

	exit_();
}

void MSBackupTable::saveTable(MSDatabase *db)
{
	SysTabRec		*backupData;
	MSBackupInfo		*info;
	enter_();
	
	push_(db);
	
	new_(backupData, SysTabRec("pbms", BACKUP_TABLE_NAME".dat", BACKUP_TABLE_NAME));
	push_(backupData);
	
	// Build the table records
	backupData->clear();
	lock_(MSBackupInfo::gBackupInfo);
	
	backupData->beginRecord();	
	backupData->setInt4Field(MSBackupInfo::gMaxInfoRef);
	backupData->endRecord();	
	for  (int i = 0;(info = (MSBackupInfo*) MSBackupInfo::gBackupInfo->itemAt(i)); i++) { // info is not referenced.
		
		backupData->beginRecord();	
		backupData->setInt4Field(info->getBackupRefId());
		
		backupData->setStringField(info->getName());
		backupData->setInt4Field(info->getDatabaseId());
		backupData->setInt4Field(info->getStart());
		backupData->setInt4Field(info->getEnd());
		backupData->setInt1Field(info->isDump());
		backupData->setStringField(info->getLocation());
		backupData->setInt4Field(info->getcloudRef());
		backupData->setInt4Field(info->getcloudBackupNo());
		backupData->endRecord();			
	}
	unlock_(MSBackupInfo::gBackupInfo);

	restoreTable(RETAIN(db), backupData->getBuffer(0), backupData->length(), false);
	
	release_(backupData);
	release_(db);
	exit_();
}


MSBackupTable::MSBackupTable(MSSystemTableShare *share, TABLE *table):
MSOpenSystemTable(share, table),
iBackupIndex(0)
{
}

MSBackupTable::~MSBackupTable()
{
	//unuse();
}

void MSBackupTable::use()
{
	MSBackupInfo::gBackupInfo->lock();
}

void MSBackupTable::unuse()
{
	MSBackupInfo::gBackupInfo->unlock();
	
}


void MSBackupTable::seqScanInit()
{
	iBackupIndex = 0;
}

bool MSBackupTable::seqScanNext(char *buf)
{
	TABLE		*table = mySQLTable;
	Field		*curr_field;
	byte		*save;
	MY_BITMAP	*save_write_set;
	MSBackupInfo	*info;
	CSTime		*timeVal;
	const char	*val;
	
	enter_();
	
	info = (MSBackupInfo	*) MSBackupInfo::gBackupInfo->itemAt(iBackupIndex++); // Object is not referenced.
	if (!info)
		return_(false);
	
	save_write_set = table->write_set;
	table->write_set = NULL;

	new_(timeVal, CSTime());
	push_(timeVal);
	memset(buf, 0xFF, table->s->null_bytes);
 	for (Field **field=table->field ; *field ; field++) {
 		curr_field = *field;
		save = curr_field->ptr;
#if MYSQL_VERSION_ID < 50114
		curr_field->ptr = (byte *) buf + curr_field->offset();
#else
		curr_field->ptr = (byte *) buf + curr_field->offset(curr_field->table->record[0]);
#endif
		switch (curr_field->field_name[0]) {
			case 'I':
				if (curr_field->field_name[1] == 'd') {
					ASSERT(strcmp(curr_field->field_name, "Id") == 0);
					curr_field->store(info->getBackupRefId(), true);
					ms_my_set_notnull_in_record(curr_field, buf);
				} else if (curr_field->field_name[2] == 'D') {
					ASSERT(strcmp(curr_field->field_name, "IsDump") == 0);
					val = (info->isDump())? "Yes": "No";
					curr_field->store(val, strlen(val), &UTF8_CHARSET);
					ms_my_set_notnull_in_record(curr_field, buf);
				} else {
					ASSERT(strcmp(curr_field->field_name, "IsRunning") == 0);
					val = (info->isBackupRunning())? "Yes": "No";
					curr_field->store(val, strlen(val), &UTF8_CHARSET);
					ms_my_set_notnull_in_record(curr_field, buf);
				} 
				break;

			case 'D':
				if (curr_field->field_name[9] == 'I') {
					ASSERT(strcmp(curr_field->field_name, "Database_Id") == 0);
					curr_field->store(info->getDatabaseId(), true);
					ms_my_set_notnull_in_record(curr_field, buf);
				} else {
					ASSERT(strcmp(curr_field->field_name, "Database_Name") == 0);
					val = info->getName();
					curr_field->store(val, strlen(val), &UTF8_CHARSET);
					ms_my_set_notnull_in_record(curr_field, buf);
				}
				
				break;

			case 'S': 
				ASSERT(strcmp(curr_field->field_name, "Started") == 0);
				if (info->getStart()) {
					timeVal->setUTC1970(info->getStart(), 0);
					val = timeVal->getCString();
					curr_field->store(val, strlen(val), &UTF8_CHARSET);
					ms_my_set_notnull_in_record(curr_field, buf);
				}
				break;

			case 'L':
				ASSERT(strcmp(curr_field->field_name, "Location") == 0);
				val = info->getLocation();
				if (val) {
					curr_field->store(val, strlen(val), &UTF8_CHARSET);
					ms_my_set_notnull_in_record(curr_field, buf);
				}
				break;

			case 'C': 
				if (curr_field->field_name[1] == 'o') {
					ASSERT(strcmp(curr_field->field_name, "Completed") == 0);
					if (info->getEnd()) {
						timeVal->setUTC1970(info->getEnd(), 0);
						val = timeVal->getCString();
						curr_field->store(val, strlen(val), &UTF8_CHARSET);
						ms_my_set_notnull_in_record(curr_field, buf);
					}
				} else if (curr_field->field_name[6] == 'R') {
					ASSERT(strcmp(curr_field->field_name, "Cloud_Ref") == 0);
					curr_field->store(info->getcloudRef(), true);
					ms_my_set_notnull_in_record(curr_field, buf);
				} else if (curr_field->field_name[6] == 'B') {
					ASSERT(strcmp(curr_field->field_name, "Cloud_Backup_No") == 0);
					curr_field->store(info->getcloudBackupNo(), true);
					ms_my_set_notnull_in_record(curr_field, buf);
				} else {
					ASSERT(false);
					break;
				}
				break;
				
			default:
				ASSERT(false);
		}
		curr_field->ptr = save;
	}

	release_(timeVal);
	table->write_set = save_write_set;
	
	return_(true);
}

void MSBackupTable::seqScanPos(uint8_t *pos)
{
	int32_t index = iBackupIndex -1;
	if (index < 0)
		index = 0; // This is probably an error condition.
		
	mi_int4store(pos, index);
}

void MSBackupTable::seqScanRead(uint8_t *pos, char *buf)
{
	iBackupIndex = mi_uint4korr(pos);
	seqScanNext(buf);
}

void MSBackupTable::updateRow(char *old_data, char *new_data) 
{
	TABLE	*table = mySQLTable;	
	uint32_t n_id, db_id, cloud_ref, cloud_backup_no, n_indx;
	uint32_t o_id, o_db_id, o_cloud_ref, o_cloud_backup_no, o_indx;
	String name_val, start_val, end_val, isRunning_val, isDump_val, location_val, *name = &name_val, *start = &start_val, *end = &end_val, *isRunning = &isRunning_val, *isDump = &isDump_val, *location = &location_val;
	String o_name_val, o_start_val, o_end_val, o_isRunning_val, o_isDump_val, o_location_val, *o_name = &o_name_val, *o_start = &o_start_val, *o_end = &o_end_val, *o_isRunning = &o_isRunning_val, *o_isDump = &o_isDump_val, *o_location = &o_location_val;
	MSBackupInfo *info, *old_info;

	enter_();
	
	GET_INT_FIELD(o_id, 0, table, old_data);
	GET_INT_FIELD(n_id, 0, table, new_data);
		
	GET_STR_FIELD(name, 1, table, new_data);
	GET_INT_FIELD(db_id, 2, table, new_data);
	GET_STR_FIELD(start, 3, table, new_data);
	GET_STR_FIELD(end, 4, table, new_data);
	GET_STR_FIELD(isRunning, 5, table, new_data); 
	GET_STR_FIELD(isDump, 6, table, new_data);
	GET_STR_FIELD(location, 7, table, new_data);
	GET_INT_FIELD(cloud_ref, 8, table, new_data);
	GET_INT_FIELD(cloud_backup_no, 9, table, new_data);
	
	GET_STR_FIELD(o_name, 1, table, old_data);
	GET_INT_FIELD(o_db_id, 2, table, old_data);
	GET_STR_FIELD(o_start, 3, table, old_data);
	GET_STR_FIELD(o_end, 4, table, old_data);
	GET_STR_FIELD(o_isRunning, 5, table, new_data); 
	GET_STR_FIELD(o_isDump, 6, table, old_data);
	GET_STR_FIELD(o_location, 7, table, old_data);
	GET_INT_FIELD(o_cloud_ref, 8, table, old_data);
	GET_INT_FIELD(o_cloud_backup_no, 9, table, old_data);
	
	// The only fields that are allowed to be updated are 'Location' and 'Cloud_Ref'.
	// It makes no scence to update any of the other fields.
	if (n_id != o_id )
		CSException::throwException(CS_CONTEXT, HA_ERR_TABLE_READONLY, "Attempt to update read only field (Id) in the "BACKUP_TABLE_NAME" table.");
	
	if (strcmp(name->c_ptr(), o_name->c_ptr()) == 0 )
		CSException::throwException(CS_CONTEXT, HA_ERR_TABLE_READONLY, "Attempt to update read only field (Database_Name) in the "BACKUP_TABLE_NAME" table.");
	
	if (db_id != o_db_id )
		CSException::throwException(CS_CONTEXT, HA_ERR_TABLE_READONLY, "Attempt to update read only field (Database_Id) in the "BACKUP_TABLE_NAME" table.");
	
	if (strcmp(start->c_ptr(), o_start->c_ptr()) == 0 )
		CSException::throwException(CS_CONTEXT, HA_ERR_TABLE_READONLY, "Attempt to update read only field (Started) in the "BACKUP_TABLE_NAME" table.");
	
	if (strcmp(end->c_ptr(), o_end->c_ptr()) == 0 )
		CSException::throwException(CS_CONTEXT, HA_ERR_TABLE_READONLY, "Attempt to update read only field (Completed) in the "BACKUP_TABLE_NAME" table.");
	
	if (strcmp(isRunning->c_ptr(), o_isRunning->c_ptr()) == 0 )
		CSException::throwException(CS_CONTEXT, HA_ERR_TABLE_READONLY, "Attempt to update read only field (isRunning) in the "BACKUP_TABLE_NAME" table.");
	
	if (strcmp(isDump->c_ptr(), o_isDump->c_ptr()) == 0 )
		CSException::throwException(CS_CONTEXT, HA_ERR_TABLE_READONLY, "Attempt to update read only field (IsDump) in the "BACKUP_TABLE_NAME" table.");
	
	if (cloud_backup_no != o_cloud_backup_no )
		CSException::throwException(CS_CONTEXT, HA_ERR_TABLE_READONLY, "Attempt to update read only field (Cloud_Backup_No) in the "BACKUP_TABLE_NAME" table.");

	old_info = (MSBackupInfo*)  MSBackupInfo::gBackupInfo->get(o_id); // A non referenced object.
	
	new_(info, MSBackupInfo(n_id, old_info->getName(), db_id, old_info->getStart(), old_info->getEnd(), old_info->isDump(), location->c_ptr(), cloud_ref, cloud_backup_no));
	push_(info);
	
	o_indx = MSBackupInfo::gBackupInfo->getIndex(o_id);

	MSBackupInfo::gBackupInfo->remove(o_id);
	pop_(info);
	MSBackupInfo::gBackupInfo->set(n_id, info);
	
	// Adjust the current position in the array if required.
	n_indx = MSBackupInfo::gBackupInfo->getIndex(n_id);
	if (o_indx < n_indx )
		iBackupIndex--;

	saveTable(RETAIN(myShare->mySysDatabase));

	exit_();
}

void MSBackupTable::insertRow(char *data) 
{
	TABLE	*table = mySQLTable;	
	uint32_t ref_id = 0, db_id, cloud_ref, cloud_backup_no;
	String name_val, start_val, end_val, isRunning_val, isDump_val, location_val, *name = &name_val, *start = &start_val, *end = &end_val, *isRunning = &isRunning_val, *isDump = &isDump_val, *location = &location_val;
	MSBackupInfo *info = NULL;
	const char *db_name;
	bool duplicate = true;

	enter_();

	try_(a) {
		GET_INT_FIELD(ref_id, 0, table, data);
			
		// The id must be unique.
		if (ref_id && MSBackupInfo::gBackupInfo->get(ref_id)) {
			CSException::throwException(CS_CONTEXT, MS_ERR_DUPLICATE, "Attempt to insert a row with a duplicate key in the "BACKUP_TABLE_NAME" table.");
		}
		duplicate = false;
		
		// The 'Database_Id', 'Start', 'Completion' and "IsDump" fields are ignored.
		// I still need to get the fields though to advance the field position pointer.
		GET_STR_FIELD(name, 1, table, data);
		GET_INT_FIELD(db_id, 2, table, data);
		GET_STR_FIELD(start, 3, table, data);
		GET_STR_FIELD(end, 4, table, data);
		GET_STR_FIELD(isRunning, 5, table, data);
		GET_STR_FIELD(isDump, 6, table, data);
		GET_STR_FIELD(location, 7, table, data);
		GET_INT_FIELD(cloud_ref, 8, table, data);
		GET_INT_FIELD(cloud_backup_no, 9, table, data);
		
		
		if (ref_id == 0)
			ref_id = MSBackupInfo::gMaxInfoRef++;
		else if (ref_id >= MSBackupInfo::gMaxInfoRef)
			MSBackupInfo::gMaxInfoRef = ref_id +1;
		
		db_name	= name->c_ptr();
		db_id = MSDatabase::getDatabaseID(db_name, false);
		
		new_(info, MSBackupInfo(ref_id, db_name, db_id, 0, 0, false, location->c_ptr(), cloud_ref, cloud_backup_no));
		MSBackupInfo::gBackupInfo->set(ref_id, info);
		
		// There is no need to call this now, startBackup() will call it
		// after the backup is started.
		// saveTable(RETAIN(myShare->mySysDatabase)); 
		info->startBackup(RETAIN(myShare->mySysDatabase));
	}
	
	catch_(a);
	// It is good to know the details if the backup could not be started.
	self->logException();
	if (ref_id && ! duplicate)
		MSBackupInfo::gBackupInfo->remove(ref_id);
	throw_();
	
	cont_(a);
	exit_();
}

void MSBackupTable::deleteRow(char *data) 
{
	TABLE	*table = mySQLTable;	
	uint32_t ref_id, indx;


	enter_();
	
	GET_INT_FIELD(ref_id, 0, table, data);
	
	// Adjust the current position in the array if required.
	indx = MSBackupInfo::gBackupInfo->getIndex(ref_id);
	if (indx <= iBackupIndex)
		iBackupIndex--;
	
	MSBackupInfo::gBackupInfo->remove(ref_id);
	saveTable(RETAIN(myShare->mySysDatabase));
	exit_();
}

void MSBackupTable::transferTable(MSDatabase *to_db, MSDatabase *from_db)
{
	CSPath	*path;
	enter_();
	
	push_(from_db);
	push_(to_db);
	
	path = CSPath::newPath(getPBMSPath(RETAIN(from_db->myDatabasePath)), BACKUP_TABLE_NAME".dat");
	push_(path);
	if (path->exists()) {
		CSPath	*bu_path;
		bu_path = CSPath::newPath(getPBMSPath(RETAIN(to_db->myDatabasePath)), BACKUP_TABLE_NAME".dat");
		path->copyTo(bu_path, true);
	}
	
	release_(path);
	release_(to_db);
	release_(from_db);
	
	exit_();
}

CSStringBuffer *MSBackupTable::dumpTable(MSDatabase *db)
{

	CSPath			*path;
	CSStringBuffer	*dump;

	enter_();
	
	push_(db);
	path = getSysFile(getPBMSPath(RETAIN(db->myDatabasePath)), BACKUP_TABLE_NAME, MIN_BACKUP_TABLE_FILE_SIZE);
	release_(db);
	
	push_(path);
	new_(dump, CSStringBuffer(20));
	push_(dump);

	if (path->exists()) {
		CSFile	*file;
		size_t	size;
		
		file = path->openFile(CSFile::READONLY);
		push_(file);
		
		size = file->getEOF();
		dump->setLength(size);
		file->read(dump->getBuffer(0), 0, size, size);
		release_(file);
	}
	
	pop_(dump);
	release_(path);
	return_(dump);
}

void MSBackupTable::restoreTable(MSDatabase *db, const char *data, size_t size, bool reload)
{
	CSPath	*path;
	CSFile	*file;

	enter_();
	
	push_(db);
	path = getSysFile(getPBMSPath(RETAIN(db->myDatabasePath)), BACKUP_TABLE_NAME, MIN_BACKUP_TABLE_FILE_SIZE);
	push_(path);
	
	file = path->openFile(CSFile::CREATE | CSFile::TRUNCATE);
	push_(file);
	
	file->write(data, 0, size);
	file->close();
	release_(file);
	
	release_(path);
	
	pop_(db);
	if (reload)
		loadTable(db);
	else
		db->release();
		
	exit_();
}

// The cloud info table is only removed from the pbms database
// if there are no more databases.
void MSBackupTable::removeTable(CSString *db_path)
{
	CSPath	*path;
	char pbms_path[PATH_MAX];
	
	enter_();
	
	push_(db_path);	
	cs_strcpy(PATH_MAX, pbms_path, db_path->getCString());
	release_(db_path);
	
	if (strcmp(cs_last_name_of_path(pbms_path), "pbms")  != 0)
		exit_();
		
	cs_remove_last_name_of_path(pbms_path);

	path = getSysFile(CSString::newString(pbms_path), BACKUP_TABLE_NAME, MIN_BACKUP_TABLE_FILE_SIZE);
	push_(path);
	
	if (path->exists())
		path->removeFile();
	release_(path);
	
	exit_();
}