Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 1 | /* GDBM module, hacked from the still-breathing corpse of the |
| 2 | DBM module by anthony.baxter@aaii.oz.au. Original copyright |
| 3 | follows: |
| 4 | */ |
| 5 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 6 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 7 | The Netherlands. |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 8 | |
| 9 | All Rights Reserved |
| 10 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 11 | Permission to use, copy, modify, and distribute this software and its |
| 12 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 13 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 14 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 15 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 16 | Centrum or CWI or Corporation for National Research Initiatives or |
| 17 | CNRI not be used in advertising or publicity pertaining to |
| 18 | distribution of the software without specific, written prior |
| 19 | permission. |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 21 | While CWI is the initial source for this software, a modified version |
| 22 | is made available by the Corporation for National Research Initiatives |
| 23 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 24 | |
| 25 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 26 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 27 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 28 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 29 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 30 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 31 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 32 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 33 | |
| 34 | ******************************************************************/ |
| 35 | |
| 36 | /* DBM module using dictionary interface */ |
| 37 | |
| 38 | |
| 39 | #include "allobjects.h" |
| 40 | #include "modsupport.h" |
| 41 | |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <fcntl.h> |
| 45 | #include "gdbm.h" |
| 46 | |
| 47 | typedef struct { |
| 48 | OB_HEAD |
| 49 | int di_size; /* -1 means recompute */ |
| 50 | GDBM_FILE di_dbm; |
| 51 | } dbmobject; |
| 52 | |
| 53 | staticforward typeobject Dbmtype; |
| 54 | |
| 55 | #define is_dbmobject(v) ((v)->ob_type == &Dbmtype) |
| 56 | |
| 57 | static object *DbmError; |
| 58 | |
| 59 | static object * |
| 60 | newdbmobject(file, flags, mode) |
| 61 | char *file; |
| 62 | int flags; |
| 63 | int mode; |
| 64 | { |
| 65 | dbmobject *dp; |
| 66 | |
| 67 | dp = NEWOBJ(dbmobject, &Dbmtype); |
| 68 | if (dp == NULL) |
| 69 | return NULL; |
| 70 | dp->di_size = -1; |
Guido van Rossum | b045afc | 1995-03-14 15:04:40 +0000 | [diff] [blame] | 71 | errno = 0; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 72 | if ( (dp->di_dbm = gdbm_open(file, 0, flags, mode, NULL)) == 0 ) { |
Guido van Rossum | b045afc | 1995-03-14 15:04:40 +0000 | [diff] [blame] | 73 | if (errno != 0) |
| 74 | err_errno(DbmError); |
| 75 | else |
| 76 | err_setstr(DbmError, (char *) gdbm_strerror(gdbm_errno)); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 77 | DECREF(dp); |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 78 | return NULL; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 79 | } |
| 80 | return (object *)dp; |
| 81 | } |
| 82 | |
| 83 | /* Methods */ |
| 84 | |
| 85 | static void |
| 86 | dbm_dealloc(dp) |
| 87 | register dbmobject *dp; |
| 88 | { |
| 89 | if ( dp->di_dbm ) |
| 90 | gdbm_close(dp->di_dbm); |
| 91 | DEL(dp); |
| 92 | } |
| 93 | |
| 94 | static int |
| 95 | dbm_length(dp) |
| 96 | dbmobject *dp; |
| 97 | { |
| 98 | if ( dp->di_size < 0 ) { |
| 99 | datum key,okey; |
| 100 | int size; |
| 101 | okey.dsize=0; |
| 102 | |
| 103 | size = 0; |
| 104 | for ( key=gdbm_firstkey(dp->di_dbm); key.dptr; |
| 105 | key = gdbm_nextkey(dp->di_dbm,okey)) { |
| 106 | size++; |
Sjoerd Mullender | 2abc494 | 1994-10-13 09:11:13 +0000 | [diff] [blame] | 107 | if(okey.dsize) free(okey.dptr); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 108 | okey=key; |
| 109 | } |
| 110 | dp->di_size = size; |
| 111 | } |
| 112 | return dp->di_size; |
| 113 | } |
| 114 | |
| 115 | static object * |
| 116 | dbm_subscript(dp, key) |
| 117 | dbmobject *dp; |
| 118 | register object *key; |
| 119 | { |
| 120 | object *v; |
| 121 | datum drec, krec; |
| 122 | |
| 123 | if (!getargs(key, "s#", &krec.dptr, &krec.dsize) ) |
| 124 | return NULL; |
| 125 | |
| 126 | drec = gdbm_fetch(dp->di_dbm, krec); |
| 127 | if ( drec.dptr == 0 ) { |
| 128 | err_setstr(KeyError, GETSTRINGVALUE((stringobject *)key)); |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 129 | return NULL; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 130 | } |
Guido van Rossum | b045afc | 1995-03-14 15:04:40 +0000 | [diff] [blame] | 131 | v = newsizedstringobject(drec.dptr, drec.dsize); |
| 132 | free(drec.dptr); |
| 133 | return v; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static int |
| 137 | dbm_ass_sub(dp, v, w) |
| 138 | dbmobject *dp; |
| 139 | object *v, *w; |
| 140 | { |
| 141 | datum krec, drec; |
| 142 | |
| 143 | if ( !getargs(v, "s#", &krec.dptr, &krec.dsize) ) { |
| 144 | err_setstr(TypeError, "gdbm mappings have string indices only"); |
| 145 | return -1; |
| 146 | } |
| 147 | dp->di_size = -1; |
| 148 | if (w == NULL) { |
| 149 | if ( gdbm_delete(dp->di_dbm, krec) < 0 ) { |
| 150 | err_setstr(KeyError, GETSTRINGVALUE((stringobject *)v)); |
| 151 | return -1; |
| 152 | } |
| 153 | } else { |
| 154 | if ( !getargs(w, "s#", &drec.dptr, &drec.dsize) ) { |
| 155 | err_setstr(TypeError, |
| 156 | "gdbm mappings have string elements only"); |
| 157 | return -1; |
| 158 | } |
Guido van Rossum | b045afc | 1995-03-14 15:04:40 +0000 | [diff] [blame] | 159 | errno = 0; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 160 | if ( gdbm_store(dp->di_dbm, krec, drec, GDBM_REPLACE) < 0 ) { |
Guido van Rossum | b045afc | 1995-03-14 15:04:40 +0000 | [diff] [blame] | 161 | if (errno != 0) |
| 162 | err_errno(DbmError); |
| 163 | else |
| 164 | err_setstr(DbmError, (char *) gdbm_strerror(gdbm_errno)); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 165 | return -1; |
| 166 | } |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static mapping_methods dbm_as_mapping = { |
| 172 | (inquiry)dbm_length, /*mp_length*/ |
| 173 | (binaryfunc)dbm_subscript, /*mp_subscript*/ |
| 174 | (objobjargproc)dbm_ass_sub, /*mp_ass_subscript*/ |
| 175 | }; |
| 176 | |
| 177 | static object * |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 178 | dbm_close(dp, args) |
| 179 | register dbmobject *dp; |
| 180 | object *args; |
| 181 | { |
| 182 | if ( !getnoarg(args) ) |
| 183 | return NULL; |
| 184 | if ( dp->di_dbm ) |
| 185 | gdbm_close(dp->di_dbm); |
| 186 | dp->di_dbm = NULL; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 187 | INCREF(None); |
| 188 | return None; |
| 189 | } |
| 190 | |
| 191 | static object * |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 192 | dbm_keys(dp, args) |
| 193 | register dbmobject *dp; |
| 194 | object *args; |
| 195 | { |
| 196 | register object *v, *item; |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 197 | datum key, nextkey; |
Guido van Rossum | 66017aa | 1995-08-28 02:58:00 +0000 | [diff] [blame] | 198 | int err; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 199 | |
| 200 | if (dp == NULL || !is_dbmobject(dp)) { |
| 201 | err_badcall(); |
| 202 | return NULL; |
| 203 | } |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 204 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 205 | if (!getnoarg(args)) |
| 206 | return NULL; |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 207 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 208 | v = newlistobject(0); |
| 209 | if (v == NULL) |
| 210 | return NULL; |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 211 | |
| 212 | key = gdbm_firstkey(dp->di_dbm); |
| 213 | while (key.dptr) { |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 214 | item = newsizedstringobject(key.dptr, key.dsize); |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 215 | if (item == NULL) { |
| 216 | free(key.dptr); |
| 217 | DECREF(v); |
| 218 | return NULL; |
Guido van Rossum | 66017aa | 1995-08-28 02:58:00 +0000 | [diff] [blame] | 219 | } |
| 220 | err = addlistitem(v, item); |
| 221 | DECREF(item); |
Guido van Rossum | 66017aa | 1995-08-28 02:58:00 +0000 | [diff] [blame] | 222 | if (err != 0) { |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 223 | free(key.dptr); |
| 224 | DECREF(v); |
| 225 | return NULL; |
Guido van Rossum | 66017aa | 1995-08-28 02:58:00 +0000 | [diff] [blame] | 226 | } |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 227 | nextkey = gdbm_nextkey(dp->di_dbm, key); |
| 228 | free(key.dptr); |
| 229 | key = nextkey; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 230 | } |
Guido van Rossum | 3be7140 | 1996-07-21 02:32:44 +0000 | [diff] [blame] | 231 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 232 | return v; |
| 233 | } |
| 234 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 235 | static object * |
| 236 | dbm_has_key(dp, args) |
| 237 | register dbmobject *dp; |
| 238 | object *args; |
| 239 | { |
Guido van Rossum | b045afc | 1995-03-14 15:04:40 +0000 | [diff] [blame] | 240 | datum key; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 241 | |
| 242 | if (!getargs(args, "s#", &key.dptr, &key.dsize)) |
| 243 | return NULL; |
Guido van Rossum | b045afc | 1995-03-14 15:04:40 +0000 | [diff] [blame] | 244 | return newintobject((long) gdbm_exists(dp->di_dbm, key)); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 247 | static object * |
| 248 | dbm_firstkey(dp, args) |
| 249 | register dbmobject *dp; |
| 250 | object *args; |
| 251 | { |
| 252 | register object *v; |
| 253 | datum key; |
| 254 | |
| 255 | if (!getnoarg(args)) |
| 256 | return NULL; |
| 257 | key = gdbm_firstkey(dp->di_dbm); |
| 258 | if (key.dptr) { |
| 259 | v = newsizedstringobject(key.dptr, key.dsize); |
| 260 | free(key.dptr); |
| 261 | return v; |
| 262 | } else { |
| 263 | INCREF(None); |
| 264 | return None; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | static object * |
| 269 | dbm_nextkey(dp, args) |
| 270 | register dbmobject *dp; |
| 271 | object *args; |
| 272 | { |
| 273 | register object *v; |
| 274 | datum key, nextkey; |
| 275 | |
| 276 | if (!getargs(args, "s#", &key.dptr, &key.dsize)) |
| 277 | return NULL; |
| 278 | nextkey = gdbm_nextkey(dp->di_dbm, key); |
| 279 | if (nextkey.dptr) { |
| 280 | v = newsizedstringobject(nextkey.dptr, nextkey.dsize); |
| 281 | free(nextkey.dptr); |
| 282 | return v; |
| 283 | } else { |
| 284 | INCREF(None); |
| 285 | return None; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | static object * |
| 290 | dbm_reorganize(dp, args) |
| 291 | register dbmobject *dp; |
| 292 | object *args; |
| 293 | { |
| 294 | if (!getnoarg(args)) |
| 295 | return NULL; |
| 296 | errno = 0; |
| 297 | if (gdbm_reorganize(dp->di_dbm) < 0) { |
| 298 | if (errno != 0) |
| 299 | err_errno(DbmError); |
| 300 | else |
| 301 | err_setstr(DbmError, (char *) gdbm_strerror(gdbm_errno)); |
| 302 | return NULL; |
| 303 | } |
| 304 | INCREF(None); |
| 305 | return None; |
| 306 | } |
| 307 | |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 308 | static struct methodlist dbm_methods[] = { |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 309 | {"close", (method)dbm_close}, |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 310 | {"keys", (method)dbm_keys}, |
| 311 | {"has_key", (method)dbm_has_key}, |
Guido van Rossum | fbd30e9 | 1995-03-16 16:07:34 +0000 | [diff] [blame] | 312 | {"firstkey", (method)dbm_firstkey}, |
| 313 | {"nextkey", (method)dbm_nextkey}, |
| 314 | {"reorganize", (method)dbm_reorganize}, |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 315 | {NULL, NULL} /* sentinel */ |
| 316 | }; |
| 317 | |
| 318 | static object * |
| 319 | dbm_getattr(dp, name) |
| 320 | dbmobject *dp; |
| 321 | char *name; |
| 322 | { |
| 323 | return findmethod(dbm_methods, (object *)dp, name); |
| 324 | } |
| 325 | |
| 326 | static typeobject Dbmtype = { |
| 327 | OB_HEAD_INIT(&Typetype) |
| 328 | 0, |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 329 | "gdbm", |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 330 | sizeof(dbmobject), |
| 331 | 0, |
| 332 | (destructor)dbm_dealloc, /*tp_dealloc*/ |
| 333 | 0, /*tp_print*/ |
| 334 | (getattrfunc)dbm_getattr, /*tp_getattr*/ |
| 335 | 0, /*tp_setattr*/ |
| 336 | 0, /*tp_compare*/ |
| 337 | 0, /*tp_repr*/ |
| 338 | 0, /*tp_as_number*/ |
| 339 | 0, /*tp_as_sequence*/ |
| 340 | &dbm_as_mapping, /*tp_as_mapping*/ |
| 341 | }; |
| 342 | |
| 343 | /* ----------------------------------------------------------------- */ |
| 344 | |
| 345 | static object * |
| 346 | dbmopen(self, args) |
| 347 | object *self; |
| 348 | object *args; |
| 349 | { |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 350 | char *name; |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 351 | char *flags = "r "; |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 352 | int iflags; |
| 353 | int mode = 0666; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 354 | |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 355 | /* XXXX add other flags. 2nd character can be "f" meaning open in fast mode. */ |
| 356 | if ( !newgetargs(args, "s|si", &name, &flags, &mode) ) |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 357 | return NULL; |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 358 | switch (flags[0]) { |
| 359 | case 'r': |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 360 | iflags = GDBM_READER; |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 361 | break; |
| 362 | case 'w': |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 363 | iflags = GDBM_WRITER; |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 364 | break; |
| 365 | case 'c': |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 366 | iflags = GDBM_WRCREAT; |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 367 | break; |
| 368 | case 'n': |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 369 | iflags = GDBM_NEWDB; |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 370 | break; |
| 371 | default: |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 372 | err_setstr(DbmError, |
Guido van Rossum | b045afc | 1995-03-14 15:04:40 +0000 | [diff] [blame] | 373 | "Flags should be one of 'r', 'w', 'c' or 'n'"); |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 374 | return NULL; |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 375 | } |
Guido van Rossum | e36e1fe | 1996-01-26 21:08:01 +0000 | [diff] [blame] | 376 | if (flags[1] == 'f') |
| 377 | iflags |= GDBM_FAST; |
| 378 | return newdbmobject(name, iflags, mode); |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static struct methodlist dbmmodule_methods[] = { |
Guido van Rossum | 807b7be | 1995-07-07 22:37:11 +0000 | [diff] [blame] | 382 | { "open", (method)dbmopen, 1 }, |
Guido van Rossum | 4b4c664 | 1994-08-08 08:06:37 +0000 | [diff] [blame] | 383 | { 0, 0 }, |
| 384 | }; |
| 385 | |
| 386 | void |
| 387 | initgdbm() { |
| 388 | object *m, *d; |
| 389 | |
| 390 | m = initmodule("gdbm", dbmmodule_methods); |
| 391 | d = getmoduledict(m); |
| 392 | DbmError = newstringobject("gdbm.error"); |
| 393 | if ( DbmError == NULL || dictinsert(d, "error", DbmError) ) |
| 394 | fatal("can't define gdbm.error"); |
| 395 | } |