static char rcsid[] = "@(#)$Id: astorage_hash.c,v 1.6 2006/04/09 07:37:30 hurtta Exp $";

/******************************************************************************
 *  The Elm (ME+) Mail System  -  $Revision: 1.6 $   $State: Exp $
 *
 *  Author: Kari Hurtta <hurtta+elm@posti.FMI.FI> (was hurtta+elm@ozone.FMI.FI)
 ******************************************************************************
 *  Some code copied from src/alias.c. That file is following
 *  copyright:
 *
 *  The Elm Mail System 
 *
 * 			Copyright (c) 1988-1992 USENET Community Trust
 * 			Copyright (c) 1986,1987 Dave Taylor
 *****************************************************************************/

#include "def_aliases.h"

DEBUG_VAR(Debug,__FILE__,"alias");

void add_files_to_aview(x)
     struct AliasView *x;
{
    struct aliasclass * u;
    struct aliasclass * s;

    if (x->magic != ALIASVIEW_magic)
	panic("ALIAS VIEW PANIC",__FILE__,__LINE__,"add_files_to_aview",
	      "bad aliasview magic",0);
    
    u = add_aliasclass(x,&at_astorage);
    
    u->p.astorage->filename  = safe_strdup(user_data_file);
    u->p.astorage->is_system = 0;
    
    s = add_aliasclass(x,&at_astorage);
    
    s->p.astorage->filename = safe_strdup(system_data_file);
    s->p.astorage->is_system = 0;
}

static int get_one_alias P_((DBZ *db, int current,
			     struct current_astorage *astorage));


int at_update_astorage(c)
     struct aliasclass *c;
{
    struct stat hst;
    int dups = 0;             /* Not really needed */
    int ptr  = 0;

    /* Copied from open_system_alaises() on src/alias.c */

    /* If file hasn't changed, don't bother re-opening. */
    
    if (stat(c->p.astorage->filename,
	     &hst) == 0) {	/* File exists */
	if (hst.st_ctime == c->p.astorage->ctime &&
	    hst.st_mtime == c->p.astorage->mtime) {	   /* No changes */

	    return 0;
	}

	
	/*
	 * Re-open table.  If we can't, just return.
	 */

	if (c->p.astorage->hash != NULL)
	    dbz_close(c->p.astorage->hash);

	if ((c->p.astorage->hash = 
	     dbz_open(c->p.astorage->filename, O_RDONLY, 0)) == NULL)
	    return 0 ;


	c->p.astorage->ctime = hst.st_ctime;
	c->p.astorage->mtime = hst.st_mtime;

	/* Is changed */

    } else {					/* File does not exist */

	/* be sure that table is cleared */
	reset_astorage(c->p.astorage);

	if (c->p.astorage->ctime == 0 && 
	    c->p.astorage->mtime == 0) {
	    return(0);			/* File never existed */
	} else {			/* Once existed, better re-read */

	    /*
	     * Since we no longer exist, we pretend we never existed.
	     */
	    
	    c->p.astorage->ctime = 0;
	    c->p.astorage->mtime = 0;
	    
	    return 1;                 /* We can't read it .... */
	}
    }

    if (! c->p.astorage->hash) 
	panic("ALIAS VIEW PANIC",__FILE__,__LINE__,"at_update_astorage",
	      "Hash not open",0);

  /* Read aliases if changed */

  /* Somewhat copied from get_aliases() on src/alias.c */
    
    /* be sure that table is cleared */
    reset_astorage(c->p.astorage);
    
    DPRINT(Debug,4, (&Debug,
	    "About to read data file = %d.\n",
	    c->p.astorage->hash->dbz_basef));


    fseek(c->p.astorage->hash->dbz_basef, 0L, 0);


    while (get_one_alias(c->p.astorage->hash, ptr, 
			 c->p.astorage)) {

	struct alias_rec *a =  at_astorage_give_alias(c,ptr);

	if (a) {
	    DPRINT(Debug,6,
		   (&Debug, "%d\t%s\t%s\n", ptr,
		    a->alias,
		    a->address));
	}
	
	ptr++;
    }

    c->p.astorage->aliascount = ptr;

    DPRINT(Debug,4,(&Debug,"Setting aliascount of storage to %d\n",ptr));

    return 1; /* Mark changed */
}


static int get_one_alias(db, current, astorage)
     DBZ *db;
     int current;
     struct current_astorage *astorage;
{
    /* Moved from src/alias.c */

/*
 *	Get an alias (name, address, etc.) from the data file
 */

	register struct alias_rec *a;


	if ((a = fetch_alias(db, (char *)NULL)) == NULL)
	    return 0;

	realloc_aliases(astorage,current);

	set_astorage_alias(astorage,current,a);


	return 1;
}

/*
 * Local Variables:
 *  mode:c
 *  c-basic-offset:4
 *  buffer-file-coding-system: iso-8859-1
 * End:
 */


syntax highlighted by Code2HTML, v. 0.9.1