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

source code of gtk/subprojects/glib/gio/gio-tool-save.c