1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/* GIO - GLib Input, Output and Streaming Library
*
* Copyright (C) 2006-2007 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author: Alexander Larsson <alexl@redhat.com>
*/
#include <config.h>
#include <stdio.h>
#include <unistd.h>
#include <locale.h>
#include <errno.h>
#include <string.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
static gboolean progress = FALSE;
static gboolean interactive = FALSE;
static gboolean backup = FALSE;
static gboolean no_target_directory = FALSE;
static gboolean show_version = FALSE;
static GOptionEntry entries[] =
{
{ "no-target-directory", 'T', 0, G_OPTION_ARG_NONE, &no_target_directory, N_("No target directory"), NULL },
{ "progress", 'p', 0, G_OPTION_ARG_NONE, &progress, N_("Show progress"), NULL },
{ "interactive", 'i', 0, G_OPTION_ARG_NONE, &interactive, N_("Prompt before overwrite"), NULL },
{ "backup", 'b', 0, G_OPTION_ARG_NONE, &backup, N_("Backup existing destination files"), NULL },
{ "version", 0, 0, G_OPTION_ARG_NONE, &show_version, N_("Show program version"), NULL },
{ NULL }
};
static gboolean
is_dir (GFile *file)
{
GFileInfo *info;
gboolean res;
info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, 0, NULL, NULL);
res = info && g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY;
if (info)
g_object_unref (info);
return res;
}
static gint64 start_time;
static gint64 previous_time;
static void
show_progress (goffset current_num_bytes,
goffset total_num_bytes,
gpointer user_data)
{
gint64 tv;
char *current_size, *total_size, *rate;
tv = g_get_monotonic_time ();
if (tv - previous_time < (G_USEC_PER_SEC / 5) &&
current_num_bytes != total_num_bytes)
return;
current_size = g_format_size (current_num_bytes);
total_size = g_format_size (total_num_bytes);
rate = g_format_size (current_num_bytes /
MAX ((tv - start_time) / G_USEC_PER_SEC, 1));
g_print ("\r\033[K");
g_print (_("Transferred %s out of %s (%s/s)"),
current_size, total_size, rate);
previous_time = tv;
g_free (current_size);
g_free (total_size);
g_free (rate);
}
static void
show_help (GOptionContext *context, const char *error)
{
char *help;
if (error)
g_printerr (_("Error: %s"), error);
help = g_option_context_get_help (context, TRUE, NULL);
g_printerr ("%s", help);
g_free (help);
g_option_context_free (context);
}
int
main (int argc, char *argv[])
{
GError *error;
GOptionContext *context;
GFile *source, *dest, *target;
gboolean dest_is_dir;
char *basename;
char *uri;
int i;
GFileCopyFlags flags;
int retval = 0;
gchar *param;
gchar *summary;
setlocale (LC_ALL, "");
bindtextdomain (GETTEXT_PACKAGE, GVFS_LOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
error = NULL;
param = g_strdup_printf ("%s... %s", _("SOURCE"), _("DEST"));
summary = _("Move one or more files from SOURCE to DEST.");
context = g_option_context_new (param);
g_option_context_set_summary (context, summary);
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
g_option_context_parse (context, &argc, &argv, &error);
if (error != NULL)
{
g_printerr (_("Error parsing commandline options: %s\n"), error->message);
g_printerr ("\n");
g_printerr (_("Try \"%s --help\" for more information."), g_get_prgname ());
g_printerr ("\n");
g_error_free (error);
return 1;
}
if (show_version)
{
g_print (PACKAGE_STRING "\n");
return 0;
}
if (argc <= 2)
{
show_help (context, _("Missing operand\n"));
return 1;
}
dest = g_file_new_for_commandline_arg (argv[argc-1]);
if (no_target_directory && argc > 3)
{
show_help (context, _("Too many arguments\n"));
g_object_unref (dest);
return 1;
}
dest_is_dir = is_dir (dest);
if (!dest_is_dir && argc > 3)
{
g_printerr (_("Target %s is not a directory\n"), argv[argc-1]);
show_help (context, NULL);
g_object_unref (dest);
return 1;
}
g_option_context_free (context);
g_free (param);
for (i = 1; i < argc - 1; i++)
{
source = g_file_new_for_commandline_arg (argv[i]);
if (dest_is_dir && !no_target_directory)
{
basename = g_file_get_basename (source);
target = g_file_get_child (dest, basename);
g_free (basename);
}
else
target = g_object_ref (dest);
flags = 0;
if (backup)
flags |= G_FILE_COPY_BACKUP;
if (!interactive)
flags |= G_FILE_COPY_OVERWRITE;
error = NULL;
start_time = g_get_monotonic_time ();
if (!g_file_move (source, target, flags, NULL, progress?show_progress:NULL, NULL, &error))
{
if (interactive && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
{
char line[16];
g_error_free (error);
error = NULL;
uri = g_file_get_uri (target);
g_print (_("%s: overwrite ā%sā? "), argv[0], uri);
g_free (uri);
if (fgets(line, sizeof (line), stdin) &&
(line[0] == 'y' || line[0] == 'Y'))
{
flags |= G_FILE_COPY_OVERWRITE;
if (!g_file_move (source, target, flags, NULL, NULL, NULL, &error))
goto move_failed;
}
}
else
{
move_failed:
g_printerr (_("Error moving file %s: %s\n"), argv[i], error->message);
g_error_free (error);
retval = 1;
}
}
if (progress && retval == 0)
g_print("\n");
g_object_unref (source);
g_object_unref (target);
}
g_object_unref (dest);
return retval;
}
|