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
|
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <gdbm.h>
#include <fcntl.h>
#define fetch_key 0
#define store_key 1
#define fetch_value 2
#define store_value 3
typedef struct {
GDBM_FILE dbp ;
SV * filter[4];
int filtering ;
} GDBM_File_type;
typedef GDBM_File_type * GDBM_File ;
typedef datum datum_key ;
typedef datum datum_value ;
typedef datum datum_key_copy;
/* Indexes for gdbm_flags aliases */
enum {
opt_flags = 0,
opt_cache_size,
opt_sync_mode,
opt_centfree,
opt_coalesce,
opt_dbname,
opt_block_size,
opt_mmap,
opt_mmapsize
};
/* Names of gdbm_flags aliases, for error reporting.
Indexed by opt_ constants above.
*/
char const *opt_names[] = {
"GDBM_File::flags",
"GDBM_File::cache_size",
"GDBM_File::sync_mode",
"GDBM_File::centfree",
"GDBM_File::coalesce",
"GDBM_File::dbname",
"GDBM_File::block_size",
"GDBM_File::mmap",
"GDBM_File::mmapsize"
};
#ifdef GDBM_VERSION_MAJOR
# define GDBM_VERSION_GUESS 0
#else
/* Try educated guess
* The value of GDBM_VERSION_GUESS indicates how rough the guess is:
* 1 - Precise; based on the CVS logs and existing archives
* 2 - Moderate. The major and minor number are correct. The patchlevel
* is set to the upper bound.
* 3 - Rough; The version is guaranteed to be not newer than major.minor.
*/
# if defined(GDBM_SYNCMODE)
/* CHANGES from 1.7.3 to 1.8
* 1. Added GDBM_CENTFREE functionality and option.
*/
# define GDBM_VERSION_MAJOR 1
# define GDBM_VERSION_MINOR 8
# define GDBM_VERSION_PATCH 3
# define GDBM_VERSION_GUESS 1
# elif defined(GDBM_FASTMODE)
/* CHANGES from 1.7.2 to 1.7.3
* 1. Fixed a couple of last minute problems. (Namely, no autoconf.h in
* version.c, and no GDBM_FASTMODE in gdbm.h!)
*/
# define GDBM_VERSION_MAJOR 1
# define GDBM_VERSION_MINOR 7
# define GDBM_VERSION_PATCH 3
# define GDBM_VERSION_GUESS 1
# elif defined(GDBM_FAST)
/* From CVS logs:
* Mon May 17 12:32:02 1993 Phil Nelson (phil at cs.wwu.edu)
*
* * gdbm.proto: Added GDBM_FAST to the read_write flags.
*/
# define GDBM_VERSION_MAJOR 1
# define GDBM_VERSION_MINOR 7
# define GDBM_VERSION_PATCH 2
# define GDBM_VERSION_GUESS 2
# else
# define GDBM_VERSION_MAJOR 1
# define GDBM_VERSION_MINOR 6
# define GDBM_VERSION_GUESS 3
# endif
#endif
#ifndef GDBM_VERSION_PATCH
# define GDBM_VERSION_PATCH 0
#endif
/* The use of fatal_func argument to gdbm_open is deprecated since 1.13 */
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 13
# define FATALFUNC NULL
#elif GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 9
# define FATALFUNC croak_string
# define NEED_FATALFUNC 1
#else
# define FATALFUNC (void (*)()) croak_string
# define NEED_FATALFUNC 1
#endif
#ifdef NEED_FATALFUNC
static void
croak_string(const char *message) {
Perl_croak_nocontext("%s", message);
}
#endif
#define not_here(s) (croak("GDBM_File::%s not implemented", #s),-1)
#if ! (GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 11)
typedef unsigned gdbm_count_t;
#endif
/* GDBM allocates the datum with system malloc() and expects the user
* to free() it. So we either have to free() it immediately, or have
* perl free() it when it deallocates the SV, depending on whether
* perl uses malloc()/free() or not. */
static void
output_datum(pTHX_ SV *arg, char *str, int size)
{
sv_setpvn(arg, str, size);
# undef free
free(str);
}
/* Versions of gdbm prior to 1.7x might not have the gdbm_sync,
gdbm_exists, and gdbm_setopt functions. Apparently Slackware
(Linux) 2.1 contains gdbm-1.5 (which dates back to 1991).
*/
#ifndef GDBM_FAST
#define gdbm_exists(db,key) not_here("gdbm_exists")
#define gdbm_sync(db) (void) not_here("gdbm_sync")
#define gdbm_setopt(db,optflag,optval,optlen) not_here("gdbm_setopt")
#endif
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR < 13
/* Prior to 1.13, only gdbm_fetch set GDBM_ITEM_NOT_FOUND if the requested
key did not exist. Other similar functions would set GDBM_NO_ERROR instead.
The GDBM_ITEM_NOT_FOUND existed as early as in 1.7.3 */
# define ITEM_NOT_FOUND() (gdbm_errno == GDBM_NO_ERROR || gdbm_errno == GDBM_ITEM_NOT_FOUND)
#else
# define ITEM_NOT_FOUND() (gdbm_errno == GDBM_ITEM_NOT_FOUND)
#endif
#define CHECKDB(db) do { \
if (!db->dbp) { \
croak("database was closed"); \
} \
} while (0)
static void
dbcroak(GDBM_File db, char const *func)
{
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 13
if (db)
croak("%s: %s", func, gdbm_db_strerror(db->dbp));
if (gdbm_check_syserr(gdbm_errno))
croak("%s: %s: %s", func, gdbm_strerror(gdbm_errno), strerror(errno));
#else
(void)db;
#endif
croak("%s: %s", func, gdbm_strerror(gdbm_errno));
}
#if GDBM_VERSION_MAJOR == 1 && (GDBM_VERSION_MINOR > 16 || GDBM_VERSION_PATCH >= 90)
# define gdbm_close(db) gdbm_close(db->dbp)
#else
# define gdbm_close(db) (gdbm_close(db->dbp),0)
#endif
static int
gdbm_file_close(GDBM_File db)
{
int rc = 0;
if (db->dbp) {
rc = gdbm_close(db);
db->dbp = NULL;
}
return rc;
}
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 13
/* Error-reporting wrapper for gdbm_recover */
static void
rcvr_errfun(void *cv, char const *fmt, ...)
{
dTHX;
dSP;
va_list ap;
ENTER;
SAVETMPS;
PUSHMARK(SP);
va_start(ap, fmt);
XPUSHs(sv_2mortal(vnewSVpvf(fmt, &ap)));
va_end(ap);
PUTBACK;
call_sv((SV*)cv, G_DISCARD);
FREETMPS;
LEAVE;
}
#endif
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR < 13
static int
gdbm_check_syserr(int ec)
{
switch (ec) {
case GDBM_FILE_OPEN_ERROR:
case GDBM_FILE_WRITE_ERROR:
case GDBM_FILE_SEEK_ERROR:
case GDBM_FILE_READ_ERROR:
return 1;
default:
return 0;
}
}
#endif
static I32
get_gdbm_errno(pTHX_ IV idx, SV *sv)
{
PERL_UNUSED_ARG(idx);
sv_setiv(sv, gdbm_errno);
sv_setpv(sv, gdbm_strerror(gdbm_errno));
if (gdbm_check_syserr(gdbm_errno)) {
SV *val = get_sv("!", 0);
if (val) {
sv_catpv(sv, ": ");
sv_catsv(sv, val);
}
}
SvIOK_on(sv);
return 0;
}
static I32
set_gdbm_errno(pTHX_ IV idx, SV *sv)
{
PERL_UNUSED_ARG(idx);
gdbm_errno = SvIV(sv);
return 0;
}
#include "const-c.inc"
MODULE = GDBM_File PACKAGE = GDBM_File PREFIX = gdbm_
INCLUDE: const-xs.inc
BOOT:
{
SV *sv = get_sv("GDBM_File::gdbm_errno", GV_ADD);
struct ufuncs uf;
uf.uf_val = get_gdbm_errno;
uf.uf_set = set_gdbm_errno;
uf.uf_index = 0;
sv_magic(sv, NULL, PERL_MAGIC_uvar, (char*)&uf, sizeof(uf));
}
void
gdbm_GDBM_version(package)
PPCODE:
I32 gimme = GIMME_V;
if (gimme == G_VOID) {
/* nothing */;
} else if (gimme == G_SCALAR) {
static char const *guess[] = {
"",
" (exact guess)",
" (approximate)",
" (rough guess)"
};
if (GDBM_VERSION_PATCH > 0) {
XPUSHs(sv_2mortal(newSVpvf("%d.%d.%d%s",
GDBM_VERSION_MAJOR,
GDBM_VERSION_MINOR,
GDBM_VERSION_PATCH,
guess[GDBM_VERSION_GUESS])));
} else {
XPUSHs(sv_2mortal(newSVpvf("%d.%d%s",
GDBM_VERSION_MAJOR,
GDBM_VERSION_MINOR,
guess[GDBM_VERSION_GUESS])));
}
} else {
XPUSHs(sv_2mortal(newSVuv(GDBM_VERSION_MAJOR)));
XPUSHs(sv_2mortal(newSVuv(GDBM_VERSION_MINOR)));
XPUSHs(sv_2mortal(newSVuv(GDBM_VERSION_PATCH)));
if (GDBM_VERSION_GUESS > 0) {
XPUSHs(sv_2mortal(newSVuv(GDBM_VERSION_GUESS)));
}
}
GDBM_File
gdbm_TIEHASH(dbtype, name, read_write, mode)
char * dbtype
char * name
int read_write
int mode
PREINIT:
GDBM_FILE dbp;
CODE:
dbp = gdbm_open(name, 0, read_write, mode, FATALFUNC);
if (!dbp && gdbm_errno == GDBM_BLOCK_SIZE_ERROR) {
/*
* By specifying a block size of 0 above, we asked gdbm to
* default to the filesystem's block size. That's usually the
* right size to choose. But some versions of gdbm require
* a power-of-two block size, and some unusual filesystems
* or devices have a non-power-of-two size that cause this
* defaulting to fail. In that case, force an acceptable
* block size.
*/
dbp = gdbm_open(name, 4096, read_write, mode, FATALFUNC);
}
if (dbp) {
RETVAL = (GDBM_File)safecalloc(1, sizeof(GDBM_File_type));
RETVAL->dbp = dbp;
} else {
RETVAL = NULL;
}
OUTPUT:
RETVAL
void
gdbm_DESTROY(db)
GDBM_File db
PREINIT:
int i = store_value;
CODE:
if (gdbm_file_close(db)) {
croak("gdbm_close: %s; %s", gdbm_strerror(gdbm_errno),
strerror(errno));
}
do {
if (db->filter[i])
SvREFCNT_dec(db->filter[i]);
} while (i-- > 0);
safefree(db);
void
gdbm_UNTIE(db, count)
GDBM_File db
unsigned count
CODE:
if (count == 0) {
if (gdbm_file_close(db))
croak("gdbm_close: %s; %s",
gdbm_strerror(gdbm_errno),
strerror(errno));
}
#define gdbm_FETCH(db,key) gdbm_fetch(db->dbp,key)
datum_value
gdbm_FETCH(db, key)
GDBM_File db
datum_key_copy key
INIT:
CHECKDB(db);
CLEANUP:
if (RETVAL.dptr == NULL && !ITEM_NOT_FOUND()) {
dbcroak(db, "gdbm_fetch");
}
#define gdbm_STORE(db,key,value,flags) gdbm_store(db->dbp,key,value,flags)
int
gdbm_STORE(db, key, value, flags = GDBM_REPLACE)
GDBM_File db
datum_key key
datum_value value
int flags
INIT:
CHECKDB(db);
CLEANUP:
if (RETVAL) {
dbcroak(db, "gdbm_store");
}
#define gdbm_DELETE(db,key) gdbm_delete(db->dbp,key)
int
gdbm_DELETE(db, key)
GDBM_File db
datum_key key
INIT:
CHECKDB(db);
CLEANUP:
if (RETVAL && !ITEM_NOT_FOUND()) {
dbcroak(db, "gdbm_delete");
}
#define gdbm_FIRSTKEY(db) gdbm_firstkey(db->dbp)
datum_key
gdbm_FIRSTKEY(db)
GDBM_File db
INIT:
CHECKDB(db);
CLEANUP:
if (RETVAL.dptr == NULL && !ITEM_NOT_FOUND()) {
dbcroak(db, "gdbm_firstkey");
}
#define gdbm_NEXTKEY(db,key) gdbm_nextkey(db->dbp,key)
datum_key
gdbm_NEXTKEY(db, key)
GDBM_File db
datum_key key
INIT:
CHECKDB(db);
CLEANUP:
if (RETVAL.dptr == NULL && !ITEM_NOT_FOUND()) {
dbcroak(db, "gdbm_nextkey");
}
#define gdbm_EXISTS(db,key) gdbm_exists(db->dbp,key)
int
gdbm_EXISTS(db, key)
GDBM_File db
datum_key key
INIT:
CHECKDB(db);
##
int
gdbm_close(db)
GDBM_File db
INIT:
CHECKDB(db);
CODE:
RETVAL = gdbm_file_close(db);
OUTPUT:
RETVAL
#define gdbm_gdbm_check_syserr(ec) gdbm_check_syserr(ec)
int
gdbm_gdbm_check_syserr(ec)
int ec
SV *
gdbm_errno(db)
GDBM_File db
INIT:
CHECKDB(db);
CODE:
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 13
{
int ec = gdbm_last_errno(db->dbp);
RETVAL = newSViv(ec);
sv_setpv(RETVAL, gdbm_db_strerror (db->dbp));
SvIOK_on(RETVAL);
}
#else
RETVAL = newSVsv(get_sv("GDBM_File::gdbm_errno", 0));
#endif
OUTPUT:
RETVAL
int
gdbm_syserrno(db)
GDBM_File db
INIT:
CHECKDB(db);
CODE:
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 13
{
int ec = gdbm_last_errno(db->dbp);
if (gdbm_check_syserr(ec)) {
RETVAL = gdbm_last_syserr(db->dbp);
} else {
RETVAL = 0;
}
}
#else
RETVAL = not_here("syserrno");
#endif
OUTPUT:
RETVAL
SV *
gdbm_strerror(db)
GDBM_File db
PREINIT:
char const *errstr;
INIT:
CHECKDB(db);
CODE:
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 13
errstr = gdbm_db_strerror(db->dbp);
#else
errstr = gdbm_strerror(gdbm_errno);
#endif
RETVAL = newSVpv(errstr, 0);
OUTPUT:
RETVAL
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 13
# define gdbm_clear_error(db) gdbm_clear_error(db->dbp)
#else
# define gdbm_clear_error(db) (gdbm_errno = 0)
#endif
void
gdbm_clear_error(db)
GDBM_File db
INIT:
CHECKDB(db);
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 13
# define gdbm_needs_recovery(db) gdbm_needs_recovery(db->dbp)
#else
# define gdbm_needs_recovery(db) not_here("gdbm_needs_recovery")
#endif
int
gdbm_needs_recovery(db)
GDBM_File db
INIT:
CHECKDB(db);
#define gdbm_reorganize(db) gdbm_reorganize(db->dbp)
int
gdbm_reorganize(db)
GDBM_File db
INIT:
CHECKDB(db);
# Arguments:
# err => sub { ... }
# max_failed_keys => $n
# max_failed_buckets => $n
# max_failures => $n
# backup => \$str
# stat => \%hash
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 13
void
gdbm_recover(db, ...)
GDBM_File db
PREINIT:
int flags = GDBM_RCVR_FORCE;
SV *backup_ref = &PL_sv_undef;
SV *stat_ref = &PL_sv_undef;
gdbm_recovery rcvr;
INIT:
CHECKDB(db);
CODE:
if (items > 1) {
int i;
if ((items % 2) == 0) {
croak_xs_usage(cv, "db, %opts");
}
for (i = 1; i < items; i += 2) {
char *kw;
SV *sv = ST(i);
SV *val = ST(i+1);
kw = SvPV_nolen(sv);
if (strcmp(kw, "err") == 0) {
SvGETMAGIC(val);
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) {
rcvr.data = SvRV(val);
} else {
croak("%s must be a code ref", kw);
}
rcvr.errfun = rcvr_errfun;
flags |= GDBM_RCVR_ERRFUN;
} else if (strcmp(kw, "max_failed_keys") == 0) {
rcvr.max_failed_keys = SvUV(val);
flags |= GDBM_RCVR_MAX_FAILED_KEYS;
} else if (strcmp(kw, "max_failed_buckets") == 0) {
rcvr.max_failed_buckets = SvUV(val);
flags |= GDBM_RCVR_MAX_FAILED_BUCKETS;
} else if (strcmp(kw, "max_failures") == 0) {
rcvr.max_failures = SvUV(val);
flags |= GDBM_RCVR_MAX_FAILURES;
} else if (strcmp(kw, "backup") == 0) {
SvGETMAGIC(val);
if (SvROK(val) && SvTYPE(SvRV(val)) < SVt_PVAV) {
backup_ref = val;
} else {
croak("%s must be a scalar reference", kw);
}
flags |= GDBM_RCVR_BACKUP;
} else if (strcmp(kw, "stat") == 0) {
SvGETMAGIC(val);
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVHV) {
stat_ref = val;
} else {
croak("%s must be a scalar reference", kw);
}
} else {
croak("%s: unrecognized argument", kw);
}
}
}
if (gdbm_recover(db->dbp, &rcvr, flags)) {
dbcroak(db, "gdbm_recover");
}
if (stat_ref != &PL_sv_undef) {
HV *hv = (HV*)SvRV(stat_ref);
#define STAT_RECOVERED_KEYS_STR "recovered_keys"
#define STAT_RECOVERED_KEYS_LEN (sizeof(STAT_RECOVERED_KEYS_STR)-1)
#define STAT_RECOVERED_BUCKETS_STR "recovered_buckets"
#define STAT_RECOVERED_BUCKETS_LEN (sizeof(STAT_RECOVERED_BUCKETS_STR)-1)
#define STAT_FAILED_KEYS_STR "failed_keys"
#define STAT_FAILED_KEYS_LEN (sizeof(STAT_FAILED_KEYS_STR)-1)
#define STAT_FAILED_BUCKETS_STR "failed_buckets"
#define STAT_FAILED_BUCKETS_LEN (sizeof(STAT_FAILED_BUCKETS_STR)-1)
hv_store(hv, STAT_RECOVERED_KEYS_STR, STAT_RECOVERED_KEYS_LEN,
newSVuv(rcvr.recovered_keys), 0);
hv_store(hv,
STAT_RECOVERED_BUCKETS_STR,
STAT_RECOVERED_BUCKETS_LEN,
newSVuv(rcvr.recovered_buckets), 0);
hv_store(hv,
STAT_FAILED_KEYS_STR,
STAT_FAILED_KEYS_LEN,
newSVuv(rcvr.failed_keys), 0);
hv_store(hv,
STAT_FAILED_BUCKETS_STR,
STAT_FAILED_BUCKETS_LEN,
newSVuv(rcvr.failed_buckets), 0);
}
if (backup_ref != &PL_sv_undef) {
SV *sv = SvRV(backup_ref);
sv_setpv(sv, rcvr.backup_name);
free(rcvr.backup_name);
}
#endif
#if GDBM_VERSION_MAJOR == 1 && (GDBM_VERSION_MINOR > 16 || GDBM_VERSION_PATCH >= 90)
# define gdbm_sync(db) gdbm_sync(db->dbp)
#else
# define gdbm_sync(db) (gdbm_sync(db->dbp),0)
#endif
int
gdbm_sync(db)
GDBM_File db
INIT:
CHECKDB(db);
CLEANUP:
if (RETVAL) {
dbcroak(db, "gdbm_sync");
}
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 11
gdbm_count_t
gdbm_count(db)
GDBM_File db
PREINIT:
gdbm_count_t c;
INIT:
CHECKDB(db);
CODE:
if (gdbm_count(db->dbp, &c)) {
dbcroak(db, "gdbm_count");
}
RETVAL = c;
OUTPUT:
RETVAL
void
gdbm_dump(db, filename, ...)
GDBM_File db
char * filename
PREINIT:
int format = GDBM_DUMP_FMT_ASCII;
int flags = GDBM_WRCREAT;
int mode = 0666;
INIT:
CHECKDB(db);
CODE:
if (items % 2) {
croak_xs_usage(cv, "db, filename, %opts");
} else {
int i;
for (i = 2; i < items; i += 2) {
char *kw;
SV *sv = ST(i);
SV *val = ST(i+1);
kw = SvPV_nolen(sv);
if (strcmp(kw, "mode") == 0) {
mode = SvUV(val) & 0777;
} else if (strcmp(kw, "binary") == 0) {
if (SvTRUE(val)) {
format = GDBM_DUMP_FMT_BINARY;
}
} else if (strcmp(kw, "overwrite") == 0) {
if (SvTRUE(val)) {
flags = GDBM_NEWDB;
}
} else {
croak("unrecognized keyword: %s", kw);
}
}
if (gdbm_dump(db->dbp, filename, format, flags, mode)) {
dbcroak(NULL, "dump");
}
}
void
gdbm_load(db, filename, ...)
GDBM_File db
char * filename
PREINIT:
int flag = GDBM_INSERT;
int meta_mask = 0;
unsigned long errline;
int result;
int strict_errors = 0;
INIT:
CHECKDB(db);
CODE:
if (items % 2) {
croak_xs_usage(cv, "db, filename, %opts");
} else {
int i;
for (i = 2; i < items; i += 2) {
char *kw;
SV *sv = ST(i);
SV *val = ST(i+1);
kw = SvPV_nolen(sv);
if (strcmp(kw, "restore_mode") == 0) {
if (!SvTRUE(val))
meta_mask |= GDBM_META_MASK_MODE;
} else if (strcmp(kw, "restore_owner") == 0) {
if (!SvTRUE(val))
meta_mask |= GDBM_META_MASK_OWNER;
} else if (strcmp(kw, "replace") == 0) {
if (SvTRUE(val))
flag = GDBM_REPLACE;
} else if (strcmp(kw, "strict_errors") == 0) {
strict_errors = SvTRUE(val);
} else {
croak("unrecognized keyword: %s", kw);
}
}
}
result = gdbm_load(&db->dbp, filename, flag, meta_mask, &errline);
if (result == -1 || (result == 1 && strict_errors)) {
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 13
if (errline) {
croak("%s:%lu: database load error: %s",
filename, errline, gdbm_db_strerror(db->dbp));
} else {
croak("%s: database load error: %s",
filename, gdbm_db_strerror(db->dbp));
}
#else
if (errline) {
croak("%s:%lu: database load error: %s",
filename, errline, gdbm_strerror(gdbm_errno));
} else {
croak("%s: database load error: %s",
filename, gdbm_strerror(gdbm_errno));
}
#endif
}
#endif
#define OPTNAME(a,b) a ## b
#define INTOPTSETUP(opt) \
do { \
if (items == 1) { \
opcode = OPTNAME(GDBM_GET, opt); \
} else { \
opcode = OPTNAME(GDBM_SET, opt); \
c_iv = SvIV(ST(1)); \
} \
} while (0)
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 9
# define OPTVALPTR void *
#else
# define OPTVALPTR int *
#endif
# GDBM_GET defines appeared in version 1.9 (2011-08-12).
#
# Provide definitions for earlier versions. These will cause gdbm_setopt
# to fail with GDBM_OPT_ILLEGAL
#ifndef GDBM_GETFLAGS
# define GDBM_GETFLAGS -1
#endif
#ifndef GDBM_GETMMAP
# define GDBM_GETMMAP -1
#endif
#ifndef GDBM_GETCACHESIZE
# define GDBM_GETCACHESIZE -1
#endif
#ifndef GDBM_GETSYNCMODE
# define GDBM_GETSYNCMODE -1
#endif
#ifndef GDBM_GETCENTFREE
# define GDBM_GETCENTFREE -1
#endif
#ifndef GDBM_GETCOALESCEBLKS
# define GDBM_GETCOALESCEBLKS -1
#endif
#ifndef GDBM_GETMAXMAPSIZE
# define GDBM_GETMAXMAPSIZE -1
#endif
#ifndef GDBM_GETDBNAME
# define GDBM_GETDBNAME -1
#endif
#ifndef GDBM_GETBLOCKSIZE
# define GDBM_GETBLOCKSIZE -1
#endif
# These two appeared in version 1.10:
#ifndef GDBM_SETMAXMAPSIZE
# define GDBM_SETMAXMAPSIZE -1
#endif
#ifndef GDBM_SETMMAP
# define GDBM_SETMMAP -1
#endif
# These GDBM_SET defines appeared in 1.10, replacing obsolete opcodes.
# Provide definitions for older versions
#ifndef GDBM_SETCACHESIZE
# define GDBM_SETCACHESIZE GDBM_CACHESIZE
#endif
#ifndef GDBM_SETSYNCMODE
# define GDBM_SETSYNCMODE GDBM_SYNCMODE
#endif
#ifndef GDBM_SETCENTFREE
# define GDBM_SETCENTFREE GDBM_CENTFREE
#endif
#ifndef GDBM_SETCOALESCEBLKS
# define GDBM_SETCOALESCEBLKS GDBM_COALESCEBLKS
#endif
SV *
gdbm_flags(db, ...)
GDBM_File db
SV * RETVAL = &PL_sv_undef;
ALIAS:
GDBM_File::cache_size = opt_cache_size
GDBM_File::sync_mode = opt_sync_mode
GDBM_File::centfree = opt_centfree
GDBM_File::coalesce = opt_coalesce
GDBM_File::dbname = opt_dbname
GDBM_File::block_size = opt_block_size
GDBM_File::mmap = opt_mmap
GDBM_File::mmapsize = opt_mmapsize
PREINIT:
int opcode = -1;
int c_iv;
size_t c_uv;
char *c_cv;
OPTVALPTR vptr = (OPTVALPTR) &c_iv;
size_t vsiz = sizeof(c_iv);
INIT:
CHECKDB(db);
CODE:
if (items > 2) {
croak("%s: too many arguments", opt_names[ix]);
}
switch (ix) {
case opt_flags:
if (items > 1) {
croak("%s: too many arguments", opt_names[ix]);
}
opcode = GDBM_GETFLAGS;
break;
case opt_cache_size:
INTOPTSETUP(CACHESIZE);
break;
case opt_sync_mode:
INTOPTSETUP(SYNCMODE);
break;
case opt_centfree:
INTOPTSETUP(CENTFREE);
break;
case opt_coalesce:
INTOPTSETUP(COALESCEBLKS);
break;
case opt_dbname:
if (items > 1) {
croak("%s: too many arguments", opt_names[ix]);
}
opcode = GDBM_GETDBNAME;
vptr = (OPTVALPTR) &c_cv;
vsiz = sizeof(c_cv);
break;
case opt_block_size:
if (items > 1) {
croak("%s: too many arguments", opt_names[ix]);
}
opcode = GDBM_GETBLOCKSIZE;
break;
case opt_mmap:
if (items > 1) {
croak("%s: too many arguments", opt_names[ix]);
}
opcode = GDBM_GETMMAP;
break;
case opt_mmapsize:
vptr = (OPTVALPTR) &c_uv;
vsiz = sizeof(c_uv);
if (items == 1) {
opcode = GDBM_GETMAXMAPSIZE;
} else {
opcode = GDBM_SETMAXMAPSIZE;
c_uv = SvUV(ST(1));
}
break;
}
if (gdbm_setopt(db->dbp, opcode, vptr, vsiz)) {
if (gdbm_errno == GDBM_OPT_ILLEGAL)
croak("%s not implemented", opt_names[ix]);
dbcroak(db, "gdbm_setopt");
}
if (vptr == (OPTVALPTR) &c_iv) {
RETVAL = newSViv(c_iv);
} else if (vptr == (OPTVALPTR) &c_uv) {
RETVAL = newSVuv(c_uv);
} else {
RETVAL = newSVpv(c_cv, 0);
free(c_cv);
}
OUTPUT:
RETVAL
#define gdbm_setopt(db,optflag, optval, optlen) gdbm_setopt(db->dbp,optflag, optval, optlen)
int
gdbm_setopt (db, optflag, optval, optlen)
GDBM_File db
int optflag
int &optval
int optlen
INIT:
CHECKDB(db);
CLEANUP:
if (RETVAL) {
dbcroak(db, "gdbm_setopt");
}
SV *
filter_fetch_key(db, code)
GDBM_File db
SV * code
SV * RETVAL = &PL_sv_undef ;
ALIAS:
GDBM_File::filter_fetch_key = fetch_key
GDBM_File::filter_store_key = store_key
GDBM_File::filter_fetch_value = fetch_value
GDBM_File::filter_store_value = store_value
CODE:
DBM_setFilter(db->filter[ix], code);
#
# Export/Import API
#
#
# Crash tolerance API
#
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 21
#define gdbm_convert(db, flag) gdbm_convert(db->dbp, flag)
int
gdbm_convert(db, flag)
GDBM_File db
int flag
INIT:
CHECKDB(db);
CLEANUP:
if (RETVAL) {
dbcroak(db, "gdbm_convert");
}
#define gdbm_failure_atomic(db, even, odd) gdbm_failure_atomic(db->dbp, even, odd)
int
gdbm_failure_atomic(db, even, odd)
GDBM_File db
char * even
char * odd
INIT:
CHECKDB(db);
CLEANUP:
if (RETVAL) {
dbcroak(db, "gdbm_failure_atomic");
}
void
gdbm_latest_snapshot(package, even, odd)
char * even
char * odd
INIT:
int result;
int syserr;
const char * filename;
PPCODE:
result = gdbm_latest_snapshot(even, odd, &filename);
syserr = errno;
if (result == GDBM_SNAPSHOT_OK) {
XPUSHs(sv_2mortal(newSVpv(filename, 0)));
} else {
XPUSHs(&PL_sv_undef);
}
if (GIMME_V == G_ARRAY) {
XPUSHs(sv_2mortal(newSVuv(result)));
if (result == GDBM_SNAPSHOT_ERR)
XPUSHs(sv_2mortal(newSVuv(syserr)));
}
#endif
int
gdbm_crash_tolerance_status(package)
CODE:
#if GDBM_VERSION_MAJOR == 1 && GDBM_VERSION_MINOR >= 21
/*
* The call below returns GDBM_SNAPSHOT_ERR and sets errno to
* EINVAL, if crash tolerance is implemented, or ENOSYS, if it
* is not.
*/
gdbm_latest_snapshot(NULL, NULL, NULL);
RETVAL = (errno != ENOSYS);
#else
RETVAL = 0;
#endif
OUTPUT:
RETVAL
|