Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / global / mkmap_open.c
blobdc5ba95260c419706bc491bfd23195526530ebcc
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* mkmap_open 3
6 /* SUMMARY
7 /* create or rewrite database, generic interface
8 /* SYNOPSIS
9 /* #include <mkmap.h>
11 /* MKMAP *mkmap_open(type, path, open_flags, dict_flags)
12 /* char *type;
13 /* char *path;
14 /* int open_flags;
15 /* int dict_flags;
17 /* void mkmap_append(mkmap, key, value, lineno)
18 /* MKMAP *mkmap;
19 /* char *key;
20 /* char *value;
21 /* int lineno;
23 /* void mkmap_close(mkmap)
24 /* MKMAP *mkmap;
25 /* DESCRIPTION
26 /* This module implements support for creating Postfix databases.
27 /* It is a dict(3) wrapper that adds global locking to dict-level
28 /* routines where appropriate.
30 /* mkmap_open() creates or truncates the named database, after
31 /* appending the appropriate suffixes to the specified filename.
32 /* Before the database is updated, it is locked for exclusive
33 /* access, and signal delivery is suspended.
34 /* See dict(3) for a description of \fBopen_flags\fR and \fBdict_flags\fR.
35 /* All errors are fatal.
37 /* mkmap_append() appends the named (key, value) pair to the
38 /* database. Update errors are fatal; duplicate keys are ignored
39 /* (but a warning is issued).
40 /* \fBlineno\fR is used for diagnostics.
42 /* mkmap_close() closes the database, releases any locks,
43 /* and resumes signal delivery. All errors are fatal.
44 /* SEE ALSO
45 /* sigdelay(3) suspend/resume signal delivery
46 /* LICENSE
47 /* .ad
48 /* .fi
49 /* The Secure Mailer license must be distributed with this software.
50 /* AUTHOR(S)
51 /* Wietse Venema
52 /* IBM T.J. Watson Research
53 /* P.O. Box 704
54 /* Yorktown Heights, NY 10598, USA
55 /*--*/
57 /* System library. */
59 #include <sys_defs.h>
60 #include <unistd.h>
61 #include <string.h>
63 /* Utility library. */
65 #include <msg.h>
66 #include <dict.h>
67 #include <dict_db.h>
68 #include <dict_cdb.h>
69 #include <dict_dbm.h>
70 #include <dict_sdbm.h>
71 #include <dict_proxy.h>
72 #include <sigdelay.h>
73 #include <mymalloc.h>
75 /* Global library. */
77 #include "mkmap.h"
80 * Information about available database types. Here, we list only those map
81 * types that support "create" operations.
83 * We use a different table (in dict_open.c) when querying maps.
85 typedef struct {
86 char *type;
87 MKMAP *(*before_open) (const char *);
88 } MKMAP_OPEN_INFO;
90 static const MKMAP_OPEN_INFO mkmap_types[] = {
91 DICT_TYPE_PROXY, mkmap_proxy_open,
92 #ifdef HAS_CDB
93 DICT_TYPE_CDB, mkmap_cdb_open,
94 #endif
95 #ifdef HAS_SDBM
96 DICT_TYPE_SDBM, mkmap_sdbm_open,
97 #endif
98 #ifdef HAS_DBM
99 DICT_TYPE_DBM, mkmap_dbm_open,
100 #endif
101 #ifdef HAS_DB
102 DICT_TYPE_HASH, mkmap_hash_open,
103 DICT_TYPE_BTREE, mkmap_btree_open,
104 #endif
108 /* mkmap_append - append entry to map */
110 #undef mkmap_append
112 void mkmap_append(MKMAP *mkmap, const char *key, const char *value)
114 dict_put(mkmap->dict, key, value);
117 /* mkmap_close - close database */
119 void mkmap_close(MKMAP *mkmap)
123 * Close the database.
125 dict_close(mkmap->dict);
128 * Do whatever special processing is needed after closing the database,
129 * such as releasing a global exclusive lock on the database file.
130 * Individual Postfix dict modules implement locking only for individual
131 * record operations, because most Postfix applications don't need global
132 * exclusive locks.
134 if (mkmap->after_close)
135 mkmap->after_close(mkmap);
138 * Resume signal delivery.
140 sigresume();
143 * Cleanup.
145 myfree((char *) mkmap);
148 /* mkmap_open - create or truncate database */
150 MKMAP *mkmap_open(const char *type, const char *path,
151 int open_flags, int dict_flags)
153 MKMAP *mkmap;
154 const MKMAP_OPEN_INFO *mp;
157 * Find out what map type to use.
159 for (mp = mkmap_types; /* void */ ; mp++) {
160 if (mp->type == 0)
161 msg_fatal("unsupported map type: %s", type);
162 if (strcmp(type, mp->type) == 0)
163 break;
165 if (msg_verbose)
166 msg_info("open %s %s", type, path);
169 * Do whatever before-open initialization is needed, such as acquiring a
170 * global exclusive lock on an existing database file. Individual Postfix
171 * dict modules implement locking only for individual record operations,
172 * because most Postfix applications don't need global exclusive locks.
174 mkmap = mp->before_open(path);
177 * Delay signal delivery, so that we won't leave the database in an
178 * inconsistent state if we can avoid it.
180 sigdelay();
183 * Truncate the database upon open, and update it. Read-write mode is
184 * needed because the underlying routines read as well as write.
186 mkmap->dict = mkmap->open(path, open_flags, dict_flags);
187 mkmap->dict->lock_fd = -1; /* XXX just in case */
188 mkmap->dict->stat_fd = -1; /* XXX just in case */
189 mkmap->dict->flags |= DICT_FLAG_DUP_WARN;
192 * Do whatever post-open initialization is needed, such as acquiring a
193 * global exclusive lock on a database file that did not exist.
194 * Individual Postfix dict modules implement locking only for individual
195 * record operations, because most Postfix applications don't need global
196 * exclusive locks.
198 if (mkmap->after_open)
199 mkmap->after_open(mkmap);
201 return (mkmap);