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