summaryrefslogtreecommitdiff
path: root/source3/tdb/tdbutil.c
blob: 2546b9d88c73e894f4914101ec9a172afe471b8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* 
   Unix SMB/Netbios implementation.
   Version 3.0
   tdb utility functions
   Copyright (C) Andrew Tridgell 1992-1998
   
   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 of the License, 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 "includes.h"

/* these are little tdb utility functions that are meant to make
   dealing with a tdb database a little less cumbersome in Samba */


/* fetch a value by a arbitrary blob key, return -1 if not found */
int tdb_fetch_int_byblob(TDB_CONTEXT *tdb, char *keyval, size_t len)
{
	TDB_DATA key, data;
	int ret;

	key.dptr = keyval;
	key.dsize = len;
	data = tdb_fetch(tdb, key);
	if (!data.dptr || data.dsize != sizeof(int)) return -1;
	
	memcpy(&ret, data.dptr, sizeof(int));
	free(data.dptr);
	return ret;
}

/* fetch a value by string key, return -1 if not found */
int tdb_fetch_int(TDB_CONTEXT *tdb, char *keystr)
{
	return tdb_fetch_int_byblob(tdb, keystr, strlen(keystr) + 1);
}

/* store a value by an arbitary blob key, return 0 on success, -1 on failure */
int tdb_store_int_byblob(TDB_CONTEXT *tdb, char *keystr, size_t len, int v)
{
	TDB_DATA key, data;

	key.dptr = keystr;
	key.dsize = len;
	data.dptr = (void *)&v;
	data.dsize = sizeof(int);

	return tdb_store(tdb, key, data, TDB_REPLACE);
}

/* store a value by string key, return 0 on success, -1 on failure */
int tdb_store_int(TDB_CONTEXT *tdb, char *keystr, int v)
{
	return tdb_store_int_byblob(tdb, keystr, strlen(keystr) + 1, v);
}

/* Store a buffer by a null terminated string key.  Return 0 on success, -1
   on failure */
int tdb_store_by_string(TDB_CONTEXT *tdb, char *keystr, void *buffer, int len)
{
    TDB_DATA key, data;

    key.dptr = keystr;
    key.dsize = strlen(keystr) + 1;

    data.dptr = buffer;
    data.dsize = len;

    return tdb_store(tdb, key, data, TDB_REPLACE);
}

/* Fetch a buffer using a null terminated string key.  Don't forget to call
   free() on the result dptr. */
TDB_DATA tdb_fetch_by_string(TDB_CONTEXT *tdb, char *keystr)
{
    TDB_DATA key;

    key.dptr = keystr;
    key.dsize = strlen(keystr) + 1;

    return tdb_fetch(tdb, key);
}