GNUnet debian-0.24.3-29-g453fda2cf
 
Loading...
Searching...
No Matches
gnunet-search.c File Reference

searching for files on GNUnet More...

#include "platform.h"
#include <ctype.h>
#include <inttypes.h>
#include <limits.h>
#include "gnunet_fs_service.h"
Include dependency graph for gnunet-search.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_SEARCH_MetadataPrinterInfo
 

Macros

#define GNUNET_SEARCH_log(kind, ...)    GNUNET_log_from (kind, "gnunet-search", __VA_ARGS__)
 
#define DEFAULT_DIR_FORMAT   "#%n:\ngnunet-download -o \"%f\" -R %u\n\n"
 
#define HELP_DEFAULT_DIR_FORMAT   "#%n:\\ngnunet-download -o \"%f\" -R %u\\n\\n"
 
#define DEFAULT_FILE_FORMAT   "#%n:\ngnunet-download -o \"%f\" %u\n\n"
 
#define HELP_DEFAULT_FILE_FORMAT   "#%n:\\ngnunet-download -o \"%f\" %u\\n\\n"
 
#define VERB_DEFAULT_DIR_FORMAT   DEFAULT_DIR_FORMAT "%a\n"
 
#define VERB_DEFAULT_FILE_FORMAT   DEFAULT_FILE_FORMAT "%a\n"
 
#define DEFAULT_META_FORMAT   " MetaType #%i: %p\n"
 
#define HELP_DEFAULT_META_FORMAT   " MetaType #%i: %p\\n"
 
#define HELP_EXTRACTOR_TEXTADD   ""
 
#define GENERIC_DIRECTORY_NAME   "collection"
 
#define GENERIC_FILE_NAME   "no-name"
 
#define GENERIC_FILE_MIMETYPE   "application/octet-stream"
 
#define info   ((struct GNUNET_SEARCH_MetadataPrinterInfo *) cls)
 

Enumerations

enum  GNUNET_SEARCH_MetadataPrinterFlags { METADATA_PRINTER_FLAG_NONE = 0 , METADATA_PRINTER_FLAG_ONE_RUN = 1 , METADATA_PRINTER_FLAG_HAVE_TYPE = 2 }
 

Functions

static const char * print_escape_sequence (const char *const esc)
 Print the escape sequence at the beginning of a string.
 
static int item_printer (void *const cls, const char *const plugin_name, const enum EXTRACTOR_MetaType type, const enum EXTRACTOR_MetaFormat format, const char *const data_mime_type, const char *const data, const size_t data_size)
 Type of a function that libextractor calls for each meta data item found.
 
static void print_search_result (const char *const filename, const struct GNUNET_FS_Uri *const uri, const struct GNUNET_FS_MetaData *const metadata, const unsigned int resultnum, const int is_directory)
 Print a search result according to the current formats.
 
static void clean_task (void *const cls)
 
static void * progress_cb (void *const cls, const struct GNUNET_FS_ProgressInfo *const info)
 Called by FS client to give information about the progress of an operation.
 
static void shutdown_task (void *const cls)
 
static void timeout_task (void *const cls)
 
static void run (void *const cls, char *const *const args, const char *const cfgfile, const struct GNUNET_CONFIGURATION_Handle *const cfgarg)
 Main function that will be run by the scheduler.
 
int main (int argc, char *const *argv)
 The main function to search GNUnet.
 

Variables

static int ret
 
static const struct GNUNET_CONFIGURATION_Handlecfg
 
static struct GNUNET_FS_Handlectx
 
static struct GNUNET_FS_SearchContextsc
 
static char * output_filename
 
static char * format_string_opt
 
static char * dir_format_string_opt
 
static char * meta_format_string_opt
 
static const char * format_string
 
static const char * dir_format_string
 
static const char * meta_format_string
 
static struct GNUNET_FS_DirectoryBuilderdb
 
static unsigned int anonymity = 1
 
static struct GNUNET_TIME_Relative timeout
 Timeout for the search, 0 means to wait for CTRL-C.
 
static unsigned int results_limit
 
static unsigned int results
 
static unsigned int verbose
 
static int bookmark_only
 
static int local_only
 
static int silent_mode
 
static struct GNUNET_SCHEDULER_Tasktt
 
static int stop_searching
 

Detailed Description

searching for files on GNUnet

Author
Christian Grothoff
Krista Bennett
James Blackwell
Igor Wronsky
madmurphy

Definition in file gnunet-search.c.

Macro Definition Documentation

◆ GNUNET_SEARCH_log

#define GNUNET_SEARCH_log (   kind,
  ... 
)     GNUNET_log_from (kind, "gnunet-search", __VA_ARGS__)

Definition at line 37 of file gnunet-search.c.

42 :\ngnunet-download -o \"%f\" -R %u\n\n"
43#define HELP_DEFAULT_DIR_FORMAT "#%n:\\ngnunet-download -o \"%f\" -R %u\\n\\n"
44#define DEFAULT_FILE_FORMAT "#%n:\ngnunet-download -o \"%f\" %u\n\n"
45#define HELP_DEFAULT_FILE_FORMAT "#%n:\\ngnunet-download -o \"%f\" %u\\n\\n"
46#define VERB_DEFAULT_DIR_FORMAT DEFAULT_DIR_FORMAT "%a\n"
47#define VERB_DEFAULT_FILE_FORMAT DEFAULT_FILE_FORMAT "%a\n"
48
49#if HAVE_LIBEXTRACTOR
50#define DEFAULT_META_FORMAT " %t: %p\n"
51#define HELP_DEFAULT_META_FORMAT " %t: %p\\n"
52#define HELP_EXTRACTOR_TEXTADD ", %t"
53#else
54#define DEFAULT_META_FORMAT " MetaType #%i: %p\n"
55#define HELP_DEFAULT_META_FORMAT " MetaType #%i: %p\\n"
56#define HELP_EXTRACTOR_TEXTADD ""
57#endif
58
59#define GENERIC_DIRECTORY_NAME "collection"
60#define GENERIC_FILE_NAME "no-name"
61#define GENERIC_FILE_MIMETYPE "application/octet-stream"
62
63
65{
69};
70
71
73{
74 unsigned int counter;
75 unsigned int flags;
76 int type;
77};
78
79
80static int ret;
81
82static const struct GNUNET_CONFIGURATION_Handle *cfg;
83
84static struct GNUNET_FS_Handle *ctx;
85
86static struct GNUNET_FS_SearchContext *sc;
87
88static char *output_filename;
89
90static char *format_string_opt;
91
92static char *dir_format_string_opt;
93
94static char *meta_format_string_opt;
95
96static const char *format_string;
97
98static const char *dir_format_string;
99
100static const char *meta_format_string;
101
102static struct GNUNET_FS_DirectoryBuilder *db;
103
104static unsigned int anonymity = 1;
105
109static struct GNUNET_TIME_Relative timeout;
110
111static unsigned int results_limit;
112
113static unsigned int results;
114
115static unsigned int verbose;
116
117static int bookmark_only;
118
119static int local_only;
120
121static int silent_mode;
122
123static struct GNUNET_SCHEDULER_Task *tt;
124
125static int stop_searching;
126
127
139static const char *
140print_escape_sequence (const char *const esc)
141{
142 unsigned int probe;
143 const char *cursor = esc + 1;
144 char tmp;
145 switch (*cursor)
146 {
147 /* Trivia */
148 case '\\': putchar ('\\'); return cursor + 1;
149 case 'a': putchar ('\a'); return cursor + 1;
150 case 'b': putchar ('\b'); return cursor + 1;
151 case 'e': putchar ('\x1B'); return cursor + 1;
152 case 'f': putchar ('\f'); return cursor + 1;
153 case 'n': putchar ('\n'); return cursor + 1;
154 case 'r': putchar ('\r'); return cursor + 1;
155 case 't': putchar ('\t'); return cursor + 1;
156 case 'v': putchar ('\v'); return cursor + 1;
157
158 /* Possibly hexadecimal code point */
159 case 'x':
160 probe = 0;
161 while (probe < 256 && isxdigit ((tmp = *++cursor)))
162 probe = (probe << 4) + tmp - (tmp > 96 ? 87 : tmp > 64 ? 55 : 48);
163 goto maybe_codepoint;
164
165 /* Possibly octal code point */
166 case '0': case '1': case '2': case '3':
167 case '4': case '5': case '6': case '7':
168 probe = *cursor++ - 48;
169 do probe = (probe << 3) + *cursor++ - 48;
170 while (probe < 256 && cursor < esc + 4 && *cursor > 47 && *cursor < 56);
171 goto maybe_codepoint;
172
173 /* Boredom */
174 case '\0': putchar ('\\'); return cursor;
175 default: printf ("\\%c", *cursor); return cursor + 1;
176 }
177
178maybe_codepoint:
179 if (probe < 256)
180 putchar (probe);
181 else
182 fwrite (esc, 1, cursor - esc, stdout);
183 return cursor;
184}
185
186
204static int
205item_printer (void *const cls,
206 const char *const plugin_name,
207 const enum EXTRACTOR_MetaType type,
208 const enum EXTRACTOR_MetaFormat format,
209 const char *const data_mime_type,
210 const char *const data,
211 const size_t data_size)
212{
213 const char *cursor;
214 const char *next_spec;
215 const char *next_esc;
216#define info ((struct GNUNET_SEARCH_MetadataPrinterInfo *) cls)
217 if ((format != EXTRACTOR_METAFORMAT_UTF8 &&
220 return 0;
221 info->counter++;
222 if ((info->flags & METADATA_PRINTER_FLAG_HAVE_TYPE) && type != info->type)
223 return 0;
224
225 cursor = meta_format_string;
226 next_spec = strchr (cursor, '%');
227 next_esc = strchr (cursor, '\\');
228
229parse_format:
230
231 /* If an escape sequence exists before the next format specifier... */
232 if (next_esc && (! next_spec || next_esc < next_spec))
233 {
234 if (next_esc > cursor)
235 fwrite (cursor, 1, next_esc - cursor, stdout);
236
237 cursor = print_escape_sequence (next_esc);
238 next_esc = strchr (cursor, '\\');
239 goto parse_format;
240 }
241
242 /* If a format specifier exists before the next escape sequence... */
243 if (next_spec && (! next_esc || next_spec < next_esc))
244 {
245 if (next_spec > cursor)
246 fwrite (cursor, 1, next_spec - cursor, stdout);
247
248 switch (*++next_spec)
249 {
250 case '%': putchar ('%'); break;
251 case 'i': printf ("%d", type); break;
252 case 'l': printf ("%lu", (long unsigned int) data_size); break;
253 case 'n': printf ("%u", info->counter); break;
254 case 'p': printf ("%s", data); break;
255#if HAVE_LIBEXTRACTOR
256 case 't':
257 printf ("%s",
259 EXTRACTOR_metatype_to_string (type)));
260 break;
261#endif
262 case 'w': printf ("%s", plugin_name); break;
263 case '\0': putchar ('%'); return 0;
264 default: printf ("%%%c", *next_spec); break;
265 }
266 cursor = next_spec + 1;
267 next_spec = strchr (cursor, '%');
268 goto parse_format;
269 }
270
271 if (*cursor)
272 printf ("%s", cursor);
273
274 return info->flags & METADATA_PRINTER_FLAG_ONE_RUN;
275#undef info
276}
277
278
290static void
291print_search_result (const char *const filename,
292 const struct GNUNET_FS_Uri *const uri,
293 const struct GNUNET_FS_MetaData *const metadata,
294 const unsigned int resultnum,
295 const int is_directory)
296{
297
298 const char *cursor = GNUNET_YES == is_directory ?
301
302 const char *next_spec = strchr (cursor, '%');
303 const char *next_esc = strchr (cursor, '\\');
304 char *placeholder;
306
307parse_format:
308 /* If an escape sequence exists before the next format specifier... */
309 if (next_esc && (! next_spec || next_esc < next_spec))
310 {
311 if (next_esc > cursor)
312 fwrite (cursor, 1, next_esc - cursor, stdout);
313
314 cursor = print_escape_sequence (next_esc);
315 next_esc = strchr (cursor, '\\');
316 goto parse_format;
317 }
318
319 /* If a format specifier exists before the next escape sequence... */
320 if (next_spec && (! next_esc || next_spec < next_esc))
321 {
322 if (next_spec > cursor)
323 fwrite (cursor, 1, next_spec - cursor, stdout);
324
325 switch (*++next_spec)
326 {
327 /* All metadata fields */
328 case 'a':
330
331iterate_meta:
332 info.counter = 0;
334 break;
335 /* File's name */
336 case 'f':
337 if (GNUNET_YES == is_directory)
338 {
339 printf ("%s%s", filename, GNUNET_FS_DIRECTORY_EXT);
340 break;
341 }
342 printf ("%s", filename);
343 break;
344 /* Only the first metadata field */
345 case 'j':
347 goto iterate_meta;
348 /* File name's length */
349 case 'l':
350 printf ("%lu",
351 (long unsigned int) (GNUNET_YES == is_directory ?
352 strlen (filename)
353 + (sizeof(GNUNET_FS_DIRECTORY_EXT) - 1)
354 :
355 strlen (filename)));
356 break;
357 /* File's mime type */
358 case 'm':
359 if (GNUNET_YES == is_directory)
360 {
361 printf ("%s", GNUNET_FS_DIRECTORY_MIME);
362 break;
363 }
364 placeholder = GNUNET_FS_meta_data_get_by_type (
365 metadata,
367 printf ("%s", placeholder ? placeholder : GENERIC_FILE_MIMETYPE);
368 GNUNET_free (placeholder);
369 break;
370 /* Result number */
371 case 'n': printf ("%u", resultnum); break;
372 /* File's size */
373 case 's':
374 printf ("%" PRIu64, GNUNET_FS_uri_chk_get_file_size (uri));
375 break;
376 /* File's URI */
377 case 'u':
378 placeholder = GNUNET_FS_uri_to_string (uri);
379 printf ("%s", placeholder);
380 GNUNET_free (placeholder);
381 break;
382
383 /* We can add as many cases as we want here... */
384
385 /* Handle `%123#a` and `%123#j` (e.g. `%5#j` is a book title) */
386 case '0': case '1': case '2': case '3': case '4':
387 case '5': case '6': case '7': case '8': case '9':
388 cursor = next_spec;
389 info.type = *cursor - 48;
390 while (isdigit (*++cursor) && info.type < (INT_MAX - *cursor + 48) / 10)
391 info.type = info.type * 10 + *cursor - 48;
392 if (info.type == 0 || *cursor != '#')
393 goto not_a_specifier;
394 switch (*++cursor)
395 {
396 /* All metadata fields of type `info.type` */
397 case 'a':
398 next_spec = cursor;
400 goto iterate_meta;
401
402 /* Only the first metadata field of type `info.type` */
403 case 'j':
404 next_spec = cursor;
407 goto iterate_meta;
408 }
409 goto not_a_specifier;
410
411 /* All other cases */
412 case '%': putchar ('%'); break;
413 case '\0': putchar ('%'); return;
414
415not_a_specifier:
416 default: printf ("%%%c", *next_spec); break;
417 }
418 cursor = next_spec + 1;
419 next_spec = strchr (cursor, '%');
420 goto parse_format;
421 }
422
423 if (*cursor)
424 printf ("%s", cursor);
425}
426
427
428static void
429clean_task (void *const cls)
430{
431 size_t dsize;
432 void *ddata;
433
435 ctx = NULL;
436 if (output_filename == NULL)
437 return;
438 if (GNUNET_OK !=
439 GNUNET_FS_directory_builder_finish (db, &dsize, &ddata))
440 {
441 GNUNET_break (0);
443 return;
444 }
446 if (GNUNET_OK !=
448 ddata,
449 dsize,
452 {
454 _ ("Failed to write directory with search results to "
455 "`%s'\n"),
457 }
458 GNUNET_free (ddata);
460}
461
462
476static void *
477progress_cb (void *const cls,
478 const struct GNUNET_FS_ProgressInfo *const info)
479{
480 static unsigned int cnt;
481 int is_directory;
482 char *filename;
483
484 switch (info->status)
485 {
487 break;
488
490 if (stop_searching)
491 break;
492
493 if (db != NULL)
495 db,
496 info->value.search.specifics.result.uri,
497 info->value.search.specifics.result.meta,
498 NULL);
499
500 if (silent_mode)
501 break;
502
503 cnt++;
505 info->value.search.specifics.result.meta,
508 info->value.search.specifics.result.meta);
509 if (NULL != filename)
510 {
511 while ((filename[0] != '\0') && ('/' == filename[strlen (filename) - 1]))
512 filename[strlen (filename) - 1] = '\0';
514 }
517 : is_directory ?
519 :
521 info->value.search.specifics.result.uri,
522 info->value.search.specifics.result.meta,
523 cnt,
524 is_directory);
525 fflush (stdout);
527 results++;
528 if ((results_limit > 0) && (results >= results_limit))
529 {
531 /* otherwise the function might keep printing results for a while... */
533 }
534 break;
535
538 /* ignore */
539 break;
540
543 _ ("Error searching: %s.\n"),
544 info->value.search.specifics.error.message);
546 break;
547
550 break;
551
552 default:
554 _ ("Unexpected status: %d\n"),
555 info->status);
556 break;
557 }
558 return NULL;
559}
560
561
562static void
563shutdown_task (void *const cls)
564{
565 if (sc != NULL)
566 {
568 sc = NULL;
569 }
573}
574
575
576static void
577timeout_task (void *const cls)
578{
579 tt = NULL;
582}
583
584
593static void
594run (void *const cls,
595 char *const *const args,
596 const char *const cfgfile,
597 const struct GNUNET_CONFIGURATION_Handle *const cfgarg)
598{
599 struct GNUNET_FS_Uri *uri;
600 unsigned int argc;
602
604 {
605 fprintf (stderr,
606 _ ("Conflicting options --bookmark-only and --silent.\n"));
607 ret = 1;
608 return;
609 }
611 {
612 fprintf (stderr,
613 _ ("Conflicting options --bookmark-only and --output.\n"));
614 ret = 1;
615 return;
616 }
618 {
619 fprintf (stderr, _ ("An output file is mandatory for silent mode.\n"));
620 ret = 1;
621 return;
622 }
623 if (NULL == dir_format_string_opt)
627 else
629 if (NULL == format_string_opt)
632 else
634 if (NULL == meta_format_string_opt)
636 else
638 argc = 0;
639 while (NULL != args[argc])
640 argc++;
641 uri = GNUNET_FS_uri_ksk_create_from_args (argc, (const char **) args);
642 if (NULL == uri)
643 {
644 fprintf (stderr,
645 "%s",
646 _ ("Could not create keyword URI from arguments.\n"));
647 ret = 1;
648 return;
649 }
651 {
652 fprintf (stderr,
653 "%s",
654 _ ("Invalid URI. Valid URIs for searching are keyword query "
655 "URIs\n(\"gnunet://fs/ksk/...\") and namespace content URIs "
656 "(\"gnunet://fs/sks/...\").\n"));
658 ret = 1;
659 return;
660 }
661 if (bookmark_only)
662 {
663 char *bmstr = GNUNET_FS_uri_to_string (uri);
664 printf ("%s\n", bmstr);
665 GNUNET_free (bmstr);
667 ret = 0;
668 return;
669 }
670 cfg = cfgarg;
672 "gnunet-search",
674 NULL,
677 if (NULL == ctx)
678 {
679 fprintf (stderr, _ ("Could not initialize the `%s` subsystem.\n"), "FS");
681 ret = 1;
682 return;
683 }
684 if (output_filename != NULL)
687 if (local_only)
691 if (NULL == sc)
692 {
693 fprintf (stderr, "%s", _ ("Could not start searching.\n"));
695 ret = 1;
696 return;
697 }
698 if (0 != timeout.rel_value_us)
701}
702
703
711int
712main (int argc, char *const *argv)
713{
716 'a',
717 "anonymity",
718 "LEVEL",
719 gettext_noop ("set the desired LEVEL of receiver-anonymity (default: "
720 "1)"),
721 &anonymity),
723 'b',
724 "bookmark-only",
725 gettext_noop ("do not search, print only the URI that points to this "
726 "search"),
729 'F',
730 "dir-printf",
731 "FORMAT",
732 gettext_noop ("write search results for directories according to "
733 "FORMAT; accepted placeholders are: %a, %f, %j, %l, %m, "
734 "%n, %s; defaults to the value of --printf when omitted "
735 "or to `" HELP_DEFAULT_DIR_FORMAT "` if --printf is "
736 "omitted too"),
739 'f',
740 "printf",
741 "FORMAT",
742 gettext_noop ("write search results according to FORMAT; accepted "
743 "placeholders are: %a, %f, %j, %l, %m, %n, %s; defaults "
744 "to `" HELP_DEFAULT_FILE_FORMAT "` when omitted"),
747 'i',
748 "iter-printf",
749 "FORMAT",
750 gettext_noop ("when the %a or %j placeholders appear in --printf or "
751 "--dir-printf, list each metadata property according to "
752 "FORMAT; accepted placeholders are: %i, %l, %n, %p"
753 HELP_EXTRACTOR_TEXTADD ", %w; defaults to `"
754 HELP_DEFAULT_META_FORMAT "` when omitted"),
757 "results",
758 "VALUE",
759 gettext_noop ("automatically terminate search "
760 "after VALUE results are found"),
763 'n',
764 "no-network",
765 gettext_noop ("only search the local peer (no P2P network search)"),
766 &local_only),
768 'o',
769 "output",
770 "FILENAME",
771 gettext_noop ("create a GNUnet directory with search results at "
772 "FILENAME (e.g. `gnunet-search --output=commons"
773 GNUNET_FS_DIRECTORY_EXT " commons`)"),
776 's',
777 "silent",
778 gettext_noop ("silent mode (requires the --output argument)"),
779 &silent_mode),
781 't',
782 "timeout",
783 "DELAY",
784 gettext_noop ("automatically terminate search after DELAY; the value "
785 "given must be a number followed by a space and a time "
786 "unit, for example \"500 ms\"; without a unit it defaults "
787 "to microseconds - 1000000 = 1 second; if 0 or omitted "
788 "it means to wait for CTRL-C"),
789 &timeout),
791 'V',
792 "verbose",
793 gettext_noop ("be verbose (append \"%a\\n\" to the default --printf and "
794 "--dir-printf arguments - ignored when these are provided "
795 "by the user)"),
796 &verbose),
798
799 if (GNUNET_SYSERR ==
801 argc,
802 argv,
803 "gnunet-search [OPTIONS] KEYWORD1 KEYWORD2 ...",
804 gettext_noop ("Search for files that have been "
805 "published on GNUnet\n"),
806 options,
807 &run,
808 NULL))
809 ret = 1;
810
811 return ret;
812}
813
814
815/* end of gnunet-search.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition 002.c:5
#define INT_MAX
int main()
Program to simulate results from GCP_get_desirability_of_path() for various plausible inputs.
#define gettext_noop(String)
Definition gettext.h:74
#define dgettext(Domainname, Msgid)
Definition gettext.h:51
static struct GNUNET_SCHEDULER_Task * timeout_task
Task to be run on timeout.
Definition gnunet-arm.c:123
static char * plugin_name
Name of our plugin.
static char * data
The data to insert into the dht.
static char * filename
struct GNUNET_SCHEDULER_Task * shutdown_task
static char * value
Value of the record to add/remove.
static uint32_t type
Type string converted to DNS type value.
static size_t data_size
Number of bytes in data.
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
static struct GNUNET_FS_MetaData * meta
Meta-data provided via command-line option.
static int result
Global testing status.
#define VERB_DEFAULT_DIR_FORMAT
static const char * meta_format_string
#define info
static const char * dir_format_string
static char * output_filename
#define DEFAULT_META_FORMAT
static unsigned int verbose
#define HELP_DEFAULT_FILE_FORMAT
#define DEFAULT_DIR_FORMAT
static struct GNUNET_FS_SearchContext * sc
#define HELP_DEFAULT_DIR_FORMAT
#define HELP_DEFAULT_META_FORMAT
static unsigned int anonymity
#define GENERIC_FILE_NAME
static void run(void *const cls, char *const *const args, const char *const cfgfile, const struct GNUNET_CONFIGURATION_Handle *const cfgarg)
Main function that will be run by the scheduler.
static void clean_task(void *const cls)
static int silent_mode
#define GNUNET_SEARCH_log(kind,...)
static const char * format_string
static const struct GNUNET_CONFIGURATION_Handle * cfg
static int bookmark_only
static int ret
static char * format_string_opt
static char * dir_format_string_opt
static struct GNUNET_SCHEDULER_Task * tt
static unsigned int results
static struct GNUNET_FS_Handle * ctx
GNUNET_SEARCH_MetadataPrinterFlags
@ METADATA_PRINTER_FLAG_HAVE_TYPE
@ METADATA_PRINTER_FLAG_NONE
@ METADATA_PRINTER_FLAG_ONE_RUN
#define GENERIC_FILE_MIMETYPE
static const char * print_escape_sequence(const char *const esc)
Print the escape sequence at the beginning of a string.
static struct GNUNET_TIME_Relative timeout
Timeout for the search, 0 means to wait for CTRL-C.
static int stop_searching
#define DEFAULT_FILE_FORMAT
#define HELP_EXTRACTOR_TEXTADD
static int local_only
static unsigned int results_limit
#define GENERIC_DIRECTORY_NAME
static char * meta_format_string_opt
static void * progress_cb(void *const cls, const struct GNUNET_FS_ProgressInfo *const info)
Called by FS client to give information about the progress of an operation.
#define VERB_DEFAULT_FILE_FORMAT
static int item_printer(void *const cls, const char *const plugin_name, const enum EXTRACTOR_MetaType type, const enum EXTRACTOR_MetaFormat format, const char *const data_mime_type, const char *const data, const size_t data_size)
Type of a function that libextractor calls for each meta data item found.
static void print_search_result(const char *const filename, const struct GNUNET_FS_Uri *const uri, const struct GNUNET_FS_MetaData *const metadata, const unsigned int resultnum, const int is_directory)
Print a search result according to the current formats.
static struct GNUNET_FS_DirectoryBuilder * db
void GNUNET_DISK_filename_canonicalize(char *fn)
Removes special characters as ':' from a filename.
Definition disk.c:1212
enum GNUNET_GenericReturnValue GNUNET_DISK_fn_write(const char *fn, const void *buf, size_t buf_size, enum GNUNET_DISK_AccessPermissions mode)
Write a buffer to a file atomically.
Definition disk.c:750
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -rf).
Definition disk.c:1098
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
#define EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME
GNUNET_FS_SearchOptions
Options for searching.
int GNUNET_FS_directory_builder_finish(struct GNUNET_FS_DirectoryBuilder *bld, size_t *rsize, void **rdata)
Finish building the directory.
uint64_t GNUNET_FS_uri_chk_get_file_size(const struct GNUNET_FS_Uri *uri)
What is the size of the file that this URI refers to?
Definition fs_uri.c:1360
int GNUNET_FS_uri_test_ksk(const struct GNUNET_FS_Uri *uri)
Is this a keyword URI?
Definition fs_uri.c:1324
#define GNUNET_FS_DIRECTORY_MIME
char * GNUNET_FS_uri_to_string(const struct GNUNET_FS_Uri *uri)
Convert a URI to a UTF-8 String.
Definition fs_uri.c:2034
struct GNUNET_FS_Handle * GNUNET_FS_start(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *client_name, GNUNET_FS_ProgressCallback upcb, void *upcb_cls, enum GNUNET_FS_Flags flags,...)
Setup a connection to the file-sharing service.
Definition fs_api.c:3264
struct GNUNET_FS_Uri * GNUNET_FS_uri_ksk_create_from_args(unsigned int argc, const char **argv)
Create an FS URI from a user-supplied command line of keywords.
Definition fs_uri.c:1144
void GNUNET_FS_search_stop(struct GNUNET_FS_SearchContext *sc)
Stop search for content.
Definition fs_search.c:1766
void GNUNET_FS_uri_destroy(struct GNUNET_FS_Uri *uri)
Free URI.
Definition fs_uri.c:677
int GNUNET_FS_uri_test_sks(const struct GNUNET_FS_Uri *uri)
Is this a namespace URI?
Definition fs_uri.c:1271
struct GNUNET_FS_DirectoryBuilder * GNUNET_FS_directory_builder_create(const struct GNUNET_FS_MetaData *mdir)
Create a directory builder.
int GNUNET_FS_meta_data_test_for_directory(const struct GNUNET_FS_MetaData *md)
Does the meta-data claim that this is a directory? Checks if the mime-type is that of a GNUnet direct...
#define GNUNET_FS_DIRECTORY_EXT
void GNUNET_FS_stop(struct GNUNET_FS_Handle *h)
Close our connection with the file-sharing service.
Definition fs_api.c:3330
EXTRACTOR_MetaFormat
Format in which the extracted meta data is presented.
EXTRACTOR_MetaType
Enumeration defining various sources of keywords.
struct GNUNET_FS_SearchContext * GNUNET_FS_search_start(struct GNUNET_FS_Handle *h, const struct GNUNET_FS_Uri *uri, uint32_t anonymity, enum GNUNET_FS_SearchOptions options, void *cctx)
Start search for content.
Definition fs_search.c:1607
void GNUNET_FS_directory_builder_add(struct GNUNET_FS_DirectoryBuilder *bld, const struct GNUNET_FS_Uri *uri, const struct GNUNET_FS_MetaData *md, const void *data)
Add an entry to a directory.
@ GNUNET_FS_FLAGS_NONE
No special flags set.
@ GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY
Only search the local host, do not search remote systems (no P2P)
@ GNUNET_FS_SEARCH_OPTION_NONE
No options (use defaults for everything).
@ GNUNET_FS_OPTIONS_END
Last option in the VARARG list.
@ GNUNET_FS_STATUS_SEARCH_UPDATE
We have additional data about the quality or availability of a search result.
@ GNUNET_FS_STATUS_SEARCH_ERROR
Signals a problem with this search.
@ GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED
Event generated for each search result when the respective search is stopped.
@ GNUNET_FS_STATUS_SEARCH_RESULT
This search has yielded a result.
@ GNUNET_FS_STATUS_SEARCH_START
First event generated when a client requests a search to begin or when a namespace result automatical...
@ GNUNET_FS_STATUS_SEARCH_STOPPED
Last message from a search; this signals that there will be no further events associated with this se...
@ EXTRACTOR_METAFORMAT_C_STRING
0-terminated string.
@ EXTRACTOR_METAFORMAT_UTF8
0-terminated, UTF-8 encoded string.
@ EXTRACTOR_METATYPE_MIMETYPE
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_increment_uint(char shortName, const char *name, const char *description, unsigned int *val)
Increment val each time the option flag is given by one.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Relative *val)
Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time).
#define GNUNET_GETOPT_OPTION_END
Marker for the end of the list of options.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_free(ptr)
Wrapper around free.
int GNUNET_FS_meta_data_iterate(const struct GNUNET_FS_MetaData *md, EXTRACTOR_MetaDataProcessor iter, void *iter_cls)
Iterate over MD entries.
Definition meta_data.c:418
char * GNUNET_FS_meta_data_get_by_type(const struct GNUNET_FS_MetaData *md, enum EXTRACTOR_MetaType type)
Get the first MD entry of the given type.
Definition meta_data.c:438
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition program.c:407
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition scheduler.c:567
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition scheduler.c:1339
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition scheduler.c:1304
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition scheduler.c:1277
#define _(String)
GNU gettext support macro.
Definition platform.h:179
#define LIBEXTRACTOR_GETTEXT_DOMAIN
Definition platform.h:180
Internal state of a directory builder.
struct GNUNET_FS_MetaData * meta
Meta-data for the directory itself.
Master context for most FS operations.
Definition fs_api.h:1070
Meta data to associate with a file, directory or namespace.
Definition meta_data.c:92
Argument given to the progress callback with information about what is going on.
Handle for controlling a search.
Definition fs_api.h:1511
A Universal Resource Identifier (URI), opaque.
Definition fs_api.h:167
Definition of a command line option.
Entry in list of pending tasks.
Definition scheduler.c:136
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.

◆ DEFAULT_DIR_FORMAT

#define DEFAULT_DIR_FORMAT   "#%n:\ngnunet-download -o \"%f\" -R %u\n\n"

Definition at line 43 of file gnunet-search.c.

◆ HELP_DEFAULT_DIR_FORMAT

#define HELP_DEFAULT_DIR_FORMAT   "#%n:\\ngnunet-download -o \"%f\" -R %u\\n\\n"

Definition at line 44 of file gnunet-search.c.

◆ DEFAULT_FILE_FORMAT

#define DEFAULT_FILE_FORMAT   "#%n:\ngnunet-download -o \"%f\" %u\n\n"

Definition at line 45 of file gnunet-search.c.

◆ HELP_DEFAULT_FILE_FORMAT

#define HELP_DEFAULT_FILE_FORMAT   "#%n:\\ngnunet-download -o \"%f\" %u\\n\\n"

Definition at line 46 of file gnunet-search.c.

◆ VERB_DEFAULT_DIR_FORMAT

#define VERB_DEFAULT_DIR_FORMAT   DEFAULT_DIR_FORMAT "%a\n"

Definition at line 47 of file gnunet-search.c.

◆ VERB_DEFAULT_FILE_FORMAT

#define VERB_DEFAULT_FILE_FORMAT   DEFAULT_FILE_FORMAT "%a\n"

Definition at line 48 of file gnunet-search.c.

◆ DEFAULT_META_FORMAT

#define DEFAULT_META_FORMAT   " MetaType #%i: %p\n"

Definition at line 55 of file gnunet-search.c.

◆ HELP_DEFAULT_META_FORMAT

#define HELP_DEFAULT_META_FORMAT   " MetaType #%i: %p\\n"

Definition at line 56 of file gnunet-search.c.

◆ HELP_EXTRACTOR_TEXTADD

#define HELP_EXTRACTOR_TEXTADD   ""

Definition at line 57 of file gnunet-search.c.

◆ GENERIC_DIRECTORY_NAME

#define GENERIC_DIRECTORY_NAME   "collection"

Definition at line 60 of file gnunet-search.c.

◆ GENERIC_FILE_NAME

#define GENERIC_FILE_NAME   "no-name"

Definition at line 61 of file gnunet-search.c.

◆ GENERIC_FILE_MIMETYPE

#define GENERIC_FILE_MIMETYPE   "application/octet-stream"

Definition at line 62 of file gnunet-search.c.

◆ info

#define info   ((struct GNUNET_SEARCH_MetadataPrinterInfo *) cls)

Enumeration Type Documentation

◆ GNUNET_SEARCH_MetadataPrinterFlags

Enumerator
METADATA_PRINTER_FLAG_NONE 
METADATA_PRINTER_FLAG_ONE_RUN 
METADATA_PRINTER_FLAG_HAVE_TYPE 

Definition at line 65 of file gnunet-search.c.

Function Documentation

◆ print_escape_sequence()

static const char * print_escape_sequence ( const char *const  esc)
static

Print the escape sequence at the beginning of a string.

Parameters
esca string that must begin with a backslash (the function only assumes that it does, but does not check)
Returns
the fragment that follows what has been printed
Author
madmurphy

If "\\nfoo" is passed as argument, this function prints a new line and returns "foo"

Definition at line 141 of file gnunet-search.c.

142{
143 unsigned int probe;
144 const char *cursor = esc + 1;
145 char tmp;
146 switch (*cursor)
147 {
148 /* Trivia */
149 case '\\': putchar ('\\'); return cursor + 1;
150 case 'a': putchar ('\a'); return cursor + 1;
151 case 'b': putchar ('\b'); return cursor + 1;
152 case 'e': putchar ('\x1B'); return cursor + 1;
153 case 'f': putchar ('\f'); return cursor + 1;
154 case 'n': putchar ('\n'); return cursor + 1;
155 case 'r': putchar ('\r'); return cursor + 1;
156 case 't': putchar ('\t'); return cursor + 1;
157 case 'v': putchar ('\v'); return cursor + 1;
158
159 /* Possibly hexadecimal code point */
160 case 'x':
161 probe = 0;
162 while (probe < 256 && isxdigit ((tmp = *++cursor)))
163 probe = (probe << 4) + tmp - (tmp > 96 ? 87 : tmp > 64 ? 55 : 48);
164 goto maybe_codepoint;
165
166 /* Possibly octal code point */
167 case '0': case '1': case '2': case '3':
168 case '4': case '5': case '6': case '7':
169 probe = *cursor++ - 48;
170 do probe = (probe << 3) + *cursor++ - 48;
171 while (probe < 256 && cursor < esc + 4 && *cursor > 47 && *cursor < 56);
172 goto maybe_codepoint;
173
174 /* Boredom */
175 case '\0': putchar ('\\'); return cursor;
176 default: printf ("\\%c", *cursor); return cursor + 1;
177 }
178
179maybe_codepoint:
180 if (probe < 256)
181 putchar (probe);
182 else
183 fwrite (esc, 1, cursor - esc, stdout);
184 return cursor;
185}

Referenced by item_printer(), and print_search_result().

Here is the caller graph for this function:

◆ item_printer()

static int item_printer ( void *const  cls,
const char *const  plugin_name,
const enum EXTRACTOR_MetaType  type,
const enum EXTRACTOR_MetaFormat  format,
const char *const  data_mime_type,
const char *const  data,
const size_t  data_size 
)
static

Type of a function that libextractor calls for each meta data item found.

Parameters
clsclosure (user-defined, used for the iteration info)
plugin_namename of the plugin that produced this value; special values can be used (e.g. '<zlib>' for zlib being used in the main libextractor library and yielding meta data).
typelibextractor-type describing the meta data
formatbasic format information about data
data_mime_typemime-type of data (not of the original file); can be NULL (if mime-type is not known)
dataactual meta-data found
data_sizenumber of bytes in data
Returns
0 to continue extracting, 1 to abort

Definition at line 206 of file gnunet-search.c.

213{
214 const char *cursor;
215 const char *next_spec;
216 const char *next_esc;
217#define info ((struct GNUNET_SEARCH_MetadataPrinterInfo *) cls)
218 if ((format != EXTRACTOR_METAFORMAT_UTF8 &&
221 return 0;
222 info->counter++;
223 if ((info->flags & METADATA_PRINTER_FLAG_HAVE_TYPE) && type != info->type)
224 return 0;
225
226 cursor = meta_format_string;
227 next_spec = strchr (cursor, '%');
228 next_esc = strchr (cursor, '\\');
229
230parse_format:
231
232 /* If an escape sequence exists before the next format specifier... */
233 if (next_esc && (! next_spec || next_esc < next_spec))
234 {
235 if (next_esc > cursor)
236 fwrite (cursor, 1, next_esc - cursor, stdout);
237
238 cursor = print_escape_sequence (next_esc);
239 next_esc = strchr (cursor, '\\');
240 goto parse_format;
241 }
242
243 /* If a format specifier exists before the next escape sequence... */
244 if (next_spec && (! next_esc || next_spec < next_esc))
245 {
246 if (next_spec > cursor)
247 fwrite (cursor, 1, next_spec - cursor, stdout);
248
249 switch (*++next_spec)
250 {
251 case '%': putchar ('%'); break;
252 case 'i': printf ("%d", type); break;
253 case 'l': printf ("%lu", (long unsigned int) data_size); break;
254 case 'n': printf ("%u", info->counter); break;
255 case 'p': printf ("%s", data); break;
256#if HAVE_LIBEXTRACTOR
257 case 't':
258 printf ("%s",
260 EXTRACTOR_metatype_to_string (type)));
261 break;
262#endif
263 case 'w': printf ("%s", plugin_name); break;
264 case '\0': putchar ('%'); return 0;
265 default: printf ("%%%c", *next_spec); break;
266 }
267 cursor = next_spec + 1;
268 next_spec = strchr (cursor, '%');
269 goto parse_format;
270 }
271
272 if (*cursor)
273 printf ("%s", cursor);
274
275 return info->flags & METADATA_PRINTER_FLAG_ONE_RUN;
276#undef info
277}

References data, data_size, dgettext, EXTRACTOR_METAFORMAT_C_STRING, EXTRACTOR_METAFORMAT_UTF8, EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, info, LIBEXTRACTOR_GETTEXT_DOMAIN, meta_format_string, METADATA_PRINTER_FLAG_HAVE_TYPE, METADATA_PRINTER_FLAG_ONE_RUN, plugin_name, print_escape_sequence(), and type.

Referenced by print_search_result().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_search_result()

static void print_search_result ( const char *const  filename,
const struct GNUNET_FS_Uri *const  uri,
const struct GNUNET_FS_MetaData *const  metadata,
const unsigned int  resultnum,
const int  is_directory 
)
static

Print a search result according to the current formats.

Parameters
filenamethe filename for this result
urithe struct GNUNET_FS_Uri this result refers to
metadatathe struct GNUNET_FS_MetaData associated with this result
resultnumthe result number
is_directoryGNUNET_YES if this is a directory, otherwise GNUNET_NO
Author
madmurphy

Definition at line 292 of file gnunet-search.c.

297{
298
299 const char *cursor = GNUNET_YES == is_directory ?
302
303 const char *next_spec = strchr (cursor, '%');
304 const char *next_esc = strchr (cursor, '\\');
305 char *placeholder;
307
308parse_format:
309 /* If an escape sequence exists before the next format specifier... */
310 if (next_esc && (! next_spec || next_esc < next_spec))
311 {
312 if (next_esc > cursor)
313 fwrite (cursor, 1, next_esc - cursor, stdout);
314
315 cursor = print_escape_sequence (next_esc);
316 next_esc = strchr (cursor, '\\');
317 goto parse_format;
318 }
319
320 /* If a format specifier exists before the next escape sequence... */
321 if (next_spec && (! next_esc || next_spec < next_esc))
322 {
323 if (next_spec > cursor)
324 fwrite (cursor, 1, next_spec - cursor, stdout);
325
326 switch (*++next_spec)
327 {
328 /* All metadata fields */
329 case 'a':
331
332iterate_meta:
333 info.counter = 0;
335 break;
336 /* File's name */
337 case 'f':
338 if (GNUNET_YES == is_directory)
339 {
340 printf ("%s%s", filename, GNUNET_FS_DIRECTORY_EXT);
341 break;
342 }
343 printf ("%s", filename);
344 break;
345 /* Only the first metadata field */
346 case 'j':
348 goto iterate_meta;
349 /* File name's length */
350 case 'l':
351 printf ("%lu",
352 (long unsigned int) (GNUNET_YES == is_directory ?
353 strlen (filename)
354 + (sizeof(GNUNET_FS_DIRECTORY_EXT) - 1)
355 :
356 strlen (filename)));
357 break;
358 /* File's mime type */
359 case 'm':
360 if (GNUNET_YES == is_directory)
361 {
362 printf ("%s", GNUNET_FS_DIRECTORY_MIME);
363 break;
364 }
365 placeholder = GNUNET_FS_meta_data_get_by_type (
366 metadata,
368 printf ("%s", placeholder ? placeholder : GENERIC_FILE_MIMETYPE);
369 GNUNET_free (placeholder);
370 break;
371 /* Result number */
372 case 'n': printf ("%u", resultnum); break;
373 /* File's size */
374 case 's':
375 printf ("%" PRIu64, GNUNET_FS_uri_chk_get_file_size (uri));
376 break;
377 /* File's URI */
378 case 'u':
379 placeholder = GNUNET_FS_uri_to_string (uri);
380 printf ("%s", placeholder);
381 GNUNET_free (placeholder);
382 break;
383
384 /* We can add as many cases as we want here... */
385
386 /* Handle `%123#a` and `%123#j` (e.g. `%5#j` is a book title) */
387 case '0': case '1': case '2': case '3': case '4':
388 case '5': case '6': case '7': case '8': case '9':
389 cursor = next_spec;
390 info.type = *cursor - 48;
391 while (isdigit (*++cursor) && info.type < (INT_MAX - *cursor + 48) / 10)
392 info.type = info.type * 10 + *cursor - 48;
393 if (info.type == 0 || *cursor != '#')
394 goto not_a_specifier;
395 switch (*++cursor)
396 {
397 /* All metadata fields of type `info.type` */
398 case 'a':
399 next_spec = cursor;
401 goto iterate_meta;
402
403 /* Only the first metadata field of type `info.type` */
404 case 'j':
405 next_spec = cursor;
408 goto iterate_meta;
409 }
410 goto not_a_specifier;
411
412 /* All other cases */
413 case '%': putchar ('%'); break;
414 case '\0': putchar ('%'); return;
415
416not_a_specifier:
417 default: printf ("%%%c", *next_spec); break;
418 }
419 cursor = next_spec + 1;
420 next_spec = strchr (cursor, '%');
421 goto parse_format;
422 }
423
424 if (*cursor)
425 printf ("%s", cursor);
426}

References dir_format_string, EXTRACTOR_METATYPE_MIMETYPE, filename, format_string, GENERIC_FILE_MIMETYPE, GNUNET_free, GNUNET_FS_DIRECTORY_EXT, GNUNET_FS_DIRECTORY_MIME, GNUNET_FS_meta_data_get_by_type(), GNUNET_FS_meta_data_iterate(), GNUNET_FS_uri_chk_get_file_size(), GNUNET_FS_uri_to_string(), GNUNET_YES, info, INT_MAX, item_printer(), METADATA_PRINTER_FLAG_HAVE_TYPE, METADATA_PRINTER_FLAG_NONE, METADATA_PRINTER_FLAG_ONE_RUN, print_escape_sequence(), and uri.

Referenced by progress_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clean_task()

static void clean_task ( void *const  cls)
static

Definition at line 430 of file gnunet-search.c.

431{
432 size_t dsize;
433 void *ddata;
434
436 ctx = NULL;
437 if (output_filename == NULL)
438 return;
439 if (GNUNET_OK !=
440 GNUNET_FS_directory_builder_finish (db, &dsize, &ddata))
441 {
442 GNUNET_break (0);
444 return;
445 }
447 if (GNUNET_OK !=
449 ddata,
450 dsize,
453 {
455 _ ("Failed to write directory with search results to "
456 "`%s'\n"),
458 }
459 GNUNET_free (ddata);
461}

References _, ctx, db, GNUNET_break, GNUNET_DISK_directory_remove(), GNUNET_DISK_fn_write(), GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_FS_directory_builder_finish(), GNUNET_FS_stop(), GNUNET_OK, GNUNET_SEARCH_log, and output_filename.

Referenced by progress_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ progress_cb()

static void * progress_cb ( void *const  cls,
const struct GNUNET_FS_ProgressInfo *const  info 
)
static

Called by FS client to give information about the progress of an operation.

Parameters
clsclosure
infodetails about the event, specifying the event type and various bits about the event
Returns
client-context (for the next progress call for this operation; should be set to NULL for SUSPEND and STOPPED events). The value returned will be passed to future callbacks in the respective field in the GNUNET_FS_ProgressInfo struct.

Definition at line 478 of file gnunet-search.c.

480{
481 static unsigned int cnt;
482 int is_directory;
483 char *filename;
484
485 switch (info->status)
486 {
488 break;
489
491 if (stop_searching)
492 break;
493
494 if (db != NULL)
496 db,
497 info->value.search.specifics.result.uri,
498 info->value.search.specifics.result.meta,
499 NULL);
500
501 if (silent_mode)
502 break;
503
504 cnt++;
506 info->value.search.specifics.result.meta,
509 info->value.search.specifics.result.meta);
510 if (NULL != filename)
511 {
512 while ((filename[0] != '\0') && ('/' == filename[strlen (filename) - 1]))
513 filename[strlen (filename) - 1] = '\0';
515 }
518 : is_directory ?
520 :
522 info->value.search.specifics.result.uri,
523 info->value.search.specifics.result.meta,
524 cnt,
525 is_directory);
526 fflush (stdout);
528 results++;
529 if ((results_limit > 0) && (results >= results_limit))
530 {
532 /* otherwise the function might keep printing results for a while... */
534 }
535 break;
536
539 /* ignore */
540 break;
541
544 _ ("Error searching: %s.\n"),
545 info->value.search.specifics.error.message);
547 break;
548
551 break;
552
553 default:
555 _ ("Unexpected status: %d\n"),
556 info->status);
557 break;
558 }
559 return NULL;
560}

References _, clean_task(), db, EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, filename, GENERIC_DIRECTORY_NAME, GENERIC_FILE_NAME, GNUNET_DISK_filename_canonicalize(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_FS_directory_builder_add(), GNUNET_FS_meta_data_get_by_type(), GNUNET_FS_meta_data_test_for_directory(), GNUNET_FS_STATUS_SEARCH_ERROR, GNUNET_FS_STATUS_SEARCH_RESULT, GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED, GNUNET_FS_STATUS_SEARCH_START, GNUNET_FS_STATUS_SEARCH_STOPPED, GNUNET_FS_STATUS_SEARCH_UPDATE, GNUNET_SCHEDULER_add_now(), GNUNET_SCHEDULER_shutdown(), GNUNET_SEARCH_log, GNUNET_YES, info, GNUNET_FS_DirectoryBuilder::meta, print_search_result(), results, results_limit, silent_mode, and stop_searching.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ shutdown_task()

static void shutdown_task ( void *const  cls)
static

Definition at line 564 of file gnunet-search.c.

565{
566 if (sc != NULL)
567 {
569 sc = NULL;
570 }
574}

References dir_format_string_opt, format_string_opt, GNUNET_free, GNUNET_FS_search_stop(), meta_format_string_opt, and sc.

Here is the call graph for this function:

◆ timeout_task()

static void timeout_task ( void *const  cls)
static

Definition at line 578 of file gnunet-search.c.

579{
580 tt = NULL;
583}

References GNUNET_SCHEDULER_shutdown(), GNUNET_YES, stop_searching, and tt.

Here is the call graph for this function:

◆ run()

static void run ( void *const  cls,
char *const *const  args,
const char *const  cfgfile,
const struct GNUNET_CONFIGURATION_Handle *const  cfgarg 
)
static

Main function that will be run by the scheduler.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cfgargconfiguration

Definition at line 595 of file gnunet-search.c.

599{
600 struct GNUNET_FS_Uri *uri;
601 unsigned int argc;
603
605 {
606 fprintf (stderr,
607 _ ("Conflicting options --bookmark-only and --silent.\n"));
608 ret = 1;
609 return;
610 }
612 {
613 fprintf (stderr,
614 _ ("Conflicting options --bookmark-only and --output.\n"));
615 ret = 1;
616 return;
617 }
619 {
620 fprintf (stderr, _ ("An output file is mandatory for silent mode.\n"));
621 ret = 1;
622 return;
623 }
624 if (NULL == dir_format_string_opt)
628 else
630 if (NULL == format_string_opt)
633 else
635 if (NULL == meta_format_string_opt)
637 else
639 argc = 0;
640 while (NULL != args[argc])
641 argc++;
642 uri = GNUNET_FS_uri_ksk_create_from_args (argc, (const char **) args);
643 if (NULL == uri)
644 {
645 fprintf (stderr,
646 "%s",
647 _ ("Could not create keyword URI from arguments.\n"));
648 ret = 1;
649 return;
650 }
652 {
653 fprintf (stderr,
654 "%s",
655 _ ("Invalid URI. Valid URIs for searching are keyword query "
656 "URIs\n(\"gnunet://fs/ksk/...\") and namespace content URIs "
657 "(\"gnunet://fs/sks/...\").\n"));
659 ret = 1;
660 return;
661 }
662 if (bookmark_only)
663 {
664 char *bmstr = GNUNET_FS_uri_to_string (uri);
665 printf ("%s\n", bmstr);
666 GNUNET_free (bmstr);
668 ret = 0;
669 return;
670 }
671 cfg = cfgarg;
673 "gnunet-search",
675 NULL,
678 if (NULL == ctx)
679 {
680 fprintf (stderr, _ ("Could not initialize the `%s` subsystem.\n"), "FS");
682 ret = 1;
683 return;
684 }
685 if (output_filename != NULL)
688 if (local_only)
692 if (NULL == sc)
693 {
694 fprintf (stderr, "%s", _ ("Could not start searching.\n"));
696 ret = 1;
697 return;
698 }
699 if (0 != timeout.rel_value_us)
702}

References _, anonymity, bookmark_only, cfg, ctx, db, DEFAULT_DIR_FORMAT, DEFAULT_FILE_FORMAT, DEFAULT_META_FORMAT, dir_format_string, dir_format_string_opt, format_string, format_string_opt, GNUNET_free, GNUNET_FS_directory_builder_create(), GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END, GNUNET_FS_SEARCH_OPTION_LOOPBACK_ONLY, GNUNET_FS_SEARCH_OPTION_NONE, GNUNET_FS_search_start(), GNUNET_FS_start(), GNUNET_FS_stop(), GNUNET_FS_uri_destroy(), GNUNET_FS_uri_ksk_create_from_args(), GNUNET_FS_uri_test_ksk(), GNUNET_FS_uri_test_sks(), GNUNET_FS_uri_to_string(), GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_add_shutdown(), local_only, meta_format_string, meta_format_string_opt, options, output_filename, progress_cb(), GNUNET_TIME_Relative::rel_value_us, ret, sc, shutdown_task, silent_mode, timeout, timeout_task, tt, uri, VERB_DEFAULT_DIR_FORMAT, VERB_DEFAULT_FILE_FORMAT, and verbose.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const *  argv 
)

The main function to search GNUnet.

Parameters
argcnumber of arguments from the command line
argvcommand line arguments
Returns
0 ok, an error number on error

Definition at line 713 of file gnunet-search.c.

714{
717 'a',
718 "anonymity",
719 "LEVEL",
720 gettext_noop ("set the desired LEVEL of receiver-anonymity (default: "
721 "1)"),
722 &anonymity),
724 'b',
725 "bookmark-only",
726 gettext_noop ("do not search, print only the URI that points to this "
727 "search"),
730 'F',
731 "dir-printf",
732 "FORMAT",
733 gettext_noop ("write search results for directories according to "
734 "FORMAT; accepted placeholders are: %a, %f, %j, %l, %m, "
735 "%n, %s; defaults to the value of --printf when omitted "
736 "or to `" HELP_DEFAULT_DIR_FORMAT "` if --printf is "
737 "omitted too"),
740 'f',
741 "printf",
742 "FORMAT",
743 gettext_noop ("write search results according to FORMAT; accepted "
744 "placeholders are: %a, %f, %j, %l, %m, %n, %s; defaults "
745 "to `" HELP_DEFAULT_FILE_FORMAT "` when omitted"),
748 'i',
749 "iter-printf",
750 "FORMAT",
751 gettext_noop ("when the %a or %j placeholders appear in --printf or "
752 "--dir-printf, list each metadata property according to "
753 "FORMAT; accepted placeholders are: %i, %l, %n, %p"
754 HELP_EXTRACTOR_TEXTADD ", %w; defaults to `"
755 HELP_DEFAULT_META_FORMAT "` when omitted"),
758 "results",
759 "VALUE",
760 gettext_noop ("automatically terminate search "
761 "after VALUE results are found"),
764 'n',
765 "no-network",
766 gettext_noop ("only search the local peer (no P2P network search)"),
767 &local_only),
769 'o',
770 "output",
771 "FILENAME",
772 gettext_noop ("create a GNUnet directory with search results at "
773 "FILENAME (e.g. `gnunet-search --output=commons"
774 GNUNET_FS_DIRECTORY_EXT " commons`)"),
777 's',
778 "silent",
779 gettext_noop ("silent mode (requires the --output argument)"),
780 &silent_mode),
782 't',
783 "timeout",
784 "DELAY",
785 gettext_noop ("automatically terminate search after DELAY; the value "
786 "given must be a number followed by a space and a time "
787 "unit, for example \"500 ms\"; without a unit it defaults "
788 "to microseconds - 1000000 = 1 second; if 0 or omitted "
789 "it means to wait for CTRL-C"),
790 &timeout),
792 'V',
793 "verbose",
794 gettext_noop ("be verbose (append \"%a\\n\" to the default --printf and "
795 "--dir-printf arguments - ignored when these are provided "
796 "by the user)"),
797 &verbose),
799
800 if (GNUNET_SYSERR ==
802 argc,
803 argv,
804 "gnunet-search [OPTIONS] KEYWORD1 KEYWORD2 ...",
805 gettext_noop ("Search for files that have been "
806 "published on GNUnet\n"),
807 options,
808 &run,
809 NULL))
810 ret = 1;
811
812 return ret;
813}

References anonymity, bookmark_only, dir_format_string_opt, format_string_opt, gettext_noop, GNUNET_FS_DIRECTORY_EXT, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_flag(), GNUNET_GETOPT_option_increment_uint(), GNUNET_GETOPT_option_relative_time(), GNUNET_GETOPT_option_string(), GNUNET_GETOPT_option_uint(), GNUNET_OS_project_data_gnunet(), GNUNET_PROGRAM_run(), GNUNET_SYSERR, HELP_DEFAULT_DIR_FORMAT, HELP_DEFAULT_FILE_FORMAT, HELP_DEFAULT_META_FORMAT, HELP_EXTRACTOR_TEXTADD, local_only, meta_format_string_opt, options, output_filename, results_limit, ret, run(), silent_mode, timeout, and verbose.

Here is the call graph for this function:

Variable Documentation

◆ ret

int ret
static

Definition at line 81 of file gnunet-search.c.

Referenced by main(), and run().

◆ cfg

const struct GNUNET_CONFIGURATION_Handle* cfg
static

Definition at line 83 of file gnunet-search.c.

Referenced by run().

◆ ctx

struct GNUNET_FS_Handle* ctx
static

Definition at line 85 of file gnunet-search.c.

Referenced by clean_task(), and run().

◆ sc

◆ output_filename

char* output_filename
static

Definition at line 89 of file gnunet-search.c.

Referenced by clean_task(), main(), and run().

◆ format_string_opt

char* format_string_opt
static

Definition at line 91 of file gnunet-search.c.

Referenced by main(), run(), and shutdown_task().

◆ dir_format_string_opt

char* dir_format_string_opt
static

Definition at line 93 of file gnunet-search.c.

Referenced by main(), run(), and shutdown_task().

◆ meta_format_string_opt

char* meta_format_string_opt
static

Definition at line 95 of file gnunet-search.c.

Referenced by main(), run(), and shutdown_task().

◆ format_string

const char* format_string
static

Definition at line 97 of file gnunet-search.c.

Referenced by print_search_result(), and run().

◆ dir_format_string

const char* dir_format_string
static

Definition at line 99 of file gnunet-search.c.

Referenced by print_search_result(), and run().

◆ meta_format_string

const char* meta_format_string
static

Definition at line 101 of file gnunet-search.c.

Referenced by item_printer(), and run().

◆ db

struct GNUNET_FS_DirectoryBuilder* db
static

Definition at line 103 of file gnunet-search.c.

Referenced by check_patch_applied(), clean_task(), do_poll(), do_scheduler_notify(), download_cb(), GNUNET_CURL_download_get_result_(), GNUNET_PQ_connect2(), GNUNET_PQ_connect_with_cfg2(), GNUNET_PQ_disconnect(), GNUNET_PQ_eval_prepared_multi_select(), GNUNET_PQ_eval_prepared_non_select(), GNUNET_PQ_eval_prepared_singleton_select(), GNUNET_PQ_eval_result(), GNUNET_PQ_event_do_poll(), GNUNET_PQ_event_listen(), GNUNET_PQ_event_listen_cancel(), GNUNET_PQ_event_notify(), GNUNET_PQ_event_reconnect_(), GNUNET_PQ_exec_prepared(), GNUNET_PQ_exec_sql(), GNUNET_PQ_exec_statements(), GNUNET_PQ_get_oid_by_name(), GNUNET_PQ_prepare_once(), GNUNET_PQ_prepare_statements(), GNUNET_PQ_query_param_array_abs_time(), GNUNET_PQ_query_param_array_bool(), GNUNET_PQ_query_param_array_bytes(), GNUNET_PQ_query_param_array_bytes_same_size(), GNUNET_PQ_query_param_array_ptrs_abs_time(), GNUNET_PQ_query_param_array_ptrs_bytes(), GNUNET_PQ_query_param_array_ptrs_bytes_same_size(), GNUNET_PQ_query_param_array_ptrs_rel_time(), GNUNET_PQ_query_param_array_ptrs_string(), GNUNET_PQ_query_param_array_ptrs_timestamp(), GNUNET_PQ_query_param_array_rel_time(), GNUNET_PQ_query_param_array_string(), GNUNET_PQ_query_param_array_timestamp(), GNUNET_PQ_query_param_array_uint16(), GNUNET_PQ_query_param_array_uint32(), GNUNET_PQ_query_param_array_uint64(), GNUNET_PQ_reconnect(), GNUNET_PQ_reconnect_if_down(), GNUNET_PQ_result_spec_array_abs_time(), GNUNET_PQ_result_spec_array_bool(), GNUNET_PQ_result_spec_array_fixed_size(), GNUNET_PQ_result_spec_array_rel_time(), GNUNET_PQ_result_spec_array_string(), GNUNET_PQ_result_spec_array_timestamp(), GNUNET_PQ_result_spec_array_uint16(), GNUNET_PQ_result_spec_array_uint32(), GNUNET_PQ_result_spec_array_uint64(), GNUNET_PQ_result_spec_array_variable_size(), GNUNET_PQ_run_sql(), load_initial_oids(), manage_subscribe(), prepare_check_patch(), prepare_get_oid_by_name(), progress_cb(), publish_content(), register_notify(), reset_connection(), run(), and scheduler_fd_cb().

◆ anonymity

unsigned int anonymity = 1
static

Definition at line 105 of file gnunet-search.c.

Referenced by main(), and run().

◆ timeout

struct GNUNET_TIME_Relative timeout
static

Timeout for the search, 0 means to wait for CTRL-C.

Definition at line 110 of file gnunet-search.c.

Referenced by main(), and run().

◆ results_limit

unsigned int results_limit
static

Definition at line 112 of file gnunet-search.c.

Referenced by main(), and progress_cb().

◆ results

unsigned int results
static

◆ verbose

unsigned int verbose
static

Definition at line 116 of file gnunet-search.c.

Referenced by main(), and run().

◆ bookmark_only

int bookmark_only
static

Definition at line 118 of file gnunet-search.c.

Referenced by main(), and run().

◆ local_only

int local_only
static

Definition at line 120 of file gnunet-search.c.

Referenced by main(), and run().

◆ silent_mode

int silent_mode
static

Definition at line 122 of file gnunet-search.c.

Referenced by main(), progress_cb(), and run().

◆ tt

struct GNUNET_SCHEDULER_Task* tt
static

Definition at line 124 of file gnunet-search.c.

Referenced by run(), and timeout_task().

◆ stop_searching

int stop_searching
static

Definition at line 126 of file gnunet-search.c.

Referenced by progress_cb(), and timeout_task().