2 * Copyright 2015 Red Hat, Inc.
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Author: Matthias Clasen <mclasen@redhat.com>
31 static char *attr_type = "string";
32 static gboolean nofollow_symlinks = FALSE;
33 static gboolean delete = FALSE;
35 static const GOptionEntry entries[] = {
36 { "type", 't', 0, G_OPTION_ARG_STRING, &attr_type, N_("Type of the attribute"), N_("TYPE") },
37 { "nofollow-symlinks", 'n', 0, G_OPTION_ARG_NONE, &nofollow_symlinks, N_("Don’t follow symbolic links"), NULL },
38 { "delete", 'd', 0, G_OPTION_ARG_NONE, &delete, N_("Unset given attribute"), NULL },
43 hex_unescape (const char *str)
46 char *unescaped_str, *p;
51 unescaped_str = g_malloc (len + 1);
54 for (i = 0; i < len; i++)
61 (g_ascii_xdigit_value (str[i+2]) << 4) |
62 g_ascii_xdigit_value (str[i+3]);
75 handle_set (int argc, char *argv[], gboolean do_help)
77 GOptionContext *context;
80 const char *attribute;
81 GFileAttributeType type;
83 gpointer value_allocated = NULL;
92 g_set_prgname ("gio set");
94 /* Translators: commandline placeholder */
95 param = g_strdup_printf ("%s %s %s…", _("LOCATION"), _("ATTRIBUTE"), _("VALUE"));
96 context = g_option_context_new (param);
98 g_option_context_set_help_enabled (context, FALSE);
99 g_option_context_set_summary (context, _("Set a file attribute of LOCATION."));
100 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
104 show_help (context, NULL);
105 g_option_context_free (context);
109 if (!g_option_context_parse (context, &argc, &argv, &error))
111 show_help (context, error->message);
112 g_error_free (error);
113 g_option_context_free (context);
119 show_help (context, _("Location not specified"));
120 g_option_context_free (context);
126 show_help (context, _("Attribute not specified"));
127 g_option_context_free (context);
134 type = G_FILE_ATTRIBUTE_TYPE_INVALID;
138 type = attribute_type_from_string (attr_type);
141 if ((argc < 4) && (type != G_FILE_ATTRIBUTE_TYPE_INVALID))
143 show_help (context, _("Value not specified"));
144 g_option_context_free (context);
148 if ((argc > 4) && (type != G_FILE_ATTRIBUTE_TYPE_STRINGV))
150 show_help (context, _("Too many arguments"));
151 g_option_context_free (context);
155 g_option_context_free (context);
159 case G_FILE_ATTRIBUTE_TYPE_STRING:
162 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
163 value = value_allocated = hex_unescape (argv[3]);
165 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
166 b = g_ascii_strcasecmp (argv[3], "true") == 0;
169 case G_FILE_ATTRIBUTE_TYPE_UINT32:
170 uint32 = atol (argv[3]);
173 case G_FILE_ATTRIBUTE_TYPE_INT32:
174 int32 = atol (argv[3]);
177 case G_FILE_ATTRIBUTE_TYPE_UINT64:
178 uint64 = g_ascii_strtoull (argv[3], NULL, 10);
181 case G_FILE_ATTRIBUTE_TYPE_INT64:
182 int64 = g_ascii_strtoll (argv[3], NULL, 10);
185 case G_FILE_ATTRIBUTE_TYPE_STRINGV:
188 case G_FILE_ATTRIBUTE_TYPE_INVALID:
191 case G_FILE_ATTRIBUTE_TYPE_OBJECT:
193 print_error (_("Invalid attribute type “%s”"), attr_type);
197 file = g_file_new_for_commandline_arg (argv[1]);
199 if (!g_file_set_attribute (file,
204 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS :
205 G_FILE_QUERY_INFO_NONE,
208 print_error ("%s", error->message);
209 g_error_free (error);
213 g_clear_pointer (&value_allocated, g_free);
214 g_object_unref (file);