From 2d0caadbd16d6d9c23024db2d0233b8bbfaee2c9 Mon Sep 17 00:00:00 2001
From: Bastien Nocera <hadess@hadess.net>
Date: Tue, 22 Aug 2017 01:00:15 +0200
Subject: [PATCH] thumbnailer: Fix left-over files in /tmp

The fd that bwrap copies from the --file argument needs to be cleaned up
as well, otherwise the temporary file as well as the directory
containing it will be left on disk.

https://bugzilla.gnome.org/show_bug.cgi?id=786584
---
 libgnome-desktop/gnome-desktop-thumbnail-script.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/libgnome-desktop/gnome-desktop-thumbnail-script.c b/libgnome-desktop/gnome-desktop-thumbnail-script.c
index c319d0e9..f9a0184d 100644
--- a/libgnome-desktop/gnome-desktop-thumbnail-script.c
+++ b/libgnome-desktop/gnome-desktop-thumbnail-script.c
@@ -48,6 +48,7 @@ typedef struct {
   GArray *fd_array;
   /* Input/output file paths outside the sandbox */
   char *infile;
+  char *infile_tmp; /* the host version of /tmp/gnome-desktop-file-to-thumbnail.* */
   char *outfile;
   char *outdir; /* outdir is outfile's parent dir, if it needs to be deleted */
   /* I/O file paths inside the sandbox */
@@ -647,6 +648,11 @@ script_exec_free (ScriptExec *exec)
     return;
 
   g_free (exec->infile);
+  if (exec->infile_tmp)
+    {
+      g_unlink (exec->infile_tmp);
+      g_free (exec->infile_tmp);
+    }
   if (exec->outfile)
     {
       g_unlink (exec->outfile);
@@ -703,6 +709,7 @@ script_exec_new (const char  *uri,
     {
       char *tmpl;
       g_autofree char *ext = NULL;
+      g_autofree char *infile = NULL;
 
       exec->fd_array = g_array_new (FALSE, TRUE, sizeof (int));
       g_array_set_clear_func (exec->fd_array, clear_fd);
@@ -716,10 +723,12 @@ script_exec_new (const char  *uri,
           goto bail;
         }
       exec->outfile = g_build_filename (exec->outdir, "gnome-desktop-thumbnailer.png", NULL);
-
       ext = get_extension (exec->infile);
-      exec->s_infile = g_strdup_printf ("/tmp/gnome-desktop-file-to-thumbnail.%s", ext);
-      exec->s_outfile = g_strdup ("/tmp/gnome-desktop-thumbnailer.png");
+      infile = g_strdup_printf ("gnome-desktop-file-to-thumbnail.%s", ext);
+      exec->infile_tmp = g_build_filename (exec->outdir, infile, NULL);
+
+      exec->s_infile = g_build_filename ("/tmp/", infile, NULL);
+      exec->s_outfile = g_build_filename ("/tmp/", "gnome-desktop-thumbnailer.png", NULL);
     }
   else
 #endif
-- 
2.13.5