Repository navigation
Expand file tree
/
Copy pathdl.c
More file actions
273 lines (230 loc) · 7.41 KB
/
Copy pathdl.c
File metadata and controls
273 lines (230 loc) · 7.41 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* megatools - Mega.nz client library and tools
* Copyright (C) 2013 Ondřej Jirman <megous@megous.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "tools.h"
#ifdef G_OS_WIN32
#include <io.h>
#include <fcntl.h>
#endif
static gchar* opt_path = ".";
static gboolean opt_stream = FALSE;
static gboolean opt_noprogress = FALSE;
static gboolean opt_print_names = FALSE;
static GOptionEntry entries[] =
{
{ "path", '\0', 0, G_OPTION_ARG_FILENAME, &opt_path, "Local directory or file name, to save data to", "PATH" },
{ "no-progress", '\0', 0, G_OPTION_ARG_NONE, &opt_noprogress, "Disable progress bar", NULL},
{ "print-names", '\0', 0, G_OPTION_ARG_NONE, &opt_print_names, "Print names of downloaded files", NULL},
{ NULL }
};
static gchar* cur_file = NULL;
static mega_session* s;
static gboolean status_callback(mega_status_data* data, gpointer userdata)
{
if (opt_stream && data->type == MEGA_STATUS_DATA)
{
fwrite(data->data.buf, data->data.size, 1, stdout);
fflush(stdout);
}
if (data->type == MEGA_STATUS_FILEINFO)
{
cur_file = g_strdup(data->fileinfo.name);
}
if (!opt_noprogress && data->type == MEGA_STATUS_PROGRESS)
tool_show_progress(cur_file, data);
return FALSE;
}
// download operation
static gboolean dl_sync_file(mega_node* node, GFile* file, const gchar* remote_path)
{
gc_error_free GError *local_err = NULL;
gc_free gchar* local_path = g_file_get_path(file);
if (g_file_query_exists(file, NULL))
{
g_printerr("ERROR: File already exists at %s\n", local_path);
return FALSE;
}
if (!opt_noprogress)
g_print("F %s\n", local_path);
if (!mega_session_get(s, local_path, remote_path, &local_err))
{
if (!opt_noprogress)
g_print("\r" ESC_CLREOL);
g_printerr("ERROR: Download failed for %s: %s\n", remote_path, local_err->message);
return FALSE;
}
if (!opt_noprogress)
g_print("\r" ESC_CLREOL);
if (opt_print_names)
g_print("%s\n", local_path);
return TRUE;
}
static gboolean dl_sync_dir(mega_node* node, GFile* file, const gchar* remote_path)
{
gc_error_free GError *local_err = NULL;
gc_free gchar* local_path = g_file_get_path(file);
if (!g_file_query_exists(file, NULL))
{
if (!opt_noprogress)
g_print("D %s\n", local_path);
if (!g_file_make_directory(file, NULL, &local_err))
{
g_printerr("ERROR: Can't create local directory %s: %s\n", local_path, local_err->message);
return FALSE;
}
}
else
{
if (g_file_query_file_type(file, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL) != G_FILE_TYPE_DIRECTORY)
{
g_printerr("ERROR: Can't create local directory %s: file exists\n", local_path);
return FALSE;
}
}
// sync children
GSList* children = mega_session_get_node_chilren(s, node), *i;
for (i = children; i; i = i->next)
{
mega_node* child = i->data;
gc_free gchar* child_remote_path = g_strconcat(remote_path, "/", child->name, NULL);
gc_object_unref GFile* child_file = g_file_get_child(file, child->name);
if (child->type == 0)
{
dl_sync_file(child, child_file, child_remote_path);
}
else
{
dl_sync_dir(child, child_file, child_remote_path);
}
}
g_slist_free(children);
return TRUE;
}
int main(int ac, char* av[])
{
gc_error_free GError *local_err = NULL;
gc_regex_unref GRegex *file_regex = NULL, *folder_regex = NULL;
gint i;
int status = 0;
tool_init_bare(&ac, &av, "- download exported files from mega.nz", entries);
if (!strcmp(opt_path, "-"))
{
opt_noprogress = opt_stream = TRUE;
// see https://github.com/megous/megatools/issues/38
#ifdef G_OS_WIN32
setmode(fileno(stdout), O_BINARY);
#endif
}
if (ac < 2)
{
g_printerr("ERROR: No links specified for download!\n");
tool_fini(NULL);
return 1;
}
if (opt_stream && ac != 2)
{
g_printerr("ERROR: Can't stream from multiple files!\n");
tool_fini(NULL);
return 1;
}
// prepare link parsers
file_regex = g_regex_new("^https?://mega(?:\\.co)?\\.nz/#!([a-z0-9_-]{8})!([a-z0-9_-]{43})$", G_REGEX_CASELESS, 0, NULL);
g_assert(file_regex != NULL);
folder_regex = g_regex_new("^https?://mega(?:\\.co)?\\.nz/#F!([a-z0-9_-]{8})!([a-z0-9_-]{22})$", G_REGEX_CASELESS, 0, NULL);
g_assert(folder_regex != NULL);
// create session
s = mega_session_new();
mega_session_watch_status(s, status_callback, NULL);
// process links
for (i = 1; i < ac; i++)
{
gc_match_info_unref GMatchInfo* m1 = NULL;
gc_match_info_unref GMatchInfo* m2 = NULL;
gc_free gchar* key = NULL;
gc_free gchar* handle = NULL;
gc_free gchar* link = tool_convert_filename(av[i], FALSE);
if (g_regex_match(file_regex, link, 0, &m1))
{
handle = g_match_info_fetch(m1, 1);
key = g_match_info_fetch(m1, 2);
// perform download
if (!mega_session_dl(s, handle, key, opt_stream ? NULL : opt_path, &local_err))
{
if (!opt_noprogress)
g_print("\r" ESC_CLREOL "\n");
g_printerr("ERROR: Download failed for '%s': %s\n", link, local_err->message);
g_clear_error(&local_err);
status++;
}
else
{
if (!opt_noprogress)
g_print("\r" ESC_CLREOL "Downloaded %s\n", cur_file);
if (opt_print_names)
g_print("%s\n", cur_file);
}
}
else if (g_regex_match(folder_regex, link, 0, &m2))
{
if (opt_stream)
{
g_printerr("ERROR: Can't stream from a directory!\n");
tool_fini(s);
return 1;
}
handle = g_match_info_fetch(m2, 1);
key = g_match_info_fetch(m2, 2);
// perform download
if (!mega_session_open_exp_folder(s, handle, key, &local_err))
{
g_printerr("ERROR: Can't open folder '%s': %s\n", link, local_err->message);
g_clear_error(&local_err);
}
else
{
mega_session_watch_status(s, status_callback, NULL);
GSList* l = mega_session_ls(s, "/", FALSE);
if (g_slist_length(l) == 1)
{
mega_node* root_node = l->data;
gc_object_unref GFile* local_dir = g_file_new_for_path(opt_path);
if (g_file_query_file_type(local_dir, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL) == G_FILE_TYPE_DIRECTORY)
{
gc_free gchar* node_path = mega_node_get_path_dup(root_node);
dl_sync_dir(root_node, local_dir, node_path);
}
else
{
g_printerr("ERROR: %s must be a directory\n", opt_path);
}
}
else
{
g_printerr("ERROR: EXP folder fs has multiple toplevel nodes? Weird!\n");
}
g_slist_free(l);
}
}
else
{
g_printerr("WARNING: Skipping invalid Mega download link: %s\n", link);
}
}
tool_fini(s);
return status;
}