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>
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
);
116 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
118 show_help (context
, error
->message
);
119 g_error_free (error
);
125 show_help (context
, NULL
);
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
);
138 dest_is_dir
= file_is_dir (dest
);
139 if (!dest_is_dir
&& argc
> 3)
143 message
= g_strdup_printf (_("Destination %s is not a directory"), argv
[argc
- 1]);
144 show_help (context
, message
);
146 g_object_unref (dest
);
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
);
162 target
= g_object_ref (dest
);
166 flags
|= G_FILE_COPY_BACKUP
;
168 flags
|= G_FILE_COPY_OVERWRITE
;
170 flags
|= G_FILE_COPY_NOFOLLOW_SYMLINKS
;
172 flags
|= G_FILE_COPY_ALL_METADATA
;
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
))
183 g_error_free (error
);
186 uri
= g_file_get_uri (target
);
187 g_print (_("%s: overwrite ā%sā? "), argv
[0], 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
))
202 print_file_error (source
, error
->message
);
203 g_error_free (error
);
208 if (progress
&& retval
== 0)
211 g_object_unref (source
);
212 g_object_unref (target
);
215 g_object_unref (dest
);