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.1 of the License, 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
);
99 g_option_context_free (context
);
103 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
105 show_help (context
, error
->message
);
106 g_error_free (error
);
107 g_option_context_free (context
);
113 show_help (context
, _("Location not specified"));
114 g_option_context_free (context
);
120 show_help (context
, _("Attribute not specified"));
121 g_option_context_free (context
);
127 type
= attribute_type_from_string (attr_type
);
128 if ((argc
< 4) && (type
!= G_FILE_ATTRIBUTE_TYPE_INVALID
))
130 show_help (context
, _("Value not specified"));
131 g_option_context_free (context
);
135 if ((argc
> 4) && (type
!= G_FILE_ATTRIBUTE_TYPE_STRINGV
))
137 show_help (context
, _("Too many arguments"));
138 g_option_context_free (context
);
142 g_option_context_free (context
);
146 case G_FILE_ATTRIBUTE_TYPE_STRING
:
149 case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING
:
150 value
= hex_unescape (argv
[3]);
152 case G_FILE_ATTRIBUTE_TYPE_BOOLEAN
:
153 b
= g_ascii_strcasecmp (argv
[3], "true") == 0;
156 case G_FILE_ATTRIBUTE_TYPE_UINT32
:
157 uint32
= atol (argv
[3]);
160 case G_FILE_ATTRIBUTE_TYPE_INT32
:
161 int32
= atol (argv
[3]);
164 case G_FILE_ATTRIBUTE_TYPE_UINT64
:
165 uint64
= g_ascii_strtoull (argv
[3], NULL
, 10);
168 case G_FILE_ATTRIBUTE_TYPE_INT64
:
169 int64
= g_ascii_strtoll (argv
[3], NULL
, 10);
172 case G_FILE_ATTRIBUTE_TYPE_STRINGV
:
175 case G_FILE_ATTRIBUTE_TYPE_INVALID
:
178 case G_FILE_ATTRIBUTE_TYPE_OBJECT
:
180 print_error (_("Invalid attribute type “%s”"), attr_type
);
184 file
= g_file_new_for_commandline_arg (argv
[1]);
186 if (!g_file_set_attribute (file
,
191 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
:
192 G_FILE_QUERY_INFO_NONE
,
195 print_error ("%s", error
->message
);
196 g_error_free (error
);
197 g_object_unref (file
);
201 g_object_unref (file
);