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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/* libguestfs
* Copyright (C) 2009-2020 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Implement a private data area where libguestfs C API users can
* attach arbitrary pieces of data to a C<guestfs_h> handle.
*
* For more information see L<guestfs(3)/PRIVATE DATA AREA>.
*
* Language bindings do not generally expose this, largely because in
* non-C languages it is easy to associate data with handles in other
* ways (using hash tables or maps).
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "hash.h"
#include "hash-pjw.h"
#include "guestfs.h"
#include "guestfs-internal.h"
/**
* The private data area is internally stored as a gnulib hash
* table containing C<pda_entry> structures.
*
* Note the private data area is allocated lazily, since the vast
* majority of callers will never use it. This means C<g-E<gt>pda> is
* likely to be C<NULL>.
*/
struct pda_entry {
char *key; /* key */
void *data; /* opaque user data pointer */
};
static size_t
hasher (void const *x, size_t table_size)
{
struct pda_entry const *p = x;
return hash_pjw (p->key, table_size);
}
static bool
comparator (void const *x, void const *y)
{
struct pda_entry const *a = x;
struct pda_entry const *b = y;
return STREQ (a->key, b->key);
}
static void
freer (void *x)
{
if (x) {
struct pda_entry *p = x;
free (p->key);
free (p);
}
}
void
guestfs_set_private (guestfs_h *g, const char *key, void *data)
{
ACQUIRE_LOCK_FOR_CURRENT_SCOPE (g);
struct pda_entry *new_entry, *old_entry, *entry;
if (g->pda == NULL) {
g->pda = hash_initialize (16, NULL, hasher, comparator, freer);
if (g->pda == NULL)
g->abort_cb ();
}
new_entry = safe_malloc (g, sizeof *new_entry);
new_entry->key = safe_strdup (g, key);
new_entry->data = data;
old_entry = hash_remove (g->pda, new_entry);
freer (old_entry);
entry = hash_insert (g->pda, new_entry);
if (entry == NULL)
g->abort_cb ();
assert (entry == new_entry);
}
void *
guestfs_get_private (guestfs_h *g, const char *key)
{
ACQUIRE_LOCK_FOR_CURRENT_SCOPE (g);
if (g->pda == NULL)
return NULL; /* no keys have been set */
const struct pda_entry k = { .key = (char *) key };
struct pda_entry *entry = hash_lookup (g->pda, &k);
if (entry)
return entry->data;
else
return NULL;
}
/* Iterator. */
void *
guestfs_first_private (guestfs_h *g, const char **key_rtn)
{
ACQUIRE_LOCK_FOR_CURRENT_SCOPE (g);
if (g->pda == NULL)
return NULL;
g->pda_next = hash_get_first (g->pda);
/* Ignore any keys with NULL data pointers. */
while (g->pda_next && g->pda_next->data == NULL)
g->pda_next = hash_get_next (g->pda, g->pda_next);
if (g->pda_next == NULL)
return NULL;
*key_rtn = g->pda_next->key;
return g->pda_next->data;
}
void *
guestfs_next_private (guestfs_h *g, const char **key_rtn)
{
ACQUIRE_LOCK_FOR_CURRENT_SCOPE (g);
if (g->pda == NULL)
return NULL;
if (g->pda_next == NULL)
return NULL;
/* Walk to the next key with a non-NULL data pointer. */
do {
g->pda_next = hash_get_next (g->pda, g->pda_next);
} while (g->pda_next && g->pda_next->data == NULL);
if (g->pda_next == NULL)
return NULL;
*key_rtn = g->pda_next->key;
return g->pda_next->data;
}
|