Blame gio/gio-tool-info.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 gboolean writable = FALSE;
Packit ae235b
static gboolean filesystem = FALSE;
Packit ae235b
static char *attributes = NULL;
Packit ae235b
static gboolean nofollow_symlinks = FALSE;
Packit ae235b
Packit ae235b
static const GOptionEntry entries[] = {
Packit ae235b
  { "query-writable", 'w', 0, G_OPTION_ARG_NONE, &writable, N_("List writable attributes"), NULL },
Packit ae235b
  { "filesystem", 'f', 0, G_OPTION_ARG_NONE, &filesystem, N_("Get file system info"), NULL },
Packit ae235b
  { "attributes", 'a', 0, G_OPTION_ARG_STRING, &attributes, N_("The attributes to get"), N_("ATTRIBUTES") },
Packit ae235b
  { "nofollow-symlinks", 'n', 0, G_OPTION_ARG_NONE, &nofollow_symlinks, N_("Don’t follow symbolic links"), NULL },
Packit ae235b
  { NULL }
Packit ae235b
};
Packit ae235b
Packit ae235b
static char *
Packit ae235b
escape_string (const char *in)
Packit ae235b
{
Packit ae235b
  GString *str;
Packit ae235b
  static char *hex_digits = "0123456789abcdef";
Packit ae235b
  unsigned char c;
Packit ae235b
Packit ae235b
Packit ae235b
  str = g_string_new ("");
Packit ae235b
Packit ae235b
  while ((c = *in++) != 0)
Packit ae235b
    {
Packit ae235b
      if (c >= 32 && c <= 126 && c != '\\')
Packit ae235b
        g_string_append_c (str, c);
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_string_append (str, "\\x");
Packit ae235b
          g_string_append_c (str, hex_digits[(c >> 4) & 0xf]);
Packit ae235b
          g_string_append_c (str, hex_digits[c & 0xf]);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_string_free (str, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
show_attributes (GFileInfo *info)
Packit ae235b
{
Packit ae235b
  char **attributes;
Packit ae235b
  char *s;
Packit ae235b
  int i;
Packit ae235b
Packit ae235b
  attributes = g_file_info_list_attributes (info, NULL);
Packit ae235b
Packit ae235b
  g_print (_("attributes:\n"));
Packit ae235b
  for (i = 0; attributes[i] != NULL; i++)
Packit ae235b
    {
Packit ae235b
      /* list the icons in order rather than displaying "GThemedIcon:0x8df7200" */
Packit ae235b
      if (strcmp (attributes[i], "standard::icon") == 0 ||
Packit ae235b
          strcmp (attributes[i], "standard::symbolic-icon") == 0)
Packit ae235b
        {
Packit ae235b
          GIcon *icon;
Packit ae235b
          int j;
Packit ae235b
          const char * const *names = NULL;
Packit ae235b
Packit ae235b
          if (strcmp (attributes[i], "standard::symbolic-icon") == 0)
Packit ae235b
            icon = g_file_info_get_symbolic_icon (info);
Packit ae235b
          else
Packit ae235b
            icon = g_file_info_get_icon (info);
Packit ae235b
Packit ae235b
          /* only look up names if GThemedIcon */
Packit ae235b
          if (G_IS_THEMED_ICON(icon))
Packit ae235b
            {
Packit ae235b
              names = g_themed_icon_get_names (G_THEMED_ICON (icon));
Packit ae235b
              g_print ("  %s: ", attributes[i]);
Packit ae235b
              for (j = 0; names[j] != NULL; j++)
Packit ae235b
                g_print ("%s%s", names[j], (names[j+1] == NULL)?"":", ");
Packit ae235b
              g_print ("\n");
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              s = g_file_info_get_attribute_as_string (info, attributes[i]);
Packit ae235b
              g_print ("  %s: %s\n", attributes[i], s);
Packit ae235b
              g_free (s);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          s = g_file_info_get_attribute_as_string (info, attributes[i]);
Packit ae235b
          g_print ("  %s: %s\n", attributes[i], s);
Packit ae235b
          g_free (s);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  g_strfreev (attributes);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
show_info (GFile *file, GFileInfo *info)
Packit ae235b
{
Packit ae235b
  const char *name, *type;
Packit ae235b
  char *escaped, *uri;
Packit ae235b
  goffset size;
Packit ae235b
Packit ae235b
  name = g_file_info_get_display_name (info);
Packit ae235b
  if (name)
Packit ae235b
    /* Translators: This is a noun and represents and attribute of a file */
Packit ae235b
    g_print (_("display name: %s\n"), name);
Packit ae235b
Packit ae235b
  name = g_file_info_get_edit_name (info);
Packit ae235b
  if (name)
Packit ae235b
    /* Translators: This is a noun and represents and attribute of a file */
Packit ae235b
    g_print (_("edit name: %s\n"), name);
Packit ae235b
Packit ae235b
  name = g_file_info_get_name (info);
Packit ae235b
  if (name)
Packit ae235b
    {
Packit ae235b
      escaped = escape_string (name);
Packit ae235b
      g_print (_("name: %s\n"), escaped);
Packit ae235b
      g_free (escaped);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE))
Packit ae235b
    {
Packit ae235b
      type = file_type_to_string (g_file_info_get_file_type (info));
Packit ae235b
      g_print (_("type: %s\n"), type);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_SIZE))
Packit ae235b
    {
Packit ae235b
      size = g_file_info_get_size (info);
Packit ae235b
      g_print (_("size: "));
Packit ae235b
      g_print (" %"G_GUINT64_FORMAT"\n", (guint64)size);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_file_info_get_is_hidden (info))
Packit ae235b
    g_print (_("hidden\n"));
Packit ae235b
Packit ae235b
  uri = g_file_get_uri (file);
Packit ae235b
  g_print (_("uri: %s\n"), uri);
Packit ae235b
  g_free (uri);
Packit ae235b
Packit ae235b
  show_attributes (info);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
query_info (GFile *file)
Packit ae235b
{
Packit ae235b
  GFileQueryInfoFlags flags;
Packit ae235b
  GFileInfo *info;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  if (file == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (attributes == NULL)
Packit ae235b
    attributes = "*";
Packit ae235b
Packit ae235b
  flags = 0;
Packit ae235b
  if (nofollow_symlinks)
Packit ae235b
    flags |= G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  if (filesystem)
Packit ae235b
    info = g_file_query_filesystem_info (file, attributes, NULL, &error);
Packit ae235b
  else
Packit ae235b
    info = g_file_query_info (file, attributes, flags, NULL, &error);
Packit ae235b
Packit ae235b
  if (info == 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
  if (filesystem)
Packit ae235b
    show_attributes (info);
Packit ae235b
  else
Packit ae235b
    show_info (file, info);
Packit ae235b
Packit ae235b
  g_object_unref (info);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
get_writable_info (GFile *file)
Packit ae235b
{
Packit ae235b
  GFileAttributeInfoList *list;
Packit ae235b
  GError *error;
Packit ae235b
  int i;
Packit ae235b
  char *flags;
Packit ae235b
Packit ae235b
  if (file == NULL)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
Packit ae235b
  list = g_file_query_settable_attributes (file, NULL, &error);
Packit ae235b
  if (list == 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
  if (list->n_infos > 0)
Packit ae235b
    {
Packit ae235b
      g_print (_("Settable attributes:\n"));
Packit ae235b
      for (i = 0; i < list->n_infos; i++)
Packit ae235b
        {
Packit ae235b
          flags = attribute_flags_to_string (list->infos[i].flags);
Packit ae235b
          g_print (" %s (%s%s%s)\n",
Packit ae235b
                   list->infos[i].name,
Packit ae235b
                   attribute_type_to_string (list->infos[i].type),
Packit ae235b
                   (*flags != 0)?", ":"", flags);
Packit ae235b
          g_free (flags);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_file_attribute_info_list_unref (list);
Packit ae235b
Packit ae235b
  list = g_file_query_writable_namespaces (file, NULL, &error);
Packit ae235b
  if (list == 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
  if (list->n_infos > 0)
Packit ae235b
    {
Packit ae235b
      g_print (_("Writable attribute namespaces:\n"));
Packit ae235b
      for (i = 0; i < list->n_infos; i++)
Packit ae235b
        {
Packit ae235b
          flags = attribute_flags_to_string (list->infos[i].flags);
Packit ae235b
          g_print (" %s (%s%s%s)\n",
Packit ae235b
                   list->infos[i].name,
Packit ae235b
                   attribute_type_to_string (list->infos[i].type),
Packit ae235b
                   (*flags != 0)?", ":"", flags);
Packit ae235b
          g_free (flags);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_file_attribute_info_list_unref (list);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
handle_info (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 info");
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
      _("Show information about locations."));
Packit ae235b
  g_option_context_set_description (context,
Packit ae235b
      _("gio info 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, or just by\n"
Packit ae235b
        "namespace, e.g. unix, or by “*”, which matches all attributes"));
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
  if (argc < 2)
Packit ae235b
    {
Packit ae235b
      show_help (context, _("No locations given"));
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
  res = TRUE;
Packit ae235b
  for (i = 1; i < argc; i++)
Packit ae235b
    {
Packit ae235b
      file = g_file_new_for_commandline_arg (argv[i]);
Packit ae235b
      if (writable)
Packit ae235b
        res &= get_writable_info (file);
Packit ae235b
      else
Packit ae235b
        res &= query_info (file);
Packit ae235b
      g_object_unref (file);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return res ? 0 : 2;
Packit ae235b
}