ndbm.c
5.28 KB
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2011-2012, 2014-2016 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <mailutils/types.h>
#include <mailutils/dbm.h>
#include <mailutils/util.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/stream.h>
#include <mailutils/io.h>
#include "mudbm.h"
#if defined(WITH_NDBM)
#include <ndbm.h>
static int
_ndbm_file_safety (mu_dbm_file_t db, int mode, uid_t owner)
{
int rc;
char *name;
rc = mu_asprintf (&name, "%s.pag", db->db_name);
if (rc)
return rc;
rc = mu_file_safety_check (name, mode, owner, NULL);
if (rc)
{
free (name);
return rc;
}
strcpy (name + strlen (name) - 3, "dir");
rc = mu_file_safety_check (name, mode, owner, NULL);
free (name);
return rc;
}
#ifndef HAVE_DBM_PAGFNO
# ifdef HAVE_DBM_DIRFNO
# undef dbm_pagfno
# define dbm_pagfno dbm_dirfno
# else
# error "neither dbm_pagfno nor dbm_dirfno available"
# endif
#endif
#ifndef HAVE_DBM_DIRFNO
# ifdef HAVE_DBM_PAGFNO
# undef dbm_dirfno
# define dbm_dirfno dbm_pagfno
# else
# error "neither dbm_pagfno nor dbm_dirfno available"
# endif
#endif
int
_ndbm_get_fd (mu_dbm_file_t db, int *pag, int *dir)
{
DBM *dbm = db->db_descr;
*pag = dbm_pagfno (dbm);
if (dir)
*dir = dbm_dirfno (dbm);
return 0;
}
static int
_ndbm_open (mu_dbm_file_t db, int flags, int mode)
{
int f;
DBM *dbm;
switch (flags)
{
case MU_STREAM_CREAT:
f = O_CREAT|O_TRUNC|O_RDWR;
break;
case MU_STREAM_READ:
f = O_RDONLY;
break;
case MU_STREAM_RDWR:
f = O_CREAT|O_RDWR;
break;
default:
errno = EINVAL;
return -1;
}
dbm = dbm_open (db->db_name, f, mode);
if (!dbm)
return MU_ERR_FAILURE;
db->db_descr = dbm;
return 0;
}
static int
_ndbm_close (mu_dbm_file_t db)
{
if (db->db_descr)
{
dbm_close ((DBM *) db->db_descr);
db->db_descr = NULL;
}
return 0;
}
static int
_ndbm_conv_datum (mu_dbm_file_t db, struct mu_dbm_datum *ret, datum content)
{
ret->mu_dptr = malloc (content.dsize);
if (!ret->mu_dptr)
return errno;
memcpy (ret->mu_dptr, content.dptr, content.dsize);
ret->mu_dsize = content.dsize;
ret->mu_sys = db->db_sys;
return 0;
}
static int
_ndbm_fetch (mu_dbm_file_t db, struct mu_dbm_datum const *key,
struct mu_dbm_datum *ret)
{
datum keydat, content;
keydat.dptr = key->mu_dptr;
keydat.dsize = key->mu_dsize;
errno = 0;
content = dbm_fetch (db->db_descr, keydat);
mu_dbm_datum_free (ret);
if (content.dptr == NULL)
return MU_ERR_NOENT;
return _ndbm_conv_datum (db, ret, content);
}
static int
_ndbm_store (mu_dbm_file_t db,
struct mu_dbm_datum const *key,
struct mu_dbm_datum const *contents,
int replace)
{
DBM *dbm = db->db_descr;
datum keydat, condat;
keydat.dptr = key->mu_dptr;
keydat.dsize = key->mu_dsize;
condat.dptr = contents->mu_dptr;
condat.dsize = contents->mu_dsize;
errno = 0;
switch (dbm_store (dbm, keydat, condat,
replace ? DBM_REPLACE : DBM_INSERT))
{
case 0:
break;
case 1:
return MU_ERR_EXISTS;
case -1:
db->db_errno.n = errno;
return MU_ERR_FAILURE;
}
return 0;
}
static int
_ndbm_delete (mu_dbm_file_t db, struct mu_dbm_datum const *key)
{
DBM *dbm = db->db_descr;
datum keydat;
keydat.dptr = key->mu_dptr;
keydat.dsize = key->mu_dsize;
errno = 0;
switch (dbm_delete (dbm, keydat))
{
case 0:
break;
case 1:
return MU_ERR_NOENT;
case -1:
db->db_errno.n = errno;
return MU_ERR_FAILURE;
}
return 0;
}
static int
_ndbm_firstkey (mu_dbm_file_t db, struct mu_dbm_datum *ret)
{
DBM *dbm = db->db_descr;
datum keydat;
errno = 0;
keydat = dbm_firstkey (dbm);
if (keydat.dptr == NULL)
return MU_ERR_NOENT;
return _ndbm_conv_datum (db, ret, keydat);
}
static int
_ndbm_nextkey (mu_dbm_file_t db, struct mu_dbm_datum *ret)
{
DBM *dbm = db->db_descr;
datum keydat;
keydat = dbm_nextkey (dbm);
if (keydat.dptr == NULL)
return MU_ERR_NOENT;
return _ndbm_conv_datum (db, ret, keydat);
}
static void
_ndbm_datum_free (struct mu_dbm_datum *datum)
{
free (datum->mu_dptr);
}
static char const *
_ndbm_strerror (mu_dbm_file_t db)
{
return strerror (db->db_errno.n);
}
struct mu_dbm_impl _mu_dbm_ndbm = {
"ndbm",
_ndbm_file_safety,
_ndbm_get_fd,
_ndbm_open,
_ndbm_close,
_ndbm_fetch,
_ndbm_store,
_ndbm_delete,
_ndbm_firstkey,
_ndbm_nextkey,
_ndbm_datum_free,
_ndbm_strerror
};
#endif