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 of the licence, 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") },
65 GFileCreateFlags flags
;
75 flags
= priv
? G_FILE_CREATE_PRIVATE
: G_FILE_CREATE_NONE
;
76 flags
|= replace_dest
? G_FILE_CREATE_REPLACE_DESTINATION
: 0;
79 out
= (GOutputStream
*)g_file_create (file
, flags
, NULL
, &error
);
81 out
= (GOutputStream
*)g_file_append_to (file
, flags
, NULL
, &error
);
83 out
= (GOutputStream
*)g_file_replace (file
, etag
, backup
, flags
, NULL
, &error
);
86 print_file_error (file
, error
->message
);
95 res
= read (STDIN_FILENO
, buffer
, 1024);
104 written
= g_output_stream_write (out
, p
, res
, NULL
, &error
);
108 g_printerr ("gio: Error writing to stream: %s\n", error
->message
);
109 g_error_free (error
);
119 g_printerr ("gio: Error reading from standard input\n");
128 close_res
= g_output_stream_close (out
, NULL
, &error
);
132 g_printerr ("gio: Error closing: %s\n", error
->message
);
133 g_error_free (error
);
136 if (close_res
&& print_etag
)
139 etag
= g_file_output_stream_get_etag (G_FILE_OUTPUT_STREAM (out
));
142 g_print ("Etag: %s\n", etag
);
144 /* Translators: The "etag" is a token allowing to verify whether a file has been modified */
145 g_print (_("Etag not available\n"));
149 g_object_unref (out
);
155 handle_save (int argc
, char *argv
[], gboolean do_help
)
157 GOptionContext
*context
;
158 GError
*error
= NULL
;
162 g_set_prgname ("gio save");
164 /* Translators: commandline placeholder */
165 context
= g_option_context_new (_("DESTINATION"));
166 g_option_context_set_help_enabled (context
, FALSE
);
167 g_option_context_set_summary (context
,
168 _("Read from standard input and save to DEST."));
169 g_option_context_add_main_entries (context
, entries
, GETTEXT_PACKAGE
);
173 show_help (context
, NULL
);
177 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
179 show_help (context
, error
->message
);
180 g_error_free (error
);
186 show_help (context
, _("No destination given"));
192 show_help (context
, _("Too many arguments"));
196 g_option_context_free (context
);
198 file
= g_file_new_for_commandline_arg (argv
[1]);
200 g_object_unref (file
);