86
85
result = htable_find(dict_thash->table, name);
87
DICT_ERR_VAL_RETURN(dict, DICT_ERR_NONE, result);
91
90
/* dict_thash_sequence - traverse the dictionary */
119
118
if (dict_thash->cursor[0]) {
120
119
*key = dict_thash->cursor[0]->key;
121
120
*value = dict_thash->cursor[0]->value;
121
DICT_ERR_VAL_RETURN(dict, DICT_ERR_NONE, DICT_STAT_SUCCESS);
125
DICT_ERR_VAL_RETURN(dict, DICT_ERR_NONE, DICT_STAT_FAIL);
151
VSTRING *line_buffer = vstring_alloc(100);
152
VSTRING *line_buffer = 0;
160
162
if (open_flags != O_RDONLY)
161
msg_fatal("%s:%s map requires O_RDONLY access mode",
162
DICT_TYPE_THASH, path);
165
* Create the in-memory table.
167
dict_thash = (DICT_THASH *)
168
dict_alloc(DICT_TYPE_THASH, path, sizeof(*dict_thash));
169
dict_thash->dict.lookup = dict_thash_lookup;
170
dict_thash->dict.sequence = dict_thash_sequence;
171
dict_thash->dict.close = dict_thash_close;
172
dict_thash->dict.flags = dict_flags | DICT_FLAG_DUP_WARN | DICT_FLAG_FIXED;
173
if (dict_flags & DICT_FLAG_FOLD_FIX)
174
dict_thash->dict.fold_buf = vstring_alloc(10);
175
dict_thash->info = 0;
163
return (dict_surrogate(DICT_TYPE_THASH, path, open_flags, dict_flags,
164
"%s:%s map requires O_RDONLY access mode",
165
DICT_TYPE_THASH, path));
178
168
* Read the flat text file into in-memory hash. Read the file again if it
179
169
* may have changed while we were reading.
181
171
for (before = time((time_t *) 0); /* see below */ ; before = after) {
182
if ((fp = vstream_fopen(path, open_flags, 0644)) == 0)
183
msg_fatal("open database %s: %m", path);
172
if ((fp = vstream_fopen(path, open_flags, 0644)) == 0) {
173
return (dict_surrogate(DICT_TYPE_THASH, path, open_flags, dict_flags,
174
"open database %s: %m", path));
176
if (line_buffer == 0)
177
line_buffer = vstring_alloc(100);
185
dict_thash->table = htable_create(13);
179
table = htable_create(13);
186
180
while (readlline(line_buffer, fp, &lineno)) {
215
209
* Optionally fold the key.
217
if (dict_thash->dict.flags & DICT_FLAG_FOLD_FIX)
211
if (dict_flags & DICT_FLAG_FOLD_FIX)
221
215
* Store the value under the key. Handle duplicates
224
if ((ht = htable_locate(dict_thash->table, key)) != 0) {
225
if (dict_thash->dict.flags & DICT_FLAG_DUP_IGNORE) {
218
if ((ht = htable_locate(table, key)) != 0) {
219
if (dict_flags & DICT_FLAG_DUP_IGNORE) {
227
} else if (dict_thash->dict.flags & DICT_FLAG_DUP_REPLACE) {
221
} else if (dict_flags & DICT_FLAG_DUP_REPLACE) {
228
222
myfree(ht->value);
229
223
ht->value = mystrdup(value);
230
} else if (dict_thash->dict.flags & DICT_FLAG_DUP_WARN) {
224
} else if (dict_flags & DICT_FLAG_DUP_WARN) {
231
225
msg_warn("%s, line %d: duplicate entry: \"%s\"",
232
226
path, lineno, key);
254
248
* Yes, it is hot. Discard the result and read the file again.
256
htable_free(dict_thash->table, myfree);
250
htable_free(table, myfree);
257
251
if (msg_verbose > 1)
258
252
msg_info("pausing to let file %s cool down", path);
261
255
vstring_free(line_buffer);
258
* Create the in-memory table.
260
dict_thash = (DICT_THASH *)
261
dict_alloc(DICT_TYPE_THASH, path, sizeof(*dict_thash));
262
dict_thash->dict.lookup = dict_thash_lookup;
263
dict_thash->dict.sequence = dict_thash_sequence;
264
dict_thash->dict.close = dict_thash_close;
265
dict_thash->dict.flags = dict_flags | DICT_FLAG_DUP_WARN | DICT_FLAG_FIXED;
266
if (dict_flags & DICT_FLAG_FOLD_FIX)
267
dict_thash->dict.fold_buf = vstring_alloc(10);
268
dict_thash->info = 0;
269
dict_thash->table = table;
270
dict_thash->dict.owner.uid = st.st_uid;
271
dict_thash->dict.owner.status = (st.st_uid != 0);
263
273
return (DICT_DEBUG (&dict_thash->dict));