summaryrefslogtreecommitdiffstats
path: root/upstream/debian-bookworm/man3/GDBM_File.3perl
blob: f539cd88b725672a8d133bd5266a6e359e10d33e (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
.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  \*(C+ will
.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
.    ds C`
.    ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
.    if \nF \{\
.        de IX
.        tm Index:\\$1\t\\n%\t"\\$2"
..
.        if !\nF==2 \{\
.            nr % 0
.            nr F 2
.        \}
.    \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "GDBM_File 3perl"
.TH GDBM_File 3perl "2023-11-25" "perl v5.36.0" "Perl Programmers Reference Guide"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
GDBM_File \- Perl5 access to the gdbm library.
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 4
\&    use GDBM_File;
\&    [$db =] tie %hash, \*(AqGDBM_File\*(Aq, $filename, GDBM_WRCREAT, 0640
\&                or die "$GDBM_File::gdbm_errno";
\&    # Use the %hash...
\&
\&    $e = $db\->errno;
\&    $e = $db\->syserrno;
\&    $str = $db\->strerror;
\&    $bool = $db\->needs_recovery;
\&
\&    $db\->clear_error;
\&
\&    $db\->reorganize;
\&    $db\->sync;
\&
\&    $n = $db\->count;
\&
\&    $n = $db\->flags;
\&
\&    $str = $db\->dbname;
\&
\&    $db\->cache_size;
\&    $db\->cache_size($newsize);
\&
\&    $n = $db\->block_size;
\&
\&    $bool = $db\->sync_mode;
\&    $db\->sync_mode($bool);
\&
\&    $bool = $db\->centfree;
\&    $db\->centfree($bool);
\&
\&    $bool = $db\->coalesce;
\&    $db\->coalesce($bool);
\&
\&    $bool = $db\->mmap;
\&
\&    $size = $db\->mmapsize;
\&    $db\->mmapsize($newsize);
\&
\&    $db\->recover(%args);
\&
\&    untie %hash ;
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
\&\fBGDBM_File\fR is a module which allows Perl programs to make use of the
facilities provided by the \s-1GNU\s0 gdbm library.  If you intend to use this
module you should really have a copy of the \fB\s-1GDBM\s0 manual\fR at hand.
The manual is avaialble online at
<https://www.gnu.org.ua/software/gdbm/manual>.
.PP
Most of the \fBgdbm\fR functions are available through the \fBGDBM_File\fR
interface.
.PP
Unlike Perl's built-in hashes, it is not safe to \f(CW\*(C`delete\*(C'\fR the current
item from a GDBM_File tied hash while iterating over it with \f(CW\*(C`each\*(C'\fR.
This is a limitation of the gdbm library.
.SS "Tie"
.IX Subsection "Tie"
Use the Perl built-in \fBtie\fR to associate a \fB\s-1GDBM\s0\fR database with a Perl
hash:
.PP
.Vb 1
\&   tie %hash, \*(AqGDBM_File\*(Aq, $filename, $flags, $mode;
.Ve
.PP
Here, \fI\f(CI$filename\fI\fR is the name of the database file to open or create.
\&\fI\f(CI$flags\fI\fR is a bitwise \s-1OR\s0 of \fIaccess mode\fR and optional \fImodifiers\fR.
Access mode is one of:
.IP "\fB\s-1GDBM_READER\s0\fR" 4
.IX Item "GDBM_READER"
Open existing database file in read-only mode.
.IP "\fB\s-1GDBM_WRITER\s0\fR" 4
.IX Item "GDBM_WRITER"
Open existing database file in read-write mode.
.IP "\fB\s-1GDBM_WRCREAT\s0\fR" 4
.IX Item "GDBM_WRCREAT"
If the database file exists, open it in read-write mode.  If it doesn't,
create it first and open read-write.
.IP "\fB\s-1GDBM_NEWDB\s0\fR" 4
.IX Item "GDBM_NEWDB"
Create new database and open it read-write.  If the database already exists,
truncate it first.
.PP
A number of modifiers can be \s-1OR\s0'd to the access mode.  Most of them are
rarely needed (see <https://www.gnu.org.ua/software/gdbm/manual/Open.html>
for a complete list), but one is worth mentioning.  The \fB\s-1GDBM_NUMSYNC\s0\fR
modifier, when used with \fB\s-1GDBM_NEWDB\s0\fR, instructs \fB\s-1GDBM\s0\fR to create the
database in \fIextended\fR (so called \fInumsync\fR) format.  This format is
best suited for crash-tolerant implementations.  See \fB\s-1CRASH TOLERANCE\s0\fR
below for more information.
.PP
The \fI\f(CI$mode\fI\fR parameter is the file mode for creating new database
file.  Use an octal constant or a combination of \f(CW\*(C`S_I*\*(C'\fR constants
from the \fBFcntl\fR module.  This parameter is used if \fI\f(CI$flags\fI\fR is
\&\fB\s-1GDBM_NEWDB\s0\fR or \fB\s-1GDBM_WRCREAT\s0\fR.
.PP
On success, \fBtie\fR returns an object of class \fBGDBM_File\fR.  On failure,
it returns \fBundef\fR.  It is recommended to always check the return value,
to make sure your hash is successfully associated with the database file.
See \fB\s-1ERROR HANDLING\s0\fR below for examples.
.SH "STATIC METHODS"
.IX Header "STATIC METHODS"
.SS "GDBM_version"
.IX Subsection "GDBM_version"
.Vb 2
\&    $str = GDBM_File\->GDBM_version;
\&    @ar = GDBM_File\->GDBM_version;
.Ve
.PP
Returns the version number of the underlying \fBlibgdbm\fR library. In scalar
context, returns the library version formatted as string:
.PP
.Vb 1
\&    MINOR.MAJOR[.PATCH][ (GUESS)]
.Ve
.PP
where \fI\s-1MINOR\s0\fR, \fI\s-1MAJOR\s0\fR, and \fI\s-1PATCH\s0\fR are version numbers, and \fI\s-1GUESS\s0\fR is
a guess level (see below).
.PP
In list context, returns a list:
.PP
.Vb 1
\&    ( MINOR, MAJOR, PATCH [, GUESS] )
.Ve
.PP
The \fI\s-1GUESS\s0\fR component is present only if \fBlibgdbm\fR version is 1.8.3 or
earlier. This is because earlier releases of \fBlibgdbm\fR did not include
information about their version and the \fBGDBM_File\fR module has to implement
certain guesswork in order to determine it. \fI\s-1GUESS\s0\fR is a textual description
in string context, and a positive number indicating how rough the guess is
in list context. Possible values are:
.IP "1  \- exact guess" 4
.IX Item "1 - exact guess"
The major and minor version numbers are guaranteed to be correct. The actual
patchlevel is most probably guessed right, but can be 1\-2 less than indicated.
.IP "2  \- approximate" 4
.IX Item "2 - approximate"
The major and minor number are guaranteed to be correct. The patchlevel is
set to the upper bound.
.IP "3  \- rough guess" 4
.IX Item "3 - rough guess"
The version is guaranteed to be not newer than \fB\f(BI\s-1MAJOR\s0\fB.\f(BI\s-1MINOR\s0\fB\fR.
.SH "ERROR HANDLING"
.IX Header "ERROR HANDLING"
.ie n .SS "$GDBM_File::gdbm_errno"
.el .SS "\f(CW$GDBM_File::gdbm_errno\fP"
.IX Subsection "$GDBM_File::gdbm_errno"
When referenced in numeric context, retrieves the current value of the
\&\fBgdbm_errno\fR variable, i.e. a numeric code describing the state of the
most recent operation on any \fBgdbm\fR database.  Each numeric code has a
symbolic name associated with it.   For a comprehensive list  of these, see
<https://www.gnu.org.ua/software/gdbm/manual/Error\-codes.html>.  Notice,
that this list includes all error codes defined for the most recent
version of \fBgdbm\fR.  Depending on the actual version of the library
\&\fBGDBM_File\fR is built with, some of these may be missing.
.PP
In string context, \fB\f(CB$gdbm_errno\fB\fR returns a human-readable description of
the error.  If necessary, this description includes the value of \fB$!\fR.
This makes it possible to use it in diagnostic messages.  For example,
the usual tying sequence is
.PP
.Vb 2
\&    tie %hash, \*(AqGDBM_File\*(Aq, $filename, GDBM_WRCREAT, 0640
\&         or die "$GDBM_File::gdbm_errno";
.Ve
.PP
The following, more complex, example illustrates how you can fall back
to read-only mode if the database file permissions forbid read-write
access:
.PP
.Vb 11
\&    use Errno qw(EACCES);
\&    unless (tie(%hash, \*(AqGDBM_File\*(Aq, $filename, GDBM_WRCREAT, 0640)) {
\&        if ($GDBM_File::gdbm_errno == GDBM_FILE_OPEN_ERROR
\&            && $!{EACCES}) {
\&            if (tie(%hash, \*(AqGDBM_File\*(Aq, $filename, GDBM_READER, 0640)) {
\&                die "$GDBM_File::gdbm_errno";
\&            }
\&        } else {
\&            die "$GDBM_File::gdbm_errno";
\&        }
\&    }
.Ve
.SS "gdbm_check_syserr"
.IX Subsection "gdbm_check_syserr"
.Vb 1
\&    if (gdbm_check_syserr(gdbm_errno)) ...
.Ve
.PP
Returns true if the system error number (\fB$!\fR) gives more information on
the cause of the error.
.SH "DATABASE METHODS"
.IX Header "DATABASE METHODS"
.SS "close"
.IX Subsection "close"
.Vb 1
\&    $db\->close;
.Ve
.PP
Closes the database.  Normally you would just do \fBuntie\fR.  However, you
will need to use this function if you have explicitly assigned the result
of \fBtie\fR to a variable, and wish to release the database to another
users.  Consider the following code:
.PP
.Vb 4
\&    $db = tie %hash, \*(AqGDBM_File\*(Aq, $filename, GDBM_WRCREAT, 0640;
\&    # Do something with %hash or $db...
\&    untie %hash;
\&    $db\->close;
.Ve
.PP
In this example, doing \fBuntie\fR alone is not enough, since the database
would remain referenced by \fB\f(CB$db\fB\fR, and, as a consequence, the database file
would remain locked.  Calling \fB\f(CB$db\fB\->close\fR ensures the database file is
closed and unlocked.
.SS "errno"
.IX Subsection "errno"
.Vb 1
\&    $db\->errno
.Ve
.PP
Returns the last error status associated with this database.  In string
context, returns a human-readable description of the error.  See also
\&\fB\f(CB$GDBM_File::gdbm_errno\fB\fR variable above.
.SS "syserrno"
.IX Subsection "syserrno"
.Vb 1
\&    $db\->syserrno
.Ve
.PP
Returns the last system error status (C \f(CW\*(C`errno\*(C'\fR variable), associated with
this database,
.SS "strerror"
.IX Subsection "strerror"
.Vb 1
\&    $db\->strerror
.Ve
.PP
Returns textual description of the last error that occurred in this database.
.SS "clear_error"
.IX Subsection "clear_error"
.Vb 1
\&    $db\->clear_error
.Ve
.PP
Clear error status.
.SS "needs_recovery"
.IX Subsection "needs_recovery"
.Vb 1
\&    $db\->needs_recovery
.Ve
.PP
Returns true if the database needs recovery.
.SS "reorganize"
.IX Subsection "reorganize"
.Vb 1
\&    $db\->reorganize;
.Ve
.PP
Reorganizes the database.
.SS "sync"
.IX Subsection "sync"
.Vb 1
\&    $db\->sync;
.Ve
.PP
Synchronizes recent changes to the database with its disk copy.
.SS "count"
.IX Subsection "count"
.Vb 1
\&    $n = $db\->count;
.Ve
.PP
Returns number of keys in the database.
.SS "flags"
.IX Subsection "flags"
.Vb 1
\&    $db\->flags;
.Ve
.PP
Returns flags passed as 4th argument to \fBtie\fR.
.SS "dbname"
.IX Subsection "dbname"
.Vb 1
\&    $db\->dbname;
.Ve
.PP
Returns the database name (i.e. 3rd argument to \fBtie\fR.
.SS "cache_size"
.IX Subsection "cache_size"
.Vb 2
\&    $db\->cache_size;
\&    $db\->cache_size($newsize);
.Ve
.PP
Returns the size of the internal \fB\s-1GDBM\s0\fR cache for that database.
.PP
Called with argument, sets the size to \fI\f(CI$newsize\fI\fR.
.SS "block_size"
.IX Subsection "block_size"
.Vb 1
\&    $db\->block_size;
.Ve
.PP
Returns the block size of the database.
.SS "sync_mode"
.IX Subsection "sync_mode"
.Vb 2
\&    $db\->sync_mode;
\&    $db\->sync_mode($bool);
.Ve
.PP
Returns the status of the automatic synchronization mode. Called with argument,
enables or disables the sync mode, depending on whether \f(CW$bool\fR is \fBtrue\fR or
\&\fBfalse\fR.
.PP
When synchronization mode is on (\fBtrue\fR), any changes to the database are
immediately written to the disk. This ensures database consistency in case
of any unforeseen errors (e.g. power failures), at the expense of considerable
slowdown of operation.
.PP
Synchronization mode is off by default.
.SS "centfree"
.IX Subsection "centfree"
.Vb 2
\&    $db\->centfree;
\&    $db\->centfree($bool);
.Ve
.PP
Returns status of the central free block pool (\fB0\fR \- disabled,
\&\fB1\fR \- enabled).
.PP
With argument, changes its status.
.PP
By default, central free block pool is disabled.
.SS "coalesce"
.IX Subsection "coalesce"
.Vb 2
\&    $db\->coalesce;
\&    $db\->coalesce($bool);
.Ve
.SS "mmap"
.IX Subsection "mmap"
.Vb 1
\&    $db\->mmap;
.Ve
.PP
Returns true if memory mapping is enabled.
.PP
This method will \fBcroak\fR if the \fBlibgdbm\fR library is complied without
memory mapping support.
.SS "mmapsize"
.IX Subsection "mmapsize"
.Vb 2
\&    $db\->mmapsize;
\&    $db\->mmapsize($newsize);
.Ve
.PP
If memory mapping is enabled, returns the size of memory mapping. With
argument, sets the size to \fB\f(CB$newsize\fB\fR.
.PP
This method will \fBcroak\fR if the \fBlibgdbm\fR library is complied without
memory mapping support.
.SS "recover"
.IX Subsection "recover"
.Vb 1
\&    $db\->recover(%args);
.Ve
.PP
Recovers data from a failed database. \fB\f(CB%args\fB\fR is optional and can contain
following keys:
.IP "err => sub { ... }" 4
.IX Item "err => sub { ... }"
Reference to code for detailed error reporting. Upon encountering an error,
\&\fBrecover\fR will call this sub with a single argument \- a description of the
error.
.IP "backup => \e$str" 4
.IX Item "backup => $str"
Creates a backup copy of the database before recovery and returns its
filename in \fB\f(CB$str\fB\fR.
.ie n .IP "max_failed_keys => $n" 4
.el .IP "max_failed_keys => \f(CW$n\fR" 4
.IX Item "max_failed_keys => $n"
Maximum allowed number of failed keys. If the actual number becomes equal
to \fI\f(CI$n\fI\fR, \fBrecover\fR aborts and returns error.
.ie n .IP "max_failed_buckets => $n" 4
.el .IP "max_failed_buckets => \f(CW$n\fR" 4
.IX Item "max_failed_buckets => $n"
Maximum allowed number of failed buckets. If the actual number becomes equal
to \fI\f(CI$n\fI\fR, \fBrecover\fR aborts and returns error.
.ie n .IP "max_failures => $n" 4
.el .IP "max_failures => \f(CW$n\fR" 4
.IX Item "max_failures => $n"
Maximum allowed number of failures during recovery.
.IP "stat => \e%hash" 4
.IX Item "stat => %hash"
Return recovery statistics in \fI\f(CI%hash\fI\fR. Upon return, the following keys will
be present:
.RS 4
.IP "recovered_keys" 8
.IX Item "recovered_keys"
Number of successfully recovered keys.
.IP "recovered_buckets" 8
.IX Item "recovered_buckets"
Number of successfully recovered buckets.
.IP "failed_keys" 8
.IX Item "failed_keys"
Number of keys that failed to be retrieved.
.IP "failed_buckets" 8
.IX Item "failed_buckets"
Number of buckets that failed to be retrieved.
.RE
.RS 4
.RE
.SS "convert"
.IX Subsection "convert"
.Vb 1
\&    $db\->convert($format);
.Ve
.PP
Changes the format of the database file referred to by \fB\f(CB$db\fB\fR.
.PP
Starting from version 1.20, \fBgdbm\fR supports two database file formats:
\&\fIstandard\fR and \fIextended\fR.  The former is the traditional database
format, used by previous \fBgdbm\fR versions.  The \fIextended\fR format contains
additional data and is recommended for use in crash tolerant applications.
.PP
<https://www.gnu.org.ua/software/gdbm/manual/Numsync.html>, for the
discussion of both formats.
.PP
The \fB\f(CB$format\fB\fR argument sets the new desired database format.  It is
\&\fB\s-1GDBM_NUMSYNC\s0\fR to convert the database from standard to extended format, and
\&\fB0\fR to convert it from extended to standard format.
.PP
If the database is already in the requested format, the function returns
success without doing anything.
.SS "dump"
.IX Subsection "dump"
.Vb 1
\&    $db\->dump($filename, %options)
.Ve
.PP
Creates a dump of the database file in \fI\f(CI$filename\fI\fR.  Such file can be used
as a backup copy or sent over a wire to recreate the database on another
machine.  To create a database from the dump file, use the \fBload\fR method.
.PP
\&\fB\s-1GDBM\s0\fR supports two dump formats: old \fIbinary\fR and new \fIascii\fR.  The
binary format is not portable across architectures and is deprecated.  It
is supported for backward compatibility.  The ascii format is portable and
stores additional meta-data about the file.  It was introduced with the
\&\fBgdbm\fR version 1.11 and is the preferred dump format.  The \fBdump\fR method
creates ascii dumps by default.
.PP
If the named file already exists, the function will refuse to overwrite and
will croak an error.  If it doesn't exist, it will be created with the
mode \fB0666\fR modified by the current \fBumask\fR.
.PP
These defaults can be altered using the following \fI\f(CI%options\fI\fR:
.IP "\fBbinary\fR => 1" 4
.IX Item "binary => 1"
Create dump in \fIbinary\fR format.
.IP "\fBmode\fR => \fI\s-1MODE\s0\fR" 4
.IX Item "mode => MODE"
Set file mode to \fI\s-1MODE\s0\fR.
.IP "\fBoverwrite\fR => 1" 4
.IX Item "overwrite => 1"
Silently overwrite existing files.
.SS "load"
.IX Subsection "load"
.Vb 1
\&    $db\->load($filename, %options)
.Ve
.PP
Load the data from the dump file \fI\f(CI$filename\fI\fR into the database \fI\f(CI$db\fI\fR.
The file must have been previously created using the \fBdump\fR method.  File
format is recognized automatically.  By default, the function will croak
if the dump contains a key that already exists in the database.  It will
silently ignore the failure to restore database mode and/or ownership.
These defaults can be altered using the following \fI\f(CI%options\fI\fR:
.IP "\fBreplace\fR => 1" 4
.IX Item "replace => 1"
Replace existing keys.
.IP "\fBrestore_mode\fR => 0 | 1" 4
.IX Item "restore_mode => 0 | 1"
If \fI0\fR, don't try to restore the mode of the database file to that stored
in the dump.
.IP "\fBrestore_owner\fR => 0 | 1" 4
.IX Item "restore_owner => 0 | 1"
If \fI0\fR, don't try to restore the owner of the database file to that stored
in the dump.
.IP "\fBstrict_errors\fR => 1" 4
.IX Item "strict_errors => 1"
Croak if failed to restore ownership and/or mode.
.PP
The usual sequence to recreate a database from the dump file is:
.PP
.Vb 3
\&    my %hash;
\&    my $db = tie %hash, \*(AqGDBM_File\*(Aq, \*(Aqa.db\*(Aq, GDBM_NEWDB, 0640;
\&    $db\->load(\*(Aqa.dump\*(Aq);
.Ve
.SH "CRASH TOLERANCE"
.IX Header "CRASH TOLERANCE"
Crash tolerance is a new feature that, given appropriate support from the \s-1OS\s0
and the filesystem, guarantees that a logically consistent recent state of the
database can be recovered following a crash, such as power outage, \s-1OS\s0 kernel
panic, or the like.
.PP
Crash tolerance support appeared in \fBgdbm\fR version 1.21.  The theory behind
it is explained in \*(L"Crashproofing the Original NoSQL Key-Value Store\*(R",
by Terence Kelly (<https://queue.acm.org/detail.cfm?id=3487353>).  A
detailed discussion of the \fBgdbm\fR implementation is available in the
\&\fB\s-1GDBM\s0 Manual\fR (<https://www.gnu.org.ua/software/gdbm/manual/Crash\-Tolerance.html>).  The information below describes the Perl interface.
.PP
For maximum robustness, we recommend to use \fIextended database format\fR
for crash tolerant databases.  To create a database in extended format,
use the \fBGDBM_NEWDB|GDBM_NUMSYNC\fR when opening the database, e.g.:
.PP
.Vb 2
\&    $db = tie %hash, \*(AqGDBM_File\*(Aq, $filename,
\&              GDBM_NEWDB|GDBM_NUMSYNC, 0640;
.Ve
.PP
To convert existing database to the extended format, use the \fBconvert\fR
method, described above, e.g.:
.PP
.Vb 1
\&    $db\->convert(GDBM_NUMSYNC);
.Ve
.SS "crash_tolerance_status"
.IX Subsection "crash_tolerance_status"
.Vb 1
\&    GDBM_File\->crash_tolerance_status;
.Ve
.PP
This static method returns the status of crash tolerance support.  A
non-zero value means crash tolerance is compiled in and supported by
the operating system.
.SS "failure_atomic"
.IX Subsection "failure_atomic"
.Vb 1
\&    $db\->failure_atomic($even, $odd)
.Ve
.PP
Enables crash tolerance for the database \fB\f(CB$db\fB\fR,  Arguments are
the pathnames of two files that will be created and filled with
\&\fIsnapshots\fR of the database file.  The two files must not exist
when this method is called and must reside on the same filesystem
as the database file.  This filesystem must be support the \fIreflink\fR
operation (https://www.gnu.org.ua/software/gdbm/manual/Filesystems\-supporting\-crash\-tolerance.html>.
.PP
After a successful call to \fBfailure_atomic\fR, every call to \fB\f(CB$db\fB\-\fRsync>
method will make an efficient reflink snapshot of the database file in
one of these files; consecutive calls to \fBsync\fR alternate between the
two, hence the names.
.PP
The most recent of these files can be used to recover the database after
a crash.  To select the right snapshot, use the \fBlatest_snapshot\fR
static method.
.SS "latest_snapshot"
.IX Subsection "latest_snapshot"
.Vb 1
\&    $file = GDBM_File\->latest_snapshot($even, $odd);
\&
\&    ($file, $error) = GDBM_File\->latest_snapshot($even, $odd);
.Ve
.PP
Given the two snapshot names (the ones used previously in a call to
\&\fBfailure_atomic\fR), this method selects the one suitable for database
recovery, i.e. the file which contains the most recent database snapshot.
.PP
In scalar context, it returns the selected file name or \fBundef\fR in case
of failure.
.PP
In array context, the returns a list of two elements: the file name
and status code.  On success, the file name is defined and the code
is \fB\s-1GDBM_SNAPSHOT_OK\s0\fR.  On error, the file name is \fBundef\fR, and
the status is one of the following:
.IP "\s-1GDBM_SNAPSHOT_BAD\s0" 4
.IX Item "GDBM_SNAPSHOT_BAD"
Neither snapshot file is applicable. This means that the crash has occurred
before a call to \fBfailure_atomic\fR completed.  In this case, it is best to
fall back on a safe backup copy of the data file.
.IP "\s-1GDBM_SNAPSHOT_ERR\s0" 4
.IX Item "GDBM_SNAPSHOT_ERR"
A system error occurred.  Examine \fB$!\fR for details.  See
<https://www.gnu.org.ua/software/gdbm/manual/Crash\-recovery.html> for
a comprehensive list of error codes and their meaning.
.IP "\s-1GDBM_SNAPSHOT_SAME\s0" 4
.IX Item "GDBM_SNAPSHOT_SAME"
The file modes and modification dates of both snapshot files are exactly the
same.  This can happen only for databases in standard format.
.IP "\s-1GDBM_SNAPSHOT_SUSPICIOUS\s0" 4
.IX Item "GDBM_SNAPSHOT_SUSPICIOUS"
The \fInumsync\fR counters of the two snapshots differ by more than one.  The
most probable reason is programmer's error: the two parameters refer to
snapshots belonging to different database files.
.SH "AVAILABILITY"
.IX Header "AVAILABILITY"
gdbm is available from any \s-1GNU\s0 archive.  The master site is
\&\f(CW\*(C`ftp.gnu.org\*(C'\fR, but you are strongly urged to use one of the many
mirrors.  You can obtain a list of mirror sites from
<http://www.gnu.org/order/ftp.html>.
.SH "SECURITY AND PORTABILITY"
.IX Header "SECURITY AND PORTABILITY"
\&\s-1GDBM\s0 files are not portable across platforms.  If you wish to transfer
a \s-1GDBM\s0 file over the wire, dump it to a portable format first.
.PP
\&\fBDo not accept \s-1GDBM\s0 files from untrusted sources.\fR
.PP
Robustness of \s-1GDBM\s0 against corrupted databases depends highly on its
version.  Versions prior to 1.15 did not implement any validity
checking, so that a corrupted or maliciously crafted database file
could cause perl to crash or even expose a security vulnerability.
Versions between 1.15 and 1.20 were progressively strengthened against
invalid inputs.  Finally, version 1.21 had undergone extensive fuzzy
checking which proved its ability to withstand any kinds of inputs
without crashing.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
\&\fBperl\fR\|(1), \fBDB_File\fR\|(3), perldbmfilter,
\&\fBgdbm\fR\|(3),
<https://www.gnu.org.ua/software/gdbm/manual.html>.