Fix getauxval error at qemu
[platform/upstream/glib.git] / gio / gio-tool-save.c
1 /*
2  * Copyright 2015 Red Hat, Inc.
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  * Author: Matthias Clasen <mclasen@redhat.com>
20  */
21
22 #include "config.h"
23
24 #include <gio/gio.h>
25 #include <gi18n.h>
26
27 #ifdef G_OS_WIN32
28 #include <io.h>
29 #endif
30
31 #ifndef STDIN_FILENO
32 #define STDIN_FILENO 0
33 #endif
34
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38
39 #include "gio-tool.h"
40
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;
48
49 static const GOptionEntry entries[] =
50 {
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") },
60   G_OPTION_ENTRY_NULL
61 };
62
63 /* 256k minus malloc overhead */
64 #define STREAM_BUFFER_SIZE (1024*256 - 2*sizeof(gpointer))
65
66 static gboolean
67 save (GFile *file)
68 {
69   GOutputStream *out;
70   GFileCreateFlags flags;
71   char *buffer;
72   gssize res;
73   gboolean close_res;
74   GError *error;
75   gboolean save_res;
76
77   error = NULL;
78
79   flags = priv ? G_FILE_CREATE_PRIVATE : G_FILE_CREATE_NONE;
80   flags |= replace_dest ? G_FILE_CREATE_REPLACE_DESTINATION : 0;
81
82   if (create)
83     out = (GOutputStream *)g_file_create (file, flags, NULL, &error);
84   else if (append)
85     out = (GOutputStream *)g_file_append_to (file, flags, NULL, &error);
86   else
87     out = (GOutputStream *)g_file_replace (file, global_etag, backup, flags, NULL, &error);
88   if (out == NULL)
89     {
90       print_file_error (file, error->message);
91       g_error_free (error);
92       return FALSE;
93     }
94
95   buffer = g_malloc (STREAM_BUFFER_SIZE);
96   save_res = TRUE;
97
98   while (1)
99     {
100       res = read (STDIN_FILENO, buffer, STREAM_BUFFER_SIZE);
101       if (res > 0)
102         {
103           g_output_stream_write_all (out, buffer, res, NULL, NULL, &error);
104           if (error != NULL)
105             {
106               save_res = FALSE;
107               print_file_error (file, error->message);
108               g_clear_error (&error);
109               goto out;
110             }
111         }
112       else if (res < 0)
113         {
114           save_res = FALSE;
115           print_error ("%s", _("Error reading from standard input"));
116           break;
117         }
118       else if (res == 0)
119         break;
120     }
121
122  out:
123
124   close_res = g_output_stream_close (out, NULL, &error);
125   if (!close_res)
126     {
127       save_res = FALSE;
128       print_file_error (file, error->message);
129       g_error_free (error);
130     }
131
132   if (close_res && print_etag)
133     {
134       char *etag;
135       etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (out));
136
137       if (etag)
138         g_print ("Etag: %s\n", etag);
139       else
140         /* Translators: The "etag" is a token allowing to verify whether a file has been modified */
141         g_print (_("Etag not available\n"));
142       g_free (etag);
143     }
144
145   g_object_unref (out);
146   g_free (buffer);
147
148   return save_res;
149 }
150
151 int
152 handle_save (int argc, char *argv[], gboolean do_help)
153 {
154   GOptionContext *context;
155   GError *error = NULL;
156   GFile *file;
157   gboolean res;
158
159   g_set_prgname ("gio save");
160
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);
167
168   if (do_help)
169     {
170       show_help (context, NULL);
171       g_option_context_free (context);
172       return 0;
173     }
174
175   if (!g_option_context_parse (context, &argc, &argv, &error))
176     {
177       show_help (context, error->message);
178       g_error_free (error);
179       g_option_context_free (context);
180       return 1;
181     }
182
183   if (argc < 2)
184     {
185       show_help (context, _("No destination given"));
186       g_option_context_free (context);
187       return 1;
188     }
189
190   if (argc > 2)
191     {
192       show_help (context, _("Too many arguments"));
193       g_option_context_free (context);
194       return 1;
195     }
196
197   g_option_context_free (context);
198
199   file = g_file_new_for_commandline_arg (argv[1]);
200   res = save (file);
201   g_object_unref (file);
202
203   return res ? 0 : 2;
204 }