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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/* dbmopen.c - Open the file for dbm operations. This looks like the
NDBM interface for dbm use. */
/* This file is part of GDBM, the GNU data base manager.
Copyright (C) 1990-1991, 1993, 2007, 2011, 2016-2019 Free Software
Foundation, Inc.
GDBM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GDBM 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
/* Include system configuration before all else. */
#include "autoconf.h"
#include "ndbm.h"
#include "gdbmdefs.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define DIRSUF ".dir"
/* dir file structure:
GDBM_DIR_MAGIC 4 bytes
GDBM_VERSION_MAJOR 4 bytes
GDBM_VERSION_MINOR 4 bytes
GDBM_VERSION_PATCH 4 bytes
Total length: 16 bytes
*/
#define GDBM_DIR_MAGIC 0x4744424d /* GDBM */
#define DEF_DIR_SIZE 16
static unsigned
getint (const unsigned char *cp)
{
return (cp[0] << 24) + (cp[1] << 16) + (cp[2] << 8) + cp[3];
}
static void
putint (unsigned char *cp, unsigned n)
{
cp[0] = (n >> 24) & 0xff;
cp[1] = (n >> 16) & 0xff;
cp[2] = (n >> 8) & 0xff;
cp[3] = n & 0xff;
}
/* FIXME: revise return codes */
static int
ndbm_open_dir_file0 (const char *file_name, int pagfd, int mode)
{
int fd = -1;
struct stat st, pagst;
unsigned char dirbuf[DEF_DIR_SIZE];
int flags = (mode & GDBM_OPENMASK) == GDBM_READER ?
O_RDONLY : O_RDWR;
if (mode & GDBM_CLOEXEC)
flags |= O_CLOEXEC;
if (fstat (pagfd, &pagst))
{
gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, TRUE); /* FIXME: special code? */
return -1;
}
/* Previous versions of GDBM linked pag to dir. Try to detect this: */
if (stat (file_name, &st) == 0)
{
if (st.st_nlink >= 2)
{
if (st.st_dev == pagst.st_dev && st.st_ino == pagst.st_ino)
{
if (unlink (file_name))
{
if ((mode & GDBM_OPENMASK) == GDBM_READER)
/* Ok, try to cope with it. */
return pagfd;
else if (errno != ENOENT)
{
gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, TRUE);
return -1;
}
}
}
else
{
gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return -1;
}
}
else if (st.st_size == 0)
/* ok */;
else if (st.st_size != DEF_DIR_SIZE)
{
gdbm_set_errno (NULL, GDBM_BAD_MAGIC_NUMBER, FALSE);
return -1;
}
else
{
fd = open (file_name, flags);
if (fd == -1)
{
gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
return fd;
}
if (read (fd, dirbuf, sizeof (dirbuf)) != sizeof (dirbuf))
{
gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
close (fd);
return -1;
}
if (getint (dirbuf) == GDBM_DIR_MAGIC)
{
int v[3];
v[0] = getint (dirbuf + 4);
v[1] = getint (dirbuf + 8);
v[2] = getint (dirbuf + 12);
if (gdbm_version_cmp (v, gdbm_version_number) <= 0)
return fd;
}
close (fd);
gdbm_set_errno (NULL, GDBM_BAD_MAGIC_NUMBER, FALSE);
return -1;
}
}
/* File does not exist. Create it. */
fd = open (file_name, flags | O_CREAT, pagst.st_mode & 0777);
if (fd >= 0)
{
putint (dirbuf, GDBM_DIR_MAGIC);
putint (dirbuf + 4, gdbm_version_number[0]);
putint (dirbuf + 8, gdbm_version_number[1]);
putint (dirbuf + 12, gdbm_version_number[2]);
if (write (fd, dirbuf, sizeof (dirbuf)) != sizeof (dirbuf))
{
gdbm_set_errno (NULL, GDBM_FILE_WRITE_ERROR, FALSE);
close (fd);
fd = -1;
}
}
return fd;
}
static int
ndbm_open_dir_file (const char *base, int pagfd, int mode)
{
char *file_name = malloc (strlen (base) + sizeof (DIRSUF));
int fd;
if (!file_name)
{
gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE);
return -1;
}
fd = ndbm_open_dir_file0 (strcat (strcpy (file_name, base), DIRSUF),
pagfd, mode);
free (file_name);
return fd;
}
/* Initialize ndbm system. FILE is a pointer to the file name. In
standard dbm, the database is found in files called FILE.pag and
FILE.dir. To make gdbm compatable with dbm using the dbminit call,
the same file names are used. Specifically, dbminit will use the file
name FILE.pag in its call to gdbm open. If the file (FILE.pag) has a
size of zero bytes, a file initialization procedure is performed,
setting up the initial structure in the file. Any error detected will
cause a return value of -1. No errors cause the return value of 0.
The file.dir file is used only to ensure that the corresponding file.pag
was created using a compatible version of GDBM. If file.dir is a hard
link to file.pag, as was in GDBM 1.8.x, it is removed and recreated in
the right format (see above). If the database is being opened read-only,
the file.dir remains untouched.
FLAGS and MODE are the same as for the open(2) call. This call will
look at the FLAGS and decide what call to make to gdbm_open. For
FLAGS == O_RDONLY, it will be a GDBM_READER, if FLAGS == O_RDWR|O_CREAT,
it will be a GDBM_WRCREAT (creater and writer) and if the FLAGS == O_RDWR,
it will be a GDBM_WRITER and if FLAGS contain O_TRUNC then it will be
a GDBM_NEWDB. The O_CLOEXEC bit raises GDBM_CLOEXEC flag.
All other values of FLAGS are ignored. */
DBM *
dbm_open (char *file, int flags, int mode)
{
char *pag_file; /* Used to construct "file.pag". */
DBM *dbm = NULL;
int open_flags;
int f;
/* Prepare the correct name for "file.pag". */
pag_file = (char *) malloc (strlen (file) + 5);
if (!pag_file)
{
gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE); /* For the hell of it. */
return NULL;
}
strcpy (pag_file, file);
strcat (pag_file, ".pag");
/* Call the actual routine, saving the pointer to the file information. */
f = flags & (O_RDONLY | O_RDWR | O_CREAT | O_TRUNC);
if (f == O_RDONLY)
{
open_flags = GDBM_READER;
mode = 0;
}
else if (f == (O_RDWR | O_CREAT))
{
open_flags = GDBM_WRCREAT;
}
else if ((f & O_TRUNC) == O_TRUNC)
{
open_flags = GDBM_NEWDB;
}
else
{
open_flags = GDBM_WRITER;
mode = 0;
}
if (flags & O_CLOEXEC)
open_flags |= GDBM_CLOEXEC;
open_flags |= GDBM_NOLOCK;
dbm = calloc (1, sizeof (*dbm));
if (!dbm)
{
free (pag_file);
gdbm_set_errno (NULL, GDBM_MALLOC_ERROR, FALSE); /* For the hell of it. */
return NULL;
}
dbm->file = gdbm_open (pag_file, 0, open_flags, mode, NULL);
/* Did we successfully open the file? */
if (dbm->file == NULL)
{
gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, FALSE);
free (dbm);
dbm = NULL;
}
else
{
dbm->dirfd = ndbm_open_dir_file (file, dbm->file->desc, open_flags);
if (dbm->dirfd == -1)
{
gdbm_close (dbm->file);
free (dbm);
dbm = NULL;
}
}
free (pag_file);
return dbm;
}
|