[spice-xpi 02/12] Use gio to write trust store

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

 



This has the big advantage of being portable, in addition to removing
some code. Big disadvantage is that this adds a dependency on gio.
---
 SpiceXPI/src/plugin/plugin.cpp | 67 +++++++++++++++++++++---------------------
 SpiceXPI/src/plugin/plugin.h   |  2 ++
 configure.ac                   |  2 +-
 3 files changed, 37 insertions(+), 34 deletions(-)

diff --git a/SpiceXPI/src/plugin/plugin.cpp b/SpiceXPI/src/plugin/plugin.cpp
index b2ea8a2..2da69cb 100644
--- a/SpiceXPI/src/plugin/plugin.cpp
+++ b/SpiceXPI/src/plugin/plugin.cpp
@@ -57,6 +57,8 @@
 #include <sstream>
 #include <signal.h>
 #include <glib.h>
+#include <glib/gstdio.h>
+#include <gio/gio.h>
 
 extern "C" {
 #include <pthread.h>
@@ -188,6 +190,8 @@ nsPluginInstance::nsPluginInstance(NPP aInstance):
     // create temporary directory in /tmp
     char tmp_dir[] = "/tmp/spicec-XXXXXX";
     m_tmp_dir = mkdtemp(tmp_dir);
+
+    g_type_init();
 }
 
 nsPluginInstance::~nsPluginInstance()
@@ -647,41 +651,39 @@ bool nsPluginInstance::StartClient()
     g_return_val_if_reached(false);
 }
 
-bool nsPluginInstance::CreateTrustStore(void)
+bool nsPluginInstance::CreateTrustStoreFile(const std::string &trust_store)
 {
-    // create trust store filename
-    FILE *fp;
-    int fd = -1;
-    char trust_store_template[] = "/tmp/truststore.pem-XXXXXX";
-    mode_t prev_umask = umask(0177);
-    fd = mkstemp(trust_store_template);
-    umask(prev_umask);
-    m_trust_store_file = trust_store_template;
+    GFile *tmp_file;
+    GFileIOStream *iostream;
+    GOutputStream *stream;
 
-    if (fd != -1)
-    {
-        fp = fdopen(fd,"w+");
-        if (fp != NULL)
-        {
-            fputs(m_trust_store.c_str(), fp);
-            fflush(fp);
-            fsync(fd);
-            fclose(fp);
-        }
-        else
-        {
-            g_critical("could not open truststore temp file");
-            close(fd);
-            unlink(m_trust_store_file.c_str());
-            m_trust_store_file.clear();
-            return false;
-        }
+    tmp_file = g_file_new_tmp ("trustore.pem-XXXXXX", &iostream, NULL);
+    if (tmp_file == NULL) {
+        g_message("Couldn't create truststore");
+        return false;
     }
-    else
-    {
-        g_critical("could not create truststore temp file: %s", g_strerror(errno));
+
+    stream = g_io_stream_get_output_stream(G_IO_STREAM(iostream));
+    if (!g_output_stream_write_all(stream,
+                                   trust_store.c_str(),
+                                   trust_store.length(),
+                                   NULL, NULL, NULL)) {
+        g_message("Couldn't write truststore");
         return false;
     }
+    m_trust_store_file = g_file_get_path(tmp_file);
+    g_object_unref(tmp_file);
+    g_object_unref(iostream);
+
+    return true;
+}
+
+bool nsPluginInstance::RemoveTrustStoreFile()
+{
+    if (g_unlink(m_trust_store_file.c_str()) != 0)
+        return false;;
+
+    m_trust_store_file.clear();
 
     return true;
 }
@@ -712,7 +714,7 @@ void nsPluginInstance::Connect()
         return;
     }
 
-    if (!this->CreateTrustStore()) {
+    if (!this->CreateTrustStoreFile(m_trust_store)) {
         g_critical("failed to create trust store");
         return;
     }
@@ -844,8 +846,7 @@ void *nsPluginInstance::ControllerWaitHelper(void *opaque)
         fake_this->m_external_controller.Disconnect();
     }
     
-    unlink(fake_this->m_trust_store_file.c_str());
-    fake_this->m_trust_store_file.clear();
+    fake_this->RemoveTrustStoreFile();
     fake_this->m_pid_controller = -1;
     return NULL;
 }
diff --git a/SpiceXPI/src/plugin/plugin.h b/SpiceXPI/src/plugin/plugin.h
index 9c56f73..ea50ca5 100644
--- a/SpiceXPI/src/plugin/plugin.h
+++ b/SpiceXPI/src/plugin/plugin.h
@@ -187,6 +187,8 @@ private:
 private:
     bool StartClient();
     bool CreateTrustStore();
+    bool CreateTrustStoreFile(const std::string &trust_store);
+    bool RemoveTrustStoreFile();
 
     pid_t m_pid_controller;
     int32_t m_connected_status;
diff --git a/configure.ac b/configure.ac
index 0ca271c..48d3a6b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -24,7 +24,7 @@ AC_CONFIG_SUBDIRS([spice-protocol])
 SPICE_PROTOCOL_CFLAGS='-I ${top_srcdir}/spice-protocol'
 AC_SUBST(SPICE_PROTOCOL_CFLAGS)
 
-PKG_CHECK_MODULES(GLIB, glib-2.0)
+PKG_CHECK_MODULES(GLIB, glib-2.0 gio-2.0)
 AC_SUBST(GLIB_CFLAGS)
 AC_SUBST(GLIB_LIBS)
 
-- 
1.8.1.4

_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
http://lists.freedesktop.org/mailman/listinfo/spice-devel


[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]