~ubuntu-branches/ubuntu/vivid/postfix/vivid-proposed

« back to all changes in this revision

Viewing changes to src/util/dict_thash.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2012-03-20 13:47:16 UTC
  • mfrom: (1.1.33)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: package-import@ubuntu.com-20120320134716-v7ab94fmor2z9pvp
Tags: upstream-2.9.1
ImportĀ upstreamĀ versionĀ 2.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
#include <readlline.h>
50
50
#include <dict.h>
51
51
#include <dict_thash.h>
 
52
#include <warn_stat.h>
52
53
 
53
54
/* Application-specific. */
54
55
 
57
58
    HTABLE *table;                      /* in-memory hash */
58
59
    HTABLE_INFO **info;                 /* for iterator */
59
60
    HTABLE_INFO **cursor;               /* ditto */
60
 
}       DICT_THASH;
 
61
} DICT_THASH;
61
62
 
62
63
#define STR     vstring_str
63
64
 
68
69
    DICT_THASH *dict_thash = (DICT_THASH *) dict;
69
70
    const char *result = 0;
70
71
 
71
 
    dict_errno = 0;
72
 
 
73
72
    /*
74
73
     * Optionally fold the key.
75
74
     */
85
84
     */
86
85
    result = htable_find(dict_thash->table, name);
87
86
 
88
 
    return (result);
 
87
    DICT_ERR_VAL_RETURN(dict, DICT_ERR_NONE, result);
89
88
}
90
89
 
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;
122
 
        return (0);
 
121
        DICT_ERR_VAL_RETURN(dict, DICT_ERR_NONE, DICT_STAT_SUCCESS);
123
122
    } else {
124
 
        return (1);
 
123
        *key = 0;
 
124
        *value = 0;
 
125
        DICT_ERR_VAL_RETURN(dict, DICT_ERR_NONE, DICT_STAT_FAIL);
125
126
    }
126
127
}
127
128
 
148
149
    struct stat st;
149
150
    time_t  before;
150
151
    time_t  after;
151
 
    VSTRING *line_buffer = vstring_alloc(100);
 
152
    VSTRING *line_buffer = 0;
152
153
    int     lineno;
153
154
    char   *key;
154
155
    char   *value;
 
156
    HTABLE *table;
155
157
    HTABLE_INFO *ht;
156
158
 
157
159
    /*
158
160
     * Sanity checks.
159
161
     */
160
162
    if (open_flags != O_RDONLY)
161
 
        msg_fatal("%s:%s map requires O_RDONLY access mode",
162
 
                  DICT_TYPE_THASH, path);
163
 
 
164
 
    /*
165
 
     * Create the in-memory table.
166
 
     */
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));
176
166
 
177
167
    /*
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.
180
170
     */
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));
 
175
        }
 
176
        if (line_buffer == 0)
 
177
            line_buffer = vstring_alloc(100);
184
178
        lineno = 0;
185
 
        dict_thash->table = htable_create(13);
 
179
        table = htable_create(13);
186
180
        while (readlline(line_buffer, fp, &lineno)) {
187
181
 
188
182
            /*
214
208
            /*
215
209
             * Optionally fold the key.
216
210
             */
217
 
            if (dict_thash->dict.flags & DICT_FLAG_FOLD_FIX)
 
211
            if (dict_flags & DICT_FLAG_FOLD_FIX)
218
212
                lowercase(key);
219
213
 
220
214
            /*
221
215
             * Store the value under the key. Handle duplicates
222
216
             * appropriately.
223
217
             */
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) {
226
220
                     /* void */ ;
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);
233
227
                } else {
235
229
                              path, lineno, key);
236
230
                }
237
231
            } else {
238
 
                htable_enter(dict_thash->table, key, mystrdup(value));
 
232
                htable_enter(table, key, mystrdup(value));
239
233
            }
240
234
        }
241
235
 
253
247
        /*
254
248
         * Yes, it is hot. Discard the result and read the file again.
255
249
         */
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);
259
253
        doze(300000);
260
254
    }
261
255
    vstring_free(line_buffer);
262
256
 
 
257
    /*
 
258
     * Create the in-memory table.
 
259
     */
 
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);
 
272
 
263
273
    return (DICT_DEBUG (&dict_thash->dict));
264
274
}