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>
30 #define STDIN_FILENO 0
39 static char *etag = NULL;
40 static gboolean backup = FALSE;
41 static gboolean create = FALSE;
42 static gboolean append = FALSE;
43 static gboolean priv = FALSE;
44 static gboolean replace_dest = FALSE;
45 static gboolean print_etag = FALSE;
47 static const GOptionEntry entries[] =
49 { "backup", 'b', 0, G_OPTION_ARG_NONE, &backup, N_("Backup existing destination files"), NULL },
50 { "create", 'c', 0, G_OPTION_ARG_NONE, &create, N_("Only create if not existing"), NULL },
51 { "append", 'a', 0, G_OPTION_ARG_NONE, &append, N_("Append to end of file"), NULL },
52 { "private", 'p', 0, G_OPTION_ARG_NONE, &priv, N_("When creating, restrict access to the current user"), NULL },
53 { "unlink", 'u', 0, G_OPTION_ARG_NONE, &replace_dest, N_("When replacing, replace as if the destination did not exist"), NULL },
54 /* Translators: The "etag" is a token allowing to verify whether a file has been modified */
55 { "print-etag", 'v', 0, G_OPTION_ARG_NONE, &print_etag, N_("Print new etag at end"), NULL },
56 /* Translators: The "etag" is a token allowing to verify whether a file has been modified */
57 { "etag", 'e', 0, G_OPTION_ARG_STRING, &etag, N_("The etag of the file being overwritten"), N_("ETAG") },
61 /* 256k minus malloc overhead */
62 #define STREAM_BUFFER_SIZE (1024*256 - 2*sizeof(gpointer))
68 GFileCreateFlags flags;
77 flags = priv ? G_FILE_CREATE_PRIVATE : G_FILE_CREATE_NONE;
78 flags |= replace_dest ? G_FILE_CREATE_REPLACE_DESTINATION : 0;
81 out = (GOutputStream *)g_file_create (file, flags, NULL, &error);
83 out = (GOutputStream *)g_file_append_to (file, flags, NULL, &error);
85 out = (GOutputStream *)g_file_replace (file, etag, backup, flags, NULL, &error);
88 print_file_error (file, error->message);
93 buffer = g_malloc (STREAM_BUFFER_SIZE);
98 res = read (STDIN_FILENO, buffer, STREAM_BUFFER_SIZE);
101 g_output_stream_write_all (out, buffer, res, NULL, NULL, &error);
105 print_file_error (file, error->message);
106 g_clear_error (&error);
113 print_error ("%s", _("Error reading from standard input"));
122 close_res = g_output_stream_close (out, NULL, &error);
126 print_file_error (file, error->message);
127 g_error_free (error);
130 if (close_res && print_etag)
133 etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (out));
136 g_print ("Etag: %s\n", etag);
138 /* Translators: The "etag" is a token allowing to verify whether a file has been modified */
139 g_print (_("Etag not available\n"));
143 g_object_unref (out);
150 handle_save (int argc, char *argv[], gboolean do_help)
152 GOptionContext *context;
153 GError *error = NULL;
157 g_set_prgname ("gio save");
159 /* Translators: commandline placeholder */
160 context = g_option_context_new (_("DESTINATION"));
161 g_option_context_set_help_enabled (context, FALSE);
162 g_option_context_set_summary (context,
163 _("Read from standard input and save to DEST."));
164 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
168 show_help (context, NULL);
169 g_option_context_free (context);
173 if (!g_option_context_parse (context, &argc, &argv, &error))
175 show_help (context, error->message);
176 g_error_free (error);
177 g_option_context_free (context);
183 show_help (context, _("No destination given"));
184 g_option_context_free (context);
190 show_help (context, _("Too many arguments"));
191 g_option_context_free (context);
195 g_option_context_free (context);
197 file = g_file_new_for_commandline_arg (argv[1]);
199 g_object_unref (file);