Blame gio/gio-tool-set.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
#include <stdlib.h>
Packit ae235b
Packit ae235b
#include "gio-tool.h"
Packit ae235b
Packit ae235b
Packit ae235b
static char *attr_type = "string";
Packit ae235b
static gboolean nofollow_symlinks = FALSE;
Packit ae235b
Packit ae235b
static const GOptionEntry entries[] = {
Packit ae235b
  { "type", 't', 0, G_OPTION_ARG_STRING, &attr_type, N_("Type of the attribute"), N_("TYPE") },
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
hex_unescape (const char *str)
Packit ae235b
{
Packit ae235b
  int i;
Packit ae235b
  char *unescaped_str, *p;
Packit ae235b
  unsigned char c;
Packit ae235b
  int len;
Packit ae235b
Packit ae235b
  len = strlen (str);
Packit ae235b
  unescaped_str = g_malloc (len + 1);
Packit ae235b
Packit ae235b
  p = unescaped_str;
Packit ae235b
  for (i = 0; i < len; i++)
Packit ae235b
    {
Packit ae235b
      if (str[i] == '\\' &&
Packit ae235b
	  str[i+1] == 'x' &&
Packit ae235b
	  len - i >= 4)
Packit ae235b
	{
Packit ae235b
	  c =
Packit ae235b
	    (g_ascii_xdigit_value (str[i+2]) << 4) |
Packit ae235b
	    g_ascii_xdigit_value (str[i+3]);
Packit ae235b
	  *p++ = c;
Packit ae235b
	  i += 3;
Packit ae235b
	}
Packit ae235b
      else
Packit ae235b
	*p++ = str[i];
Packit ae235b
    }
Packit ae235b
  *p++ = 0;
Packit ae235b
Packit ae235b
  return unescaped_str;
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
handle_set (int argc, char *argv[], gboolean do_help)
Packit ae235b
{
Packit ae235b
  GOptionContext *context;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GFile *file;
Packit ae235b
  const char *attribute;
Packit ae235b
  GFileAttributeType type;
Packit ae235b
  gpointer value;
Packit ae235b
  gboolean b;
Packit ae235b
  guint32 uint32;
Packit ae235b
  gint32 int32;
Packit ae235b
  guint64 uint64;
Packit ae235b
  gint64 int64;
Packit ae235b
  gchar *param;
Packit ae235b
Packit ae235b
  g_set_prgname ("gio set");
Packit ae235b
Packit ae235b
  /* Translators: commandline placeholder */
Packit ae235b
  param = g_strdup_printf ("%s %s %s...", _("LOCATION"), _("ATTRIBUTE"), _("VALUE"));
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, _("Set a file attribute of LOCATION."));
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, _("Location not specified"));
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (argc < 3)
Packit ae235b
    {
Packit ae235b
      show_help (context, _("Attribute not specified"));
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  attribute = argv[2];
Packit ae235b
Packit ae235b
  type = attribute_type_from_string (attr_type);
Packit ae235b
  if ((argc < 4) && (type != G_FILE_ATTRIBUTE_TYPE_INVALID))
Packit ae235b
    {
Packit ae235b
      show_help (context, _("Value not specified"));
Packit ae235b
      g_option_context_free (context);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if ((argc > 4) && (type != G_FILE_ATTRIBUTE_TYPE_STRINGV))
Packit ae235b
    {
Packit ae235b
      show_help (context, _("Too many arguments"));
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
  switch (type)
Packit ae235b
    {
Packit ae235b
    case G_FILE_ATTRIBUTE_TYPE_STRING:
Packit ae235b
      value = argv[3];
Packit ae235b
      break;
Packit ae235b
    case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
Packit ae235b
      value = hex_unescape (argv[3]);
Packit ae235b
      break;
Packit ae235b
    case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
Packit ae235b
      b = g_ascii_strcasecmp (argv[3], "true") == 0;
Packit ae235b
      value = &b;
Packit ae235b
      break;
Packit ae235b
    case G_FILE_ATTRIBUTE_TYPE_UINT32:
Packit ae235b
      uint32 = atol (argv[3]);
Packit ae235b
      value = &uint32;
Packit ae235b
      break;
Packit ae235b
    case G_FILE_ATTRIBUTE_TYPE_INT32:
Packit ae235b
      int32 = atol (argv[3]);
Packit ae235b
      value = &int3;;
Packit ae235b
      break;
Packit ae235b
    case G_FILE_ATTRIBUTE_TYPE_UINT64:
Packit ae235b
      uint64 = g_ascii_strtoull (argv[3], NULL, 10);
Packit ae235b
      value = &uint64;
Packit ae235b
      break;
Packit ae235b
    case G_FILE_ATTRIBUTE_TYPE_INT64:
Packit ae235b
      int64 = g_ascii_strtoll (argv[3], NULL, 10);
Packit ae235b
      value = &int6;;
Packit ae235b
      break;
Packit ae235b
    case G_FILE_ATTRIBUTE_TYPE_STRINGV:
Packit ae235b
      value = &argv[3];
Packit ae235b
      break;
Packit ae235b
    case G_FILE_ATTRIBUTE_TYPE_INVALID:
Packit ae235b
      value = NULL;
Packit ae235b
      break;
Packit ae235b
    case G_FILE_ATTRIBUTE_TYPE_OBJECT:
Packit ae235b
    default:
Packit ae235b
      print_error (_("Invalid attribute type “%s”"), attr_type);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  file = g_file_new_for_commandline_arg (argv[1]);
Packit ae235b
Packit ae235b
  if (!g_file_set_attribute (file,
Packit ae235b
			     attribute,
Packit ae235b
			     type,
Packit ae235b
			     value,
Packit ae235b
                             nofollow_symlinks ?
Packit ae235b
                               G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS :
Packit ae235b
                               G_FILE_QUERY_INFO_NONE,
Packit ae235b
                             NULL, &error))
Packit ae235b
    {
Packit ae235b
      print_error ("%s", error->message);
Packit ae235b
      g_error_free (error);
Packit ae235b
      g_object_unref (file);
Packit ae235b
      return 1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_object_unref (file);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}