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 2, 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 * - add many options (timeout, namespace search, etc.)
32 #include "gnunet_fs_service.h"
36 static const struct GNUNET_CONFIGURATION_Handle *cfg;
38 static struct GNUNET_SCHEDULER_Handle *sched;
40 static struct GNUNET_FS_Handle *ctx;
42 static struct GNUNET_FS_SearchContext *sc;
44 static unsigned int anonymity = 1;
49 item_printer (void *cls,
50 const char *plugin_name,
51 enum EXTRACTOR_MetaType type,
52 enum EXTRACTOR_MetaFormat format,
53 const char *data_mime_type,
57 if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
58 (format != EXTRACTOR_METAFORMAT_C_STRING) )
60 printf ("\t%20s: %s\n",
61 dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
62 EXTRACTOR_metatype_to_string (type)),
68 clean_task (void *cls,
69 const struct GNUNET_SCHEDULER_TaskContext *tc)
77 * Called by FS client to give information about the progress of an
81 * @param info details about the event, specifying the event type
82 * and various bits about the event
83 * @return client-context (for the next progress call
84 * for this operation; should be set to NULL for
85 * SUSPEND and STOPPED events). The value returned
86 * will be passed to future callbacks in the respective
87 * field in the GNUNET_FS_ProgressInfo struct.
90 progress_cb (void *cls,
91 const struct GNUNET_FS_ProgressInfo *info)
99 case GNUNET_FS_STATUS_SEARCH_START:
101 case GNUNET_FS_STATUS_SEARCH_RESULT:
102 uri = GNUNET_FS_uri_to_string (info->value.search.specifics.result.uri);
103 printf ("%s:\n", uri);
105 GNUNET_CONTAINER_meta_data_get_by_type (info->value.search.specifics.result.meta,
106 EXTRACTOR_METATYPE_FILENAME);
107 if (filename != NULL)
109 while (NULL != (dotdot = strstr (filename, "..")))
110 dotdot[0] = dotdot[1] = '_';
111 printf ("gnunet-download -o \"%s\" %s\n",
116 printf ("gnunet-download %s\n", uri);
118 GNUNET_CONTAINER_meta_data_iterate (info->value.search.specifics.result.meta,
123 GNUNET_free_non_null (filename);
126 case GNUNET_FS_STATUS_SEARCH_UPDATE:
128 case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
131 case GNUNET_FS_STATUS_SEARCH_ERROR:
133 _("Error searching: %s.\n"),
134 info->value.search.specifics.error.message);
135 GNUNET_SCHEDULER_shutdown (sched);
137 case GNUNET_FS_STATUS_SEARCH_STOPPED:
139 GNUNET_SCHEDULER_add_continuation (sched,
142 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
146 _("Unexpected status: %d\n"),
155 shutdown_task (void *cls,
156 const struct GNUNET_SCHEDULER_TaskContext *tc)
160 GNUNET_FS_search_stop (sc);
167 * Main function that will be run by the scheduler.
170 * @param s the scheduler to use
171 * @param args remaining command-line arguments
172 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
173 * @param c configuration
177 struct GNUNET_SCHEDULER_Handle *s,
180 const struct GNUNET_CONFIGURATION_Handle *c)
182 struct GNUNET_FS_Uri *uri;
187 while (NULL != args[argc])
189 uri = GNUNET_FS_uri_ksk_create_from_args (argc,
190 (const char **) args);
194 _("Could not create keyword URI from arguments.\n"));
196 GNUNET_FS_uri_destroy (uri);
200 ctx = GNUNET_FS_start (sched,
205 GNUNET_FS_FLAGS_NONE,
206 GNUNET_FS_OPTIONS_END);
210 _("Could not initialize `%s' subsystem.\n"),
212 GNUNET_FS_uri_destroy (uri);
213 GNUNET_FS_stop (ctx);
217 sc = GNUNET_FS_search_start (ctx,
221 GNUNET_FS_uri_destroy (uri);
225 _("Could not start searching.\n"));
226 GNUNET_FS_stop (ctx);
230 GNUNET_SCHEDULER_add_delayed (sched,
231 GNUNET_TIME_UNIT_FOREVER_REL,
238 * gnunet-search command line options
240 static struct GNUNET_GETOPT_CommandLineOption options[] = {
241 {'a', "anonymity", "LEVEL",
242 gettext_noop ("set the desired LEVEL of receiver-anonymity"),
243 1, &GNUNET_GETOPT_set_uint, &anonymity},
245 GNUNET_GETOPT_OPTION_END
250 * The main function to search GNUnet.
252 * @param argc number of arguments from the command line
253 * @param argv command line arguments
254 * @return 0 ok, 1 on error
257 main (int argc, char *const *argv)
260 GNUNET_PROGRAM_run (argc,
265 options, &run, NULL)) ? ret : 1;
268 /* end of gnunet-search.c */