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>
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
},
51 static gint64 start_time
;
52 static gint64 previous_time
;
55 show_progress (goffset current_num_bytes
,
56 goffset total_num_bytes
,
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
)
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));
72 g_print (_("Transferred %s out of %s (%s/s)"), current_size
, total_size
, rate
);
76 g_free (current_size
);
82 handle_copy (int argc
, char *argv
[], gboolean do_help
)
84 GOptionContext
*context
;
87 GFile
*source
, *dest
, *target
;
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
);
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
);
112 show_help (context
, NULL
);
113 g_option_context_free (context
);
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
);
127 show_help (context
, NULL
);
128 g_option_context_free (context
);
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
);
142 dest_is_dir
= file_is_dir (dest
);
143 if (!dest_is_dir
&& argc
> 3)
147 message
= g_strdup_printf (_("Destination %s is not a directory"), argv
[argc
- 1]);
148 show_help (context
, message
);
150 g_object_unref (dest
);
151 g_option_context_free (context
);
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
);
167 target
= g_object_ref (dest
);
171 flags
|= G_FILE_COPY_BACKUP
;
173 flags
|= G_FILE_COPY_OVERWRITE
;
175 flags
|= G_FILE_COPY_NOFOLLOW_SYMLINKS
;
177 flags
|= G_FILE_COPY_ALL_METADATA
;
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
))
188 g_error_free (error
);
191 uri
= g_file_get_uri (target
);
192 g_print (_("%s: overwrite ā%sā? "), argv
[0], 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
))
207 print_file_error (source
, error
->message
);
208 g_error_free (error
);
213 if (progress
&& retval
== 0)
216 g_object_unref (source
);
217 g_object_unref (target
);
220 g_object_unref (dest
);