Blame gio/gio-tool-list.c

Packit ae235b
/*
Packit ae235b
 * Copyright 2015 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Matthias Clasen <mclasen@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <gi18n.h>
Packit ae235b
Packit ae235b
#include "gio-tool.h"
Packit ae235b
Packit ae235b
Packit ae235b
static char *attributes = NULL;
Packit ae235b
static gboolean show_hidden = FALSE;
Packit ae235b
static gboolean show_long = FALSE;
Packit ae235b
static gboolean nofollow_symlinks = FALSE;
Packit ae235b
static gboolean print_uris = FALSE;
Packit ae235b
Packit ae235b
static const GOptionEntry entries[] = {
Packit ae235b
  { "attributes", 'a', 0, G_OPTION_ARG_STRING, &attributes, N_("The attributes to get"), N_("ATTRIBUTES") },
Packit ae235b
  { "hidden", 'h', 0, G_OPTION_ARG_NONE, &show_hidden, N_("Show hidden files"), NULL },
Packit ae235b
  { "long", 'l', 0, G_OPTION_ARG_NONE, &show_long, N_("Use a long listing format"), NULL },
Packit ae235b
  { "nofollow-symlinks", 'n', 0, G_OPTION_ARG_NONE, &nofollow_symlinks, N_("Don’t follow symbolic links"), NULL},
Packit ae235b
  { "print-uris", 'u', 0, G_OPTION_ARG_NONE, &print_uris, N_("Print full URIs"), NULL},
Packit ae235b
  { NULL }
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
show_file_listing (GFileInfo *info, GFile *parent)
Packit ae235b
{
Packit ae235b
  const char *name, *type;
Packit ae235b
  char *uri = NULL;
Packit ae235b
  goffset size;
Packit ae235b
  char **attributes;
Packit ae235b
  int i;
Packit ae235b
  gboolean first_attr;
Packit ae235b
  GFile *child;
Packit ae235b
Packit ae235b
  if ((g_file_info_get_is_hidden (info)) && !show_hidden)
Packit ae235b
    return;
Packit ae235b
Packit ae235b
  name = g_file_info_get_name (info);
Packit ae235b
  if (name == NULL)
Packit ae235b
    name = "";
Packit ae235b
Packit ae235b
  if (print_uris) {
Packit ae235b
    child = g_file_get_child (parent, name);
Packit ae235b
    uri = g_file_get_uri (child);
Packit ae235b
    g_object_unref (child);
Packit ae235b
  }
Packit ae235b
Packit ae235b
  size = g_file_info_get_size (info);
Packit ae235b
  type = file_type_to_string (g_file_info_get_file_type (info));
Packit ae235b
  if (show_long)
Packit ae235b
    g_print ("%s\t%"G_GUINT64_FORMAT"\t(%s)", print_uris? uri: name, (guint64)size, type);
Packit ae235b
  else
Packit ae235b
    g_print ("%s", print_uris? uri: name);
Packit ae235b
Packit ae235b
  if (print_uris)
Packit ae235b
    g_free (uri);
Packit ae235b
Packit ae235b
  first_attr = TRUE;
Packit ae235b
  attributes = g_file_info_list_attributes (info, NULL);
Packit ae235b
  for (i = 0 ; attributes[i] != NULL; i++)
Packit ae235b
    {
Packit ae235b
      char *val_as_string;
Packit ae235b
Packit ae235b
      if (!show_long ||
Packit ae235b
          strcmp (attributes[i], G_FILE_ATTRIBUTE_STANDARD_NAME) == 0 ||
Packit ae235b
          strcmp (attributes[i], G_FILE_ATTRIBUTE_STANDARD_SIZE) == 0 ||
Packit ae235b
          strcmp (attributes[i], G_FILE_ATTRIBUTE_STANDARD_TYPE) == 0 ||
Packit ae235b
          strcmp (attributes[i], G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN) == 0)
Packit ae235b
        continue;
Packit ae235b
Packit ae235b
      if (first_attr)
Packit ae235b
        {
Packit ae235b
          g_print ("\t");
Packit ae235b
          first_attr = FALSE;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        g_print (" ");
Packit ae235b
      val_as_string = g_file_info_get_attribute_as_string (info, attributes[i]);
Packit ae235b
      g_print ("%s=%s", attributes[i], val_as_string);
Packit ae235b
      g_free (val_as_string);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_strfreev (attributes);
Packit ae235b
Packit ae235b
  g_print ("\n");
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
list (GFile *file)
Packit ae235b
{
Packit ae235b
  GFileEnumerator *enumerator;
Packit ae235b
  GFileInfo *info;
Packit ae235b
  GError *error;
Packit ae235b
  gboolean res;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  enumerator = g_file_enumerate_children (file,
Packit ae235b
                                          attributes,
Packit ae235b
                                          nofollow_symlinks ? G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS : 0,
Packit ae235b
                                          NULL,
Packit ae235b
                                          &error);
Packit ae235b
  if (enumerator == NULL)
Packit ae235b
    {
Packit ae235b
      print_file_error (file, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  res = TRUE;
Packit ae235b
  while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)) != NULL)
Packit ae235b
    {
Packit ae235b
      show_file_listing (info, file);
Packit ae235b
      g_object_unref (info);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (error)
Packit ae235b
    {
Packit ae235b
      print_file_error (file, error->message);
Packit ae235b
      g_clear_error (&error);
Packit ae235b
      res = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_file_enumerator_close (enumerator, NULL, &error))
Packit ae235b
    {
Packit ae235b
      print_file_error (file, error->message);
Packit ae235b
      g_clear_error (&error);
Packit ae235b
      res = FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
handle_list (int argc, char *argv[], gboolean do_help)
Packit ae235b
{
Packit ae235b
  GOptionContext *context;
Packit ae235b
  gchar *param;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gboolean res;
Packit ae235b
  gint i;
Packit ae235b
  GFile *file;
Packit ae235b
Packit ae235b
  g_set_prgname ("gio list");
Packit ae235b
Packit ae235b
  /* Translators: commandline placeholder */
Packit ae235b
  param = g_strdup_printf ("[%s...]", _("LOCATION"));
Packit ae235b
  context = g_option_context_new (param);
Packit ae235b
  g_free (param);
Packit ae235b
  g_option_context_set_help_enabled (context, FALSE);
Packit ae235b
  g_option_context_set_summary (context,
Packit ae235b
      _("List the contents of the locations."));
Packit ae235b
  g_option_context_set_description (context,
Packit ae235b
      _("gio list is similar to the traditional ls utility, but using GIO\n"
Packit ae235b
        "locations instead of local files: for example, you can use something\n"
Packit ae235b
        "like smb://server/resource/file.txt as location. File attributes can\n"
Packit ae235b
        "be specified with their GIO name, e.g. standard::icon"));
Packit ae235b
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Packit ae235b
Packit ae235b
  if (do_help)
Packit ae235b
    {
Packit ae235b
      show_help (context, NULL);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!g_option_context_parse (context, &argc, &argv, &error))
Packit ae235b
    {
Packit ae235b
      show_help (context, error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_option_context_free (context);
Packit ae235b
Packit ae235b
  if (attributes != NULL)
Packit ae235b
    show_long = TRUE;
Packit ae235b
Packit ae235b
  attributes = g_strconcat (G_FILE_ATTRIBUTE_STANDARD_NAME ","
Packit ae235b
                            G_FILE_ATTRIBUTE_STANDARD_TYPE ","
Packit ae235b
                            G_FILE_ATTRIBUTE_STANDARD_SIZE ","
Packit ae235b
                            G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN,
Packit ae235b
                            attributes != NULL ? "," : "",
Packit ae235b
                            attributes,
Packit ae235b
                            NULL);
Packit ae235b
Packit ae235b
  res = TRUE;
Packit ae235b
  if (argc > 1)
Packit ae235b
    {
Packit ae235b
      for (i = 1; i < argc; i++)
Packit ae235b
        {
Packit ae235b
          file = g_file_new_for_commandline_arg (argv[i]);
Packit ae235b
          res &= list (file);
Packit ae235b
          g_object_unref (file);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      char *cwd;
Packit ae235b
Packit ae235b
      cwd = g_get_current_dir ();
Packit ae235b
      file = g_file_new_for_path (cwd);
Packit ae235b
      res = list (file);
Packit ae235b
      g_object_unref (file);
Packit ae235b
      g_free (cwd);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (attributes);
Packit ae235b
Packit ae235b
  return res ? 0 : 2;
Packit ae235b
}