37#define GNUNET_SEARCH_log(kind, ...) \
38 GNUNET_log_from (kind, "gnunet-search", __VA_ARGS__)
43#define DEFAULT_DIR_FORMAT "#%n:\ngnunet-download -o \"%f\" -R %u\n\n"
44#define HELP_DEFAULT_DIR_FORMAT "#%n:\\ngnunet-download -o \"%f\" -R %u\\n\\n"
45#define DEFAULT_FILE_FORMAT "#%n:\ngnunet-download -o \"%f\" %u\n\n"
46#define HELP_DEFAULT_FILE_FORMAT "#%n:\\ngnunet-download -o \"%f\" %u\\n\\n"
47#define VERB_DEFAULT_DIR_FORMAT DEFAULT_DIR_FORMAT "%a\n"
48#define VERB_DEFAULT_FILE_FORMAT DEFAULT_FILE_FORMAT "%a\n"
51#define DEFAULT_META_FORMAT " %t: %p\n"
52#define HELP_DEFAULT_META_FORMAT " %t: %p\\n"
53#define HELP_EXTRACTOR_TEXTADD ", %t"
55#define DEFAULT_META_FORMAT " MetaType #%i: %p\n"
56#define HELP_DEFAULT_META_FORMAT " MetaType #%i: %p\\n"
57#define HELP_EXTRACTOR_TEXTADD ""
60#define GENERIC_DIRECTORY_NAME "collection"
61#define GENERIC_FILE_NAME "no-name"
62#define GENERIC_FILE_MIMETYPE "application/octet-stream"
144 const char *cursor = esc + 1;
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;
162 while (probe < 256 && isxdigit ((tmp = *++cursor)))
163 probe = (probe << 4) + tmp - (tmp > 96 ? 87 : tmp > 64 ? 55 : 48);
164 goto maybe_codepoint;
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;
175 case '\0': putchar (
'\\');
return cursor;
176 default: printf (
"\\%c", *cursor);
return cursor + 1;
183 fwrite (esc, 1, cursor - esc, stdout);
208 const enum EXTRACTOR_MetaType
type,
209 const enum EXTRACTOR_MetaFormat format,
210 const char *
const data_mime_type,
211 const char *
const data,
215 const char *next_spec;
216 const char *next_esc;
217#define info ((struct GNUNET_SEARCH_MetadataPrinterInfo *) cls)
218 if ((format != EXTRACTOR_METAFORMAT_UTF8 &&
219 format != EXTRACTOR_METAFORMAT_C_STRING) ||
227 next_spec = strchr (cursor,
'%');
228 next_esc = strchr (cursor,
'\\');
233 if (next_esc && (! next_spec || next_esc < next_spec))
235 if (next_esc > cursor)
236 fwrite (cursor, 1, next_esc - cursor, stdout);
239 next_esc = strchr (cursor,
'\\');
244 if (next_spec && (! next_esc || next_spec < next_esc))
246 if (next_spec > cursor)
247 fwrite (cursor, 1, next_spec - cursor, stdout);
249 switch (*++next_spec)
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;
260 EXTRACTOR_metatype_to_string (
type)));
264 case '\0': putchar (
'%');
return 0;
265 default: printf (
"%%%c", *next_spec);
break;
267 cursor = next_spec + 1;
268 next_spec = strchr (cursor,
'%');
273 printf (
"%s", cursor);
295 const unsigned int resultnum,
296 const int is_directory)
299 const char *cursor =
GNUNET_YES == is_directory ?
303 const char *next_spec = strchr (cursor,
'%');
304 const char *next_esc = strchr (cursor,
'\\');
310 if (next_esc && (! next_spec || next_esc < next_spec))
312 if (next_esc > cursor)
313 fwrite (cursor, 1, next_esc - cursor, stdout);
316 next_esc = strchr (cursor,
'\\');
321 if (next_spec && (! next_esc || next_spec < next_esc))
323 if (next_spec > cursor)
324 fwrite (cursor, 1, next_spec - cursor, stdout);
326 switch (*++next_spec)
352 (
long unsigned int) (
GNUNET_YES == is_directory ?
367 EXTRACTOR_METATYPE_MIMETYPE);
372 case 'n': printf (
"%u", resultnum);
break;
380 printf (
"%s", placeholder);
387 case '0':
case '1':
case '2':
case '3':
case '4':
388 case '5':
case '6':
case '7':
case '8':
case '9':
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;
410 goto not_a_specifier;
413 case '%': putchar (
'%');
break;
414 case '\0': putchar (
'%');
return;
417 default: printf (
"%%%c", *next_spec);
break;
419 cursor = next_spec + 1;
420 next_spec = strchr (cursor,
'%');
425 printf (
"%s", cursor);
455 _ (
"Failed to write directory with search results to "
481 static unsigned int cnt;
485 switch (
info->status)
497 info->value.search.specifics.result.uri,
498 info->value.search.specifics.result.meta,
506 info->value.search.specifics.result.meta,
509 info->value.search.specifics.result.meta);
522 info->value.search.specifics.result.uri,
523 info->value.search.specifics.result.meta,
544 _ (
"Error searching: %s.\n"),
545 info->value.search.specifics.error.message);
555 _ (
"Unexpected status: %d\n"),
596 char *
const *
const args,
597 const char *
const cfgfile,
607 _ (
"Conflicting options --bookmark-only and --silent.\n"));
614 _ (
"Conflicting options --bookmark-only and --output.\n"));
620 fprintf (stderr,
_ (
"An output file is mandatory for silent mode.\n"));
640 while (NULL !=
args[argc])
647 _ (
"Could not create keyword URI from arguments.\n"));
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"));
665 printf (
"%s\n", bmstr);
680 fprintf (stderr,
_ (
"Could not initialize the `%s` subsystem.\n"),
"FS");
694 fprintf (stderr,
"%s",
_ (
"Could not start searching.\n"));
713main (
int argc,
char *
const *argv)
720 gettext_noop (
"set the desired LEVEL of receiver-anonymity (default: "
726 gettext_noop (
"do not search, print only the URI that points to this "
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 "
743 gettext_noop (
"write search results according to FORMAT; accepted "
744 "placeholders are: %a, %f, %j, %l, %m, %n, %s; defaults "
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"
761 "after VALUE results are found"),
766 gettext_noop (
"only search the local peer (no P2P network search)"),
772 gettext_noop (
"create a GNUnet directory with search results at "
773 "FILENAME (e.g. `gnunet-search --output=commons"
779 gettext_noop (
"silent mode (requires the --output argument)"),
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"),
794 gettext_noop (
"be verbose (append \"%a\\n\" to the default --printf and "
795 "--dir-printf arguments - ignored when these are provided "
804 "gnunet-search [OPTIONS] KEYWORD1 KEYWORD2 ...",
806 "published on GNUnet\n"),
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
struct GNUNET_GETOPT_CommandLineOption options[]
#define gettext_noop(String)
#define dgettext(Domainname, Msgid)
static char * plugin_name
Name of our plugin.
static char * data
The data to insert into the dht.
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 ...
#define VERB_DEFAULT_DIR_FORMAT
static const char * meta_format_string
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
static void timeout_task(void *const cls)
#define HELP_DEFAULT_META_FORMAT
static void shutdown_task(void *const cls)
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)
#define GNUNET_SEARCH_log(kind,...)
static const char * format_string
static const struct GNUNET_CONFIGURATION_Handle * cfg
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 unsigned int results_limit
int main(int argc, char *const *argv)
The main function to search GNUnet.
#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
API for file sharing via GNUnet.
void GNUNET_DISK_filename_canonicalize(char *fn)
Removes special characters as ':' from a filename.
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.
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_remove(const char *filename)
Remove all files in a directory (rm -rf).
@ 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?
int GNUNET_FS_uri_test_ksk(const struct GNUNET_FS_Uri *uri)
Is this a keyword URI?
#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.
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.
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.
void GNUNET_FS_search_stop(struct GNUNET_FS_SearchContext *sc)
Stop search for content.
void GNUNET_FS_uri_destroy(struct GNUNET_FS_Uri *uri)
Free URI.
int GNUNET_FS_uri_test_sks(const struct GNUNET_FS_Uri *uri)
Is this a namespace URI?
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.
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.
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...
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.
#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.
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,...
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
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,...
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.
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.
Internal state of a directory builder.
Master context for most FS operations.
Argument given to the progress callback with information about what is going on.
Handle for controlling a search.
A Universal Resource Identifier (URI), opaque.
Definition of a command line option.
Entry in list of pending tasks.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.