aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-search.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/gnunet-search.c')
-rw-r--r--src/fs/gnunet-search.c125
1 files changed, 64 insertions, 61 deletions
diff --git a/src/fs/gnunet-search.c b/src/fs/gnunet-search.c
index 54f0095ea..a72cf97cc 100644
--- a/src/fs/gnunet-search.c
+++ b/src/fs/gnunet-search.c
@@ -26,15 +26,16 @@
26 * @author Igor Wronsky 26 * @author Igor Wronsky
27 * @author madmurphy 27 * @author madmurphy
28 */ 28 */
29#include "platform.h"
29#include <ctype.h> 30#include <ctype.h>
30#include <inttypes.h> 31#include <inttypes.h>
31#include <limits.h> 32#include <limits.h>
32#include "platform.h" 33
33#include "gnunet_fs_service.h" 34#include "gnunet_fs_service.h"
34 35
35 36
36#define GNUNET_SEARCH_log(kind, ...) \ 37#define GNUNET_SEARCH_log(kind, ...) \
37 GNUNET_log_from(kind, "gnunet-search", __VA_ARGS__) 38 GNUNET_log_from (kind, "gnunet-search", __VA_ARGS__)
38 39
39 40
40/* The default settings that we use for the printed output */ 41/* The default settings that we use for the printed output */
@@ -61,14 +62,16 @@
61#define GENERIC_FILE_MIMETYPE "application/octet-stream" 62#define GENERIC_FILE_MIMETYPE "application/octet-stream"
62 63
63 64
64enum GNUNET_SEARCH_MetadataPrinterFlags { 65enum GNUNET_SEARCH_MetadataPrinterFlags
66{
65 METADATA_PRINTER_FLAG_NONE = 0, 67 METADATA_PRINTER_FLAG_NONE = 0,
66 METADATA_PRINTER_FLAG_ONE_RUN = 1, 68 METADATA_PRINTER_FLAG_ONE_RUN = 1,
67 METADATA_PRINTER_FLAG_HAVE_TYPE = 2 69 METADATA_PRINTER_FLAG_HAVE_TYPE = 2
68}; 70};
69 71
70 72
71struct GNUNET_SEARCH_MetadataPrinterInfo { 73struct GNUNET_SEARCH_MetadataPrinterInfo
74{
72 unsigned int counter; 75 unsigned int counter;
73 unsigned int flags; 76 unsigned int flags;
74 int type; 77 int type;
@@ -132,7 +135,7 @@ static const char *
132print_escape_sequence (const char *const esc) 135print_escape_sequence (const char *const esc)
133{ 136{
134 unsigned int probe; 137 unsigned int probe;
135 const char * cursor = esc + 1; 138 const char *cursor = esc + 1;
136 char tmp; 139 char tmp;
137 switch (*cursor) 140 switch (*cursor)
138 { 141 {
@@ -150,7 +153,7 @@ print_escape_sequence (const char *const esc)
150 /* Possibly hexadecimal code point */ 153 /* Possibly hexadecimal code point */
151 case 'x': 154 case 'x':
152 probe = 0; 155 probe = 0;
153 while (probe < 256 && isxdigit((tmp = *++cursor))) 156 while (probe < 256 && isxdigit ((tmp = *++cursor)))
154 probe = (probe << 4) + tmp - (tmp > 96 ? 87 : tmp > 64 ? 55 : 48); 157 probe = (probe << 4) + tmp - (tmp > 96 ? 87 : tmp > 64 ? 55 : 48);
155 goto maybe_codepoint; 158 goto maybe_codepoint;
156 159
@@ -167,7 +170,7 @@ print_escape_sequence (const char *const esc)
167 default: printf ("\\%c", *cursor); return cursor + 1; 170 default: printf ("\\%c", *cursor); return cursor + 1;
168 } 171 }
169 172
170maybe_codepoint: 173 maybe_codepoint:
171 if (probe < 256) 174 if (probe < 256)
172 putchar (probe); 175 putchar (probe);
173 else 176 else
@@ -212,31 +215,31 @@ item_printer (void *const cls,
212 return 0; 215 return 0;
213 216
214 const char *cursor = meta_format_string; 217 const char *cursor = meta_format_string;
215 const char *next_spec = strchr(cursor, '%'); 218 const char *next_spec = strchr (cursor, '%');
216 const char *next_esc = strchr(cursor, '\\'); 219 const char *next_esc = strchr (cursor, '\\');
217 220
218parse_format: 221 parse_format:
219 222
220 /* If an escape sequence exists before the next format specifier... */ 223 /* If an escape sequence exists before the next format specifier... */
221 if (next_esc && (!next_spec || next_esc < next_spec)) 224 if (next_esc && (! next_spec || next_esc < next_spec))
222 { 225 {
223 if (next_esc > cursor) 226 if (next_esc > cursor)
224 fwrite (cursor, 1, next_esc - cursor, stdout); 227 fwrite (cursor, 1, next_esc - cursor, stdout);
225 228
226 cursor = print_escape_sequence (next_esc); 229 cursor = print_escape_sequence (next_esc);
227 next_esc = strchr(cursor, '\\'); 230 next_esc = strchr (cursor, '\\');
228 goto parse_format; 231 goto parse_format;
229 } 232 }
230 233
231 /* If a format specifier exists before the next escape sequence... */ 234 /* If a format specifier exists before the next escape sequence... */
232 if (next_spec && (!next_esc || next_spec < next_esc)) 235 if (next_spec && (! next_esc || next_spec < next_esc))
233 { 236 {
234 if (next_spec > cursor) 237 if (next_spec > cursor)
235 fwrite (cursor, 1, next_spec - cursor, stdout); 238 fwrite (cursor, 1, next_spec - cursor, stdout);
236 239
237 switch (*++next_spec) 240 switch (*++next_spec)
238 { 241 {
239 case '%': putchar('%'); break; 242 case '%': putchar ('%'); break;
240 case 'i': printf ("%d", type); break; 243 case 'i': printf ("%d", type); break;
241 case 'l': printf ("%lu", (long unsigned int) data_size); break; 244 case 'l': printf ("%lu", (long unsigned int) data_size); break;
242 case 'n': printf ("%u", info->counter); break; 245 case 'n': printf ("%u", info->counter); break;
@@ -249,11 +252,11 @@ parse_format:
249 break; 252 break;
250#endif 253#endif
251 case 'w': printf ("%s", plugin_name); break; 254 case 'w': printf ("%s", plugin_name); break;
252 case '\0': putchar('%'); return 0; 255 case '\0': putchar ('%'); return 0;
253 default: printf ("%%%c", *next_spec); break; 256 default: printf ("%%%c", *next_spec); break;
254 } 257 }
255 cursor = next_spec + 1; 258 cursor = next_spec + 1;
256 next_spec = strchr(cursor, '%'); 259 next_spec = strchr (cursor, '%');
257 goto parse_format; 260 goto parse_format;
258 } 261 }
259 262
@@ -270,7 +273,7 @@ parse_format:
270 * 273 *
271 * @param filename the filename for this result 274 * @param filename the filename for this result
272 * @param uri the `struct GNUNET_FS_Uri` this result refers to 275 * @param uri the `struct GNUNET_FS_Uri` this result refers to
273 * @param metadata the `struct GNUNET_CONTAINER_MetaData` associated with this 276 * @param metadata the `struct GNUNET_FS_MetaData` associated with this
274 result 277 result
275 * @param resultnum the result number 278 * @param resultnum the result number
276 * @param is_directory GNUNET_YES if this is a directory, otherwise GNUNET_NO 279 * @param is_directory GNUNET_YES if this is a directory, otherwise GNUNET_NO
@@ -279,34 +282,34 @@ parse_format:
279static void 282static void
280print_search_result (const char *const filename, 283print_search_result (const char *const filename,
281 const struct GNUNET_FS_Uri *const uri, 284 const struct GNUNET_FS_Uri *const uri,
282 const struct GNUNET_CONTAINER_MetaData *const metadata, 285 const struct GNUNET_FS_MetaData *const metadata,
283 const unsigned int resultnum, 286 const unsigned int resultnum,
284 const int is_directory) 287 const int is_directory)
285{ 288{
286 289
287 const char *cursor = GNUNET_YES == is_directory ? 290 const char *cursor = GNUNET_YES == is_directory ?
288 dir_format_string 291 dir_format_string
289 : format_string; 292 : format_string;
290 293
291 const char *next_spec = strchr(cursor, '%'); 294 const char *next_spec = strchr (cursor, '%');
292 const char *next_esc = strchr(cursor, '\\'); 295 const char *next_esc = strchr (cursor, '\\');
293 char *placeholder; 296 char *placeholder;
294 struct GNUNET_SEARCH_MetadataPrinterInfo info; 297 struct GNUNET_SEARCH_MetadataPrinterInfo info;
295 298
296parse_format: 299 parse_format:
297 /* If an escape sequence exists before the next format specifier... */ 300 /* If an escape sequence exists before the next format specifier... */
298 if (next_esc && (!next_spec || next_esc < next_spec)) 301 if (next_esc && (! next_spec || next_esc < next_spec))
299 { 302 {
300 if (next_esc > cursor) 303 if (next_esc > cursor)
301 fwrite (cursor, 1, next_esc - cursor, stdout); 304 fwrite (cursor, 1, next_esc - cursor, stdout);
302 305
303 cursor = print_escape_sequence (next_esc); 306 cursor = print_escape_sequence (next_esc);
304 next_esc = strchr(cursor, '\\'); 307 next_esc = strchr (cursor, '\\');
305 goto parse_format; 308 goto parse_format;
306 } 309 }
307 310
308 /* If a format specifier exists before the next escape sequence... */ 311 /* If a format specifier exists before the next escape sequence... */
309 if (next_spec && (!next_esc || next_spec < next_esc)) 312 if (next_spec && (! next_esc || next_spec < next_esc))
310 { 313 {
311 if (next_spec > cursor) 314 if (next_spec > cursor)
312 fwrite (cursor, 1, next_spec - cursor, stdout); 315 fwrite (cursor, 1, next_spec - cursor, stdout);
@@ -317,9 +320,9 @@ parse_format:
317 case 'a': 320 case 'a':
318 info.flags = METADATA_PRINTER_FLAG_NONE; 321 info.flags = METADATA_PRINTER_FLAG_NONE;
319 322
320iterate_meta: 323 iterate_meta:
321 info.counter = 0; 324 info.counter = 0;
322 GNUNET_CONTAINER_meta_data_iterate (metadata, &item_printer, &info); 325 GNUNET_FS_meta_data_iterate (metadata, &item_printer, &info);
323 break; 326 break;
324 /* File's name */ 327 /* File's name */
325 case 'f': 328 case 'f':
@@ -337,11 +340,11 @@ iterate_meta:
337 /* File name's length */ 340 /* File name's length */
338 case 'l': 341 case 'l':
339 printf ("%lu", 342 printf ("%lu",
340 (long unsigned int) ( GNUNET_YES == is_directory ? 343 (long unsigned int) (GNUNET_YES == is_directory ?
341 strlen(filename) + 344 strlen (filename)
342 (sizeof(GNUNET_FS_DIRECTORY_EXT) - 1) 345 + (sizeof(GNUNET_FS_DIRECTORY_EXT) - 1)
343 : 346 :
344 strlen(filename))); 347 strlen (filename)));
345 break; 348 break;
346 /* File's mime type */ 349 /* File's mime type */
347 case 'm': 350 case 'm':
@@ -350,7 +353,7 @@ iterate_meta:
350 printf ("%s", GNUNET_FS_DIRECTORY_MIME); 353 printf ("%s", GNUNET_FS_DIRECTORY_MIME);
351 break; 354 break;
352 } 355 }
353 placeholder = GNUNET_CONTAINER_meta_data_get_by_type ( 356 placeholder = GNUNET_FS_meta_data_get_by_type (
354 metadata, 357 metadata,
355 EXTRACTOR_METATYPE_MIMETYPE); 358 EXTRACTOR_METATYPE_MIMETYPE);
356 printf ("%s", placeholder ? placeholder : GENERIC_FILE_MIMETYPE); 359 printf ("%s", placeholder ? placeholder : GENERIC_FILE_MIMETYPE);
@@ -376,7 +379,7 @@ iterate_meta:
376 case '5': case '6': case '7': case '8': case '9': 379 case '5': case '6': case '7': case '8': case '9':
377 cursor = next_spec; 380 cursor = next_spec;
378 info.type = *cursor - 48; 381 info.type = *cursor - 48;
379 while (isdigit(*++cursor) && info.type < (INT_MAX - *cursor + 48) / 10) 382 while (isdigit (*++cursor) && info.type < (INT_MAX - *cursor + 48) / 10)
380 info.type = info.type * 10 + *cursor - 48; 383 info.type = info.type * 10 + *cursor - 48;
381 if (info.type == 0 || *cursor != '#') 384 if (info.type == 0 || *cursor != '#')
382 goto not_a_specifier; 385 goto not_a_specifier;
@@ -391,21 +394,21 @@ iterate_meta:
391 /* Only the first metadata field of type `info.type` */ 394 /* Only the first metadata field of type `info.type` */
392 case 'j': 395 case 'j':
393 next_spec = cursor; 396 next_spec = cursor;
394 info.flags = METADATA_PRINTER_FLAG_HAVE_TYPE | 397 info.flags = METADATA_PRINTER_FLAG_HAVE_TYPE
395 METADATA_PRINTER_FLAG_ONE_RUN; 398 | METADATA_PRINTER_FLAG_ONE_RUN;
396 goto iterate_meta; 399 goto iterate_meta;
397 } 400 }
398 goto not_a_specifier; 401 goto not_a_specifier;
399 402
400 /* All other cases */ 403 /* All other cases */
401 case '%': putchar('%'); break; 404 case '%': putchar ('%'); break;
402 case '\0': putchar('%'); return; 405 case '\0': putchar ('%'); return;
403 406
404not_a_specifier: 407 not_a_specifier:
405 default: printf ("%%%c", *next_spec); break; 408 default: printf ("%%%c", *next_spec); break;
406 } 409 }
407 cursor = next_spec + 1; 410 cursor = next_spec + 1;
408 next_spec = strchr(cursor, '%'); 411 next_spec = strchr (cursor, '%');
409 goto parse_format; 412 goto parse_format;
410 } 413 }
411 414
@@ -439,10 +442,10 @@ clean_task (void *const cls)
439 GNUNET_DISK_PERM_USER_READ 442 GNUNET_DISK_PERM_USER_READ
440 | GNUNET_DISK_PERM_USER_WRITE)) 443 | GNUNET_DISK_PERM_USER_WRITE))
441 { 444 {
442 GNUNET_SEARCH_log(GNUNET_ERROR_TYPE_ERROR, 445 GNUNET_SEARCH_log (GNUNET_ERROR_TYPE_ERROR,
443 _ ("Failed to write directory with search results to " 446 _ ("Failed to write directory with search results to "
444 "`%s'\n"), 447 "`%s'\n"),
445 output_filename); 448 output_filename);
446 } 449 }
447 GNUNET_free (ddata); 450 GNUNET_free (ddata);
448 GNUNET_free (output_filename); 451 GNUNET_free (output_filename);
@@ -490,7 +493,7 @@ progress_cb (void *const cls,
490 break; 493 break;
491 494
492 cnt++; 495 cnt++;
493 filename = GNUNET_CONTAINER_meta_data_get_by_type ( 496 filename = GNUNET_FS_meta_data_get_by_type (
494 info->value.search.specifics.result.meta, 497 info->value.search.specifics.result.meta,
495 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME); 498 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME);
496 is_directory = GNUNET_FS_meta_data_test_for_directory ( 499 is_directory = GNUNET_FS_meta_data_test_for_directory (
@@ -501,12 +504,12 @@ progress_cb (void *const cls,
501 filename[strlen (filename) - 1] = '\0'; 504 filename[strlen (filename) - 1] = '\0';
502 GNUNET_DISK_filename_canonicalize (filename); 505 GNUNET_DISK_filename_canonicalize (filename);
503 } 506 }
504 print_search_result ( filename ? 507 print_search_result (filename ?
505 filename 508 filename
506 : is_directory ? 509 : is_directory ?
507 GENERIC_DIRECTORY_NAME 510 GENERIC_DIRECTORY_NAME
508 : 511 :
509 GENERIC_FILE_NAME, 512 GENERIC_FILE_NAME,
510 info->value.search.specifics.result.uri, 513 info->value.search.specifics.result.uri,
511 info->value.search.specifics.result.meta, 514 info->value.search.specifics.result.meta,
512 cnt, 515 cnt,
@@ -528,9 +531,9 @@ progress_cb (void *const cls,
528 break; 531 break;
529 532
530 case GNUNET_FS_STATUS_SEARCH_ERROR: 533 case GNUNET_FS_STATUS_SEARCH_ERROR:
531 GNUNET_SEARCH_log(GNUNET_ERROR_TYPE_ERROR, 534 GNUNET_SEARCH_log (GNUNET_ERROR_TYPE_ERROR,
532 _ ("Error searching: %s.\n"), 535 _ ("Error searching: %s.\n"),
533 info->value.search.specifics.error.message); 536 info->value.search.specifics.error.message);
534 GNUNET_SCHEDULER_shutdown (); 537 GNUNET_SCHEDULER_shutdown ();
535 break; 538 break;
536 539
@@ -539,9 +542,9 @@ progress_cb (void *const cls,
539 break; 542 break;
540 543
541 default: 544 default:
542 GNUNET_SEARCH_log(GNUNET_ERROR_TYPE_ERROR, 545 GNUNET_SEARCH_log (GNUNET_ERROR_TYPE_ERROR,
543 _ ("Unexpected status: %d\n"), 546 _ ("Unexpected status: %d\n"),
544 info->status); 547 info->status);
545 break; 548 break;
546 } 549 }
547 return NULL; 550 return NULL;
@@ -600,7 +603,7 @@ run (void *const cls,
600 ret = 1; 603 ret = 1;
601 return; 604 return;
602 } 605 }
603 if (silent_mode && !output_filename) 606 if (silent_mode && ! output_filename)
604 { 607 {
605 fprintf (stderr, _ ("An output file is mandatory for silent mode.\n")); 608 fprintf (stderr, _ ("An output file is mandatory for silent mode.\n"));
606 ret = 1; 609 ret = 1;
@@ -627,7 +630,7 @@ run (void *const cls,
627 ret = 1; 630 ret = 1;
628 return; 631 return;
629 } 632 }
630 if (!GNUNET_FS_uri_test_ksk (uri) && !GNUNET_FS_uri_test_sks (uri)) 633 if (! GNUNET_FS_uri_test_ksk (uri) && ! GNUNET_FS_uri_test_sks (uri))
631 { 634 {
632 fprintf (stderr, 635 fprintf (stderr,
633 "%s", 636 "%s",
@@ -640,7 +643,7 @@ run (void *const cls,
640 } 643 }
641 if (bookmark_only) 644 if (bookmark_only)
642 { 645 {
643 char * bmstr = GNUNET_FS_uri_to_string (uri); 646 char *bmstr = GNUNET_FS_uri_to_string (uri);
644 printf ("%s\n", bmstr); 647 printf ("%s\n", bmstr);
645 GNUNET_free (bmstr); 648 GNUNET_free (bmstr);
646 GNUNET_FS_uri_destroy (uri); 649 GNUNET_FS_uri_destroy (uri);
@@ -779,15 +782,15 @@ main (int argc, char *const *argv)
779 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 782 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
780 return 12; 783 return 12;
781 784
782 if (GNUNET_SYSERR == 785 if (GNUNET_SYSERR ==
783 GNUNET_PROGRAM_run (argc, 786 GNUNET_PROGRAM_run (argc,
784 argv, 787 argv,
785 "gnunet-search [OPTIONS] KEYWORD1 KEYWORD2 ...", 788 "gnunet-search [OPTIONS] KEYWORD1 KEYWORD2 ...",
786 gettext_noop ("Search for files that have been " 789 gettext_noop ("Search for files that have been "
787 "published on GNUnet\n"), 790 "published on GNUnet\n"),
788 options, 791 options,
789 &run, 792 &run,
790 NULL)) 793 NULL))
791 ret = 1; 794 ret = 1;
792 795
793 GNUNET_free_nz ((void *) argv); 796 GNUNET_free_nz ((void *) argv);