2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
29 #include "gnunet_fs_service.h"
33 static const struct GNUNET_CONFIGURATION_Handle *cfg;
35 static struct GNUNET_SCHEDULER_Handle *sched;
37 static struct GNUNET_FS_Handle *ctx;
39 static struct GNUNET_FS_SearchContext *sc;
41 static char *output_filename;
43 static struct GNUNET_FS_DirectoryBuilder *db;
45 static unsigned int anonymity = 1;
49 static int local_only;
52 * Type of a function that libextractor calls for each
53 * meta data item found.
55 * @param cls closure (user-defined, unused)
56 * @param plugin_name name of the plugin that produced this value;
57 * special values can be used (i.e. '<zlib>' for zlib being
58 * used in the main libextractor library and yielding
60 * @param type libextractor-type describing the meta data
61 * @param format basic format information about data
62 * @param data_mime_type mime-type of data (not of the original file);
63 * can be NULL (if mime-type is not known)
64 * @param data actual meta-data found
65 * @param data_size number of bytes in data
66 * @return 0 to continue extracting, 1 to abort
69 item_printer (void *cls,
70 const char *plugin_name,
71 enum EXTRACTOR_MetaType type,
72 enum EXTRACTOR_MetaFormat format,
73 const char *data_mime_type,
77 if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
78 (format != EXTRACTOR_METAFORMAT_C_STRING) )
80 printf ("\t%20s: %s\n",
81 dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
82 EXTRACTOR_metatype_to_string (type)),
89 clean_task (void *cls,
90 const struct GNUNET_SCHEDULER_TaskContext *tc)
97 if (output_filename == NULL)
100 GNUNET_FS_directory_builder_finish (db,
105 GNUNET_free (output_filename);
109 GNUNET_DISK_fn_write (output_filename,
112 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE))
115 _("Failed to write directory with search results to `%s'\n"),
118 GNUNET_free_non_null (ddata);
119 GNUNET_free (output_filename);
124 * Called by FS client to give information about the progress of an
128 * @param info details about the event, specifying the event type
129 * and various bits about the event
130 * @return client-context (for the next progress call
131 * for this operation; should be set to NULL for
132 * SUSPEND and STOPPED events). The value returned
133 * will be passed to future callbacks in the respective
134 * field in the GNUNET_FS_ProgressInfo struct.
137 progress_cb (void *cls,
138 const struct GNUNET_FS_ProgressInfo *info)
140 static unsigned int cnt;
145 switch (info->status)
147 case GNUNET_FS_STATUS_SEARCH_START:
149 case GNUNET_FS_STATUS_SEARCH_RESULT:
151 GNUNET_FS_directory_builder_add (db,
152 info->value.search.specifics.result.uri,
153 info->value.search.specifics.result.meta,
155 uri = GNUNET_FS_uri_to_string (info->value.search.specifics.result.uri);
156 printf ("#%u:\n", cnt++);
158 GNUNET_CONTAINER_meta_data_get_by_type (info->value.search.specifics.result.meta,
159 EXTRACTOR_METATYPE_FILENAME);
160 if (filename != NULL)
162 while (NULL != (dotdot = strstr (filename, "..")))
163 dotdot[0] = dotdot[1] = '_';
164 printf ("gnunet-download -o \"%s\" %s\n",
169 printf ("gnunet-download %s\n", uri);
171 GNUNET_CONTAINER_meta_data_iterate (info->value.search.specifics.result.meta,
176 GNUNET_free_non_null (filename);
179 case GNUNET_FS_STATUS_SEARCH_UPDATE:
181 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
184 case GNUNET_FS_STATUS_SEARCH_ERROR:
186 _("Error searching: %s.\n"),
187 info->value.search.specifics.error.message);
188 GNUNET_SCHEDULER_shutdown (sched);
190 case GNUNET_FS_STATUS_SEARCH_STOPPED:
191 GNUNET_SCHEDULER_add_continuation (sched,
194 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
198 _("Unexpected status: %d\n"),
207 shutdown_task (void *cls,
208 const struct GNUNET_SCHEDULER_TaskContext *tc)
212 GNUNET_FS_search_stop (sc);
219 * Main function that will be run by the scheduler.
222 * @param s the scheduler to use
223 * @param args remaining command-line arguments
224 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
225 * @param c configuration
229 struct GNUNET_SCHEDULER_Handle *s,
232 const struct GNUNET_CONFIGURATION_Handle *c)
234 struct GNUNET_FS_Uri *uri;
236 enum GNUNET_FS_SearchOptions options;
240 while (NULL != args[argc])
242 uri = GNUNET_FS_uri_ksk_create_from_args (argc,
243 (const char **) args);
247 _("Could not create keyword URI from arguments.\n"));
249 GNUNET_FS_uri_destroy (uri);
253 ctx = GNUNET_FS_start (sched,
258 GNUNET_FS_FLAGS_NONE,
259 GNUNET_FS_OPTIONS_END);
263 _("Could not initialize `%s' subsystem.\n"),
265 GNUNET_FS_uri_destroy (uri);
269 if (output_filename != NULL)
270 db = GNUNET_FS_directory_builder_create (NULL);
271 options = GNUNET_FS_SEARCH_OPTION_NONE;
273 options |= GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY;
274 sc = GNUNET_FS_search_start (ctx,
279 GNUNET_FS_uri_destroy (uri);
283 _("Could not start searching.\n"));
284 GNUNET_FS_stop (ctx);
288 GNUNET_SCHEDULER_add_delayed (sched,
289 GNUNET_TIME_UNIT_FOREVER_REL,
296 * The main function to search GNUnet.
298 * @param argc number of arguments from the command line
299 * @param argv command line arguments
300 * @return 0 ok, 1 on error
303 main (int argc, char *const *argv)
305 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
306 {'a', "anonymity", "LEVEL",
307 gettext_noop ("set the desired LEVEL of receiver-anonymity"),
308 1, &GNUNET_GETOPT_set_uint, &anonymity},
309 {'n', "no-network", NULL,
310 gettext_noop ("only search the local peer (no P2P network search)"),
311 1, &GNUNET_GETOPT_set_uint, &local_only},
312 {'o', "output", "PREFIX",
314 ("write search results to file starting with PREFIX"),
315 1, &GNUNET_GETOPT_set_string, &output_filename},
316 {'V', "verbose", NULL,
317 gettext_noop ("be verbose (print progress information)"),
318 0, &GNUNET_GETOPT_set_one, &verbose},
319 GNUNET_GETOPT_OPTION_END
322 GNUNET_PROGRAM_run (argc,
327 options, &run, NULL)) ? ret : 1;
330 /* end of gnunet-search.c */