GFileEnumerator *
_g_local_file_enumerator_new (GLocalFile *file,
			      const char           *attributes,
			      GFileQueryInfoFlags   flags,
			      GCancellable         *cancellable,
			      GError              **error)
{
  GLocalFileEnumerator *local;
  char *filename = g_file_get_path (G_FILE (file));

#ifdef USE_GDIR
  GError *dir_error;
  GDir *dir;
  
  dir_error = NULL;
  dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
  if (dir == NULL) 
    {
      if (error != NULL)
	{
	  convert_file_to_io_error (error, dir_error);
	  g_error_free (dir_error);
	}
      g_free (filename);
      return NULL;
    }
#else
  DIR *dir;
  int errsv;

  dir = opendir (filename);
  if (dir == NULL)
    {
      errsv = errno;

      g_set_error_literal (error, G_IO_ERROR,
                           g_io_error_from_errno (errsv),
                           g_strerror (errsv));
      g_free (filename);
      return NULL;
    }

#endif
  
  local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR,
                        "container", file,
                        NULL);

  local->dir = dir;
  local->filename = filename;
  local->matcher = g_file_attribute_matcher_new (attributes);
#ifndef USE_GDIR
  local->reduced_matcher = g_file_attribute_matcher_subtract_attributes (local->matcher,
                                                                         G_LOCAL_FILE_INFO_NOSTAT_ATTRIBUTES","
                                                                         "standard::type");
#endif
  local->flags = flags;
  
  return G_FILE_ENUMERATOR (local);
}
static void
delete_batch (GObject      *source,
              GAsyncResult *res,
              gpointer      user_data)
{
        GFileEnumerator *enumerator = G_FILE_ENUMERATOR (source);
        DeleteData *data = user_data;
        GList *files, *f;
        GFile *child_file;
        DeleteData *child;
        GFileInfo *info;
        GError *error = NULL;

        files = g_file_enumerator_next_files_finish (enumerator, res, &error);

        g_debug ("GsdHousekeeping: purging %d children of %s", g_list_length (files), data->name);

        if (files) {
                for (f = files; f; f = f->next) {
                        if (g_cancellable_is_cancelled (data->cancellable))
                                break;
                        info = f->data;

                        child_file = g_file_get_child (data->file, g_file_info_get_name (info));
                        child = delete_data_new (child_file,
                                                 data->cancellable,
                                                 data->old,
                                                 data->dry_run,
                                                 data->trash,
                                                 data->depth + 1);
                        delete_recursively_by_age (child);
                        delete_data_unref (child);
                        g_object_unref (child_file);
                }
                g_list_free_full (files, g_object_unref);
                if (!g_cancellable_is_cancelled (data->cancellable)) {
                        g_file_enumerator_next_files_async (enumerator, 20,
                                                            0,
                                                            data->cancellable,
                                                            delete_batch,
                                                            data);
                        return;
                }
        }

        g_file_enumerator_close (enumerator, data->cancellable, NULL);
        g_object_unref (enumerator);

        if (data->depth > 0 && !g_cancellable_is_cancelled (data->cancellable)) {
                if ((data->trash && data->depth > 1) ||
                     should_purge_file (data->file, data->cancellable, data->old)) {
                        g_debug ("GsdHousekeeping: purging %s\n", data->name);
                        if (!data->dry_run) {
                                g_file_delete (data->file, data->cancellable, NULL);
                        }
                }
        }
        delete_data_unref (data);
}
static void
file_info_async_ready (GObject      *source,
                       GAsyncResult *res,
                       gpointer      user_data)
{
  BgPicturesSource *bg_source;
  GList *files, *l;
  GError *err = NULL;
  GFile *parent;

  files = g_file_enumerator_next_files_finish (G_FILE_ENUMERATOR (source),
                                               res,
                                               &err);
  if (err)
    {
      if (!g_error_matches (err, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("Could not get pictures file information: %s", err->message);
      g_error_free (err);

      g_list_foreach (files, (GFunc) g_object_unref, NULL);
      g_list_free (files);
      return;
    }

  bg_source = BG_PICTURES_SOURCE (user_data);

  parent = g_file_enumerator_get_container (G_FILE_ENUMERATOR (source));

  files = g_list_sort (files, file_sort_func);

  /* iterate over the available files */
  for (l = files; l; l = g_list_next (l))
    {
      GFileInfo *info = l->data;
      GFile *file;

      file = g_file_get_child (parent, g_file_info_get_name (info));

      add_single_file_from_info (bg_source, file, info, NULL);
    }

  g_list_foreach (files, (GFunc) g_object_unref, NULL);
  g_list_free (files);
}
示例#4
0
static void
plugin_manager_next_files_ready (GObject      *object,
				 GAsyncResult *result,
				 gpointer      user_data)
{
	GigglePluginManager     *manager = user_data;
	GigglePluginManagerPriv *priv = GET_PRIV (manager);
	GFileEnumerator         *children = G_FILE_ENUMERATOR (object);
	GError                  *error = NULL;
	GList                   *files, *l;

	files = g_file_enumerator_next_files_finish (children,
						     result, &error);

	if (error) {
		g_warning ("%s: %s", G_STRFUNC, error->message);
		g_clear_error (&error);
	}

	for (l = files; l; l = g_list_delete_link (l, l)) {
		const char   *name = g_file_info_get_name (l->data);
		char         *filename;
		GigglePlugin *plugin;
		GFile        *file;

		if (g_str_has_suffix (name, ".xml")) {
			file = g_file_get_child (priv->plugin_dir, name);
			filename = g_file_get_path (file);

			plugin = giggle_plugin_new_from_file (filename, &error);

			g_object_unref (file);
			g_free (filename);

			if (plugin) {
				giggle_plugin_set_manager (plugin, manager);
				priv->plugins = g_list_prepend (priv->plugins, plugin);
				g_signal_emit (manager, signals[PLUGIN_ADDED], 0, plugin);
			} else {
				g_warning ("%s: %s", G_STRFUNC, error->message);
				g_clear_error (&error);
			}

		}

		g_object_unref (l->data);
	}

	if (files) {
		g_file_enumerator_next_files_async (children, 16,
						    G_PRIORITY_DEFAULT, priv->cancellable,
						    plugin_manager_next_files_ready, user_data);
	}
}
示例#5
0
static void
g_file_enumerator_finalize (GObject *object)
{
  GFileEnumerator *enumerator;

  enumerator = G_FILE_ENUMERATOR (object);
  
  if (!enumerator->priv->closed)
    g_file_enumerator_close (enumerator, NULL, NULL);

  G_OBJECT_CLASS (g_file_enumerator_parent_class)->finalize (object);
}
示例#6
0
static void
g_file_enumerator_dispose (GObject *object)
{
  GFileEnumerator *enumerator;

  enumerator = G_FILE_ENUMERATOR (object);
  
  if (enumerator->priv->container) {
    g_object_unref (enumerator->priv->container);
    enumerator->priv->container = NULL;
  }

  G_OBJECT_CLASS (g_file_enumerator_parent_class)->dispose (object);
}
示例#7
0
GFileEnumerator *
_g_local_file_enumerator_new (const char           *filename,
			      const char           *attributes,
			      GFileQueryInfoFlags   flags,
			      GCancellable         *cancellable,
			      GError              **error)
{
  GLocalFileEnumerator *local;

#ifdef USE_GDIR
  GError *dir_error;
  GDir *dir;
  
  dir_error = NULL;
  dir = g_dir_open (filename, 0, error != NULL ? &dir_error : NULL);
  if (dir == NULL) 
    {
      if (error != NULL)
	{
	  convert_file_to_io_error (error, dir_error);
	  g_error_free (dir_error);
	}
      return NULL;
    }
#else
  DIR *dir;
  int errsv;

  dir = opendir (filename);
  if (dir == NULL)
    {
      errsv = errno;

      g_set_error (error, G_IO_ERROR,
		   g_io_error_from_errno (errsv),
		   "%s", g_strerror (errsv));
      return NULL;
    }

#endif
  
  local = g_object_new (G_TYPE_LOCAL_FILE_ENUMERATOR, NULL);

  local->dir = dir;
  local->filename = g_strdup (filename);
  local->matcher = g_file_attribute_matcher_new (attributes);
  local->flags = flags;
  
  return G_FILE_ENUMERATOR (local);
}
示例#8
0
static void
enumerate_next_files_ready_cb (GObject *source,
                               GAsyncResult *res,
                               gpointer user_data)
{
  GFileEnumerator *enumerator = G_FILE_ENUMERATOR (source);
  BijiNoteBook *self;
  GList *files, *l;
  GError *error = NULL;
  gchar *base_path;

  files = g_file_enumerator_next_files_finish (enumerator, res, &error);

  if (error != NULL)
    {
      load_location_error (g_file_enumerator_get_container (enumerator), error);
      return;
    }

  self = user_data;
  base_path = g_file_get_path (self->priv->location);

  // now load the notes
  for (l = files; l != NULL; l = l->next)
    {
      GFileInfo *info;
      const gchar *name;
      gchar *path;
      BijiNoteObj *note;

      info = l->data;
      name = g_file_info_get_name (info);

      if (!g_str_has_suffix (name, ".note"))
        continue;

      path = g_build_filename (base_path, name, NULL);
      note = biji_note_get_new_from_file (path);

      _biji_note_book_add_one_note (self, note);

      g_free (path);
    }

  g_free (base_path);
  g_list_free_full (files, g_object_unref);
}
示例#9
0
static void
g_file_enumerator_set_property (GObject      *object,
                                guint         property_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
  GFileEnumerator *enumerator;
  
  enumerator = G_FILE_ENUMERATOR (object);
  
  switch (property_id) {
  case PROP_CONTAINER:
    enumerator->priv->container = g_value_dup_object (value);
    break;
  default:
    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    break;
  }
}
示例#10
0
static GFileEnumerator *
_g_resource_file_enumerator_new (GResourceFile *file,
				 const char           *attributes,
				 GFileQueryInfoFlags   flags,
				 GCancellable         *cancellable,
				 GError              **error)
{
  GResourceFileEnumerator *resource;
  char **children;
  gboolean res;

  children = g_resources_enumerate_children (file->path, 0, NULL);
  if (children == NULL &&
      strcmp ("/", file->path) != 0)
    {
      res = g_resources_get_info (file->path, 0, NULL, NULL, NULL);
      if (res)
	g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY,
		     _("The resource at '%s' is not a directory"),
		     file->path);
      else
	g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
		     _("The resource at '%s' does not exist"),
		     file->path);
      return NULL;
    }

  resource = g_object_new (G_TYPE_RESOURCE_FILE_ENUMERATOR,
			   "container", file,
			   NULL);

  resource->children = children;
  resource->path = g_strdup (file->path);
  resource->attributes = g_strdup (attributes);
  resource->flags = flags;

  return G_FILE_ENUMERATOR (resource);
}
示例#11
0
static void
on_files_listed (GObject *source_object,
                 GAsyncResult *res,
                 gpointer user_data)
{
  GError *error = NULL;
  JsonObject *options;
  GList *files;

  files = g_file_enumerator_next_files_finish (G_FILE_ENUMERATOR (source_object), res, &error);
  if (error)
    {
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        {
          CockpitFslist *self = COCKPIT_FSLIST (user_data);
          g_message ("%s: couldn't process files %s", COCKPIT_FSLIST(user_data)->path, error->message);
          options = cockpit_channel_close_options (COCKPIT_CHANNEL (self));
          json_object_set_string_member (options, "message", error->message);
          cockpit_channel_close (COCKPIT_CHANNEL (self), "internal-error");
        }
      g_clear_error (&error);
      return;
    }

  CockpitFslist *self = COCKPIT_FSLIST (user_data);

  if (files == NULL)
    {
      JsonObject *msg;
      GBytes *msg_bytes;

      msg = json_object_new ();
      json_object_set_string_member (msg, "event", "present-done");
      msg_bytes = cockpit_json_write_bytes (msg);
      json_object_unref (msg);
      cockpit_channel_send (COCKPIT_CHANNEL(self), msg_bytes, FALSE);
      g_bytes_unref (msg_bytes);

      g_clear_object (&self->cancellable);
      g_object_unref (source_object);

      if (self->monitor == NULL)
        {
          cockpit_channel_done (COCKPIT_CHANNEL (self));
          cockpit_channel_close (COCKPIT_CHANNEL (self), NULL);
        }
      return;
    }

  for (GList *l = files; l; l = l->next)
    {
      GFileInfo *info = G_FILE_INFO (l->data);
      JsonObject *msg;
      GBytes *msg_bytes;

      msg = json_object_new ();
      json_object_set_string_member (msg, "event", "present");
      json_object_set_string_member
        (msg, "path", g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_STANDARD_NAME));
      json_object_set_string_member
        (msg, "type", cockpit_file_type_to_string (g_file_info_get_file_type (info)));
      msg_bytes = cockpit_json_write_bytes (msg);
      json_object_unref (msg);
      cockpit_channel_send (COCKPIT_CHANNEL(self), msg_bytes, FALSE);
      g_bytes_unref (msg_bytes);
    }

  g_list_free_full (files, g_object_unref);

  g_file_enumerator_next_files_async (G_FILE_ENUMERATOR (source_object),
                                      10,
                                      G_PRIORITY_DEFAULT,
                                      self->cancellable,
                                      on_files_listed,
                                      self);
}
示例#12
0
static void
enumerate_next_files_async_cb (GObject *source,
                               GAsyncResult *res,
                               gpointer user_data)
{
  EnumerateJob *job = user_data;
  GList *enumerated_files, *l;
  GFileInfo *info;
  GSList *logs;
  const char *content_type, *name;
  char *parse_string, *container_path;
  GFileType type;
  GFile *container;

  enumerated_files = g_file_enumerator_next_files_finish (G_FILE_ENUMERATOR (source),
                                                          res, NULL);
  if (!enumerated_files) {
    enumerate_job_finish (job);
    return;
  }

  logs = job->logs;
  container = g_file_enumerator_get_container (G_FILE_ENUMERATOR (source));
  container_path = g_file_get_path (container);

  /* TODO: we don't support grouping rotated logs yet, skip gzipped files
   * and those which name contains a formatted date.
   */
  for (l = enumerated_files; l; l = l->next) {
    info = l->data;
    type = g_file_info_get_file_type (info);
    content_type = g_file_info_get_content_type (info);
    name = g_file_info_get_name (info);
    
    if (!g_file_info_get_attribute_boolean (info, "access::can-read")) {
      g_object_unref (info);
      continue;
    }

    if (type != (G_FILE_TYPE_REGULAR || G_FILE_TYPE_SYMBOLIC_LINK) ||
        !g_content_type_is_a (content_type, "text/plain"))
    {
      g_object_unref (info);
      continue;
    }

    if (g_content_type_is_a (content_type, "application/x-gzip")) {
      g_object_unref (info);
      continue;
    }

    if (g_regex_match_simple ("\\d{8}$", name, 0, 0)) {
      g_object_unref (info);
      continue;
    }

    parse_string = g_build_filename (container_path, name, NULL);

    if (g_slist_find_custom (logs, parse_string, (GCompareFunc) g_ascii_strcasecmp) == NULL) {
      logs = g_slist_append (logs, parse_string);
    } else {
      g_free (parse_string);
    }

    g_object_unref (info);
    parse_string = NULL;
  }

  g_list_free (enumerated_files);
  g_object_unref (container);
  g_free (container_path);

  job->logs = logs;

  enumerate_job_finish (job);
}
static void
enumerate_dir (MpdStorageDevice  *self)
{
  MpdStorageDevicePrivate *priv = GET_PRIVATE (self);
  GFileEnumerator *enumerator;
  GFileInfo       *info;
  GError          *error = NULL;

  g_return_if_fail (priv->dir_stack);
  enumerator = G_FILE_ENUMERATOR (priv->dir_stack->data);
  g_return_if_fail (G_IS_FILE_ENUMERATOR (enumerator));

  while (NULL !=
         (info = g_file_enumerator_next_file (enumerator, NULL, &error)))
  {
    char const *content_type = g_file_info_get_content_type (info);
    char const *name = g_file_info_get_name (info);

    /* Debug */
    char *indent = g_strnfill (g_slist_length (priv->dir_stack), ' ');
    g_debug ("%s%s %s", indent, name, content_type);
    g_free (indent);

    /* Do not recurse into "dot" directories, they are use for trash. */
    if (0 == g_strcmp0 ("inode/directory", content_type) &&
        name[0] != '.')
    {
      char const *path = g_object_get_data (G_OBJECT (enumerator), "path");
      char *subpath = g_build_filename (path, name, NULL);
      GFile *subdir = file_new_for_path (subpath);

      /* Push and recurse. */
      push_dir_async (self, subdir);

      file_destroy (subdir);
      g_free (subpath);
      break;

    } else if (g_str_has_prefix (content_type, "audio/") ||
               g_str_has_prefix (content_type, "image/") ||
               g_str_has_prefix (content_type, "video/")) {

      /* Media found. */
      char const *path = g_object_get_data (G_OBJECT (enumerator), "path");
      char *filename = g_build_filename (path, name, NULL);
      priv->media_files = g_slist_prepend (priv->media_files, filename);
      priv->media_files_size += g_file_info_get_size (info);
    }

    g_object_unref (info);
  }

  if (info)
  {
    /* Broke out of loop, not done yet. */
    g_object_unref (info);

  } else {

    /* Directory finished, pop. */
    g_object_unref (enumerator);
    priv->dir_stack = g_slist_delete_link (priv->dir_stack, priv->dir_stack);
    if (priv->dir_stack)
    {
      enumerate_dir (self);
    } else {
      /* Done iterating. */
      g_signal_emit_by_name (self, "has-media", (bool) priv->media_files);
    }
  }

  if (error)
  {
    g_critical ("%s : %s", G_STRLOC, error->message);
    g_clear_error (&error);
  }
}
示例#14
0
static void
enum_files_cb (GObject *obj, GAsyncResult *result, gpointer data)
{
	RBAndroidSource *source = RB_ANDROID_SOURCE (data);
	RBAndroidSourcePrivate *priv = GET_PRIVATE(source);
	GFileEnumerator *e = G_FILE_ENUMERATOR (obj);
	GError *error = NULL;
	GFileInfo *info;
	GList *files;
	GList *l;

	files = g_file_enumerator_next_files_finish (e, result, &error);
	if (error != NULL) {
		rb_debug ("error listing files: %s", error->message);
		music_dirs_done (source);
		return;
	}

	if (files == NULL) {
		priv->scanned++;
		g_object_unref (e);
		find_music_dirs (source);
		return;
	}

	for (l = files; l != NULL; l = l->next) {
		guint32 filetype;
		info = (GFileInfo *)l->data;

		filetype = g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
		if (filetype == G_FILE_TYPE_DIRECTORY) {
			GFile *dir;
			if (priv->scanned == 0) {

				rb_debug ("got storage container %s", g_file_info_get_name (info));
				dir = g_file_get_child (g_file_enumerator_get_container (e), g_file_info_get_name (info));
				g_queue_push_tail (&priv->to_scan, dir);
			} else if (g_ascii_strcasecmp (g_file_info_get_name (info), "music") == 0) {
				GFile *storage;
				char *uri;

				storage = g_file_enumerator_get_container (e);
				dir = g_file_get_child (storage, g_file_info_get_name (info));
				uri = g_file_get_uri (dir);
				rb_debug ("music dir found at %s", uri);

				/* keep the container around for space/capacity calculation */
				priv->storage = g_list_append (priv->storage, dir);

				rhythmdb_import_job_add_uri (priv->import_job, uri);
				g_free (uri);
			}
		}

		g_object_unref (info);
	}

	g_list_free (files);

	g_file_enumerator_next_files_async (G_FILE_ENUMERATOR (obj), 64, G_PRIORITY_DEFAULT, priv->cancel, enum_files_cb, source);
}