gimp r26653 - in trunk: . plug-ins/common



Author: neo
Date: Mon Aug 18 20:47:19 2008
New Revision: 26653
URL: http://svn.gnome.org/viewvc/gimp?rev=26653&view=rev

Log:
2008-08-18  Sven Neumann  <sven gimp org>

	* plug-ins/common/file-compressor.c: open the temporary file
	before forking. This allows us to return an error message if the
	file can't be opened. Also changed the code to not use 
g_message()
	from the child process.



Modified:
   trunk/ChangeLog
   trunk/plug-ins/common/file-compressor.c

Modified: trunk/plug-ins/common/file-compressor.c
==============================================================================
--- trunk/plug-ins/common/file-compressor.c	(original)
+++ trunk/plug-ins/common/file-compressor.c	Mon Aug 18 20:47:19 2008
@@ -407,42 +407,50 @@
 
 #ifndef G_OS_WIN32
   {
-    gint pid;
+    FILE *f;
+    gint  pid;
+
+    f = g_fopen (filename, "wb");
+
+    if (! f)
+      {
+        g_unlink (tmpname);
+        g_free (tmpname);
+
+        g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
+                     _("Could not open '%s' for writing: %s"),
+                     gimp_filename_to_utf8 (filename), g_strerror (errno));
+
+        return GIMP_PDB_EXECUTION_ERROR;
+      }
 
     /* fork off a compressor process */
     if ((pid = fork ()) < 0)
       {
         g_message ("fork() failed: %s", g_strerror (errno));
+
+        fclose (f);
+        g_unlink (tmpname);
         g_free (tmpname);
 
         return GIMP_PDB_EXECUTION_ERROR;
       }
-    else if (pid == 0)
+    else if (pid == 0)  /* child process */
       {
-        FILE *f;
-
-        if (!(f = g_fopen (filename, "wb")))
-          {
-            g_message (_("Could not open '%s' for writing: %s"),
-                       gimp_filename_to_utf8 (filename), g_strerror (errno));
-            g_free (tmpname);
-            _exit (127);
-          }
-
         /* make stdout for this process be the output file */
         if (dup2 (fileno (f), fileno (stdout)) == -1)
-          g_message ("dup2() failed: %s", g_strerror (errno));
+          g_printerr ("dup2() failed: %s", g_strerror (errno));
 
         /* and compress into it */
         execlp (compressor->save_program,
                 compressor->save_program,
                 compressor->save_options, tmpname, NULL);
 
-        g_message ("execlp(\"%s %s\") failed: %s",
-                   compressor->save_program,
-                   compressor->save_options,
-                   g_strerror (errno));
-        g_free (tmpname);
+        g_printerr ("execlp(\"%s %s\") failed: %s",
+                    compressor->save_program,
+                    compressor->save_options,
+                    g_strerror (errno));
+
         _exit(127);
       }
     else
@@ -450,6 +458,8 @@
         gint wpid;
         gint process_status;
 
+        fclose (f);
+
         wpid = waitpid (pid, &process_status, 0);
 
         if ((wpid < 0)
@@ -459,6 +469,8 @@
             g_message ("%s exited abnormally on file '%s'",
                        compressor->save_program,
                        gimp_filename_to_utf8 (tmpname));
+
+            g_unlink (tmpname);
             g_free (tmpname);
 
             return GIMP_PDB_EXECUTION_ERROR;
@@ -562,12 +574,28 @@
 
 #ifndef G_OS_WIN32
   {
-    gint pid;
+    FILE *f;
+    gint  pid;
+
+    f = g_fopen (tmpname, "wb");
+
+    if (! f)
+      {
+        g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
+                     _("Could not open '%s' for writing: %s"),
+                     gimp_filename_to_utf8 (tmpname), g_strerror (errno));
+        g_free (tmpname);
+
+        *status = GIMP_PDB_EXECUTION_ERROR;
+        return -1;
+      }
 
     /* fork off a compressor and wait for it */
     if ((pid = fork ()) < 0)
       {
         g_message ("fork() failed: %s", g_strerror (errno));
+
+        g_unlink (tmpname);
         g_free (tmpname);
 
         *status = GIMP_PDB_EXECUTION_ERROR;
@@ -575,33 +603,20 @@
       }
     else if (pid == 0)  /* child process */
       {
-        FILE *f;
-
-        if (! (f = g_fopen (tmpname, "wb")))
-          {
-            g_message (_("Could not open '%s' for writing: %s"),
-                       gimp_filename_to_utf8 (tmpname), g_strerror (errno));
-            g_free (tmpname);
-            _exit(127);
-          }
-
         /* make stdout for this child process be the temp file */
         if (dup2 (fileno (f), fileno (stdout)) == -1)
-          {
-            g_free (tmpname);
-            g_message ("dup2() failed: %s", g_strerror (errno));
-          }
+          g_printerr ("dup2() failed: %s", g_strerror (errno));
 
         /* and uncompress into it */
         execlp (compressor->load_program,
                 compressor->load_program,
                 compressor->load_options, filename, NULL);
 
-        g_message ("execlp(\"%s %s\") failed: %s",
-                   compressor->load_program,
-                   compressor->load_options,
-                   g_strerror (errno));
-        g_free (tmpname);
+        g_printerr ("execlp(\"%s %s\") failed: %s",
+                    compressor->load_program,
+                    compressor->load_options,
+                    g_strerror (errno));
+
         _exit(127);
       }
     else  /* parent process */
@@ -609,6 +624,8 @@
         gint wpid;
         gint process_status;
 
+        fclose (f);
+
         wpid = waitpid (pid, &process_status, 0);
 
         if ((wpid < 0)
@@ -618,6 +635,8 @@
             g_message ("%s exited abnormally on file '%s'",
                        compressor->load_program,
                        gimp_filename_to_utf8 (filename));
+
+            g_unlink (tmpname);
             g_free (tmpname);
 
             *status = GIMP_PDB_EXECUTION_ERROR;



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