comments
[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       if (verbose > 1)
103         fprintf (stderr,
104                  _("Starting download `%s'.\n"),
105                  info->value.download.filename);
106       break;
107     case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
108       if (verbose)
109         {         
110           s = GNUNET_STRINGS_relative_time_to_string(info->value.download.eta);
111           t = GNUNET_STRINGS_byte_size_fancy(info->value.download.completed * 1000LL / (info->value.download.duration.rel_value + 1));
112           fprintf (stdout,
113                    _("Downloading `%s' at %llu/%llu (%s remaining, %s/s)\n"),
114                    info->value.download.filename,
115                    (unsigned long long) info->value.download.completed,
116                    (unsigned long long) info->value.download.size,
117                    s,
118                    t);
119           GNUNET_free (s);
120           GNUNET_free (t);
121         }
122       break;
123     case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
124       fprintf (stderr,
125                _("Error downloading: %s.\n"),
126                info->value.download.specifics.error.message);
127       GNUNET_SCHEDULER_shutdown ();
128       break;
129     case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
130       s = GNUNET_STRINGS_byte_size_fancy(info->value.download.completed * 1000 / (info->value.download.duration.rel_value + 1));
131       fprintf (stdout,
132                _("Downloading `%s' done (%s/s).\n"),
133                info->value.download.filename,
134                s);
135       GNUNET_free (s);
136       if (info->value.download.dc == dc)
137         GNUNET_SCHEDULER_shutdown ();
138       break;
139     case GNUNET_FS_STATUS_DOWNLOAD_STOPPED: 
140       if (info->value.download.dc == dc)
141         GNUNET_SCHEDULER_add_continuation (&cleanup_task,
142                                            NULL,
143                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
144       break;      
145     case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
146     case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
147       break;
148     default:
149       fprintf (stderr,
150                _("Unexpected status: %d\n"),
151                info->status);
152       break;
153     }
154   return NULL;
155 }
156
157
158 /**
159  * Main function that will be run by the scheduler.
160  *
161  * @param cls closure
162  * @param args remaining command-line arguments
163  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
164  * @param c configuration
165  */
166 static void
167 run (void *cls,
168      char *const *args,
169      const char *cfgfile,
170      const struct GNUNET_CONFIGURATION_Handle *c)
171 {
172   struct GNUNET_FS_Uri *uri;
173   char *emsg;
174   enum GNUNET_FS_DownloadOptions options;
175
176   if (NULL == args[0])
177     {
178       fprintf (stderr,
179                _("You need to specify a URI argument.\n"));
180       return;
181     }
182   uri = GNUNET_FS_uri_parse (args[0],
183                              &emsg);
184   if (NULL == uri)
185     {
186       fprintf (stderr,
187                _("Failed to parse URI: %s\n"),
188                emsg);
189       GNUNET_free (emsg);
190       ret = 1;
191       return;
192     }
193   if ( (! GNUNET_FS_uri_test_chk (uri)) &&
194        (! GNUNET_FS_uri_test_loc (uri)) )
195     {
196       fprintf (stderr,
197                _("Only CHK or LOC URIs supported.\n"));
198       ret = 1;
199       GNUNET_FS_uri_destroy (uri);
200       return;            
201     }
202   if (NULL == filename)
203     {
204       fprintf (stderr,
205                _("Target filename must be specified.\n"));
206       ret = 1;
207       GNUNET_FS_uri_destroy (uri);
208       return;            
209     }
210   cfg = c;
211   ctx = GNUNET_FS_start (cfg,
212                          "gnunet-download",
213                          &progress_cb,
214                          NULL,
215                          GNUNET_FS_FLAGS_NONE,
216                          GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM,
217                          parallelism,
218                          GNUNET_FS_OPTIONS_REQUEST_PARALLELISM,
219                          request_parallelism,
220                          GNUNET_FS_OPTIONS_END);
221   if (NULL == ctx)
222     {
223       fprintf (stderr,
224                _("Could not initialize `%s' subsystem.\n"),
225                "FS");
226       GNUNET_FS_uri_destroy (uri);
227       ret = 1;
228       return;
229     }
230   options = GNUNET_FS_DOWNLOAD_OPTION_NONE;
231   if (do_recursive)
232     options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
233   if (local_only)
234     options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY;
235   dc = GNUNET_FS_download_start (ctx,
236                                  uri,
237                                  NULL,
238                                  filename, NULL,
239                                  0,
240                                  GNUNET_FS_uri_chk_get_file_size (uri),
241                                  anonymity,
242                                  options,
243                                  NULL,
244                                  NULL);
245   GNUNET_FS_uri_destroy (uri);
246   if (dc == NULL)
247     {
248       GNUNET_FS_stop (ctx);
249       ctx = NULL;
250       return;
251     }
252   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
253                                 &shutdown_task,
254                                 NULL);
255 }
256
257
258 /**
259  * The main function to download GNUnet.
260  *
261  * @param argc number of arguments from the command line
262  * @param argv command line arguments
263  * @return 0 ok, 1 on error
264  */
265 int
266 main (int argc, char *const *argv)
267 {
268   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
269     {'a', "anonymity", "LEVEL",
270      gettext_noop ("set the desired LEVEL of receiver-anonymity"),
271      1, &GNUNET_GETOPT_set_uint, &anonymity},
272     {'D', "delete-incomplete", NULL,
273      gettext_noop ("delete incomplete downloads (when aborted with CTRL-C)"),
274      0, &GNUNET_GETOPT_set_one, &delete_incomplete},
275     {'n', "no-network", NULL,
276      gettext_noop ("only search the local peer (no P2P network search)"),
277      1, &GNUNET_GETOPT_set_uint, &local_only},
278     {'o', "output", "FILENAME",
279      gettext_noop ("write the file to FILENAME"),
280      1, &GNUNET_GETOPT_set_string, &filename},
281     {'p', "parallelism", "DOWNLOADS",
282      gettext_noop
283      ("set the maximum number of parallel downloads that is allowed"),
284      1, &GNUNET_GETOPT_set_uint, &parallelism},
285     {'r', "request-parallelism", "REQUESTS",
286      gettext_noop
287      ("set the maximum number of parallel requests for blocks that is allowed"),
288      1, &GNUNET_GETOPT_set_uint, &request_parallelism},
289     {'R', "recursive", NULL,
290      gettext_noop ("download a GNUnet directory recursively"),
291      0, &GNUNET_GETOPT_set_one, &do_recursive},
292     {'V', "verbose", NULL,
293      gettext_noop ("be verbose (print progress information)"),
294      0, &GNUNET_GETOPT_increment_value, &verbose},
295     GNUNET_GETOPT_OPTION_END
296   };
297   return (GNUNET_OK ==
298           GNUNET_PROGRAM_run (argc,
299                               argv,
300                               "gnunet-download [OPTIONS] URI",
301                               gettext_noop
302                               ("Download files from GNUnet using a GNUnet CHK or LOC URI (gnunet://fs/chk/...)"),
303                               options, &run, NULL)) ? ret : 1;
304 }
305
306 /* end of gnunet-download.c */
307