2 Samba Unix/Linux CIFS implementation
4 low level TDB/CTDB tool using the dbwrap interface
6 Copyright (C) 2009 Michael Adam <obnox@samba.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 extern bool AllowDebugChange;
26 typedef enum { OP_FETCH, OP_STORE, OP_DELETE } dbwrap_op;
28 typedef enum { TYPE_INT32, TYPE_UINT32 } dbwrap_type;
30 static int dbwrap_tool_fetch_int32(struct db_context *db,
36 value = dbwrap_fetch_int32(db, keyname);
37 d_printf("%d\n", value);
42 static int dbwrap_tool_fetch_uint32(struct db_context *db,
49 ret = dbwrap_fetch_uint32(db, keyname, &value);
51 d_printf("%u\n", value);
54 d_fprintf(stderr, "ERROR: could not fetch uint32 key '%s'\n",
60 static int dbwrap_tool_store_int32(struct db_context *db,
65 int32_t value = *((int32_t *)data);
67 status = dbwrap_trans_store_int32(db, keyname, value);
69 if (!NT_STATUS_IS_OK(status)) {
70 d_fprintf(stderr, "ERROR: could not store int32 key '%s': %s\n",
71 keyname, nt_errstr(status));
78 static int dbwrap_tool_store_uint32(struct db_context *db,
83 uint32_t value = *((uint32_t *)data);
85 status = dbwrap_trans_store_uint32(db, keyname, value);
87 if (!NT_STATUS_IS_OK(status)) {
89 "ERROR: could not store uint32 key '%s': %s\n",
90 keyname, nt_errstr(status));
97 static int dbwrap_tool_delete(struct db_context *db,
103 status = dbwrap_trans_delete_bystring(db, keyname);
105 if (!NT_STATUS_IS_OK(status)) {
106 d_fprintf(stderr, "ERROR deleting record %s : %s\n",
107 keyname, nt_errstr(status));
114 struct dbwrap_op_dispatch_table {
117 int (*cmd)(struct db_context *db,
122 struct dbwrap_op_dispatch_table dispatch_table[] = {
123 { OP_FETCH, TYPE_INT32, dbwrap_tool_fetch_int32 },
124 { OP_FETCH, TYPE_UINT32, dbwrap_tool_fetch_uint32 },
125 { OP_STORE, TYPE_INT32, dbwrap_tool_store_int32 },
126 { OP_STORE, TYPE_UINT32, dbwrap_tool_store_uint32 },
127 { OP_DELETE, TYPE_INT32, dbwrap_tool_delete },
128 { OP_DELETE, TYPE_UINT32, dbwrap_tool_delete },
132 int main(int argc, const char **argv)
134 struct tevent_context *evt_ctx;
135 struct messaging_context *msg_ctx;
136 struct db_context *db;
144 const char *keytype = "int32";
146 const char *valuestr = "0";
149 TALLOC_CTX *mem_ctx = talloc_stackframe();
154 DEBUGLEVEL_CLASS[DBGC_ALL] = 0;
156 AllowDebugChange = false;
157 lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
159 if ((argc != 4) && (argc != 5) && (argc != 6)) {
161 "USAGE: %s <database> <op> <key> [<type> [<value>]]\n"
162 " ops: fetch, store, delete\n"
163 " types: int32, uint32\n",
172 if (strcmp(opname, "store") == 0) {
174 d_fprintf(stderr, "ERROR: operation 'store' requires "
181 } else if (strcmp(opname, "fetch") == 0) {
183 d_fprintf(stderr, "ERROR: operation 'fetch' requires "
184 "type but not value argument\n");
189 } else if (strcmp(opname, "delete") == 0) {
191 d_fprintf(stderr, "ERROR: operation 'delete' does "
192 "not allow type nor value argument\n");
198 "ERROR: invalid op '%s' specified\n"
199 " supported ops: fetch, store, delete\n",
204 if (strcmp(keytype, "int32") == 0) {
206 value = (int32_t)strtol(valuestr, NULL, 10);
207 } else if (strcmp(keytype, "uint32") == 0) {
209 value = (int32_t)strtoul(valuestr, NULL, 10);
211 d_fprintf(stderr, "ERROR: invalid type '%s' specified.\n"
212 " supported types: int32, uint32\n",
217 evt_ctx = tevent_context_init(mem_ctx);
218 if (evt_ctx == NULL) {
219 d_fprintf(stderr, "ERROR: could not init event context\n");
223 msg_ctx = messaging_init(mem_ctx, server_id_self(), evt_ctx);
224 if (msg_ctx == NULL) {
225 d_fprintf(stderr, "ERROR: could not init messaging context\n");
229 db = db_open(mem_ctx, dbname, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
231 d_fprintf(stderr, "ERROR: could not open dbname\n");
235 for (count = 0; dispatch_table[count].cmd != NULL; count++) {
236 if ((op == dispatch_table[count].op) &&
237 (type == dispatch_table[count].type))
239 ret = dispatch_table[count].cmd(db, keyname, &value);
245 TALLOC_FREE(mem_ctx);