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