Branch data Line data Source code
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 : : #include <stdio.h>
27 : : #if 0
28 : : #include <locale.h>
29 : : #include <string.h>
30 : : #include <stdlib.h>
31 : : #include <errno.h>
32 : : #endif
33 : :
34 : : #include "gio-tool.h"
35 : :
36 : : static gboolean no_target_directory = FALSE;
37 : : static gboolean progress = FALSE;
38 : : static gboolean interactive = FALSE;
39 : : static gboolean preserve = FALSE;
40 : : static gboolean backup = FALSE;
41 : : static gboolean no_dereference = FALSE;
42 : : static gboolean default_permissions = FALSE;
43 : : static gboolean default_modified_time = FALSE;
44 : :
45 : : static const GOptionEntry entries[] = {
46 : : { "no-target-directory", 'T', 0, G_OPTION_ARG_NONE, &no_target_directory, N_("No target directory"), NULL },
47 : : { "progress", 'p', 0, G_OPTION_ARG_NONE, &progress, N_("Show progress"), NULL },
48 : : { "interactive", 'i', 0, G_OPTION_ARG_NONE, &interactive, N_("Prompt before overwrite"), NULL },
49 : : { "preserve", 'p', 0, G_OPTION_ARG_NONE, &preserve, N_("Preserve all attributes"), NULL },
50 : : { "backup", 'b', 0, G_OPTION_ARG_NONE, &backup, N_("Backup existing destination files"), NULL },
51 : : { "no-dereference", 'P', 0, G_OPTION_ARG_NONE, &no_dereference, N_("Never follow symbolic links"), NULL },
52 : : { "default-permissions", 0, 0, G_OPTION_ARG_NONE, &default_permissions, N_("Use default permissions for the destination"), NULL },
53 : : { "default-modified-time", 0, 0, G_OPTION_ARG_NONE, &default_modified_time, N_("Use default file modification timestamps for the destination"), NULL },
54 : : G_OPTION_ENTRY_NULL
55 : : };
56 : :
57 : : static gint64 start_time;
58 : : static gint64 previous_time;
59 : : static goffset previous_num_bytes;
60 : :
61 : : static void
62 : 0 : show_progress (goffset current_num_bytes,
63 : : goffset total_num_bytes,
64 : : gpointer user_data)
65 : : {
66 : : gint64 tv;
67 : : char *current_size, *total_size, *current_rate, *average_rate;
68 : : goffset bytes_since_last;
69 : : gint64 time_since_last;
70 : :
71 : 0 : tv = g_get_monotonic_time ();
72 : 0 : if (tv - previous_time < (G_USEC_PER_SEC / 5) &&
73 : : current_num_bytes != total_num_bytes)
74 : 0 : return;
75 : :
76 : 0 : current_size = g_format_size (current_num_bytes);
77 : 0 : total_size = g_format_size (total_num_bytes);
78 : :
79 : 0 : average_rate = g_format_size (current_num_bytes * G_USEC_PER_SEC /
80 : 0 : MAX ((tv - start_time), 1));
81 : :
82 : 0 : bytes_since_last = current_num_bytes - previous_num_bytes;
83 : 0 : time_since_last = tv - previous_time;
84 : 0 : current_rate = g_format_size ((bytes_since_last * G_USEC_PER_SEC) /
85 : 0 : MAX (time_since_last, 1));
86 : :
87 : 0 : g_print ("\r\033[K");
88 : :
89 : 0 : if (current_num_bytes == total_num_bytes)
90 : : {
91 : 0 : g_print (_("Copied %s (average: %s/s)"),
92 : : current_size, average_rate);
93 : : }
94 : : else
95 : : {
96 : 0 : g_print (_("Copied %s out of %s (%s/s; average: %s/s)"),
97 : : current_size, total_size, current_rate, average_rate);
98 : : }
99 : :
100 : 0 : previous_time = tv;
101 : 0 : previous_num_bytes = current_num_bytes;
102 : :
103 : 0 : g_free (current_size);
104 : 0 : g_free (total_size);
105 : 0 : g_free (current_rate);
106 : 0 : g_free (average_rate);
107 : : }
108 : :
109 : : int
110 : 0 : handle_copy (int argc, char *argv[], gboolean do_help)
111 : : {
112 : : GOptionContext *context;
113 : 0 : GError *error = NULL;
114 : : char *param;
115 : : GFile *source, *dest, *target;
116 : : gboolean dest_is_dir;
117 : : char *basename;
118 : : char *uri;
119 : : int i;
120 : : GFileCopyFlags flags;
121 : 0 : int retval = 0;
122 : :
123 : 0 : g_set_prgname ("gio copy");
124 : :
125 : : /* Translators: commandline placeholder */
126 : 0 : param = g_strdup_printf ("%s… %s", _("SOURCE"), _("DESTINATION"));
127 : 0 : context = g_option_context_new (param);
128 : 0 : g_free (param);
129 : 0 : g_option_context_set_help_enabled (context, FALSE);
130 : 0 : g_option_context_set_summary (context,
131 : 0 : _("Copy one or more files from SOURCE to DESTINATION."));
132 : 0 : g_option_context_set_description (context,
133 : 0 : _("gio copy is similar to the traditional cp utility, but using GIO\n"
134 : : "locations instead of local files: for example, you can use something\n"
135 : : "like smb://server/resource/file.txt as location."));
136 : 0 : g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
137 : :
138 : 0 : if (do_help)
139 : : {
140 : 0 : show_help (context, NULL);
141 : 0 : g_option_context_free (context);
142 : 0 : return 0;
143 : : }
144 : :
145 : 0 : if (!g_option_context_parse (context, &argc, &argv, &error))
146 : : {
147 : 0 : show_help (context, error->message);
148 : 0 : g_error_free (error);
149 : 0 : g_option_context_free (context);
150 : 0 : return 1;
151 : : }
152 : :
153 : 0 : if (argc < 3)
154 : : {
155 : 0 : show_help (context, NULL);
156 : 0 : g_option_context_free (context);
157 : 0 : return 1;
158 : : }
159 : :
160 : 0 : dest = g_file_new_for_commandline_arg (argv[argc - 1]);
161 : :
162 : 0 : if (no_target_directory && argc > 3)
163 : : {
164 : 0 : show_help (context, NULL);
165 : 0 : g_object_unref (dest);
166 : 0 : g_option_context_free (context);
167 : 0 : return 1;
168 : : }
169 : :
170 : 0 : dest_is_dir = file_is_dir (dest);
171 : 0 : if (!dest_is_dir && argc > 3)
172 : : {
173 : : char *message;
174 : :
175 : 0 : message = g_strdup_printf (_("Destination %s is not a directory"), argv[argc - 1]);
176 : 0 : show_help (context, message);
177 : 0 : g_free (message);
178 : 0 : g_object_unref (dest);
179 : 0 : g_option_context_free (context);
180 : 0 : return 1;
181 : : }
182 : :
183 : 0 : g_option_context_free (context);
184 : :
185 : 0 : for (i = 1; i < argc - 1; i++)
186 : : {
187 : 0 : source = g_file_new_for_commandline_arg (argv[i]);
188 : 0 : if (dest_is_dir && !no_target_directory)
189 : : {
190 : 0 : basename = g_file_get_basename (source);
191 : 0 : target = g_file_get_child (dest, basename);
192 : 0 : g_free (basename);
193 : : }
194 : : else
195 : 0 : target = g_object_ref (dest);
196 : :
197 : 0 : flags = 0;
198 : 0 : if (backup)
199 : 0 : flags |= G_FILE_COPY_BACKUP;
200 : 0 : if (!interactive)
201 : 0 : flags |= G_FILE_COPY_OVERWRITE;
202 : 0 : if (no_dereference)
203 : 0 : flags |= G_FILE_COPY_NOFOLLOW_SYMLINKS;
204 : 0 : if (preserve)
205 : 0 : flags |= G_FILE_COPY_ALL_METADATA;
206 : 0 : if (default_permissions)
207 : 0 : flags |= G_FILE_COPY_TARGET_DEFAULT_PERMS;
208 : 0 : if (default_modified_time)
209 : 0 : flags |= G_FILE_COPY_TARGET_DEFAULT_MODIFIED_TIME;
210 : :
211 : 0 : error = NULL;
212 : 0 : start_time = g_get_monotonic_time ();
213 : 0 : previous_num_bytes = 0;
214 : :
215 : 0 : if (!g_file_copy (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
216 : : {
217 : 0 : if (interactive && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
218 : 0 : {
219 : : char line[16];
220 : :
221 : 0 : g_error_free (error);
222 : 0 : error = NULL;
223 : :
224 : 0 : uri = g_file_get_uri (target);
225 : 0 : g_print (_("%s: overwrite “%s”? "), argv[0], uri);
226 : 0 : g_free (uri);
227 : :
228 : 0 : if (fgets (line, sizeof (line), stdin) &&
229 : 0 : (line[0] == 'y' || line[0] == 'Y'))
230 : : {
231 : 0 : flags |= G_FILE_COPY_OVERWRITE;
232 : 0 : start_time = g_get_monotonic_time ();
233 : 0 : previous_num_bytes = 0;
234 : 0 : if (!g_file_copy (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
235 : 0 : goto copy_failed;
236 : : }
237 : : }
238 : : else
239 : : {
240 : 0 : copy_failed:
241 : 0 : print_file_error (source, error->message);
242 : 0 : g_error_free (error);
243 : 0 : retval = 1;
244 : : }
245 : : }
246 : :
247 : 0 : if (progress && retval == 0)
248 : 0 : g_print ("\n");
249 : :
250 : 0 : g_object_unref (source);
251 : 0 : g_object_unref (target);
252 : : }
253 : :
254 : 0 : g_object_unref (dest);
255 : :
256 : 0 : return retval;
257 : : }
|