2 * Copyright 2015 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the licence, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 * Author: Matthias Clasen <mclasen@redhat.com>
29 static char *attr_type
= "string";
30 static gboolean nofollow_symlinks
= FALSE
;
32 static const GOptionEntry entries
[] = {
33 { "type", 't', 0, G_OPTION_ARG_STRING
, &attr_type
, N_("Type of the attribute"), N_("TYPE") },
34 { "nofollow-symlinks", 'n', 0, G_OPTION_ARG_NONE
, &nofollow_symlinks
, N_("Don’t follow symbolic links"), NULL
},
39 hex_unescape (const char *str
)
42 char *unescaped_str
, *p
;
47 unescaped_str
= g_malloc (len
+ 1);
50 for (i
= 0; i
< len
; i
++)
57 (g_ascii_xdigit_value (str
[i
+2]) << 4) |
58 g_ascii_xdigit_value (str
[i
+3]);
71 handle_set (int argc
, char *argv
[], gboolean do_help
)
73 GOptionContext
*context
;
76 const char *attribute
;
77 GFileAttributeType type
;
86 g_set_prgname ("gio set");
88 /* Translators: commandline placeholder */
89 param
= g_strdup_printf ("%s %s %s...", _("LOCATION"), _("ATTRIBUTE"), _("VALUE"));
90 context
= g_option_context_new (param
);
92 g_option_context_set_help_enabled (context
, FALSE
);
93 g_option_context_set_summary (context
, _("Set a file attribute of LOCATION."));
94 g_option_context_add_main_entries (context
, entries
, GETTEXT_PACKAGE
);
98 show_help (context
, NULL
);
102 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
104 show_help (context
, error
->message
);
105 g_error_free (error
);
111 show_help (context
, _("Location not specified"));
115 file
= g_file_new_for_commandline_arg (argv
[1]);
119 show_help (context
, _("Attribute not specified"));
125 type
= attribute_type_from_string (attr_type
);
126 if ((argc
< 4) && (type
!= G_FILE_ATTRIBUTE_TYPE_INVALID
))
128 show_help (context
, _("Value not specified"));
132 if ((argc
> 4) && (type
!= G_FILE_ATTRIBUTE_TYPE_STRINGV
))
134 show_help (context
, _("Too many arguments"));
138 g_option_context_free (context
);
142 case G_FILE_ATTRIBUTE_TYPE_STRING
:
145 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING
:
146 value
= hex_unescape (argv
[3]);
148 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN
:
149 b
= g_ascii_strcasecmp (argv
[3], "true") == 0;
152 case G_FILE_ATTRIBUTE_TYPE_UINT32
:
153 uint32
= atol (argv
[3]);
156 case G_FILE_ATTRIBUTE_TYPE_INT32
:
157 int32
= atol (argv
[3]);
160 case G_FILE_ATTRIBUTE_TYPE_UINT64
:
161 uint64
= g_ascii_strtoull (argv
[3], NULL
, 10);
164 case G_FILE_ATTRIBUTE_TYPE_INT64
:
165 int64
= g_ascii_strtoll (argv
[3], NULL
, 10);
168 case G_FILE_ATTRIBUTE_TYPE_STRINGV
:
171 case G_FILE_ATTRIBUTE_TYPE_INVALID
:
174 case G_FILE_ATTRIBUTE_TYPE_OBJECT
:
176 g_printerr (_("Invalid attribute type %s\n"), attr_type
);
180 if (!g_file_set_attribute (file
,
185 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
:
186 G_FILE_QUERY_INFO_NONE
,
189 g_printerr (_("Error setting attribute: %s\n"), error
->message
);
190 g_error_free (error
);