added option to restrict search to local-only
[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 2, 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_SCHEDULER_Handle *sched;
42
43 static struct GNUNET_FS_DownloadContext *dc;
44
45 static unsigned int anonymity = 1;
46
47 static unsigned int parallelism = 16;
48
49 static unsigned int request_parallelism = 4092;
50
51 static int do_recursive;
52
53 static char *filename;
54
55 static int local_only;
56
57 static void
58 cleanup_task (void *cls,
59               const struct GNUNET_SCHEDULER_TaskContext *tc)
60 {
61   GNUNET_FS_stop (ctx);
62   ctx = NULL;
63 }
64
65
66 static void
67 shutdown_task (void *cls,
68               const struct GNUNET_SCHEDULER_TaskContext *tc)
69 {
70   struct GNUNET_FS_DownloadContext *d;
71
72   if (dc != NULL)
73     {
74       d = dc;
75       dc = NULL;
76       GNUNET_FS_download_stop (d, delete_incomplete);
77     }
78 }
79
80
81 /**
82  * Called by FS client to give information about the progress of an 
83  * operation.
84  *
85  * @param cls closure
86  * @param info details about the event, specifying the event type
87  *        and various bits about the event
88  * @return client-context (for the next progress call
89  *         for this operation; should be set to NULL for
90  *         SUSPEND and STOPPED events).  The value returned
91  *         will be passed to future callbacks in the respective
92  *         field in the GNUNET_FS_ProgressInfo struct.
93  */
94 static void *
95 progress_cb (void *cls,
96              const struct GNUNET_FS_ProgressInfo *info)
97 {
98   char *s;
99   char *t;
100
101   switch (info->status)
102     {
103     case GNUNET_FS_STATUS_DOWNLOAD_START:
104       break;
105     case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
106       if (verbose)
107         {         
108           s = GNUNET_STRINGS_relative_time_to_string(info->value.download.eta);
109           t = GNUNET_STRINGS_byte_size_fancy(info->value.download.completed * 1000LL / (info->value.download.duration.value + 1));
110           fprintf (stdout,
111                    _("Downloading `%s' at %llu/%llu (%s remaining, %s/s)\n"),
112                    info->value.download.filename,
113                    (unsigned long long) info->value.download.completed,
114                    (unsigned long long) info->value.download.size,
115                    s,
116                    t);
117           GNUNET_free (s);
118           GNUNET_free (t);
119         }
120       break;
121     case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
122       fprintf (stderr,
123                _("Error downloading: %s.\n"),
124                info->value.download.specifics.error.message);
125       GNUNET_SCHEDULER_shutdown (sched);
126       break;
127     case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
128       s = GNUNET_STRINGS_byte_size_fancy(info->value.download.completed * 1000 / (info->value.download.duration.value + 1));
129       fprintf (stdout,
130                _("Downloading `%s' done (%s/s).\n"),
131                info->value.download.filename,
132                s);
133       GNUNET_free (s);
134       if (info->value.download.dc == dc)
135         GNUNET_SCHEDULER_shutdown (sched);
136       break;
137     case GNUNET_FS_STATUS_DOWNLOAD_STOPPED: 
138       if (info->value.download.dc == dc)
139         GNUNET_SCHEDULER_add_continuation (sched,
140                                            &cleanup_task,
141                                            NULL,
142                                            GNUNET_SCHEDULER_REASON_PREREQ_DONE);
143       break;      
144     case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
145     case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
146       break;
147     default:
148       fprintf (stderr,
149                _("Unexpected status: %d\n"),
150                info->status);
151       break;
152     }
153   return NULL;
154 }
155
156
157 /**
158  * Main function that will be run by the scheduler.
159  *
160  * @param cls closure
161  * @param s the scheduler to use
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      struct GNUNET_SCHEDULER_Handle *s,
169      char *const *args,
170      const char *cfgfile,
171      const struct GNUNET_CONFIGURATION_Handle *c)
172 {
173   struct GNUNET_FS_Uri *uri;
174   char *emsg;
175   enum GNUNET_FS_DownloadOptions options;
176
177   sched = s;
178   uri = GNUNET_FS_uri_parse (args[0],
179                              &emsg);
180   if (NULL == uri)
181     {
182       fprintf (stderr,
183                _("Failed to parse URI: %s\n"),
184                emsg);
185       GNUNET_free (emsg);
186       ret = 1;
187       return;
188     }
189   if (! GNUNET_FS_uri_test_chk (uri))
190     {
191       fprintf (stderr,
192                "Only CHK URIs supported right now.\n");
193       ret = 1;
194       GNUNET_FS_uri_destroy (uri);
195       return;            
196     }
197   if (NULL == filename)
198     {
199       fprintf (stderr,
200                "Target filename must be specified.\n");
201       ret = 1;
202       GNUNET_FS_uri_destroy (uri);
203       return;            
204     }
205   cfg = c;
206   ctx = GNUNET_FS_start (sched,
207                          cfg,
208                          "gnunet-download",
209                          &progress_cb,
210                          NULL,
211                          GNUNET_FS_FLAGS_NONE,
212                          GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM,
213                          parallelism,
214                          GNUNET_FS_OPTIONS_REQUEST_PARALLELISM,
215                          request_parallelism,
216                          GNUNET_FS_OPTIONS_END);
217   if (NULL == ctx)
218     {
219       fprintf (stderr,
220                _("Could not initialize `%s' subsystem.\n"),
221                "FS");
222       GNUNET_FS_uri_destroy (uri);
223       ret = 1;
224       return;
225     }
226   options = GNUNET_FS_DOWNLOAD_OPTION_NONE;
227   if (do_recursive)
228     options |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
229   if (local_only)
230     options |= GNUNET_FS_DOWNLOAD_OPTION_LOOPBACK_ONLY;
231   dc = GNUNET_FS_download_start (ctx,
232                                  uri,
233                                  NULL,
234                                  filename, NULL,
235                                  0,
236                                  GNUNET_FS_uri_chk_get_file_size (uri),
237                                  anonymity,
238                                  options,
239                                  NULL,
240                                  NULL);
241   GNUNET_FS_uri_destroy (uri);
242   if (dc == NULL)
243     {
244       GNUNET_FS_stop (ctx);
245       ctx = NULL;
246       return;
247     }
248   GNUNET_SCHEDULER_add_delayed (sched,
249                                 GNUNET_TIME_UNIT_FOREVER_REL,
250                                 &shutdown_task,
251                                 NULL);
252 }
253
254
255 /**
256  * gnunet-download command line options
257  */
258 static 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
288
289 /**
290  * The main function to download GNUnet.
291  *
292  * @param argc number of arguments from the command line
293  * @param argv command line arguments
294  * @return 0 ok, 1 on error
295  */
296 int
297 main (int argc, char *const *argv)
298 {
299   return (GNUNET_OK ==
300           GNUNET_PROGRAM_run (argc,
301                               argv,
302                               "gnunet-download",
303                               gettext_noop
304                               ("Download files from GNUnet."),
305                               options, &run, NULL)) ? ret : 1;
306 }
307
308 /* end of gnunet-download.c */
309