Blame gegl/module/gegldatafiles.c

Packit bc1512
/* this file is part of GEGL
Packit bc1512
 *
Packit bc1512
 * Datafiles module copyight (C) 1996 Federico Mena Quintero
Packit bc1512
 * federico@nuclecu.unam.mx
Packit bc1512
 *
Packit bc1512
 * This library is free software; you can redistribute it and/or
Packit bc1512
 * modify it under the terms of the GNU Lesser General Public
Packit bc1512
 * License as published by the Free Software Foundation; either
Packit bc1512
 * version 3 of the License, or (at your option) any later version.
Packit bc1512
 *
Packit bc1512
 * This library is distributed in the hope that it will be useful,
Packit bc1512
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit bc1512
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit bc1512
 * Lesser General Public License for more details.
Packit bc1512
 *
Packit bc1512
 * You should have received a copy of the GNU Lesser General Public
Packit bc1512
 * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
Packit bc1512
 *
Packit bc1512
 * Datafiles module copyight (C) 1996 Federico Mena Quintero
Packit bc1512
 * federico@nuclecu.unam.mx
Packit bc1512
 */
Packit bc1512
Packit bc1512
#include "config.h"
Packit bc1512
Packit bc1512
#include <string.h>
Packit bc1512
Packit bc1512
#include <sys/types.h>
Packit bc1512
#ifdef HAVE_UNISTD_H
Packit bc1512
#include <unistd.h>
Packit bc1512
#endif
Packit bc1512
Packit bc1512
#include <glib-object.h>
Packit bc1512
#include <glib/gstdio.h>
Packit bc1512
Packit bc1512
#ifdef G_OS_WIN32
Packit bc1512
#ifndef S_ISREG
Packit bc1512
#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
Packit bc1512
#endif
Packit bc1512
#ifndef S_ISDIR
Packit bc1512
#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
Packit bc1512
#endif
Packit bc1512
#ifndef S_IXUSR
Packit bc1512
#define S_IXUSR _S_IEXEC
Packit bc1512
#endif
Packit bc1512
#endif
Packit bc1512
Packit bc1512
/*
Packit bc1512
#include "geglbasetypes.h"*/
Packit bc1512
Packit bc1512
#include "gegldatafiles.h"
Packit bc1512
Packit bc1512
gboolean
Packit bc1512
gegl_datafiles_check_extension (const gchar *filename,
Packit bc1512
                                const gchar *extension)
Packit bc1512
{
Packit bc1512
  gint name_len;
Packit bc1512
  gint ext_len;
Packit bc1512
Packit bc1512
  g_return_val_if_fail (filename != NULL, FALSE);
Packit bc1512
  g_return_val_if_fail (extension != NULL, FALSE);
Packit bc1512
Packit bc1512
  name_len = strlen (filename);
Packit bc1512
  ext_len  = strlen (extension);
Packit bc1512
Packit bc1512
  if (! (name_len && ext_len && (name_len > ext_len)))
Packit bc1512
    return FALSE;
Packit bc1512
Packit bc1512
  return (g_ascii_strcasecmp (&filename[name_len - ext_len], extension) == 0);
Packit bc1512
}
Packit bc1512
Packit bc1512
/**
Packit bc1512
 * gegl_path_parse:
Packit bc1512
 * @path:         A list of directories separated by #G_SEARCHPATH_SEPARATOR.
Packit bc1512
 * @max_paths:    The maximum number of directories to return.
Packit bc1512
 * @check:        %TRUE if you want the directories to be checked.
Packit bc1512
 * @check_failed: Returns a #GList of path elements for which the
Packit bc1512
 *                check failed.
Packit bc1512
 *
Packit bc1512
 * Returns: A #GList of all directories in @path.
Packit bc1512
 **/
Packit bc1512
static GList *
Packit bc1512
gegl_path_parse (const gchar  *path,
Packit bc1512
                 gint          max_paths,
Packit bc1512
                 gboolean      check,
Packit bc1512
                 GList       **check_failed)
Packit bc1512
{
Packit bc1512
  const gchar  *home;
Packit bc1512
  gchar       **patharray;
Packit bc1512
  GList        *list      = NULL;
Packit bc1512
  GList        *fail_list = NULL;
Packit bc1512
  gint          i;
Packit bc1512
  gboolean      exists    = TRUE;
Packit bc1512
Packit bc1512
  if (!path || !*path || max_paths < 1 || max_paths > 256)
Packit bc1512
    return NULL;
Packit bc1512
Packit bc1512
  home = g_get_home_dir ();
Packit bc1512
Packit bc1512
  patharray = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, max_paths);
Packit bc1512
Packit bc1512
  for (i = 0; i < max_paths; i++)
Packit bc1512
    {
Packit bc1512
      GString *dir;
Packit bc1512
Packit bc1512
      if (! patharray[i])
Packit bc1512
        break;
Packit bc1512
Packit bc1512
#ifndef G_OS_WIN32
Packit bc1512
      if (*patharray[i] == '~')
Packit bc1512
        {
Packit bc1512
          dir = g_string_new (home);
Packit bc1512
          g_string_append (dir, patharray[i] + 1);
Packit bc1512
        }
Packit bc1512
      else
Packit bc1512
#endif
Packit bc1512
        {
Packit bc1512
          dir = g_string_new (patharray[i]);
Packit bc1512
        }
Packit bc1512
Packit bc1512
      if (check)
Packit bc1512
        exists = g_file_test (dir->str, G_FILE_TEST_IS_DIR);
Packit bc1512
Packit bc1512
      if (exists)
Packit bc1512
        list = g_list_prepend (list, g_strdup (dir->str));
Packit bc1512
      else if (check_failed)
Packit bc1512
        fail_list = g_list_prepend (fail_list, g_strdup (dir->str));
Packit bc1512
Packit bc1512
      g_string_free (dir, TRUE);
Packit bc1512
    }
Packit bc1512
Packit bc1512
  g_strfreev (patharray);
Packit bc1512
Packit bc1512
  list = g_list_reverse (list);
Packit bc1512
Packit bc1512
  if (check && check_failed)
Packit bc1512
    {
Packit bc1512
      fail_list = g_list_reverse (fail_list);
Packit bc1512
      *check_failed = fail_list;
Packit bc1512
    }
Packit bc1512
Packit bc1512
  return list;
Packit bc1512
}
Packit bc1512
Packit bc1512
/**
Packit bc1512
 * gegl_path_free:
Packit bc1512
 * @path: A list of directories as returned by gegl_path_parse().
Packit bc1512
 *
Packit bc1512
 * This function frees the memory allocated for the list and the strings
Packit bc1512
 * it contains.
Packit bc1512
 **/
Packit bc1512
static void
Packit bc1512
gegl_path_free (GList *path)
Packit bc1512
{
Packit bc1512
  g_list_foreach (path, (GFunc) g_free, NULL);
Packit bc1512
  g_list_free (path);
Packit bc1512
}
Packit bc1512
Packit bc1512
void
Packit bc1512
gegl_datafiles_read_directories (const gchar            *path_str,
Packit bc1512
                                 GFileTest               flags,
Packit bc1512
                                 GeglDatafileLoaderFunc  loader_func,
Packit bc1512
                                 gpointer                user_data)
Packit bc1512
{
Packit bc1512
  GeglDatafileData  file_data;
Packit bc1512
  struct stat       filestat;
Packit bc1512
  gchar            *local_path;
Packit bc1512
  GList            *path;
Packit bc1512
  GList            *list;
Packit bc1512
  gchar            *filename;
Packit bc1512
  gint              err;
Packit bc1512
  GDir             *dir;
Packit bc1512
  const gchar      *dir_ent;
Packit bc1512
Packit bc1512
  g_return_if_fail (path_str != NULL);
Packit bc1512
  g_return_if_fail (loader_func != NULL);
Packit bc1512
Packit bc1512
  local_path = g_strdup (path_str);
Packit bc1512
Packit bc1512
  path = gegl_path_parse (local_path, 16, TRUE, NULL);
Packit bc1512
Packit bc1512
  for (list = path; list; list = g_list_next (list))
Packit bc1512
    {
Packit bc1512
      const gchar *dirname = list->data;
Packit bc1512
Packit bc1512
      dir = g_dir_open (dirname, 0, NULL);
Packit bc1512
Packit bc1512
      if (dir)
Packit bc1512
        {
Packit bc1512
          while ((dir_ent = g_dir_read_name (dir)))
Packit bc1512
            {
Packit bc1512
              filename = g_build_filename (dirname, dir_ent, NULL);
Packit bc1512
Packit bc1512
              err = g_stat (filename, &filestat);
Packit bc1512
Packit bc1512
              file_data.filename = filename;
Packit bc1512
              file_data.dirname  = dirname;
Packit bc1512
              file_data.basename = dir_ent;
Packit bc1512
              file_data.atime    = filestat.st_atime;
Packit bc1512
              file_data.mtime    = filestat.st_mtime;
Packit bc1512
              file_data.ctime    = filestat.st_ctime;
Packit bc1512
Packit bc1512
              if (! err)
Packit bc1512
                {
Packit bc1512
                  if (/*(flags & G_FILE_TEST_IS_DIR) &&*/
Packit bc1512
                           S_ISDIR (filestat.st_mode))
Packit bc1512
                    {
Packit bc1512
                      gegl_datafiles_read_directories (filename,
Packit bc1512
                                                       flags,
Packit bc1512
                                                       loader_func,
Packit bc1512
                                                       user_data);
Packit bc1512
                    }
Packit bc1512
                  else if (flags & G_FILE_TEST_EXISTS)
Packit bc1512
                    {
Packit bc1512
                      (* loader_func) (&file_data, user_data);
Packit bc1512
                    }
Packit bc1512
                  else if ((flags & G_FILE_TEST_IS_REGULAR) &&
Packit bc1512
                           S_ISREG (filestat.st_mode))
Packit bc1512
                    {
Packit bc1512
                      (* loader_func) (&file_data, user_data);
Packit bc1512
                    }
Packit bc1512
#ifndef G_OS_WIN32
Packit bc1512
                  else if ((flags & G_FILE_TEST_IS_SYMLINK) &&
Packit bc1512
                           S_ISLNK (filestat.st_mode))
Packit bc1512
                    {
Packit bc1512
                      (* loader_func) (&file_data, user_data);
Packit bc1512
                    }
Packit bc1512
#endif
Packit bc1512
                  else if ((flags & G_FILE_TEST_IS_EXECUTABLE) &&
Packit bc1512
                           (((filestat.st_mode & S_IXUSR) &&
Packit bc1512
                             !S_ISDIR (filestat.st_mode)) ||
Packit bc1512
                            (S_ISREG (filestat.st_mode) /*&&
Packit bc1512
                             is_script (filename)*/)))
Packit bc1512
                    {
Packit bc1512
                      (* loader_func) (&file_data, user_data);
Packit bc1512
                    }
Packit bc1512
                }
Packit bc1512
Packit bc1512
              g_free (filename);
Packit bc1512
            }
Packit bc1512
Packit bc1512
          g_dir_close (dir);
Packit bc1512
        }
Packit bc1512
    }
Packit bc1512
Packit bc1512
  gegl_path_free (path);
Packit bc1512
  g_free (local_path);
Packit bc1512
}