2 libctdb local tdb access code
4 Copyright (C) Andrew Tridgell 2006
5 Copyright (C) Rusty Russell 2010
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include <ctdb_protocol.h> // For struct ctdb_ltdb_header
28 #include "local_tdb.h"
31 fetch a record from the ltdb, separating out the header information
32 and returning the body of the record. The caller should free() the
33 header when done, rather than the (optional) data->dptr.
35 struct ctdb_ltdb_header *ctdb_local_fetch(struct tdb_context *tdb,
36 TDB_DATA key, TDB_DATA *data)
40 rec = tdb_fetch(tdb, key);
41 if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
47 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
48 data->dptr = rec.dptr + sizeof(struct ctdb_ltdb_header);
50 return (struct ctdb_ltdb_header *)rec.dptr;
55 write a record to a normal database: 1 on success, 0 if noop, -1 on fail.
56 errno = EIO => tdb error.
58 int ctdb_local_store(struct tdb_context *tdb, TDB_DATA key,
59 struct ctdb_ltdb_header *header, TDB_DATA data)
65 old = tdb_fetch(tdb, key);
66 if (old.dsize < sizeof(*header)) {
71 /* Debugging check: we have lock and should not change hdr. */
72 if (memcmp(old.dptr, header, sizeof(*header)) != 0) {
78 /* Optimize out the nothing-changed case. */
79 if (old.dsize == sizeof(*header) + data.dsize
80 && memcmp(old.dptr+sizeof(*header), data.dptr, data.dsize) == 0) {
85 rec.dsize = sizeof(*header) + data.dsize;
86 rec.dptr = malloc(rec.dsize);
92 memcpy(rec.dptr, header, sizeof(*header));
93 memcpy(rec.dptr + sizeof(*header), data.dptr, data.dsize);
95 ret = tdb_store(tdb, key, rec, TDB_REPLACE);