[evolution-data-server] CamelCertDB: Further cleanups.



commit 79281cd5f38a686d3bb699192d5a8fe0cb5cd990
Author: Matthew Barnes <mbarnes redhat com>
Date:   Sun Sep 22 11:08:15 2013 -0400

    CamelCertDB: Further cleanups.

 camel/camel-certdb.c                    |  345 +++++++++++++++----------------
 camel/camel-certdb.h                    |  117 +++++++----
 docs/reference/camel/camel-sections.txt |    8 +-
 3 files changed, 244 insertions(+), 226 deletions(-)
---
diff --git a/camel/camel-certdb.c b/camel/camel-certdb.c
index dc69448..13bedb9 100644
--- a/camel/camel-certdb.c
+++ b/camel/camel-certdb.c
@@ -57,13 +57,6 @@ struct _CamelCertDBPrivate {
        GMutex io_lock;         /* load/save lock, for access to saved_count, etc */
 };
 
-static gint certdb_header_load (CamelCertDB *certdb, FILE *istream);
-static gint certdb_header_save (CamelCertDB *certdb, FILE *ostream);
-static CamelCert *certdb_cert_load (CamelCertDB *certdb, FILE *istream);
-static gint certdb_cert_save (CamelCertDB *certdb, CamelCert *cert, FILE *ostream);
-static const gchar *cert_get_string (CamelCertDB *certdb, CamelCert *cert, gint string);
-static void cert_set_string (CamelCertDB *certdb, CamelCert *cert, gint string, const gchar *value);
-
 G_DEFINE_TYPE (CamelCertDB, camel_certdb, G_TYPE_OBJECT)
 
 typedef struct {
@@ -137,13 +130,12 @@ certdb_key_equal (gconstpointer ptr1,
 static void
 certdb_finalize (GObject *object)
 {
-       CamelCertDB *certdb = CAMEL_CERTDB (object);
        CamelCertDBPrivate *priv;
 
        priv = CAMEL_CERTDB_GET_PRIVATE (object);
 
-       if (certdb->priv->dirty)
-               camel_certdb_save (certdb);
+       if (priv->dirty)
+               camel_certdb_save (CAMEL_CERTDB (object));
 
        camel_certdb_clear (CAMEL_CERTDB (object));
        g_ptr_array_free (priv->certs, TRUE);
@@ -158,6 +150,131 @@ certdb_finalize (GObject *object)
        G_OBJECT_CLASS (camel_certdb_parent_class)->finalize (object);
 }
 
+static gint
+certdb_header_load (CamelCertDB *certdb,
+                    FILE *istream)
+{
+       if (camel_file_util_decode_uint32 (
+               istream, &certdb->priv->version) == -1)
+               return -1;
+       if (camel_file_util_decode_uint32 (
+               istream, &certdb->priv->saved_certs) == -1)
+               return -1;
+
+       return 0;
+}
+
+static gint
+certdb_header_save (CamelCertDB *certdb,
+                    FILE *ostream)
+{
+       if (camel_file_util_encode_uint32 (
+               ostream, certdb->priv->version) == -1)
+               return -1;
+       if (camel_file_util_encode_uint32 (
+               ostream, certdb->priv->saved_certs) == -1)
+               return -1;
+
+       return 0;
+}
+
+static CamelCert *
+certdb_cert_load (CamelCertDB *certdb,
+                  FILE *istream)
+{
+       CamelCert *cert;
+
+       cert = camel_cert_new ();
+
+       if (camel_file_util_decode_string (istream, &cert->issuer) == -1)
+               goto error;
+       if (camel_file_util_decode_string (istream, &cert->subject) == -1)
+               goto error;
+       if (camel_file_util_decode_string (istream, &cert->hostname) == -1)
+               goto error;
+       if (camel_file_util_decode_string (istream, &cert->fingerprint) == -1)
+               goto error;
+       if (camel_file_util_decode_uint32 (istream, &cert->trust) == -1)
+               goto error;
+
+       /* unset temporary trusts on load */
+       if (cert->trust == CAMEL_CERT_TRUST_TEMPORARY)
+               cert->trust = CAMEL_CERT_TRUST_UNKNOWN;
+
+       return cert;
+
+error:
+       camel_cert_unref (cert);
+
+       return NULL;
+}
+
+static gint
+certdb_cert_save (CamelCertDB *certdb,
+                  CamelCert *cert,
+                  FILE *ostream)
+{
+       if (camel_file_util_encode_string (ostream, cert->issuer) == -1)
+               return -1;
+       if (camel_file_util_encode_string (ostream, cert->subject) == -1)
+               return -1;
+       if (camel_file_util_encode_string (ostream, cert->hostname) == -1)
+               return -1;
+       if (camel_file_util_encode_string (ostream, cert->fingerprint) == -1)
+               return -1;
+       if (camel_file_util_encode_uint32 (ostream, cert->trust) == -1)
+               return -1;
+
+       return 0;
+}
+
+static const gchar *
+certdb_cert_get_string (CamelCertDB *certdb,
+                        CamelCert *cert,
+                        gint string)
+{
+       switch (string) {
+               case CAMEL_CERT_STRING_ISSUER:
+                       return cert->issuer;
+               case CAMEL_CERT_STRING_SUBJECT:
+                       return cert->subject;
+               case CAMEL_CERT_STRING_HOSTNAME:
+                       return cert->hostname;
+               case CAMEL_CERT_STRING_FINGERPRINT:
+                       return cert->fingerprint;
+               default:
+                       return NULL;
+       }
+}
+
+static void
+certdb_cert_set_string (CamelCertDB *certdb,
+                        CamelCert *cert,
+                        gint string,
+                        const gchar *value)
+{
+       switch (string) {
+               case CAMEL_CERT_STRING_ISSUER:
+                       g_free (cert->issuer);
+                       cert->issuer = g_strdup (value);
+                       break;
+               case CAMEL_CERT_STRING_SUBJECT:
+                       g_free (cert->subject);
+                       cert->subject = g_strdup (value);
+                       break;
+               case CAMEL_CERT_STRING_HOSTNAME:
+                       g_free (cert->hostname);
+                       cert->hostname = g_strdup (value);
+                       break;
+               case CAMEL_CERT_STRING_FINGERPRINT:
+                       g_free (cert->fingerprint);
+                       cert->fingerprint = g_strdup (value);
+                       break;
+               default:
+                       break;
+       }
+}
+
 static void
 camel_certdb_class_init (CamelCertDBClass *class)
 {
@@ -170,11 +287,10 @@ camel_certdb_class_init (CamelCertDBClass *class)
 
        class->header_load = certdb_header_load;
        class->header_save = certdb_header_save;
-
        class->cert_load = certdb_cert_load;
        class->cert_save = certdb_cert_save;
-       class->cert_get_string = cert_get_string;
-       class->cert_set_string = cert_set_string;
+       class->cert_get_string = certdb_cert_get_string;
+       class->cert_set_string = certdb_cert_set_string;
 }
 
 static void
@@ -195,6 +311,45 @@ camel_certdb_init (CamelCertDB *certdb)
        g_mutex_init (&certdb->priv->io_lock);
 }
 
+CamelCert *
+camel_cert_new (void)
+{
+       CamelCert *cert;
+
+       cert = g_slice_new0 (CamelCert);
+       cert->refcount = 1;
+
+       return cert;
+}
+
+void
+camel_cert_ref (CamelCert *cert)
+{
+       g_return_if_fail (cert != NULL);
+       g_return_if_fail (cert->refcount > 0);
+
+       g_atomic_int_inc (&cert->refcount);
+}
+
+void
+camel_cert_unref (CamelCert *cert)
+{
+       g_return_if_fail (cert != NULL);
+       g_return_if_fail (cert->refcount > 0);
+
+       if (g_atomic_int_dec_and_test (&cert->refcount)) {
+               g_free (cert->issuer);
+               g_free (cert->subject);
+               g_free (cert->hostname);
+               g_free (cert->fingerprint);
+
+               if (cert->rawcert)
+                       g_byte_array_free (cert->rawcert, TRUE);
+
+               g_slice_free (CamelCert, cert);
+       }
+}
+
 CamelCertDB *
 camel_certdb_new (void)
 {
@@ -252,51 +407,6 @@ camel_certdb_set_filename (CamelCertDB *certdb,
        g_mutex_unlock (&certdb->priv->db_lock);
 }
 
-static gint
-certdb_header_load (CamelCertDB *certdb,
-                    FILE *istream)
-{
-       if (camel_file_util_decode_uint32 (
-               istream, &certdb->priv->version) == -1)
-               return -1;
-       if (camel_file_util_decode_uint32 (
-               istream, &certdb->priv->saved_certs) == -1)
-               return -1;
-
-       return 0;
-}
-
-static CamelCert *
-certdb_cert_load (CamelCertDB *certdb,
-                  FILE *istream)
-{
-       CamelCert *cert;
-
-       cert = camel_cert_new ();
-
-       if (camel_file_util_decode_string (istream, &cert->issuer) == -1)
-               goto error;
-       if (camel_file_util_decode_string (istream, &cert->subject) == -1)
-               goto error;
-       if (camel_file_util_decode_string (istream, &cert->hostname) == -1)
-               goto error;
-       if (camel_file_util_decode_string (istream, &cert->fingerprint) == -1)
-               goto error;
-       if (camel_file_util_decode_uint32 (istream, &cert->trust) == -1)
-               goto error;
-
-       /* unset temporary trusts on load */
-       if (cert->trust == CAMEL_CERT_TRUST_TEMPORARY)
-               cert->trust = CAMEL_CERT_TRUST_UNKNOWN;
-
-       return cert;
-
-error:
-       camel_cert_unref (cert);
-
-       return NULL;
-}
-
 gint
 camel_certdb_load (CamelCertDB *certdb)
 {
@@ -359,39 +469,6 @@ camel_certdb_load (CamelCertDB *certdb)
        return -1;
 }
 
-static gint
-certdb_header_save (CamelCertDB *certdb,
-                    FILE *ostream)
-{
-       if (camel_file_util_encode_uint32 (
-               ostream, certdb->priv->version) == -1)
-               return -1;
-       if (camel_file_util_encode_uint32 (
-               ostream, certdb->priv->saved_certs) == -1)
-               return -1;
-
-       return 0;
-}
-
-static gint
-certdb_cert_save (CamelCertDB *certdb,
-                  CamelCert *cert,
-                  FILE *ostream)
-{
-       if (camel_file_util_encode_string (ostream, cert->issuer) == -1)
-               return -1;
-       if (camel_file_util_encode_string (ostream, cert->subject) == -1)
-               return -1;
-       if (camel_file_util_encode_string (ostream, cert->hostname) == -1)
-               return -1;
-       if (camel_file_util_encode_string (ostream, cert->fingerprint) == -1)
-               return -1;
-       if (camel_file_util_encode_uint32 (ostream, cert->trust) == -1)
-               return -1;
-
-       return 0;
-}
-
 gint
 camel_certdb_save (CamelCertDB *certdb)
 {
@@ -599,45 +676,6 @@ camel_certdb_remove_host (CamelCertDB *certdb,
        g_mutex_unlock (&certdb->priv->db_lock);
 }
 
-CamelCert *
-camel_cert_new (void)
-{
-       CamelCert *cert;
-
-       cert = g_slice_new0 (CamelCert);
-       cert->refcount = 1;
-
-       return cert;
-}
-
-void
-camel_cert_ref (CamelCert *cert)
-{
-       g_return_if_fail (cert != NULL);
-       g_return_if_fail (cert->refcount > 0);
-
-       g_atomic_int_inc (&cert->refcount);
-}
-
-void
-camel_cert_unref (CamelCert *cert)
-{
-       g_return_if_fail (cert != NULL);
-       g_return_if_fail (cert->refcount > 0);
-
-       if (g_atomic_int_dec_and_test (&cert->refcount)) {
-               g_free (cert->issuer);
-               g_free (cert->subject);
-               g_free (cert->hostname);
-               g_free (cert->fingerprint);
-
-               if (cert->rawcert)
-                       g_byte_array_free (cert->rawcert, TRUE);
-
-               g_slice_free (CamelCert, cert);
-       }
-}
-
 static gboolean
 cert_remove (gpointer key,
              gpointer value,
@@ -669,25 +707,6 @@ camel_certdb_clear (CamelCertDB *certdb)
        g_mutex_unlock (&certdb->priv->db_lock);
 }
 
-static const gchar *
-cert_get_string (CamelCertDB *certdb,
-                 CamelCert *cert,
-                 gint string)
-{
-       switch (string) {
-       case CAMEL_CERT_STRING_ISSUER:
-               return cert->issuer;
-       case CAMEL_CERT_STRING_SUBJECT:
-               return cert->subject;
-       case CAMEL_CERT_STRING_HOSTNAME:
-               return cert->hostname;
-       case CAMEL_CERT_STRING_FINGERPRINT:
-               return cert->fingerprint;
-       default:
-               return NULL;
-       }
-}
-
 const gchar *
 camel_cert_get_string (CamelCertDB *certdb,
                        CamelCert *cert,
@@ -706,34 +725,6 @@ camel_cert_get_string (CamelCertDB *certdb,
        return class->cert_get_string (certdb, cert, string);
 }
 
-static void
-cert_set_string (CamelCertDB *certdb,
-                 CamelCert *cert,
-                 gint string,
-                 const gchar *value)
-{
-       switch (string) {
-       case CAMEL_CERT_STRING_ISSUER:
-               g_free (cert->issuer);
-               cert->issuer = g_strdup (value);
-               break;
-       case CAMEL_CERT_STRING_SUBJECT:
-               g_free (cert->subject);
-               cert->subject = g_strdup (value);
-               break;
-       case CAMEL_CERT_STRING_HOSTNAME:
-               g_free (cert->hostname);
-               cert->hostname = g_strdup (value);
-               break;
-       case CAMEL_CERT_STRING_FINGERPRINT:
-               g_free (cert->fingerprint);
-               cert->fingerprint = g_strdup (value);
-               break;
-       default:
-               break;
-       }
-}
-
 void
 camel_cert_set_string (CamelCertDB *certdb,
                        CamelCert *cert,
diff --git a/camel/camel-certdb.h b/camel/camel-certdb.h
index f17f680..4c7ab6b 100644
--- a/camel/camel-certdb.h
+++ b/camel/camel-certdb.h
@@ -91,63 +91,90 @@ struct _CamelCertDB {
 struct _CamelCertDBClass {
        GObjectClass parent_class;
 
-       gint (*header_load) (CamelCertDB *certdb, FILE *istream);
-       gint (*header_save) (CamelCertDB *certdb, FILE *ostream);
-
-       CamelCert * (*cert_load) (CamelCertDB *certdb, FILE *istream);
-       gint (*cert_save) (CamelCertDB *certdb, CamelCert *cert, FILE *ostream);
-
-       const gchar * (*cert_get_string) (CamelCertDB *certdb, CamelCert *cert, gint string);
-       void (*cert_set_string) (CamelCertDB *certdb, CamelCert *cert, gint string, const gchar *value);
+       gint            (*header_load)          (CamelCertDB *certdb,
+                                                FILE *istream);
+       gint            (*header_save)          (CamelCertDB *certdb,
+                                                FILE *ostream);
+
+       CamelCert *     (*cert_load)            (CamelCertDB *certdb,
+                                                FILE *istream);
+       gint            (*cert_save)            (CamelCertDB *certdb,
+                                                CamelCert *cert,
+                                                FILE *ostream);
+
+       const gchar *   (*cert_get_string)      (CamelCertDB *certdb,
+                                                CamelCert *cert,
+                                                gint string);
+       void            (*cert_set_string)      (CamelCertDB *certdb,
+                                                CamelCert *cert,
+                                                gint string,
+                                                const gchar *value);
 };
 
-GType camel_certdb_get_type (void);
-
-CamelCertDB *camel_certdb_new (void);
-
-void camel_certdb_set_default (CamelCertDB *certdb);
-CamelCertDB *camel_certdb_get_default (void);
-
-void camel_certdb_set_filename (CamelCertDB *certdb, const gchar *filename);
-
-gint camel_certdb_load (CamelCertDB *certdb);
-gint camel_certdb_save (CamelCertDB *certdb);
+CamelCert *    camel_cert_new                  (void);
+void           camel_cert_ref                  (CamelCert *cert);
+void           camel_cert_unref                (CamelCert *cert);
 
-void camel_certdb_touch (CamelCertDB *certdb);
+GType          camel_certdb_get_type           (void) G_GNUC_CONST;
+CamelCertDB *  camel_certdb_new                (void);
+void           camel_certdb_set_default        (CamelCertDB *certdb);
+CamelCertDB *  camel_certdb_get_default        (void);
+void           camel_certdb_set_filename       (CamelCertDB *certdb,
+                                                const gchar *filename);
+gint           camel_certdb_load               (CamelCertDB *certdb);
+gint           camel_certdb_save               (CamelCertDB *certdb);
+void           camel_certdb_touch              (CamelCertDB *certdb);
 
 /* The lookup key was changed from fingerprint to hostname to fix bug 606181. */
 
 /* Get the certificate for the given hostname, if any. */
-CamelCert *camel_certdb_get_host (CamelCertDB *certdb, const gchar *hostname, const gchar *fingerprint);
+CamelCert *    camel_certdb_get_host           (CamelCertDB *certdb,
+                                                const gchar *hostname,
+                                                const gchar *fingerprint);
 
 /* Store cert for cert->hostname, replacing any existing certificate for the
  * same hostname. */
-void camel_certdb_put (CamelCertDB *certdb, CamelCert *cert);
+void           camel_certdb_put                (CamelCertDB *certdb,
+                                                CamelCert *cert);
 
 /* Remove any user-accepted certificate for the given hostname. */
-void camel_certdb_remove_host (CamelCertDB *certdb, const gchar *hostname, const gchar *fingerprint);
-
-CamelCert *camel_cert_new (void);
-void camel_cert_ref (CamelCert *cert);
-void camel_cert_unref (CamelCert *cert);
-
-void camel_certdb_clear (CamelCertDB *certdb);
-
-const gchar *camel_cert_get_string (CamelCertDB *certdb, CamelCert *cert, gint string);
-void camel_cert_set_string (CamelCertDB *certdb, CamelCert *cert, gint string, const gchar *value);
-
-#define camel_cert_get_issuer(certdb,cert) camel_cert_get_string (certdb, cert, CAMEL_CERT_STRING_ISSUER)
-#define camel_cert_get_subject(certdb,cert) camel_cert_get_string (certdb, cert, CAMEL_CERT_STRING_SUBJECT)
-#define camel_cert_get_hostname(certdb,cert) camel_cert_get_string (certdb, cert, CAMEL_CERT_STRING_HOSTNAME)
-#define camel_cert_get_fingerprint(certdb,cert) camel_cert_get_string (certdb, cert, 
CAMEL_CERT_STRING_FINGERPRINT)
-
-#define camel_cert_set_issuer(certdb,cert,issuer) camel_cert_set_string (certdb, cert, 
CAMEL_CERT_STRING_ISSUER, issuer)
-#define camel_cert_set_subject(certdb,cert,subject) camel_cert_set_string (certdb, cert, 
CAMEL_CERT_STRING_SUBJECT, subject)
-#define camel_cert_set_hostname(certdb,cert,hostname) camel_cert_set_string (certdb, cert, 
CAMEL_CERT_STRING_HOSTNAME, hostname)
-#define camel_cert_set_fingerprint(certdb,cert,fingerprint) camel_cert_set_string (certdb, cert, 
CAMEL_CERT_STRING_FINGERPRINT, fingerprint)
-
-CamelCertTrust camel_cert_get_trust (CamelCertDB *certdb, CamelCert *cert);
-void camel_cert_set_trust (CamelCertDB *certdb, CamelCert *cert, CamelCertTrust trust);
+void           camel_certdb_remove_host        (CamelCertDB *certdb,
+                                                const gchar *hostname,
+                                                const gchar *fingerprint);
+
+void           camel_certdb_clear              (CamelCertDB *certdb);
+
+const gchar *  camel_cert_get_string           (CamelCertDB *certdb,
+                                                CamelCert *cert,
+                                                gint string);
+void           camel_cert_set_string           (CamelCertDB *certdb,
+                                                CamelCert *cert,
+                                                gint string,
+                                                const gchar *value);
+
+#define camel_cert_get_issuer(certdb, cert) \
+       camel_cert_get_string (certdb, cert, CAMEL_CERT_STRING_ISSUER)
+#define camel_cert_get_subject(certdb, cert) \
+       camel_cert_get_string (certdb, cert, CAMEL_CERT_STRING_SUBJECT)
+#define camel_cert_get_hostname(certdb, cert) \
+       camel_cert_get_string (certdb, cert, CAMEL_CERT_STRING_HOSTNAME)
+#define camel_cert_get_fingerprint(certdb, cert) \
+       camel_cert_get_string (certdb, cert, CAMEL_CERT_STRING_FINGERPRINT)
+
+#define camel_cert_set_issuer(certdb, cert, issuer) \
+       camel_cert_set_string (certdb, cert, CAMEL_CERT_STRING_ISSUER, issuer)
+#define camel_cert_set_subject(certdb, cert, subject) \
+       camel_cert_set_string (certdb, cert, CAMEL_CERT_STRING_SUBJECT, subject)
+#define camel_cert_set_hostname(certdb, cert, hostname) \
+       camel_cert_set_string (certdb, cert, CAMEL_CERT_STRING_HOSTNAME, hostname)
+#define camel_cert_set_fingerprint(certdb, cert, fingerprint) \
+       camel_cert_set_string (certdb, cert, CAMEL_CERT_STRING_FINGERPRINT, fingerprint)
+
+CamelCertTrust camel_cert_get_trust            (CamelCertDB *certdb,
+                                                CamelCert *cert);
+void           camel_cert_set_trust            (CamelCertDB *certdb,
+                                                CamelCert *cert,
+                                                CamelCertTrust trust);
 
 G_END_DECLS
 
diff --git a/docs/reference/camel/camel-sections.txt b/docs/reference/camel/camel-sections.txt
index 4270a03..d57b292 100644
--- a/docs/reference/camel/camel-sections.txt
+++ b/docs/reference/camel/camel-sections.txt
@@ -81,9 +81,12 @@ CamelKeyFilePrivate
 <SECTION>
 <FILE>camel-certdb</FILE>
 <TITLE>CamelCertDB</TITLE>
-CamelCertDB
 CamelCertTrust
 CamelCert
+camel_cert_new
+camel_cert_ref
+camel_cert_unref
+CamelCertDB
 camel_certdb_new
 camel_certdb_set_default
 camel_certdb_get_default
@@ -94,9 +97,6 @@ camel_certdb_touch
 camel_certdb_get_host
 camel_certdb_put
 camel_certdb_remove_host
-camel_cert_new
-camel_cert_ref
-camel_cert_unref
 camel_certdb_clear
 camel_cert_get_string
 camel_cert_set_string


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]