gp: Test with binary content for certificate data
[Samba.git] / third_party / heimdal / lib / base / db.c
blobe6f6af41a20348d270e608d61ad0d021d0f0512e
1 /*
2 * Copyright (c) 2011, Secure Endpoints Inc.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 * This is a pluggable simple DB abstraction, with a simple get/set/
33 * delete key/value pair interface.
35 * Plugins may provide any of the following optional features:
37 * - tables -- multiple attribute/value tables in one DB
38 * - locking
39 * - transactions (i.e., allow any heim_object_t as key or value)
40 * - transcoding of values
42 * Stackable plugins that provide missing optional features are
43 * possible.
45 * Any plugin that provides locking will also provide transactions, but
46 * those transactions will not be atomic in the face of failures (a
47 * memory-based rollback log is used).
50 #include "config.h"
52 #include <errno.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #ifdef WIN32
59 #include <io.h>
60 #else
61 #include <sys/file.h>
62 #endif
63 #ifdef HAVE_UNISTD_H
64 #include <unistd.h>
65 #endif
66 #include <fcntl.h>
68 #include "baselocl.h"
69 #include <base64.h>
71 #define HEIM_ENOMEM(ep) \
72 (((ep) && !*(ep)) ? \
73 heim_error_get_code((*(ep) = heim_error_create_enomem())) : ENOMEM)
75 #define HEIM_ERROR_HELPER(ep, ec, args) \
76 (((ep) && !*(ep)) ? \
77 heim_error_get_code((*(ep) = heim_error_create args)) : (ec))
79 #define HEIM_ERROR(ep, ec, args) \
80 (ec == ENOMEM) ? HEIM_ENOMEM(ep) : HEIM_ERROR_HELPER(ep, ec, args);
82 static heim_string_t to_base64(heim_data_t, heim_error_t *);
83 static heim_data_t from_base64(heim_string_t, heim_error_t *);
85 static int open_file(const char *, int , int, int *, heim_error_t *);
86 static int read_json(const char *, heim_object_t *, heim_error_t *);
87 static struct heim_db_type json_dbt;
89 static void HEIM_CALLCONV db_dealloc(void *ptr);
91 struct heim_type_data db_object = {
92 HEIM_TID_DB,
93 "db-object",
94 NULL,
95 db_dealloc,
96 NULL,
97 NULL,
98 NULL,
99 NULL
103 static heim_base_once_t db_plugin_init_once = HEIM_BASE_ONCE_INIT;
105 static heim_dict_t db_plugins;
107 typedef struct db_plugin {
108 heim_string_t name;
109 heim_db_plug_open_f_t openf;
110 heim_db_plug_clone_f_t clonef;
111 heim_db_plug_close_f_t closef;
112 heim_db_plug_lock_f_t lockf;
113 heim_db_plug_unlock_f_t unlockf;
114 heim_db_plug_sync_f_t syncf;
115 heim_db_plug_begin_f_t beginf;
116 heim_db_plug_commit_f_t commitf;
117 heim_db_plug_rollback_f_t rollbackf;
118 heim_db_plug_copy_value_f_t copyf;
119 heim_db_plug_set_value_f_t setf;
120 heim_db_plug_del_key_f_t delf;
121 heim_db_plug_iter_f_t iterf;
122 void *data;
123 } db_plugin_desc, *db_plugin;
125 struct heim_db_data {
126 db_plugin plug;
127 heim_string_t dbtype;
128 heim_string_t dbname;
129 heim_dict_t options;
130 void *db_data;
131 heim_data_t to_release;
132 heim_error_t error;
133 int ret;
134 unsigned int in_transaction:1;
135 unsigned int ro:1;
136 unsigned int ro_tx:1;
137 heim_dict_t set_keys;
138 heim_dict_t del_keys;
139 heim_string_t current_table;
142 static int
143 db_do_log_actions(heim_db_t db, heim_error_t *error);
144 static int
145 db_replay_log(heim_db_t db, heim_error_t *error);
147 static HEIMDAL_MUTEX db_type_mutex = HEIMDAL_MUTEX_INITIALIZER;
149 static void
150 db_init_plugins_once(void *arg)
152 db_plugins = heim_retain(arg);
155 static void HEIM_CALLCONV
156 plugin_dealloc(void *arg)
158 db_plugin plug = arg;
160 heim_release(plug->name);
163 /** heim_db_register
164 * @brief Registers a DB type for use with heim_db_create().
166 * @param dbtype Name of DB type
167 * @param data Private data argument to the dbtype's openf method
168 * @param plugin Structure with DB type methods (function pointers)
170 * Backends that provide begin/commit/rollback methods must provide ACID
171 * semantics.
173 * The registered DB type will have ACID semantics for backends that do
174 * not provide begin/commit/rollback methods but do provide lock/unlock
175 * and rdjournal/wrjournal methods (using a replay log journalling
176 * scheme).
178 * If the registered DB type does not natively provide read vs. write
179 * transaction isolation but does provide a lock method then the DB will
180 * provide read/write transaction isolation.
182 * @return ENOMEM on failure, else 0.
184 * @addtogroup heimbase
187 heim_db_register(const char *dbtype,
188 void *data,
189 struct heim_db_type *plugin)
191 heim_dict_t plugins;
192 heim_string_t s;
193 db_plugin plug, plug2;
194 int ret = 0;
196 if ((plugin->beginf != NULL && plugin->commitf == NULL) ||
197 (plugin->beginf != NULL && plugin->rollbackf == NULL) ||
198 (plugin->lockf != NULL && plugin->unlockf == NULL) ||
199 plugin->copyf == NULL)
200 heim_abort("Invalid DB plugin; make sure methods are paired");
202 /* Initialize */
203 plugins = heim_dict_create(11);
204 if (plugins == NULL)
205 return ENOMEM;
206 heim_base_once_f(&db_plugin_init_once, plugins, db_init_plugins_once);
207 heim_release(plugins);
208 heim_assert(db_plugins != NULL, "heim_db plugin table initialized");
210 s = heim_string_create(dbtype);
211 if (s == NULL)
212 return ENOMEM;
214 plug = heim_alloc(sizeof (*plug), "db_plug", plugin_dealloc);
215 if (plug == NULL) {
216 heim_release(s);
217 return ENOMEM;
220 plug->name = heim_retain(s);
221 plug->openf = plugin->openf;
222 plug->clonef = plugin->clonef;
223 plug->closef = plugin->closef;
224 plug->lockf = plugin->lockf;
225 plug->unlockf = plugin->unlockf;
226 plug->syncf = plugin->syncf;
227 plug->beginf = plugin->beginf;
228 plug->commitf = plugin->commitf;
229 plug->rollbackf = plugin->rollbackf;
230 plug->copyf = plugin->copyf;
231 plug->setf = plugin->setf;
232 plug->delf = plugin->delf;
233 plug->iterf = plugin->iterf;
234 plug->data = data;
236 HEIMDAL_MUTEX_lock(&db_type_mutex);
237 plug2 = heim_dict_get_value(db_plugins, s);
238 if (plug2 == NULL)
239 ret = heim_dict_set_value(db_plugins, s, plug);
240 HEIMDAL_MUTEX_unlock(&db_type_mutex);
241 heim_release(plug);
242 heim_release(s);
244 return ret;
247 static void HEIM_CALLCONV
248 db_dealloc(void *arg)
250 heim_db_t db = arg;
251 heim_assert(!db->in_transaction,
252 "rollback or commit heim_db_t before releasing it");
253 if (db->db_data)
254 (void) db->plug->closef(db->db_data, NULL);
255 heim_release(db->to_release);
256 heim_release(db->dbtype);
257 heim_release(db->dbname);
258 heim_release(db->options);
259 heim_release(db->set_keys);
260 heim_release(db->del_keys);
261 heim_release(db->error);
264 struct dbtype_iter {
265 heim_db_t db;
266 const char *dbname;
267 heim_dict_t options;
268 heim_error_t *error;
272 * Helper to create a DB handle with the first registered DB type that
273 * can open the given DB. This is useful when the app doesn't know the
274 * DB type a priori. This assumes that DB types can "taste" DBs, either
275 * from the filename extension or from the actual file contents.
277 static void
278 dbtype_iter2create_f(heim_object_t dbtype, heim_object_t junk, void *arg)
280 struct dbtype_iter *iter_ctx = arg;
282 if (iter_ctx->db != NULL)
283 return;
284 iter_ctx->db = heim_db_create(heim_string_get_utf8(dbtype),
285 iter_ctx->dbname, iter_ctx->options,
286 iter_ctx->error);
290 * Open a database of the given dbtype.
292 * Database type names can be composed of one or more pseudo-DB types
293 * and one concrete DB type joined with a '+' between each. For
294 * example: "transaction+bdb" might be a Berkeley DB with a layer above
295 * that provides transactions.
297 * Options may be provided via a dict (an associative array). Existing
298 * options include:
300 * - "create", with any value (create if DB doesn't exist)
301 * - "exclusive", with any value (exclusive create)
302 * - "truncate", with any value (truncate the DB)
303 * - "read-only", with any value (disallow writes)
304 * - "sync", with any value (make transactions durable)
305 * - "journal-name", with a string value naming a journal file name
307 * @param dbtype Name of DB type
308 * @param dbname Name of DB (likely a file path)
309 * @param options Options dict
310 * @param db Output open DB handle
311 * @param error Output error object
313 * @return a DB handle
315 * @addtogroup heimbase
317 heim_db_t
318 heim_db_create(const char *dbtype, const char *dbname,
319 heim_dict_t options, heim_error_t *error)
321 heim_string_t s;
322 char *p;
323 db_plugin plug;
324 heim_db_t db;
325 int ret = 0;
327 if (options == NULL) {
328 options = heim_dict_create(11);
329 if (options == NULL) {
330 if (error)
331 *error = heim_error_create_enomem();
332 return NULL;
334 } else {
335 (void) heim_retain(options);
338 if (db_plugins == NULL) {
339 heim_release(options);
340 return NULL;
343 if (dbtype == NULL || *dbtype == '\0') {
344 struct dbtype_iter iter_ctx = { NULL, dbname, options, error};
346 /* Try all dbtypes */
347 heim_dict_iterate_f(db_plugins, &iter_ctx, dbtype_iter2create_f);
348 heim_release(options);
349 return iter_ctx.db;
350 } else if (strstr(dbtype, "json")) {
351 (void) heim_db_register(dbtype, NULL, &json_dbt);
355 * Allow for dbtypes that are composed from pseudo-dbtypes chained
356 * to a real DB type with '+'. For example a pseudo-dbtype might
357 * add locking, transactions, transcoding of values, ...
359 p = strchr(dbtype, '+');
360 if (p != NULL)
361 s = heim_string_create_with_bytes(dbtype, p - dbtype);
362 else
363 s = heim_string_create(dbtype);
364 if (s == NULL) {
365 heim_release(options);
366 return NULL;
369 HEIMDAL_MUTEX_lock(&db_type_mutex);
370 plug = heim_dict_get_value(db_plugins, s);
371 HEIMDAL_MUTEX_unlock(&db_type_mutex);
372 heim_release(s);
373 if (plug == NULL) {
374 if (error)
375 *error = heim_error_create(ENOENT,
376 N_("Heimdal DB plugin not found: %s", ""),
377 dbtype);
378 heim_release(options);
379 return NULL;
382 db = _heim_alloc_object(&db_object, sizeof(*db));
383 if (db == NULL) {
384 heim_release(options);
385 return NULL;
388 db->in_transaction = 0;
389 db->ro_tx = 0;
390 db->set_keys = NULL;
391 db->del_keys = NULL;
392 db->plug = plug;
393 db->options = options;
395 ret = plug->openf(plug->data, dbtype, dbname, options, &db->db_data, error);
396 if (ret) {
397 heim_release(db);
398 if (error && *error == NULL)
399 *error = heim_error_create(ENOENT,
400 N_("Heimdal DB could not be opened: %s", ""),
401 dbname);
402 return NULL;
405 ret = db_replay_log(db, error);
406 if (ret) {
407 heim_release(db);
408 return NULL;
411 if (plug->clonef == NULL) {
412 db->dbtype = heim_string_create(dbtype);
413 db->dbname = heim_string_create(dbname);
415 if (!db->dbtype || ! db->dbname) {
416 heim_release(db);
417 if (error)
418 *error = heim_error_create_enomem();
419 return NULL;
423 return db;
427 * Clone (duplicate) an open DB handle.
429 * This is useful for multi-threaded applications. Applications must
430 * synchronize access to any given DB handle.
432 * Returns EBUSY if there is an open transaction for the input db.
434 * @param db Open DB handle
435 * @param error Output error object
437 * @return a DB handle
439 * @addtogroup heimbase
441 heim_db_t
442 heim_db_clone(heim_db_t db, heim_error_t *error)
444 heim_db_t result;
445 int ret;
447 if (heim_get_tid(db) != HEIM_TID_DB)
448 heim_abort("Expected a database");
449 if (db->in_transaction)
450 heim_abort("DB handle is busy");
452 if (db->plug->clonef == NULL) {
453 return heim_db_create(heim_string_get_utf8(db->dbtype),
454 heim_string_get_utf8(db->dbname),
455 db->options, error);
458 result = _heim_alloc_object(&db_object, sizeof(*result));
459 if (result == NULL) {
460 if (error)
461 *error = heim_error_create_enomem();
462 return NULL;
465 result->set_keys = NULL;
466 result->del_keys = NULL;
467 ret = db->plug->clonef(db->db_data, &result->db_data, error);
468 if (ret) {
469 heim_release(result);
470 if (error && !*error)
471 *error = heim_error_create(ENOENT,
472 N_("Could not re-open DB while cloning", ""));
473 return NULL;
475 db->db_data = NULL;
476 return result;
480 * Open a transaction on the given db.
482 * @param db Open DB handle
483 * @param error Output error object
485 * @return 0 on success, system error otherwise
487 * @addtogroup heimbase
490 heim_db_begin(heim_db_t db, int read_only, heim_error_t *error)
492 int ret;
494 if (heim_get_tid(db) != HEIM_TID_DB)
495 return EINVAL;
497 if (db->in_transaction && (read_only || !db->ro_tx || (!read_only && !db->ro_tx)))
498 heim_abort("DB already in transaction");
500 if (db->plug->setf == NULL || db->plug->delf == NULL)
501 return EINVAL;
503 if (db->plug->beginf) {
504 ret = db->plug->beginf(db->db_data, read_only, error);
505 if (ret)
506 return ret;
507 } else if (!db->in_transaction) {
508 /* Try to emulate transactions */
510 if (db->plug->lockf == NULL)
511 return EINVAL; /* can't lock? -> no transactions */
513 /* Assume unlock provides sync/durability */
514 ret = db->plug->lockf(db->db_data, read_only, error);
515 if (ret)
516 return ret;
518 ret = db_replay_log(db, error);
519 if (ret) {
520 ret = db->plug->unlockf(db->db_data, error);
521 return ret;
524 db->set_keys = heim_dict_create(11);
525 if (db->set_keys == NULL)
526 return ENOMEM;
527 db->del_keys = heim_dict_create(11);
528 if (db->del_keys == NULL) {
529 heim_release(db->set_keys);
530 db->set_keys = NULL;
531 return ENOMEM;
533 } else {
534 heim_assert(read_only == 0, "Internal error");
535 ret = db->plug->lockf(db->db_data, 0, error);
536 if (ret)
537 return ret;
539 db->in_transaction = 1;
540 db->ro_tx = !!read_only;
541 return 0;
545 * Commit an open transaction on the given db.
547 * @param db Open DB handle
548 * @param error Output error object
550 * @return 0 on success, system error otherwise
552 * @addtogroup heimbase
555 heim_db_commit(heim_db_t db, heim_error_t *error)
557 int ret, ret2;
558 heim_string_t journal_fname = NULL;
560 if (heim_get_tid(db) != HEIM_TID_DB)
561 return EINVAL;
562 if (!db->in_transaction)
563 return 0;
564 if (db->plug->commitf == NULL && db->plug->lockf == NULL)
565 return EINVAL;
567 if (db->plug->commitf != NULL) {
568 ret = db->plug->commitf(db->db_data, error);
569 if (ret)
570 (void) db->plug->rollbackf(db->db_data, error);
572 db->in_transaction = 0;
573 db->ro_tx = 0;
574 return ret;
577 if (db->ro_tx) {
578 ret = 0;
579 goto done;
582 if (db->options)
583 journal_fname = heim_dict_get_value(db->options, HSTR("journal-filename"));
585 if (journal_fname != NULL) {
586 heim_array_t a;
587 heim_string_t journal_contents;
588 size_t len, bytes;
589 int save_errno;
591 /* Create contents for replay log */
592 ret = ENOMEM;
593 a = heim_array_create();
594 if (a == NULL)
595 goto err;
596 ret = heim_array_append_value(a, db->set_keys);
597 if (ret) {
598 heim_release(a);
599 goto err;
601 ret = heim_array_append_value(a, db->del_keys);
602 if (ret) {
603 heim_release(a);
604 goto err;
606 journal_contents = heim_json_copy_serialize(a, 0, error);
607 heim_release(a);
609 /* Write replay log */
610 if (journal_fname != NULL) {
611 int fd;
613 ret = open_file(heim_string_get_utf8(journal_fname), 1, 0, &fd, error);
614 if (ret) {
615 heim_release(journal_contents);
616 goto err;
618 len = strlen(heim_string_get_utf8(journal_contents));
619 bytes = write(fd, heim_string_get_utf8(journal_contents), len);
620 save_errno = errno;
621 heim_release(journal_contents);
622 ret = close(fd);
623 if (bytes != len) {
624 /* Truncate replay log */
625 (void) open_file(heim_string_get_utf8(journal_fname), 1, 0, NULL, error);
626 ret = save_errno;
627 goto err;
629 if (ret)
630 goto err;
634 /* Apply logged actions */
635 ret = db_do_log_actions(db, error);
636 if (ret)
637 return ret;
639 if (db->plug->syncf != NULL) {
640 /* fsync() or whatever */
641 ret = db->plug->syncf(db->db_data, error);
642 if (ret)
643 return ret;
646 /* Truncate replay log and we're done */
647 if (journal_fname != NULL) {
648 int fd;
650 ret2 = open_file(heim_string_get_utf8(journal_fname), 1, 0, &fd, error);
651 if (ret2 == 0)
652 (void) close(fd);
656 * Clean up; if we failed to remore the replay log that's OK, we'll
657 * handle that again in heim_db_commit()
659 done:
660 heim_release(db->set_keys);
661 heim_release(db->del_keys);
662 db->set_keys = NULL;
663 db->del_keys = NULL;
664 db->in_transaction = 0;
665 db->ro_tx = 0;
667 ret2 = db->plug->unlockf(db->db_data, error);
668 if (ret == 0)
669 ret = ret2;
671 return ret;
673 err:
674 return HEIM_ERROR(error, ret,
675 (ret, N_("Error while committing transaction: %s", ""),
676 strerror(ret)));
680 * Rollback an open transaction on the given db.
682 * @param db Open DB handle
683 * @param error Output error object
685 * @return 0 on success, system error otherwise
687 * @addtogroup heimbase
690 heim_db_rollback(heim_db_t db, heim_error_t *error)
692 int ret = 0;
694 if (heim_get_tid(db) != HEIM_TID_DB)
695 return EINVAL;
696 if (!db->in_transaction)
697 return 0;
699 if (db->plug->rollbackf != NULL)
700 ret = db->plug->rollbackf(db->db_data, error);
701 else if (db->plug->unlockf != NULL)
702 ret = db->plug->unlockf(db->db_data, error);
704 heim_release(db->set_keys);
705 heim_release(db->del_keys);
706 db->set_keys = NULL;
707 db->del_keys = NULL;
708 db->in_transaction = 0;
709 db->ro_tx = 0;
711 return ret;
715 * Get type ID of heim_db_t objects.
717 * @addtogroup heimbase
719 heim_tid_t
720 heim_db_get_type_id(void)
722 return HEIM_TID_DB;
725 heim_data_t
726 _heim_db_get_value(heim_db_t db, heim_string_t table, heim_data_t key,
727 heim_error_t *error)
729 heim_release(db->to_release);
730 db->to_release = heim_db_copy_value(db, table, key, error);
731 return db->to_release;
735 * Lookup a key's value in the DB.
737 * Returns 0 on success, -1 if the key does not exist in the DB, or a
738 * system error number on failure.
740 * @param db Open DB handle
741 * @param key Key
742 * @param error Output error object
744 * @return the value (retained), if there is one for the given key
746 * @addtogroup heimbase
748 heim_data_t
749 heim_db_copy_value(heim_db_t db, heim_string_t table, heim_data_t key,
750 heim_error_t *error)
752 heim_object_t v;
753 heim_data_t result;
755 if (heim_get_tid(db) != HEIM_TID_DB)
756 return NULL;
758 if (error != NULL)
759 *error = NULL;
761 if (table == NULL)
762 table = HSTR("");
764 if (db->in_transaction) {
765 heim_string_t key64;
767 key64 = to_base64(key, error);
768 if (key64 == NULL) {
769 if (error)
770 *error = heim_error_create_enomem();
771 return NULL;
774 v = heim_path_copy(db->set_keys, error, table, key64, NULL);
775 if (v != NULL) {
776 heim_release(key64);
777 return v;
779 v = heim_path_copy(db->del_keys, error, table, key64, NULL); /* can't be NULL */
780 heim_release(key64);
781 if (v != NULL)
782 return NULL;
785 result = db->plug->copyf(db->db_data, table, key, error);
787 return result;
791 * Set a key's value in the DB.
793 * @param db Open DB handle
794 * @param key Key
795 * @param value Value (if NULL the key will be deleted, but empty is OK)
796 * @param error Output error object
798 * @return 0 on success, system error otherwise
800 * @addtogroup heimbase
803 heim_db_set_value(heim_db_t db, heim_string_t table,
804 heim_data_t key, heim_data_t value, heim_error_t *error)
806 heim_string_t key64 = NULL;
807 int ret;
809 if (error != NULL)
810 *error = NULL;
812 if (table == NULL)
813 table = HSTR("");
815 if (value == NULL)
816 /* Use heim_null_t instead of NULL */
817 return heim_db_delete_key(db, table, key, error);
819 if (heim_get_tid(db) != HEIM_TID_DB)
820 return EINVAL;
822 if (heim_get_tid(key) != HEIM_TID_DATA)
823 return HEIM_ERROR(error, EINVAL,
824 (EINVAL, N_("DB keys must be data", "")));
826 if (db->plug->setf == NULL)
827 return EBADF;
829 if (!db->in_transaction) {
830 ret = heim_db_begin(db, 0, error);
831 if (ret)
832 goto err;
833 heim_assert(db->in_transaction, "Internal error");
834 ret = heim_db_set_value(db, table, key, value, error);
835 if (ret) {
836 (void) heim_db_rollback(db, NULL);
837 return ret;
839 return heim_db_commit(db, error);
842 /* Transaction emulation */
843 heim_assert(db->set_keys != NULL, "Internal error");
844 key64 = to_base64(key, error);
845 if (key64 == NULL)
846 return HEIM_ENOMEM(error);
848 if (db->ro_tx) {
849 ret = heim_db_begin(db, 0, error);
850 if (ret)
851 goto err;
853 ret = heim_path_create(db->set_keys, 29, value, error, table, key64, NULL);
854 if (ret)
855 goto err;
856 heim_path_delete(db->del_keys, error, table, key64, NULL);
857 heim_release(key64);
859 return 0;
861 err:
862 heim_release(key64);
863 return HEIM_ERROR(error, ret,
864 (ret, N_("Could not set a dict value while while "
865 "setting a DB value", "")));
869 * Delete a key and its value from the DB
872 * @param db Open DB handle
873 * @param key Key
874 * @param error Output error object
876 * @return 0 on success, system error otherwise
878 * @addtogroup heimbase
881 heim_db_delete_key(heim_db_t db, heim_string_t table, heim_data_t key,
882 heim_error_t *error)
884 heim_string_t key64 = NULL;
885 int ret;
887 if (error != NULL)
888 *error = NULL;
890 if (table == NULL)
891 table = HSTR("");
893 if (heim_get_tid(db) != HEIM_TID_DB)
894 return EINVAL;
896 if (db->plug->delf == NULL)
897 return EBADF;
899 if (!db->in_transaction) {
900 ret = heim_db_begin(db, 0, error);
901 if (ret)
902 goto err;
903 heim_assert(db->in_transaction, "Internal error");
904 ret = heim_db_delete_key(db, table, key, error);
905 if (ret) {
906 (void) heim_db_rollback(db, NULL);
907 return ret;
909 return heim_db_commit(db, error);
912 /* Transaction emulation */
913 heim_assert(db->set_keys != NULL, "Internal error");
914 key64 = to_base64(key, error);
915 if (key64 == NULL)
916 return HEIM_ENOMEM(error);
917 if (db->ro_tx) {
918 ret = heim_db_begin(db, 0, error);
919 if (ret)
920 goto err;
922 ret = heim_path_create(db->del_keys, 29, heim_number_create(1), error, table, key64, NULL);
923 if (ret)
924 goto err;
925 heim_path_delete(db->set_keys, error, table, key64, NULL);
926 heim_release(key64);
928 return 0;
930 err:
931 heim_release(key64);
932 return HEIM_ERROR(error, ret,
933 (ret, N_("Could not set a dict value while while "
934 "deleting a DB value", "")));
938 * Iterate a callback function over keys and values from a DB.
940 * @param db Open DB handle
941 * @param iter_data Callback function's private data
942 * @param iter_f Callback function, called once per-key/value pair
943 * @param error Output error object
945 * @addtogroup heimbase
947 void
948 heim_db_iterate_f(heim_db_t db, heim_string_t table, void *iter_data,
949 heim_db_iterator_f_t iter_f, heim_error_t *error)
951 if (error != NULL)
952 *error = NULL;
954 if (heim_get_tid(db) != HEIM_TID_DB)
955 return;
957 if (!db->in_transaction)
958 db->plug->iterf(db->db_data, table, iter_data, iter_f, error);
961 static void
962 db_replay_log_table_set_keys_iter(heim_object_t key, heim_object_t value,
963 void *arg)
965 heim_db_t db = arg;
966 heim_data_t k, v;
968 if (db->ret)
969 return;
971 k = from_base64((heim_string_t)key, &db->error);
972 if (k == NULL) {
973 db->ret = ENOMEM;
974 return;
976 v = (heim_data_t)value;
978 db->ret = db->plug->setf(db->db_data, db->current_table, k, v, &db->error);
979 heim_release(k);
982 static void
983 db_replay_log_table_del_keys_iter(heim_object_t key, heim_object_t value,
984 void *arg)
986 heim_db_t db = arg;
987 heim_data_t k;
989 if (db->ret) {
990 db->ret = ENOMEM;
991 return;
994 k = from_base64((heim_string_t)key, &db->error);
995 if (k == NULL)
996 return;
998 db->ret = db->plug->delf(db->db_data, db->current_table, k, &db->error);
999 heim_release(k);
1002 static void
1003 db_replay_log_set_keys_iter(heim_object_t table, heim_object_t table_dict,
1004 void *arg)
1006 heim_db_t db = arg;
1008 if (db->ret)
1009 return;
1011 db->current_table = table;
1012 heim_dict_iterate_f(table_dict, db, db_replay_log_table_set_keys_iter);
1015 static void
1016 db_replay_log_del_keys_iter(heim_object_t table, heim_object_t table_dict,
1017 void *arg)
1019 heim_db_t db = arg;
1021 if (db->ret)
1022 return;
1024 db->current_table = table;
1025 heim_dict_iterate_f(table_dict, db, db_replay_log_table_del_keys_iter);
1028 static int
1029 db_do_log_actions(heim_db_t db, heim_error_t *error)
1031 int ret;
1033 if (error)
1034 *error = NULL;
1036 db->ret = 0;
1037 db->error = NULL;
1038 if (db->set_keys != NULL)
1039 heim_dict_iterate_f(db->set_keys, db, db_replay_log_set_keys_iter);
1040 if (db->del_keys != NULL)
1041 heim_dict_iterate_f(db->del_keys, db, db_replay_log_del_keys_iter);
1043 ret = db->ret;
1044 db->ret = 0;
1045 if (error && db->error) {
1046 *error = db->error;
1047 db->error = NULL;
1048 } else {
1049 heim_release(db->error);
1050 db->error = NULL;
1052 return ret;
1055 static int
1056 db_replay_log(heim_db_t db, heim_error_t *error)
1058 int ret;
1059 heim_string_t journal_fname = NULL;
1060 heim_object_t journal;
1061 size_t len;
1063 heim_assert(!db->in_transaction, "DB transaction not open");
1064 heim_assert(db->set_keys == NULL && db->set_keys == NULL, "DB transaction not open");
1066 if (error)
1067 *error = NULL;
1069 if (db->options == NULL)
1070 return 0;
1072 journal_fname = heim_dict_get_value(db->options, HSTR("journal-filename"));
1073 if (journal_fname == NULL)
1074 return 0;
1076 ret = read_json(heim_string_get_utf8(journal_fname), &journal, error);
1077 if (ret == ENOENT) {
1078 heim_release(journal_fname);
1079 return 0;
1081 if (ret == 0 && journal == NULL) {
1082 heim_release(journal_fname);
1083 return 0;
1085 if (ret != 0) {
1086 heim_release(journal_fname);
1087 return ret;
1090 if (heim_get_tid(journal) != HEIM_TID_ARRAY) {
1091 heim_release(journal_fname);
1092 return HEIM_ERROR(error, EINVAL,
1093 (ret, N_("Invalid journal contents; delete journal",
1094 "")));
1097 len = heim_array_get_length(journal);
1099 if (len > 0)
1100 db->set_keys = heim_array_get_value(journal, 0);
1101 if (len > 1)
1102 db->del_keys = heim_array_get_value(journal, 1);
1103 ret = db_do_log_actions(db, error);
1104 if (ret) {
1105 heim_release(journal_fname);
1106 return ret;
1109 /* Truncate replay log and we're done */
1110 ret = open_file(heim_string_get_utf8(journal_fname), 1, 0, NULL, error);
1111 heim_release(journal_fname);
1112 if (ret)
1113 return ret;
1114 heim_release(db->set_keys);
1115 heim_release(db->del_keys);
1116 db->set_keys = NULL;
1117 db->del_keys = NULL;
1119 return 0;
1122 static
1123 heim_string_t to_base64(heim_data_t data, heim_error_t *error)
1125 char *b64 = NULL;
1126 heim_string_t s = NULL;
1127 const heim_octet_string *d;
1128 int ret;
1130 d = heim_data_get_data(data);
1131 ret = rk_base64_encode(d->data, d->length, &b64);
1132 if (ret < 0 || b64 == NULL)
1133 goto enomem;
1134 s = heim_string_ref_create(b64, free);
1135 if (s == NULL)
1136 goto enomem;
1137 return s;
1139 enomem:
1140 free(b64);
1141 if (error)
1142 *error = heim_error_create_enomem();
1143 return NULL;
1146 static
1147 heim_data_t from_base64(heim_string_t s, heim_error_t *error)
1149 ssize_t len = -1;
1150 void *buf;
1151 heim_data_t d;
1153 buf = malloc(strlen(heim_string_get_utf8(s)));
1154 if (buf)
1155 len = rk_base64_decode(heim_string_get_utf8(s), buf);
1156 if (len > -1 && (d = heim_data_ref_create(buf, len, free)))
1157 return d;
1158 free(buf);
1159 if (error)
1160 *error = heim_error_create_enomem();
1161 return NULL;
1165 static int
1166 open_file(const char *dbname, int for_write, int excl, int *fd_out, heim_error_t *error)
1168 #ifdef WIN32
1169 HANDLE hFile;
1170 int ret = 0;
1172 if (fd_out)
1173 *fd_out = -1;
1175 if (for_write)
1176 hFile = CreateFile(dbname, GENERIC_WRITE | GENERIC_READ, 0,
1177 NULL, /* we'll close as soon as we read */
1178 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1179 else
1180 hFile = CreateFile(dbname, GENERIC_READ, FILE_SHARE_READ,
1181 NULL, /* we'll close as soon as we read */
1182 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1183 if (hFile == INVALID_HANDLE_VALUE) {
1184 ret = GetLastError();
1185 _set_errno(ret); /* CreateFile() does not set errno */
1186 goto err;
1188 if (fd_out == NULL) {
1189 (void) CloseHandle(hFile);
1190 return 0;
1193 *fd_out = _open_osfhandle((intptr_t) hFile, 0);
1194 if (*fd_out < 0) {
1195 ret = errno;
1196 (void) CloseHandle(hFile);
1197 goto err;
1200 /* No need to lock given share deny mode */
1201 return 0;
1203 err:
1204 if (error != NULL) {
1205 char *s = NULL;
1206 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
1207 0, ret, 0, (LPTSTR) &s, 0, NULL);
1208 *error = heim_error_create(ret, N_("Could not open JSON file %s: %s", ""),
1209 dbname, s ? s : "<error formatting error>");
1210 LocalFree(s);
1212 return ret;
1213 #else
1214 int ret = 0;
1215 int fd;
1217 if (fd_out)
1218 *fd_out = -1;
1220 if (for_write && excl)
1221 fd = open(dbname, O_CREAT | O_EXCL | O_WRONLY, 0600);
1222 else if (for_write)
1223 fd = open(dbname, O_CREAT | O_TRUNC | O_WRONLY, 0600);
1224 else
1225 fd = open(dbname, O_RDONLY);
1226 if (fd < 0) {
1227 if (error != NULL)
1228 *error = heim_error_create(ret, N_("Could not open JSON file %s: %s", ""),
1229 dbname, strerror(errno));
1230 return errno;
1233 if (fd_out == NULL) {
1234 (void) close(fd);
1235 return 0;
1238 ret = flock(fd, for_write ? LOCK_EX : LOCK_SH);
1239 if (ret == -1) {
1240 /* Note that we if O_EXCL we're leaving the [lock] file around */
1241 (void) close(fd);
1242 return HEIM_ERROR(error, errno,
1243 (errno, N_("Could not lock JSON file %s: %s", ""),
1244 dbname, strerror(errno)));
1247 *fd_out = fd;
1249 return 0;
1250 #endif
1253 static int
1254 read_json(const char *dbname, heim_object_t *out, heim_error_t *error)
1256 struct stat st;
1257 char *str = NULL;
1258 int ret;
1259 int fd = -1;
1260 ssize_t bytes;
1262 *out = NULL;
1263 ret = open_file(dbname, 0, 0, &fd, error);
1264 if (ret)
1265 return ret;
1267 ret = fstat(fd, &st);
1268 if (ret == -1) {
1269 (void) close(fd);
1270 return HEIM_ERROR(error, errno,
1271 (ret, N_("Could not stat JSON DB %s: %s", ""),
1272 dbname, strerror(errno)));
1275 if (st.st_size == 0) {
1276 (void) close(fd);
1277 return 0;
1280 str = malloc(st.st_size + 1);
1281 if (str == NULL) {
1282 (void) close(fd);
1283 return HEIM_ENOMEM(error);
1286 bytes = read(fd, str, st.st_size);
1287 (void) close(fd);
1288 if (bytes != st.st_size) {
1289 free(str);
1290 if (bytes >= 0)
1291 errno = EINVAL; /* ?? */
1292 return HEIM_ERROR(error, errno,
1293 (ret, N_("Could not read JSON DB %s: %s", ""),
1294 dbname, strerror(errno)));
1296 str[st.st_size] = '\0';
1297 *out = heim_json_create(str, 10, 0, error);
1298 free(str);
1299 if (*out == NULL)
1300 return (error && *error) ? heim_error_get_code(*error) : EINVAL;
1301 return 0;
1304 typedef struct json_db {
1305 heim_dict_t dict;
1306 heim_string_t dbname;
1307 heim_string_t bkpname;
1308 int fd;
1309 time_t last_read_time;
1310 unsigned int read_only:1;
1311 unsigned int locked:1;
1312 unsigned int locked_needs_unlink:1;
1313 } *json_db_t;
1315 static int
1316 json_db_open(void *plug, const char *dbtype, const char *dbname,
1317 heim_dict_t options, void **db, heim_error_t *error)
1319 json_db_t jsondb;
1320 heim_dict_t contents = NULL;
1321 heim_string_t dbname_s = NULL;
1322 heim_string_t bkpname_s = NULL;
1324 if (error)
1325 *error = NULL;
1326 if (dbtype && *dbtype && strcmp(dbtype, "json") != 0)
1327 return HEIM_ERROR(error, EINVAL, (EINVAL, N_("Wrong DB type", "")));
1328 if (dbname && *dbname && strcmp(dbname, "MEMORY") != 0) {
1329 char *ext = strrchr(dbname, '.');
1330 char *bkpname;
1331 size_t len;
1332 int ret;
1334 if (ext == NULL || strcmp(ext, ".json") != 0)
1335 return HEIM_ERROR(error, EINVAL,
1336 (EINVAL, N_("JSON DB files must end in .json",
1337 "")));
1339 if (options) {
1340 heim_object_t vc, ve, vt;
1342 vc = heim_dict_get_value(options, HSTR("create"));
1343 ve = heim_dict_get_value(options, HSTR("exclusive"));
1344 vt = heim_dict_get_value(options, HSTR("truncate"));
1345 if (vc && vt) {
1346 ret = open_file(dbname, 1, ve ? 1 : 0, NULL, error);
1347 if (ret)
1348 return ret;
1349 } else if (vc || ve || vt) {
1350 return HEIM_ERROR(error, EINVAL,
1351 (EINVAL, N_("Invalid JSON DB open options",
1352 "")));
1355 * We don't want cloned handles to truncate the DB, eh?
1357 * We should really just create a copy of the options dict
1358 * rather than modify the caller's! But for that it'd be
1359 * nicer to have copy utilities in heimbase, something like
1360 * this:
1362 * heim_object_t heim_copy(heim_object_t src, int depth,
1363 * heim_error_t *error);
1365 * so that options = heim_copy(options, 1); means copy the
1366 * dict but nothing else (whereas depth == 0 would mean
1367 * heim_retain(), and depth > 1 would be copy that many
1368 * levels).
1370 heim_dict_delete_key(options, HSTR("create"));
1371 heim_dict_delete_key(options, HSTR("exclusive"));
1372 heim_dict_delete_key(options, HSTR("truncate"));
1374 dbname_s = heim_string_create(dbname);
1375 if (dbname_s == NULL)
1376 return HEIM_ENOMEM(error);
1378 len = snprintf(NULL, 0, "%s~", dbname);
1379 bkpname = malloc(len + 2);
1380 if (bkpname == NULL) {
1381 heim_release(dbname_s);
1382 return HEIM_ENOMEM(error);
1384 (void) snprintf(bkpname, len + 1, "%s~", dbname);
1385 bkpname_s = heim_string_create(bkpname);
1386 free(bkpname);
1387 if (bkpname_s == NULL) {
1388 heim_release(dbname_s);
1389 return HEIM_ENOMEM(error);
1392 ret = read_json(dbname, (heim_object_t *)&contents, error);
1393 if (ret) {
1394 heim_release(bkpname_s);
1395 heim_release(dbname_s);
1396 return ret;
1399 if (contents != NULL && heim_get_tid(contents) != HEIM_TID_DICT) {
1400 heim_release(bkpname_s);
1401 heim_release(dbname_s);
1402 return HEIM_ERROR(error, EINVAL,
1403 (EINVAL, N_("JSON DB contents not valid JSON",
1404 "")));
1408 jsondb = heim_alloc(sizeof (*jsondb), "json_db", NULL);
1409 if (jsondb == NULL) {
1410 heim_release(contents);
1411 heim_release(dbname_s);
1412 heim_release(bkpname_s);
1413 return ENOMEM;
1416 jsondb->last_read_time = time(NULL);
1417 jsondb->fd = -1;
1418 jsondb->dbname = dbname_s;
1419 jsondb->bkpname = bkpname_s;
1420 jsondb->read_only = 0;
1422 if (contents != NULL)
1423 jsondb->dict = contents;
1424 else {
1425 jsondb->dict = heim_dict_create(29);
1426 if (jsondb->dict == NULL) {
1427 heim_release(jsondb);
1428 return ENOMEM;
1432 *db = jsondb;
1433 return 0;
1436 static int
1437 json_db_close(void *db, heim_error_t *error)
1439 json_db_t jsondb = db;
1441 if (error)
1442 *error = NULL;
1443 if (jsondb->fd > -1)
1444 (void) close(jsondb->fd);
1445 jsondb->fd = -1;
1446 heim_release(jsondb->dbname);
1447 heim_release(jsondb->bkpname);
1448 heim_release(jsondb->dict);
1449 heim_release(jsondb);
1450 return 0;
1453 static int
1454 json_db_lock(void *db, int read_only, heim_error_t *error)
1456 json_db_t jsondb = db;
1457 int ret;
1459 heim_assert(jsondb->fd == -1 || (jsondb->read_only && !read_only),
1460 "DB locks are not recursive");
1462 jsondb->read_only = read_only ? 1 : 0;
1463 if (jsondb->fd > -1)
1464 return 0;
1466 ret = open_file(heim_string_get_utf8(jsondb->bkpname), 1, 1, &jsondb->fd, error);
1467 if (ret == 0) {
1468 jsondb->locked_needs_unlink = 1;
1469 jsondb->locked = 1;
1471 return ret;
1474 static int
1475 json_db_unlock(void *db, heim_error_t *error)
1477 json_db_t jsondb = db;
1478 int ret = 0;
1480 heim_assert(jsondb->locked, "DB not locked when unlock attempted");
1481 if (jsondb->fd > -1)
1482 ret = close(jsondb->fd);
1483 jsondb->fd = -1;
1484 jsondb->read_only = 0;
1485 jsondb->locked = 0;
1486 if (jsondb->locked_needs_unlink)
1487 unlink(heim_string_get_utf8(jsondb->bkpname));
1488 jsondb->locked_needs_unlink = 0;
1489 return ret;
1492 static int
1493 json_db_sync(void *db, heim_error_t *error)
1495 json_db_t jsondb = db;
1496 size_t len, bytes;
1497 heim_error_t e;
1498 heim_string_t json;
1499 const char *json_text = NULL;
1500 int ret = 0;
1501 int fd = -1;
1502 #ifdef WIN32
1503 int tries = 3;
1504 #endif
1506 heim_assert(jsondb->fd > -1, "DB not locked when sync attempted");
1508 json = heim_json_copy_serialize(jsondb->dict, 0, &e);
1509 if (json == NULL) {
1510 ret = heim_error_get_code(e);
1511 if (error)
1512 *error = e;
1513 else
1514 heim_release(e);
1515 return ret;
1518 json_text = heim_string_get_utf8(json);
1519 len = strlen(json_text);
1520 errno = 0;
1522 #ifdef WIN32
1523 while (tries--) {
1524 ret = open_file(heim_string_get_utf8(jsondb->dbname), 1, 0, &fd, error);
1525 if (ret == 0)
1526 break;
1527 sleep(1);
1529 if (ret) {
1530 heim_release(json);
1531 return ret;
1533 #else
1534 fd = jsondb->fd;
1535 #endif /* WIN32 */
1537 bytes = write(fd, json_text, len);
1538 heim_release(json);
1539 if (bytes != len)
1540 return errno ? errno : EIO;
1541 ret = fsync(fd);
1542 if (ret)
1543 return ret;
1545 #ifdef WIN32
1546 ret = close(fd);
1547 if (ret)
1548 return GetLastError();
1549 #else
1550 ret = rename(heim_string_get_utf8(jsondb->bkpname), heim_string_get_utf8(jsondb->dbname));
1551 if (ret == 0) {
1552 jsondb->locked_needs_unlink = 0;
1553 return 0;
1555 #endif /* WIN32 */
1557 return errno;
1560 static heim_data_t
1561 json_db_copy_value(void *db, heim_string_t table, heim_data_t key,
1562 heim_error_t *error)
1564 json_db_t jsondb = db;
1565 heim_string_t key_string;
1566 const heim_octet_string *key_data = heim_data_get_data(key);
1567 struct stat st;
1568 heim_data_t result;
1570 if (error)
1571 *error = NULL;
1573 if (strnlen(key_data->data, key_data->length) != key_data->length) {
1574 HEIM_ERROR(error, EINVAL,
1575 (EINVAL, N_("JSON DB requires keys that are actually "
1576 "strings", "")));
1577 return NULL;
1580 if (stat(heim_string_get_utf8(jsondb->dbname), &st) == -1) {
1581 HEIM_ERROR(error, errno,
1582 (errno, N_("Could not stat JSON DB file", "")));
1583 return NULL;
1586 if (st.st_mtime > jsondb->last_read_time ||
1587 st.st_ctime > jsondb->last_read_time) {
1588 heim_dict_t contents = NULL;
1589 int ret;
1591 /* Ignore file is gone (ENOENT) */
1592 ret = read_json(heim_string_get_utf8(jsondb->dbname),
1593 (heim_object_t *)&contents, error);
1594 if (ret)
1595 return NULL;
1596 if (contents == NULL)
1597 contents = heim_dict_create(29);
1598 heim_release(jsondb->dict);
1599 jsondb->dict = contents;
1600 jsondb->last_read_time = time(NULL);
1603 key_string = heim_string_create_with_bytes(key_data->data,
1604 key_data->length);
1605 if (key_string == NULL) {
1606 (void) HEIM_ENOMEM(error);
1607 return NULL;
1610 result = heim_path_copy(jsondb->dict, error, table, key_string, NULL);
1611 heim_release(key_string);
1612 return result;
1615 static int
1616 json_db_set_value(void *db, heim_string_t table,
1617 heim_data_t key, heim_data_t value, heim_error_t *error)
1619 json_db_t jsondb = db;
1620 heim_string_t key_string;
1621 const heim_octet_string *key_data = heim_data_get_data(key);
1622 int ret;
1624 if (error)
1625 *error = NULL;
1627 if (strnlen(key_data->data, key_data->length) != key_data->length)
1628 return HEIM_ERROR(error, EINVAL,
1629 (EINVAL,
1630 N_("JSON DB requires keys that are actually strings",
1631 "")));
1633 key_string = heim_string_create_with_bytes(key_data->data,
1634 key_data->length);
1635 if (key_string == NULL)
1636 return HEIM_ENOMEM(error);
1638 if (table == NULL)
1639 table = HSTR("");
1641 ret = heim_path_create(jsondb->dict, 29, value, error, table, key_string, NULL);
1642 heim_release(key_string);
1643 return ret;
1646 static int
1647 json_db_del_key(void *db, heim_string_t table, heim_data_t key,
1648 heim_error_t *error)
1650 json_db_t jsondb = db;
1651 heim_string_t key_string;
1652 const heim_octet_string *key_data = heim_data_get_data(key);
1654 if (error)
1655 *error = NULL;
1657 if (strnlen(key_data->data, key_data->length) != key_data->length)
1658 return HEIM_ERROR(error, EINVAL,
1659 (EINVAL,
1660 N_("JSON DB requires keys that are actually strings",
1661 "")));
1663 key_string = heim_string_create_with_bytes(key_data->data,
1664 key_data->length);
1665 if (key_string == NULL)
1666 return HEIM_ENOMEM(error);
1668 if (table == NULL)
1669 table = HSTR("");
1671 heim_path_delete(jsondb->dict, error, table, key_string, NULL);
1672 heim_release(key_string);
1673 return 0;
1676 struct json_db_iter_ctx {
1677 heim_db_iterator_f_t iter_f;
1678 void *iter_ctx;
1681 static void json_db_iter_f(heim_object_t key, heim_object_t value, void *arg)
1683 struct json_db_iter_ctx *ctx = arg;
1684 const char *key_string;
1685 heim_data_t key_data;
1687 key_string = heim_string_get_utf8((heim_string_t)key);
1688 key_data = heim_data_ref_create(key_string, strlen(key_string), NULL);
1689 ctx->iter_f(key_data, (heim_object_t)value, ctx->iter_ctx);
1690 heim_release(key_data);
1693 static void
1694 json_db_iter(void *db, heim_string_t table, void *iter_data,
1695 heim_db_iterator_f_t iter_f, heim_error_t *error)
1697 json_db_t jsondb = db;
1698 struct json_db_iter_ctx ctx;
1699 heim_dict_t table_dict;
1701 if (error)
1702 *error = NULL;
1704 if (table == NULL)
1705 table = HSTR("");
1707 table_dict = heim_dict_get_value(jsondb->dict, table);
1708 if (table_dict == NULL)
1709 return;
1711 ctx.iter_ctx = iter_data;
1712 ctx.iter_f = iter_f;
1714 heim_dict_iterate_f(table_dict, &ctx, json_db_iter_f);
1717 static struct heim_db_type json_dbt = {
1718 1, json_db_open, NULL, json_db_close,
1719 json_db_lock, json_db_unlock, json_db_sync,
1720 NULL, NULL, NULL,
1721 json_db_copy_value, json_db_set_value,
1722 json_db_del_key, json_db_iter