summaryrefslogtreecommitdiff
path: root/src/db/db_pr.c
blob: d95440f996b0451398936a4ab3df76a663c93490 (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
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 2012 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

#include "db_config.h"

#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/btree.h"
#include "dbinc/hash.h"
#include "dbinc/heap.h"
#include "dbinc/mp.h"
#include "dbinc/partition.h"
#include "dbinc/qam.h"
#include "dbinc/db_verify.h"

static int	 __db_bmeta __P((ENV *, DB *, BTMETA *, u_int32_t));
static int	 __db_heapmeta __P((ENV *, DB *, HEAPMETA *, u_int32_t));
static int	 __db_heapint __P((DB *, HEAPPG *, u_int32_t));
static int	 __db_hmeta __P((ENV *, DB *, HMETA *, u_int32_t));
static void	 __db_meta __P((ENV *, DB *, DBMETA *, FN const *, u_int32_t));
static void	 __db_proff __P((ENV *, DB_MSGBUF *, void *));
static int	 __db_qmeta __P((ENV *, DB *, QMETA *, u_int32_t));
#ifdef HAVE_STATISTICS
static void	 __db_prdb __P((DB *, u_int32_t));
static int	 __db_prtree __P((DB *, DB_TXN *,
		    u_int32_t, db_pgno_t, db_pgno_t));
#endif

/*
 * __db_loadme --
 *	A nice place to put a breakpoint.
 *
 * PUBLIC: void __db_loadme __P((void));
 */
void
__db_loadme()
{
	pid_t pid;

	__os_id(NULL, &pid, NULL);
}

#ifdef HAVE_STATISTICS
/*
 * __db_dumptree --
 *	Dump the tree to a file.
 *
 * PUBLIC: int __db_dumptree __P((DB *, DB_TXN *,
 * PUBLIC:     char *, char *, db_pgno_t, db_pgno_t));
 */
int
__db_dumptree(dbp, txn, op, name, first, last)
	DB *dbp;
	DB_TXN *txn;
	char *op, *name;
	db_pgno_t first, last;
{
	ENV *env;
	FILE *fp, *orig_fp;
	u_int32_t flags;
	int ret;

	env = dbp->env;

	for (flags = 0; *op != '\0'; ++op)
		switch (*op) {
		case 'a':
			LF_SET(DB_PR_PAGE);
			break;
		case 'h':
			break;
		case 'r':
			LF_SET(DB_PR_RECOVERYTEST);
			break;
		default:
			return (EINVAL);
		}

	if (name != NULL) {
		if ((fp = fopen(name, "w")) == NULL)
			return (__os_get_errno());

		orig_fp = dbp->dbenv->db_msgfile;
		dbp->dbenv->db_msgfile = fp;
	} else
		fp = orig_fp = NULL;

	__db_prdb(dbp, flags);

	__db_msg(env, "%s", DB_GLOBAL(db_line));

	ret = __db_prtree(dbp, txn, flags, first, last);

	if (fp != NULL) {
		(void)fclose(fp);
		env->dbenv->db_msgfile = orig_fp;
	}

	return (ret);
}

static const FN __db_flags_fn[] = {
	{ DB_AM_CHKSUM,			"checksumming" },
	{ DB_AM_COMPENSATE,		"created by compensating transaction" },
	{ DB_AM_CREATED,		"database created" },
	{ DB_AM_CREATED_MSTR,		"encompassing file created" },
	{ DB_AM_DBM_ERROR,		"dbm/ndbm error" },
	{ DB_AM_DELIMITER,		"variable length" },
	{ DB_AM_DISCARD,		"discard cached pages" },
	{ DB_AM_DUP,			"duplicates" },
	{ DB_AM_DUPSORT,		"sorted duplicates" },
	{ DB_AM_ENCRYPT,		"encrypted" },
	{ DB_AM_FIXEDLEN,		"fixed-length records" },
	{ DB_AM_INMEM,			"in-memory" },
	{ DB_AM_IN_RENAME,		"file is being renamed" },
	{ DB_AM_NOT_DURABLE,		"changes not logged" },
	{ DB_AM_OPEN_CALLED,		"open called" },
	{ DB_AM_PAD,			"pad value" },
	{ DB_AM_PGDEF,			"default page size" },
	{ DB_AM_RDONLY,			"read-only" },
	{ DB_AM_READ_UNCOMMITTED,	"read-uncommitted" },
	{ DB_AM_RECNUM,			"Btree record numbers" },
	{ DB_AM_RECOVER,		"opened for recovery" },
	{ DB_AM_RENUMBER,		"renumber" },
	{ DB_AM_REVSPLITOFF,		"no reverse splits" },
	{ DB_AM_SECONDARY,		"secondary" },
	{ DB_AM_SNAPSHOT,		"load on open" },
	{ DB_AM_SUBDB,			"subdatabases" },
	{ DB_AM_SWAP,			"needswap" },
	{ DB_AM_TXN,			"transactional" },
	{ DB_AM_VERIFYING,		"verifier" },
	{ 0,				NULL }
};

/*
 * __db_get_flags_fn --
 *	Return the __db_flags_fn array.
 *
 * PUBLIC: const FN * __db_get_flags_fn __P((void));
 */
const FN *
__db_get_flags_fn()
{
	return (__db_flags_fn);
}

/*
 * __db_prdb --
 *	Print out the DB structure information.
 */
static void
__db_prdb(dbp, flags)
	DB *dbp;
	u_int32_t flags;
{
	BTREE *bt;
	DB_MSGBUF mb;
	ENV *env;
	HASH *h;
	QUEUE *q;
	HEAP *hp;

	env = dbp->env;

	DB_MSGBUF_INIT(&mb);
	__db_msg(env, "In-memory DB structure:");
	__db_msgadd(env, &mb, "%s: %#lx",
	    __db_dbtype_to_string(dbp->type), (u_long)dbp->flags);
	__db_prflags(env, &mb, dbp->flags, __db_flags_fn, " (", ")");
	DB_MSGBUF_FLUSH(env, &mb);

	switch (dbp->type) {
	case DB_BTREE:
	case DB_RECNO:
		bt = dbp->bt_internal;
		__db_msg(env, "bt_meta: %lu bt_root: %lu",
		    (u_long)bt->bt_meta, (u_long)bt->bt_root);
		__db_msg(env, "bt_minkey: %lu", (u_long)bt->bt_minkey);
		if (!LF_ISSET(DB_PR_RECOVERYTEST))
			__db_msg(env, "bt_compare: %#lx bt_prefix: %#lx",
			    P_TO_ULONG(bt->bt_compare),
			    P_TO_ULONG(bt->bt_prefix));
#ifdef HAVE_COMPRESSION
		if (!LF_ISSET(DB_PR_RECOVERYTEST))
			__db_msg(env, "bt_compress: %#lx bt_decompress: %#lx",
			    P_TO_ULONG(bt->bt_compress),
			    P_TO_ULONG(bt->bt_decompress));
#endif
		__db_msg(env, "bt_lpgno: %lu", (u_long)bt->bt_lpgno);
		if (dbp->type == DB_RECNO) {
			__db_msg(env,
		    "re_pad: %#lx re_delim: %#lx re_len: %lu re_source: %s",
			    (u_long)bt->re_pad, (u_long)bt->re_delim,
			    (u_long)bt->re_len,
			    bt->re_source == NULL ? "" : bt->re_source);
			__db_msg(env,
			    "re_modified: %d re_eof: %d re_last: %lu",
			    bt->re_modified, bt->re_eof, (u_long)bt->re_last);
		}
		break;
	case DB_HASH:
		h = dbp->h_internal;
		__db_msg(env, "meta_pgno: %lu", (u_long)h->meta_pgno);
		__db_msg(env, "h_ffactor: %lu", (u_long)h->h_ffactor);
		__db_msg(env, "h_nelem: %lu", (u_long)h->h_nelem);
		if (!LF_ISSET(DB_PR_RECOVERYTEST))
			__db_msg(env, "h_hash: %#lx", P_TO_ULONG(h->h_hash));
		break;
	case DB_QUEUE:
		q = dbp->q_internal;
		__db_msg(env, "q_meta: %lu", (u_long)q->q_meta);
		__db_msg(env, "q_root: %lu", (u_long)q->q_root);
		__db_msg(env, "re_pad: %#lx re_len: %lu",
		    (u_long)q->re_pad, (u_long)q->re_len);
		__db_msg(env, "rec_page: %lu", (u_long)q->rec_page);
		__db_msg(env, "page_ext: %lu", (u_long)q->page_ext);
		break;
	case DB_HEAP:
		hp = dbp->heap_internal;
		__db_msg(env, "gbytes: %lu", (u_long)hp->gbytes);
		__db_msg(env, "bytes: %lu", (u_long)hp->bytes);
		__db_msg(env, "curregion: %lu", (u_long)hp->curregion);
		__db_msg(env, "region_size: %lu", (u_long)hp->region_size);
		__db_msg(env, "maxpgno: %lu", (u_long)hp->maxpgno);
		break;
	case DB_UNKNOWN:
	default:
		break;
	}
}

/*
 * __db_prtree --
 *	Print out the entire tree.
 */
static int
__db_prtree(dbp, txn, flags, first, last)
	DB *dbp;
	DB_TXN *txn;
	u_int32_t flags;
	db_pgno_t first, last;
{
	DB_MPOOLFILE *mpf;
	PAGE *h;
	db_pgno_t i;
	int ret;

	mpf = dbp->mpf;

	if (dbp->type == DB_QUEUE)
		return (__db_prqueue(dbp, flags));

	/*
	 * Find out the page number of the last page in the database, then
	 * dump each page.
	 */
	if (last == PGNO_INVALID &&
	    (ret = __memp_get_last_pgno(mpf, &last)) != 0)
		return (ret);
	for (i = first; i <= last; ++i) {
		if ((ret = __memp_fget(mpf, &i, NULL, txn, 0, &h)) != 0)
			return (ret);
		(void)__db_prpage(dbp, h, flags);
		if ((ret = __memp_fput(mpf, NULL, h, dbp->priority)) != 0)
			return (ret);
	}

	return (0);
}

/*
 * __db_prnpage
 *	-- Print out a specific page.
 *
 * PUBLIC: int __db_prnpage __P((DB *, DB_TXN *, db_pgno_t));
 */
int
__db_prnpage(dbp, txn, pgno)
	DB *dbp;
	DB_TXN *txn;
	db_pgno_t pgno;
{
	DB_MPOOLFILE *mpf;
	PAGE *h;
	int ret, t_ret;

	mpf = dbp->mpf;

	if ((ret = __memp_fget(mpf, &pgno, NULL, txn, 0, &h)) != 0)
		return (ret);

	ret = __db_prpage(dbp, h, DB_PR_PAGE);

	if ((t_ret = __memp_fput(mpf, NULL, h, dbp->priority)) != 0 && ret == 0)
		ret = t_ret;

	return (ret);
}

/*
 * __db_prpage
 *	-- Print out a page.
 *
 * PUBLIC: int __db_prpage __P((DB *, PAGE *, u_int32_t));
 */
int
__db_prpage(dbp, h, flags)
	DB *dbp;
	PAGE *h;
	u_int32_t flags;
{
	DB_MSGBUF mb;
	u_int32_t pagesize;
	/*
	 * !!!
	 * Find out the page size.  We don't want to do it the "right" way,
	 * by reading the value from the meta-data page, that's going to be
	 * slow.  Reach down into the mpool region.
	 */
	pagesize = (u_int32_t)dbp->mpf->mfp->pagesize;
	DB_MSGBUF_INIT(&mb);
	return (__db_prpage_int(dbp->env,
	    &mb, dbp, "", h, pagesize, NULL, flags));
}

/*
 * __db_lockmode_to_string --
 *	Return the name of the lock mode.
 *
 * PUBLIC: const char * __db_lockmode_to_string __P((db_lockmode_t));
 */
const char *
__db_lockmode_to_string(mode)
	db_lockmode_t mode;
{
	switch (mode) {
	case DB_LOCK_NG:
		return ("Not granted");
	case DB_LOCK_READ:
		return ("Shared/read");
	case DB_LOCK_WRITE:
		return ("Exclusive/write");
	case DB_LOCK_WAIT:
		return ("Wait for event");
	case DB_LOCK_IWRITE:
		return ("Intent exclusive/write");
	case DB_LOCK_IREAD:
		return ("Intent shared/read");
	case DB_LOCK_IWR:
		return ("Intent to read/write");
	case DB_LOCK_READ_UNCOMMITTED:
		return ("Read uncommitted");
	case DB_LOCK_WWRITE:
		return ("Was written");
	default:
		break;
	}
	return ("UNKNOWN LOCK MODE");
}

#else /* !HAVE_STATISTICS */

/*
 * __db_dumptree --
 *	Dump the tree to a file.
 *
 * PUBLIC: int __db_dumptree __P((DB *, DB_TXN *,
 * PUBLIC:     char *, char *, db_pgno_t, db_pgno_t));
 */
int
__db_dumptree(dbp, txn, op, name, first, last)
	DB *dbp;
	DB_TXN *txn;
	char *op, *name;
	db_pgno_t first, last;
{
	COMPQUIET(txn, NULL);
	COMPQUIET(op, NULL);
	COMPQUIET(name, NULL);
	COMPQUIET(first, last);

	return (__db_stat_not_built(dbp->env));
}

/*
 * __db_get_flags_fn --
 *	Return the __db_flags_fn array.
 *
 * PUBLIC: const FN * __db_get_flags_fn __P((void));
 */
const FN *
__db_get_flags_fn()
{
	/*
	 * !!!
	 * The Tcl API uses this interface, stub it off.
	 */
	return (NULL);
}
#endif

/*
 * __db_meta --
 *	Print out common metadata information.
 */
static void
__db_meta(env, dbp, dbmeta, fn, flags)
	DB *dbp;
	ENV *env;
	DBMETA *dbmeta;
	FN const *fn;
	u_int32_t flags;
{
	DB_MPOOLFILE *mpf;
	DB_MSGBUF mb;
	PAGE *h;
	db_pgno_t pgno;
	u_int8_t *p;
	int cnt, ret;
	const char *sep;

	DB_MSGBUF_INIT(&mb);

	__db_msg(env, "\tmagic: %#lx", (u_long)dbmeta->magic);
	__db_msg(env, "\tversion: %lu", (u_long)dbmeta->version);
	__db_msg(env, "\tpagesize: %lu", (u_long)dbmeta->pagesize);
	__db_msg(env, "\ttype: %lu", (u_long)dbmeta->type);
	__db_msg(env, "\tmetaflags %#lx", (u_long)dbmeta->metaflags);
	__db_msg(env, "\tkeys: %lu\trecords: %lu",
	    (u_long)dbmeta->key_count, (u_long)dbmeta->record_count);
	if (dbmeta->nparts)
		__db_msg(env, "\tnparts: %lu", (u_long)dbmeta->nparts);

	/*
	 * If we're doing recovery testing, don't display the free list,
	 * it may have changed and that makes the dump diff not work.
	 */
	if (dbp != NULL && !LF_ISSET(DB_PR_RECOVERYTEST)) {
		mpf = dbp->mpf;
		__db_msgadd(
		    env, &mb, "\tfree list: %lu", (u_long)dbmeta->free);
		for (pgno = dbmeta->free,
		    cnt = 0, sep = ", "; pgno != PGNO_INVALID;) {
			if ((ret = __memp_fget(mpf,
			     &pgno, NULL, NULL, 0, &h)) != 0) {
				DB_MSGBUF_FLUSH(env, &mb);
				__db_msg(env,
			    "Unable to retrieve free-list page: %lu: %s",
				    (u_long)pgno, db_strerror(ret));
				break;
			}
			pgno = h->next_pgno;
			(void)__memp_fput(mpf, NULL, h, dbp->priority);
			__db_msgadd(env, &mb, "%s%lu", sep, (u_long)pgno);
			if (++cnt % 10 == 0) {
				DB_MSGBUF_FLUSH(env, &mb);
				cnt = 0;
				sep = "\t";
			} else
				sep = ", ";
		}
		DB_MSGBUF_FLUSH(env, &mb);
		__db_msg(env, "\tlast_pgno: %lu", (u_long)dbmeta->last_pgno);
	}

	if (fn != NULL) {
		DB_MSGBUF_FLUSH(env, &mb);
		__db_msgadd(env, &mb, "\tflags: %#lx", (u_long)dbmeta->flags);
		__db_prflags(env, &mb, dbmeta->flags, fn, " (", ")");
	}

	DB_MSGBUF_FLUSH(env, &mb);
	__db_msgadd(env, &mb, "\tuid: ");
	for (p = (u_int8_t *)dbmeta->uid,
	    cnt = 0; cnt < DB_FILE_ID_LEN; ++cnt) {
		__db_msgadd(env, &mb, "%x", *p++);
		if (cnt < DB_FILE_ID_LEN - 1)
			__db_msgadd(env, &mb, " ");
	}
	DB_MSGBUF_FLUSH(env, &mb);
}

/*
 * __db_bmeta --
 *	Print out the btree meta-data page.
 */
static int
__db_bmeta(env, dbp, h, flags)
	ENV *env;
	DB *dbp;
	BTMETA *h;
	u_int32_t flags;
{
	static const FN fn[] = {
		{ BTM_DUP,	"duplicates" },
		{ BTM_RECNO,	"recno" },
		{ BTM_RECNUM,	"btree:recnum" },
		{ BTM_FIXEDLEN,	"recno:fixed-length" },
		{ BTM_RENUMBER,	"recno:renumber" },
		{ BTM_SUBDB,	"multiple-databases" },
		{ BTM_DUPSORT,	"sorted duplicates" },
		{ BTM_COMPRESS,	"compressed" },
		{ 0,		NULL }
	};

	__db_meta(env, dbp, (DBMETA *)h, fn, flags);

	__db_msg(env, "\tminkey: %lu", (u_long)h->minkey);
	if (F_ISSET(&h->dbmeta, BTM_RECNO))
		__db_msg(env, "\tre_len: %#lx re_pad: %#lx",
		    (u_long)h->re_len, (u_long)h->re_pad);
	__db_msg(env, "\troot: %lu", (u_long)h->root);

	return (0);
}

/*
 * __db_hmeta --
 *	Print out the hash meta-data page.
 */
static int
__db_hmeta(env, dbp, h, flags)
	ENV *env;
	DB *dbp;
	HMETA *h;
	u_int32_t flags;
{
	static const FN fn[] = {
		{ DB_HASH_DUP,		"duplicates" },
		{ DB_HASH_SUBDB,	"multiple-databases" },
		{ DB_HASH_DUPSORT,	"sorted duplicates" },
		{ 0,			NULL }
	};
	DB_MSGBUF mb;
	int i;

	DB_MSGBUF_INIT(&mb);

	__db_meta(env, dbp, (DBMETA *)h, fn, flags);

	__db_msg(env, "\tmax_bucket: %lu", (u_long)h->max_bucket);
	__db_msg(env, "\thigh_mask: %#lx", (u_long)h->high_mask);
	__db_msg(env, "\tlow_mask:  %#lx", (u_long)h->low_mask);
	__db_msg(env, "\tffactor: %lu", (u_long)h->ffactor);
	__db_msg(env, "\tnelem: %lu", (u_long)h->nelem);
	__db_msg(env, "\th_charkey: %#lx", (u_long)h->h_charkey);
	__db_msgadd(env, &mb, "\tspare points:\n\t");
	for (i = 0; i < NCACHED; i++) {
		__db_msgadd(env, &mb, "%lu (%lu) ", (u_long)h->spares[i],
		    (u_long)(h->spares[i] == 0 ?
		    0 : h->spares[i] + (i == 0 ? 0 : 1 << (i-1))));
		if ((i + 1) % 8 == 0)
			__db_msgadd(env, &mb, "\n\t");
	}
	DB_MSGBUF_FLUSH(env, &mb);

	return (0);
}

/*
 * __db_qmeta --
 *	Print out the queue meta-data page.
 */
static int
__db_qmeta(env, dbp, h, flags)
	ENV *env;
	DB *dbp;
	QMETA *h;
	u_int32_t flags;
{

	__db_meta(env, dbp, (DBMETA *)h, NULL, flags);

	__db_msg(env, "\tfirst_recno: %lu", (u_long)h->first_recno);
	__db_msg(env, "\tcur_recno: %lu", (u_long)h->cur_recno);
	__db_msg(env, "\tre_len: %#lx re_pad: %lu",
	    (u_long)h->re_len, (u_long)h->re_pad);
	__db_msg(env, "\trec_page: %lu", (u_long)h->rec_page);
	__db_msg(env, "\tpage_ext: %lu", (u_long)h->page_ext);

	return (0);
}

/*
 * __db_heapmeta --
 *	Print out the heap meta-data page.
 */
static int
__db_heapmeta(env, dbp, h, flags)
	ENV *env;
	DB *dbp;
	HEAPMETA *h;
	u_int32_t flags;
{
	__db_meta(env, dbp, (DBMETA *)h, NULL, flags);

	__db_msg(env, "\tcurregion: %lu", (u_long)h->curregion);
	__db_msg(env, "\tregion_size: %lu", (u_long)h->region_size);
	__db_msg(env, "\tnregions: %lu", (u_long)h->nregions);
	__db_msg(env, "\tgbytes: %lu", (u_long)h->gbytes);
	__db_msg(env, "\tbytes: %lu", (u_long)h->bytes);

	return (0);
}

/*
 * __db_heapint --
 *	Print out the heap internal-data page.
 */
static int
__db_heapint(dbp, h, flags)
	DB *dbp;
	HEAPPG *h;
	u_int32_t flags;
{
	DB_MSGBUF mb;
	ENV *env;
	int count, printed;
	u_int32_t i, max;
	u_int8_t avail;

	env = dbp->env;
	DB_MSGBUF_INIT(&mb);
	count = printed = 0;
	COMPQUIET(flags, 0);

	__db_msgadd(env, &mb, "\thigh: %4lu\n", (u_long)h->high_pgno);
	/* How many entries could there be on a page */
	max = HEAP_REGION_SIZE(dbp);

	for (i = 0; i < max; i++, count++) {
		avail = HEAP_SPACE(dbp, h, i);
		if (avail != 0) {
			__db_msgadd(env, &mb,
			    "%5lu:%1lu ", (u_long)i, (u_long)avail);
			printed = 1;
		}
		/* We get 10 entries per line this way */
		if (count == 9) {
			DB_MSGBUF_FLUSH(env, &mb);
			count = -1;
		}
	}
	/* All pages were less than 33% full */
	if (printed == 0)
		 __db_msgadd(env, &mb,
		    "All pages in this region less than 33 percent full");

	DB_MSGBUF_FLUSH(env, &mb);
	return (0);
}

/*
 * For printing pages from the log we may be passed the data segment
 * separate from the header, if so then it starts at HOFFSET.
 */
#define	PR_ENTRY(dbp, h, i, data)				\
	(data == NULL ? P_ENTRY(dbp, h, i) :			\
	    (u_int8_t *)data + P_INP(dbp, h)[i] - HOFFSET(h))
/*
 * __db_prpage_int
 *	-- Print out a page.
 *
 * PUBLIC: int __db_prpage_int __P((ENV *, DB_MSGBUF *,
 * PUBLIC:      DB *, char *, PAGE *, u_int32_t, u_int8_t *, u_int32_t));
 */
int
__db_prpage_int(env, mbp, dbp, lead, h, pagesize, data, flags)
	ENV *env;
	DB_MSGBUF *mbp;
	DB *dbp;
	char *lead;
	PAGE *h;
	u_int32_t pagesize;
	u_int8_t *data;
	u_int32_t flags;
{
	BINTERNAL *bi;
	BKEYDATA *bk;
	HOFFPAGE a_hkd;
	QAMDATA *qp, *qep;
	RINTERNAL *ri;
	HEAPHDR *hh;
	HEAPSPLITHDR *hs;
	db_indx_t dlen, len, i, *inp, max;
	db_pgno_t pgno;
	db_recno_t recno;
	u_int32_t qlen;
	u_int8_t *ep, *hk, *p;
	int deleted, ret;
	const char *s;
	void *hdata, *sp;

	/*
	 * If we're doing recovery testing and this page is P_INVALID,
	 * assume it's a page that's on the free list, and don't display it.
	 */
	if (LF_ISSET(DB_PR_RECOVERYTEST) && TYPE(h) == P_INVALID)
		return (0);

	if ((s = __db_pagetype_to_string(TYPE(h))) == NULL) {
		__db_msg(env, "%sILLEGAL PAGE TYPE: page: %lu type: %lu",
		    lead, (u_long)h->pgno, (u_long)TYPE(h));
		return (EINVAL);
	}

	/* Page number, page type. */
	__db_msgadd(env, mbp, "%spage %lu: %s:", lead, (u_long)h->pgno, s);

	/*
	 * LSNs on a metadata page will be different from the original after an
	 * abort, in some cases.  Don't display them if we're testing recovery.
	 */
	if (!LF_ISSET(DB_PR_RECOVERYTEST) ||
	    (TYPE(h) != P_BTREEMETA && TYPE(h) != P_HASHMETA &&
	    TYPE(h) != P_QAMMETA && TYPE(h) != P_QAMDATA &&
	    TYPE(h) != P_HEAPMETA))
		__db_msgadd(env, mbp, " LSN [%lu][%lu]:",
		    (u_long)LSN(h).file, (u_long)LSN(h).offset);

	/*
	 * Page level (only applicable for Btree/Recno, but we always display
	 * it, for no particular reason, except for Heap.
	 */
	if (!HEAPTYPE(h))
	    __db_msgadd(env, mbp, " level %lu", (u_long)h->level);

	/* Record count. */
	if (TYPE(h) == P_IBTREE || TYPE(h) == P_IRECNO ||
	    (dbp != NULL && TYPE(h) == P_LRECNO &&
	    h->pgno == ((BTREE *)dbp->bt_internal)->bt_root))
		__db_msgadd(env, mbp, " records: %lu", (u_long)RE_NREC(h));
	DB_MSGBUF_FLUSH(env, mbp);

	switch (TYPE(h)) {
	case P_BTREEMETA:
		return (__db_bmeta(env, dbp, (BTMETA *)h, flags));
	case P_HASHMETA:
		return (__db_hmeta(env, dbp, (HMETA *)h, flags));
	case P_QAMMETA:
		return (__db_qmeta(env, dbp, (QMETA *)h, flags));
	case P_QAMDATA:				/* Should be meta->start. */
		if (!LF_ISSET(DB_PR_PAGE) || dbp == NULL)
			return (0);

		qlen = ((QUEUE *)dbp->q_internal)->re_len;
		recno = (h->pgno - 1) * QAM_RECNO_PER_PAGE(dbp) + 1;
		i = 0;
		qep = (QAMDATA *)((u_int8_t *)h + pagesize - qlen);
		for (qp = QAM_GET_RECORD(dbp, h, i); qp < qep;
		    recno++, i++, qp = QAM_GET_RECORD(dbp, h, i)) {
			if (!F_ISSET(qp, QAM_SET))
				continue;

			__db_msgadd(env, mbp, "%s",
			    F_ISSET(qp, QAM_VALID) ? "\t" : "       D");
			__db_msgadd(env, mbp, "[%03lu] %4lu ", (u_long)recno,
			    (u_long)((u_int8_t *)qp - (u_int8_t *)h));
			__db_prbytes(env, mbp, qp->data, qlen);
		}
		return (0);
	case P_HEAPMETA:
		return (__db_heapmeta(env, dbp, (HEAPMETA *)h, flags));
	case P_IHEAP:
		if (!LF_ISSET(DB_PR_PAGE) || dbp == NULL)
			return (0);
		return (__db_heapint(dbp, (HEAPPG *)h, flags));
	default:
		break;
	}

	s = "\t";
	if (!HEAPTYPE(h) && TYPE(h) != P_IBTREE && TYPE(h) != P_IRECNO) {
		__db_msgadd(env, mbp, "%sprev: %4lu next: %4lu",
		    s, (u_long)PREV_PGNO(h), (u_long)NEXT_PGNO(h));
		s = " ";
	}

	if (HEAPTYPE(h)) {
		__db_msgadd(env, mbp, "%shigh indx: %4lu free indx: %4lu", s,
		    (u_long)HEAP_HIGHINDX(h), (u_long)HEAP_FREEINDX(h));
		s = " ";
	}

	if (TYPE(h) == P_OVERFLOW) {
		__db_msgadd(env, mbp,
		    "%sref cnt: %4lu ", s, (u_long)OV_REF(h));
		if (dbp == NULL)
			__db_msgadd(env, mbp,
			    " len: %4lu ", (u_long)OV_LEN(h));
		else
			__db_prbytes(env,
			    mbp, (u_int8_t *)h + P_OVERHEAD(dbp), OV_LEN(h));
		return (0);
	}
	__db_msgadd(env, mbp, "%sentries: %4lu", s, (u_long)NUM_ENT(h));
	__db_msgadd(env, mbp, " offset: %4lu", (u_long)HOFFSET(h));
	DB_MSGBUF_FLUSH(env, mbp);

	if (dbp == NULL || TYPE(h) == P_INVALID || !LF_ISSET(DB_PR_PAGE))
		return (0);

	if (data != NULL)
		pagesize += HOFFSET(h);
	else if (pagesize < HOFFSET(h))
		return (0);

	ret = 0;
	inp = P_INP(dbp, h);
	max = TYPE(h) == P_HEAP ? HEAP_HIGHINDX(h) + 1 : NUM_ENT(h);
	for (i = 0; i < max; i++) {
		if (TYPE(h) == P_HEAP && inp[i] == 0)
			continue;
		if ((uintptr_t)(P_ENTRY(dbp, h, i) - (u_int8_t *)h) <
		    (uintptr_t)(P_OVERHEAD(dbp)) ||
		    (size_t)(P_ENTRY(dbp, h, i) - (u_int8_t *)h) >= pagesize) {
			__db_msg(env,
			    "ILLEGAL PAGE OFFSET: indx: %lu of %lu",
			    (u_long)i, (u_long)inp[i]);
			ret = EINVAL;
			continue;
		}
		deleted = 0;
		switch (TYPE(h)) {
		case P_HASH_UNSORTED:
		case P_HASH:
		case P_IBTREE:
		case P_IRECNO:
			sp = PR_ENTRY(dbp, h, i, data);
			break;
		case P_HEAP:
			sp = P_ENTRY(dbp, h, i);
			break;
		case P_LBTREE:
			sp = PR_ENTRY(dbp, h, i, data);
			deleted = i % 2 == 0 &&
			    B_DISSET(GET_BKEYDATA(dbp, h, i + O_INDX)->type);
			break;
		case P_LDUP:
		case P_LRECNO:
			sp = PR_ENTRY(dbp, h, i, data);
			deleted = B_DISSET(GET_BKEYDATA(dbp, h, i)->type);
			break;
		default:
			goto type_err;
		}
		__db_msgadd(env, mbp, "%s", deleted ? "       D" : "\t");
		__db_msgadd(
		    env, mbp, "[%03lu] %4lu ", (u_long)i, (u_long)inp[i]);
		switch (TYPE(h)) {
		case P_HASH_UNSORTED:
		case P_HASH:
			hk = sp;
			switch (HPAGE_PTYPE(hk)) {
			case H_OFFDUP:
				memcpy(&pgno,
				    HOFFDUP_PGNO(hk), sizeof(db_pgno_t));
				__db_msgadd(env, mbp,
				    "%4lu [offpage dups]", (u_long)pgno);
				DB_MSGBUF_FLUSH(env, mbp);
				break;
			case H_DUPLICATE:
				/*
				 * If this is the first item on a page, then
				 * we cannot figure out how long it is, so
				 * we only print the first one in the duplicate
				 * set.
				 */
				if (i != 0)
					len = LEN_HKEYDATA(dbp, h, 0, i);
				else
					len = 1;

				__db_msgadd(env, mbp, "Duplicates:");
				DB_MSGBUF_FLUSH(env, mbp);
				for (p = HKEYDATA_DATA(hk),
				    ep = p + len; p < ep;) {
					memcpy(&dlen, p, sizeof(db_indx_t));
					p += sizeof(db_indx_t);
					__db_msgadd(env, mbp, "\t\t");
					__db_prbytes(env, mbp, p, dlen);
					p += sizeof(db_indx_t) + dlen;
				}
				break;
			case H_KEYDATA:
				__db_prbytes(env, mbp, HKEYDATA_DATA(hk),
				    LEN_HKEYDATA(dbp, h, i == 0 ?
				    pagesize : 0, i));
				break;
			case H_OFFPAGE:
				memcpy(&a_hkd, hk, HOFFPAGE_SIZE);
				__db_msgadd(env, mbp,
				    "overflow: total len: %4lu page: %4lu",
				    (u_long)a_hkd.tlen, (u_long)a_hkd.pgno);
				DB_MSGBUF_FLUSH(env, mbp);
				break;
			default:
				DB_MSGBUF_FLUSH(env, mbp);
				__db_msg(env, "ILLEGAL HASH PAGE TYPE: %lu",
				    (u_long)HPAGE_PTYPE(hk));
				ret = EINVAL;
				break;
			}
			break;
		case P_IBTREE:
			bi = sp;

			if (F_ISSET(dbp, DB_AM_RECNUM))
				__db_msgadd(env, mbp,
				    "count: %4lu ", (u_long)bi->nrecs);
			__db_msgadd(env, mbp,
			    "pgno: %4lu type: %lu ",
			    (u_long)bi->pgno, (u_long)bi->type);
			switch (B_TYPE(bi->type)) {
			case B_KEYDATA:
				__db_prbytes(env, mbp, bi->data, bi->len);
				break;
			case B_DUPLICATE:
			case B_OVERFLOW:
				__db_proff(env, mbp, bi->data);
				break;
			default:
				DB_MSGBUF_FLUSH(env, mbp);
				__db_msg(env, "ILLEGAL BINTERNAL TYPE: %lu",
				    (u_long)B_TYPE(bi->type));
				ret = EINVAL;
				break;
			}
			break;
		case P_IRECNO:
			ri = sp;
			__db_msgadd(env, mbp, "entries %4lu pgno %4lu",
			    (u_long)ri->nrecs, (u_long)ri->pgno);
			DB_MSGBUF_FLUSH(env, mbp);
			break;
		case P_LBTREE:
		case P_LDUP:
		case P_LRECNO:
			bk = sp;
			switch (B_TYPE(bk->type)) {
			case B_KEYDATA:
				__db_prbytes(env, mbp, bk->data, bk->len);
				break;
			case B_DUPLICATE:
			case B_OVERFLOW:
				__db_proff(env, mbp, bk);
				break;
			default:
				DB_MSGBUF_FLUSH(env, mbp);
				__db_msg(env,
			    "ILLEGAL DUPLICATE/LBTREE/LRECNO TYPE: %lu",
				    (u_long)B_TYPE(bk->type));
				ret = EINVAL;
				break;
			}
			break;
		case P_HEAP:
			hh = sp;
			if (!F_ISSET(hh,HEAP_RECSPLIT))
				hdata = (u_int8_t *)hh + sizeof(HEAPHDR);
			else {
				hs = sp;
				__db_msgadd(env, mbp,
				     "split: 0x%02x tsize: %lu next: %lu.%lu ",
				     hh->flags, (u_long)hs->tsize,
				     (u_long)hs->nextpg, (u_long)hs->nextindx);

				hdata = (u_int8_t *)hh + sizeof(HEAPSPLITHDR);
			}
			__db_prbytes(env, mbp, hdata, hh->size);
			break;
		default:
type_err:		DB_MSGBUF_FLUSH(env, mbp);
			__db_msg(env,
			    "ILLEGAL PAGE TYPE: %lu", (u_long)TYPE(h));
			ret = EINVAL;
			continue;
		}
	}
	return (ret);
}

/*
 * __db_prbytes --
 *	Print out a data element.
 *
 * PUBLIC: void __db_prbytes __P((ENV *, DB_MSGBUF *, u_int8_t *, u_int32_t));
 */
void
__db_prbytes(env, mbp, bytes, len)
	ENV *env;
	DB_MSGBUF *mbp;
	u_int8_t *bytes;
	u_int32_t len;
{
	u_int8_t *p;
	u_int32_t i, not_printable;
	int msg_truncated;

	__db_msgadd(env, mbp, "len: %3lu", (u_long)len);
	if (len != 0) {
		__db_msgadd(env, mbp, " data: ");

		/*
		 * Print the first N bytes of the data.   If that
		 * chunk is at least 3/4  printable characters, print
		 * it as text, else print it in hex.  We have this
		 * heuristic because we're displaying things like
		 * lock objects that could be either text or data.
		 */
		if (len > env->data_len) {
			len = env->data_len;
			msg_truncated = 1;
		} else
			msg_truncated = 0;
		not_printable = 0;
		for (p = bytes, i = 0; i < len; ++i, ++p) {
			if (!isprint((int)*p) && *p != '\t' && *p != '\n') {
				if (i == len - 1 && *p == '\0')
					break;
				if (++not_printable >= (len >> 2))
					break;
			}
		}
		if (not_printable < (len >> 2))
			for (p = bytes, i = len; i > 0; --i, ++p) {
				if (isprint((int)*p))
					__db_msgadd(env, mbp, "%c", *p);
				else
					__db_msgadd(env,
					    mbp, "\\%x", (u_int)*p);
			}
		else
			for (p = bytes, i = len; i > 0; --i, ++p)
				__db_msgadd(env, mbp, "%.2x", (u_int)*p);
		if (msg_truncated)
			__db_msgadd(env, mbp, "...");
	}
	DB_MSGBUF_FLUSH(env, mbp);
}

/*
 * __db_proff --
 *	Print out an off-page element.
 */
static void
__db_proff(env, mbp, vp)
	ENV *env;
	DB_MSGBUF *mbp;
	void *vp;
{
	BOVERFLOW *bo;

	bo = vp;
	switch (B_TYPE(bo->type)) {
	case B_OVERFLOW:
		__db_msgadd(env, mbp, "overflow: total len: %4lu page: %4lu",
		    (u_long)bo->tlen, (u_long)bo->pgno);
		break;
	case B_DUPLICATE:
		__db_msgadd(
		    env, mbp, "duplicate: page: %4lu", (u_long)bo->pgno);
		break;
	default:
		/* NOTREACHED */
		break;
	}
	DB_MSGBUF_FLUSH(env, mbp);
}

/*
 * __db_prflags --
 *	Print out flags values.
 *
 * PUBLIC: void __db_prflags __P((ENV *, DB_MSGBUF *,
 * PUBLIC:     u_int32_t, const FN *, const char *, const char *));
 */
void
__db_prflags(env, mbp, flags, fn, prefix, suffix)
	ENV *env;
	DB_MSGBUF *mbp;
	u_int32_t flags;
	FN const *fn;
	const char *prefix, *suffix;
{
	DB_MSGBUF mb;
	const FN *fnp;
	int found, standalone;
	const char *sep;

	if (fn == NULL)
		return;

	/*
	 * If it's a standalone message, output the suffix (which will be the
	 * label), regardless of whether we found anything or not, and flush
	 * the line.
	 */
	if (mbp == NULL) {
		standalone = 1;
		mbp = &mb;
		DB_MSGBUF_INIT(mbp);
	} else
		standalone = 0;

	sep = prefix == NULL ? "" : prefix;
	for (found = 0, fnp = fn; fnp->mask != 0; ++fnp)
		if (LF_ISSET(fnp->mask)) {
			__db_msgadd(env, mbp, "%s%s", sep, fnp->name);
			sep = ", ";
			found = 1;
		}

	if ((standalone || found) && suffix != NULL)
		__db_msgadd(env, mbp, "%s", suffix);
	if (standalone)
		DB_MSGBUF_FLUSH(env, mbp);
}

/*
 * __db_name_to_val --
 *	Return the integral value associated with the string, or -1 if missing.
 *	It is intended for looking up string names of enums and single bit
 *	in order to get a numeric value.
 *
 * PUBLIC: int __db_name_to_val __P((FN const *, char *));
 */
int
__db_name_to_val(strtable, s)
	FN const *strtable;
	char *s;
{
	if (s != NULL) {
		do {
			if (strcasecmp(strtable->name, s) == 0)
				return ((int)strtable->mask);
		} while ((++strtable)->name != NULL);
	}
	return (-1);
}

/*
 * __db_pagetype_to_string --
 *	Return the name of the specified page type.
 * PUBLIC: const char *__db_pagetype_to_string __P((u_int32_t));
 */
const char *
__db_pagetype_to_string(type)
	u_int32_t type;
{
	char *s;

	s = NULL;
	switch (type) {
	case P_BTREEMETA:
		s = "btree metadata";
		break;
	case P_LDUP:
		s = "duplicate";
		break;
	case P_HASH_UNSORTED:
		s = "hash unsorted";
		break;
	case P_HASH:
		s = "hash";
		break;
	case P_HASHMETA:
		s = "hash metadata";
		break;
	case P_IBTREE:
		s = "btree internal";
		break;
	case P_INVALID:
		s = "invalid";
		break;
	case P_IRECNO:
		s = "recno internal";
		break;
	case P_LBTREE:
		s = "btree leaf";
		break;
	case P_LRECNO:
		s = "recno leaf";
		break;
	case P_OVERFLOW:
		s = "overflow";
		break;
	case P_QAMMETA:
		s = "queue metadata";
		break;
	case P_QAMDATA:
		s = "queue";
		break;
	case P_HEAPMETA:
		s = "heap metadata";
		break;
	case P_HEAP:
		s = "heap data";
		break;
	case P_IHEAP:
		s = "heap internal";
		break;
	default:
		/* Just return a NULL. */
		break;
	}
	return (s);
}

/*
 * __db_dump_pp --
 *	DB->dump pre/post processing.
 *
 * PUBLIC: int __db_dump_pp __P((DB *, const char *,
 * PUBLIC:     int (*)(void *, const void *), void *, int, int));
 */
int
__db_dump_pp(dbp, subname, callback, handle, pflag, keyflag)
	DB *dbp;
	const char *subname;
	int (*callback) __P((void *, const void *));
	void *handle;
	int pflag, keyflag;
{
	DB_THREAD_INFO *ip;
	ENV *env;
	int handle_check, ret, t_ret;

	env = dbp->env;

	DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->dump");

	ENV_ENTER(env, ip);

	/* Check for replication block. */
	handle_check = IS_ENV_REPLICATED(env);
	if (handle_check && (ret = __db_rep_enter(dbp, 1, 0, 1)) != 0) {
		handle_check = 0;
		goto err;
	}

	ret = __db_dump(dbp, subname, callback, handle, pflag, keyflag);

	/* Release replication block. */
	if (handle_check && (t_ret = __env_db_rep_exit(env)) != 0 && ret == 0)
		ret = t_ret;

err:	ENV_LEAVE(env, ip);
	return (ret);
}

/*
 * __db_dump --
 *	DB->dump.
 *
 * PUBLIC: int __db_dump __P((DB *, const char *,
 * PUBLIC:     int (*)(void *, const void *), void *, int, int));
 */
int
__db_dump(dbp, subname, callback, handle, pflag, keyflag)
	DB *dbp;
	const char *subname;
	int (*callback) __P((void *, const void *));
	void *handle;
	int pflag, keyflag;
{
	DBC *dbcp;
	DBT key, data;
	DBT keyret, dataret;
	DB_HEAP_RID rid;
	ENV *env;
	db_recno_t recno;
	int is_recno, is_heap, ret, t_ret;
	void *pointer;

	env = dbp->env;
	is_heap = 0;

	if ((ret = __db_prheader(
	    dbp, subname, pflag, keyflag, handle, callback, NULL, 0)) != 0)
		return (ret);

	/*
	 * Get a cursor and step through the database, printing out each
	 * key/data pair.
	 */
	if ((ret = __db_cursor(dbp, NULL, NULL, &dbcp, 0)) != 0)
		return (ret);

	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
	if ((ret = __os_malloc(env, 1024 * 1024, &data.data)) != 0)
		goto err;
	data.ulen = 1024 * 1024;
	data.flags = DB_DBT_USERMEM;
	is_recno = (dbp->type == DB_RECNO || dbp->type == DB_QUEUE);
	keyflag = is_recno ? keyflag : 1;
	if (is_recno) {
		keyret.data = &recno;
		keyret.size = sizeof(recno);
	}

	if (dbp->type == DB_HEAP) {
		is_heap = 1;
		key.data = &rid;
		key.size = key.ulen = sizeof(DB_HEAP_RID);
		key.flags = DB_DBT_USERMEM;
	}

retry: while ((ret =
	    __dbc_get(dbcp, &key, &data,
	    !is_heap ? DB_NEXT | DB_MULTIPLE_KEY : DB_NEXT )) == 0) {
		if (is_heap) {
			/* Never dump keys for HEAP */
			if ((ret = __db_prdbt(
			    &data, pflag, " ", handle, callback, 0, 0)) != 0)
				goto err;
			continue;
		}
		DB_MULTIPLE_INIT(pointer, &data);
		for (;;) {
			if (is_recno)
				DB_MULTIPLE_RECNO_NEXT(pointer, &data,
				    recno, dataret.data, dataret.size);
			else
				DB_MULTIPLE_KEY_NEXT(pointer, &data,
				    keyret.data, keyret.size,
				    dataret.data, dataret.size);

			if (dataret.data == NULL)
				break;

			if ((keyflag &&
			    (ret = __db_prdbt(&keyret, pflag, " ",
			    handle, callback, is_recno, 0)) != 0) ||
			    (ret = __db_prdbt(&dataret, pflag, " ",
			    handle, callback, 0, 0)) != 0)
					goto err;
		}
	}
	if (ret == DB_BUFFER_SMALL) {
		data.size = (u_int32_t)DB_ALIGN(data.size, 1024);
		if ((ret = __os_realloc(env, data.size, &data.data)) != 0)
			goto err;
		data.ulen = data.size;
		goto retry;
	}
	if (ret == DB_NOTFOUND)
		ret = 0;

	if ((t_ret = __db_prfooter(handle, callback)) != 0 && ret == 0)
		ret = t_ret;

err:	if ((t_ret = __dbc_close(dbcp)) != 0 && ret == 0)
		ret = t_ret;
	if (data.data != NULL)
		__os_free(env, data.data);

	return (ret);
}

/*
 * __db_prdbt --
 *	Print out a DBT data element.
 *
 * PUBLIC: int __db_prdbt __P((DBT *, int, const char *, void *,
 * PUBLIC:     int (*)(void *, const void *), int, int));
 */
int
__db_prdbt(dbtp, checkprint, prefix, handle, callback, is_recno, is_heap)
	DBT *dbtp;
	int checkprint;
	const char *prefix;
	void *handle;
	int (*callback) __P((void *, const void *));
	int is_recno;
	int is_heap;
{
	static const u_char hex[] = "0123456789abcdef";
	db_recno_t recno;
	DB_HEAP_RID rid;
	size_t len;
	int ret;
#define	DBTBUFLEN	100
	u_int8_t *p, *hp;
	char buf[DBTBUFLEN], hbuf[DBTBUFLEN];

	/*
	 * !!!
	 * This routine is the routine that dumps out items in the format
	 * used by db_dump(1) and db_load(1).  This means that the format
	 * cannot change.
	 */
	if (prefix != NULL && (ret = callback(handle, prefix)) != 0)
		return (ret);
	if (is_recno) {
		/*
		 * We're printing a record number, and this has to be done
		 * in a platform-independent way.  So we use the numeral in
		 * straight ASCII.
		 */
		(void)__ua_memcpy(&recno, dbtp->data, sizeof(recno));
		snprintf(buf, DBTBUFLEN, "%lu", (u_long)recno);

		/* If we're printing data as hex, print keys as hex too. */
		if (!checkprint) {
			for (len = strlen(buf), p = (u_int8_t *)buf,
			    hp = (u_int8_t *)hbuf; len-- > 0; ++p) {
				*hp++ = hex[(u_int8_t)(*p & 0xf0) >> 4];
				*hp++ = hex[*p & 0x0f];
			}
			*hp = '\0';
			ret = callback(handle, hbuf);
		} else
			ret = callback(handle, buf);

		if (ret != 0)
			return (ret);
	} else if (is_heap) {
		/*
		 * We're printing a heap record number, and this has to be
		 * done in a platform-independent way.  So we use the numeral
		 * in straight ASCII.
		 */
		(void)__ua_memcpy(&rid, dbtp->data, sizeof(rid));
		snprintf(buf, DBTBUFLEN, "%lu %hu",
		    (u_long)rid.pgno, (u_short)rid.indx);

		/* If we're printing data as hex, print keys as hex too. */
		if (!checkprint) {
			for (len = strlen(buf), p = (u_int8_t *)buf,
			    hp = (u_int8_t *)hbuf; len-- > 0; ++p) {
				*hp++ = hex[(u_int8_t)(*p & 0xf0) >> 4];
				*hp++ = hex[*p & 0x0f];
			}
			*hp = '\0';
			ret = callback(handle, hbuf);
		} else
			ret = callback(handle, buf);

		if (ret != 0)
			return (ret);
	} else if (checkprint) {
		for (len = dbtp->size, p = dbtp->data; len--; ++p)
			if (isprint((int)*p)) {
				if (*p == '\\' &&
				    (ret = callback(handle, "\\")) != 0)
					return (ret);
				snprintf(buf, DBTBUFLEN, "%c", *p);
				if ((ret = callback(handle, buf)) != 0)
					return (ret);
			} else {
				snprintf(buf, DBTBUFLEN, "\\%c%c",
				    hex[(u_int8_t)(*p & 0xf0) >> 4],
				    hex[*p & 0x0f]);
				if ((ret = callback(handle, buf)) != 0)
					return (ret);
			}
	} else
		for (len = dbtp->size, p = dbtp->data; len--; ++p) {
			snprintf(buf, DBTBUFLEN, "%c%c",
			    hex[(u_int8_t)(*p & 0xf0) >> 4],
			    hex[*p & 0x0f]);
			if ((ret = callback(handle, buf)) != 0)
				return (ret);
		}

	return (callback(handle, "\n"));
}

/*
 * __db_prheader --
 *	Write out header information in the format expected by db_load.
 *
 * PUBLIC: int	__db_prheader __P((DB *, const char *, int, int, void *,
 * PUBLIC:     int (*)(void *, const void *), VRFY_DBINFO *, db_pgno_t));
 */
int
__db_prheader(dbp, subname, pflag, keyflag, handle, callback, vdp, meta_pgno)
	DB *dbp;
	const char *subname;
	int pflag, keyflag;
	void *handle;
	int (*callback) __P((void *, const void *));
	VRFY_DBINFO *vdp;
	db_pgno_t meta_pgno;
{
	DBT dbt;
	DBTYPE dbtype;
	ENV *env;
	VRFY_PAGEINFO *pip;
	u_int32_t flags, tmp_u_int32;
	size_t buflen;
	char *buf;
	int using_vdp, ret, t_ret, tmp_int;
#ifdef HAVE_HEAP
	u_int32_t tmp2_u_int32;
#endif

	ret = 0;
	buf = NULL;
	COMPQUIET(buflen, 0);

	/*
	 * If dbp is NULL, then pip is guaranteed to be non-NULL; we only ever
	 * call __db_prheader with a NULL dbp from one case inside __db_prdbt,
	 * and this is a special subdatabase for "lost" items.  In this case
	 * we have a vdp (from which we'll get a pip).  In all other cases, we
	 * will have a non-NULL dbp (and vdp may or may not be NULL depending
	 * on whether we're salvaging).
	 */
	if (dbp == NULL)
		env = NULL;
	else
		env = dbp->env;
	DB_ASSERT(env, dbp != NULL || vdp != NULL);

	/*
	 * If we've been passed a verifier statistics object, use that;  we're
	 * being called in a context where dbp->stat is unsafe.
	 *
	 * Also, the verifier may set the pflag on a per-salvage basis.  If so,
	 * respect that.
	 */
	if (vdp != NULL) {
		if ((ret = __db_vrfy_getpageinfo(vdp, meta_pgno, &pip)) != 0)
			return (ret);

		if (F_ISSET(vdp, SALVAGE_PRINTABLE))
			pflag = 1;
		using_vdp = 1;
	} else {
		pip = NULL;
		using_vdp = 0;
	}

	/*
	 * If dbp is NULL, make it a btree.  Otherwise, set dbtype to whatever
	 * appropriate type for the specified meta page, or the type of the dbp.
	 */
	if (dbp == NULL)
		dbtype = DB_BTREE;
	else if (using_vdp)
		switch (pip->type) {
		case P_BTREEMETA:
			if (F_ISSET(pip, VRFY_IS_RECNO))
				dbtype = DB_RECNO;
			else
				dbtype = DB_BTREE;
			break;
		case P_HASHMETA:
			dbtype = DB_HASH;
			break;
		case P_HEAPMETA:
			dbtype = DB_HEAP;
			break;
		case P_QAMMETA:
			dbtype = DB_QUEUE;
			break;
		default:
			/*
			 * If the meta page is of a bogus type, it's because
			 * we have a badly corrupt database.  (We must be in
			 * the verifier for pip to be non-NULL.) Pretend we're
			 * a Btree and salvage what we can.
			 */
			DB_ASSERT(env, F_ISSET(dbp, DB_AM_VERIFYING));
			dbtype = DB_BTREE;
			break;
		}
	else
		dbtype = dbp->type;

	if ((ret = callback(handle, "VERSION=3\n")) != 0)
		goto err;
	if (pflag) {
		if ((ret = callback(handle, "format=print\n")) != 0)
			goto err;
	} else if ((ret = callback(handle, "format=bytevalue\n")) != 0)
		goto err;

	/*
	 * 64 bytes is long enough, as a minimum bound, for any of the
	 * fields besides subname.  Subname uses __db_prdbt and therefore
	 * does not need buffer space here.
	 */
	buflen = 64;
	if ((ret = __os_malloc(env, buflen, &buf)) != 0)
		goto err;
	if (subname != NULL) {
		snprintf(buf, buflen, "database=");
		if ((ret = callback(handle, buf)) != 0)
			goto err;
		DB_INIT_DBT(dbt, subname, strlen(subname));
		if ((ret = __db_prdbt(&dbt, 1,
		    NULL, handle, callback, 0, 0)) != 0)
			goto err;
	}
	switch (dbtype) {
	case DB_BTREE:
		if ((ret = callback(handle, "type=btree\n")) != 0)
			goto err;
		if (using_vdp)
			tmp_int = F_ISSET(pip, VRFY_HAS_RECNUMS) ? 1 : 0;
		else {
			if ((ret = __db_get_flags(dbp, &flags)) != 0) {
				__db_err(env, ret, "DB->get_flags");
				goto err;
			}
			tmp_int = F_ISSET(dbp, DB_AM_RECNUM) ? 1 : 0;
		}
		if (tmp_int && (ret = callback(handle, "recnum=1\n")) != 0)
			goto err;

		if (using_vdp)
			tmp_u_int32 = pip->bt_minkey;
		else
			if ((ret =
			    __bam_get_bt_minkey(dbp, &tmp_u_int32)) != 0) {
				__db_err(env, ret, "DB->get_bt_minkey");
				goto err;
			}
		if (tmp_u_int32 != 0 && tmp_u_int32 != DEFMINKEYPAGE) {
			snprintf(buf, buflen,
			    "bt_minkey=%lu\n", (u_long)tmp_u_int32);
			if ((ret = callback(handle, buf)) != 0)
				goto err;
		}
		break;
	case DB_HASH:
#ifdef HAVE_HASH
		if ((ret = callback(handle, "type=hash\n")) != 0)
			goto err;
		if (using_vdp)
			tmp_u_int32 = pip->h_ffactor;
		else
			if ((ret =
			    __ham_get_h_ffactor(dbp, &tmp_u_int32)) != 0) {
				__db_err(env, ret, "DB->get_h_ffactor");
				goto err;
			}
		if (tmp_u_int32 != 0) {
			snprintf(buf, buflen,
			    "h_ffactor=%lu\n", (u_long)tmp_u_int32);
			if ((ret = callback(handle, buf)) != 0)
				goto err;
		}

		if (using_vdp)
			tmp_u_int32 = pip->h_nelem;
		else
			if ((ret = __ham_get_h_nelem(dbp, &tmp_u_int32)) != 0) {
				__db_err(env, ret, "DB->get_h_nelem");
				goto err;
			}
		/*
		 * Hash databases have an h_nelem field of 0 or 1, neither
		 * of those values is interesting.
		 */
		if (tmp_u_int32 > 1) {
			snprintf(buf, buflen,
			    "h_nelem=%lu\n", (u_long)tmp_u_int32);
			if ((ret = callback(handle, buf)) != 0)
				goto err;
		}
		break;
#else
		ret = __db_no_hash_am(env);
		goto err;
#endif
	case DB_HEAP:
#ifdef HAVE_HEAP
		if ((ret = callback(handle, "type=heap\n")) != 0)
			goto err;

		if ((ret = __heap_get_heapsize(
		    dbp, &tmp_u_int32, &tmp2_u_int32)) != 0) {
			__db_err(env, ret, "DB->get_heapsize");
			goto err;
		}
		if (tmp_u_int32 != 0) {
			snprintf(buf,
			    buflen, "heap_gbytes=%lu\n", (u_long)tmp_u_int32);
			if ((ret = callback(handle, buf)) != 0)
				goto err;
		}
		if (tmp2_u_int32 != 0) {
			snprintf(buf,
			    buflen, "heap_bytes=%lu\n", (u_long)tmp2_u_int32);
			if ((ret = callback(handle, buf)) != 0)
				goto err;
		}

		if ((ret =
		    __heap_get_heap_regionsize(dbp, &tmp_u_int32)) != 0) {
			__db_err(env, ret, "DB->get_heap_regionsize");
			goto err;
		}
		if (tmp_u_int32 != 0) {
			snprintf(buf, buflen,
			    "heap_regionsize=%lu\n", (u_long)tmp_u_int32);
			if ((ret = callback(handle, buf)) != 0)
				goto err;
		}
		break;
#else
		ret = __db_no_heap_am(env);
		goto err;
#endif
	case DB_QUEUE:
#ifdef HAVE_QUEUE
		if ((ret = callback(handle, "type=queue\n")) != 0)
			goto err;
		if (using_vdp)
			tmp_u_int32 = vdp->re_len;
		else
			if ((ret = __ram_get_re_len(dbp, &tmp_u_int32)) != 0) {
				__db_err(env, ret, "DB->get_re_len");
				goto err;
			}
		snprintf(buf, buflen, "re_len=%lu\n", (u_long)tmp_u_int32);
		if ((ret = callback(handle, buf)) != 0)
			goto err;

		if (using_vdp)
			tmp_int = (int)vdp->re_pad;
		else
			if ((ret = __ram_get_re_pad(dbp, &tmp_int)) != 0) {
				__db_err(env, ret, "DB->get_re_pad");
				goto err;
			}
		if (tmp_int != 0 && tmp_int != ' ') {
			snprintf(buf, buflen, "re_pad=%#x\n", tmp_int);
			if ((ret = callback(handle, buf)) != 0)
				goto err;
		}

		if (using_vdp)
			tmp_u_int32 = vdp->page_ext;
		else
			if ((ret =
			    __qam_get_extentsize(dbp, &tmp_u_int32)) != 0) {
				__db_err(env, ret, "DB->get_q_extentsize");
				goto err;
			}
		if (tmp_u_int32 != 0) {
			snprintf(buf, buflen,
			    "extentsize=%lu\n", (u_long)tmp_u_int32);
			if ((ret = callback(handle, buf)) != 0)
				goto err;
		}
		break;
#else
		ret = __db_no_queue_am(env);
		goto err;
#endif
	case DB_RECNO:
		if ((ret = callback(handle, "type=recno\n")) != 0)
			goto err;
		if (using_vdp)
			tmp_int = F_ISSET(pip, VRFY_IS_RRECNO) ? 1 : 0;
		else
			tmp_int = F_ISSET(dbp, DB_AM_RENUMBER) ? 1 : 0;
		if (tmp_int != 0 &&
		    (ret = callback(handle, "renumber=1\n")) != 0)
				goto err;

		if (using_vdp)
			tmp_int = F_ISSET(pip, VRFY_IS_FIXEDLEN) ? 1 : 0;
		else
			tmp_int = F_ISSET(dbp, DB_AM_FIXEDLEN) ? 1 : 0;
		if (tmp_int) {
			if (using_vdp)
				tmp_u_int32 = pip->re_len;
			else
				if ((ret =
				    __ram_get_re_len(dbp, &tmp_u_int32)) != 0) {
					__db_err(env, ret, "DB->get_re_len");
					goto err;
				}
			snprintf(buf, buflen,
			    "re_len=%lu\n", (u_long)tmp_u_int32);
			if ((ret = callback(handle, buf)) != 0)
				goto err;

			if (using_vdp)
				tmp_int = (int)pip->re_pad;
			else
				if ((ret =
				    __ram_get_re_pad(dbp, &tmp_int)) != 0) {
					__db_err(env, ret, "DB->get_re_pad");
					goto err;
				}
			if (tmp_int != 0 && tmp_int != ' ') {
				snprintf(buf,
				    buflen, "re_pad=%#x\n", (u_int)tmp_int);
				if ((ret = callback(handle, buf)) != 0)
					goto err;
			}
		}
		break;
	case DB_UNKNOWN:			/* Impossible. */
		ret = __db_unknown_path(env, "__db_prheader");
		goto err;
	}

	if (using_vdp) {
		if (F_ISSET(pip, VRFY_HAS_CHKSUM))
			if ((ret = callback(handle, "chksum=1\n")) != 0)
				goto err;
		if (F_ISSET(pip, VRFY_HAS_DUPS))
			if ((ret = callback(handle, "duplicates=1\n")) != 0)
				goto err;
		if (F_ISSET(pip, VRFY_HAS_DUPSORT))
			if ((ret = callback(handle, "dupsort=1\n")) != 0)
				goto err;
#ifdef HAVE_COMPRESSION
		if (F_ISSET(pip, VRFY_HAS_COMPRESS))
			if ((ret = callback(handle, "compressed=1\n")) != 0)
				goto err;
#endif
		/*
		 * !!!
		 * We don't know if the page size was the default if we're
		 * salvaging.  It doesn't seem that interesting to have, so
		 * we ignore it for now.
		 */
	} else {
		if (F_ISSET(dbp, DB_AM_CHKSUM))
			if ((ret = callback(handle, "chksum=1\n")) != 0)
				goto err;
		if (F_ISSET(dbp, DB_AM_DUP))
			if ((ret = callback(handle, "duplicates=1\n")) != 0)
				goto err;
		if (F_ISSET(dbp, DB_AM_DUPSORT))
			if ((ret = callback(handle, "dupsort=1\n")) != 0)
				goto err;
#ifdef HAVE_COMPRESSION
		if (DB_IS_COMPRESSED(dbp))
			if ((ret = callback(handle, "compressed=1\n")) != 0)
				goto err;
#endif
		if (!F_ISSET(dbp, DB_AM_PGDEF)) {
			snprintf(buf, buflen,
			    "db_pagesize=%lu\n", (u_long)dbp->pgsize);
			if ((ret = callback(handle, buf)) != 0)
				goto err;
		}
	}

#ifdef HAVE_PARTITION
	if (dbp != NULL && DB_IS_PARTITIONED(dbp) &&
	    F_ISSET((DB_PARTITION *)dbp->p_internal, PART_RANGE)) {
		DBT *keys;
		u_int32_t i;

		if ((ret = __partition_get_keys(dbp, &tmp_u_int32, &keys)) != 0)
			goto err;
		if (tmp_u_int32 != 0) {
			snprintf(buf,
			     buflen, "nparts=%lu\n", (u_long)tmp_u_int32);
			if ((ret = callback(handle, buf)) != 0)
				goto err;
			for (i = 0; i < tmp_u_int32 - 1; i++)
			    if ((ret = __db_prdbt(&keys[i],
				pflag, " ", handle, callback, 0, 0)) != 0)
					goto err;
		}
	}
#endif

	if (keyflag && (ret = callback(handle, "keys=1\n")) != 0)
		goto err;

	ret = callback(handle, "HEADER=END\n");

err:	if (using_vdp &&
	    (t_ret = __db_vrfy_putpageinfo(env, vdp, pip)) != 0 && ret == 0)
		ret = t_ret;
	if (buf != NULL)
		__os_free(env, buf);

	return (ret);
}

/*
 * __db_prfooter --
 *	Print the footer that marks the end of a DB dump.  This is trivial,
 *	but for consistency's sake we don't want to put its literal contents
 *	in multiple places.
 *
 * PUBLIC: int __db_prfooter __P((void *, int (*)(void *, const void *)));
 */
int
__db_prfooter(handle, callback)
	void *handle;
	int (*callback) __P((void *, const void *));
{
	return (callback(handle, "DATA=END\n"));
}

/*
 * __db_pr_callback --
 *	Callback function for using pr_* functions from C.
 *
 * PUBLIC: int  __db_pr_callback __P((void *, const void *));
 */
int
__db_pr_callback(handle, str_arg)
	void *handle;
	const void *str_arg;
{
	char *str;
	FILE *f;

	str = (char *)str_arg;
	f = (FILE *)handle;

	if (fprintf(f, "%s", str) != (int)strlen(str))
		return (EIO);

	return (0);
}

/*
 * __db_dbtype_to_string --
 *	Return the name of the database type.
 *
 * PUBLIC: const char * __db_dbtype_to_string __P((DBTYPE));
 */
const char *
__db_dbtype_to_string(type)
	DBTYPE type;
{
	switch (type) {
	case DB_BTREE:
		return ("btree");
	case DB_HASH:
		return ("hash");
	case DB_RECNO:
		return ("recno");
	case DB_QUEUE:
		return ("queue");
	case DB_HEAP:
		return ("heap");
	case DB_UNKNOWN:
	default:
		break;
	}
	return ("UNKNOWN TYPE");
}