This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp_leaveeval: use EVAL_KEEPERR
[perl5.git] / ext / SDBM_File / sdbm.c
1 /*
2  * sdbm - ndbm work-alike hashed database library
3  * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978).
4  * author: oz@nexus.yorku.ca
5  * status: public domain.
6  *
7  * core routines
8  */
9
10 #include "INTERN.h"
11 #include "config.h"
12 #ifdef WIN32
13 #include "io.h"
14 #endif
15 #include "sdbm.h"
16 #include "tune.h"
17 #include "pair.h"
18
19 #ifdef I_FCNTL
20 # include <fcntl.h>
21 #endif
22 #ifdef I_SYS_FILE
23 # include <sys/file.h>
24 #endif
25
26 #ifdef I_STRING
27 # ifndef __ultrix__
28 #  include <string.h>
29 # endif
30 #else
31 # include <strings.h>
32 #endif
33
34 /*
35  * externals
36  */
37
38 #include <errno.h> /* See notes in perl.h about avoiding
39                         extern int errno; */
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 extern Malloc_t malloc proto((MEM_SIZE));
45 extern Free_t free proto((Malloc_t));
46
47 #ifdef __cplusplus
48 }
49 #endif
50
51 /*
52  * forward
53  */
54 static int getdbit proto((DBM *, long));
55 static int setdbit proto((DBM *, long));
56 static int getpage proto((DBM *, long));
57 static datum getnext proto((DBM *));
58 static int makroom proto((DBM *, long, int));
59
60 /*
61  * useful macros
62  */
63 #define bad(x)          ((x).dptr == NULL || (x).dsize < 0)
64 #define exhash(item)    sdbm_hash((item).dptr, (item).dsize)
65 #define ioerr(db)       ((db)->flags |= DBM_IOERR)
66
67 #define OFF_PAG(off)    (long) (off) * PBLKSIZ
68 #define OFF_DIR(off)    (long) (off) * DBLKSIZ
69
70 static const long masks[] = {
71         000000000000, 000000000001, 000000000003, 000000000007,
72         000000000017, 000000000037, 000000000077, 000000000177,
73         000000000377, 000000000777, 000000001777, 000000003777,
74         000000007777, 000000017777, 000000037777, 000000077777,
75         000000177777, 000000377777, 000000777777, 000001777777,
76         000003777777, 000007777777, 000017777777, 000037777777,
77         000077777777, 000177777777, 000377777777, 000777777777,
78         001777777777, 003777777777, 007777777777, 017777777777
79 };
80
81 DBM *
82 sdbm_open(char *file, int flags, int mode)
83 {
84         DBM *db;
85         char *dirname;
86         char *pagname;
87         size_t filelen;
88         const size_t dirfext_size = sizeof(DIRFEXT "");
89         const size_t pagfext_size = sizeof(PAGFEXT "");
90
91         if (file == NULL || !*file)
92                 return errno = EINVAL, (DBM *) NULL;
93 /*
94  * need space for two separate filenames
95  */
96         filelen = strlen(file);
97
98         if ((dirname = (char *) malloc(filelen + dirfext_size
99                                        + filelen + pagfext_size)) == NULL)
100                 return errno = ENOMEM, (DBM *) NULL;
101 /*
102  * build the file names
103  */
104         memcpy(dirname, file, filelen);
105         memcpy(dirname + filelen, DIRFEXT, dirfext_size);
106         pagname = dirname + filelen + dirfext_size;
107         memcpy(pagname, file, filelen);
108         memcpy(pagname + filelen, PAGFEXT, pagfext_size);
109
110         db = sdbm_prep(dirname, pagname, flags, mode);
111         free((char *) dirname);
112         return db;
113 }
114
115 DBM *
116 sdbm_prep(char *dirname, char *pagname, int flags, int mode)
117 {
118         DBM *db;
119         struct stat dstat;
120
121         if ((db = (DBM *) malloc(sizeof(DBM))) == NULL)
122                 return errno = ENOMEM, (DBM *) NULL;
123
124         db->flags = 0;
125         db->hmask = 0;
126         db->blkptr = 0;
127         db->keyptr = 0;
128 /*
129  * adjust user flags so that WRONLY becomes RDWR, 
130  * as required by this package. Also set our internal
131  * flag for RDONLY if needed.
132  */
133         if (flags & O_WRONLY)
134                 flags = (flags & ~O_WRONLY) | O_RDWR;
135
136         else if ((flags & 03) == O_RDONLY)
137                 db->flags = DBM_RDONLY;
138 /*
139  * open the files in sequence, and stat the dirfile.
140  * If we fail anywhere, undo everything, return NULL.
141  */
142 #if defined(OS2) || defined(MSDOS) || defined(WIN32) || defined(__CYGWIN__)
143         flags |= O_BINARY;
144 #       endif
145         if ((db->pagf = open(pagname, flags, mode)) > -1) {
146                 if ((db->dirf = open(dirname, flags, mode)) > -1) {
147 /*
148  * need the dirfile size to establish max bit number.
149  */
150                         if (fstat(db->dirf, &dstat) == 0) {
151 /*
152  * zero size: either a fresh database, or one with a single,
153  * unsplit data page: dirpage is all zeros.
154  */
155                                 db->dirbno = (!dstat.st_size) ? 0 : -1;
156                                 db->pagbno = -1;
157                                 db->maxbno = dstat.st_size * BYTESIZ;
158
159                                 (void) memset(db->pagbuf, 0, PBLKSIZ);
160                                 (void) memset(db->dirbuf, 0, DBLKSIZ);
161                         /*
162                          * success
163                          */
164                                 return db;
165                         }
166                         (void) close(db->dirf);
167                 }
168                 (void) close(db->pagf);
169         }
170         free((char *) db);
171         return (DBM *) NULL;
172 }
173
174 void
175 sdbm_close(DBM *db)
176 {
177         if (db == NULL)
178                 errno = EINVAL;
179         else {
180                 (void) close(db->dirf);
181                 (void) close(db->pagf);
182                 free((char *) db);
183         }
184 }
185
186 datum
187 sdbm_fetch(DBM *db, datum key)
188 {
189         if (db == NULL || bad(key))
190                 return errno = EINVAL, nullitem;
191
192         if (getpage(db, exhash(key)))
193                 return getpair(db->pagbuf, key);
194
195         return ioerr(db), nullitem;
196 }
197
198 int
199 sdbm_exists(DBM *db, datum key)
200 {
201         if (db == NULL || bad(key))
202                 return errno = EINVAL, -1;
203
204         if (getpage(db, exhash(key)))
205                 return exipair(db->pagbuf, key);
206
207         return ioerr(db), -1;
208 }
209
210 int
211 sdbm_delete(DBM *db, datum key)
212 {
213         if (db == NULL || bad(key))
214                 return errno = EINVAL, -1;
215         if (sdbm_rdonly(db))
216                 return errno = EPERM, -1;
217
218         if (getpage(db, exhash(key))) {
219                 if (!delpair(db->pagbuf, key))
220                         return -1;
221 /*
222  * update the page file
223  */
224                 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
225                     || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
226                         return ioerr(db), -1;
227
228                 return 0;
229         }
230
231         return ioerr(db), -1;
232 }
233
234 int
235 sdbm_store(DBM *db, datum key, datum val, int flags)
236 {
237         int need;
238         long hash;
239
240         if (db == NULL || bad(key))
241                 return errno = EINVAL, -1;
242         if (sdbm_rdonly(db))
243                 return errno = EPERM, -1;
244
245         need = key.dsize + val.dsize;
246 /*
247  * is the pair too big (or too small) for this database ??
248  */
249         if (need < 0 || need > PAIRMAX)
250                 return errno = EINVAL, -1;
251
252         if (getpage(db, (hash = exhash(key)))) {
253 /*
254  * if we need to replace, delete the key/data pair
255  * first. If it is not there, ignore.
256  */
257                 if (flags == DBM_REPLACE)
258                         (void) delpair(db->pagbuf, key);
259 #ifdef SEEDUPS
260                 else if (duppair(db->pagbuf, key))
261                         return 1;
262 #endif
263 /*
264  * if we do not have enough room, we have to split.
265  */
266                 if (!fitpair(db->pagbuf, need))
267                         if (!makroom(db, hash, need))
268                                 return ioerr(db), -1;
269 /*
270  * we have enough room or split is successful. insert the key,
271  * and update the page file.
272  */
273                 (void) putpair(db->pagbuf, key, val);
274
275                 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
276                     || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
277                         return ioerr(db), -1;
278         /*
279          * success
280          */
281                 return 0;
282         }
283
284         return ioerr(db), -1;
285 }
286
287 /*
288  * makroom - make room by splitting the overfull page
289  * this routine will attempt to make room for SPLTMAX times before
290  * giving up.
291  */
292 static int
293 makroom(DBM *db, long int hash, int need)
294 {
295         long newp;
296         char twin[PBLKSIZ];
297 #if defined(DOSISH) || defined(WIN32)
298         char zer[PBLKSIZ];
299         long oldtail;
300 #endif
301         char *pag = db->pagbuf;
302         char *New = twin;
303         int smax = SPLTMAX;
304 #ifdef BADMESS
305         int rc;
306 #endif
307
308         do {
309 /*
310  * split the current page
311  */
312                 (void) splpage(pag, New, db->hmask + 1);
313 /*
314  * address of the new page
315  */
316                 newp = (hash & db->hmask) | (db->hmask + 1);
317
318 /*
319  * write delay, read avoidance/cache shuffle:
320  * select the page for incoming pair: if key is to go to the new page,
321  * write out the previous one, and copy the new one over, thus making
322  * it the current page. If not, simply write the new page, and we are
323  * still looking at the page of interest. current page is not updated
324  * here, as sdbm_store will do so, after it inserts the incoming pair.
325  */
326
327 #if defined(DOSISH) || defined(WIN32)
328                 /*
329                  * Fill hole with 0 if made it.
330                  * (hole is NOT read as 0)
331                  */
332                 oldtail = lseek(db->pagf, 0L, SEEK_END);
333                 memset(zer, 0, PBLKSIZ);
334                 while (OFF_PAG(newp) > oldtail) {
335                         if (lseek(db->pagf, 0L, SEEK_END) < 0 ||
336                             write(db->pagf, zer, PBLKSIZ) < 0) {
337
338                                 return 0;
339                         }
340                         oldtail += PBLKSIZ;
341                 }
342 #endif
343                 if (hash & (db->hmask + 1)) {
344                         if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
345                             || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
346                                 return 0;
347                         db->pagbno = newp;
348                         (void) memcpy(pag, New, PBLKSIZ);
349                 }
350                 else if (lseek(db->pagf, OFF_PAG(newp), SEEK_SET) < 0
351                          || write(db->pagf, New, PBLKSIZ) < 0)
352                         return 0;
353
354                 if (!setdbit(db, db->curbit))
355                         return 0;
356 /*
357  * see if we have enough room now
358  */
359                 if (fitpair(pag, need))
360                         return 1;
361 /*
362  * try again... update curbit and hmask as getpage would have
363  * done. because of our update of the current page, we do not
364  * need to read in anything. BUT we have to write the current
365  * [deferred] page out, as the window of failure is too great.
366  */
367                 db->curbit = 2 * db->curbit +
368                         ((hash & (db->hmask + 1)) ? 2 : 1);
369                 db->hmask |= db->hmask + 1;
370
371                 if (lseek(db->pagf, OFF_PAG(db->pagbno), SEEK_SET) < 0
372                     || write(db->pagf, db->pagbuf, PBLKSIZ) < 0)
373                         return 0;
374
375         } while (--smax);
376 /*
377  * if we are here, this is real bad news. After SPLTMAX splits,
378  * we still cannot fit the key. say goodnight.
379  */
380 #ifdef BADMESS
381         rc = write(2, "sdbm: cannot insert after SPLTMAX attempts.\n", 44);
382         /* PERL_UNUSED_VAR() or PERL_UNUSED_RESULT() would be
383          * useful here but that would mean pulling in perl.h */
384         (void)rc;
385 #endif
386         return 0;
387
388 }
389
390 /*
391  * the following two routines will break if
392  * deletions aren't taken into account. (ndbm bug)
393  */
394 datum
395 sdbm_firstkey(DBM *db)
396 {
397         if (db == NULL)
398                 return errno = EINVAL, nullitem;
399 /*
400  * start at page 0
401  */
402         if (lseek(db->pagf, OFF_PAG(0), SEEK_SET) < 0
403             || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
404                 return ioerr(db), nullitem;
405         db->pagbno = 0;
406         db->blkptr = 0;
407         db->keyptr = 0;
408
409         return getnext(db);
410 }
411
412 datum
413 sdbm_nextkey(DBM *db)
414 {
415         if (db == NULL)
416                 return errno = EINVAL, nullitem;
417         return getnext(db);
418 }
419
420 /*
421  * all important binary trie traversal
422  */
423 static int
424 getpage(DBM *db, long int hash)
425 {
426         int hbit;
427         long dbit;
428         long pagb;
429
430         dbit = 0;
431         hbit = 0;
432         while (dbit < db->maxbno && getdbit(db, dbit))
433                 dbit = 2 * dbit + ((hash & (1 << hbit++)) ? 2 : 1);
434
435         debug(("dbit: %d...", dbit));
436
437         db->curbit = dbit;
438         db->hmask = masks[hbit];
439
440         pagb = hash & db->hmask;
441 /*
442  * see if the block we need is already in memory.
443  * note: this lookaside cache has about 10% hit rate.
444  */
445         if (pagb != db->pagbno) { 
446 /*
447  * note: here, we assume a "hole" is read as 0s.
448  * if not, must zero pagbuf first.
449  */
450                 if (lseek(db->pagf, OFF_PAG(pagb), SEEK_SET) < 0
451                     || read(db->pagf, db->pagbuf, PBLKSIZ) < 0)
452                         return 0;
453                 if (!chkpage(db->pagbuf))
454                         return 0;
455                 db->pagbno = pagb;
456
457                 debug(("pag read: %d\n", pagb));
458         }
459         return 1;
460 }
461
462 static int
463 getdbit(DBM *db, long int dbit)
464 {
465         long c;
466         long dirb;
467
468         c = dbit / BYTESIZ;
469         dirb = c / DBLKSIZ;
470
471         if (dirb != db->dirbno) {
472                 int got;
473                 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
474                     || (got=read(db->dirf, db->dirbuf, DBLKSIZ)) < 0)
475                         return 0;
476                 if (got==0) 
477                         memset(db->dirbuf,0,DBLKSIZ);
478                 db->dirbno = dirb;
479
480                 debug(("dir read: %d\n", dirb));
481         }
482
483         return db->dirbuf[c % DBLKSIZ] & (1 << dbit % BYTESIZ);
484 }
485
486 static int
487 setdbit(DBM *db, long int dbit)
488 {
489         long c;
490         long dirb;
491
492         c = dbit / BYTESIZ;
493         dirb = c / DBLKSIZ;
494
495         if (dirb != db->dirbno) {
496                 int got;
497                 if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
498                     || (got=read(db->dirf, db->dirbuf, DBLKSIZ)) < 0)
499                         return 0;
500                 if (got==0) 
501                         memset(db->dirbuf,0,DBLKSIZ);
502                 db->dirbno = dirb;
503
504                 debug(("dir read: %d\n", dirb));
505         }
506
507         db->dirbuf[c % DBLKSIZ] |= (1 << dbit % BYTESIZ);
508
509 #if 0
510         if (dbit >= db->maxbno)
511                 db->maxbno += DBLKSIZ * BYTESIZ;
512 #else
513         if (OFF_DIR((dirb+1))*BYTESIZ > db->maxbno) 
514                 db->maxbno=OFF_DIR((dirb+1))*BYTESIZ;
515 #endif
516
517         if (lseek(db->dirf, OFF_DIR(dirb), SEEK_SET) < 0
518             || write(db->dirf, db->dirbuf, DBLKSIZ) < 0)
519                 return 0;
520
521         return 1;
522 }
523
524 /*
525  * getnext - get the next key in the page, and if done with
526  * the page, try the next page in sequence
527  */
528 static datum
529 getnext(DBM *db)
530 {
531         datum key;
532
533         for (;;) {
534                 db->keyptr++;
535                 key = getnkey(db->pagbuf, db->keyptr);
536                 if (key.dptr != NULL)
537                         return key;
538 /*
539  * we either run out, or there is nothing on this page..
540  * try the next one... If we lost our position on the
541  * file, we will have to seek.
542  */
543                 db->keyptr = 0;
544                 if (db->pagbno != db->blkptr++)
545                         if (lseek(db->pagf, OFF_PAG(db->blkptr), SEEK_SET) < 0)
546                                 break;
547                 db->pagbno = db->blkptr;
548                 if (read(db->pagf, db->pagbuf, PBLKSIZ) <= 0)
549                         break;
550                 if (!chkpage(db->pagbuf))
551                         break;
552         }
553
554         return ioerr(db), nullitem;
555 }
556