diff cinst/nssstore_win.c @ 330:1e6d1eab8395

Fix NSS unit test for Windows and change how instructions are written This was supposed to fix the block on error. But it did not.
author Andre Heinecke <aheinecke@intevation.de>
date Tue, 08 Apr 2014 15:08:57 +0000
parents b1059360a0c7
children c0eac5c8c245
line wrap: on
line diff
--- a/cinst/nssstore_win.c	Tue Apr 08 14:52:31 2014 +0000
+++ b/cinst/nssstore_win.c	Tue Apr 08 15:08:57 2014 +0000
@@ -32,53 +32,50 @@
 * Writes the null terminated list of instructions to
 * the handle.
 *
-* @param [in] instructions instructions to write
+* @param [in] base64 encoded der certificates to write
 * @param [in] write_handle to write to
+* @param [in] remove weather the certificate should be installed or removed
 *
 * @returns true on success, false on failure
 */
 static bool
-write_instructions(char **instructions, HANDLE write_handle)
+write_instructions(char **certificates, HANDLE write_handle,
+                   bool remove)
 {
-  bool retval = false;
   int i = 0;
-  const char *line_end = "\n\0";
+  int cHandle = -1;
+  FILE *write_stream = NULL;
 
-  if (!instructions)
+  if (!certificates)
     {
       return true;
     }
 
-  for (i = 0; instructions[i]; i++)
+  cHandle = _open_osfhandle ((intptr_t)write_handle, 0);
+
+  if (cHandle == -1)
     {
-      DWORD written = 0;
-      DWORD inst_len = strlen (instructions[i]);
-      retval = WriteFile (write_handle, (LPCVOID) instructions[i], inst_len, &written, NULL);
-      if (!retval)
-        {
-          PRINTLASTERROR ("Failed to write\n");
-          return false;
-        }
-      if (inst_len != written)
+      ERRORPRINTF ("Failed to open write handle.\n");
+    }
+
+  write_stream = _fdopen(cHandle, "w");
+  for (i = 0; certificates[i]; i++)
+    {
+      int ret = 0;
+      DEBUGPRINTF("Writing \n");
+      if (remove)
+        ret = fprintf (write_stream, "R:%s\n", certificates[i]);
+      else
+        ret = fprintf (write_stream, "I:%s\n", certificates[i]);
+
+      if (ret <= 0)
         {
-          ERRORPRINTF ("Failed to write everything\n");
-          retval = false;
-          return false;
-        }
-      written = 0;
-      retval = WriteFile (write_handle, (LPCVOID) line_end, 2, &written, NULL);
-      if (!retval)
-        {
-          PRINTLASTERROR ("Failed to write line end\n");
-          return false;
-        }
-      if (inst_len != written)
-        {
-          ERRORPRINTF ("Failed to write full line end\n");
-          retval = false;
-          return false;
+          DEBUGPRINTF ("Failed to write everything.\n");
+          break;
         }
     }
+
+  DEBUGPRINTF("Write done\n");
   return true;
 }
 
@@ -151,7 +148,7 @@
   siStartInfo.cb = sizeof (STARTUPINFO);
   siStartInfo.hStdError = h_stdout_child_w;
   siStartInfo.hStdOutput = h_stdout_child_w;
-  siStartInfo.hStdInput = h_stdin_child_w;
+  siStartInfo.hStdInput = h_stdin_child_r;
   siStartInfo.dwFlags = STARTF_USESTDHANDLES;
 
   success = CreateProcessAsUserW (hToken,
@@ -160,7 +157,7 @@
                                   NULL, /* Process attributes. Take hToken */
                                   NULL, /* Thread attribues. Take hToken */
                                   TRUE, /* Inherit Handles */
-                                  0, /* Creation flags. Use default */
+                                  CREATE_UNICODE_ENVIRONMENT, /* Creation flags. */
                                   lpEnvironment,
                                   NULL, /* Current working directory */
                                   &siStartInfo,
@@ -171,12 +168,12 @@
       goto closepipes;
     }
 
-  if (!write_instructions (to_install, h_stdin_child_w))
+  if (!write_instructions (to_install, h_stdin_child_w, false))
     {
       ERRORPRINTF ("Failed to write install instructions.\n");
       goto closepipes;
     }
-  if (!write_instructions (to_remove, h_stdin_child_w))
+  if (!write_instructions (to_remove, h_stdin_child_w, true))
     {
       ERRORPRINTF ("Failed to write remove instructions.\n");
       goto closepipes;
@@ -225,19 +222,21 @@
 
 closeprocess:
   if (piProcInfo.hProcess)
-    CloseHandle(piProcInfo.hProcess);
+    CloseHandle (piProcInfo.hProcess);
   if (piProcInfo.hThread)
-    CloseHandle(piProcInfo.hThread);
+    CloseHandle (piProcInfo.hThread);
 
 closepipes:
+  if (lpEnvironment)
+    DestroyEnvironmentBlock (lpEnvironment);
   if (h_stdin_child_w)
-    CloseHandle(h_stdin_child_w);
+    CloseHandle (h_stdin_child_w);
   if (h_stdin_child_r)
-    CloseHandle(h_stdin_child_r);
+    CloseHandle (h_stdin_child_r);
   if (h_stdout_child_w)
-    CloseHandle(h_stdout_child_w);
+    CloseHandle (h_stdout_child_w);
   if (h_stdout_child_r)
-    CloseHandle(h_stdout_child_r);
+    CloseHandle (h_stdout_child_r);
 
   return retval;
 }

http://wald.intevation.org/projects/trustbridge/