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_FS_Handle *ctx;
37 static struct GNUNET_FS_SearchContext *sc;
39 static char *output_filename;
41 static struct GNUNET_FS_DirectoryBuilder *db;
43 static unsigned int anonymity = 1;
46 * Timeout for the search, 0 means to wait for CTRL-C.
48 static struct GNUNET_TIME_Relative timeout;
50 static unsigned int results_limit;
52 static unsigned int results;
56 static int local_only;
59 * Type of a function that libextractor calls for each
60 * meta data item found.
62 * @param cls closure (user-defined, unused)
63 * @param plugin_name name of the plugin that produced this value;
64 * special values can be used (i.e. '<zlib>' for zlib being
65 * used in the main libextractor library and yielding
67 * @param type libextractor-type describing the meta data
68 * @param format basic format information about data
69 * @param data_mime_type mime-type of data (not of the original file);
70 * can be NULL (if mime-type is not known)
71 * @param data actual meta-data found
72 * @param data_size number of bytes in data
73 * @return 0 to continue extracting, 1 to abort
76 item_printer (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
77 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
78 const char *data, size_t data_size)
80 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
81 (format != EXTRACTOR_METAFORMAT_C_STRING))
83 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
85 printf ("\t%20s: %s\n",
86 dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
87 EXTRACTOR_metatype_to_string (type)), data);
93 clean_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
100 if (output_filename == NULL)
102 if (GNUNET_OK != GNUNET_FS_directory_builder_finish (db, &dsize, &ddata))
105 GNUNET_free (output_filename);
109 GNUNET_DISK_fn_write (output_filename, ddata, dsize,
110 GNUNET_DISK_PERM_USER_READ |
111 GNUNET_DISK_PERM_USER_WRITE))
114 _("Failed to write directory with search results to `%s'\n"),
117 GNUNET_free_non_null (ddata);
118 GNUNET_free (output_filename);
123 * Called by FS client to give information about the progress of an
127 * @param info details about the event, specifying the event type
128 * and various bits about the event
129 * @return client-context (for the next progress call
130 * for this operation; should be set to NULL for
131 * SUSPEND and STOPPED events). The value returned
132 * will be passed to future callbacks in the respective
133 * field in the GNUNET_FS_ProgressInfo struct.
136 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
138 static unsigned int cnt;
143 switch (info->status)
145 case GNUNET_FS_STATUS_SEARCH_START:
147 case GNUNET_FS_STATUS_SEARCH_RESULT:
149 GNUNET_FS_directory_builder_add (db,
150 info->value.search.specifics.result.uri,
151 info->value.search.specifics.result.meta,
153 uri = GNUNET_FS_uri_to_string (info->value.search.specifics.result.uri);
154 printf ("#%u:\n", cnt++);
156 GNUNET_CONTAINER_meta_data_get_by_type (info->value.search.
157 specifics.result.meta,
158 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
160 GNUNET_FS_meta_data_test_for_directory (info->value.search.
161 specifics.result.meta);
162 if (filename != NULL)
164 GNUNET_DISK_filename_canonicalize (filename);
165 if (GNUNET_YES == is_directory)
166 printf ("gnunet-download -o \"%s%s\" -R %s\n", filename, GNUNET_FS_DIRECTORY_EXT, uri);
168 printf ("gnunet-download -o \"%s\" %s\n", filename, uri);
170 else if (GNUNET_YES == is_directory)
171 printf ("gnunet-download -o \"collection%s\" -R %s\n", GNUNET_FS_DIRECTORY_EXT, uri);
173 printf ("gnunet-download %s\n", uri);
175 GNUNET_CONTAINER_meta_data_iterate (info->value.search.specifics.
176 result.meta, &item_printer, NULL);
179 GNUNET_free_non_null (filename);
182 if ((results_limit > 0) && (results >= results_limit))
183 GNUNET_SCHEDULER_shutdown ();
185 case GNUNET_FS_STATUS_SEARCH_UPDATE:
187 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
190 case GNUNET_FS_STATUS_SEARCH_ERROR:
191 FPRINTF (stderr, _("Error searching: %s.\n"),
192 info->value.search.specifics.error.message);
193 GNUNET_SCHEDULER_shutdown ();
195 case GNUNET_FS_STATUS_SEARCH_STOPPED:
196 GNUNET_SCHEDULER_add_continuation (&clean_task, NULL,
197 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
200 FPRINTF (stderr, _("Unexpected status: %d\n"), info->status);
208 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
212 GNUNET_FS_search_stop (sc);
219 * Main function that will be run by the scheduler.
222 * @param args remaining command-line arguments
223 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
224 * @param c configuration
227 run (void *cls, char *const *args, const char *cfgfile,
228 const struct GNUNET_CONFIGURATION_Handle *c)
230 struct GNUNET_FS_Uri *uri;
232 enum GNUNET_FS_SearchOptions options;
235 while (NULL != args[argc])
237 uri = GNUNET_FS_uri_ksk_create_from_args (argc, (const char **) args);
240 FPRINTF (stderr, "%s", _("Could not create keyword URI from arguments.\n"));
246 GNUNET_FS_start (cfg, "gnunet-search", &progress_cb, NULL,
247 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
250 FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
251 GNUNET_FS_uri_destroy (uri);
255 if (output_filename != NULL)
256 db = GNUNET_FS_directory_builder_create (NULL);
257 options = GNUNET_FS_SEARCH_OPTION_NONE;
259 options |= GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY;
260 sc = GNUNET_FS_search_start (ctx, uri, anonymity, options, NULL);
261 GNUNET_FS_uri_destroy (uri);
264 FPRINTF (stderr, "%s", _("Could not start searching.\n"));
265 GNUNET_FS_stop (ctx);
269 if (0 != timeout.rel_value_us)
270 GNUNET_SCHEDULER_add_delayed (timeout, &shutdown_task, NULL);
272 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
278 * The main function to search GNUnet.
280 * @param argc number of arguments from the command line
281 * @param argv command line arguments
282 * @return 0 ok, 1 on error
285 main (int argc, char *const *argv)
287 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
288 {'a', "anonymity", "LEVEL",
289 gettext_noop ("set the desired LEVEL of receiver-anonymity"),
290 1, &GNUNET_GETOPT_set_uint, &anonymity},
291 {'n', "no-network", NULL,
292 gettext_noop ("only search the local peer (no P2P network search)"),
293 0, &GNUNET_GETOPT_set_one, &local_only},
294 {'o', "output", "PREFIX",
295 gettext_noop ("write search results to file starting with PREFIX"),
296 1, &GNUNET_GETOPT_set_string, &output_filename},
297 {'t', "timeout", "DELAY",
298 gettext_noop ("automatically terminate search after DELAY"),
299 1, &GNUNET_GETOPT_set_relative_time, &timeout},
300 {'V', "verbose", NULL,
301 gettext_noop ("be verbose (print progress information)"),
302 0, &GNUNET_GETOPT_set_one, &verbose},
303 {'N', "results", "VALUE",
305 ("automatically terminate search after VALUE results are found"),
306 1, &GNUNET_GETOPT_set_uint, &results_limit},
307 GNUNET_GETOPT_OPTION_END
310 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
314 GNUNET_PROGRAM_run (argc, argv, "gnunet-search [OPTIONS] KEYWORD",
316 ("Search GNUnet for files that were published on GNUnet"),
317 options, &run, NULL)) ? ret : 1;
318 GNUNET_free ((void*) argv);
322 /* end of gnunet-search.c */