home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / gdbmopen.c < prev    next >
C/C++ Source or Header  |  1992-01-19  |  13KB  |  398 lines

  1. /* gdbmopen.c - Open the dbm file and initialize data structures for use. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 1, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.         phone:  (206) 676-3035
  27.  
  28. *************************************************************************/
  29.  
  30.  
  31. #include "gdbmdefs.h"
  32. #include "gdbmerrno.h"
  33.  
  34. extern gdbm_error gdbm_errno;
  35.  
  36.  
  37.  
  38. /* Initialize dbm system.  FILE is a pointer to the file name.  If the file
  39.    has a size of zero bytes, a file initialization procedure is performed,
  40.    setting up the initial structure in the file.  BLOCK_SIZE is used during
  41.    initialization to determine the size of various constructs.  If the value
  42.    is less than 512, the file system blocksize is used, otherwise the value
  43.    of BLOCK_SIZE is used.  BLOCK_SIZE is ignored if the file has previously
  44.    initialized.  If READ_WRITE is set to GDBM_READ the user wants to just
  45.    read the database and any call to dbm_store or dbm_delete will fail. Many
  46.    readers can access the database at the same time.  If READ_WRITE is set to
  47.    GDBM_WRITE, the user wants both read and write access to the database and
  48.    requires exclusive access.  If READ_WRITE is GDBM_WRCREAT, the user wants
  49.    both read and write access to the database and if the database does not
  50.    exist, create a new one.  If READ_WRITE is GDBM_NEWDB, the user want a
  51.    new database created, regardless of whether one existed, and wants read
  52.    and write access to the new database.  Any error detected will cause a
  53.    return value of null and an approprate value will be in gdbm_errno.  If
  54.    no errors occur, a pointer to the "gdbm file descriptor" will be
  55.    returned. */
  56.  
  57.  
  58. gdbm_file_info *
  59. gdbm_open (file, block_size, read_write, mode, fatal_func)
  60.      char *file;
  61.      int  block_size;
  62.      int  read_write;
  63.      int  mode;
  64.      void (*fatal_func) ();
  65. {
  66.   gdbm_file_info *dbf;        /* The record to return. */
  67.   struct stat file_stat;    /* Space for the stat information. */
  68.   int         len;        /* Length of the file name. */
  69.   int         num_bytes;    /* Used in reading and writing. */
  70.   long        file_pos;        /* Used with seeks. */
  71.   int          lock_val;         /* Returned by the flock call. */
  72.   int          file_block_size;    /* Block size to use for a new file. */
  73.   int           index;        /* Used as a loop index. */
  74.   char        need_trunc;    /* Used with GDBM_NEWDB and locking to avoid
  75.                    truncating a file from under a reader. */
  76.  
  77.   /* Allocate new info structure. */
  78.   dbf = (gdbm_file_info *) malloc (sizeof (gdbm_file_info));
  79.   if (dbf == NULL)
  80.     {
  81.       gdbm_errno = GDBM_MALLOC_ERROR;
  82.       return NULL;
  83.     }
  84.  
  85.   /* Initialize some fields for known values.  This is done so gdbm_close
  86.      will work if called before allocating some structures. */
  87.   dbf->dir  = NULL;
  88.   dbf->bucket = NULL;
  89.   for (index = 0; index < CACHE_SIZE; index++)
  90.     {
  91.       dbf->bucket_cache[index].ca_bucket = NULL;
  92.       dbf->bucket_cache[index].ca_data.dptr = NULL;
  93.     }
  94.  
  95.   /* Save name of file. */
  96.   len = strlen (file);
  97.   dbf->name = (char *) malloc (len + 1);
  98.   if (dbf->name == NULL)
  99.     {
  100.       free (dbf);
  101.       gdbm_errno = GDBM_MALLOC_ERROR;
  102.       return NULL;
  103.     }
  104.   strcpy (dbf->name, file);
  105.  
  106.   /* Initialize the fatal error routine. */
  107.   dbf->fatal_err = fatal_func;
  108.  
  109.   /* Open the file. */
  110.   need_trunc = FALSE;
  111.   if (read_write == GDBM_READER)
  112.     {
  113.       dbf->desc = open (dbf->name, O_RDONLY|O_BINARY, 0);
  114.     }
  115.   else if (read_write == GDBM_WRITER)
  116.     {
  117.       dbf->desc = open (dbf->name, O_RDWR|O_BINARY, 0);
  118.     }
  119.   else if (read_write == GDBM_NEWDB)
  120.     {
  121.       dbf->desc = open (dbf->name, O_RDWR|O_CREAT|O_BINARY, mode);
  122.       read_write = GDBM_WRITER;
  123.       need_trunc = TRUE;
  124.     }
  125.   else
  126.     {
  127.       dbf->desc = open (dbf->name, O_RDWR|O_CREAT|O_BINARY, mode);
  128.       read_write = GDBM_WRITER;
  129.     }
  130.   if (dbf->desc < 0)
  131.     {
  132.       free (dbf->name);
  133.       free (dbf);
  134.       gdbm_errno = GDBM_FILE_OPEN_ERROR;
  135.       return NULL;
  136.     }
  137.  
  138.   /* Get the status of the file. */
  139.   fstat (dbf->desc, &file_stat);
  140.  
  141.   /* Lock the file in the approprate way. */
  142.   if (read_write == GDBM_READER)
  143.     {
  144.       if (file_stat.st_size == 0)
  145.     {
  146.       close (dbf->desc);
  147.       free (dbf->name);
  148.       free (dbf);
  149.       gdbm_errno = GDBM_EMPTY_DATABASE;
  150.       return NULL;
  151.     }
  152.       /* Sets lock_val to 0 for success.  See systems.h. */
  153.       READLOCK_FILE(dbf);
  154.     }
  155.   else
  156.     {
  157.       /* Sets lock_val to 0 for success.  See systems.h. */
  158.       WRITELOCK_FILE(dbf);
  159.     }
  160.   if (lock_val != 0)
  161.     {
  162.       close (dbf->desc);
  163.       free (dbf->name);
  164.       free (dbf);
  165.       if (read_write == GDBM_READER)
  166.     gdbm_errno = GDBM_CANT_BE_READER;
  167.       else
  168.     gdbm_errno = GDBM_CANT_BE_WRITER;
  169.       return NULL;
  170.     }
  171.  
  172.   /* Record the kind of user. */
  173.   dbf->read_write = read_write;
  174.  
  175.   /* If we do have a write lock and it was a GDBM_NEWDB, it is
  176.      now time to truncate the file. */
  177.   if (need_trunc && file_stat.st_size != 0)
  178.     {
  179.       TRUNCATE (dbf);
  180.       fstat (dbf->desc, &file_stat);
  181.     }
  182.  
  183.   /* Decide if this is a new file or an old file. */
  184.   if (file_stat.st_size == 0)
  185.     {
  186.  
  187.       /* This is a new file.  Create an empty database.  */
  188.  
  189.       /* Start with the blocksize. */
  190.       if (block_size < 512)
  191.     file_block_size = STATBLKSIZE;
  192.       else
  193.     file_block_size = block_size;
  194.  
  195.       /* Get space for the file header. */
  196.       dbf->header = (gdbm_file_header *) malloc (file_block_size);
  197.       if (dbf->header == NULL)
  198.     {
  199.       gdbm_close (dbf);
  200.       gdbm_errno = GDBM_MALLOC_ERROR;
  201.       return NULL;
  202.     }
  203.  
  204.       /* Set the magic number and the block_size. */
  205.       dbf->header->header_magic = 0x13579ace;
  206.       dbf->header->block_size = file_block_size;
  207.  
  208.       /* Create the initial hash table directory.  */
  209.       dbf->header->dir_size = 8 * sizeof (long);
  210.       dbf->header->dir_bits = 3;
  211.       while (dbf->header->dir_size < dbf->header->block_size)
  212.     {
  213.       dbf->header->dir_size <<= 1;
  214.       dbf->header->dir_bits += 1;
  215.     }
  216.  
  217.       /* Check for correct block_size. */
  218.       if (dbf->header->dir_size != dbf->header->block_size)
  219.     {
  220.       gdbm_close (dbf);
  221.       gdbm_errno = GDBM_BLOCK_SIZE_ERROR;
  222.       return NULL;
  223.     }
  224.  
  225.       /* Allocate the space for the directory. */
  226.       dbf->dir = (long *) malloc (dbf->header->dir_size);
  227.       if (dbf->dir == NULL)
  228.     {
  229.       gdbm_close (dbf);
  230.       gdbm_errno = GDBM_MALLOC_ERROR;
  231.       return NULL;
  232.     }
  233.       dbf->header->dir = dbf->header->block_size;
  234.  
  235.       /* Create the first and only hash bucket. */
  236.       dbf->header->bucket_elems =
  237.     (dbf->header->block_size - sizeof (hash_bucket))
  238.     / sizeof (bucket_element) + 1;
  239.       dbf->header->bucket_size  = dbf->header->block_size;
  240.       dbf->bucket = (hash_bucket *) (alloca (dbf->header->bucket_size));
  241.       if (dbf->bucket == NULL)
  242.     {
  243.       gdbm_close (dbf);
  244.       gdbm_errno = GDBM_MALLOC_ERROR;
  245.       return NULL;
  246.     }
  247.       _gdbm_new_bucket (dbf, dbf->bucket, 0);
  248.       dbf->bucket->av_count = 1;
  249.       dbf->bucket->bucket_avail[0].av_adr = 3*dbf->header->block_size;
  250.       dbf->bucket->bucket_avail[0].av_size = dbf->header->block_size;
  251.  
  252.       /* Set table entries to point to hash buckets. */
  253.       for (index = 0; index < dbf->header->dir_size / sizeof (long); index++)
  254.     dbf->dir[index] = 2*dbf->header->block_size;
  255.  
  256.       /* Initialize the active avail block. */
  257.       dbf->header->avail.size
  258.     = ( (dbf->header->block_size - sizeof (gdbm_file_header))
  259.      / sizeof (avail_elem)) + 1;
  260.       dbf->header->avail.count = 0;
  261.       dbf->header->avail.next_block = 0;
  262.       dbf->header->next_block  = 4*dbf->header->block_size;
  263.  
  264.       /* Write initial configuration to the file. */
  265.       /* Block 0 is the file header and active avail block. */
  266.       num_bytes = write (dbf->desc, dbf->header, dbf->header->block_size);
  267.       if (num_bytes != dbf->header->block_size)
  268.     {
  269.       gdbm_close (dbf);
  270.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  271.       return NULL;
  272.     }
  273.  
  274.       /* Block 1 is the initial bucket directory. */
  275.       num_bytes = write (dbf->desc, dbf->dir, dbf->header->dir_size);
  276.       if (num_bytes != dbf->header->dir_size)
  277.     {
  278.       gdbm_close (dbf);
  279.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  280.       return NULL;
  281.     }
  282.  
  283.       /* Block 2 is the only bucket. */
  284.       num_bytes = write (dbf->desc, dbf->bucket, dbf->header->bucket_size);
  285.       if (num_bytes != dbf->header->bucket_size)
  286.     {
  287.       gdbm_close (dbf);
  288.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  289.       return NULL;
  290.     }
  291.  
  292.       /* Wait for initial configuration to be written to disk. */
  293.       fsync (dbf->desc);
  294.  
  295.     }
  296.   else
  297.     {
  298.       /* This is an old database.  Read in the information from the file
  299.      header and initialize the hash directory. */
  300.  
  301.       gdbm_file_header partial_header;  /* For the first part of it. */
  302.  
  303.       /* Read the partial file header. */
  304.       num_bytes = read (dbf->desc, &partial_header, sizeof (gdbm_file_header));
  305.       if (num_bytes != sizeof (gdbm_file_header))
  306.     {
  307.       gdbm_close (dbf);
  308.       gdbm_errno = GDBM_FILE_READ_ERROR;
  309.       return NULL;
  310.     }
  311.  
  312.       /* Is the magic number good? */
  313.       if (partial_header.header_magic != 0x13579ACEL)
  314.     {
  315.       gdbm_close (dbf);
  316.       gdbm_errno = GDBM_BAD_MAGIC_NUMBER;
  317.       return NULL;
  318.     }
  319.  
  320.       /* It is a good database, read the entire header. */
  321.       dbf->header = (gdbm_file_header *) malloc (partial_header.block_size);
  322.       if (dbf->header == NULL)
  323.     {
  324.       gdbm_close (dbf);
  325.       gdbm_errno = GDBM_MALLOC_ERROR;
  326.       return NULL;
  327.     }
  328.       bcopy (&partial_header, dbf->header, sizeof (gdbm_file_header));
  329.       num_bytes = read (dbf->desc, &dbf->header->avail.av_table[1],
  330.             dbf->header->block_size-sizeof (gdbm_file_header));
  331.       if (num_bytes != dbf->header->block_size-sizeof (gdbm_file_header))
  332.     {
  333.       gdbm_close (dbf);
  334.       gdbm_errno = GDBM_FILE_READ_ERROR;
  335.       return NULL;
  336.     }
  337.  
  338.       /* Allocate space for the hash table directory.  */
  339.       dbf->dir = (long *) malloc (dbf->header->dir_size);
  340.       if (dbf->dir == NULL)
  341.     {
  342.       gdbm_close (dbf);
  343.       gdbm_errno = GDBM_MALLOC_ERROR;
  344.       return NULL;
  345.     }
  346.  
  347.       /* Read the hash table directory. */
  348.       file_pos = lseek (dbf->desc, dbf->header->dir, L_SET);
  349.       if (file_pos != dbf->header->dir)
  350.     {
  351.       gdbm_close (dbf);
  352.       gdbm_errno = GDBM_FILE_SEEK_ERROR;
  353.       return NULL;
  354.     }
  355.  
  356.       num_bytes = read (dbf->desc, dbf->dir, dbf->header->dir_size);
  357.       if (num_bytes != dbf->header->dir_size)
  358.     {
  359.       gdbm_close (dbf);
  360.       gdbm_errno = GDBM_FILE_READ_ERROR;
  361.       return NULL;
  362.     }
  363.  
  364.     }
  365.  
  366.   /* Initialize the bucket cache. */
  367.   for (index = 0; index < CACHE_SIZE; index++)
  368.     {
  369.       dbf->bucket_cache[index].ca_bucket
  370.     = (hash_bucket *) malloc (dbf->header->bucket_size);
  371.       if (dbf->bucket_cache[index].ca_bucket == NULL)
  372.     {
  373.       gdbm_close (dbf);
  374.       gdbm_errno = GDBM_MALLOC_ERROR;
  375.       return NULL;
  376.     }
  377.       dbf->bucket_cache[index].ca_adr = 0;
  378.       dbf->bucket_cache[index].ca_changed = FALSE;
  379.       dbf->bucket_cache[index].ca_data.hash_val = -1;
  380.       dbf->bucket_cache[index].ca_data.elem_loc = -1;
  381.     }
  382.  
  383.   /* Finish initializing dbf. */
  384.   dbf->last_read = -1;
  385.   dbf->bucket = dbf->bucket_cache[0].ca_bucket;
  386.   dbf->bucket_dir = 0;
  387.   dbf->cache_entry = &dbf->bucket_cache[0];
  388.   dbf->header_changed = FALSE;
  389.   dbf->directory_changed = FALSE;
  390.   dbf->bucket_changed = FALSE;
  391.   dbf->second_changed = FALSE;
  392.  
  393.   /* Everything is fine, return the pointer to the file
  394.      information structure.  */
  395.   return dbf;
  396.  
  397. }
  398.