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