33c81c9d751787023b584415f2199b1028d5c7a6
[oweals/gnunet.git] / src / fs / gnunet-search.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-search.c
22  * @brief searching 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 const struct GNUNET_CONFIGURATION_Handle *cfg;
34
35 static struct GNUNET_SCHEDULER_Handle *sched;
36
37 static struct GNUNET_FS_Handle *ctx;
38
39 static struct GNUNET_FS_SearchContext *sc;
40
41 static char *output_filename;
42
43 static struct GNUNET_FS_DirectoryBuilder *db;
44
45 static unsigned int anonymity = 1;
46
47 static int verbose;
48
49 static int
50 item_printer (void *cls,
51               const char *plugin_name,
52               enum EXTRACTOR_MetaType type, 
53               enum EXTRACTOR_MetaFormat format,
54               const char *data_mime_type,
55               const char *data,
56               size_t data_size)
57 {
58   if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
59        (format != EXTRACTOR_METAFORMAT_C_STRING) )
60     return 0;
61   printf ("\t%20s: %s\n",
62           dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
63                     EXTRACTOR_metatype_to_string (type)),
64           data);
65   return GNUNET_OK;
66 }
67
68
69 static void
70 clean_task (void *cls,
71             const struct GNUNET_SCHEDULER_TaskContext *tc)
72 {
73   size_t dsize;
74   void *ddata;
75
76   GNUNET_FS_stop (ctx);
77   ctx = NULL;
78   if (output_filename == NULL)
79     return;
80   if (GNUNET_OK !=
81       GNUNET_FS_directory_builder_finish (db,
82                                           &dsize,       
83                                           &ddata))
84     {
85       GNUNET_break (0);
86       GNUNET_free (output_filename);    
87       return;
88     }
89   if (dsize != 
90       GNUNET_DISK_fn_write (output_filename,
91                             ddata,
92                             dsize,
93                             GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE))
94     {
95       fprintf (stderr,
96                _("Failed to write directory with search results to `%s'\n"),
97                output_filename);
98     }
99   GNUNET_free_non_null (ddata);
100   GNUNET_free (output_filename);
101 }
102
103
104 /**
105  * Called by FS client to give information about the progress of an 
106  * operation.
107  *
108  * @param cls closure
109  * @param info details about the event, specifying the event type
110  *        and various bits about the event
111  * @return client-context (for the next progress call
112  *         for this operation; should be set to NULL for
113  *         SUSPEND and STOPPED events).  The value returned
114  *         will be passed to future callbacks in the respective
115  *         field in the GNUNET_FS_ProgressInfo struct.
116  */
117 static void *
118 progress_cb (void *cls,
119              const struct GNUNET_FS_ProgressInfo *info)
120 {
121   char *uri;
122   char *dotdot;
123   char *filename;
124
125   switch (info->status)
126     {
127     case GNUNET_FS_STATUS_SEARCH_START:
128       break;
129     case GNUNET_FS_STATUS_SEARCH_RESULT:
130       if (db != NULL)
131         GNUNET_FS_directory_builder_add (db,
132                                          info->value.search.specifics.result.uri,
133                                          info->value.search.specifics.result.meta,
134                                          NULL);
135       uri = GNUNET_FS_uri_to_string (info->value.search.specifics.result.uri);
136       printf ("%s:\n", uri);
137       filename =
138         GNUNET_CONTAINER_meta_data_get_by_type (info->value.search.specifics.result.meta,
139                                                 EXTRACTOR_METATYPE_FILENAME);
140       if (filename != NULL)
141         {
142           while (NULL != (dotdot = strstr (filename, "..")))
143             dotdot[0] = dotdot[1] = '_';
144           printf ("gnunet-download -o \"%s\" %s\n", 
145                   filename, 
146                   uri);
147         }
148       else
149         printf ("gnunet-download %s\n", uri);
150       if (verbose)
151         GNUNET_CONTAINER_meta_data_iterate (info->value.search.specifics.result.meta, 
152                                             &item_printer,
153                                             NULL);
154       printf ("\n");
155       fflush(stdout);
156       GNUNET_free_non_null (filename);
157       GNUNET_free (uri);
158       break;
159     case GNUNET_FS_STATUS_SEARCH_UPDATE:
160       break;
161     case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
162       /* ignore */
163       break;
164     case GNUNET_FS_STATUS_SEARCH_ERROR:
165       fprintf (stderr,
166                _("Error searching: %s.\n"),
167                info->value.search.specifics.error.message);
168       GNUNET_SCHEDULER_shutdown (sched);
169       break;
170     case GNUNET_FS_STATUS_SEARCH_STOPPED: 
171       GNUNET_SCHEDULER_add_continuation (sched,
172                                          &clean_task, 
173                                          NULL,
174                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
175       break;      
176     default:
177       fprintf (stderr,
178                _("Unexpected status: %d\n"),
179                info->status);
180       break;
181     }
182   return NULL;
183 }
184
185
186 static void
187 shutdown_task (void *cls,
188                const struct GNUNET_SCHEDULER_TaskContext *tc)
189 {
190   if (sc != NULL)
191     {
192       GNUNET_FS_search_stop (sc); 
193       sc = NULL;
194     }
195 }
196
197
198 /**
199  * Main function that will be run by the scheduler.
200  *
201  * @param cls closure
202  * @param s the scheduler to use
203  * @param args remaining command-line arguments
204  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
205  * @param c configuration
206  */
207 static void
208 run (void *cls,
209      struct GNUNET_SCHEDULER_Handle *s,
210      char *const *args,
211      const char *cfgfile,
212      const struct GNUNET_CONFIGURATION_Handle *c)
213 {
214   struct GNUNET_FS_Uri *uri;
215   unsigned int argc;
216
217   sched = s;
218   argc = 0;
219   while (NULL != args[argc])
220     argc++;
221   uri = GNUNET_FS_uri_ksk_create_from_args (argc,
222                                             (const char **) args);
223   if (NULL == uri)
224     {
225       fprintf (stderr,
226                _("Could not create keyword URI from arguments.\n"));
227       ret = 1;
228       GNUNET_FS_uri_destroy (uri);
229       return;
230     }
231   cfg = c;
232   ctx = GNUNET_FS_start (sched,
233                          cfg,
234                          "gnunet-search",
235                          &progress_cb,
236                          NULL,
237                          GNUNET_FS_FLAGS_NONE,
238                          GNUNET_FS_OPTIONS_END);
239   if (NULL == ctx)
240     {
241       fprintf (stderr,
242                _("Could not initialize `%s' subsystem.\n"),
243                "FS");
244       GNUNET_FS_uri_destroy (uri);
245       GNUNET_FS_stop (ctx);
246       ret = 1;
247       return;
248     }
249   if (output_filename != NULL)
250     db = GNUNET_FS_directory_builder_create (NULL);
251   sc = GNUNET_FS_search_start (ctx,
252                                uri,
253                                anonymity,
254                                NULL);
255   GNUNET_FS_uri_destroy (uri);
256   if (NULL == sc)
257     {
258       fprintf (stderr,
259                _("Could not start searching.\n"));
260       GNUNET_FS_stop (ctx);
261       ret = 1;
262       return;
263     }
264   GNUNET_SCHEDULER_add_delayed (sched,
265                                 GNUNET_TIME_UNIT_FOREVER_REL,
266                                 &shutdown_task,
267                                 NULL);
268 }
269
270
271 /**
272  * gnunet-search command line options
273  */
274 static struct GNUNET_GETOPT_CommandLineOption options[] = {
275   {'a', "anonymity", "LEVEL",
276    gettext_noop ("set the desired LEVEL of receiver-anonymity"),
277    1, &GNUNET_GETOPT_set_uint, &anonymity},
278   {'o', "output", "PREFIX",
279    gettext_noop
280    ("write search results to file starting with PREFIX"),
281    1, &GNUNET_GETOPT_set_string, &output_filename},
282   GNUNET_GETOPT_OPTION_END
283 };
284
285
286 /**
287  * The main function to search GNUnet.
288  *
289  * @param argc number of arguments from the command line
290  * @param argv command line arguments
291  * @return 0 ok, 1 on error
292  */
293 int
294 main (int argc, char *const *argv)
295 {
296   return (GNUNET_OK ==
297           GNUNET_PROGRAM_run (argc,
298                               argv,
299                               "gnunet-search",
300                               gettext_noop
301                               ("Search GNUnet."),
302                               options, &run, NULL)) ? ret : 1;
303 }
304
305 /* end of gnunet-search.c */
306