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 Affero 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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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;
54 static unsigned int verbose;
56 static int local_only;
58 static struct GNUNET_SCHEDULER_Task *tt;
62 * Type of a function that libextractor calls for each
63 * meta data item found.
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
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
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,
87 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
88 (format != EXTRACTOR_METAFORMAT_C_STRING))
90 if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
93 printf ("\t%20s: %s\n",
94 dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
95 EXTRACTOR_metatype_to_string (type)),
98 printf ("\t%20d: %s\n", type, data);
105 clean_task (void *cls)
110 GNUNET_FS_stop (ctx);
112 if (output_filename == NULL)
114 if (GNUNET_OK != GNUNET_FS_directory_builder_finish (db, &dsize, &ddata))
117 GNUNET_free (output_filename);
120 if (dsize != GNUNET_DISK_fn_write (output_filename,
123 GNUNET_DISK_PERM_USER_READ
124 | GNUNET_DISK_PERM_USER_WRITE))
127 _ ("Failed to write directory with search results to `%s'\n"),
130 GNUNET_free_non_null (ddata);
131 GNUNET_free (output_filename);
136 * Called by FS client to give information about the progress of an
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.
149 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
151 static unsigned int cnt;
156 switch (info->status)
158 case GNUNET_FS_STATUS_SEARCH_START:
161 case GNUNET_FS_STATUS_SEARCH_RESULT:
163 GNUNET_FS_directory_builder_add (db,
164 info->value.search.specifics.result.uri,
165 info->value.search.specifics.result.meta,
167 uri = GNUNET_FS_uri_to_string (info->value.search.specifics.result.uri);
168 printf ("#%u:\n", ++cnt);
169 filename = GNUNET_CONTAINER_meta_data_get_by_type (
170 info->value.search.specifics.result.meta,
171 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
172 is_directory = GNUNET_FS_meta_data_test_for_directory (
173 info->value.search.specifics.result.meta);
174 if (NULL != filename)
176 while ((filename[0] != '\0') && ('/' == filename[strlen (filename) - 1]))
177 filename[strlen (filename) - 1] = '\0';
178 GNUNET_DISK_filename_canonicalize (filename);
179 if (GNUNET_YES == is_directory)
180 printf ("gnunet-download -o \"%s%s\" -R %s\n",
182 GNUNET_FS_DIRECTORY_EXT,
185 printf ("gnunet-download -o \"%s\" %s\n", filename, uri);
187 else if (GNUNET_YES == is_directory)
188 printf ("gnunet-download -o \"collection%s\" -R %s\n",
189 GNUNET_FS_DIRECTORY_EXT,
192 printf ("gnunet-download %s\n", uri);
194 GNUNET_CONTAINER_meta_data_iterate (info->value.search.specifics.result
200 GNUNET_free_non_null (filename);
203 if ((results_limit > 0) && (results >= results_limit))
204 GNUNET_SCHEDULER_shutdown ();
207 case GNUNET_FS_STATUS_SEARCH_UPDATE:
210 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
214 case GNUNET_FS_STATUS_SEARCH_ERROR:
216 _ ("Error searching: %s.\n"),
217 info->value.search.specifics.error.message);
218 GNUNET_SCHEDULER_shutdown ();
221 case GNUNET_FS_STATUS_SEARCH_STOPPED:
222 GNUNET_SCHEDULER_add_now (&clean_task, NULL);
226 fprintf (stderr, _ ("Unexpected status: %d\n"), info->status);
234 shutdown_task (void *cls)
238 GNUNET_FS_search_stop (sc);
245 timeout_task (void *cls)
248 GNUNET_SCHEDULER_shutdown ();
253 * Main function that will be run by the scheduler.
256 * @param args remaining command-line arguments
257 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
258 * @param c configuration
264 const struct GNUNET_CONFIGURATION_Handle *c)
266 struct GNUNET_FS_Uri *uri;
268 enum GNUNET_FS_SearchOptions options;
271 while (NULL != args[argc])
273 uri = GNUNET_FS_uri_ksk_create_from_args (argc, (const char **) args);
278 _ ("Could not create keyword URI from arguments.\n"));
283 ctx = GNUNET_FS_start (cfg,
287 GNUNET_FS_FLAGS_NONE,
288 GNUNET_FS_OPTIONS_END);
291 fprintf (stderr, _ ("Could not initialize `%s' subsystem.\n"), "FS");
292 GNUNET_FS_uri_destroy (uri);
296 if (output_filename != NULL)
297 db = GNUNET_FS_directory_builder_create (NULL);
298 options = GNUNET_FS_SEARCH_OPTION_NONE;
300 options |= GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY;
301 sc = GNUNET_FS_search_start (ctx, uri, anonymity, options, NULL);
302 GNUNET_FS_uri_destroy (uri);
305 fprintf (stderr, "%s", _ ("Could not start searching.\n"));
306 GNUNET_FS_stop (ctx);
310 if (0 != timeout.rel_value_us)
311 tt = GNUNET_SCHEDULER_add_delayed (timeout, &timeout_task, NULL);
312 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
317 * The main function to search GNUnet.
319 * @param argc number of arguments from the command line
320 * @param argv command line arguments
321 * @return 0 ok, 1 on error
324 main (int argc, char *const *argv)
326 struct GNUNET_GETOPT_CommandLineOption options[] =
327 { GNUNET_GETOPT_option_uint ('a',
331 "set the desired LEVEL of receiver-anonymity"),
333 GNUNET_GETOPT_option_flag (
336 gettext_noop ("only search the local peer (no P2P network search)"),
338 GNUNET_GETOPT_option_string (
342 gettext_noop ("write search results to file starting with PREFIX"),
344 GNUNET_GETOPT_option_relative_time (
348 gettext_noop ("automatically terminate search after DELAY"),
350 GNUNET_GETOPT_option_verbose (&verbose),
351 GNUNET_GETOPT_option_uint ('N',
354 gettext_noop ("automatically terminate search "
355 "after VALUE results are found"),
357 GNUNET_GETOPT_OPTION_END };
359 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
364 GNUNET_PROGRAM_run (argc,
366 "gnunet-search [OPTIONS] KEYWORD",
368 "Search GNUnet for files that were published on GNUnet"),
374 GNUNET_free ((void *) argv);
379 /* end of gnunet-search.c */