2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file fs/gnunet-download.c
22 * @brief downloading for files on GNUnet
23 * @author Christian Grothoff
24 * @author Krista Bennett
25 * @author James Blackwell
26 * @author Igor Wronsky
29 #include "gnunet_fs_service.h"
33 static unsigned int verbose;
35 static int delete_incomplete;
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39 static struct GNUNET_FS_Handle *ctx;
41 static struct GNUNET_FS_DownloadContext *dc;
43 static unsigned int anonymity = 1;
45 static unsigned int parallelism = 16;
47 static unsigned int request_parallelism = 4092;
49 static int do_recursive;
51 static char *filename;
53 static int local_only;
57 cleanup_task(void *cls)
65 shutdown_task(void *cls)
69 GNUNET_FS_download_stop(dc, delete_incomplete);
76 * Display progress bar (if tty).
78 * @param x current position in the download
79 * @param n total size of the download
80 * @param w desired number of steps in the progress bar
83 display_bar(unsigned long long x, unsigned long long n, unsigned int w)
94 if (FILE_TYPE_CHAR != GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)))
97 ratio_complete = x / (float)n;
98 endeq = ratio_complete * w;
99 GNUNET_snprintf(buf, sizeof(buf), "%3d%% [", (int)(ratio_complete * 100));
100 for (p = 0; p < endeq; p++)
102 for (p = endeq; p < w; p++)
111 * Called by FS client to give information about the progress of an
115 * @param info details about the event, specifying the event type
116 * and various bits about the event
117 * @return client-context (for the next progress call
118 * for this operation; should be set to NULL for
119 * SUSPEND and STOPPED events). The value returned
120 * will be passed to future callbacks in the respective
121 * field in the `struct GNUNET_FS_ProgressInfo`
124 progress_cb(void *cls, const struct GNUNET_FS_ProgressInfo *info)
130 switch (info->status)
132 case GNUNET_FS_STATUS_DOWNLOAD_START:
135 _("Starting download `%s'.\n"),
136 info->value.download.filename);
139 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
143 GNUNET_STRINGS_relative_time_to_string(info->value.download.eta,
145 if (info->value.download.specifics.progress.block_download_duration
146 .rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
147 s2 = _("<unknown time>");
149 s2 = GNUNET_STRINGS_relative_time_to_string(info->value.download
151 .block_download_duration,
153 t = GNUNET_STRINGS_byte_size_fancy(
154 info->value.download.completed * 1000LL /
155 (info->value.download.duration.rel_value_us + 1));
159 "Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"),
160 info->value.download.filename,
161 (unsigned long long)info->value.download.completed,
162 (unsigned long long)info->value.download.size,
171 display_bar(info->value.download.completed,
172 info->value.download.size,
177 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
180 fprintf(stdout, "\n");
182 if (FILE_TYPE_CHAR == GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)))
183 fprintf(stdout, "\n");
186 _("Error downloading: %s.\n"),
187 info->value.download.specifics.error.message);
188 GNUNET_SCHEDULER_shutdown();
191 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
192 s = GNUNET_STRINGS_byte_size_fancy(
193 info->value.download.completed * 1000 /
194 (info->value.download.duration.rel_value_us + 1));
197 fprintf(stdout, "\n");
199 if (FILE_TYPE_CHAR == GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)))
200 fprintf(stdout, "\n");
203 _("Downloading `%s' done (%s/s).\n"),
204 info->value.download.filename,
207 if (info->value.download.dc == dc)
208 GNUNET_SCHEDULER_shutdown();
211 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
212 if (info->value.download.dc == dc)
213 GNUNET_SCHEDULER_add_now(&cleanup_task, NULL);
216 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
217 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
221 fprintf(stderr, _("Unexpected status: %d\n"), info->status);
229 * Main function that will be run by the scheduler.
232 * @param args remaining command-line arguments
233 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
234 * @param c configuration
240 const struct GNUNET_CONFIGURATION_Handle *c)
242 struct GNUNET_FS_Uri *uri;
244 enum GNUNET_FS_DownloadOptions options;
248 fprintf(stderr, "%s", _("You need to specify a URI argument.\n"));
251 uri = GNUNET_FS_uri_parse(args[0], &emsg);
254 fprintf(stderr, _("Failed to parse URI: %s\n"), emsg);
259 if ((!GNUNET_FS_uri_test_chk(uri)) && (!GNUNET_FS_uri_test_loc(uri)))
261 fprintf(stderr, "%s", _("Only CHK or LOC URIs supported.\n"));
263 GNUNET_FS_uri_destroy(uri);
266 if (NULL == filename)
268 fprintf(stderr, "%s", _("Target filename must be specified.\n"));
270 GNUNET_FS_uri_destroy(uri);
274 ctx = GNUNET_FS_start(cfg,
278 GNUNET_FS_FLAGS_NONE,
279 GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM,
281 GNUNET_FS_OPTIONS_REQUEST_PARALLELISM,
283 GNUNET_FS_OPTIONS_END);
286 fprintf(stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
287 GNUNET_FS_uri_destroy(uri);
291 options = GNUNET_FS_DOWNLOAD_OPTION_NONE;
293 options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
295 options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY;
296 dc = GNUNET_FS_download_start(ctx,
302 GNUNET_FS_uri_chk_get_file_size(uri),
307 GNUNET_FS_uri_destroy(uri);
314 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL);
319 * The main function to download GNUnet.
321 * @param argc number of arguments from the command line
322 * @param argv command line arguments
323 * @return 0 ok, 1 on error
326 main(int argc, char *const *argv)
328 struct GNUNET_GETOPT_CommandLineOption options[] =
329 { GNUNET_GETOPT_option_uint('a',
333 "set the desired LEVEL of receiver-anonymity"),
336 GNUNET_GETOPT_option_flag(
339 gettext_noop("delete incomplete downloads (when aborted with CTRL-C)"),
342 GNUNET_GETOPT_option_flag(
345 gettext_noop("only search the local peer (no P2P network search)"),
347 GNUNET_GETOPT_option_string('o',
350 gettext_noop("write the file to FILENAME"),
352 GNUNET_GETOPT_option_uint(
357 "set the maximum number of parallel downloads that is allowed"),
359 GNUNET_GETOPT_option_uint(
361 "request-parallelism",
364 "set the maximum number of parallel requests for blocks that is allowed"),
365 &request_parallelism),
366 GNUNET_GETOPT_option_flag('R',
369 "download a GNUnet directory recursively"),
371 GNUNET_GETOPT_option_increment_uint(
374 gettext_noop("be verbose (print progress information)"),
376 GNUNET_GETOPT_OPTION_END };
378 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
386 "gnunet-download [OPTIONS] URI",
388 "Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"),
394 GNUNET_free((void *)argv);
398 /* end of gnunet-download.c */