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