summaryrefslogtreecommitdiff
path: root/test/c/suites/TestDbHotBackup.c
blob: b330e73448c0a796a843e32d7d33fe0c56349d99 (plain)
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2011, 2012 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

/*
 * A C Unit test for db_hotbackup APIs [#20451]
 *
 * Different testing environments:
 * without any configuration,
 * have partitioned databases,
 * have multiple add_data_dir configured,
 * have set_lg_dir configured,
 * with queue extent files.
 *
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "db.h"
#include "CuTest.h"
#include "test_util.h"

struct handlers {
	DB_ENV *dbenvp;
	DB *dbp;
};

typedef enum {
	SIMPLE_ENV = 1,
	PARTITION_DB = 2,
	MULTI_DATA_DIR = 3,
	SET_LOG_DIR = 4,
	QUEUE_DB = 5
} ENV_CONF_T;

static int setup_test(ENV_CONF_T);
static int open_dbp(DB_ENV **, DB **, ENV_CONF_T);
static int store_records(DB *, ENV_CONF_T);
static int cleanup_test(DB_ENV *, DB *);
static int backup_db(CuTest *, DB_ENV *, const char *, u_int32_t, int);
static int backup_env(CuTest *, DB_ENV *, u_int32_t, int);
static int make_dbconfig(ENV_CONF_T);
static int verify_db(ENV_CONF_T);
static int verify_log(ENV_CONF_T);
static int verify_dbconfig(ENV_CONF_T);
static int cmp_files(const char *, const char *);
int backup_open(DB_ENV *, const char *, const char *, void **);
int backup_write(DB_ENV *, u_int32_t, u_int32_t, u_int32_t, u_int8_t *, void *);
int backup_close(DB_ENV *, const char *, void *);

#define BACKUP_DIR "BACKUP"
#define BACKUP_DB "backup.db"
#define LOG_DIR "LOG"
#define NPARTS 3

char *data_dirs[3] = {"DATA1", "DATA2", NULL};

int TestDbHotBackupSuiteSetup(CuSuite *suite) {
	return (0);
}

int TestDbHotBackupSuiteTeardown(CuSuite *suite) {
	return (0);
}

int TestDbHotBackupTestSetup(CuTest *ct) {
	struct handlers *info;

	if ((info = calloc(1, sizeof(*info))) == NULL)
		return (ENOMEM);
	ct->context = info;
	setup_envdir(TEST_ENV, 1);
	setup_envdir(BACKUP_DIR, 1);
	return (0);
}

int TestDbHotBackupTestTeardown(CuTest *ct) {
	struct handlers *info;
	DB_ENV *dbenv;
	DB *dbp;

	if (ct->context == NULL)
		return (EINVAL);
	info = ct->context;
	dbenv = info->dbenvp;
	dbp = info->dbp;
	/* Close all handles and clean the directories. */
	CuAssert(ct, "cleanup_test", cleanup_test(dbenv, dbp) == 0);
	free(info);
	ct->context = NULL;
	return (0);
}

int TestDbHotBackupSimpleEnv(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	ENV_CONF_T envconf;
	struct handlers *info;
	char **names;
	int cnt, has_callback;
	u_int32_t flag;

	envconf = SIMPLE_ENV;
	info = ct->context;
	has_callback = 0;
	flag = DB_EXCL;

	/* Step 1: set up test by making relative directories. */
	CuAssert(ct, "setup_test", setup_test(envconf) == 0);

	/* Step 2: open db handle. */
	CuAssert(ct,"open_dbp", open_dbp(&dbenv, &dbp, envconf) == 0);
	info->dbenvp = dbenv;
	info->dbp = dbp;

	/* Step 3: store records into db. */
	CuAssert(ct,"store_records", store_records(dbp, envconf) == 0);
	CuAssert(ct, "DB->sync", dbp->sync(dbp, 0) == 0);

	/* Step 4: backup only the db file without callbacks. */
	CuAssert(ct, "backup_env",
	    backup_db(ct, dbenv, BACKUP_DB, flag, has_callback) == 0);

	/* Step 5: check backup result. */
	/* 5a: dump the db and verify the content is same. */
	CuAssert(ct, "verify_db", verify_db(envconf) == 0);

	/* 5b: no other files are in backupdir. */
	CuAssert(ct, "__os_dirlist",
	    __os_dirlist(NULL, BACKUP_DIR, 0, &names, &cnt) == 0);
	CuAssert(ct, "too many files in backupdir", cnt == 1);

	return (0);
}

int TestDbHotBackupPartitionDB(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	ENV_CONF_T envconf;
	struct handlers *info;
	int has_callback;
	u_int32_t flag;

	envconf = PARTITION_DB;
	info = ct->context;
	has_callback = 0;
	flag = DB_BACKUP_CLEAN | DB_CREATE | DB_BACKUP_SINGLE_DIR;

	/* Step 1: set up test by making relative directories. */
	CuAssert(ct, "setup_test", setup_test(envconf) == 0);

	/* Step 2: open db handle. */
	CuAssert(ct,"open_dbp", open_dbp(&dbenv, &dbp, envconf) == 0);
	info->dbenvp = dbenv;
	info->dbp = dbp;

	/* Step 3: store records into db. */
	CuAssert(ct,"store_records", store_records(dbp, envconf) == 0);
	CuAssert(ct, "DB->sync", dbp->sync(dbp, 0) == 0);

	/* Step 4: backup the whole environment into a single directory. */
	CuAssert(ct, "backup_env",
	    backup_env(ct, dbenv, flag, has_callback) == 0);

	/* Step 5: check backup result. */
	/* 5a: dump the db and verify the content is same. */
	CuAssert(ct, "verify_db", verify_db(envconf) == 0);

	/* 5b: verify that creation directory is not in backupdir. */
	CuAssert(ct, "__os_exist", __os_exists(NULL, "BACKUP/DATA1", 0) != 0);

	/* 5c: verify that log files are in backupdir. */
	CuAssert(ct, "verify_log", verify_log(envconf) == 0);

	/* 5d: verify that DB_CONFIG is not in backupdir*/
	CuAssert(ct, "verify_dbconfig", verify_dbconfig(envconf) == 0);

	return (0);
}

int TestDbHotBackupMultiDataDir(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	ENV_CONF_T envconf;
	struct handlers *info;
	int has_callback;
	u_int32_t flag;

	envconf = MULTI_DATA_DIR;
	info = ct->context;
	has_callback = 0;
	flag = DB_BACKUP_CLEAN | DB_CREATE | DB_BACKUP_FILES;

	/* Step 1: set up test by making relative directories. */
	CuAssert(ct, "setup_test", setup_test(envconf) == 0);

	/* Step 2: open db handle. */
	CuAssert(ct,"open_dbp", open_dbp(&dbenv, &dbp, envconf) == 0);
	info->dbenvp = dbenv;
	info->dbp = dbp;

	/* Step 3: store records into db. */
	CuAssert(ct,"store_records", store_records(dbp, envconf) == 0);
	CuAssert(ct, "DB->sync", dbp->sync(dbp, 0) == 0);

	/* Step 4: backup the whole environment without callbacks. */
	CuAssert(ct, "backup_env",
	    backup_env(ct, dbenv, flag, has_callback) == 0);	

	/* Step 5: check backup result. */
	/* 5a: dump the db and verify the content is same. */
	CuAssert(ct, "verify_db", verify_db(envconf) == 0);

	/* 5b: verify that data_dirs are in backupdir. */
	CuAssert(ct, "__os_exist", __os_exists(NULL, "BACKUP/DATA1", 0) == 0);
	CuAssert(ct, "__os_exist", __os_exists(NULL, "BACKUP/DATA2", 0) == 0);

	/* 5c: verify that log files are in backupdir. */
	CuAssert(ct, "verify_log", verify_log(envconf) == 0);

	/* 5d: verify that DB_CONFIG is in backupdir. */
	CuAssert(ct, "verify_dbconfig", verify_dbconfig(envconf) == 0);

	return (0);
}

int TestDbHotBackupSetLogDir(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	ENV_CONF_T envconf;
	struct handlers *info;
	int has_callback = 1;
	u_int32_t flag;

	envconf = SET_LOG_DIR;
	info = ct->context;
	has_callback = 1;
	flag = DB_BACKUP_CLEAN | DB_CREATE | DB_BACKUP_FILES;

	/* Step 1: set up test by making relative directories. */
	CuAssert(ct, "setup_test", setup_test(envconf) == 0);

	/* Step 2: open db handle. */
	CuAssert(ct,"open_dbp", open_dbp(&dbenv, &dbp, envconf) == 0);
	info->dbenvp = dbenv;
	info->dbp = dbp;

	/* Step 3: store records into db. */
	CuAssert(ct,"store_records", store_records(dbp, envconf) == 0);
	CuAssert(ct, "DB->sync", dbp->sync(dbp, 0) == 0);

	/* Step 4: backup a whole environment with callbacks. */
	CuAssert(ct, "backup_env",
	    backup_env(ct, dbenv, flag, has_callback) == 0);

	/* Step 5: check backup result. */
	/* 5a: dump the db and verify the content is same. */
	CuAssert(ct, "verify_db", verify_db(envconf) == 0);

	/* 5b: verify that log files are in backupdir/log_dir. */
	CuAssert(ct, "verify_log", verify_log(envconf) == 0);

	/* 5c: verify that DB_CONFIG is in backupdir*/
	CuAssert(ct, "verify_dbconfig", verify_dbconfig(envconf) == 0);

	return (0);
}

int TestDbHotBackupQueueDB(CuTest *ct) {
	DB_ENV *dbenv;
	DB *dbp;
	ENV_CONF_T envconf;
	struct handlers *info;
	int has_callback;
	u_int32_t flag;

	envconf = QUEUE_DB;
	info = ct->context;
	has_callback = 0;
	flag = DB_BACKUP_CLEAN | DB_CREATE;

	/* Step 1: set up test by making relative directories. */
	CuAssert(ct, "setup_test", setup_test(envconf) == 0);

	/* Step 2: open db handle. */
	CuAssert(ct,"open_dbp", open_dbp(&dbenv, &dbp, envconf) == 0);
	info->dbenvp = dbenv;
	info->dbp = dbp;

	/* Step 3: store records into db. */
	CuAssert(ct,"store_records", store_records(dbp, envconf) == 0);
	CuAssert(ct, "DB->sync", dbp->sync(dbp, 0) == 0);

	/* Step 4: backup the whole environment without callbacks. */
	CuAssert(ct, "backup_env",
	    backup_env(ct, dbenv, flag, has_callback) == 0);

	/* Step 5: check backup result. */
	/* 5a: dump the db and verify the content is same. */
	CuAssert(ct, "verify_db", verify_db(envconf) == 0);

	/* 5b: verify that log files are in backupdir. */
	CuAssert(ct, "verify_log", verify_log(envconf) == 0);

	/* 5c: vertify that DB_CONFIG is not in backupdir. */
	CuAssert(ct, "verify_dbconfig", verify_dbconfig(envconf) == 0);

	return (0);
}

static int
setup_test(envconf)
	ENV_CONF_T envconf;
{
	char path[1024];
	int i, ret;

	/* Make directories based on config. */
	switch (envconf) {
	case SIMPLE_ENV:
		break;
	case PARTITION_DB:
		snprintf(path, sizeof(path),"%s%c%s",
		    TEST_ENV, PATH_SEPARATOR[0], data_dirs[0]);
		if ((ret = setup_envdir(path, 1)) != 0)
			return (ret);
		break;
	case MULTI_DATA_DIR:
		for (i = 0; i < 2; i++) {
			snprintf(path, sizeof(path),"%s%c%s",
			    TEST_ENV, PATH_SEPARATOR[0], data_dirs[i]);
			if ((ret = setup_envdir(path, 1)) != 0)
				return (ret);
		}
		break;
	case SET_LOG_DIR:
		snprintf(path, sizeof(path),"%s%c%s",
		    TEST_ENV, PATH_SEPARATOR[0], LOG_DIR);
		if ((ret = setup_envdir(path, 1)) != 0)
			return (ret);
		break;
	case QUEUE_DB:
		break;
	default:
		return (EINVAL);
	}

	/* Make DB_CONFIG for PARTITION_DB, MULT_DATA_DIR and SET_LOG_DIR. */
	if(envconf >= 2 && envconf <= 4)
		make_dbconfig(envconf);

	return (0);
}

static int
open_dbp(dbenvp, dbpp, envconf)
	DB_ENV **dbenvp;
	DB **dbpp;
	ENV_CONF_T envconf;
{
	DB_ENV *dbenv;
	DB *dbp;
	DBT key1, key2, keys[2];
	DBTYPE dtype;
	int i, ret, value1, value2;

	dbenv = NULL;
	dbp = NULL;
	dtype = DB_BTREE;
	ret = 0;

	if ((ret = db_env_create(&dbenv, 0)) != 0) {
		fprintf(stderr, "db_env_create: %s\n", db_strerror(ret));
		return (ret);
	}

	*dbenvp = dbenv;

	dbenv->set_errfile(dbenv, stderr);
	dbenv->set_errpfx(dbenv, "TestDbHotBackup");

	/* Configure the environment. */
	switch (envconf) {
	case SIMPLE_ENV:
	case PARTITION_DB:
		break;
	/* Add data directories. */
	case MULTI_DATA_DIR:
		for (i = 0; i < 2; i++) {
			if ((ret = dbenv->add_data_dir(dbenv,
			    data_dirs[i])) != 0) {
				fprintf(stderr, "DB_ENV->add_data_dir: %s\n",
				    db_strerror(ret));
				return (ret);
			}
		}
		break;
	/* Set log directory. */
	case SET_LOG_DIR:
		if ((ret = dbenv->set_lg_dir(dbenv, LOG_DIR)) != 0) {
			fprintf(stderr, "DB_ENV->set_lg_dir: %s\n",
			    db_strerror(ret));
			return (ret);
		}
		break;
	case QUEUE_DB:
		dtype = DB_QUEUE;
		break;
	default:
		return (EINVAL);
	}

	/* Open the environment. */
	if ((ret = dbenv->open(dbenv, TEST_ENV, DB_CREATE | DB_INIT_LOCK |
	    DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN, 0)) != 0) {
		fprintf(stderr, "DB_ENV->open: %s\n", db_strerror(ret));
		return (ret);
	}

	/* Configure the db handle. */
	if ((ret = db_create(&dbp, dbenv, 0)) != 0) {
		fprintf(stderr, "db_create: %s\n", db_strerror(ret));
		return (ret);
	}

	*dbpp = dbp;

	dbp->set_errfile(dbp, stderr);
	dbp->set_errpfx(dbp, "TestDbHotBackup");

	/* Set db creation directory for PARTTION_DB and MULTI_DATA_DRI. */
	if (envconf == PARTITION_DB || envconf == MULTI_DATA_DIR) {
		if ((ret = dbp->set_create_dir(dbp, data_dirs[0])) != 0) {
			fprintf(stderr, "DB_ENV->add_data_dir: %s\n",
			    db_strerror(ret));
			return (ret);
		}
	}

	/* Set partition. */
	if (envconf == PARTITION_DB) {
		value1 = 8;
		key1.data = &value1;
		key1.size = sizeof(value1);
		value2 = 16;
		key2.data = &value2;
		key2.size = sizeof(value2);
		keys[0] = key1;
		keys[1] = key2;
		if ((ret = dbp->set_partition(dbp, NPARTS, keys, NULL)) != 0) {
			dbp->err(dbp, ret, "DB->set_partition");
			return (ret);
		}
	}

	/* Set queue record length and extent size. */
	if (envconf == QUEUE_DB) {
		if ((ret = dbp->set_re_len(dbp, 50)) != 0) {
			dbp->err(dbp, ret, "DB->set_re_len");
			return (ret);
		}
		if ((ret = dbp->set_q_extentsize(dbp, 1)) != 0) {
			dbp->err(dbp, ret, "DB->set_q_extentsize");
			return (ret);
		}
	}
	/* Set flag for Btree. */
	else {
		if ((ret = dbp->set_flags(dbp, DB_DUPSORT)) != 0) {
			dbp->err(dbp, ret, "DB->set_flags");
			return (ret);
		}
	}
	
	if ((ret = dbp->set_pagesize(dbp, 512)) != 0) {
		dbp->err(dbp, ret, "DB->set_pagesize");
		return (ret);
	}

	/*Open the db handle. */
	if ((ret = dbp->open(dbp, NULL, BACKUP_DB,
		    NULL, dtype, DB_CREATE, 0644)) != 0) {
		dbp->err(dbp, ret, "%s: DB->open", BACKUP_DB);
		return (ret);
	}

	return (0);
}

static int
store_records(dbp, envconf)
	DB *dbp;
	ENV_CONF_T envconf;
{
	DBT key, data;
	int i, ret;
	size_t num;
	u_int32_t flag;

	char *buf = "abcdefghijefghijklmnopqrstuvwxyz";
	num = strlen(buf);
	flag = envconf == QUEUE_DB ? DB_APPEND : 0;

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	for (i = 0; i < num; i++) {
		key.data = &i;
		key.size = sizeof(i);

		data.data = &buf[i];
		data.size = sizeof(char);

		if ((ret = dbp->put(dbp, NULL, &key, &data, flag)) != 0) {
			dbp->err(dbp, ret, "DB->put");
			return (ret);
		}
	}
	return (ret);
}

static int
cleanup_test(dbenv, dbp)
	DB_ENV *dbenv;
	DB *dbp;
{
	int ret, t_ret;

	ret = 0;;
	if (dbp != NULL && (ret = dbp->close(dbp, 0)) != 0) 
		fprintf(stderr, "DB->close: %s\n", db_strerror(ret));

	if (dbenv != NULL && (t_ret = dbenv->close(dbenv, 0)) != 0) {
		fprintf(stderr, "DB_ENV->close: %s\n", db_strerror(t_ret));
		ret = t_ret;
	}

	teardown_envdir(TEST_ENV);
	teardown_envdir(BACKUP_DIR);

	return (ret);
}

/*
 * backup_env:
 * 	CuTest *ct
 *	DB_ENV *dbenv: the environment to backup
 *	u_int32_t flags: hotbackup flags
 *	int has_callback: 0 if not use callback, 1 otherwise
*/
static int
backup_env(ct, dbenv, flags, has_callback)
	CuTest *ct;
	DB_ENV *dbenv;
	u_int32_t flags;
	int has_callback;
{
	if (has_callback != 0) {
		CuAssert(ct, "DB_ENV->set_backup_callbacks",
		    dbenv->set_backup_callbacks(dbenv, backup_open,
		    backup_write, backup_close) == 0);
	}
	CuAssert(ct, "DB_ENV->backup",
	    dbenv->backup(dbenv, BACKUP_DIR, flags) == 0);
	return (0);
}

/*
 * backup_db:
 * 	CuTest *ct
 * 	DB_ENV *dbenv: the environment to backup
 * 	const char *dname: the name of db file to backup
 * 	u_int32_t flags: hot_backup flags
 * 	int has_callback: 0 if not use callback, 1 otherwise
*/

static int
backup_db(ct, dbenv, dname, flags, has_callback)
	CuTest *ct;
	DB_ENV *dbenv;
	const char *dname;
	u_int32_t flags;
	int has_callback;
{
	if (has_callback != 0) {
		CuAssert(ct, "DB_ENV->set_backup_callbacks",
		    dbenv->set_backup_callbacks(dbenv, backup_open,
		    backup_write, backup_close) == 0);
	}
	CuAssert(ct, "DB_ENV->dbbackup",
	    dbenv->dbbackup(dbenv, dname, BACKUP_DIR, flags) == 0);
	return (0);
}

static int
verify_db(envconf)
	ENV_CONF_T envconf;
{
	char buf1[100], buf2[100], path1[100], path2[100], pfx[10];
	char **names1, **names2;
	int cnt1, cnt2, i, m_cnt, ret, t_cnt1, t_cnt2;

	names1 = names2 = NULL;
	cnt1 = cnt2 = i = m_cnt = ret = t_cnt1 = t_cnt2  = 0;

	/* Get the data directory paths. */
	if (envconf == PARTITION_DB) {
		snprintf(path1, sizeof(path1), "%s%c%s",
		    TEST_ENV, PATH_SEPARATOR[0], data_dirs[0]);
		snprintf(path2, sizeof(path2), "%s", BACKUP_DIR);
	} else if (envconf == MULTI_DATA_DIR) {
		snprintf(path1, sizeof(path1), "%s%c%s",
		    TEST_ENV, PATH_SEPARATOR[0], data_dirs[0]);
		snprintf(path2, sizeof(path2), "%s%c%s",
		    BACKUP_DIR, PATH_SEPARATOR[0], data_dirs[0]);
	} else {
		snprintf(path1, sizeof(path1), "%s", TEST_ENV);
		snprintf(path2, sizeof(path2), "%s", BACKUP_DIR);
	}

	/* Define the prefix of partition db and queue extent files. */
	if (envconf == PARTITION_DB)
		snprintf(pfx, sizeof(pfx), "%s", "__dbp.");
	else if (envconf == QUEUE_DB)
		snprintf(pfx, sizeof(pfx), "%s", "__dbq.");
	else
		pfx[0] = '\0';

	/* Get the lists of db file, partition db files and queue extent. */
	if ((ret = __os_dirlist(NULL, path1, 0, &names1, &cnt1)) != 0)
		return (ret);
	if ((ret = __os_dirlist(NULL, path2, 0, &names2, &cnt2)) != 0)
		return (ret);

	/* Get the numbers of db files. */
	m_cnt = cnt1 > cnt2 ? cnt1 : cnt2;
	t_cnt1 = cnt1;
	t_cnt2 = cnt2;
	for (i = 0; i < m_cnt; i++) {
		if (i < cnt1 &&
		    strncmp(names1[i], BACKUP_DB, strlen(BACKUP_DB)) != 0 &&
		    (strlen(pfx) > 0 ?
		    strncmp(names1[i], pfx, strlen(pfx)) != 0 : 1)) {
			    t_cnt1--;
			    names1[i] = NULL;
		}
		if (i < cnt2 &&
		    strncmp(names2[i], BACKUP_DB, strlen(BACKUP_DB)) != 0 &&
		    (strlen(pfx) > 0 ?
		    strncmp(names2[i], pfx, strlen(pfx)) != 0 : 1)) {
			    t_cnt2--;
			    names2[i] = NULL;
		}		
	}
	if ((ret = t_cnt1 == t_cnt2 ? 0 : EINVAL) != 0)
		return (ret);

	/* Compare each db file. */
	for (i = 0; i < cnt1; i++) {
		if (names1[i] == NULL)
			continue;
		snprintf(buf1, sizeof(buf1), "%s%c%s",
		    path1, PATH_SEPARATOR[0], names1[i]);
		snprintf(buf2, sizeof(buf2), "%s%c%s",
		    path2, PATH_SEPARATOR[0], names1[i]);
		if ((ret = cmp_files(buf1, buf2)) != 0)
			break;
	}

	return (ret);
}

static int
verify_log(envconf)
	ENV_CONF_T envconf;
{
	char buf1[100], buf2[100], lg1[100], lg2[100], pfx[10];
	char **names1, **names2;
	int cnt1, cnt2, i, m_cnt, ret, t_cnt1, t_cnt2;

	cnt1 = cnt2 = i = m_cnt = ret = t_cnt1 = t_cnt2 = 0;

	/* Get the log paths. */
	if (envconf == SET_LOG_DIR) {
		snprintf(lg1, sizeof(lg1),
		    "%s%c%s", TEST_ENV, PATH_SEPARATOR[0], LOG_DIR);
		snprintf(lg2, sizeof(lg2),
		    "%s%c%s", BACKUP_DIR, PATH_SEPARATOR[0], LOG_DIR);
	}
	else {
		snprintf(lg1, sizeof(lg1), "%s", TEST_ENV);
		snprintf(lg2, sizeof(lg2), "%s", BACKUP_DIR);
	}

	/* Define the prefix of log file. */
	snprintf(pfx, sizeof(pfx), "%s", "log.");

	/* Get the lists of log files. */
	if ((ret = __os_dirlist(NULL, lg1, 0, &names1, &cnt1)) != 0)
		return (ret);
	if ((ret = __os_dirlist(NULL, lg2, 0, &names2, &cnt2)) != 0)
		return (ret);

	/* Get the numbers of log files. */
	m_cnt = cnt1 > cnt2 ? cnt1 : cnt2;
	t_cnt1 = cnt1;
	t_cnt2 = cnt2;
	for (i = 0; i < m_cnt; i++) {
		if (i < cnt1 &&
		    strncmp(names1[i], pfx, strlen(pfx)) != 0) {
			    t_cnt1--;
			    names1[i] = NULL;
		}
		if (i < cnt2 &&
		    strncmp(names2[i], pfx, strlen(pfx)) != 0) {
			    t_cnt2--;
			    names2[i] = NULL;
		}		
	}
	if ((ret = t_cnt1 == t_cnt2 ? 0 : EINVAL) != 0)
		return (ret);

	/* Compare each log file. */
	for (i = 0; i < cnt1; i++) {
		if (names1[i] == NULL)
			continue;
		snprintf(buf1, sizeof(buf1), "%s%c%s",
		    lg1, PATH_SEPARATOR[0], names1[i]);
		snprintf(buf2, sizeof(buf2), "%s%c%s",
		    lg2, PATH_SEPARATOR[0], names1[i]);
		if ((ret = cmp_files(buf1, buf2)) != 0)
			break;
	}

	return (ret);
}

static int
verify_dbconfig(envconf)
	ENV_CONF_T envconf;
{
	char *path1, *path2;
	int ret;

	path1 = path2 = NULL;
	ret = 0;

	if ((ret = __os_calloc(NULL, 1024, 1, &path1)) != 0)
		goto err;
	if ((ret = __os_calloc(NULL, 1024, 1, &path2)) != 0)
		goto err;

	switch(envconf) {
	/* DB_CONFIG is not in backupdir for this test cases. */
	case SIMPLE_ENV:
	case PARTITION_DB:
	case QUEUE_DB:
		if((ret = __os_exists(NULL, "BACKUP/DB_CONFIG", 0)) != 0)
			return (0);
		break;
	/* DB_CONFIG is in backupdir for MULTI_DATA_DIR and SET_LOG_DIR. */
	case MULTI_DATA_DIR:
	case SET_LOG_DIR:
		snprintf(path1, 1024, "%s%c%s",
		    TEST_ENV, PATH_SEPARATOR[0], "DB_CONFIG");
		snprintf(path2, 1024, "%s%c%s",
		    BACKUP_DIR, PATH_SEPARATOR[0], "DB_CONFIG");
		if ((ret = cmp_files(path1, path2)) != 0)
			goto err;
		break;
	default:
		return (EINVAL);
	}

err:
	if (path1 != NULL)
		__os_free(NULL, path1);
	if (path2 != NULL)
		__os_free(NULL, path2);
	return (ret);
}

int backup_open(dbenv, dbname, target, handle)
	DB_ENV *dbenv;
	const char *dbname;
	const char *target;
	void **handle;
{
	DB_FH *fp;
	const char *t_target;
	char str[1024];
	u_int32_t flags;
	int ret;

	fp = NULL;
	flags = DB_OSO_CREATE;
	ret = 0;

	if (target == NULL)
		t_target = BACKUP_DIR;
	else
		t_target = target;
	sprintf(str, "%s%s%s", t_target, "/", dbname);

	if ((ret = __os_open(NULL, str, 0, flags, DB_MODE_600, &fp)) != 0)
		return (ret);

	*handle = fp;

	return (ret);
}

int backup_write(dbenv, gigs, offset, size, buf, handle)
	DB_ENV *dbenv;
	u_int32_t gigs, offset, size;
	u_int8_t *buf;
	void *handle;
{
	DB_FH *fp;
	size_t nw;
	int ret;

	ret = 0;
	fp = (DB_FH *)handle;

	if (size <= 0)
		return (EINVAL);

	if ((ret = __os_write(NULL, fp, buf, size, &nw)) != 0)
		return (ret);
	if (nw != size)
		ret = EIO;

	return (ret);
}

int backup_close(dbenv, dbname, handle)
	DB_ENV *dbenv;
	const char *dbname;
	void *handle;
{
	DB_FH *fp;
	int ret;

	fp = (DB_FH *)handle;
	ret = 0;

	ret = __os_closehandle(NULL, fp);

	return (ret);

}

static int
make_dbconfig(envconf)
	ENV_CONF_T envconf;
{
	const char *path = "TESTDIR/DB_CONFIG";
	char str[1024];
	FILE *fp;

	if (envconf >= PARTITION_DB && envconf <= SET_LOG_DIR)
		fp = fopen(path, "w");
	else
		return (0);

	switch(envconf) {
	case PARTITION_DB:
	case MULTI_DATA_DIR:
		snprintf(str, 1024, "%s", "set_data_dir DATA1");
		break;
	case SET_LOG_DIR:
		snprintf(str, 1024, "%s", "set_lg_dir LOG");
		break;
	default:
		return (EINVAL);
	}

	fputs(str, fp);
	fclose(fp);

	return (0);
}

static int
cmp_files(name1, name2)
	const char *name1;
	const char *name2;
{
	DB_FH *fhp1, *fhp2;
	void *buf1, *buf2;
	size_t nr1, nr2;
	int ret, t_ret;

	fhp1 = fhp2 = NULL;
	buf1 = buf2 = NULL;
	nr1 = nr2 = 0;
	ret = t_ret = 0;

	/* Allocate memory for buffers. */
	if ((ret = __os_calloc(NULL, MEGABYTE, 1, &buf1)) != 0)
		goto err;
	if ((ret = __os_calloc(NULL, MEGABYTE, 1, &buf2)) != 0)
		goto err;

	/* Open the input files. */
	if ( (ret = __os_open(NULL, name1, 0, DB_OSO_RDONLY, 0, &fhp1)) != 0 ||
	    (t_ret = __os_open(NULL, name2, 0, DB_OSO_RDONLY, 0, &fhp2)) != 0) {
		    if (ret == 0)
			    ret = t_ret;
		    goto err;
	}

	/* Read and compare the file content. */
	while ((ret = __os_read(NULL, fhp1, buf1, MEGABYTE, &nr1)) == 0 &&
	    nr1 > 0 && (ret = __os_read(NULL, fhp2,
	    buf2, MEGABYTE, &nr2)) == 0 && nr2 > 0) {
		    if (nr1 != nr2) {
			    ret = EINVAL;
			    break;
		    }
		    if ((ret = memcmp(buf1, buf2, nr1)) != 0)
			    break;
	}
	if(ret == 0 && nr1 > 0 && nr2 > 0 && nr1 != nr2)
		ret = EINVAL;

err:
	if (buf1 != NULL)
		__os_free(NULL, buf1);
	if (buf2 != NULL)
		__os_free(NULL, buf2);
	if (fhp1 != NULL && (t_ret = __os_closehandle(NULL, fhp1)) != 0)
		ret = t_ret;
	if (fhp2 != NULL && (t_ret = __os_closehandle(NULL, fhp2)) != 0)
		ret = t_ret;
	return (ret);
}