[gegl] Bug 690509 - gegl-tile-backend: free disk space when finalizing a tile backend which swaps to disk.



commit 88caa4a4aced4e35e297a54325ad5336b66f4fad
Author: Jehan <jehan girinstud io>
Date:   Wed Dec 19 14:11:24 2012 +0900

    Bug 690509 - gegl-tile-backend: free disk space when finalizing a tile backend which swaps to disk.
    
    gegl_tile_backend_unlink_swap() written for this purpose and now used
    on the 3 currently existing backends which may swap to disk.
    Accessorily I ensure any swap path from GEGL config does not end with
    a directory separator, to ensure proper string comparison, even when
    provided by the user through environment variable.

 gegl/buffer/gegl-tile-backend-file-async.c  |    5 ++++-
 gegl/buffer/gegl-tile-backend-file-mapped.c |    5 ++++-
 gegl/buffer/gegl-tile-backend-file.c        |    5 ++++-
 gegl/buffer/gegl-tile-backend.c             |   15 +++++++++++++++
 gegl/buffer/gegl-tile-backend.h             |   12 ++++++++++++
 gegl/gegl-init.c                            |    8 +++++++-
 6 files changed, 46 insertions(+), 4 deletions(-)
---
diff --git a/gegl/buffer/gegl-tile-backend-file-async.c b/gegl/buffer/gegl-tile-backend-file-async.c
index 9e2efae..9686f9a 100644
--- a/gegl/buffer/gegl-tile-backend-file-async.c
+++ b/gegl/buffer/gegl-tile-backend-file-async.c
@@ -954,7 +954,10 @@ gegl_tile_backend_file_finalize (GObject *object)
     gegl_tile_backend_file_free_free_list (self);
 
   if (self->path)
-    g_free (self->path);
+    {
+      gegl_tile_backend_unlink_swap (self->path);
+      g_free (self->path);
+    }
 
   if (self->monitor)
     g_object_unref (self->monitor);
diff --git a/gegl/buffer/gegl-tile-backend-file-mapped.c b/gegl/buffer/gegl-tile-backend-file-mapped.c
index 28f95fc..5e72b46 100644
--- a/gegl/buffer/gegl-tile-backend-file-mapped.c
+++ b/gegl/buffer/gegl-tile-backend-file-mapped.c
@@ -861,7 +861,10 @@ gegl_tile_backend_file_finalize (GObject *object)
     }
 
   if (self->path)
-    g_free (self->path);
+    {
+      gegl_tile_backend_unlink_swap (self->path);
+      g_free (self->path);
+    }
 
   if (self->monitor)
     g_object_unref (self->monitor);
diff --git a/gegl/buffer/gegl-tile-backend-file.c b/gegl/buffer/gegl-tile-backend-file.c
index 40bf5b0..725759a 100644
--- a/gegl/buffer/gegl-tile-backend-file.c
+++ b/gegl/buffer/gegl-tile-backend-file.c
@@ -676,7 +676,10 @@ gegl_tile_backend_file_finalize (GObject *object)
     }
 
   if (self->path)
-    g_free (self->path);
+    {
+      gegl_tile_backend_unlink_swap (self->path);
+      g_free (self->path);
+    }
 
   if (self->monitor)
     g_object_unref (self->monitor);
diff --git a/gegl/buffer/gegl-tile-backend.c b/gegl/buffer/gegl-tile-backend.c
index a64d7fd..9b1054d 100644
--- a/gegl/buffer/gegl-tile-backend.c
+++ b/gegl/buffer/gegl-tile-backend.c
@@ -21,11 +21,13 @@
 
 #include <babl/babl.h>
 #include <glib-object.h>
+#include <glib/gstdio.h>
 
 #include "gegl-buffer-types.h"
 #include "gegl-buffer-private.h"
 #include "gegl-tile-source.h"
 #include "gegl-tile-backend.h"
+#include "gegl-config.h"
 
 G_DEFINE_TYPE (GeglTileBackend, gegl_tile_backend, GEGL_TYPE_TILE_SOURCE)
 
@@ -207,3 +209,16 @@ gegl_tile_backend_peek_storage (GeglTileBackend *backend)
 {
   return backend->priv->storage;
 }
+
+void
+gegl_tile_backend_unlink_swap (gchar *path)
+{
+  gchar *dirname = g_path_get_dirname (path);
+
+  /* Ensure we delete only files in our known swap directory for safety. */
+  if (g_file_test (path, G_FILE_TEST_EXISTS) &&
+      g_strcmp0 (dirname, gegl_config()->swap) == 0)
+    g_unlink (path);
+
+  g_free (dirname);
+}
diff --git a/gegl/buffer/gegl-tile-backend.h b/gegl/buffer/gegl-tile-backend.h
index d3d38b7..c727b5a 100644
--- a/gegl/buffer/gegl-tile-backend.h
+++ b/gegl/buffer/gegl-tile-backend.h
@@ -66,6 +66,18 @@ GeglRectangle gegl_tile_backend_get_extent (GeglTileBackend *tile_backend);
 
 GType gegl_tile_backend_get_type (void) G_GNUC_CONST;
 
+/**
+ * gegl_tile_backend_unlink_swap:
+ * @path: the path where the gegl tile backend has swapped.
+ *
+ * Delete a swap file from disk. This must be used by tile backends which may
+ * swap to disk under certain circonstances.
+ *
+ * For safety, this function will check that the swap file is in the swap
+ * directory before deletion but it won't perform any other check.
+ */
+void gegl_tile_backend_unlink_swap (gchar *path);
+
 G_END_DECLS
 
 #endif
diff --git a/gegl/gegl-init.c b/gegl/gegl-init.c
index 68d7149..a41d165 100644
--- a/gegl/gegl-init.c
+++ b/gegl/gegl-init.c
@@ -109,7 +109,13 @@ gegl_swap_dir (void)
           if (g_str_equal (g_getenv ("GEGL_SWAP"), "RAM"))
             swapdir = NULL;
           else
-            swapdir = g_strdup (g_getenv ("GEGL_SWAP"));
+            {
+              swapdir = g_strstrip (g_strdup (g_getenv ("GEGL_SWAP")));
+
+              /* Remove any trailing separator, unless the path is only made of a leading separator. */
+              while (strlen (swapdir) > strlen (G_DIR_SEPARATOR_S) && g_str_has_suffix (swapdir, G_DIR_SEPARATOR_S))
+                swapdir[strlen (swapdir) - strlen (G_DIR_SEPARATOR_S)] = '\0';
+            }
         }
       else
         {



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