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