Repository navigation
Expand file tree
/
Copy pathget.c
More file actions
109 lines (91 loc) · 2.83 KB
/
Copy pathget.c
File metadata and controls
109 lines (91 loc) · 2.83 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
/*
* 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"
static gchar* opt_path = ".";
static gboolean opt_stream = FALSE;
static gboolean opt_noprogress = 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},
{ NULL }
};
static gchar* cur_file = NULL;
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;
}
int main(int ac, char* av[])
{
gc_error_free GError *local_err = NULL;
mega_session* s;
tool_init(&ac, &av, "- download individual files from mega.nz", entries);
if (!strcmp(opt_path, "-"))
opt_noprogress = opt_stream = TRUE;
if (ac < 2)
{
g_printerr("ERROR: No files 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;
}
s = tool_start_session();
if (!s)
{
tool_fini(NULL);
return 1;
}
mega_session_watch_status(s, status_callback, NULL);
gint i;
for (i = 1; i < ac; i++)
{
gc_free gchar* path = tool_convert_filename(av[i], FALSE);
// perform download
if (!mega_session_get(s, opt_stream ? NULL : opt_path, path, &local_err))
{
if (!opt_noprogress)
g_print("\r" ESC_CLREOL "\n");
g_printerr("ERROR: Download failed for '%s': %s\n", path, local_
391B
err->message);
g_clear_error(&local_err);
}
else
{
if (!opt_noprogress)
g_print("\r" ESC_CLREOL "Downloaded %s\n", cur_file);
}
}
tool_fini(s);
return 0;
}