pike.git / src / modules / Gdbm / gdbmmod.c

version» Context lines:

pike.git/src/modules/Gdbm/gdbmmod.c:1:   /*   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id: gdbmmod.c,v 1.30 2005/11/16 16:20:44 grubba Exp $ + || $Id: gdbmmod.c,v 1.31 2006/03/18 18:53:23 grubba Exp $   */      #include "global.h"   #include "gdbm_machine.h"   #include "threads.h"      /* Todo: make sure only one thread accesses the same gdbmmod */      #include "interpret.h"   #include "svalue.h"
pike.git/src/modules/Gdbm/gdbmmod.c:46:    THREADS_ALLOW();    mt_lock(& gdbm_lock);    gdbm_close(dbf);    mt_unlock(& gdbm_lock);    THREADS_DISALLOW();    }   }      static int fixmods(char *mods)   { -  int mode; -  mode=0; +  int mode = 0; +  int flags = GDBM_NOLOCK;    while(1)    {    switch(*(mods++))    {    case 0: -  switch(mode & 15) -  { -  default: Pike_error("No mode given for gdbm->open()\n"); -  case 1|16: -  case 1: mode=GDBM_READER; break; -  case 3: mode=GDBM_WRITER; break; -  case 3|16: mode=GDBM_WRITER | GDBM_FAST; break; -  case 7: mode=GDBM_WRCREAT; break; -  case 7|16: mode=GDBM_WRCREAT | GDBM_FAST; break; -  case 15: mode=GDBM_NEWDB; break; -  case 15|16: mode=GDBM_NEWDB | GDBM_FAST; break; +  switch(mode) { +  default: +  case 0x0: +  Pike_error("No mode given for gdbm->open()\n"); +  case 0x1: /* r */ +  return GDBM_READER; +  case 0x3: /* rw */ +  return GDBM_WRITER | flags; +  case 0x7: /* rwc */ +  return GDBM_WRCREAT | flags; +  case 0xf: /* rwct */ +  return GDBM_NEWDB | flags;    } -  return mode; +     -  case 'r': case 'R': mode|=1; break; -  case 'w': case 'W': mode|=3; break; -  case 'c': case 'C': mode|=7; break; -  case 't': case 'T': mode|=15; break; -  case 'f': case 'F': mode|=16; break; +  case 'r': case 'R': mode = 0x1; break; +  case 'w': case 'W': mode = 0x3; break; +  case 'c': case 'C': mode = 0x7; break; +  case 't': case 'T': mode = 0xf; break;    -  +  /* Flags from this point on. */ +  case 'f': case 'F': flags |= GDBM_FAST; break; +  /* NOTE: The following are new in Pike 7.7. */ + #ifndef GDBM_SYNC + #define GDBM_SYNC 0 + #endif + #ifndef GDBM_NOLOCK + #define GDBM_NOLOCK 0 + #endif +  case 's': case 'S': flags |= GDBM_SYNC; break; +  +  /* NOTE: This flag is inverted! */ +  case 'l': case 'L': flags &= ~GDBM_NOLOCK; break; +     default: -  Pike_error("Bad mode flag in gdbm->open.\n"); +  Pike_error("Bad mode flag '%c' in gdbm->open.\n", mods[-1]);    }    }   }      void gdbmmod_fatal(char *err)   {    Pike_error("GDBM: %s\n",err);   }      /*! @module Gdbm
pike.git/src/modules/Gdbm/gdbmmod.c:109:    *! @value r    *! Open database for reading    *! @value w    *! Open database for writing    *! @value c    *! Create database if it does not exist    *! @value t    *! Overwrite existing database    *! @value f    *! Fast mode +  *! @value s +  *! Synchronous mode +  *! @value l +  *! Locking mode    *! @endstring    *!    *! The fast mode prevents the database from syncronizing each change    *! in the database immediately. This is dangerous because the database    *! can be left in an unusable state if Pike is terminated abnormally.    *!    *! The default mode is @expr{"rwc"@}.    *!    *! @note    *! The gdbm manual states that it is important that the database is
pike.git/src/modules/Gdbm/gdbmmod.c:133:    */      static void gdbmmod_create(INT32 args)   {    struct gdbm_glue *this=THIS;    do_free();    if(args)    {    GDBM_FILE tmp;    struct pike_string *tmp2; -  int rwmode = GDBM_WRCREAT; +  int rwmode = GDBM_WRCREAT|GDBM_NOLOCK;       if(sp[-args].type != T_STRING)    Pike_error("Bad argument 1 to gdbm->create()\n");       if(args>1)    {    if(sp[1-args].type != T_STRING)    Pike_error("Bad argument 2 to gdbm->create()\n");       rwmode=fixmods(sp[1-args].u.string->str);    }    -  +  if (this->dbf) { +  do_free(); +  } +     tmp2=sp[-args].u.string;       THREADS_ALLOW();    mt_lock(& gdbm_lock);    tmp=gdbm_open(tmp2->str, 512, rwmode, 00666, gdbmmod_fatal);    mt_unlock(& gdbm_lock);    THREADS_DISALLOW();       if(!Pike_fp->current_object->prog)    {    if(tmp) gdbm_close(tmp);    Pike_error("Object destructed in gdbm->open()n");    }    this->dbf=tmp;       pop_n_elems(args);    if(!this->dbf) -  Pike_error("Failed to open GDBM database.\n"); +  Pike_error("Failed to open GDBM database: %d: %s.\n", +  gdbm_errno, gdbm_strerror(gdbm_errno));    }   }      #define STRING_TO_DATUM(dat, st) dat.dptr=st->str,dat.dsize=st->len;   #define DATUM_TO_STRING(dat) make_shared_binary_string(dat.dptr, dat.dsize)      /*! @decl string fetch(string key)    *! @decl string `[](string key)    *!    *! Return the data associated with the key 'key' in the database.