validate/ssim: Clean-up temporary directory
authorPhilippe Normand <philn@igalia.com>
Mon, 19 Aug 2019 10:08:41 +0000 (11:08 +0100)
committerPhilippe Normand <philn@igalia.com>
Wed, 21 Aug 2019 11:50:03 +0000 (11:50 +0000)
When no output-dir is specified in the plugin config, a temporary directory is
created, so it needs to be removed when no-longer needed.

validate/plugins/ssim/gstvalidatessim.c

index 3784752..9db6a9b 100644 (file)
@@ -352,6 +352,9 @@ static void
 _finalize (GObject * object)
 {
   ValidateSsimOverridePrivate *priv = VALIDATE_SSIM_OVERRIDE (object)->priv;
+  GDir *outdir_handle = NULL;
+  const gchar *filename = NULL;
+  GError *error = NULL;
 
   if (priv->converter)
     gst_video_converter_free (priv->converter);
@@ -359,6 +362,25 @@ _finalize (GObject * object)
   if (priv->last_caps)
     gst_caps_unref (priv->last_caps);
 
+  if (priv->config && !gst_structure_has_field (priv->config, "output-dir")) {
+    /* Remove temporary directory contents (expected to be files, no sub-directories). */
+    outdir_handle = g_dir_open (priv->outdir, 0, &error);
+    if (outdir_handle != NULL) {
+      while ((filename = g_dir_read_name (outdir_handle))) {
+        gchar *path =
+            g_build_path (G_DIR_SEPARATOR_S, priv->outdir, filename, NULL);
+        g_remove (path);
+        g_free (path);
+      }
+      g_dir_close (outdir_handle);
+    } else {
+      GST_ERROR ("Unable to cleanup temporary directory %s: %s", priv->outdir,
+          error->message);
+      g_error_free (error);
+    }
+    g_rmdir (priv->outdir);
+  }
+
   g_free (priv->outdir);
   g_free (priv->result_outdir);
   g_array_unref (priv->frames);