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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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,
96 if (FILE_TYPE_CHAR != GetFileType (GetStdHandle (STD_OUTPUT_HANDLE)))
99 ratio_complete = x/(float)n;
100 endeq = ratio_complete * w;
101 GNUNET_snprintf (buf, sizeof (buf),
102 "%3d%% [", (int)(ratio_complete*100) );
103 for (p=0; p<endeq; p++)
105 for (p=endeq; p<w; p++)
114 * Called by FS client to give information about the progress of an
118 * @param info details about the event, specifying the event type
119 * and various bits about the event
120 * @return client-context (for the next progress call
121 * for this operation; should be set to NULL for
122 * SUSPEND and STOPPED events). The value returned
123 * will be passed to future callbacks in the respective
124 * field in the `struct GNUNET_FS_ProgressInfo`
127 progress_cb (void *cls,
128 const struct GNUNET_FS_ProgressInfo *info)
134 switch (info->status)
136 case GNUNET_FS_STATUS_DOWNLOAD_START:
139 _("Starting download `%s'.\n"),
140 info->value.download.filename);
142 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
145 s = GNUNET_strdup (GNUNET_STRINGS_relative_time_to_string (info->value.download.eta,
147 if (info->value.download.specifics.progress.block_download_duration.rel_value_us
148 == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
149 s2 = _("<unknown time>");
151 s2 = GNUNET_STRINGS_relative_time_to_string (info->value.download.specifics.progress.block_download_duration,
153 t = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed *
155 (info->value.download.
156 duration.rel_value_us + 1));
158 _("Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"),
159 info->value.download.filename,
160 (unsigned long long) info->value.download.completed,
161 (unsigned long long) info->value.download.size,
170 display_bar (info->value.download.completed,
171 info->value.download.size,
175 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
178 fprintf (stdout, "\n");
180 if (FILE_TYPE_CHAR ==
181 GetFileType (GetStdHandle (STD_OUTPUT_HANDLE)))
182 fprintf (stdout, "\n");
184 FPRINTF (stderr, _("Error downloading: %s.\n"),
185 info->value.download.specifics.error.message);
186 GNUNET_SCHEDULER_shutdown ();
188 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
189 s = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed * 1000 /
190 (info->value.download.
191 duration.rel_value_us + 1));
194 fprintf (stdout, "\n");
196 if (FILE_TYPE_CHAR ==
197 GetFileType (GetStdHandle (STD_OUTPUT_HANDLE)))
198 fprintf (stdout, "\n");
201 _("Downloading `%s' done (%s/s).\n"),
202 info->value.download.filename, s);
204 if (info->value.download.dc == dc)
205 GNUNET_SCHEDULER_shutdown ();
207 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
208 if (info->value.download.dc == dc)
209 GNUNET_SCHEDULER_add_now (&cleanup_task, NULL);
211 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
212 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
216 _("Unexpected status: %d\n"),
225 * Main function that will be run by the scheduler.
228 * @param args remaining command-line arguments
229 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
230 * @param c configuration
236 const struct GNUNET_CONFIGURATION_Handle *c)
238 struct GNUNET_FS_Uri *uri;
240 enum GNUNET_FS_DownloadOptions options;
246 _("You need to specify a URI argument.\n"));
249 uri = GNUNET_FS_uri_parse (args[0], &emsg);
253 _("Failed to parse URI: %s\n"),
259 if ( (! GNUNET_FS_uri_test_chk (uri)) &&
260 (! GNUNET_FS_uri_test_loc (uri)))
264 _("Only CHK or LOC URIs supported.\n"));
266 GNUNET_FS_uri_destroy (uri);
269 if (NULL == filename)
273 _("Target filename must be specified.\n"));
275 GNUNET_FS_uri_destroy (uri);
279 ctx = GNUNET_FS_start (cfg,
282 GNUNET_FS_FLAGS_NONE,
283 GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM,
285 GNUNET_FS_OPTIONS_REQUEST_PARALLELISM,
287 GNUNET_FS_OPTIONS_END);
291 _("Could not initialize `%s' subsystem.\n"),
293 GNUNET_FS_uri_destroy (uri);
297 options = GNUNET_FS_DOWNLOAD_OPTION_NONE;
299 options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
301 options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY;
302 dc = GNUNET_FS_download_start (ctx,
308 GNUNET_FS_uri_chk_get_file_size (uri),
313 GNUNET_FS_uri_destroy (uri);
316 GNUNET_FS_stop (ctx);
320 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
326 * The main function to download GNUnet.
328 * @param argc number of arguments from the command line
329 * @param argv command line arguments
330 * @return 0 ok, 1 on error
333 main (int argc, char *const *argv)
335 struct GNUNET_GETOPT_CommandLineOption options[] = {
336 GNUNET_GETOPT_option_uint ('a',
339 gettext_noop ("set the desired LEVEL of receiver-anonymity"),
342 GNUNET_GETOPT_option_flag ('D',
344 gettext_noop ("delete incomplete downloads (when aborted with CTRL-C)"),
347 GNUNET_GETOPT_option_flag ('n',
349 gettext_noop ("only search the local peer (no P2P network search)"),
351 GNUNET_GETOPT_option_string ('o',
354 gettext_noop ("write the file to FILENAME"),
356 GNUNET_GETOPT_option_uint ('p',
359 gettext_noop ("set the maximum number of parallel downloads that is allowed"),
361 GNUNET_GETOPT_option_uint ('r',
362 "request-parallelism",
364 gettext_noop ("set the maximum number of parallel requests for blocks that is allowed"),
365 &request_parallelism),
366 GNUNET_GETOPT_option_flag ('R',
368 gettext_noop ("download a GNUnet directory recursively"),
370 GNUNET_GETOPT_option_increment_uint ('V',
372 gettext_noop ("be verbose (print progress information)"),
374 GNUNET_GETOPT_OPTION_END
378 GNUNET_STRINGS_get_utf8_args (argc, argv,
383 GNUNET_PROGRAM_run (argc, argv,
384 "gnunet-download [OPTIONS] URI",
386 ("Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"),
388 &run, NULL)) ? ret : 1;
389 GNUNET_free ((void*) argv);
393 /* end of gnunet-download.c */