fixing shutdown
[oweals/gnunet.git] / src / fs / gnunet-search.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20 /**
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
27  *
28  * TODO:
29  * - add many options (timeout, namespace search, etc.)
30  */
31 #include "platform.h"
32 #include "gnunet_fs_service.h"
33
34 static int ret;
35
36 static const struct GNUNET_CONFIGURATION_Handle *cfg;
37
38 static struct GNUNET_SCHEDULER_Handle *sched;
39
40 static struct GNUNET_FS_Handle *ctx;
41
42 static struct GNUNET_FS_SearchContext *sc;
43
44 static unsigned int anonymity = 1;
45
46 static int verbose;
47
48 static int
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,
54               const char *data,
55               size_t data_size)
56 {
57   if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
58        (format != EXTRACTOR_METAFORMAT_C_STRING) )
59     return 0;
60   printf ("\t%20s: %s\n",
61           dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
62                     EXTRACTOR_metatype_to_string (type)),
63           data);
64   return GNUNET_OK;
65 }
66
67 static void
68 clean_task (void *cls,
69             const struct GNUNET_SCHEDULER_TaskContext *tc)
70 {
71   GNUNET_FS_stop (ctx);
72   ctx = NULL;
73 }
74
75
76 /**
77  * Called by FS client to give information about the progress of an 
78  * operation.
79  *
80  * @param cls closure
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.
88  */
89 static void *
90 progress_cb (void *cls,
91              const struct GNUNET_FS_ProgressInfo *info)
92 {
93   char *uri;
94   char *dotdot;
95   char *filename;
96
97   switch (info->status)
98     {
99     case GNUNET_FS_STATUS_SEARCH_START:
100       break;
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);
104       filename =
105         GNUNET_CONTAINER_meta_data_get_by_type (info->value.search.specifics.result.meta,
106                                                 EXTRACTOR_METATYPE_FILENAME);
107       if (filename != NULL)
108         {
109           while (NULL != (dotdot = strstr (filename, "..")))
110             dotdot[0] = dotdot[1] = '_';
111           printf ("gnunet-download -o \"%s\" %s\n", 
112                   filename, 
113                   uri);
114         }
115       else
116         printf ("gnunet-download %s\n", uri);
117       if (verbose)
118         GNUNET_CONTAINER_meta_data_iterate (info->value.search.specifics.result.meta, 
119                                             &item_printer,
120                                             NULL);
121       printf ("\n");
122       fflush(stdout);
123       GNUNET_free_non_null (filename);
124       GNUNET_free (uri);
125       break;
126     case GNUNET_FS_STATUS_SEARCH_UPDATE:
127       break;
128     case GNUNET_FS_STATUS_SEARCH_ERROR:
129       fprintf (stderr,
130                _("Error searching: %s.\n"),
131                info->value.search.specifics.error.message);
132       GNUNET_SCHEDULER_shutdown (sched);
133       break;
134     case GNUNET_FS_STATUS_SEARCH_STOPPED: 
135       sc = NULL;
136       GNUNET_SCHEDULER_add_continuation (sched,
137                                          &clean_task, 
138                                          NULL,
139                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
140       break;      
141     default:
142       fprintf (stderr,
143                _("Unexpected status: %d\n"),
144                info->status);
145       break;
146     }
147   return NULL;
148 }
149
150
151 static void
152 shutdown_task (void *cls,
153                const struct GNUNET_SCHEDULER_TaskContext *tc)
154 {
155   if (sc != NULL)
156     {
157       GNUNET_FS_search_stop (sc); 
158       sc = NULL;
159     }
160 }
161
162
163 /**
164  * Main function that will be run by the scheduler.
165  *
166  * @param cls closure
167  * @param sched the scheduler to use
168  * @param args remaining command-line arguments
169  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
170  * @param c configuration
171  */
172 static void
173 run (void *cls,
174      struct GNUNET_SCHEDULER_Handle *s,
175      char *const *args,
176      const char *cfgfile,
177      const struct GNUNET_CONFIGURATION_Handle *c)
178 {
179   struct GNUNET_FS_Uri *uri;
180   unsigned int argc;
181
182   sched = s;
183   argc = 0;
184   while (NULL != args[argc])
185     argc++;
186   uri = GNUNET_FS_uri_ksk_create_from_args (argc,
187                                             (const char **) args);
188   if (NULL == uri)
189     {
190       fprintf (stderr,
191                _("Could not create keyword URI from arguments.\n"));
192       ret = 1;
193       GNUNET_FS_uri_destroy (uri);
194       return;
195     }
196   cfg = c;
197   ctx = GNUNET_FS_start (sched,
198                          cfg,
199                          "gnunet-search",
200                          &progress_cb,
201                          NULL,
202                          GNUNET_FS_FLAGS_NONE,
203                          GNUNET_FS_OPTIONS_END);
204   if (NULL == ctx)
205     {
206       fprintf (stderr,
207                _("Could not initialize `%s' subsystem.\n"),
208                "FS");
209       GNUNET_FS_uri_destroy (uri);
210       GNUNET_FS_stop (ctx);
211       ret = 1;
212       return;
213     }
214   sc = GNUNET_FS_search_start (ctx,
215                                uri,
216                                anonymity,
217                                NULL);
218   GNUNET_FS_uri_destroy (uri);
219   if (NULL == sc)
220     {
221       fprintf (stderr,
222                _("Could not start searching.\n"));
223       GNUNET_FS_stop (ctx);
224       ret = 1;
225       return;
226     }
227   GNUNET_SCHEDULER_add_delayed (sched,
228                                 GNUNET_TIME_UNIT_FOREVER_REL,
229                                 &shutdown_task,
230                                 NULL);
231 }
232
233
234 /**
235  * gnunet-search command line options
236  */
237 static struct GNUNET_GETOPT_CommandLineOption options[] = {
238   {'a', "anonymity", "LEVEL",
239    gettext_noop ("set the desired LEVEL of receiver-anonymity"),
240    1, &GNUNET_GETOPT_set_uint, &anonymity},
241   // FIXME: options!
242   GNUNET_GETOPT_OPTION_END
243 };
244
245
246 /**
247  * The main function to search GNUnet.
248  *
249  * @param argc number of arguments from the command line
250  * @param argv command line arguments
251  * @return 0 ok, 1 on error
252  */
253 int
254 main (int argc, char *const *argv)
255 {
256   return (GNUNET_OK ==
257           GNUNET_PROGRAM_run (argc,
258                               argv,
259                               "gnunet-search",
260                               gettext_noop
261                               ("Search GNUnet."),
262                               options, &run, NULL)) ? ret : 1;
263 }
264
265 /* end of gnunet-search.c */
266