Add _put.

git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@3171 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Johan Danielsson
1997-08-26 22:29:08 +00:00
parent 0357db7294
commit 5c5e6599d9

View File

@@ -189,6 +189,25 @@ DB_nextkey(krb5_context context, HDB *db, hdb_entry *entry)
return DB_seq(context, db, entry, R_NEXT); return DB_seq(context, db, entry, R_NEXT);
} }
static krb5_error_code
DB_rename(krb5_context context, HDB *db, const char *new_name)
{
int ret;
char *old, *new;
asprintf(&old, "%s.db", db->name);
asprintf(&new, "%s.db", new_name);
ret = rename(old, new);
free(old);
free(new);
if(ret)
return errno;
free(db->name);
db->name = strdup(new_name);
return 0;
}
static krb5_error_code static krb5_error_code
DB__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply) DB__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
{ {
@@ -215,21 +234,26 @@ DB__get(krb5_context context, HDB *db, krb5_data key, krb5_data *reply)
} }
static krb5_error_code static krb5_error_code
DB_rename(krb5_context context, HDB *db, const char *new_name) DB__put(krb5_context context, HDB *db, int replace,
krb5_data key, krb5_data value)
{ {
int ret; DB *d = (DB*)db->db;
char *old, *new; DBT k, v;
int code;
asprintf(&old, "%s.db", db->name); k.data = key.data;
asprintf(&new, "%s.db", new_name); k.size = key.length;
ret = rename(old, new); v.data = value.data;
free(old); v.size = value.length;
free(new); code = db->lock(context, db, HDB_WLOCK);
if(ret) if(code)
return code;
code = d->put(d, &k, &v, replace ? 0 : R_NOOVERWRITE);
db->unlock(context, db);
if(code < 0)
return errno; return errno;
if(code == 1)
free(db->name); return HDB_ERR_EXISTS;
db->name = strdup(new_name);
return 0; return 0;
} }
@@ -256,6 +280,8 @@ hdb_db_open(krb5_context context, HDB **db,
(*db)->lock = DB_lock; (*db)->lock = DB_lock;
(*db)->unlock = DB_unlock; (*db)->unlock = DB_unlock;
(*db)->rename = DB_rename; (*db)->rename = DB_rename;
(*db)->_get = DB__get;
(*db)->_put = DB__put;
return 0; return 0;
} }