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>
32 #define STDIN_FILENO 0
41 static char *global_etag = NULL;
42 static gboolean backup = FALSE;
43 static gboolean create = FALSE;
44 static gboolean append = FALSE;
45 static gboolean priv = FALSE;
46 static gboolean replace_dest = FALSE;
47 static gboolean print_etag = FALSE;
49 static const GOptionEntry entries[] =
51 { "backup", 'b', 0, G_OPTION_ARG_NONE, &backup, N_("Backup existing destination files"), NULL },
52 { "create", 'c', 0, G_OPTION_ARG_NONE, &create, N_("Only create if not existing"), NULL },
53 { "append", 'a', 0, G_OPTION_ARG_NONE, &append, N_("Append to end of file"), NULL },
54 { "private", 'p', 0, G_OPTION_ARG_NONE, &priv, N_("When creating, restrict access to the current user"), NULL },
55 { "unlink", 'u', 0, G_OPTION_ARG_NONE, &replace_dest, N_("When replacing, replace as if the destination did not exist"), NULL },
56 /* Translators: The "etag" is a token allowing to verify whether a file has been modified */
57 { "print-etag", 'v', 0, G_OPTION_ARG_NONE, &print_etag, N_("Print new etag at end"), NULL },
58 /* Translators: The "etag" is a token allowing to verify whether a file has been modified */
59 { "etag", 'e', 0, G_OPTION_ARG_STRING, &global_etag, N_("The etag of the file being overwritten"), N_("ETAG") },
63 /* 256k minus malloc overhead */
64 #define STREAM_BUFFER_SIZE (1024*256 - 2*sizeof(gpointer))
70 GFileCreateFlags flags;
79 flags = priv ? G_FILE_CREATE_PRIVATE : G_FILE_CREATE_NONE;
80 flags |= replace_dest ? G_FILE_CREATE_REPLACE_DESTINATION : 0;
83 out = (GOutputStream *)g_file_create (file, flags, NULL, &error);
85 out = (GOutputStream *)g_file_append_to (file, flags, NULL, &error);
87 out = (GOutputStream *)g_file_replace (file, global_etag, backup, flags, NULL, &error);
90 print_file_error (file, error->message);
95 buffer = g_malloc (STREAM_BUFFER_SIZE);
100 res = read (STDIN_FILENO, buffer, STREAM_BUFFER_SIZE);
103 g_output_stream_write_all (out, buffer, res, NULL, NULL, &error);
107 print_file_error (file, error->message);
108 g_clear_error (&error);
115 print_error ("%s", _("Error reading from standard input"));
124 close_res = g_output_stream_close (out, NULL, &error);
128 print_file_error (file, error->message);
129 g_error_free (error);
132 if (close_res && print_etag)
135 etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (out));
138 g_print ("Etag: %s\n", etag);
140 /* Translators: The "etag" is a token allowing to verify whether a file has been modified */
141 g_print (_("Etag not available\n"));
145 g_object_unref (out);
152 handle_save (int argc, char *argv[], gboolean do_help)
154 GOptionContext *context;
155 GError *error = NULL;
159 g_set_prgname ("gio save");
161 /* Translators: commandline placeholder */
162 context = g_option_context_new (_("DESTINATION"));
163 g_option_context_set_help_enabled (context, FALSE);
164 g_option_context_set_summary (context,
165 _("Read from standard input and save to DEST."));
166 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
170 show_help (context, NULL);
171 g_option_context_free (context);
175 if (!g_option_context_parse (context, &argc, &argv, &error))
177 show_help (context, error->message);
178 g_error_free (error);
179 g_option_context_free (context);
185 show_help (context, _("No destination given"));
186 g_option_context_free (context);
192 show_help (context, _("Too many arguments"));
193 g_option_context_free (context);
197 g_option_context_free (context);
199 file = g_file_new_for_commandline_arg (argv[1]);
201 g_object_unref (file);