| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <libdex.h> |
| 24 |
|
| 25 |
#ifdef HAVE_UNISTD_H |
| 26 |
#include <unistd.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#define return_if_error(error) \ |
| 30 |
G_STMT_START { \ |
| 31 |
if (error) \ |
| 32 |
return dex_future_new_for_error (error); \ |
| 33 |
} G_STMT_END |
| 34 |
|
| 35 |
static DexFuture *copy (gpointer user_data); |
| 36 |
|
| 37 |
static DexScheduler *thread_pool; |
| 38 |
static gboolean verbose; |
| 39 |
static GMainLoop *main_loop; |
| 40 |
|
| 41 |
typedef struct |
| 42 |
{ |
| 43 |
GFile *from; |
| 44 |
GFile *to; |
| 45 |
guint recursive : 1; |
| 46 |
} Copy; |
| 47 |
|
| 48 |
static Copy * |
| 49 |
copy_new (GFile *from, |
| 50 |
GFile *to, |
| 51 |
gboolean recursive) |
| 52 |
{ |
| 53 |
Copy *cp = g_new0 (Copy, 1); |
| 54 |
cp->from = from; |
| 55 |
cp->to = to; |
| 56 |
cp->recursive = !!recursive; |
| 57 |
return cp; |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
copy_free (Copy *cp) |
| 62 |
{ |
| 63 |
g_clear_object (&cp->from); |
| 64 |
g_clear_object (&cp->to); |
| 65 |
g_free (cp); |
| 66 |
} |
| 67 |
|
| 68 |
static DexFuture * |
| 69 |
copy_regular (Copy *cp) |
| 70 |
{ |
| 71 |
g_autoptr(GInputStream) input = NULL; |
| 72 |
g_autoptr(GOutputStream) output = NULL; |
| 73 |
GError *error = NULL; |
| 74 |
G_GNUC_UNUSED gssize len; |
| 75 |
|
| 76 |
if (verbose) |
| 77 |
g_printerr ("%s => %s\n", g_file_peek_path (cp->from), g_file_peek_path (cp->to)); |
| 78 |
|
| 79 |
input = dex_await_object (dex_file_read (cp->from, G_PRIORITY_DEFAULT), &error); |
| 80 |
return_if_error (error); |
| 81 |
|
| 82 |
output = dex_await_object (dex_file_replace (cp->to, NULL, FALSE, |
| 83 |
G_FILE_CREATE_REPLACE_DESTINATION, |
| 84 |
G_PRIORITY_DEFAULT), |
| 85 |
&error); |
| 86 |
return_if_error (error); |
| 87 |
|
| 88 |
len = dex_await_int64 (dex_output_stream_splice (output, input, |
| 89 |
(G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | |
| 90 |
G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET), |
| 91 |
G_PRIORITY_DEFAULT), |
| 92 |
&error); |
| 93 |
return_if_error (error); |
| 94 |
|
| 95 |
return dex_future_new_for_boolean (TRUE); |
| 96 |
} |
| 97 |
|
| 98 |
static DexFuture * |
| 99 |
copy_directory (Copy *cp) |
| 100 |
{ |
| 101 |
g_autoptr(GFileEnumerator) enumerator = NULL; |
| 102 |
g_autoptr(GPtrArray) futures = g_ptr_array_new_with_free_func (dex_unref); |
| 103 |
GError *error = NULL; |
| 104 |
|
| 105 |
enumerator = dex_await_object (dex_file_enumerate_children (cp->from, |
| 106 |
G_FILE_ATTRIBUTE_STANDARD_NAME",", |
| 107 |
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, |
| 108 |
G_PRIORITY_DEFAULT), |
| 109 |
&error); |
| 110 |
return_if_error (error); |
| 111 |
|
| 112 |
if (verbose) |
| 113 |
g_printerr ("%s/ => %s/\n", g_file_peek_path (cp->from), g_file_peek_path (cp->to)); |
| 114 |
|
| 115 |
dex_await (dex_file_make_directory (cp->to, G_PRIORITY_DEFAULT), &error); |
| 116 |
return_if_error (error); |
| 117 |
|
| 118 |
for (;;) |
| 119 |
{ |
| 120 |
g_autolist(GFileInfo) files = NULL; |
| 121 |
|
| 122 |
files = dex_await_boxed (dex_file_enumerator_next_files (enumerator, 100, G_PRIORITY_DEFAULT), |
| 123 |
&error); |
| 124 |
return_if_error (error); |
| 125 |
|
| 126 |
if (files == NULL) |
| 127 |
break; |
| 128 |
|
| 129 |
for (const GList *iter = files; iter; iter = iter->next) |
| 130 |
{ |
| 131 |
GFileInfo *info = iter->data; |
| 132 |
|
| 133 |
g_ptr_array_add (futures, |
| 134 |
dex_scheduler_spawn (thread_pool, |
| 135 |
0, |
| 136 |
copy, |
| 137 |
copy_new (g_file_enumerator_get_child (enumerator, info), |
| 138 |
g_file_get_child (cp->to, g_file_info_get_name (info)), |
| 139 |
cp->recursive), |
| 140 |
(GDestroyNotify) copy_free)); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
if (futures->len > 0) |
| 145 |
{ |
| 146 |
dex_await (dex_future_allv ((DexFuture **)(gpointer)futures->pdata, futures->len), &error); |
| 147 |
return_if_error (error); |
| 148 |
} |
| 149 |
|
| 150 |
return dex_future_new_for_boolean (TRUE); |
| 151 |
} |
| 152 |
|
| 153 |
static DexFuture * |
| 154 |
copy_fallback (Copy *cp) |
| 155 |
{ |
| 156 |
if (verbose) |
| 157 |
g_print ("%s => %s\n", g_file_peek_path (cp->from), g_file_peek_path (cp->to)); |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
return dex_file_copy (cp->from, |
| 166 |
cp->to, |
| 167 |
G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA, |
| 168 |
G_PRIORITY_DEFAULT); |
| 169 |
} |
| 170 |
|
| 171 |
static DexFuture * |
| 172 |
copy (gpointer user_data) |
| 173 |
{ |
| 174 |
Copy *cp = user_data; |
| 175 |
g_autoptr(GFileInfo) info = NULL; |
| 176 |
GFileType file_type; |
| 177 |
GError *error = NULL; |
| 178 |
|
| 179 |
info = dex_await_object (dex_file_query_info (cp->from, |
| 180 |
G_FILE_ATTRIBUTE_STANDARD_SIZE"," |
| 181 |
G_FILE_ATTRIBUTE_STANDARD_TYPE"," |
| 182 |
G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK"," |
| 183 |
G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET",", |
| 184 |
G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, |
| 185 |
G_PRIORITY_DEFAULT), |
| 186 |
&error); |
| 187 |
return_if_error (error); |
| 188 |
|
| 189 |
file_type = g_file_info_get_file_type (info); |
| 190 |
|
| 191 |
if (file_type == G_FILE_TYPE_REGULAR) |
| 192 |
return copy_regular (cp); |
| 193 |
|
| 194 |
if (file_type == G_FILE_TYPE_DIRECTORY) |
| 195 |
{ |
| 196 |
if (!cp->recursive) |
| 197 |
return dex_future_new_reject (G_IO_ERROR, |
| 198 |
G_IO_ERROR_FAILED, |
| 199 |
"%s is a directory and -r is not set", |
| 200 |
g_file_peek_path (cp->from)); |
| 201 |
return copy_directory (cp); |
| 202 |
} |
| 203 |
|
| 204 |
return copy_fallback (cp); |
| 205 |
} |
| 206 |
|
| 207 |
static DexFuture * |
| 208 |
quit_cb (DexFuture *future, |
| 209 |
gpointer user_data) |
| 210 |
{ |
| 211 |
GError *error = NULL; |
| 212 |
|
| 213 |
if (!dex_await (dex_ref (future), &error)) |
| 214 |
g_error ("%s", error->message); |
| 215 |
|
| 216 |
g_main_loop_quit (main_loop); |
| 217 |
|
| 218 |
return NULL; |
| 219 |
} |
| 220 |
|
| 221 |
int |
| 222 |
main (int argc, |
| 223 |
char *argv[]) |
| 224 |
{ |
| 225 |
gboolean recursive = FALSE; |
| 226 |
GOptionEntry entries[] = { |
| 227 |
{ "recursive", 'r', 0, G_OPTION_ARG_NONE, &recursive, "Copy directory recursively" }, |
| 228 |
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Explain what is being done" }, |
| 229 |
{ NULL } |
| 230 |
}; |
| 231 |
GOptionContext *context; |
| 232 |
DexFuture *future; |
| 233 |
GError *error = NULL; |
| 234 |
|
| 235 |
dex_init (); |
| 236 |
|
| 237 |
context = g_option_context_new ("[OPTIONS...] SOURCE DEST - copy files"); |
| 238 |
g_option_context_add_main_entries (context, entries, NULL); |
| 239 |
if (!g_option_context_parse (context, &argc, &argv, &error)) |
| 240 |
g_error ("%s", error->message); |
| 241 |
|
| 242 |
if (argc != 3) |
| 243 |
{ |
| 244 |
g_printerr ("usage: %s SOURCE DEST\n", argv[0]); |
| 245 |
return EXIT_FAILURE; |
| 246 |
} |
| 247 |
|
| 248 |
main_loop = g_main_loop_new (NULL, FALSE); |
| 249 |
thread_pool = dex_thread_pool_scheduler_new (); |
| 250 |
future = dex_scheduler_spawn (NULL, |
| 251 |
0, |
| 252 |
copy, |
| 253 |
copy_new (g_file_new_for_commandline_arg (argv[1]), |
| 254 |
g_file_new_for_commandline_arg (argv[2]), |
| 255 |
recursive), |
| 256 |
(GDestroyNotify)copy_free); |
| 257 |
future = dex_future_finally (future, quit_cb, NULL, NULL); |
| 258 |
|
| 259 |
g_main_loop_run (main_loop); |
| 260 |
|
| 261 |
dex_unref (future); |
| 262 |
g_option_context_free (context); |
| 263 |
g_main_loop_unref (main_loop); |
| 264 |
|
| 265 |
return EXIT_SUCCESS; |
| 266 |
} |