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 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.
16 * @file fs/gnunet-download.c
17 * @brief downloading for files on GNUnet
18 * @author Christian Grothoff
19 * @author Krista Bennett
20 * @author James Blackwell
21 * @author Igor Wronsky
24 #include "gnunet_fs_service.h"
28 static unsigned int verbose;
30 static int delete_incomplete;
32 static const struct GNUNET_CONFIGURATION_Handle *cfg;
34 static struct GNUNET_FS_Handle *ctx;
36 static struct GNUNET_FS_DownloadContext *dc;
38 static unsigned int anonymity = 1;
40 static unsigned int parallelism = 16;
42 static unsigned int request_parallelism = 4092;
44 static int do_recursive;
46 static char *filename;
48 static int local_only;
52 cleanup_task (void *cls)
60 shutdown_task (void *cls)
64 GNUNET_FS_download_stop (dc, delete_incomplete);
71 * Display progress bar (if tty).
73 * @param x current position in the download
74 * @param n total size of the download
75 * @param w desired number of steps in the progress bar
78 display_bar (unsigned long long x,
91 if (FILE_TYPE_CHAR != GetFileType (GetStdHandle (STD_OUTPUT_HANDLE)))
94 ratio_complete = x/(float)n;
95 endeq = ratio_complete * w;
96 GNUNET_snprintf (buf, sizeof (buf),
97 "%3d%% [", (int)(ratio_complete*100) );
98 for (p=0; p<endeq; p++)
100 for (p=endeq; p<w; p++)
109 * Called by FS client to give information about the progress of an
113 * @param info details about the event, specifying the event type
114 * and various bits about the event
115 * @return client-context (for the next progress call
116 * for this operation; should be set to NULL for
117 * SUSPEND and STOPPED events). The value returned
118 * will be passed to future callbacks in the respective
119 * field in the `struct GNUNET_FS_ProgressInfo`
122 progress_cb (void *cls,
123 const struct GNUNET_FS_ProgressInfo *info)
129 switch (info->status)
131 case GNUNET_FS_STATUS_DOWNLOAD_START:
134 _("Starting download `%s'.\n"),
135 info->value.download.filename);
137 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
140 s = GNUNET_strdup (GNUNET_STRINGS_relative_time_to_string (info->value.download.eta,
142 if (info->value.download.specifics.progress.block_download_duration.rel_value_us
143 == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
144 s2 = _("<unknown time>");
146 s2 = GNUNET_STRINGS_relative_time_to_string (info->value.download.specifics.progress.block_download_duration,
148 t = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed *
150 (info->value.download.
151 duration.rel_value_us + 1));
153 _("Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"),
154 info->value.download.filename,
155 (unsigned long long) info->value.download.completed,
156 (unsigned long long) info->value.download.size,
165 display_bar (info->value.download.completed,
166 info->value.download.size,
170 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
173 fprintf (stdout, "\n");
175 if (FILE_TYPE_CHAR ==
176 GetFileType (GetStdHandle (STD_OUTPUT_HANDLE)))
177 fprintf (stdout, "\n");
179 FPRINTF (stderr, _("Error downloading: %s.\n"),
180 info->value.download.specifics.error.message);
181 GNUNET_SCHEDULER_shutdown ();
183 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
184 s = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed * 1000 /
185 (info->value.download.
186 duration.rel_value_us + 1));
189 fprintf (stdout, "\n");
191 if (FILE_TYPE_CHAR ==
192 GetFileType (GetStdHandle (STD_OUTPUT_HANDLE)))
193 fprintf (stdout, "\n");
196 _("Downloading `%s' done (%s/s).\n"),
197 info->value.download.filename, s);
199 if (info->value.download.dc == dc)
200 GNUNET_SCHEDULER_shutdown ();
202 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
203 if (info->value.download.dc == dc)
204 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
206 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
207 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
211 _("Unexpected status: %d\n"),
220 * Main function that will be run by the scheduler.
223 * @param args remaining command-line arguments
224 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
225 * @param c configuration
231 const struct GNUNET_CONFIGURATION_Handle *c)
233 struct GNUNET_FS_Uri *uri;
235 enum GNUNET_FS_DownloadOptions options;
241 _("You need to specify a URI argument.\n"));
244 uri = GNUNET_FS_uri_parse (args[0], &emsg);
248 _("Failed to parse URI: %s\n"),
254 if ( (! GNUNET_FS_uri_test_chk (uri)) &&
255 (! GNUNET_FS_uri_test_loc (uri)))
259 _("Only CHK or LOC URIs supported.\n"));
261 GNUNET_FS_uri_destroy (uri);
264 if (NULL == filename)
268 _("Target filename must be specified.\n"));
270 GNUNET_FS_uri_destroy (uri);
274 ctx = GNUNET_FS_start (cfg,
277 GNUNET_FS_FLAGS_NONE,
278 GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM,
280 GNUNET_FS_OPTIONS_REQUEST_PARALLELISM,
282 GNUNET_FS_OPTIONS_END);
286 _("Could not initialize `%s' subsystem.\n"),
288 GNUNET_FS_uri_destroy (uri);
292 options = GNUNET_FS_DOWNLOAD_OPTION_NONE;
294 options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
296 options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY;
297 dc = GNUNET_FS_download_start (ctx,
303 GNUNET_FS_uri_chk_get_file_size (uri),
308 GNUNET_FS_uri_destroy (uri);
311 GNUNET_FS_stop (ctx);
315 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
321 * The main function to download GNUnet.
323 * @param argc number of arguments from the command line
324 * @param argv command line arguments
325 * @return 0 ok, 1 on error
328 main (int argc, char *const *argv)
330 struct GNUNET_GETOPT_CommandLineOption options[] = {
331 GNUNET_GETOPT_option_uint ('a',
334 gettext_noop ("set the desired LEVEL of receiver-anonymity"),
337 GNUNET_GETOPT_option_flag ('D',
339 gettext_noop ("delete incomplete downloads (when aborted with CTRL-C)"),
342 GNUNET_GETOPT_option_flag ('n',
344 gettext_noop ("only search the local peer (no P2P network search)"),
346 GNUNET_GETOPT_option_string ('o',
349 gettext_noop ("write the file to FILENAME"),
351 GNUNET_GETOPT_option_uint ('p',
354 gettext_noop ("set the maximum number of parallel downloads that is allowed"),
356 GNUNET_GETOPT_option_uint ('r',
357 "request-parallelism",
359 gettext_noop ("set the maximum number of parallel requests for blocks that is allowed"),
360 &request_parallelism),
361 GNUNET_GETOPT_option_flag ('R',
363 gettext_noop ("download a GNUnet directory recursively"),
365 GNUNET_GETOPT_option_increment_uint ('V',
367 gettext_noop ("be verbose (print progress information)"),
369 GNUNET_GETOPT_OPTION_END
373 GNUNET_STRINGS_get_utf8_args (argc, argv,
378 GNUNET_PROGRAM_run (argc, argv,
379 "gnunet-download [OPTIONS] URI",
381 ("Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"),
383 &run, NULL)) ? ret : 1;
384 GNUNET_free ((void*) argv);
388 /* end of gnunet-download.c */