Add comment about Cherry.
[gnupg.git] / kbx / keybox-init.c
blobe4138647e763d89399f37c6d280da5d0468e872d
1 /* keybox-init.c - Initalization of the library
2 * Copyright (C) 2001 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <assert.h>
27 #include "../jnlib/mischelp.h"
28 #include "keybox-defs.h"
30 static KB_NAME kb_names;
33 /* Register a filename for plain keybox files. Returns a pointer to
34 be used to create a handles and so on. Returns NULL to indicate
35 that FNAME has already been registered. */
36 void *
37 keybox_register_file (const char *fname, int secret)
39 KB_NAME kr;
41 for (kr=kb_names; kr; kr = kr->next)
43 if (same_file_p (kr->fname, fname) )
44 return NULL; /* Already registered. */
47 kr = xtrymalloc (sizeof *kr + strlen (fname));
48 if (!kr)
49 return NULL;
50 strcpy (kr->fname, fname);
51 kr->secret = !!secret;
53 kr->handle_table = NULL;
54 kr->handle_table_size = 0;
56 /* kr->lockhd = NULL;*/
57 kr->is_locked = 0;
58 kr->did_full_scan = 0;
59 /* keep a list of all issued pointers */
60 kr->next = kb_names;
61 kb_names = kr;
63 /* create the offset table the first time a function here is used */
64 /* if (!kb_offtbl) */
65 /* kb_offtbl = new_offset_hash_table (); */
67 return kr;
70 int
71 keybox_is_writable (void *token)
73 KB_NAME r = token;
75 return r? !access (r->fname, W_OK) : 0;
80 /* Create a new handle for the resource associated with TOKEN. SECRET
81 is just a cross-check.
83 The returned handle must be released using keybox_release (). */
84 KEYBOX_HANDLE
85 keybox_new (void *token, int secret)
87 KEYBOX_HANDLE hd;
88 KB_NAME resource = token;
89 int idx;
91 assert (resource && !resource->secret == !secret);
92 hd = xtrycalloc (1, sizeof *hd);
93 if (hd)
95 hd->kb = resource;
96 hd->secret = !!secret;
97 if (!resource->handle_table)
99 resource->handle_table_size = 3;
100 resource->handle_table = xtrycalloc (resource->handle_table_size,
101 sizeof *resource->handle_table);
102 if (!resource->handle_table)
104 resource->handle_table_size = 0;
105 xfree (hd);
106 return NULL;
109 for (idx=0; idx < resource->handle_table_size; idx++)
110 if (!resource->handle_table[idx])
112 resource->handle_table[idx] = hd;
113 break;
115 if (!(idx < resource->handle_table_size))
117 KEYBOX_HANDLE *tmptbl;
118 size_t newsize;
120 newsize = resource->handle_table_size + 5;
121 tmptbl = xtryrealloc (resource->handle_table,
122 newsize * sizeof (*tmptbl));
123 if (!tmptbl)
125 xfree (hd);
126 return NULL;
128 resource->handle_table = tmptbl;
129 resource->handle_table_size = newsize;
130 resource->handle_table[idx] = hd;
131 for (idx++; idx < resource->handle_table_size; idx++)
132 resource->handle_table[idx] = NULL;
135 return hd;
138 void
139 keybox_release (KEYBOX_HANDLE hd)
141 if (!hd)
142 return;
143 if (hd->kb->handle_table)
145 int idx;
146 for (idx=0; idx < hd->kb->handle_table_size; idx++)
147 if (hd->kb->handle_table[idx] == hd)
148 hd->kb->handle_table[idx] = NULL;
150 _keybox_release_blob (hd->found.blob);
151 if (hd->fp)
153 fclose (hd->fp);
154 hd->fp = NULL;
156 xfree (hd->word_match.name);
157 xfree (hd->word_match.pattern);
158 xfree (hd);
162 const char *
163 keybox_get_resource_name (KEYBOX_HANDLE hd)
165 if (!hd || !hd->kb)
166 return NULL;
167 return hd->kb->fname;
171 keybox_set_ephemeral (KEYBOX_HANDLE hd, int yes)
173 if (!hd)
174 return gpg_error (GPG_ERR_INV_HANDLE);
175 hd->ephemeral = yes;
176 return 0;
180 /* Close the file of the resource identified by HD. For consistent
181 results this fucntion closes the files of all handles pointing to
182 the resource identified by HD. */
183 void
184 _keybox_close_file (KEYBOX_HANDLE hd)
186 int idx;
187 KEYBOX_HANDLE roverhd;
189 if (!hd || !hd->kb || !hd->kb->handle_table)
190 return;
192 for (idx=0; idx < hd->kb->handle_table_size; idx++)
193 if ((roverhd = hd->kb->handle_table[idx]))
195 if (roverhd->fp)
197 fclose (roverhd->fp);
198 roverhd->fp = NULL;
201 assert (!hd->fp);