Initialize variable
[glib.git] / gio / gio-tool-copy.c
blobc6a9b6b2da2c1dbb7d254d0c5710697feac9d305
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>
24 #include <stdio.h>
25 #if 0
26 #include <locale.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #endif
32 #include "gio-tool.h"
34 static gboolean no_target_directory = FALSE;
35 static gboolean progress = FALSE;
36 static gboolean interactive = FALSE;
37 static gboolean preserve = FALSE;
38 static gboolean backup = FALSE;
39 static gboolean no_dereference = FALSE;
41 static const GOptionEntry entries[] = {
42 { "no-target-directory", 'T', 0, G_OPTION_ARG_NONE, &no_target_directory, N_("No target directory"), NULL },
43 { "progress", 'p', 0, G_OPTION_ARG_NONE, &progress, N_("Show progress"), NULL },
44 { "interactive", 'i', 0, G_OPTION_ARG_NONE, &interactive, N_("Prompt before overwrite"), NULL },
45 { "preserve", 'p', 0, G_OPTION_ARG_NONE, &preserve, N_("Preserve all attributes"), NULL },
46 { "backup", 'b', 0, G_OPTION_ARG_NONE, &backup, N_("Backup existing destination files"), NULL },
47 { "no-dereference", 'P', 0, G_OPTION_ARG_NONE, &no_dereference, N_("Never follow symbolic links"), NULL },
48 { NULL }
51 static gint64 start_time;
52 static gint64 previous_time;
54 static void
55 show_progress (goffset current_num_bytes,
56 goffset total_num_bytes,
57 gpointer user_data)
59 gint64 tv;
60 char *current_size, *total_size, *rate;
62 tv = g_get_monotonic_time ();
63 if (tv - previous_time < (G_USEC_PER_SEC / 5) &&
64 current_num_bytes != total_num_bytes)
65 return;
67 current_size = g_format_size (current_num_bytes);
68 total_size = g_format_size (total_num_bytes);
69 rate = g_format_size (current_num_bytes /
70 MAX ((tv - start_time) / G_USEC_PER_SEC, 1));
71 g_print ("\r\033[K");
72 g_print (_("Transferred %s out of %s (%s/s)"), current_size, total_size, rate);
74 previous_time = tv;
76 g_free (current_size);
77 g_free (total_size);
78 g_free (rate);
81 int
82 handle_copy (int argc, char *argv[], gboolean do_help)
84 GOptionContext *context;
85 GError *error = NULL;
86 char *param;
87 GFile *source, *dest, *target;
88 gboolean dest_is_dir;
89 char *basename;
90 char *uri;
91 int i;
92 GFileCopyFlags flags;
93 int retval = 0;
95 g_set_prgname ("gio copy");
97 /* Translators: commandline placeholder */
98 param = g_strdup_printf ("%s... %s", _("SOURCE"), _("DESTINATION"));
99 context = g_option_context_new (param);
100 g_free (param);
101 g_option_context_set_help_enabled (context, FALSE);
102 g_option_context_set_summary (context,
103 _("Copy one or more files from SOURCE to DESTINATION."));
104 g_option_context_set_description (context,
105 _("gio copy is similar to the traditional cp utility, but using GIO\n"
106 "locations instead of local files: for example, you can use something\n"
107 "like smb://server/resource/file.txt as location."));
108 g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
110 if (do_help)
112 show_help (context, NULL);
113 g_option_context_free (context);
114 return 0;
117 if (!g_option_context_parse (context, &argc, &argv, &error))
119 show_help (context, error->message);
120 g_error_free (error);
121 g_option_context_free (context);
122 return 1;
125 if (argc < 3)
127 show_help (context, NULL);
128 g_option_context_free (context);
129 return 1;
132 dest = g_file_new_for_commandline_arg (argv[argc - 1]);
134 if (no_target_directory && argc > 3)
136 show_help (context, NULL);
137 g_object_unref (dest);
138 g_option_context_free (context);
139 return 1;
142 dest_is_dir = file_is_dir (dest);
143 if (!dest_is_dir && argc > 3)
145 char *message;
147 message = g_strdup_printf (_("Destination %s is not a directory"), argv[argc - 1]);
148 show_help (context, message);
149 g_free (message);
150 g_object_unref (dest);
151 g_option_context_free (context);
152 return 1;
155 g_option_context_free (context);
157 for (i = 1; i < argc - 1; i++)
159 source = g_file_new_for_commandline_arg (argv[i]);
160 if (dest_is_dir && !no_target_directory)
162 basename = g_file_get_basename (source);
163 target = g_file_get_child (dest, basename);
164 g_free (basename);
166 else
167 target = g_object_ref (dest);
169 flags = 0;
170 if (backup)
171 flags |= G_FILE_COPY_BACKUP;
172 if (!interactive)
173 flags |= G_FILE_COPY_OVERWRITE;
174 if (no_dereference)
175 flags |= G_FILE_COPY_NOFOLLOW_SYMLINKS;
176 if (preserve)
177 flags |= G_FILE_COPY_ALL_METADATA;
179 error = NULL;
180 start_time = g_get_monotonic_time ();
182 if (!g_file_copy (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
184 if (interactive && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
186 char line[16];
188 g_error_free (error);
189 error = NULL;
191 uri = g_file_get_uri (target);
192 g_print (_("%s: overwrite ā€œ%sā€? "), argv[0], uri);
193 g_free (uri);
195 if (fgets (line, sizeof (line), stdin) &&
196 (line[0] == 'y' || line[0] == 'Y'))
198 flags |= G_FILE_COPY_OVERWRITE;
199 start_time = g_get_monotonic_time ();
200 if (!g_file_copy (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
201 goto copy_failed;
204 else
206 copy_failed:
207 print_file_error (source, error->message);
208 g_error_free (error);
209 retval = 1;
213 if (progress && retval == 0)
214 g_print ("\n");
216 g_object_unref (source);
217 g_object_unref (target);
220 g_object_unref (dest);
222 return retval;