2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
16 * @file fs/gnunet-search.c
17 * @brief searching for files on GNUnet
18 * @author Christian Grothoff
19 * @author Krista Bennett
20 * @author James Blackwell
21 * @author Igor Wronsky
24 #include "gnunet_fs_service.h"
28 static const struct GNUNET_CONFIGURATION_Handle *cfg;
30 static struct GNUNET_FS_Handle *ctx;
32 static struct GNUNET_FS_SearchContext *sc;
34 static char *output_filename;
36 static struct GNUNET_FS_DirectoryBuilder *db;
38 static unsigned int anonymity = 1;
41 * Timeout for the search, 0 means to wait for CTRL-C.
43 static struct GNUNET_TIME_Relative timeout;
45 static unsigned int results_limit;
47 static unsigned int results;
49 static unsigned int verbose;
51 static int local_only;
53 static struct GNUNET_SCHEDULER_Task *tt;
57 * Type of a function that libextractor calls for each
58 * meta data item found.
60 * @param cls closure (user-defined, unused)
61 * @param plugin_name name of the plugin that produced this value;
62 * special values can be used (i.e. '<zlib>' for zlib being
63 * used in the main libextractor library and yielding
65 * @param type libextractor-type describing the meta data
66 * @param format basic format information about data
67 * @param data_mime_type mime-type of data (not of the original file);
68 * can be NULL (if mime-type is not known)
69 * @param data actual meta-data found
70 * @param data_size number of bytes in @a data
71 * @return 0 to continue extracting, 1 to abort
74 item_printer (void *cls,
75 const char *plugin_name,
76 enum EXTRACTOR_MetaType type,
77 enum EXTRACTOR_MetaFormat format,
78 const char *data_mime_type,
82 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
83 (format != EXTRACTOR_METAFORMAT_C_STRING))
85 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
88 printf ("\t%20s: %s\n",
89 dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
90 EXTRACTOR_metatype_to_string (type)), data);
92 printf ("\t%20d: %s\n",
101 clean_task (void *cls)
106 GNUNET_FS_stop (ctx);
108 if (output_filename == NULL)
110 if (GNUNET_OK != GNUNET_FS_directory_builder_finish (db, &dsize, &ddata))
113 GNUNET_free (output_filename);
117 GNUNET_DISK_fn_write (output_filename, ddata, dsize,
118 GNUNET_DISK_PERM_USER_READ |
119 GNUNET_DISK_PERM_USER_WRITE))
122 _("Failed to write directory with search results to `%s'\n"),
125 GNUNET_free_non_null (ddata);
126 GNUNET_free (output_filename);
131 * Called by FS client to give information about the progress of an
135 * @param info details about the event, specifying the event type
136 * and various bits about the event
137 * @return client-context (for the next progress call
138 * for this operation; should be set to NULL for
139 * SUSPEND and STOPPED events). The value returned
140 * will be passed to future callbacks in the respective
141 * field in the GNUNET_FS_ProgressInfo struct.
144 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
146 static unsigned int cnt;
151 switch (info->status)
153 case GNUNET_FS_STATUS_SEARCH_START:
155 case GNUNET_FS_STATUS_SEARCH_RESULT:
157 GNUNET_FS_directory_builder_add (db,
158 info->value.search.specifics.result.uri,
159 info->value.search.specifics.result.meta,
161 uri = GNUNET_FS_uri_to_string (info->value.search.specifics.result.uri);
162 printf ("#%u:\n", ++cnt);
164 GNUNET_CONTAINER_meta_data_get_by_type (info->value.search.
165 specifics.result.meta,
166 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
168 GNUNET_FS_meta_data_test_for_directory (info->value.search.
169 specifics.result.meta);
170 if (filename != NULL)
172 GNUNET_DISK_filename_canonicalize (filename);
173 if (GNUNET_YES == is_directory)
174 printf ("gnunet-download -o \"%s%s\" -R %s\n", filename, GNUNET_FS_DIRECTORY_EXT, uri);
176 printf ("gnunet-download -o \"%s\" %s\n", filename, uri);
178 else if (GNUNET_YES == is_directory)
179 printf ("gnunet-download -o \"collection%s\" -R %s\n", GNUNET_FS_DIRECTORY_EXT, uri);
181 printf ("gnunet-download %s\n", uri);
183 GNUNET_CONTAINER_meta_data_iterate (info->value.search.specifics.
184 result.meta, &item_printer, NULL);
187 GNUNET_free_non_null (filename);
190 if ((results_limit > 0) && (results >= results_limit))
191 GNUNET_SCHEDULER_shutdown ();
193 case GNUNET_FS_STATUS_SEARCH_UPDATE:
195 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
198 case GNUNET_FS_STATUS_SEARCH_ERROR:
199 FPRINTF (stderr, _("Error searching: %s.\n"),
200 info->value.search.specifics.error.message);
201 GNUNET_SCHEDULER_shutdown ();
203 case GNUNET_FS_STATUS_SEARCH_STOPPED:
204 GNUNET_SCHEDULER_add_now (&clean_task, NULL);
207 FPRINTF (stderr, _("Unexpected status: %d\n"), info->status);
215 shutdown_task (void *cls)
219 GNUNET_FS_search_stop (sc);
226 timeout_task (void *cls)
229 GNUNET_SCHEDULER_shutdown ();
234 * Main function that will be run by the scheduler.
237 * @param args remaining command-line arguments
238 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
239 * @param c configuration
242 run (void *cls, char *const *args, const char *cfgfile,
243 const struct GNUNET_CONFIGURATION_Handle *c)
245 struct GNUNET_FS_Uri *uri;
247 enum GNUNET_FS_SearchOptions options;
250 while (NULL != args[argc])
252 uri = GNUNET_FS_uri_ksk_create_from_args (argc, (const char **) args);
255 FPRINTF (stderr, "%s", _("Could not create keyword URI from arguments.\n"));
261 GNUNET_FS_start (cfg, "gnunet-search", &progress_cb, NULL,
262 GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
265 FPRINTF (stderr, _("Could not initialize `%s' subsystem.\n"), "FS");
266 GNUNET_FS_uri_destroy (uri);
270 if (output_filename != NULL)
271 db = GNUNET_FS_directory_builder_create (NULL);
272 options = GNUNET_FS_SEARCH_OPTION_NONE;
274 options |= GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY;
275 sc = GNUNET_FS_search_start (ctx, uri, anonymity, options, NULL);
276 GNUNET_FS_uri_destroy (uri);
279 FPRINTF (stderr, "%s", _("Could not start searching.\n"));
280 GNUNET_FS_stop (ctx);
284 if (0 != timeout.rel_value_us)
285 tt = GNUNET_SCHEDULER_add_delayed (timeout,
288 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
294 * The main function to search GNUnet.
296 * @param argc number of arguments from the command line
297 * @param argv command line arguments
298 * @return 0 ok, 1 on error
301 main (int argc, char *const *argv)
303 struct GNUNET_GETOPT_CommandLineOption options[] = {
305 GNUNET_GETOPT_option_uint ('a',
308 gettext_noop ("set the desired LEVEL of receiver-anonymity"),
312 GNUNET_GETOPT_option_flag ('n',
314 gettext_noop ("only search the local peer (no P2P network search)"),
317 GNUNET_GETOPT_option_string ('o',
320 gettext_noop ("write search results to file starting with PREFIX"),
323 GNUNET_GETOPT_option_relative_time ('t',
326 gettext_noop ("automatically terminate search after DELAY"),
330 GNUNET_GETOPT_option_verbose (&verbose),
332 GNUNET_GETOPT_option_uint ('N',
335 gettext_noop ("automatically terminate search "
336 "after VALUE results are found"),
339 GNUNET_GETOPT_OPTION_END
342 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
346 GNUNET_PROGRAM_run (argc, argv, "gnunet-search [OPTIONS] KEYWORD",
348 ("Search GNUnet for files that were published on GNUnet"),
349 options, &run, NULL)) ? ret : 1;
350 GNUNET_free ((void*) argv);
354 /* end of gnunet-search.c */