2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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"
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, const struct GNUNET_SCHEDULER_TaskContext *tc)
65 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
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 GNUNET_FS_ProgressInfo struct.
127 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
133 switch (info->status)
135 case GNUNET_FS_STATUS_DOWNLOAD_START:
137 FPRINTF (stderr, _("Starting download `%s'.\n"),
138 info->value.download.filename);
140 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
143 s = GNUNET_strdup (GNUNET_STRINGS_relative_time_to_string (info->value.download.eta,
145 if (info->value.download.specifics.progress.block_download_duration.rel_value
146 == GNUNET_TIME_UNIT_FOREVER_REL.rel_value)
147 s2 = _("<unknown time>");
149 s2 = GNUNET_STRINGS_relative_time_to_string (
150 info->value.download.specifics.progress.block_download_duration,
152 t = GNUNET_STRINGS_byte_size_fancy (info->value.download.completed *
154 (info->value.download.
155 duration.rel_value + 1));
157 _("Downloading `%s' at %llu/%llu (%s remaining, %s/s). Block took %s to download\n"),
158 info->value.download.filename,
159 (unsigned long long) info->value.download.completed,
160 (unsigned long long) info->value.download.size, s, t, s2);
166 display_bar (info->value.download.completed,
167 info->value.download.size,
171 case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
174 fprintf (stdout, "\n");
176 if (FILE_TYPE_CHAR == 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 + 1));
189 fprintf (stdout, "\n");
191 if (FILE_TYPE_CHAR == GetFileType (GetStdHandle (STD_OUTPUT_HANDLE)))
192 fprintf (stdout, "\n");
194 FPRINTF (stdout, _("Downloading `%s' done (%s/s).\n"),
195 info->value.download.filename, s);
197 if (info->value.download.dc == dc)
198 GNUNET_SCHEDULER_shutdown ();
200 case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
201 if (info->value.download.dc == dc)
202 GNUNET_SCHEDULER_add_continuation (&cleanup_task, NULL,
203 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
205 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
206 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
209 FPRINTF (stderr, _("Unexpected status: %d\n"), info->status);
217 * Main function that will be run by the scheduler.
220 * @param args remaining command-line arguments
221 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
222 * @param c configuration
225 run (void *cls, char *const *args, const char *cfgfile,
226 const struct GNUNET_CONFIGURATION_Handle *c)
228 struct GNUNET_FS_Uri *uri;
230 enum GNUNET_FS_DownloadOptions options;
234 FPRINTF (stderr, "%s", _("You need to specify a URI argument.\n"));
237 uri = GNUNET_FS_uri_parse (args[0], &emsg);
240 FPRINTF (stderr, _("Failed to parse URI: %s\n"), emsg);
245 if ((!GNUNET_FS_uri_test_chk (uri)) && (!GNUNET_FS_uri_test_loc (uri)))
247 FPRINTF (stderr, "%s", _("Only CHK or LOC URIs supported.\n"));
249 GNUNET_FS_uri_destroy (uri);
252 if (NULL == filename)
254 FPRINTF (stderr, "%s", _("Target filename must be specified.\n"));
256 GNUNET_FS_uri_destroy (uri);
261 GNUNET_FS_start (cfg, "gnunet-download", &progress_cb, NULL,
262 GNUNET_FS_FLAGS_NONE,
263 GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM, parallelism,
264 GNUNET_FS_OPTIONS_REQUEST_PARALLELISM,
265 request_parallelism, GNUNET_FS_OPTIONS_END);
268 FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
269 GNUNET_FS_uri_destroy (uri);
273 options = GNUNET_FS_DOWNLOAD_OPTION_NONE;
275 options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
277 options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY;
278 dc = GNUNET_FS_download_start (ctx, uri, NULL, filename, NULL, 0,
279 GNUNET_FS_uri_chk_get_file_size (uri),
280 anonymity, options, NULL, NULL);
281 GNUNET_FS_uri_destroy (uri);
284 GNUNET_FS_stop (ctx);
288 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
294 * The main function to download GNUnet.
296 * @param argc number of arguments from the command line
297 * @param argv command line arguments
298 * @return 0 ok, 1 on error
301 main (int argc, char *const *argv)
303 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
304 {'a', "anonymity", "LEVEL",
305 gettext_noop ("set the desired LEVEL of receiver-anonymity"),
306 1, &GNUNET_GETOPT_set_uint, &anonymity},
307 {'D', "delete-incomplete", NULL,
308 gettext_noop ("delete incomplete downloads (when aborted with CTRL-C)"),
309 0, &GNUNET_GETOPT_set_one, &delete_incomplete},
310 {'n', "no-network", NULL,
311 gettext_noop ("only search the local peer (no P2P network search)"),
312 0, &GNUNET_GETOPT_set_uint, &local_only},
313 {'o', "output", "FILENAME",
314 gettext_noop ("write the file to FILENAME"),
315 1, &GNUNET_GETOPT_set_string, &filename},
316 {'p', "parallelism", "DOWNLOADS",
318 ("set the maximum number of parallel downloads that is allowed"),
319 1, &GNUNET_GETOPT_set_uint, ¶llelism},
320 {'r', "request-parallelism", "REQUESTS",
322 ("set the maximum number of parallel requests for blocks that is allowed"),
323 1, &GNUNET_GETOPT_set_uint, &request_parallelism},
324 {'R', "recursive", NULL,
325 gettext_noop ("download a GNUnet directory recursively"),
326 0, &GNUNET_GETOPT_set_one, &do_recursive},
327 {'V', "verbose", NULL,
328 gettext_noop ("be verbose (print progress information)"),
329 0, &GNUNET_GETOPT_increment_value, &verbose},
330 GNUNET_GETOPT_OPTION_END
333 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
337 GNUNET_PROGRAM_run (argc, argv, "gnunet-download [OPTIONS] URI",
339 ("Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"),
340 options, &run, NULL)) ? ret : 1;
341 GNUNET_free ((void*) argv);
345 /* end of gnunet-download.c */