GNUnet  0.17.6
Data Structures | Macros | Enumerations | Functions | Variables
gnunet-search.c File Reference

searching for files on GNUnet More...

#include <ctype.h>
#include <inttypes.h>
#include <limits.h>
#include "platform.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. More...
 
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. More...
 
static void print_search_result (const char *const filename, const struct GNUNET_FS_Uri *const uri, const struct GNUNET_CONTAINER_MetaData *const metadata, const unsigned int resultnum, const int is_directory)
 Print a search result according to the current formats. More...
 
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. More...
 
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. More...
 
int main (int argc, char *const *argv)
 The main function to search GNUnet. More...
 

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
 
static char * dir_format_string
 
static 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. More...
 
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 36 of file gnunet-search.c.

◆ DEFAULT_DIR_FORMAT

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

Definition at line 42 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 43 of file gnunet-search.c.

◆ DEFAULT_FILE_FORMAT

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

Definition at line 44 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 45 of file gnunet-search.c.

◆ VERB_DEFAULT_DIR_FORMAT

#define VERB_DEFAULT_DIR_FORMAT   DEFAULT_DIR_FORMAT "%a\n"

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

◆ VERB_DEFAULT_FILE_FORMAT

#define VERB_DEFAULT_FILE_FORMAT   DEFAULT_FILE_FORMAT "%a\n"

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

◆ DEFAULT_META_FORMAT

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

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

◆ HELP_DEFAULT_META_FORMAT

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

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

◆ HELP_EXTRACTOR_TEXTADD

#define HELP_EXTRACTOR_TEXTADD   ""

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

◆ GENERIC_DIRECTORY_NAME

#define GENERIC_DIRECTORY_NAME   "collection"

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

◆ GENERIC_FILE_NAME

#define GENERIC_FILE_NAME   "no-name"

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

◆ GENERIC_FILE_MIMETYPE

#define GENERIC_FILE_MIMETYPE   "application/octet-stream"

Definition at line 61 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 64 of file gnunet-search.c.

64  {
68 };
@ METADATA_PRINTER_FLAG_HAVE_TYPE
Definition: gnunet-search.c:67
@ METADATA_PRINTER_FLAG_NONE
Definition: gnunet-search.c:65
@ METADATA_PRINTER_FLAG_ONE_RUN
Definition: gnunet-search.c:66

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 132 of file gnunet-search.c.

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

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 197 of file gnunet-search.c.

204 {
205 #define info ((struct GNUNET_SEARCH_MetadataPrinterInfo *) cls)
206  if ((format != EXTRACTOR_METAFORMAT_UTF8 &&
207  format != EXTRACTOR_METAFORMAT_C_STRING) ||
209  return 0;
210  info->counter++;
211  if ((info->flags & METADATA_PRINTER_FLAG_HAVE_TYPE) && type != info->type)
212  return 0;
213 
214  const char *cursor = meta_format_string;
215  const char *next_spec = strchr(cursor, '%');
216  const char *next_esc = strchr(cursor, '\\');
217 
218 parse_format:
219 
220  /* If an escape sequence exists before the next format specifier... */
221  if (next_esc && (!next_spec || next_esc < next_spec))
222  {
223  if (next_esc > cursor)
224  fwrite (cursor, 1, next_esc - cursor, stdout);
225 
226  cursor = print_escape_sequence (next_esc);
227  next_esc = strchr(cursor, '\\');
228  goto parse_format;
229  }
230 
231  /* If a format specifier exists before the next escape sequence... */
232  if (next_spec && (!next_esc || next_spec < next_esc))
233  {
234  if (next_spec > cursor)
235  fwrite (cursor, 1, next_spec - cursor, stdout);
236 
237  switch (*++next_spec)
238  {
239  case '%': putchar('%'); break;
240  case 'i': printf ("%d", type); break;
241  case 'l': printf ("%lu", (long unsigned int) data_size); break;
242  case 'n': printf ("%u", info->counter); break;
243  case 'p': printf ("%s", data); break;
244 #if HAVE_LIBEXTRACTOR
245  case 't':
246  printf ("%s",
248  EXTRACTOR_metatype_to_string (type)));
249  break;
250 #endif
251  case 'w': printf ("%s", plugin_name); break;
252  case '\0': putchar('%'); return 0;
253  default: printf ("%%%c", *next_spec); break;
254  }
255  cursor = next_spec + 1;
256  next_spec = strchr(cursor, '%');
257  goto parse_format;
258  }
259 
260  if (*cursor)
261  printf ("%s", cursor);
262 
263  return info->flags & METADATA_PRINTER_FLAG_ONE_RUN;
264 #undef info
265 }
#define dgettext(Domainname, Msgid)
Definition: gettext.h:46
static size_t data_size
Number of bytes in data.
Definition: gnunet-abd.c:187
uint32_t data
The data value.
#define info
static char * meta_format_string
Definition: gnunet-search.c:92
static const char * print_escape_sequence(const char *const esc)
Print the escape sequence at the beginning of a string.
static char * plugin_name
Name of our plugin.
@ EXTRACTOR_METAFORMAT_C_STRING
0-terminated string.
@ EXTRACTOR_METAFORMAT_UTF8
0-terminated, UTF-8 encoded string.
@ EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME
#define LIBEXTRACTOR_GETTEXT_DOMAIN
Definition: platform.h:178
enum GNUNET_TESTBED_UnderlayLinkModelType type
the type of this model

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_CONTAINER_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_CONTAINER_MetaData associated with this result
resultnumthe result number
is_directoryGNUNET_YES if this is a directory, otherwise GNUNET_NO
Author
madmurphy

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

285 {
286 
287  const char *cursor = GNUNET_YES == is_directory ?
289  : format_string;
290 
291  const char *next_spec = strchr(cursor, '%');
292  const char *next_esc = strchr(cursor, '\\');
293  char *placeholder;
295 
296 parse_format:
297  /* If an escape sequence exists before the next format specifier... */
298  if (next_esc && (!next_spec || next_esc < next_spec))
299  {
300  if (next_esc > cursor)
301  fwrite (cursor, 1, next_esc - cursor, stdout);
302 
303  cursor = print_escape_sequence (next_esc);
304  next_esc = strchr(cursor, '\\');
305  goto parse_format;
306  }
307 
308  /* If a format specifier exists before the next escape sequence... */
309  if (next_spec && (!next_esc || next_spec < next_esc))
310  {
311  if (next_spec > cursor)
312  fwrite (cursor, 1, next_spec - cursor, stdout);
313 
314  switch (*++next_spec)
315  {
316  /* All metadata fields */
317  case 'a':
319 
320 iterate_meta:
321  info.counter = 0;
323  break;
324  /* File's name */
325  case 'f':
326  if (GNUNET_YES == is_directory)
327  {
328  printf ("%s%s", filename, GNUNET_FS_DIRECTORY_EXT);
329  break;
330  }
331  printf ("%s", filename);
332  break;
333  /* Only the first metadata field */
334  case 'j':
336  goto iterate_meta;
337  /* File name's length */
338  case 'l':
339  printf ("%lu",
340  (long unsigned int) ( GNUNET_YES == is_directory ?
341  strlen(filename) +
342  (sizeof(GNUNET_FS_DIRECTORY_EXT) - 1)
343  :
344  strlen(filename)));
345  break;
346  /* File's mime type */
347  case 'm':
348  if (GNUNET_YES == is_directory)
349  {
350  printf ("%s", GNUNET_FS_DIRECTORY_MIME);
351  break;
352  }
354  metadata,
356  printf ("%s", placeholder ? placeholder : GENERIC_FILE_MIMETYPE);
357  GNUNET_free (placeholder);
358  break;
359  /* Result number */
360  case 'n': printf ("%u", resultnum); break;
361  /* File's size */
362  case 's':
363  printf ("%" PRIu64, GNUNET_FS_uri_chk_get_file_size (uri));
364  break;
365  /* File's URI */
366  case 'u':
367  placeholder = GNUNET_FS_uri_to_string (uri);
368  printf ("%s", placeholder);
369  GNUNET_free (placeholder);
370  break;
371 
372  /* We can add as many cases as we want here... */
373 
374  /* Handle `%123#a` and `%123#j` (e.g. `%5#j` is a book title) */
375  case '0': case '1': case '2': case '3': case '4':
376  case '5': case '6': case '7': case '8': case '9':
377  cursor = next_spec;
378  info.type = *cursor - 48;
379  while (isdigit(*++cursor) && info.type < (INT_MAX - *cursor + 48) / 10)
380  info.type = info.type * 10 + *cursor - 48;
381  if (info.type == 0 || *cursor != '#')
382  goto not_a_specifier;
383  switch (*++cursor)
384  {
385  /* All metadata fields of type `info.type` */
386  case 'a':
387  next_spec = cursor;
389  goto iterate_meta;
390 
391  /* Only the first metadata field of type `info.type` */
392  case 'j':
393  next_spec = cursor;
396  goto iterate_meta;
397  }
398  goto not_a_specifier;
399 
400  /* All other cases */
401  case '%': putchar('%'); break;
402  case '\0': putchar('%'); return;
403 
404 not_a_specifier:
405  default: printf ("%%%c", *next_spec); break;
406  }
407  cursor = next_spec + 1;
408  next_spec = strchr(cursor, '%');
409  goto parse_format;
410  }
411 
412  if (*cursor)
413  printf ("%s", cursor);
414 }
#define INT_MAX
static char * filename
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 char * dir_format_string
Definition: gnunet-search.c:90
#define GENERIC_FILE_MIMETYPE
Definition: gnunet-search.c:61
static char * format_string
Definition: gnunet-search.c:88
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.
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:1376
#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:2033
#define GNUNET_FS_DIRECTORY_EXT
@ GNUNET_YES
@ EXTRACTOR_METATYPE_MIMETYPE
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_CONTAINER_meta_data_get_by_type(const struct GNUNET_CONTAINER_MetaData *md, enum EXTRACTOR_MetaType type)
Get the first MD entry of the given type.
int GNUNET_CONTAINER_meta_data_iterate(const struct GNUNET_CONTAINER_MetaData *md, EXTRACTOR_MetaDataProcessor iter, void *iter_cls)
Iterate over MD entries.

References dir_format_string, EXTRACTOR_METATYPE_MIMETYPE, filename, format_string, GENERIC_FILE_MIMETYPE, GNUNET_CONTAINER_meta_data_get_by_type(), GNUNET_CONTAINER_meta_data_iterate(), GNUNET_free, GNUNET_FS_DIRECTORY_EXT, GNUNET_FS_DIRECTORY_MIME, 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 418 of file gnunet-search.c.

419 {
420  size_t dsize;
421  void *ddata;
422 
424  ctx = NULL;
425  if (output_filename == NULL)
426  return;
427  if (GNUNET_OK !=
428  GNUNET_FS_directory_builder_finish (db, &dsize, &ddata))
429  {
430  GNUNET_break (0);
432  return;
433  }
435  if (GNUNET_OK !=
437  ddata,
438  dsize,
441  {
443  _ ("Failed to write directory with search results to "
444  "`%s'\n"),
446  }
447  GNUNET_free (ddata);
449 }
static char * output_filename
Definition: gnunet-search.c:86
#define GNUNET_SEARCH_log(kind,...)
Definition: gnunet-search.c:36
static struct GNUNET_FS_Handle * ctx
Definition: gnunet-search.c:82
static struct GNUNET_FS_DirectoryBuilder * db
Definition: gnunet-search.c:94
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:725
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -rf).
Definition: disk.c:1085
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
int GNUNET_FS_directory_builder_finish(struct GNUNET_FS_DirectoryBuilder *bld, size_t *rsize, void **rdata)
Finish building the directory.
Definition: fs_directory.c:577
void GNUNET_FS_stop(struct GNUNET_FS_Handle *h)
Close our connection with the file-sharing service.
Definition: fs_api.c:3321
@ GNUNET_OK
Definition: gnunet_common.h:99
#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 _(String)
GNU gettext support macro.
Definition: platform.h:177

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 466 of file gnunet-search.c.

468 {
469  static unsigned int cnt;
470  int is_directory;
471  char *filename;
472 
473  switch (info->status)
474  {
476  break;
477 
479  if (stop_searching)
480  break;
481 
482  if (db != NULL)
484  db,
485  info->value.search.specifics.result.uri,
486  info->value.search.specifics.result.meta,
487  NULL);
488 
489  if (silent_mode)
490  break;
491 
492  cnt++;
494  info->value.search.specifics.result.meta,
497  info->value.search.specifics.result.meta);
498  if (NULL != filename)
499  {
500  while ((filename[0] != '\0') && ('/' == filename[strlen (filename) - 1]))
501  filename[strlen (filename) - 1] = '\0';
503  }
505  filename
506  : is_directory ?
508  :
510  info->value.search.specifics.result.uri,
511  info->value.search.specifics.result.meta,
512  cnt,
513  is_directory);
514  fflush (stdout);
516  results++;
517  if ((results_limit > 0) && (results >= results_limit))
518  {
520  /* otherwise the function might keep printing results for a while... */
522  }
523  break;
524 
527  /* ignore */
528  break;
529 
532  _ ("Error searching: %s.\n"),
533  info->value.search.specifics.error.message);
535  break;
536 
539  break;
540 
541  default:
543  _ ("Unexpected status: %d\n"),
544  info->status);
545  break;
546  }
547  return NULL;
548 }
#define GENERIC_FILE_NAME
Definition: gnunet-search.c:60
static void clean_task(void *const cls)
static int silent_mode
static void print_search_result(const char *const filename, const struct GNUNET_FS_Uri *const uri, const struct GNUNET_CONTAINER_MetaData *const metadata, const unsigned int resultnum, const int is_directory)
Print a search result according to the current formats.
static unsigned int results
static int stop_searching
static unsigned int results_limit
#define GENERIC_DIRECTORY_NAME
Definition: gnunet-search.c:59
void GNUNET_DISK_filename_canonicalize(char *fn)
Removes special characters as ':' from a filename.
Definition: disk.c:1189
int GNUNET_FS_meta_data_test_for_directory(const struct GNUNET_CONTAINER_MetaData *md)
Does the meta-data claim that this is a directory? Checks if the mime-type is that of a GNUnet direct...
Definition: fs_directory.c:54
void GNUNET_FS_directory_builder_add(struct GNUNET_FS_DirectoryBuilder *bld, const struct GNUNET_FS_Uri *uri, const struct GNUNET_CONTAINER_MetaData *md, const void *data)
Add an entry to a directory.
Definition: fs_directory.c:391
@ 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...
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:533
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:1281

References _, clean_task(), db, EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, filename, GENERIC_DIRECTORY_NAME, GENERIC_FILE_NAME, GNUNET_CONTAINER_meta_data_get_by_type(), GNUNET_DISK_filename_canonicalize(), GNUNET_ERROR_TYPE_ERROR, GNUNET_free, GNUNET_FS_directory_builder_add(), 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, 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 552 of file gnunet-search.c.

553 {
554  if (sc != NULL)
555  {
557  sc = NULL;
558  }
559 }
static struct GNUNET_FS_SearchContext * sc
Definition: gnunet-search.c:84
void GNUNET_FS_search_stop(struct GNUNET_FS_SearchContext *sc)
Stop search for content.
Definition: fs_search.c:1765

References GNUNET_FS_search_stop(), and sc.

Referenced by run().

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

◆ timeout_task()

static void timeout_task ( void *const  cls)
static

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

564 {
565  tt = NULL;
568 }
static struct GNUNET_SCHEDULER_Task * tt

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

Referenced by run().

Here is the call graph for this function:
Here is the caller 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 580 of file gnunet-search.c.

584 {
585  struct GNUNET_FS_Uri *uri;
586  unsigned int argc;
588 
589  if (silent_mode && bookmark_only)
590  {
591  fprintf (stderr,
592  _ ("Conflicting options --bookmark-only and --silent.\n"));
593  ret = 1;
594  return;
595  }
597  {
598  fprintf (stderr,
599  _ ("Conflicting options --bookmark-only and --output.\n"));
600  ret = 1;
601  return;
602  }
604  {
605  fprintf (stderr, _ ("An output file is mandatory for silent mode.\n"));
606  ret = 1;
607  return;
608  }
609  if (NULL == dir_format_string)
613  if (NULL == format_string)
616  if (NULL == meta_format_string)
618  argc = 0;
619  while (NULL != args[argc])
620  argc++;
621  uri = GNUNET_FS_uri_ksk_create_from_args (argc, (const char **) args);
622  if (NULL == uri)
623  {
624  fprintf (stderr,
625  "%s",
626  _ ("Could not create keyword URI from arguments.\n"));
627  ret = 1;
628  return;
629  }
631  {
632  fprintf (stderr,
633  "%s",
634  _ ("Invalid URI. Valid URIs for searching are keyword query "
635  "URIs\n(\"gnunet://fs/ksk/...\") and namespace content URIs "
636  "(\"gnunet://fs/sks/...\").\n"));
638  ret = 1;
639  return;
640  }
641  if (bookmark_only)
642  {
643  char * bmstr = GNUNET_FS_uri_to_string (uri);
644  printf ("%s\n", bmstr);
645  GNUNET_free (bmstr);
647  ret = 0;
648  return;
649  }
650  cfg = cfgarg;
652  "gnunet-search",
653  &progress_cb,
654  NULL,
657  if (NULL == ctx)
658  {
659  fprintf (stderr, _ ("Could not initialize the `%s` subsystem.\n"), "FS");
661  ret = 1;
662  return;
663  }
664  if (output_filename != NULL)
667  if (local_only)
671  if (NULL == sc)
672  {
673  fprintf (stderr, "%s", _ ("Could not start searching.\n"));
675  ret = 1;
676  return;
677  }
678  if (0 != timeout.rel_value_us)
681 }
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define VERB_DEFAULT_DIR_FORMAT
Definition: gnunet-search.c:46
#define DEFAULT_META_FORMAT
Definition: gnunet-search.c:54
static unsigned int verbose
#define DEFAULT_DIR_FORMAT
Definition: gnunet-search.c:42
static void timeout_task(void *const cls)
static void shutdown_task(void *const cls)
static unsigned int anonymity
Definition: gnunet-search.c:96
static const struct GNUNET_CONFIGURATION_Handle * cfg
Definition: gnunet-search.c:80
static int bookmark_only
static int ret
Definition: gnunet-search.c:78
static struct GNUNET_TIME_Relative timeout
Timeout for the search, 0 means to wait for CTRL-C.
#define DEFAULT_FILE_FORMAT
Definition: gnunet-search.c:44
static int local_only
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
Definition: gnunet-search.c:47
GNUNET_FS_SearchOptions
Options for searching.
int GNUNET_FS_uri_test_ksk(const struct GNUNET_FS_Uri *uri)
Is this a keyword URI?
Definition: fs_uri.c:1340
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:1160
void GNUNET_FS_uri_destroy(struct GNUNET_FS_Uri *uri)
Free URI.
Definition: fs_uri.c:684
int GNUNET_FS_uri_test_sks(const struct GNUNET_FS_Uri *uri)
Is this a namespace URI?
Definition: fs_uri.c:1287
struct GNUNET_FS_DirectoryBuilder * GNUNET_FS_directory_builder_create(const struct GNUNET_CONTAINER_MetaData *mdir)
Create a directory builder.
Definition: fs_directory.c:365
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:3246
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:1606
@ 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.
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:1316
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:1254
A Universal Resource Identifier (URI), opaque.
Definition: fs_api.h:166
uint64_t rel_value_us
The actual value.

References _, anonymity, consensus-simulation::args, bookmark_only, cfg, ctx, db, DEFAULT_DIR_FORMAT, DEFAULT_FILE_FORMAT, DEFAULT_META_FORMAT, dir_format_string, format_string, 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, 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 692 of file gnunet-search.c.

693 {
696  'a',
697  "anonymity",
698  "LEVEL",
699  gettext_noop ("set the desired LEVEL of receiver-anonymity (default: "
700  "1)"),
701  &anonymity),
703  'b',
704  "bookmark-only",
705  gettext_noop ("do not search, print only the URI that points to this "
706  "search"),
707  &bookmark_only),
709  'F',
710  "dir-printf",
711  "FORMAT",
712  gettext_noop ("write search results for directories according to "
713  "FORMAT; accepted placeholders are: %a, %f, %j, %l, %m, "
714  "%n, %s; defaults to the value of --printf when omitted "
715  "or to `" HELP_DEFAULT_DIR_FORMAT "` if --printf is "
716  "omitted too"),
719  'f',
720  "printf",
721  "FORMAT",
722  gettext_noop ("write search results according to FORMAT; accepted "
723  "placeholders are: %a, %f, %j, %l, %m, %n, %s; defaults "
724  "to `" HELP_DEFAULT_FILE_FORMAT "` when omitted"),
725  &format_string),
727  'i',
728  "iter-printf",
729  "FORMAT",
730  gettext_noop ("when the %a or %j placeholders appear in --printf or "
731  "--dir-printf, list each metadata property according to "
732  "FORMAT; accepted placeholders are: %i, %l, %n, %p"
733  HELP_EXTRACTOR_TEXTADD ", %w; defaults to `"
734  HELP_DEFAULT_META_FORMAT "` when omitted"),
737  "results",
738  "VALUE",
739  gettext_noop ("automatically terminate search "
740  "after VALUE results are found"),
741  &results_limit),
743  'n',
744  "no-network",
745  gettext_noop ("only search the local peer (no P2P network search)"),
746  &local_only),
748  'o',
749  "output",
750  "FILENAME",
751  gettext_noop ("create a GNUnet directory with search results at "
752  "FILENAME (e.g. `gnunet-search --output=commons"
753  GNUNET_FS_DIRECTORY_EXT " commons`)"),
754  &output_filename),
756  's',
757  "silent",
758  gettext_noop ("silent mode (requires the --output argument)"),
759  &silent_mode),
761  't',
762  "timeout",
763  "DELAY",
764  gettext_noop ("automatically terminate search after DELAY; the value "
765  "given must be a number followed by a space and a time "
766  "unit, for example \"500 ms\"; without a unit it defaults "
767  "to microseconds - 1000000 = 1 second; if 0 or omitted "
768  "it means to wait for CTRL-C"),
769  &timeout),
771  'V',
772  "verbose",
773  gettext_noop ("be verbose (append \"%a\\n\" to the default --printf and "
774  "--dir-printf arguments - ignored when these are provided "
775  "by the user)"),
776  &verbose),
778 
779  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
780  return 12;
781 
782  if (GNUNET_SYSERR ==
783  GNUNET_PROGRAM_run (argc,
784  argv,
785  "gnunet-search [OPTIONS] KEYWORD1 KEYWORD2 ...",
786  gettext_noop ("Search for files that have been "
787  "published on GNUnet\n"),
788  options,
789  &run,
790  NULL))
791  ret = 1;
792 
793  GNUNET_free_nz ((void *) argv);
794  return ret;
795 }
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
#define gettext_noop(String)
Definition: gettext.h:69
#define HELP_DEFAULT_FILE_FORMAT
Definition: gnunet-search.c:45
#define HELP_DEFAULT_DIR_FORMAT
Definition: gnunet-search.c:43
#define HELP_DEFAULT_META_FORMAT
Definition: gnunet-search.c:55
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.
#define HELP_EXTRACTOR_TEXTADD
Definition: gnunet-search.c:56
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).
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_SYSERR
Definition: gnunet_common.h:97
#define GNUNET_free_nz(ptr)
Wrapper around free.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(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:399
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1223
Definition of a command line option.

References anonymity, bookmark_only, dir_format_string, format_string, gettext_noop, GNUNET_free_nz, 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_OK, GNUNET_PROGRAM_run(), GNUNET_STRINGS_get_utf8_args(), GNUNET_SYSERR, HELP_DEFAULT_DIR_FORMAT, HELP_DEFAULT_FILE_FORMAT, HELP_DEFAULT_META_FORMAT, HELP_EXTRACTOR_TEXTADD, local_only, meta_format_string, 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 78 of file gnunet-search.c.

Referenced by main(), and run().

◆ cfg

const struct GNUNET_CONFIGURATION_Handle* cfg
static

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

Referenced by run().

◆ ctx

struct GNUNET_FS_Handle* ctx
static

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

Referenced by clean_task(), and run().

◆ sc

struct GNUNET_FS_SearchContext* sc
static

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

Referenced by call_completion_task(), connect_cb(), continue_writing(), database_connect(), decrypt_block_with_keyword(), deserialize_search(), deserialize_search_file(), deserialize_search_result(), disconnect_cb(), do_reconnect(), extract_box_cb(), find_opc(), find_sender_by_address(), free_search_context(), GNUNET_FS_search_continue(), GNUNET_FS_search_make_status_(), GNUNET_FS_search_pause(), GNUNET_FS_search_signal_suspend_(), GNUNET_FS_search_start_searching_(), GNUNET_FS_search_stop(), GNUNET_FS_search_sync_(), GNUNET_HELLO_extract_address(), GNUNET_PEERSTORE_disconnect(), GNUNET_PEERSTORE_store(), GNUNET_PEERSTORE_store_cancel(), GNUNET_TESTBED_get_statistics(), GNUNET_TESTING_service_run(), GNUNET_TRANSPORT_TESTING_large_send(), GNUNET_TRANSPORT_TESTING_simple_send(), handle_client_start_search(), handle_datastore_reply(), handle_request(), handle_result(), init_database(), iteration_completion_cb(), iterator_cb(), locate_by_id(), notify_client_chk_result(), notify_client_chk_update(), op_done_task(), opc_search_iterator(), oprelease_get_stats(), opstart_get_stats(), process_kblock(), process_ksk_result(), process_sblock(), process_sks_result(), reconnect(), refresh_timeout_task(), run(), schedule_transmit_search_request(), search_iterator(), search_mq_error_handler(), search_rcop(), search_result_stop(), search_result_suspend(), search_start(), select_best_pending_from_link(), server_access_cb(), server_disconnect_cb(), server_lookup_connection(), server_send_callback(), service_run_main(), setup_sender(), shutdown_task(), signal_result_resume(), signal_search_resume(), signal_socks_failure(), store_request_sent(), timeout_cadet_task(), transmit_on_queue(), try_reconnect(), and update_sre_result_maps().

◆ output_filename

char* output_filename
static

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

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

◆ format_string

char* format_string
static

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

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

◆ dir_format_string

char* dir_format_string
static

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

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

◆ meta_format_string

char* meta_format_string
static

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

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

◆ db

struct GNUNET_FS_DirectoryBuilder* db
static

◆ anonymity

unsigned int anonymity = 1
static

Definition at line 96 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 96 of file gnunet-search.c.

Referenced by main(), and run().

◆ results_limit

unsigned int results_limit
static

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

Referenced by main(), and progress_cb().

◆ results

unsigned int results
static

◆ verbose

unsigned int verbose
static

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

Referenced by main(), and run().

◆ bookmark_only

int bookmark_only
static

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

Referenced by main(), and run().

◆ local_only

int local_only
static

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

Referenced by main(), and run().

◆ silent_mode

int silent_mode
static

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

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

◆ tt

struct GNUNET_SCHEDULER_Task* tt
static

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

Referenced by run(), and timeout_task().

◆ stop_searching

int stop_searching
static

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

Referenced by progress_cb(), and timeout_task().