[gnome-desktop] thumbnail: Use an array to store the thumbnailer's cmdline



commit 483ea2e736810f5b19f229ab68d979c4eb43a5ea
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Jul 19 01:52:35 2017 +0200

    thumbnail: Use an array to store the thumbnailer's cmdline
    
    Instead of a shell-quoted string, to make it easier to add new elements
    to this command-line.

 libgnome-desktop/gnome-desktop-thumbnail.c |  116 ++++++++++++++++++++-------
 1 files changed, 86 insertions(+), 30 deletions(-)
---
diff --git a/libgnome-desktop/gnome-desktop-thumbnail.c b/libgnome-desktop/gnome-desktop-thumbnail.c
index f9889c1..a39de9f 100644
--- a/libgnome-desktop/gnome-desktop-thumbnail.c
+++ b/libgnome-desktop/gnome-desktop-thumbnail.c
@@ -969,20 +969,20 @@ gnome_desktop_thumbnail_factory_can_thumbnail (GnomeDesktopThumbnailFactory *fac
 }
 
 static char *
-expand_thumbnailing_script (const char *script,
-                           const int   size,
-                           const char *inuri,
-                           const char *outfile)
+expand_thumbnailing_elem (const char *elem,
+                         const int   size,
+                         const char *inuri,
+                         const char *outfile,
+                         gboolean   *got_input,
+                         gboolean   *got_output)
 {
   GString *str;
   const char *p, *last;
-  char *localfile, *quoted;
-  gboolean got_in;
+  char *localfile;
 
   str = g_string_new (NULL);
 
-  got_in = FALSE;
-  last = script;
+  last = elem;
   while ((p = strchr (last, '%')) != NULL)
     {
       g_string_append_len (str, last, p - last);
@@ -990,28 +990,23 @@ expand_thumbnailing_script (const char *script,
 
       switch (*p) {
       case 'u':
-       quoted = g_shell_quote (inuri);
-       g_string_append (str, quoted);
-       g_free (quoted);
-       got_in = TRUE;
+       g_string_append (str, inuri);
+       *got_input = TRUE;
        p++;
        break;
       case 'i':
        localfile = g_filename_from_uri (inuri, NULL, NULL);
        if (localfile)
          {
-           quoted = g_shell_quote (localfile);
-           g_string_append (str, quoted);
-           got_in = TRUE;
-           g_free (quoted);
+           g_string_append (str, localfile);
+           *got_input = TRUE;
            g_free (localfile);
          }
        p++;
        break;
       case 'o':
-       quoted = g_shell_quote (outfile);
-       g_string_append (str, quoted);
-       g_free (quoted);
+       g_string_append (str, outfile);
+       *got_output = TRUE;
        p++;
        break;
       case 's':
@@ -1030,10 +1025,60 @@ expand_thumbnailing_script (const char *script,
     }
   g_string_append (str, last);
 
-  if (got_in)
-    return g_string_free (str, FALSE);
+  return g_string_free (str, FALSE);
+}
+
+static char **
+expand_thumbnailing_script (const char  *script,
+                           const int    size,
+                           const char  *inuri,
+                           const char  *outfile,
+                           GError     **error)
+{
+  GPtrArray *array;
+  char **script_elems;
+  guint i;
+  gboolean got_in, got_out;
+
+  if (!g_shell_parse_argv (script, NULL, &script_elems, error))
+    return NULL;
+
+  array = g_ptr_array_new_with_free_func (g_free);
 
-  g_string_free (str, TRUE);
+  got_in = got_out = FALSE;
+  for (i = 0; script_elems[i] != NULL; i++)
+    {
+      char *expanded;
+
+      expanded = expand_thumbnailing_elem (script_elems[i],
+                                          size,
+                                          inuri,
+                                          outfile,
+                                          &got_in,
+                                          &got_out);
+
+      g_ptr_array_add (array, expanded);
+    }
+
+  if (!got_in)
+    {
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                          "Input file could not be set");
+      goto bail;
+    }
+  else if (!got_out)
+    {
+      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                          "Output file could not be set");
+      goto bail;
+    }
+
+  g_ptr_array_add (array, NULL);
+
+  return (char **) g_ptr_array_free (array, FALSE);
+
+bail:
+  g_ptr_array_free (array, TRUE);
   return NULL;
 }
 
@@ -1114,7 +1159,7 @@ gnome_desktop_thumbnail_factory_generate_thumbnail (GnomeDesktopThumbnailFactory
                                                    const char            *mime_type)
 {
   GdkPixbuf *pixbuf;
-  char *script, *expanded_script;
+  char *script;
   int size;
   int exit_status;
   char *tmpname;
@@ -1154,18 +1199,29 @@ gnome_desktop_thumbnail_factory_generate_thumbnail (GnomeDesktopThumbnailFactory
 
       if (fd != -1)
        {
+         char **expanded_script;
+         GError *error = NULL;
+
          close (fd);
 
-         expanded_script = expand_thumbnailing_script (script, size, uri, tmpname);
-         if (expanded_script != NULL &&
-             g_spawn_command_line_sync (expanded_script,
-                                        NULL, NULL, &exit_status, NULL) &&
-             exit_status == 0)
+         expanded_script = expand_thumbnailing_script (script, size, uri, tmpname, &error);
+         if (expanded_script == NULL)
+           {
+             g_warning ("Failed to expand script '%s': %s", script, error->message);
+             g_error_free (error);
+           }
+         else
            {
-             pixbuf = gdk_pixbuf_new_from_file (tmpname, NULL);
+             gboolean ret;
+
+             ret = g_spawn_sync (NULL, expanded_script, NULL, G_SPAWN_SEARCH_PATH,
+                                 NULL, NULL, NULL, NULL, &exit_status, NULL);
+             if (ret && exit_status == 0)
+               pixbuf = gdk_pixbuf_new_from_file (tmpname, NULL);
+
+             g_strfreev (expanded_script);
            }
 
-         g_free (expanded_script);
          g_unlink (tmpname);
          g_free (tmpname);
        }


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