gnunet-search.c 11 KB

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