Commit 8e1c1229 8e1c1229ef668009c479c5b1f40009c09112e6af by Sergey Poznyakoff

These functions provide an intermediate layer between the DBM API and

mailutils' applications.
1 parent 85252ec2
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <mu_dbm.h>
int
mu_fcheck_perm (int fd, int mode)
{
struct stat st;
if (fstat (fd, &st) == -1)
{
if (errno == ENOENT)
return 0;
else
return 1;
}
if ((st.st_mode & 0777) != mode)
return 1;
return 0;
}
int
mu_check_perm (char *name, int mode)
{
struct stat st;
if (stat (name, &st) == -1)
{
if (errno == ENOENT)
return 0;
else
return 1;
}
if ((st.st_mode & 0777) != mode)
return 1;
return 0;
}
#if defined(WITH_GDBM)
#define DB_SUFFIX ".db"
int
mu_dbm_open(char *name, DBM_FILE *db, int flags, int mode)
{
int f;
char *pfname;
pfname = xmalloc (strlen (name) + sizeof DB_SUFFIX);
strcat (strcpy (pfname, name), DB_SUFFIX);
if (mu_check_perm (pfname, mode))
{
free (pfname);
return -1;
}
switch (flags)
{
case MU_STREAM_CREAT:
f = GDBM_NEWDB;
break;
case MU_STREAM_READ:
f = GDBM_READER;
break;
case MU_STREAM_RDWR:
f = GDBM_WRCREAT;
break;
default:
free (pfname);
errno = EINVAL;
return 1;
}
*db = gdbm_open(pfname, 512, f, mode, NULL);
free (pfname);
return *db == NULL;
}
int
mu_dbm_close(DBM_FILE db)
{
gdbm_close(db);
return 0;
}
int
mu_dbm_fetch(DBM_FILE db, DBM_DATUM key, DBM_DATUM *ret)
{
*ret = gdbm_fetch(db, key);
return ret->dptr == NULL;
}
int
mu_dbm_insert(DBM_FILE db, DBM_DATUM key, DBM_DATUM contents, int replace)
{
return gdbm_store(db, key, contents,
replace ? GDBM_REPLACE : GDBM_INSERT);
}
DBM_DATUM
mu_dbm_firstkey (DBM_FILE db)
{
return gdbm_firstkey (db);
}
DBM_DATUM
mu_dbm_nextkey (DBM_FILE db, DBM_DATUM key)
{
return gdbm_nextkey (db, key);
}
#elif defined(WITH_BDB2)
#define DB_SUFFIX ".db"
int
mu_dbm_open(char *name, DBM_FILE *dbm, int flags, int mode)
{
int f, rc;
DB *db;
char *pfname;
pfname = xmalloc (strlen (name) + sizeof DB_SUFFIX);
strcat (strcpy (pfname, name), DB_SUFFIX);
if (mu_check_perm (pfname, mode))
{
free (pfname);
return -1;
}
switch (flags)
{
case MU_STREAM_CREAT:
f = DB_CREATE|DB_TRUNCATE;
break;
case MU_STREAM_READ:
f = DB_RDONLY;
break;
case MU_STREAM_RDWR:
f = DB_CREATE;
break;
default:
free (pfname);
errno = EINVAL;
return -1;
}
rc = db_open (pfname, DB_HASH, f, mode, NULL, NULL, &db);
free (pfname);
if (rc)
return -1;
*dbm = malloc (sizeof **dbm);
if (!*dbm)
{
db->close (db, 0);
errno = ENOMEM;
return -1;
}
(*dbm)->db = db;
(*dbm)->dbc = NULL;
return 0;
}
int
mu_dbm_close(DBM_FILE db)
{
db->db->close (db->db, 0);
free (db);
return 0;
}
int
mu_dbm_fetch(DBM_FILE db, DBM_DATUM key, DBM_DATUM *ret)
{
return db->db->get (db->db, NULL, &key, ret, 0);
}
int
mu_dbm_insert(DBM_FILE db, DBM_DATUM key, DBM_DATUM contents, int replace)
{
/*FIXME: replace unused*/
return db->db->put (db->db, NULL, &key, &contents, 0);
}
DBM_DATUM
mu_dbm_firstkey (DBM_FILE db)
{
DBT key, data;
int ret;
memset(&key, 0, sizeof key);
memset(&data, 0, sizeof data);
if (!db->dbc)
{
if (db->db->cursor(db->db, NULL, &db->dbc, 0) != 0)
return key;
}
if ((ret = db->dbc->c_get(db->dbc, &key, &data, DB_FIRST)) != 0)
{
key.data = NULL;
key.size = 0;
if (ret == DB_NOTFOUND)
errno = ENOENT;
else
errno = ret;
}
return key;
}
DBM_DATUM
mu_dbm_nextkey (DBM_FILE db, DBM_DATUM pkey /*unused*/)
{
DBT key, data;
int ret;
memset(&key, 0, sizeof key);
memset(&data, 0, sizeof data);
if (!db->dbc)
return key;
if ((ret = db->dbc->c_get(db->dbc, &key, &data, DB_NEXT)) != 0)
{
key.data = NULL;
key.size = 0;
if (ret == DB_NOTFOUND)
errno = ENOENT;
else
errno = ret;
}
return key;
}
#elif defined(WITH_NDBM)
#define DB_SUFFIX ".db"
int
mu_dbm_open(char *name, DBM_FILE *db, int flags, int mode)
{
int f;
switch (flags)
{
case MU_STREAM_CREAT:
f = O_CREAT|O_TRUNC|O_RDWR;
break;
case MU_STREAM_READ:
f = O_RDONLY;
break;
case MU_STREAM_RDWR:
f = O_CREAT|O_RDWR;
break;
default:
errno = EINVAL;
return -1;
}
*db = dbm_open (name, f, mode);
if (!*db)
return -1;
if (mu_fcheck_perm (dbm_dirfno (*db), mode)
|| mu_fcheck_perm (dbm_pagfno (*db), mode))
{
dbm_close (*db);
return 1;
}
return 0;
}
int
mu_dbm_close(DBM_FILE db)
{
dbm_close(db);
return 0;
}
int
mu_dbm_fetch(DBM_FILE db, DBM_DATUM key, DBM_DATUM *ret)
{
*ret = dbm_fetch(db, key);
return ret->dptr == NULL;
}
int
mu_dbm_insert(DBM_FILE db, DBM_DATUM key, DBM_DATUM contents, int replace)
{
return dbm_store(db, key, contents, replace ? DBM_REPLACE : DBM_INSERT);
}
DBM_DATUM
mu_dbm_firstkey (DBM_FILE db)
{
return dbm_firstkey (db);
}
DBM_DATUM
mu_dbm_nextkey (DBM_FILE db, DBM_DATUM key)
{
return dbm_nextkey (db, key);
}
#elif defined(WITH_OLD_DBM)
/*ARGSUSED*/
int
mu_dbm_open(char *name, DBM_FILE *db, int flags, int mode)
{
int f;
switch (flags)
{
case MU_STREAM_CREAT:
f = O_CREAT|O_TRUNC|O_RDWR;
break;
case MU_STREAM_READ:
f = O_RDONLY;
break;
case MU_STREAM_RDWR:
f = O_CREAT|O_RDWR;
break;
default:
return -1;
}
if (f & O_CREAT)
{
char *p;
int fd;
p = xmalloc(strlen(name)+5);
strcat(strcpy(p, name), ".pag");
fd = open(p, f, mode);
free(p);
if (fd < 0)
return -1;
close(fd);
p = xmalloc(strlen(name)+5);
strcat(strcpy(p, name), ".dir");
fd = open(p, f, mode);
free(p);
if (fd < 0)
return -1;
close(fd);
}
return dbminit(name);
}
/*ARGSUSED*/
int
mu_dbm_close(DBM_FILE db)
{
dbmclose();
return 0;
}
/*ARGSUSED*/
int
mu_dbm_fetch(DBM_FILE db, DBM_DATUM key, DBM_DATUM *ret)
{
*ret = fetch(key);
return ret->dptr == NULL;
}
int
mu_dbm_insert(DBM_FILE db, DBM_DATUM key, DBM_DATUM contents, int replace)
{
return store(key, contents);
}
DBM_DATUM
mu_dbm_firstkey (DBM_FILE db)
{
return firstkey ();
}
DBM_DATUM
mu_dbm_nextkey (DBM_FILE db, DBM_DATUM key)
{
return nextkey (key);
}
#endif
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <mailutils/stream.h>
#if defined(WITH_GDBM)
#include <gdbm.h>
#define USE_DBM
typedef GDBM_FILE DBM_FILE;
typedef datum DBM_DATUM;
#define MU_DATUM_SIZE(d) d.dsize
#define MU_DATUM_PTR(d) d.dptr
#elif defined(WITH_BDB2)
#include <db.h>
#define USE_DBM
struct db2_file
{
DB *db;
DBC *dbc;
};
typedef struct db2_file *DBM_FILE;
typedef DBT DBM_DATUM;
#define MU_DATUM_SIZE(d) d.size
#define MU_DATUM_PTR(d) d.data
#elif defined(WITH_NDBM)
#include <ndbm.h>
#define USE_DBM
typedef DBM *DBM_FILE;
typedef datum DBM_DATUM;
#define MU_DATUM_SIZE(d) d.dsize
#define MU_DATUM_PTR(d) d.dptr
#elif defined(WITH_OLD_DBM)
#include <dbm.h>
#define USE_DBM
typedef int DBM_FILE;
typedef datum DBM_DATUM;
#define MU_DATUM_SIZE(d) d.dsize
#define MU_DATUM_PTR(d) d.dptr
#endif
#ifdef USE_DBM
int mu_dbm_open __P((char *name, DBM_FILE *db, int flags, int mode));
int mu_dbm_close __P((DBM_FILE db));
int mu_dbm_fetch __P((DBM_FILE db, DBM_DATUM key, DBM_DATUM *ret));
int mu_dbm_insert __P((DBM_FILE db, DBM_DATUM key, DBM_DATUM contents, int replace));
DBM_DATUM mu_dbm_firstkey __P((DBM_FILE db));
DBM_DATUM mu_dbm_nextkey __P((DBM_FILE db, DBM_DATUM key));
#endif
int mu_fcheck_perm __P((int fd, int mode));
int mu_check_perm __P((char *name, int mode));