gnunet-search.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/gnunet-search.c
  19. * @brief searching for files on GNUnet
  20. * @author Christian Grothoff
  21. * @author Krista Bennett
  22. * @author James Blackwell
  23. * @author Igor Wronsky
  24. */
  25. #include "platform.h"
  26. #include "gnunet_fs_service.h"
  27. static int ret;
  28. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  29. static struct GNUNET_FS_Handle *ctx;
  30. static struct GNUNET_FS_SearchContext *sc;
  31. static char *output_filename;
  32. static struct GNUNET_FS_DirectoryBuilder *db;
  33. static unsigned int anonymity = 1;
  34. static unsigned long long timeout;
  35. static int verbose;
  36. static int local_only;
  37. /**
  38. * Type of a function that libextractor calls for each
  39. * meta data item found.
  40. *
  41. * @param cls closure (user-defined, unused)
  42. * @param plugin_name name of the plugin that produced this value;
  43. * special values can be used (i.e. '<zlib>' for zlib being
  44. * used in the main libextractor library and yielding
  45. * meta data).
  46. * @param type libextractor-type describing the meta data
  47. * @param format basic format information about data
  48. * @param data_mime_type mime-type of data (not of the original file);
  49. * can be NULL (if mime-type is not known)
  50. * @param data actual meta-data found
  51. * @param data_size number of bytes in data
  52. * @return 0 to continue extracting, 1 to abort
  53. */
  54. static int
  55. item_printer (void *cls,
  56. const char *plugin_name,
  57. enum EXTRACTOR_MetaType type,
  58. enum EXTRACTOR_MetaFormat format,
  59. const char *data_mime_type,
  60. const char *data,
  61. size_t data_size)
  62. {
  63. if ( (format != EXTRACTOR_METAFORMAT_UTF8) &&
  64. (format != EXTRACTOR_METAFORMAT_C_STRING) )
  65. return 0;
  66. if (type == EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME)
  67. return 0;
  68. printf ("\t%20s: %s\n",
  69. dgettext (LIBEXTRACTOR_GETTEXT_DOMAIN,
  70. EXTRACTOR_metatype_to_string (type)),
  71. data);
  72. return 0;
  73. }
  74. static void
  75. clean_task (void *cls,
  76. const struct GNUNET_SCHEDULER_TaskContext *tc)
  77. {
  78. size_t dsize;
  79. void *ddata;
  80. GNUNET_FS_stop (ctx);
  81. ctx = NULL;
  82. if (output_filename == NULL)
  83. return;
  84. if (GNUNET_OK !=
  85. GNUNET_FS_directory_builder_finish (db,
  86. &dsize,
  87. &ddata))
  88. {
  89. GNUNET_break (0);
  90. GNUNET_free (output_filename);
  91. return;
  92. }
  93. if (dsize !=
  94. GNUNET_DISK_fn_write (output_filename,
  95. ddata,
  96. dsize,
  97. GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE))
  98. {
  99. fprintf (stderr,
  100. _("Failed to write directory with search results to `%s'\n"),
  101. output_filename);
  102. }
  103. GNUNET_free_non_null (ddata);
  104. GNUNET_free (output_filename);
  105. }
  106. /**
  107. * Called by FS client to give information about the progress of an
  108. * operation.
  109. *
  110. * @param cls closure
  111. * @param info details about the event, specifying the event type
  112. * and various bits about the event
  113. * @return client-context (for the next progress call
  114. * for this operation; should be set to NULL for
  115. * SUSPEND and STOPPED events). The value returned
  116. * will be passed to future callbacks in the respective
  117. * field in the GNUNET_FS_ProgressInfo struct.
  118. */
  119. static void *
  120. progress_cb (void *cls,
  121. const struct GNUNET_FS_ProgressInfo *info)
  122. {
  123. static unsigned int cnt;
  124. char *uri;
  125. char *dotdot;
  126. char *filename;
  127. switch (info->status)
  128. {
  129. case GNUNET_FS_STATUS_SEARCH_START:
  130. break;
  131. case GNUNET_FS_STATUS_SEARCH_RESULT:
  132. if (db != NULL)
  133. GNUNET_FS_directory_builder_add (db,
  134. info->value.search.specifics.result.uri,
  135. info->value.search.specifics.result.meta,
  136. NULL);
  137. uri = GNUNET_FS_uri_to_string (info->value.search.specifics.result.uri);
  138. printf ("#%u:\n", cnt++);
  139. filename =
  140. GNUNET_CONTAINER_meta_data_get_by_type (info->value.search.specifics.result.meta,
  141. EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
  142. if (filename != NULL)
  143. {
  144. while (NULL != (dotdot = strstr (filename, "..")))
  145. dotdot[0] = dotdot[1] = '_';
  146. printf ("gnunet-download -o \"%s\" %s\n",
  147. filename,
  148. uri);
  149. }
  150. else
  151. printf ("gnunet-download %s\n", uri);
  152. if (verbose)
  153. GNUNET_CONTAINER_meta_data_iterate (info->value.search.specifics.result.meta,
  154. &item_printer,
  155. NULL);
  156. printf ("\n");
  157. fflush(stdout);
  158. GNUNET_free_non_null (filename);
  159. GNUNET_free (uri);
  160. break;
  161. case GNUNET_FS_STATUS_SEARCH_UPDATE:
  162. break;
  163. case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
  164. /* ignore */
  165. break;
  166. case GNUNET_FS_STATUS_SEARCH_ERROR:
  167. fprintf (stderr,
  168. _("Error searching: %s.\n"),
  169. info->value.search.specifics.error.message);
  170. GNUNET_SCHEDULER_shutdown ();
  171. break;
  172. case GNUNET_FS_STATUS_SEARCH_STOPPED:
  173. GNUNET_SCHEDULER_add_continuation (&clean_task,
  174. NULL,
  175. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  176. break;
  177. default:
  178. fprintf (stderr,
  179. _("Unexpected status: %d\n"),
  180. info->status);
  181. break;
  182. }
  183. return NULL;
  184. }
  185. static void
  186. shutdown_task (void *cls,
  187. const struct GNUNET_SCHEDULER_TaskContext *tc)
  188. {
  189. if (sc != NULL)
  190. {
  191. GNUNET_FS_search_stop (sc);
  192. sc = NULL;
  193. }
  194. }
  195. /**
  196. * Main function that will be run by the scheduler.
  197. *
  198. * @param cls closure
  199. * @param args remaining command-line arguments
  200. * @param cfgfile name of the configuration file used (for saving, can be NULL!)
  201. * @param c configuration
  202. */
  203. static void
  204. run (void *cls,
  205. char *const *args,
  206. const char *cfgfile,
  207. const struct GNUNET_CONFIGURATION_Handle *c)
  208. {
  209. struct GNUNET_FS_Uri *uri;
  210. unsigned int argc;
  211. enum GNUNET_FS_SearchOptions options;
  212. struct GNUNET_TIME_Relative delay;
  213. argc = 0;
  214. while (NULL != args[argc])
  215. argc++;
  216. uri = GNUNET_FS_uri_ksk_create_from_args (argc,
  217. (const char **) args);
  218. if (NULL == uri)
  219. {
  220. fprintf (stderr,
  221. _("Could not create keyword URI from arguments.\n"));
  222. ret = 1;
  223. return;
  224. }
  225. cfg = c;
  226. ctx = GNUNET_FS_start (cfg,
  227. "gnunet-search",
  228. &progress_cb,
  229. NULL,
  230. GNUNET_FS_FLAGS_NONE,
  231. GNUNET_FS_OPTIONS_END);
  232. if (NULL == ctx)
  233. {
  234. fprintf (stderr,
  235. _("Could not initialize `%s' subsystem.\n"),
  236. "FS");
  237. GNUNET_FS_uri_destroy (uri);
  238. ret = 1;
  239. return;
  240. }
  241. if (output_filename != NULL)
  242. db = GNUNET_FS_directory_builder_create (NULL);
  243. options = GNUNET_FS_SEARCH_OPTION_NONE;
  244. if (local_only)
  245. options |= GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY;
  246. sc = GNUNET_FS_search_start (ctx,
  247. uri,
  248. anonymity,
  249. options,
  250. NULL);
  251. GNUNET_FS_uri_destroy (uri);
  252. if (NULL == sc)
  253. {
  254. fprintf (stderr,
  255. _("Could not start searching.\n"));
  256. GNUNET_FS_stop (ctx);
  257. ret = 1;
  258. return;
  259. }
  260. if (timeout != 0)
  261. {
  262. delay.rel_value = timeout;
  263. GNUNET_SCHEDULER_add_delayed (delay,
  264. &shutdown_task,
  265. NULL);
  266. }
  267. else
  268. {
  269. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
  270. &shutdown_task,
  271. NULL);
  272. }
  273. }
  274. /**
  275. * The main function to search GNUnet.
  276. *
  277. * @param argc number of arguments from the command line
  278. * @param argv command line arguments
  279. * @return 0 ok, 1 on error
  280. */
  281. int
  282. main (int argc, char *const *argv)
  283. {
  284. static const struct GNUNET_GETOPT_CommandLineOption options[] = {
  285. {'a', "anonymity", "LEVEL",
  286. gettext_noop ("set the desired LEVEL of receiver-anonymity"),
  287. 1, &GNUNET_GETOPT_set_uint, &anonymity},
  288. {'n', "no-network", NULL,
  289. gettext_noop ("only search the local peer (no P2P network search)"),
  290. 0, &GNUNET_GETOPT_set_one, &local_only},
  291. {'o', "output", "PREFIX",
  292. gettext_noop
  293. ("write search results to file starting with PREFIX"),
  294. 1, &GNUNET_GETOPT_set_string, &output_filename},
  295. {'t', "timeout", "VALUE",
  296. gettext_noop
  297. ("automatically terminate search after VALUE ms"),
  298. 1, &GNUNET_GETOPT_set_ulong, &timeout},
  299. {'V', "verbose", NULL,
  300. gettext_noop ("be verbose (print progress information)"),
  301. 0, &GNUNET_GETOPT_set_one, &verbose},
  302. GNUNET_GETOPT_OPTION_END
  303. };
  304. return (GNUNET_OK ==
  305. GNUNET_PROGRAM_run (argc,
  306. argv,
  307. "gnunet-search [OPTIONS] KEYWORD",
  308. gettext_noop
  309. ("Search GNUnet for files that were published on GNUnet"),
  310. options, &run, NULL)) ? ret : 1;
  311. }
  312. /* end of gnunet-search.c */