Add Friulian translation
[glib.git] / gio / gio-tool-move.c
blobacbe8e95421b09d1b3ab00f578212da9bbf6754d
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 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>
20 #include "config.h"
22 #include <gio/gio.h>
23 #include <gi18n.h>
24 #include <stdio.h>
26 #include "gio-tool.h"
29 /* statics {{{1 */
31 static gboolean no_target_directory = FALSE;
32 static gboolean progress = FALSE;
33 static gboolean interactive = FALSE;
34 static gboolean backup = FALSE;
35 static gboolean no_copy_fallback = FALSE;
37 static const GOptionEntry entries[] = {
38 { "no-target-directory", 'T', 0, G_OPTION_ARG_NONE, &no_target_directory, N_("No target directory"), NULL },
39 { "progress", 'p', 0, G_OPTION_ARG_NONE, &progress, N_("Show progress"), NULL },
40 { "interactive", 'i', 0, G_OPTION_ARG_NONE, &interactive, N_("Prompt before overwrite"), NULL },
41 { "backup", 'b', 0, G_OPTION_ARG_NONE, &backup, N_("Backup existing destination files"), NULL },
42 { "no-copy-fallback", 'C', 0, G_OPTION_ARG_NONE, &no_copy_fallback, N_("Don’t use copy and delete fallback"), NULL },
43 { NULL }
46 static gint64 start_time;
47 static gint64 previous_time;
49 static void
50 show_progress (goffset current_num_bytes,
51 goffset total_num_bytes,
52 gpointer user_data)
54 gint64 tv;
55 char *current_size, *total_size, *rate;
57 tv = g_get_monotonic_time ();
58 if (tv - previous_time < (G_USEC_PER_SEC / 5) &&
59 current_num_bytes != total_num_bytes)
60 return;
62 current_size = g_format_size (current_num_bytes);
63 total_size = g_format_size (total_num_bytes);
64 rate = g_format_size (current_num_bytes /
65 MAX ((tv - start_time) / G_USEC_PER_SEC, 1));
66 g_print ("\r\033[K");
67 g_print (_("Transferred %s out of %s (%s/s)"),
68 current_size, total_size, rate);
70 previous_time = tv;
72 g_free (current_size);
73 g_free (total_size);
74 g_free (rate);
77 int
78 handle_move (int argc, char *argv[], gboolean do_help)
80 GOptionContext *context;
81 gchar *param;
82 GError *error = NULL;
83 GFile *source, *dest, *target;
84 gboolean dest_is_dir;
85 char *basename;
86 char *uri;
87 int i;
88 GFileCopyFlags flags;
89 int retval = 0;
91 g_set_prgname ("gio move");
93 /* Translators: commandline placeholder */
94 param = g_strdup_printf ("%s... %s", _("SOURCE"), _("DESTINATION"));
95 context = g_option_context_new (param);
96 g_free (param);
97 g_option_context_set_help_enabled (context, FALSE);
98 g_option_context_set_summary (context,
99 _("Move one or more files from SOURCE to DEST."));
100 g_option_context_set_description (context,
101 _("gio move is similar to the traditional mv utility, but using GIO\n"
102 "locations instead of local files: for example, you can use something\n"
103 "like smb://server/resource/file.txt as location"));
104 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
106 if (do_help)
108 show_help (context, NULL);
109 return 0;
112 if (!g_option_context_parse (context, &argc, &argv, &error))
114 show_help (context, error->message);
115 g_error_free (error);
116 return 1;
119 if (argc < 3)
121 show_help (context, NULL);
122 return 1;
125 dest = g_file_new_for_commandline_arg (argv[argc - 1]);
127 if (no_target_directory && argc > 3)
129 show_help (context, NULL);
130 g_object_unref (dest);
131 return 1;
134 dest_is_dir = file_is_dir (dest);
136 if (!dest_is_dir && argc > 3)
138 char *message;
139 message = g_strdup_printf (_("Target %s is not a directory"), argv[argc - 1]);
140 show_help (context, message);
141 g_free (message);
142 g_object_unref (dest);
143 return 1;
146 g_option_context_free (context);
148 for (i = 1; i < argc - 1; i++)
150 source = g_file_new_for_commandline_arg (argv[i]);
152 if (dest_is_dir && !no_target_directory)
154 basename = g_file_get_basename (source);
155 target = g_file_get_child (dest, basename);
156 g_free (basename);
158 else
159 target = g_object_ref (dest);
161 flags = 0;
162 if (backup)
163 flags |= G_FILE_COPY_BACKUP;
164 if (!interactive)
165 flags |= G_FILE_COPY_OVERWRITE;
166 if (no_copy_fallback)
167 flags |= G_FILE_COPY_NO_FALLBACK_FOR_MOVE;
169 error = NULL;
170 start_time = g_get_monotonic_time ();
171 if (!g_file_move (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
173 if (interactive && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
175 char line[16];
177 g_error_free (error);
178 error = NULL;
180 uri = g_file_get_uri (target);
181 g_print (_("%s: overwrite “%s”? "), argv[0], uri);
182 g_free (uri);
183 if (fgets (line, sizeof (line), stdin) &&
184 (line[0] == 'y' || line[0] == 'Y'))
186 flags |= G_FILE_COPY_OVERWRITE;
187 start_time = g_get_monotonic_time ();
188 if (!g_file_move (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
189 goto move_failed;
192 else
194 move_failed:
195 print_file_error (source, error->message);
196 g_error_free (error);
197 retval = 1;
201 if (progress && retval == 0)
202 g_print("\n");
204 g_object_unref (source);
205 g_object_unref (target);
208 g_object_unref (dest);
210 return retval;