[gimp] app: add gimp_file_is_executable() to gimp-utils.[ch]



commit 9bb3aee1638909b6520271e40bc0706a5c189174
Author: Michael Natterer <mitch gimp org>
Date:   Mon Aug 4 02:38:24 2014 +0200

    app: add gimp_file_is_executable() to gimp-utils.[ch]
    
    including PATHEXT magic for windows.

 app/core/gimp-utils.c |   74 +++++++++++++++++++++++++++++++++++++++++++++++++
 app/core/gimp-utils.h |    1 +
 2 files changed, 75 insertions(+), 0 deletions(-)
---
diff --git a/app/core/gimp-utils.c b/app/core/gimp-utils.c
index f6a931c..e546aa6 100644
--- a/app/core/gimp-utils.c
+++ b/app/core/gimp-utils.c
@@ -947,6 +947,80 @@ gimp_file_compare (GFile *file1,
     }
 }
 
+static inline gboolean
+is_script (const gchar *filename)
+{
+#ifdef G_OS_WIN32
+  /* On Windows there is no concept like the Unix executable flag.
+   * There is a weak emulation provided by the MS C Runtime using file
+   * extensions (com, exe, cmd, bat). This needs to be extended to
+   * treat scripts (Python, Perl, ...) as executables, too. We use the
+   * PATHEXT variable, which is also used by cmd.exe.
+   */
+  static gchar **exts = NULL;
+
+  const gchar   *ext = strrchr (filename, '.');
+  gchar         *pathext;
+  gint           i;
+
+  if (exts == NULL)
+    {
+      pathext = g_getenv ("PATHEXT");
+      if (pathext != NULL)
+        {
+          exts = g_strsplit (pathext, G_SEARCHPATH_SEPARATOR_S, 100);
+        }
+      else
+        {
+          exts = g_new (gchar *, 1);
+          exts[0] = NULL;
+        }
+    }
+
+  for (i = 0; exts[i]; i++)
+    {
+      if (g_ascii_strcasecmp (ext, exts[i]) == 0)
+        return TRUE;
+    }
+#endif /* G_OS_WIN32 */
+
+  return FALSE;
+}
+
+gboolean
+gimp_file_is_executable (GFile *file)
+{
+  GFileInfo *info;
+  gboolean   executable = FALSE;
+
+  g_return_val_if_fail (G_IS_FILE (file), FALSE);
+
+  info = g_file_query_info (file,
+                            G_FILE_ATTRIBUTE_STANDARD_NAME ","
+                            G_FILE_ATTRIBUTE_STANDARD_TYPE ","
+                            G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE ",",
+                            G_FILE_QUERY_INFO_NONE,
+                            NULL, NULL);
+
+  if (info)
+    {
+      GFileType    file_type = g_file_info_get_file_type (info);
+      const gchar *filename  = g_file_info_get_name (info);
+
+      if (g_file_info_get_attribute_boolean (info,
+                                             G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE) ||
+          ((file_type == G_FILE_TYPE_REGULAR) &&
+           is_script (filename)))
+        {
+          executable = TRUE;
+        }
+
+      g_object_unref (info);
+    }
+
+  return executable;
+}
+
 
 /*  debug stuff  */
 
diff --git a/app/core/gimp-utils.h b/app/core/gimp-utils.h
index 197eeab..370487f 100644
--- a/app/core/gimp-utils.h
+++ b/app/core/gimp-utils.h
@@ -102,6 +102,7 @@ void         gimp_constrain_line                   (gdouble          start_x,
 
 gint         gimp_file_compare                     (GFile           *file1,
                                                     GFile           *file2);
+gboolean     gimp_file_is_executable               (GFile           *file);
 
 void         gimp_create_image_from_buffer         (Gimp            *gimp,
                                                     GeglBuffer      *buffer);


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