aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--g10/keydb.c20
-rw-r--r--kbx/keybox-update.c76
-rw-r--r--kbx/keybox.h2
-rw-r--r--sm/keydb.c18
4 files changed, 64 insertions, 52 deletions
diff --git a/g10/keydb.c b/g10/keydb.c
index f155d8e7a..3d95f511a 100644
--- a/g10/keydb.c
+++ b/g10/keydb.c
@@ -755,23 +755,9 @@ keydb_add_resource (const char *url, unsigned int flags)
all_resources[used_resources].token = token;
if (!(flags & KEYDB_RESOURCE_FLAG_READONLY))
- {
- KEYBOX_HANDLE kbxhd;
-
- /* Do a compress run if needed and no other user is
- * currently using the keybox. */
- kbxhd = keybox_new_openpgp (token, 0);
- if (kbxhd)
- {
- if (!keybox_lock (kbxhd, 1, 0))
- {
- keybox_compress (kbxhd);
- keybox_lock (kbxhd, 0, 0);
- }
-
- keybox_release (kbxhd);
- }
- }
+ /* Do a compress run if needed and no other user is
+ * currently using the keybox. */
+ keybox_compress_when_no_other_users (token, 1);
used_resources++;
}
}
diff --git a/kbx/keybox-update.c b/kbx/keybox-update.c
index be49e7b4a..a712a6bf2 100644
--- a/kbx/keybox-update.c
+++ b/kbx/keybox-update.c
@@ -624,12 +624,13 @@ keybox_delete (KEYBOX_HANDLE hd)
}
-/* Compress the keybox file. This should be run with the file
- locked. */
-int
-keybox_compress (KEYBOX_HANDLE hd)
+/* Compress the keybox file, if needed and not used by other
+ * process. */
+void
+keybox_compress_when_no_other_users (void *token, int for_openpgp)
{
- gpg_err_code_t ec;
+ KEYBOX_HANDLE hd;
+ gpg_error_t err;
int read_rc, rc, rc2;
const char *fname;
estream_t fp, newfp;
@@ -641,28 +642,49 @@ keybox_compress (KEYBOX_HANDLE hd)
int any_changes = 0;
int skipped_deleted;
- if (!hd)
- return gpg_error (GPG_ERR_INV_HANDLE);
- if (!hd->kb)
- return gpg_error (GPG_ERR_INV_HANDLE);
+ if (for_openpgp)
+ hd = keybox_new_openpgp (token, 0);
+ else
+ hd = keybox_new_x509 (token, 0);
+ if (!hd || !hd->kb)
+ return;
+
if (hd->secret)
- return gpg_error (GPG_ERR_NOT_IMPLEMENTED);
+ return;
fname = hd->kb->fname;
if (!fname)
- return gpg_error (GPG_ERR_INV_HANDLE);
+ {
+ keybox_release (hd);
+ return;
+ }
+
+ if (keybox_lock (hd, 1, 0))
+ {
+ keybox_release (hd);
+ return;
+ }
_keybox_close_file (hd);
/* Open the source file. Because we do a rename, we have to check the
permissions of the file */
- if ((ec = gnupg_access (fname, W_OK)))
- return gpg_error (ec);
+ if ((rc = gnupg_access (fname, W_OK)))
+ {
+ err = gpg_error (rc);
+ goto leave;
+ }
rc = _keybox_ll_open (&fp, fname, 0);
if (gpg_err_code (rc) == GPG_ERR_ENOENT)
- return 0; /* Ready. File has been deleted right after the access above. */
+ {
+ err = 0; /* Ready. File has been deleted right after the access above. */
+ goto leave;
+ }
if (rc)
- return rc;
+ {
+ err = gpg_error (rc);
+ goto leave;
+ }
/* A quick test to see if we need to compress the file at all. We
schedule a compress run after 3 hours. */
@@ -680,7 +702,8 @@ keybox_compress (KEYBOX_HANDLE hd)
{
_keybox_ll_close (fp);
_keybox_release_blob (blob);
- return 0; /* Compress run not yet needed. */
+ err = 0;
+ goto leave; /* Compress run not yet needed. */
}
}
_keybox_release_blob (blob);
@@ -693,7 +716,8 @@ keybox_compress (KEYBOX_HANDLE hd)
if (rc)
{
_keybox_ll_close (fp);
- return rc;;
+ err = gpg_error (rc);
+ goto leave;
}
@@ -747,7 +771,7 @@ keybox_compress (KEYBOX_HANDLE hd)
KEYBOX_FLAG_BLOB, &pos, &size)
|| size != 2)
{
- rc = gpg_error (GPG_ERR_BUG);
+ rc = GPG_ERR_BUG;
break;
}
blobflags = buf16_to_uint (buffer+pos);
@@ -794,5 +818,19 @@ keybox_compress (KEYBOX_HANDLE hd)
xfree(bakfname);
xfree(tmpfname);
- return rc;
+ if (rc)
+ err = gpg_error (rc);
+ else
+ err = 0;
+
+ if (err)
+ log_error ("keybox: error compressing keybox '%s': %s\n",
+ fname, gpg_strerror (err));
+
+ leave:
+ /* Here, we unlock before the release of HD. It's safe because
+ references to the resource are all closed. */
+ keybox_lock (hd, 0, 0);
+ keybox_release (hd);
+ return;
}
diff --git a/kbx/keybox.h b/kbx/keybox.h
index 5aa0de57d..2812c06df 100644
--- a/kbx/keybox.h
+++ b/kbx/keybox.h
@@ -119,7 +119,7 @@ int keybox_update_cert (KEYBOX_HANDLE hd, ksba_cert_t cert,
int keybox_set_flags (KEYBOX_HANDLE hd, int what, int idx, unsigned int value);
int keybox_delete (KEYBOX_HANDLE hd);
-int keybox_compress (KEYBOX_HANDLE hd);
+void keybox_compress_when_no_other_users (void *token, int for_openpgp);
/*-- --*/
diff --git a/sm/keydb.c b/sm/keydb.c
index 1f93268a8..651bbd968 100644
--- a/sm/keydb.c
+++ b/sm/keydb.c
@@ -447,25 +447,13 @@ keydb_add_resource (ctrl_t ctrl, const char *url, int force, int *auto_created)
err = gpg_error (GPG_ERR_RESOURCE_LIMIT);
else
{
- KEYBOX_HANDLE kbxhd;
-
all_resources[used_resources].type = rt;
all_resources[used_resources].u.kr = NULL; /* Not used here */
all_resources[used_resources].token = token;
- /* Do a compress run if needed and the keybox is not locked. */
- kbxhd = keybox_new_x509 (token, 0);
- if (kbxhd)
- {
- if (!keybox_lock (kbxhd, 1, 0))
- {
- keybox_compress (kbxhd);
- keybox_lock (kbxhd, 0, 0);
- }
-
- keybox_release (kbxhd);
- }
-
+ /* Do a compress run if needed and no other user is
+ * currently using the keybox. */
+ keybox_compress_when_no_other_users (token, 0);
used_resources++;
}
}