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