Update Friulian translation
[glib.git] / gio / gio-tool-save.c
blobddf085f09d7dcbbd4c340da249dc341567a5bfe8
1 /*
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>
20 #include "config.h"
22 #include <gio/gio.h>
23 #include <gi18n.h>
25 #ifdef G_OS_WIN32
26 #include <io.h>
27 #endif
29 #ifndef STDIN_FILENO
30 #define STDIN_FILENO 0
31 #endif
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
37 #include "gio-tool.h"
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") },
58 { NULL }
61 /* 256k minus malloc overhead */
62 #define STREAM_BUFFER_SIZE (1024*256 - 2*sizeof(gpointer))
64 static gboolean
65 save (GFile *file)
67 GOutputStream *out;
68 GFileCreateFlags flags;
69 char *buffer;
70 gssize res;
71 gboolean close_res;
72 GError *error;
73 gboolean save_res;
75 error = NULL;
77 flags = priv ? G_FILE_CREATE_PRIVATE : G_FILE_CREATE_NONE;
78 flags |= replace_dest ? G_FILE_CREATE_REPLACE_DESTINATION : 0;
80 if (create)
81 out = (GOutputStream *)g_file_create (file, flags, NULL, &error);
82 else if (append)
83 out = (GOutputStream *)g_file_append_to (file, flags, NULL, &error);
84 else
85 out = (GOutputStream *)g_file_replace (file, etag, backup, flags, NULL, &error);
86 if (out == NULL)
88 print_file_error (file, error->message);
89 g_error_free (error);
90 return FALSE;
93 buffer = g_malloc (STREAM_BUFFER_SIZE);
94 save_res = TRUE;
96 while (1)
98 res = read (STDIN_FILENO, buffer, STREAM_BUFFER_SIZE);
99 if (res > 0)
101 g_output_stream_write_all (out, buffer, res, NULL, NULL, &error);
102 if (error != NULL)
104 save_res = FALSE;
105 print_file_error (file, error->message);
106 g_clear_error (&error);
107 goto out;
110 else if (res < 0)
112 save_res = FALSE;
113 print_error ("%s", _("Error reading from standard input"));
114 break;
116 else if (res == 0)
117 break;
120 out:
122 close_res = g_output_stream_close (out, NULL, &error);
123 if (!close_res)
125 save_res = FALSE;
126 print_file_error (file, error->message);
127 g_error_free (error);
130 if (close_res && print_etag)
132 char *etag;
133 etag = g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (out));
135 if (etag)
136 g_print ("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 (etag);
143 g_object_unref (out);
144 g_free (buffer);
146 return save_res;
150 handle_save (int argc, char *argv[], gboolean do_help)
152 GOptionContext *context;
153 GError *error = NULL;
154 GFile *file;
155 gboolean res;
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);
166 if (do_help)
168 show_help (context, NULL);
169 g_option_context_free (context);
170 return 0;
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);
178 return 1;
181 if (argc < 2)
183 show_help (context, _("No destination given"));
184 g_option_context_free (context);
185 return 1;
188 if (argc > 2)
190 show_help (context, _("Too many arguments"));
191 g_option_context_free (context);
192 return 1;
195 g_option_context_free (context);
197 file = g_file_new_for_commandline_arg (argv[1]);
198 res = save (file);
199 g_object_unref (file);
201 return res ? 0 : 2;