(no commit message)
[oweals/gnunet.git] / src / fs / gnunet-download.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
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
27  */
28 #include "platform.h"
29 #include "gnunet_fs_service.h"
30
31 static int ret;
32
33 static int verbose;
34
35 static int delete_incomplete;
36
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
38
39 static struct GNUNET_FS_Handle *ctx;
40
41 static struct GNUNET_FS_DownloadContext *dc;
42
43 static unsigned int anonymity = 1;
44
45 static unsigned int parallelism = 16;
46
47 static unsigned int request_parallelism = 4092;
48
49 static int do_recursive;
50
51 static char *filename;
52
53 static int local_only;
54
55 static void
56 cleanup_task (void *cls,
57               const struct GNUNET_SCHEDULER_TaskContext *tc)
58 {
59   GNUNET_FS_stop (ctx);
60   ctx = NULL;
61 }
62
63
64 static void
65 shutdown_task (void *cls,
66               const struct GNUNET_SCHEDULER_TaskContext *tc)
67 {
68   struct GNUNET_FS_DownloadContext *d;
69
70   if (dc != NULL)
71     {
72       d = dc;
73       dc = NULL;
74       GNUNET_FS_download_stop (d, delete_incomplete);
75     }
76 }
77
78
79 /**
80  * Called by FS client to give information about the progress of an 
81  * operation.
82  *
83  * @param cls closure
84  * @param info details about the event, specifying the event type
85  *        and various bits about the event
86  * @return client-context (for the next progress call
87  *         for this operation; should be set to NULL for
88  *         SUSPEND and STOPPED events).  The value returned
89  *         will be passed to future callbacks in the respective
90  *         field in the GNUNET_FS_ProgressInfo struct.
91  */
92 static void *
93 progress_cb (void *cls,
94              const struct GNUNET_FS_ProgressInfo *info)
95 {
96   char *s;
97   char *t;
98
99   switch (info->status)
100     {
101     case GNUNET_FS_STATUS_DOWNLOAD_START:
102       break;
103     case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
104       if (verbose)
105         {         
106           s = GNUNET_STRINGS_relative_time_to_string(info->value.download.eta);
107           t = GNUNET_STRINGS_byte_size_fancy(info->value.download.completed * 1000LL / (info->value.download.duration.rel_value + 1));
108           fprintf (stdout,
109                    _("Downloading `%s' at %llu/%llu (%s remaining, %s/s)\n"),
110                    info->value.download.filename,
111                    (unsigned long long) info->value.download.completed,
112                    (unsigned long long) info->value.download.size,
113                    s,
114                    t);
115           GNUNET_free (s);
116           GNUNET_free (t);
117         }
118       break;
119     case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
120       fprintf (stderr,
121                _("Error downloading: %s.\n"),
122                info->value.download.specifics.error.message);
123       GNUNET_SCHEDULER_shutdown ();
124       break;
125     case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
126       s = GNUNET_STRINGS_byte_size_fancy(info->value.download.completed * 1000 / (info->value.download.duration.rel_value + 1));
127       fprintf (stdout,
128                _("Downloading `%s' done (%s/s).\n"),
129                info->value.download.filename,
130                s);
131       GNUNET_free (s);
132       if (info->value.download.dc == dc)
133         GNUNET_SCHEDULER_shutdown ();
134       break;
135     case GNUNET_FS_STATUS_DOWNLOAD_STOPPED: 
136       if (info->value.download.dc == dc)
137         GNUNET_SCHEDULER_add_continuation (&cleanup_task,
138                                            NULL,
139                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
140       break;      
141     case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
142     case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
143       break;
144     default:
145       fprintf (stderr,
146                _("Unexpected status: %d\n"),
147                info->status);
148       break;
149     }
150   return NULL;
151 }
152
153
154 /**
155  * Main function that will be run by the scheduler.
156  *
157  * @param cls closure
158  * @param args remaining command-line arguments
159  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
160  * @param c configuration
161  */
162 static void
163 run (void *cls,
164      char *const *args,
165      const char *cfgfile,
166      const struct GNUNET_CONFIGURATION_Handle *c)
167 {
168   struct GNUNET_FS_Uri *uri;
169   char *emsg;
170   enum GNUNET_FS_DownloadOptions options;
171
172   uri = GNUNET_FS_uri_parse (args[0],
173                              &emsg);
174   if (NULL == uri)
175     {
176       fprintf (stderr,
177                _("Failed to parse URI: %s\n"),
178                emsg);
179       GNUNET_free (emsg);
180       ret = 1;
181       return;
182     }
183   if ( (! GNUNET_FS_uri_test_chk (uri)) &&
184        (! GNUNET_FS_uri_test_loc (uri)) )
185     {
186       fprintf (stderr,
187                "Only CHK or LOC URIs supported.\n");
188       ret = 1;
189       GNUNET_FS_uri_destroy (uri);
190       return;            
191     }
192   if (NULL == filename)
193     {
194       fprintf (stderr,
195                "Target filename must be specified.\n");
196       ret = 1;
197       GNUNET_FS_uri_destroy (uri);
198       return;            
199     }
200   cfg = c;
201   ctx = GNUNET_FS_start (cfg,
202                          "gnunet-download",
203                          &progress_cb,
204                          NULL,
205                          GNUNET_FS_FLAGS_NONE,
206                          GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM,
207                          parallelism,
208                          GNUNET_FS_OPTIONS_REQUEST_PARALLELISM,
209                          request_parallelism,
210                          GNUNET_FS_OPTIONS_END);
211   if (NULL == ctx)
212     {
213       fprintf (stderr,
214                _("Could not initialize `%s' subsystem.\n"),
215                "FS");
216       GNUNET_FS_uri_destroy (uri);
217       ret = 1;
218       return;
219     }
220   options = GNUNET_FS_DOWNLOAD_OPTION_NONE;
221   if (do_recursive)
222     options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
223   if (local_only)
224     options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY;
225   dc = GNUNET_FS_download_start (ctx,
226                                  uri,
227                                  NULL,
228                                  filename, NULL,
229                                  0,
230                                  GNUNET_FS_uri_chk_get_file_size (uri),
231                                  anonymity,
232                                  options,
233                                  NULL,
234                                  NULL);
235   GNUNET_FS_uri_destroy (uri);
236   if (dc == NULL)
237     {
238       GNUNET_FS_stop (ctx);
239       ctx = NULL;
240       return;
241     }
242   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
243                                 &shutdown_task,
244                                 NULL);
245 }
246
247
248 /**
249  * The main function to download GNUnet.
250  *
251  * @param argc number of arguments from the command line
252  * @param argv command line arguments
253  * @return 0 ok, 1 on error
254  */
255 int
256 main (int argc, char *const *argv)
257 {
258   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
259     {'a', "anonymity", "LEVEL",
260      gettext_noop ("set the desired LEVEL of receiver-anonymity"),
261      1, &GNUNET_GETOPT_set_uint, &anonymity},
262     {'D', "delete-incomplete", NULL,
263      gettext_noop ("delete incomplete downloads (when aborted with CTRL-C)"),
264      0, &GNUNET_GETOPT_set_one, &delete_incomplete},
265     {'n', "no-network", NULL,
266      gettext_noop ("only search the local peer (no P2P network search)"),
267      1, &GNUNET_GETOPT_set_uint, &local_only},
268     {'o', "output", "FILENAME",
269      gettext_noop ("write the file to FILENAME"),
270      1, &GNUNET_GETOPT_set_string, &filename},
271     {'p', "parallelism", "DOWNLOADS",
272      gettext_noop
273      ("set the maximum number of parallel downloads that is allowed"),
274      1, &GNUNET_GETOPT_set_uint, &parallelism},
275     {'r', "request-parallelism", "REQUESTS",
276      gettext_noop
277      ("set the maximum number of parallel requests for blocks that is allowed"),
278      1, &GNUNET_GETOPT_set_uint, &request_parallelism},
279     {'R', "recursive", NULL,
280      gettext_noop ("download a GNUnet directory recursively"),
281      0, &GNUNET_GETOPT_set_one, &do_recursive},
282     {'V', "verbose", NULL,
283      gettext_noop ("be verbose (print progress information)"),
284      0, &GNUNET_GETOPT_set_one, &verbose},
285     GNUNET_GETOPT_OPTION_END
286   };
287   return (GNUNET_OK ==
288           GNUNET_PROGRAM_run (argc,
289                               argv,
290                               "gnunet-download",
291                               gettext_noop
292                               ("Download files from GNUnet."),
293                               options, &run, NULL)) ? ret : 1;
294 }
295
296 /* end of gnunet-download.c */
297