gosxappinfo: fix typo in url_escape_hostname
[glib.git] / gio / gio-tool-copy.c
blobc64fd4e7816856c0cbad57230015915e5bf3b064
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>
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 return 0;
116 if (!g_option_context_parse (context, &argc, &argv, &error))
118 show_help (context, error->message);
119 g_error_free (error);
120 return 1;
123 if (argc < 3)
125 show_help (context, NULL);
126 return 1;
129 dest = g_file_new_for_commandline_arg (argv[argc - 1]);
131 if (no_target_directory && argc > 3)
133 show_help (context, NULL);
134 g_object_unref (dest);
135 return 1;
138 dest_is_dir = file_is_dir (dest);
139 if (!dest_is_dir && argc > 3)
141 char *message;
143 message = g_strdup_printf (_("Destination %s is not a directory"), argv[argc - 1]);
144 show_help (context, message);
145 g_free (message);
146 g_object_unref (dest);
147 return 1;
150 g_option_context_free (context);
152 for (i = 1; i < argc - 1; i++)
154 source = g_file_new_for_commandline_arg (argv[i]);
155 if (dest_is_dir && !no_target_directory)
157 basename = g_file_get_basename (source);
158 target = g_file_get_child (dest, basename);
159 g_free (basename);
161 else
162 target = g_object_ref (dest);
164 flags = 0;
165 if (backup)
166 flags |= G_FILE_COPY_BACKUP;
167 if (!interactive)
168 flags |= G_FILE_COPY_OVERWRITE;
169 if (no_dereference)
170 flags |= G_FILE_COPY_NOFOLLOW_SYMLINKS;
171 if (preserve)
172 flags |= G_FILE_COPY_ALL_METADATA;
174 error = NULL;
175 start_time = g_get_monotonic_time ();
177 if (!g_file_copy (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
179 if (interactive && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
181 char line[16];
183 g_error_free (error);
184 error = NULL;
186 uri = g_file_get_uri (target);
187 g_print (_("%s: overwrite ā€œ%sā€? "), argv[0], uri);
188 g_free (uri);
190 if (fgets (line, sizeof (line), stdin) &&
191 (line[0] == 'y' || line[0] == 'Y'))
193 flags |= G_FILE_COPY_OVERWRITE;
194 start_time = g_get_monotonic_time ();
195 if (!g_file_copy (source, target, flags, NULL, progress ? show_progress : NULL, NULL, &error))
196 goto copy_failed;
199 else
201 copy_failed:
202 print_file_error (source, error->message);
203 g_error_free (error);
204 retval = 1;
208 if (progress && retval == 0)
209 g_print ("\n");
211 g_object_unref (source);
212 g_object_unref (target);
215 g_object_unref (dest);
217 return retval;