[gnome-builder] beautifier-plugin: use per project Builder cache dir



commit c5f8e0557d77e03d1032f02a17c6659e7a6d3a88
Author: Sebastien Lafargue <slafargue gnome org>
Date:   Sun Jan 21 16:05:38 2018 +0100

    beautifier-plugin: use per project Builder cache dir
    
    The old beautifier cache dir is also removed at load
    time to cleanup a previous launch.

 src/plugins/beautifier/gb-beautifier-config.c      |  70 +++++++-------
 .../beautifier/gb-beautifier-editor-addin.c        |  54 +++++++++--
 src/plugins/beautifier/gb-beautifier-helper.c      | 106 +++++++--------------
 src/plugins/beautifier/gb-beautifier-process.c     |  12 ++-
 4 files changed, 124 insertions(+), 118 deletions(-)
---
diff --git a/src/plugins/beautifier/gb-beautifier-config.c b/src/plugins/beautifier/gb-beautifier-config.c
index 6fab6c5fc..cfd4a1731 100644
--- a/src/plugins/beautifier/gb-beautifier-config.c
+++ b/src/plugins/beautifier/gb-beautifier-config.c
@@ -133,7 +133,7 @@ copy_to_tmp_file (GbBeautifierEditorAddin *self,
   g_assert (!dzl_str_empty0 (tmp_dir));
   g_assert (!dzl_str_empty0 (source_path));
 
-  tmp_path = g_build_filename (tmp_dir, "gnome-builder-beautifier-XXXXXX.txt", NULL);
+  tmp_path = g_build_filename (tmp_dir, "XXXXXX.txt", NULL);
   if (-1 != (fd = g_mkstemp (tmp_path)))
     {
       close (fd);
@@ -595,8 +595,6 @@ get_entries_worker (GTask        *task,
                     GCancellable *cancellable)
 {
   GbBeautifierEditorAddin *self = (GbBeautifierEditorAddin *)source_object;
-  g_autoptr (GError) error = NULL;
-  IdeContext *context;
   IdeProject *project;
   IdeVcs *vcs;
   GArray *entries;
@@ -613,14 +611,13 @@ get_entries_worker (GTask        *task,
   g_assert (G_IS_TASK (task));
   g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
 
-  if (self->tmp_dir == NULL)
+  if (self->context == NULL)
     {
-      if (NULL == (self->tmp_dir = g_dir_make_tmp ("gnome-builder-beautifier-XXXXXX", &error)))
-        {
-          g_task_return_error (task, g_steal_pointer (&error));
-          return;
-        }
-
+      g_task_return_new_error (task,
+                               G_IO_ERROR,
+                               G_IO_ERROR_FAILED,
+                               "Failed to initialized the Beautifier plugin, no IdeContext ready");
+      return;
     }
 
   entries = g_array_new (TRUE, TRUE, sizeof (GbBeautifierConfigEntry));
@@ -638,37 +635,34 @@ get_entries_worker (GTask        *task,
   g_clear_pointer (&map, g_array_unref);
 
   /* Project wide config */
-  if (NULL != (context = self->context))
+  if (NULL != (project = ide_context_get_project (self->context)))
     {
-      if (NULL != (project = ide_context_get_project (context)))
+      project_name = ide_project_get_name (project);
+      if (dzl_str_equal0 (project_name, "Builder"))
         {
-          project_name = ide_project_get_name (project);
-          if (dzl_str_equal0 (project_name, "Builder"))
-            {
-              configdir = g_strdup ("resource:///org/gnome/builder/plugins/beautifier_plugin/self/");
-              map = gb_beautifier_config_get_map (self, configdir);
-              add_entries_from_base_path (self, configdir, entries, map, &ret_has_default);
-              has_default |= ret_has_default;
-              g_clear_pointer (&configdir, g_free);
+          configdir = g_strdup ("resource:///org/gnome/builder/plugins/beautifier_plugin/self/");
+          map = gb_beautifier_config_get_map (self, configdir);
+          add_entries_from_base_path (self, configdir, entries, map, &ret_has_default);
+          has_default |= ret_has_default;
+          g_clear_pointer (&configdir, g_free);
 
-              g_clear_pointer (&map, g_array_unref);
-            }
-          else if (NULL != (vcs = ide_context_get_vcs (context)))
-            {
-              GFile *workdir;
-              g_autofree gchar *workdir_path = NULL;
-
-              workdir = ide_vcs_get_working_directory (vcs);
-              workdir_path = g_file_get_path (workdir);
-              project_config_path = g_build_filename (workdir_path,
-                                                      ".beautifier",
-                                                      NULL);
-              map = gb_beautifier_config_get_map (self, project_config_path);
-              add_entries_from_base_path (self, project_config_path, entries, map, &ret_has_default);
-              has_default |= ret_has_default;
-
-              g_clear_pointer (&map, g_array_unref);
-            }
+          g_clear_pointer (&map, g_array_unref);
+        }
+      else if (NULL != (vcs = ide_context_get_vcs (self->context)))
+        {
+          GFile *workdir;
+          g_autofree gchar *workdir_path = NULL;
+
+          workdir = ide_vcs_get_working_directory (vcs);
+          workdir_path = g_file_get_path (workdir);
+          project_config_path = g_build_filename (workdir_path,
+                                                  ".beautifier",
+                                                  NULL);
+          map = gb_beautifier_config_get_map (self, project_config_path);
+          add_entries_from_base_path (self, project_config_path, entries, map, &ret_has_default);
+          has_default |= ret_has_default;
+
+          g_clear_pointer (&map, g_array_unref);
         }
     }
 
diff --git a/src/plugins/beautifier/gb-beautifier-editor-addin.c 
b/src/plugins/beautifier/gb-beautifier-editor-addin.c
index 5319ddbe7..776509c5e 100644
--- a/src/plugins/beautifier/gb-beautifier-editor-addin.c
+++ b/src/plugins/beautifier/gb-beautifier-editor-addin.c
@@ -354,9 +354,9 @@ add_shortcut_window_entry (GbBeautifierEditorAddin *self)
 }
 
 static void
-get_entries_async_cb (GObject      *object,
-                      GAsyncResult *result,
-                      gpointer      user_data)
+gb_beautifier_editor_addin_async_cb (GObject      *object,
+                                     GAsyncResult *result,
+                                     gpointer      user_data)
 {
   g_autoptr(GbBeautifierEditorAddin) self = (GbBeautifierEditorAddin *)object;
   GbBeautifierEntriesResult *ret;
@@ -386,12 +386,43 @@ get_entries_async_cb (GObject      *object,
   add_shortcut_window_entry (self);
 }
 
+static void
+gb_beautifier_editor_addin_reap_cb (GObject      *object,
+                                    GAsyncResult *result,
+                                    gpointer      user_data)
+{
+  DzlDirectoryReaper *reaper = (DzlDirectoryReaper *)object;
+  g_autoptr(GbBeautifierEditorAddin) self = user_data;
+  g_autoptr(GError) error = NULL;
+
+  g_assert (DZL_IS_DIRECTORY_REAPER (reaper));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (GB_IS_BEAUTIFIER_EDITOR_ADDIN (self));
+
+  if (!dzl_directory_reaper_execute_finish (reaper, result, &error))
+    g_warning ("Failed to reap old beautifier data: %s", error->message);
+
+  if (g_mkdir_with_parents (self->tmp_dir, 0750) != 0)
+    {
+      g_warning ("Failed to initialized the Beautifier plugin, can't write the temporary directory");
+      return;
+    }
+
+  gb_beautifier_config_get_entries_async (self,
+                                          &self->has_default,
+                                          gb_beautifier_editor_addin_async_cb,
+                                          NULL,
+                                          g_object_ref (self));
+}
+
 static void
 gb_beautifier_editor_addin_load (IdeEditorAddin       *addin,
                                  IdeEditorPerspective *editor)
 {
   GbBeautifierEditorAddin *self = (GbBeautifierEditorAddin *)addin;
   IdeWorkbench *workbench;
+  g_autoptr(DzlDirectoryReaper) reaper = NULL;
+  g_autoptr (GFile) tmp_file = NULL;
 
   g_assert (GB_IS_BEAUTIFIER_EDITOR_ADDIN (self));
   g_assert (IDE_IS_EDITOR_PERSPECTIVE (editor));
@@ -401,11 +432,18 @@ gb_beautifier_editor_addin_load (IdeEditorAddin       *addin,
   self->context = ide_workbench_get_context (workbench);
   ide_object_set_context (IDE_OBJECT (self), self->context);
 
-  gb_beautifier_config_get_entries_async (self,
-                                          &self->has_default,
-                                          get_entries_async_cb,
-                                          NULL,
-                                          g_object_ref (self));
+  if (self->tmp_dir == NULL)
+    self->tmp_dir = ide_context_cache_filename (self->context, "beautifier", NULL);
+
+  /* Cleanup old beautifier cache directory */
+  reaper = dzl_directory_reaper_new ();
+  tmp_file = g_file_new_for_path (self->tmp_dir);
+  dzl_directory_reaper_add_directory (reaper, tmp_file, 0);
+
+  dzl_directory_reaper_execute_async (reaper,
+                                      NULL,
+                                      gb_beautifier_editor_addin_reap_cb,
+                                      g_object_ref (self));
 }
 
 static void
diff --git a/src/plugins/beautifier/gb-beautifier-helper.c b/src/plugins/beautifier/gb-beautifier-helper.c
index 1406ef3b2..ae1e13f3c 100644
--- a/src/plugins/beautifier/gb-beautifier-helper.c
+++ b/src/plugins/beautifier/gb-beautifier-helper.c
@@ -28,14 +28,6 @@
 #include "gb-beautifier-helper.h"
 #include "gb-beautifier-private.h"
 
-typedef struct
-{
-  GbBeautifierEditorAddin *self;
-  GFile                   *file;
-  GFileIOStream           *stream;
-  gsize                    len;
-} SaveTmpState;
-
 static gboolean
 check_path_is_in_tmp_dir (const gchar *path,
                           const gchar *tmp_dir)
@@ -50,20 +42,16 @@ void
 gb_beautifier_helper_remove_temp_for_path (GbBeautifierEditorAddin *self,
                                            const gchar             *path)
 {
-  const gchar *tmp_dir;
-
   g_assert (path != NULL);
 
-  tmp_dir = g_get_tmp_dir ();
-
-  if (check_path_is_in_tmp_dir (path, tmp_dir))
+  if (check_path_is_in_tmp_dir (path, self->tmp_dir))
     g_unlink (path);
   else
     {
       ide_object_warning (self,
                           /* translators: %s and %s are replaced with the temporary dir and the file path */
                           _("Beautifier plugin: blocked attempt to remove a file outside of the “%s” 
temporary directory: “%s”"),
-                          tmp_dir,
+                          self->tmp_dir,
                           path);
       return;
     }
@@ -73,22 +61,20 @@ void
 gb_beautifier_helper_remove_temp_for_file (GbBeautifierEditorAddin *self,
                                            GFile                   *file)
 {
-  const gchar *tmp_dir;
   g_autofree gchar *path = NULL;
 
   g_assert (G_IS_FILE (file));
 
-  tmp_dir = g_get_tmp_dir ();
   path = g_file_get_path (file);
 
-  if (check_path_is_in_tmp_dir (path, tmp_dir))
+  if (check_path_is_in_tmp_dir (path, self->tmp_dir))
     g_file_delete (file, NULL, NULL);
   else
     {
       ide_object_warning (self,
                           /* translators: %s and %s are replaced with the temporary dir and the file path */
                           _("Beautifier plugin: blocked attempt to remove a file outside of the “%s” 
temporary directory: “%s”"),
-                          tmp_dir,
+                          self->tmp_dir,
                           path);
       return;
     }
@@ -100,25 +86,23 @@ gb_beautifier_helper_config_entry_remove_temp_files (GbBeautifierEditorAddin *se
 {
   GbBeautifierCommandArg *arg;
   g_autofree gchar *config_path = NULL;
-  const gchar *tmp_dir;
 
   g_assert (config_entry != NULL);
 
-  tmp_dir = g_get_tmp_dir ();
   if (config_entry->is_config_file_temp)
     {
 
       if (G_IS_FILE (config_entry->config_file))
         {
           config_path = g_file_get_path (config_entry->config_file);
-          if (check_path_is_in_tmp_dir (config_path, tmp_dir))
+          if (check_path_is_in_tmp_dir (config_path, self->tmp_dir))
             g_file_delete (config_entry->config_file, NULL, NULL);
           else
             {
               ide_object_warning (self,
                                   /* translators: %s and %s are replaced with the temporary dir and the file 
path */
                                   _("Beautifier plugin: blocked attempt to remove a file outside of the “%s” 
temporary directory: “%s”"),
-                                  tmp_dir,
+                                  self->tmp_dir,
                                   config_path);
               return;
             }
@@ -132,13 +116,13 @@ gb_beautifier_helper_config_entry_remove_temp_files (GbBeautifierEditorAddin *se
           arg = &g_array_index (config_entry->command_args, GbBeautifierCommandArg, i);
           if (arg->is_temp && !dzl_str_empty0 (arg->str))
             {
-              if (check_path_is_in_tmp_dir (arg->str, tmp_dir))
+              if (check_path_is_in_tmp_dir (arg->str, self->tmp_dir))
                 g_unlink (arg->str);
               else
                 {
                   ide_object_warning (self,
                                       _("Beautifier plugin: blocked attempt to remove a file outside of the 
“%s” temporary directory: “%s”"),
-                                      tmp_dir,
+                                      self->tmp_dir,
                                       arg->str);
                   return;
                 }
@@ -147,43 +131,26 @@ gb_beautifier_helper_config_entry_remove_temp_files (GbBeautifierEditorAddin *se
     }
 }
 
-static void
-save_tmp_state_free (gpointer data)
-{
-  SaveTmpState *state = (SaveTmpState *)data;
-
-  g_assert (data != NULL);
-
-  if (!g_io_stream_is_closed ((GIOStream *)state->stream))
-    g_io_stream_close ((GIOStream *)state->stream, NULL, NULL);
-
-  g_clear_object (&state->file);
-
-  g_slice_free (SaveTmpState, state);
-}
-
 static void
 gb_beautifier_helper_create_tmp_file_cb (GObject      *object,
                                          GAsyncResult *result,
                                          gpointer      user_data)
 {
-  GOutputStream *output_stream = (GOutputStream *)object;
+  g_autoptr (GFile) file = (GFile *)object;
   g_autoptr(GError) error = NULL;
   g_autoptr(GTask) task = (GTask *)user_data;
-  SaveTmpState *state;
-  gsize count;
 
-  g_assert (G_IS_OUTPUT_STREAM (output_stream));
+  g_assert (G_IS_FILE (file));
   g_assert (G_IS_ASYNC_RESULT (result));
   g_assert (G_IS_TASK (task));
 
-  state = (SaveTmpState *)g_task_get_task_data (task);
-  if (!g_output_stream_write_all_finish (output_stream, result, &count, &error))
+  if (!g_file_replace_contents_finish (file, result, NULL, &error))
     g_task_return_error (task, g_steal_pointer (&error));
-  else if (g_task_return_error_if_cancelled (task))
-    g_file_delete (state->file, NULL, NULL);
+
+  if (g_task_return_error_if_cancelled (task))
+    g_file_delete (file, NULL, NULL);
   else
-    g_task_return_pointer (task, g_steal_pointer (&state->file), g_object_unref);
+    g_task_return_pointer (task, g_steal_pointer (&file), g_object_unref);
 }
 
 void
@@ -193,12 +160,10 @@ gb_beautifier_helper_create_tmp_file_async (GbBeautifierEditorAddin *self,
                                             GCancellable            *cancellable,
                                             gpointer                 user_data)
 {
-  SaveTmpState *state;
-  GFile *file = NULL;
-  GFileIOStream *stream;
-  GOutputStream *output_stream;
   g_autoptr(GTask) task = NULL;
-  g_autoptr(GError) error = NULL;
+  g_autofree gchar *tmp_path = NULL;
+  GFile *file;
+  gint fd;
 
   g_assert (GB_IS_BEAUTIFIER_EDITOR_ADDIN (self));
   g_assert (!dzl_str_empty0 (text));
@@ -207,28 +172,29 @@ gb_beautifier_helper_create_tmp_file_async (GbBeautifierEditorAddin *self,
 
   task = g_task_new (self, cancellable, callback, user_data);
   g_task_set_source_tag (task, gb_beautifier_helper_create_tmp_file_async);
-  state = g_slice_new0 (SaveTmpState);
-  state->self = self;
-  g_task_set_task_data (task, state, save_tmp_state_free);
 
-  if (NULL == (file = g_file_new_tmp ("gnome-builder-beautifier-XXXXXX.txt", &stream, &error)))
+  tmp_path = g_build_filename (self->tmp_dir, "XXXXXX.txt", NULL);
+  if (-1 == (fd = g_mkstemp (tmp_path)))
     {
-      g_task_return_error (task, g_steal_pointer (&error));
+      g_task_return_new_error (task,
+                               G_IO_ERROR,
+                               G_IO_ERROR_FAILED,
+                               "Failed to create temporary file for the Beautifier plugin");
       return;
     }
 
-  state->file = file;
-  state->stream = stream;
-  state->len = strlen (text);
-
-  output_stream = g_io_stream_get_output_stream ((GIOStream *)stream);
-  g_output_stream_write_all_async (output_stream,
-                                   text,
-                                   state->len,
-                                   G_PRIORITY_DEFAULT,
-                                   cancellable,
-                                   gb_beautifier_helper_create_tmp_file_cb,
-                                   g_steal_pointer (&task));
+  g_close (fd, NULL);
+  file = g_file_new_for_path (tmp_path);
+
+  g_file_replace_contents_async (file,
+                                 text,
+                                 strlen (text),
+                                 NULL,
+                                 FALSE,
+                                 G_FILE_CREATE_REPLACE_DESTINATION,
+                                 NULL,
+                                 gb_beautifier_helper_create_tmp_file_cb,
+                                 g_steal_pointer (&task));
 }
 
 GFile *
diff --git a/src/plugins/beautifier/gb-beautifier-process.c b/src/plugins/beautifier/gb-beautifier-process.c
index 218836056..9b8b789d1 100644
--- a/src/plugins/beautifier/gb-beautifier-process.c
+++ b/src/plugins/beautifier/gb-beautifier-process.c
@@ -166,8 +166,16 @@ gb_beautifier_process_create_for_clang_format (GbBeautifierEditorAddin  *self,
   g_assert (!dzl_str_empty0 (src_path));
   g_assert (!dzl_str_empty0 (state->lang_id));
 
-  if (NULL == (tmp_workdir = g_dir_make_tmp ("gnome-builder-beautify-XXXXXX", error)))
-    return NULL;
+  tmp_workdir = g_build_filename (self->tmp_dir, "clang-XXXXXX.txt", NULL);
+  if (g_mkdtemp (tmp_workdir) == NULL)
+    {
+      g_set_error (error,
+                   G_IO_ERROR,
+                   G_IO_ERROR_FAILED,
+                   "Failed to create temporary directory for the Beautifier plugin");
+
+      return NULL;
+    }
 
   state->tmp_workdir_file = g_file_new_for_path (tmp_workdir);
   tmp_config_path = g_build_filename (tmp_workdir,


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