-fix ftbfs if extractor.h present but libextractor.so missing
[oweals/gnunet.git] / src / fs / gnunet-search.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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_FS_Handle *ctx;
36
37 static struct GNUNET_FS_SearchContext *sc;
38
39 static char *output_filename;
40
41 static struct GNUNET_FS_DirectoryBuilder *db;
42
43 static unsigned int anonymity = 1;
44
45 /**
46  * Timeout for the search, 0 means to wait for CTRL-C.
47  */
48 static struct GNUNET_TIME_Relative timeout;
49
50 static unsigned int results_limit;
51
52 static unsigned int results;
53
54 static int verbose;
55
56 static int local_only;
57
58 static struct GNUNET_SCHEDULER_Task *tt;
59
60
61 /**
62  * Type of a function that libextractor calls for each
63  * meta data item found.
64  *
65  * @param cls closure (user-defined, unused)
66  * @param plugin_name name of the plugin that produced this value;
67  *        special values can be used (i.e. '<zlib>' for zlib being
68  *        used in the main libextractor library and yielding
69  *        meta data).
70  * @param type libextractor-type describing the meta data
71  * @param format basic format information about data
72  * @param data_mime_type mime-type of data (not of the original file);
73  *        can be NULL (if mime-type is not known)
74  * @param data actual meta-data found
75  * @param data_size number of bytes in @a data
76  * @return 0 to continue extracting, 1 to abort
77  */
78 static int
79 item_printer (void *cls,
80               const char *plugin_name,
81               enum EXTRACTOR_MetaType type,
82               enum EXTRACTOR_MetaFormat format,
83               const char *data_mime_type,
84               const char *data,
85               size_t data_size)
86 {
87   if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
88       (format != EXTRACTOR_METAFORMAT_C_STRING))
89     return 0;
90   if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
91     return 0;
92 #if HAVE_LIBEXTRACTOR
93   printf ("\t%20s: %s\n",
94           dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
95                     EXTRACTOR_metatype_to_string (type)), data);
96 #else
97   printf ("\t%20d: %s\n",
98           type,
99           data);
100 #endif
101   return 0;
102 }
103
104
105 static void
106 clean_task (void *cls)
107 {
108   size_t dsize;
109   void *ddata;
110
111   GNUNET_FS_stop (ctx);
112   ctx = NULL;
113   if (output_filename == NULL)
114     return;
115   if (GNUNET_OK != GNUNET_FS_directory_builder_finish (db, &dsize, &ddata))
116   {
117     GNUNET_break (0);
118     GNUNET_free (output_filename);
119     return;
120   }
121   if (dsize !=
122       GNUNET_DISK_fn_write (output_filename, ddata, dsize,
123                             GNUNET_DISK_PERM_USER_READ |
124                             GNUNET_DISK_PERM_USER_WRITE))
125   {
126     FPRINTF (stderr,
127              _("Failed to write directory with search results to `%s'\n"),
128              output_filename);
129   }
130   GNUNET_free_non_null (ddata);
131   GNUNET_free (output_filename);
132 }
133
134
135 /**
136  * Called by FS client to give information about the progress of an
137  * operation.
138  *
139  * @param cls closure
140  * @param info details about the event, specifying the event type
141  *        and various bits about the event
142  * @return client-context (for the next progress call
143  *         for this operation; should be set to NULL for
144  *         SUSPEND and STOPPED events).  The value returned
145  *         will be passed to future callbacks in the respective
146  *         field in the GNUNET_FS_ProgressInfo struct.
147  */
148 static void *
149 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
150 {
151   static unsigned int cnt;
152   int is_directory;
153   char *uri;
154   char *filename;
155
156   switch (info->status)
157   {
158   case GNUNET_FS_STATUS_SEARCH_START:
159     break;
160   case GNUNET_FS_STATUS_SEARCH_RESULT:
161     if (db != NULL)
162       GNUNET_FS_directory_builder_add (db,
163                                        info->value.search.specifics.result.uri,
164                                        info->value.search.specifics.result.meta,
165                                        NULL);
166     uri = GNUNET_FS_uri_to_string (info->value.search.specifics.result.uri);
167     printf ("#%u:\n", cnt++);
168     filename =
169         GNUNET_CONTAINER_meta_data_get_by_type (info->value.search.
170                                                 specifics.result.meta,
171                                                 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
172     is_directory =
173         GNUNET_FS_meta_data_test_for_directory (info->value.search.
174                                                 specifics.result.meta);
175     if (filename != NULL)
176     {
177       GNUNET_DISK_filename_canonicalize (filename);
178       if (GNUNET_YES == is_directory)
179         printf ("gnunet-download -o \"%s%s\" -R %s\n", filename, GNUNET_FS_DIRECTORY_EXT, uri);
180       else
181         printf ("gnunet-download -o \"%s\" %s\n", filename, uri);
182     }
183     else if (GNUNET_YES == is_directory)
184       printf ("gnunet-download -o \"collection%s\" -R %s\n", GNUNET_FS_DIRECTORY_EXT, uri);
185     else
186       printf ("gnunet-download %s\n", uri);
187     if (verbose)
188       GNUNET_CONTAINER_meta_data_iterate (info->value.search.specifics.
189                                           result.meta, &item_printer, NULL);
190     printf ("\n");
191     fflush (stdout);
192     GNUNET_free_non_null (filename);
193     GNUNET_free (uri);
194     results++;
195     if ((results_limit > 0) && (results >= results_limit))
196       GNUNET_SCHEDULER_shutdown ();
197     break;
198   case GNUNET_FS_STATUS_SEARCH_UPDATE:
199     break;
200   case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
201     /* ignore */
202     break;
203   case GNUNET_FS_STATUS_SEARCH_ERROR:
204     FPRINTF (stderr, _("Error searching: %s.\n"),
205              info->value.search.specifics.error.message);
206     GNUNET_SCHEDULER_shutdown ();
207     break;
208   case GNUNET_FS_STATUS_SEARCH_STOPPED:
209     GNUNET_SCHEDULER_add_now (&clean_task, NULL);
210     break;
211   default:
212     FPRINTF (stderr, _("Unexpected status: %d\n"), info->status);
213     break;
214   }
215   return NULL;
216 }
217
218
219 static void
220 shutdown_task (void *cls)
221 {
222   if (sc != NULL)
223   {
224     GNUNET_FS_search_stop (sc);
225     sc = NULL;
226   }
227 }
228
229
230 static void
231 timeout_task (void *cls)
232 {
233   tt = NULL;
234   GNUNET_SCHEDULER_shutdown ();
235 }
236
237
238 /**
239  * Main function that will be run by the scheduler.
240  *
241  * @param cls closure
242  * @param args remaining command-line arguments
243  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
244  * @param c configuration
245  */
246 static void
247 run (void *cls, char *const *args, const char *cfgfile,
248      const struct GNUNET_CONFIGURATION_Handle *c)
249 {
250   struct GNUNET_FS_Uri *uri;
251   unsigned int argc;
252   enum GNUNET_FS_SearchOptions options;
253
254   argc = 0;
255   while (NULL != args[argc])
256     argc++;
257   uri = GNUNET_FS_uri_ksk_create_from_args (argc, (const char **) args);
258   if (NULL == uri)
259   {
260     FPRINTF (stderr, "%s",  _("Could not create keyword URI from arguments.\n"));
261     ret = 1;
262     return;
263   }
264   cfg = c;
265   ctx =
266       GNUNET_FS_start (cfg, "gnunet-search", &progress_cb, NULL,
267                        GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
268   if (NULL == ctx)
269   {
270     FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
271     GNUNET_FS_uri_destroy (uri);
272     ret = 1;
273     return;
274   }
275   if (output_filename != NULL)
276     db = GNUNET_FS_directory_builder_create (NULL);
277   options = GNUNET_FS_SEARCH_OPTION_NONE;
278   if (local_only)
279     options |= GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY;
280   sc = GNUNET_FS_search_start (ctx, uri, anonymity, options, NULL);
281   GNUNET_FS_uri_destroy (uri);
282   if (NULL == sc)
283   {
284     FPRINTF (stderr, "%s",  _("Could not start searching.\n"));
285     GNUNET_FS_stop (ctx);
286     ret = 1;
287     return;
288   }
289   if (0 != timeout.rel_value_us)
290     tt = GNUNET_SCHEDULER_add_delayed (timeout,
291                                        &timeout_task,
292                                        NULL);
293   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
294                                  NULL);
295 }
296
297
298 /**
299  * The main function to search GNUnet.
300  *
301  * @param argc number of arguments from the command line
302  * @param argv command line arguments
303  * @return 0 ok, 1 on error
304  */
305 int
306 main (int argc, char *const *argv)
307 {
308   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
309     {'a', "anonymity", "LEVEL",
310      gettext_noop ("set the desired LEVEL of receiver-anonymity"),
311      1, &GNUNET_GETOPT_set_uint, &anonymity},
312     {'n', "no-network", NULL,
313      gettext_noop ("only search the local peer (no P2P network search)"),
314      0, &GNUNET_GETOPT_set_one, &local_only},
315     {'o', "output", "PREFIX",
316      gettext_noop ("write search results to file starting with PREFIX"),
317      1, &GNUNET_GETOPT_set_string, &output_filename},
318     {'t', "timeout", "DELAY",
319      gettext_noop ("automatically terminate search after DELAY"),
320      1, &GNUNET_GETOPT_set_relative_time, &timeout},
321     {'V', "verbose", NULL,
322      gettext_noop ("be verbose (print progress information)"),
323      0, &GNUNET_GETOPT_set_one, &verbose},
324     {'N', "results", "VALUE",
325      gettext_noop
326      ("automatically terminate search after VALUE results are found"),
327      1, &GNUNET_GETOPT_set_uint, &results_limit},
328     GNUNET_GETOPT_OPTION_END
329   };
330
331   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
332     return 2;
333
334   ret = (GNUNET_OK ==
335          GNUNET_PROGRAM_run (argc, argv, "gnunet-search [OPTIONS] KEYWORD",
336                              gettext_noop
337                              ("Search GNUnet for files that were published on GNUnet"),
338                              options, &run, NULL)) ? ret : 1;
339   GNUNET_free ((void*) argv);
340   return ret;
341 }
342
343 /* end of gnunet-search.c */