diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-01-28 14:52:48 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-01-28 14:52:48 +0000 |
commit | 4b9e92b8f0c33dd655abff1cda89b33aaf90ef16 (patch) | |
tree | 4dbe0b3109daad81b6f7c6845b152afd787576fc | |
parent | 0f5c5a8d55e8f3df8dcb8ddff61da621b6a482af (diff) | |
download | gnunet-4b9e92b8f0c33dd655abff1cda89b33aaf90ef16.tar.gz gnunet-4b9e92b8f0c33dd655abff1cda89b33aaf90ef16.zip |
-change callback argument from char to int, as that is cleaner for a boolean; document API some
-rw-r--r-- | src/fs/fs_file_information.c | 512 | ||||
-rw-r--r-- | src/fs/gnunet-publish.c | 5 | ||||
-rw-r--r-- | src/include/gnunet_fs_service.h | 221 |
3 files changed, 95 insertions, 643 deletions
diff --git a/src/fs/fs_file_information.c b/src/fs/fs_file_information.c index 94b4c1866..01ce6f54b 100644 --- a/src/fs/fs_file_information.c +++ b/src/fs/fs_file_information.c | |||
@@ -313,518 +313,6 @@ GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h, | |||
313 | 313 | ||
314 | 314 | ||
315 | /** | 315 | /** |
316 | * Closure for "dir_scan_cb". | ||
317 | */ | ||
318 | struct DirScanCls | ||
319 | { | ||
320 | /** | ||
321 | * Metadata extractors to use. | ||
322 | */ | ||
323 | struct EXTRACTOR_PluginList *extractors; | ||
324 | |||
325 | /** | ||
326 | * Master context. | ||
327 | */ | ||
328 | struct GNUNET_FS_Handle *h; | ||
329 | |||
330 | /** | ||
331 | * Function to call on each directory entry. | ||
332 | */ | ||
333 | GNUNET_FS_FileProcessor proc; | ||
334 | |||
335 | /** | ||
336 | * Closure for proc. | ||
337 | */ | ||
338 | void *proc_cls; | ||
339 | |||
340 | /** | ||
341 | * Scanner to use for subdirectories. | ||
342 | */ | ||
343 | GNUNET_FS_DirectoryScanner scanner; | ||
344 | |||
345 | /** | ||
346 | * Closure for scanner. | ||
347 | */ | ||
348 | void *scanner_cls; | ||
349 | |||
350 | /** | ||
351 | * Set to an error message (if any). | ||
352 | */ | ||
353 | char *emsg; | ||
354 | |||
355 | /** | ||
356 | * Block options. | ||
357 | */ | ||
358 | const struct GNUNET_FS_BlockOptions *bo; | ||
359 | |||
360 | /** | ||
361 | * Should files be indexed? | ||
362 | */ | ||
363 | int do_index; | ||
364 | |||
365 | }; | ||
366 | |||
367 | |||
368 | /** | ||
369 | * Function called on each entry in a file to cause | ||
370 | * default-publishing. | ||
371 | * | ||
372 | * @param cls closure (struct DirScanCls) | ||
373 | * @param filename name of the file to be published | ||
374 | * @return GNUNET_OK on success, GNUNET_SYSERR to abort | ||
375 | */ | ||
376 | static int | ||
377 | dir_scan_cb (void *cls, const char *filename) | ||
378 | { | ||
379 | struct DirScanCls *dsc = cls; | ||
380 | struct stat sbuf; | ||
381 | struct GNUNET_FS_FileInformation *fi; | ||
382 | struct GNUNET_FS_Uri *keywords; | ||
383 | struct GNUNET_CONTAINER_MetaData *meta; | ||
384 | |||
385 | if (0 != STAT (filename, &sbuf)) | ||
386 | { | ||
387 | GNUNET_asprintf (&dsc->emsg, _("`%s' failed on file `%s': %s"), "stat", | ||
388 | filename, STRERROR (errno)); | ||
389 | return GNUNET_SYSERR; | ||
390 | } | ||
391 | if (S_ISDIR (sbuf.st_mode)) | ||
392 | { | ||
393 | fi = GNUNET_FS_file_information_create_from_directory (dsc->h, NULL, | ||
394 | filename, | ||
395 | dsc->scanner, | ||
396 | dsc->scanner_cls, | ||
397 | dsc->do_index, | ||
398 | dsc->bo, &dsc->emsg); | ||
399 | if (NULL == fi) | ||
400 | { | ||
401 | GNUNET_assert (NULL != dsc->emsg); | ||
402 | return GNUNET_SYSERR; | ||
403 | } | ||
404 | } | ||
405 | else | ||
406 | { | ||
407 | meta = GNUNET_CONTAINER_meta_data_create (); | ||
408 | GNUNET_FS_meta_data_extract_from_file (meta, filename, dsc->extractors); | ||
409 | keywords = GNUNET_FS_uri_ksk_create_from_meta_data (meta); | ||
410 | fi = GNUNET_FS_file_information_create_from_file (dsc->h, NULL, filename, | ||
411 | keywords, meta, | ||
412 | dsc->do_index, dsc->bo); | ||
413 | GNUNET_CONTAINER_meta_data_destroy (meta); | ||
414 | GNUNET_FS_uri_destroy (keywords); | ||
415 | } | ||
416 | dsc->proc (dsc->proc_cls, filename, fi); | ||
417 | return GNUNET_OK; | ||
418 | } | ||
419 | |||
420 | |||
421 | /** | ||
422 | * Simple, useful default implementation of a directory scanner | ||
423 | * (GNUNET_FS_DirectoryScanner). This implementation expects to get a | ||
424 | * UNIX filename, will publish all files in the directory except hidden | ||
425 | * files (those starting with a "."). Metadata will be extracted | ||
426 | * using GNU libextractor; the specific list of plugins should be | ||
427 | * specified in "cls", passing NULL will disable (!) metadata | ||
428 | * extraction. Keywords will be derived from the metadata and be | ||
429 | * subject to default canonicalization. This is strictly a | ||
430 | * convenience function. | ||
431 | * | ||
432 | * @param cls must be of type "struct EXTRACTOR_Extractor*" | ||
433 | * @param h handle to the file sharing subsystem | ||
434 | * @param dirname name of the directory to scan | ||
435 | * @param do_index should files be indexed or inserted | ||
436 | * @param bo block options | ||
437 | * @param proc function called on each entry | ||
438 | * @param proc_cls closure for proc | ||
439 | * @param emsg where to store an error message (on errors) | ||
440 | * @return GNUNET_OK on success | ||
441 | */ | ||
442 | int | ||
443 | GNUNET_FS_directory_scanner_default (void *cls, struct GNUNET_FS_Handle *h, | ||
444 | const char *dirname, int do_index, | ||
445 | const struct GNUNET_FS_BlockOptions *bo, | ||
446 | GNUNET_FS_FileProcessor proc, | ||
447 | void *proc_cls, char **emsg) | ||
448 | { | ||
449 | struct EXTRACTOR_PluginList *ex = cls; | ||
450 | struct DirScanCls dsc; | ||
451 | |||
452 | dsc.h = h; | ||
453 | dsc.extractors = ex; | ||
454 | dsc.proc = proc; | ||
455 | dsc.proc_cls = proc_cls; | ||
456 | dsc.scanner = &GNUNET_FS_directory_scanner_default; | ||
457 | dsc.scanner_cls = cls; | ||
458 | dsc.do_index = do_index; | ||
459 | dsc.bo = bo; | ||
460 | if (-1 == GNUNET_DISK_directory_scan (dirname, &dir_scan_cb, &dsc)) | ||
461 | { | ||
462 | GNUNET_assert (NULL != dsc.emsg); | ||
463 | *emsg = dsc.emsg; | ||
464 | return GNUNET_SYSERR; | ||
465 | } | ||
466 | return GNUNET_OK; | ||
467 | } | ||
468 | |||
469 | |||
470 | /** | ||
471 | * Aggregate information we keep for meta data in each directory. | ||
472 | */ | ||
473 | struct MetaValueInformation | ||
474 | { | ||
475 | |||
476 | /** | ||
477 | * Mime-type of data. | ||
478 | */ | ||
479 | const char *mime_type; | ||
480 | |||
481 | /** | ||
482 | * The actual meta data. | ||
483 | */ | ||
484 | const char *data; | ||
485 | |||
486 | /** | ||
487 | * Number of bytes in 'data'. | ||
488 | */ | ||
489 | size_t data_size; | ||
490 | |||
491 | /** | ||
492 | * Type of the meta data. | ||
493 | */ | ||
494 | enum EXTRACTOR_MetaType type; | ||
495 | |||
496 | /** | ||
497 | * Format of the meta data. | ||
498 | */ | ||
499 | enum EXTRACTOR_MetaFormat format; | ||
500 | |||
501 | /** | ||
502 | * How often does this meta value occur in this directory? | ||
503 | */ | ||
504 | unsigned int frequency; | ||
505 | |||
506 | }; | ||
507 | |||
508 | |||
509 | /** | ||
510 | * Type of a function that libextractor calls for each | ||
511 | * meta data item found. | ||
512 | * | ||
513 | * @param cls the container multihashmap to update | ||
514 | * @param plugin_name name of the plugin that produced this value; | ||
515 | * special values can be used (i.e. '<zlib>' for zlib being | ||
516 | * used in the main libextractor library and yielding | ||
517 | * meta data). | ||
518 | * @param type libextractor-type describing the meta data | ||
519 | * @param format basic format information about data | ||
520 | * @param data_mime_type mime-type of data (not of the original file); | ||
521 | * can be NULL (if mime-type is not known) | ||
522 | * @param data actual meta-data found | ||
523 | * @param data_len number of bytes in data | ||
524 | * @return 0 to continue extracting / iterating | ||
525 | */ | ||
526 | static int | ||
527 | update_metamap (void *cls, const char *plugin_name, | ||
528 | enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, | ||
529 | const char *data_mime_type, const char *data, size_t data_len) | ||
530 | { | ||
531 | struct GNUNET_CONTAINER_MultiHashMap *map = cls; | ||
532 | GNUNET_HashCode key; | ||
533 | struct MetaValueInformation *mvi; | ||
534 | |||
535 | GNUNET_CRYPTO_hash (data, data_len, &key); | ||
536 | mvi = GNUNET_CONTAINER_multihashmap_get (map, &key); | ||
537 | if (mvi == NULL) | ||
538 | { | ||
539 | mvi = GNUNET_malloc (sizeof (struct MetaValueInformation)); | ||
540 | mvi->mime_type = data_mime_type; | ||
541 | mvi->data = data; | ||
542 | mvi->data_size = data_len; | ||
543 | mvi->type = type; | ||
544 | mvi->format = format; | ||
545 | GNUNET_CONTAINER_multihashmap_put (map, &key, mvi, | ||
546 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
547 | } | ||
548 | mvi->frequency++; | ||
549 | return 0; | ||
550 | } | ||
551 | |||
552 | |||
553 | /** | ||
554 | * Aggregate information we keep for keywords in each directory. | ||
555 | */ | ||
556 | struct KeywordInformation | ||
557 | { | ||
558 | |||
559 | /** | ||
560 | * Mime-type of keyword. | ||
561 | */ | ||
562 | const char *keyword; | ||
563 | |||
564 | /** | ||
565 | * How often does this meta value occur in this directory? | ||
566 | */ | ||
567 | unsigned int frequency; | ||
568 | |||
569 | }; | ||
570 | |||
571 | |||
572 | /** | ||
573 | * Closure for dirproc function. | ||
574 | */ | ||
575 | struct EntryProcCls | ||
576 | { | ||
577 | /** | ||
578 | * Linked list of directory entries that is being | ||
579 | * created. | ||
580 | */ | ||
581 | struct GNUNET_FS_FileInformation *entries; | ||
582 | |||
583 | /** | ||
584 | * Map describing the meta data for all entries in the | ||
585 | * directory. Keys are the hash of the meta-value, | ||
586 | * values are of type 'struct MetaValueInformation'. | ||
587 | */ | ||
588 | struct GNUNET_CONTAINER_MultiHashMap *metamap; | ||
589 | |||
590 | /** | ||
591 | * Map describing the keywords for all entries in the | ||
592 | * directory. Keys are the hash of the keyword, | ||
593 | * values are of type 'struct KeywordInformation'. | ||
594 | */ | ||
595 | struct GNUNET_CONTAINER_MultiHashMap *keywordmap; | ||
596 | |||
597 | /** | ||
598 | * Number of entries in 'entries'. | ||
599 | */ | ||
600 | unsigned int count; | ||
601 | |||
602 | }; | ||
603 | |||
604 | |||
605 | /** | ||
606 | * Function that processes a directory entry that | ||
607 | * was obtained from the scanner. Adds each entry to | ||
608 | * the directory and computes directroy meta map. | ||
609 | * | ||
610 | * @param cls our closure | ||
611 | * @param filename name of the file (unused, why there???) | ||
612 | * @param fi information for publishing the file | ||
613 | */ | ||
614 | static void | ||
615 | dirproc_add (void *cls, const char *filename, | ||
616 | struct GNUNET_FS_FileInformation *fi) | ||
617 | { | ||
618 | struct EntryProcCls *dc = cls; | ||
619 | unsigned int i; | ||
620 | const char *kw; | ||
621 | struct KeywordInformation *ki; | ||
622 | GNUNET_HashCode key; | ||
623 | |||
624 | GNUNET_assert (fi->next == NULL); | ||
625 | GNUNET_assert (fi->dir == NULL); | ||
626 | fi->next = dc->entries; | ||
627 | dc->entries = fi; | ||
628 | dc->count++; | ||
629 | if (NULL != fi->meta) | ||
630 | GNUNET_CONTAINER_meta_data_iterate (fi->meta, &update_metamap, dc->metamap); | ||
631 | for (i = 0; i < fi->keywords->data.ksk.keywordCount; i++) | ||
632 | { | ||
633 | kw = fi->keywords->data.ksk.keywords[i]; | ||
634 | GNUNET_CRYPTO_hash (kw, strlen (kw), &key); | ||
635 | ki = GNUNET_CONTAINER_multihashmap_get (dc->keywordmap, &key); | ||
636 | if (ki == NULL) | ||
637 | { | ||
638 | ki = GNUNET_malloc (sizeof (struct KeywordInformation)); | ||
639 | ki->keyword = &kw[1]; | ||
640 | GNUNET_CONTAINER_multihashmap_put (dc->keywordmap, &key, ki, | ||
641 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY); | ||
642 | } | ||
643 | ki->frequency++; | ||
644 | } | ||
645 | } | ||
646 | |||
647 | |||
648 | /** | ||
649 | * Closure for 'compute_directory_metadata'. | ||
650 | */ | ||
651 | struct ComputeDirectoryMetadataContext | ||
652 | { | ||
653 | /** | ||
654 | * Where to store the extracted keywords. | ||
655 | */ | ||
656 | struct GNUNET_FS_Uri *ksk; | ||
657 | |||
658 | /** | ||
659 | * Where to store the extracted meta data. | ||
660 | */ | ||
661 | struct GNUNET_CONTAINER_MetaData *meta; | ||
662 | |||
663 | /** | ||
664 | * Threshold to apply for adding meta data. | ||
665 | */ | ||
666 | unsigned int threshold; | ||
667 | }; | ||
668 | |||
669 | |||
670 | /** | ||
671 | * Add metadata that occurs in more than the threshold entries of the | ||
672 | * directory to the directory itself. For example, if most files in a | ||
673 | * directory are of the same mime-type, the directory should have that | ||
674 | * mime-type as a keyword. | ||
675 | * | ||
676 | * @param cls the 'struct ComputeDirectoryMetadataContext' | ||
677 | * @param key unused | ||
678 | * @param value the 'struct MetaValueInformation' (to be freed as well) | ||
679 | * @return GNUNET_OK | ||
680 | */ | ||
681 | static int | ||
682 | compute_directory_metadata (void *cls, const GNUNET_HashCode * key, void *value) | ||
683 | { | ||
684 | struct ComputeDirectoryMetadataContext *cdmc = cls; | ||
685 | struct MetaValueInformation *mvi = value; | ||
686 | |||
687 | if (mvi->frequency > cdmc->threshold) | ||
688 | { | ||
689 | if (mvi->type != EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME) | ||
690 | (void) GNUNET_CONTAINER_meta_data_insert (cdmc->meta, "<children>", | ||
691 | mvi->type, mvi->format, | ||
692 | mvi->mime_type, mvi->data, | ||
693 | mvi->data_size); | ||
694 | if ((mvi->format == EXTRACTOR_METAFORMAT_UTF8) || | ||
695 | (mvi->format == EXTRACTOR_METAFORMAT_C_STRING)) | ||
696 | GNUNET_FS_uri_ksk_add_keyword (cdmc->ksk, mvi->data, GNUNET_NO); | ||
697 | } | ||
698 | GNUNET_free (mvi); | ||
699 | return GNUNET_OK; | ||
700 | } | ||
701 | |||
702 | |||
703 | /** | ||
704 | * Add keywords that occur in more than the threshold entries of the | ||
705 | * directory to the directory itself. | ||
706 | * | ||
707 | * @param cls the 'struct ComputeDirectoryMetadataContext' | ||
708 | * @param key unused | ||
709 | * @param value the 'struct Keywordnformation' (to be freed as well) | ||
710 | * @return GNUNET_OK | ||
711 | */ | ||
712 | static int | ||
713 | compute_directory_keywords (void *cls, const GNUNET_HashCode * key, void *value) | ||
714 | { | ||
715 | struct ComputeDirectoryMetadataContext *cdmc = cls; | ||
716 | struct KeywordInformation *ki = value; | ||
717 | |||
718 | if (ki->frequency > cdmc->threshold) | ||
719 | (void) GNUNET_FS_uri_ksk_add_keyword (cdmc->ksk, ki->keyword, GNUNET_NO); | ||
720 | GNUNET_free (ki); | ||
721 | return GNUNET_OK; | ||
722 | } | ||
723 | |||
724 | |||
725 | /** | ||
726 | * Create a publish-structure from an existing file hierarchy, inferring | ||
727 | * and organizing keywords and metadata as much as possible. This | ||
728 | * function primarily performs the recursive build and re-organizes | ||
729 | * keywords and metadata; for automatically getting metadata | ||
730 | * extraction, scanning of directories and creation of the respective | ||
731 | * GNUNET_FS_FileInformation entries the default scanner should be | ||
732 | * passed (GNUNET_FS_directory_scanner_default). This is strictly a | ||
733 | * convenience function. | ||
734 | * | ||
735 | * @param h handle to the file sharing subsystem | ||
736 | * @param client_info initial value for the client-info value for this entry | ||
737 | * @param filename name of the top-level file or directory | ||
738 | * @param scanner function used to get a list of files in a directory | ||
739 | * @param scanner_cls closure for scanner | ||
740 | * @param do_index should files in the hierarchy be indexed? | ||
741 | * @param bo block options | ||
742 | * @param emsg where to store an error message | ||
743 | * @return publish structure entry for the directory, NULL on error | ||
744 | */ | ||
745 | struct GNUNET_FS_FileInformation * | ||
746 | GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h, | ||
747 | void *client_info, | ||
748 | const char *filename, | ||
749 | GNUNET_FS_DirectoryScanner | ||
750 | scanner, void *scanner_cls, | ||
751 | int do_index, | ||
752 | const struct | ||
753 | GNUNET_FS_BlockOptions *bo, | ||
754 | char **emsg) | ||
755 | { | ||
756 | struct GNUNET_FS_FileInformation *ret; | ||
757 | struct ComputeDirectoryMetadataContext cdmc; | ||
758 | struct EntryProcCls dc; | ||
759 | const char *fn; | ||
760 | const char *ss; | ||
761 | char *dn; | ||
762 | struct GNUNET_FS_FileInformation *epos; | ||
763 | unsigned int i; | ||
764 | const char *kw; | ||
765 | |||
766 | dc.entries = NULL; | ||
767 | dc.count = 0; | ||
768 | dc.metamap = GNUNET_CONTAINER_multihashmap_create (64); | ||
769 | dc.keywordmap = GNUNET_CONTAINER_multihashmap_create (64); | ||
770 | /* update children to point to directory and generate statistics | ||
771 | * on all meta data in children */ | ||
772 | scanner (scanner_cls, h, filename, do_index, bo, &dirproc_add, &dc, emsg); | ||
773 | cdmc.meta = GNUNET_CONTAINER_meta_data_create (); | ||
774 | cdmc.ksk = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri)); | ||
775 | cdmc.ksk->type = ksk; | ||
776 | cdmc.threshold = 1 + dc.count / 2; /* 50% threshold for now */ | ||
777 | GNUNET_FS_meta_data_make_directory (cdmc.meta); | ||
778 | GNUNET_CONTAINER_multihashmap_iterate (dc.metamap, | ||
779 | &compute_directory_metadata, &cdmc); | ||
780 | GNUNET_CONTAINER_multihashmap_iterate (dc.keywordmap, | ||
781 | &compute_directory_keywords, &cdmc); | ||
782 | GNUNET_CONTAINER_multihashmap_destroy (dc.metamap); | ||
783 | GNUNET_CONTAINER_multihashmap_destroy (dc.keywordmap); | ||
784 | |||
785 | /* remove keywords in children that are already in the | ||
786 | * parent */ | ||
787 | for (epos = dc.entries; NULL != epos; epos = epos->next) | ||
788 | { | ||
789 | for (i = 0; i < cdmc.ksk->data.ksk.keywordCount; i++) | ||
790 | { | ||
791 | kw = cdmc.ksk->data.ksk.keywords[i]; | ||
792 | GNUNET_FS_uri_ksk_remove_keyword (epos->keywords, &kw[1]); | ||
793 | } | ||
794 | } | ||
795 | GNUNET_FS_uri_ksk_add_keyword (cdmc.ksk, GNUNET_FS_DIRECTORY_MIME, GNUNET_NO); | ||
796 | ret = | ||
797 | GNUNET_FS_file_information_create_empty_directory (h, client_info, cdmc.ksk, | ||
798 | cdmc.meta, bo, filename); | ||
799 | GNUNET_CONTAINER_meta_data_destroy (cdmc.meta); | ||
800 | GNUNET_FS_uri_destroy (cdmc.ksk); | ||
801 | ret->data.dir.entries = dc.entries; | ||
802 | while (dc.entries != NULL) | ||
803 | { | ||
804 | dc.entries->dir = ret; | ||
805 | dc.entries = dc.entries->next; | ||
806 | } | ||
807 | fn = filename; | ||
808 | while ((NULL != (ss = strstr (fn, DIR_SEPARATOR_STR))) && (strlen (ss) > 1)) | ||
809 | fn = ss + 1; | ||
810 | GNUNET_asprintf (&dn, "%s/", fn); | ||
811 | #if !WINDOWS | ||
812 | GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>", | ||
813 | EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, | ||
814 | EXTRACTOR_METAFORMAT_C_STRING, | ||
815 | "text/plain", dn, strlen (dn) + 1); | ||
816 | #else | ||
817 | GNUNET_CONTAINER_meta_data_insert (ret->meta, "<gnunet>", | ||
818 | EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, | ||
819 | EXTRACTOR_METAFORMAT_UTF8, | ||
820 | "text/plain", dn, strlen (dn) + 1); | ||
821 | #endif | ||
822 | GNUNET_free (dn); | ||
823 | return ret; | ||
824 | } | ||
825 | |||
826 | |||
827 | /** | ||
828 | * Test if a given entry represents a directory. | 316 | * Test if a given entry represents a directory. |
829 | * | 317 | * |
830 | * @param ent check if this FI represents a directory | 318 | * @param ent check if this FI represents a directory |
diff --git a/src/fs/gnunet-publish.c b/src/fs/gnunet-publish.c index 1c02004dc..98f39b821 100644 --- a/src/fs/gnunet-publish.c +++ b/src/fs/gnunet-publish.c | |||
@@ -427,8 +427,9 @@ directory_trim_complete (void *cls, | |||
427 | 427 | ||
428 | static int | 428 | static int |
429 | directory_scan_cb (void *cls, struct GNUNET_FS_DirScanner *ds, | 429 | directory_scan_cb (void *cls, struct GNUNET_FS_DirScanner *ds, |
430 | const char *filename, char is_directory, | 430 | const char *filename, |
431 | enum GNUNET_FS_DirScannerProgressUpdateReason reason) | 431 | int is_directory, |
432 | enum GNUNET_FS_DirScannerProgressUpdateReason reason) | ||
432 | { | 433 | { |
433 | switch (reason) | 434 | switch (reason) |
434 | { | 435 | { |
diff --git a/src/include/gnunet_fs_service.h b/src/include/gnunet_fs_service.h index 1584f283c..179a56529 100644 --- a/src/include/gnunet_fs_service.h +++ b/src/include/gnunet_fs_service.h | |||
@@ -53,8 +53,9 @@ extern "C" | |||
53 | * 6.0.0: with support for OR in KSKs | 53 | * 6.0.0: with support for OR in KSKs |
54 | * 6.1.x: with simplified namespace support | 54 | * 6.1.x: with simplified namespace support |
55 | * 9.0.0: CPS-style integrated API | 55 | * 9.0.0: CPS-style integrated API |
56 | * 9.1.1: asynchronous directory scanning | ||
56 | */ | 57 | */ |
57 | #define GNUNET_FS_VERSION 0x00090001 | 58 | #define GNUNET_FS_VERSION 0x00090101 |
58 | 59 | ||
59 | 60 | ||
60 | /* ******************** URI API *********************** */ | 61 | /* ******************** URI API *********************** */ |
@@ -1760,6 +1761,7 @@ GNUNET_FS_file_information_get_id (struct GNUNET_FS_FileInformation *s); | |||
1760 | const char * | 1761 | const char * |
1761 | GNUNET_FS_file_information_get_filename (struct GNUNET_FS_FileInformation *s); | 1762 | GNUNET_FS_file_information_get_filename (struct GNUNET_FS_FileInformation *s); |
1762 | 1763 | ||
1764 | |||
1763 | /** | 1765 | /** |
1764 | * Set the filename in the file information structure. | 1766 | * Set the filename in the file information structure. |
1765 | * If filename was already set, frees it before setting the new one. | 1767 | * If filename was already set, frees it before setting the new one. |
@@ -1772,6 +1774,7 @@ void | |||
1772 | GNUNET_FS_file_information_set_filename (struct GNUNET_FS_FileInformation *s, | 1774 | GNUNET_FS_file_information_set_filename (struct GNUNET_FS_FileInformation *s, |
1773 | const char *filename); | 1775 | const char *filename); |
1774 | 1776 | ||
1777 | |||
1775 | /** | 1778 | /** |
1776 | * Create an entry for a file in a publish-structure. | 1779 | * Create an entry for a file in a publish-structure. |
1777 | * | 1780 | * |
@@ -1880,105 +1883,6 @@ GNUNET_FS_file_information_create_from_reader (struct GNUNET_FS_Handle *h, | |||
1880 | 1883 | ||
1881 | 1884 | ||
1882 | /** | 1885 | /** |
1883 | * Function that a "GNUNET_FS_DirectoryScanner" should call | ||
1884 | * for each entry in the directory. | ||
1885 | * | ||
1886 | * @param cls closure | ||
1887 | * @param filename name of the file (including path); must end | ||
1888 | * in a "/" (even on W32) if this is a directory | ||
1889 | * @param fi information about the file (should not be | ||
1890 | * used henceforth by the caller) | ||
1891 | */ | ||
1892 | typedef void (*GNUNET_FS_FileProcessor) (void *cls, const char *filename, | ||
1893 | struct GNUNET_FS_FileInformation * fi); | ||
1894 | |||
1895 | |||
1896 | /** | ||
1897 | * Type of a function that will be used to scan a directory. | ||
1898 | * | ||
1899 | * @param cls closure | ||
1900 | * @param h handle to the file sharing subsystem | ||
1901 | * @param dirname name of the directory to scan | ||
1902 | * @param do_index should files be indexed or inserted | ||
1903 | * @param bo block options | ||
1904 | * @param proc function to call on each entry | ||
1905 | * @param proc_cls closure for proc | ||
1906 | * @param emsg where to store an error message (on errors) | ||
1907 | * @return GNUNET_OK on success | ||
1908 | */ | ||
1909 | typedef int (*GNUNET_FS_DirectoryScanner) (void *cls, | ||
1910 | struct GNUNET_FS_Handle * h, | ||
1911 | const char *dirname, int do_index, | ||
1912 | const struct GNUNET_FS_BlockOptions * | ||
1913 | bo, GNUNET_FS_FileProcessor proc, | ||
1914 | void *proc_cls, char **emsg); | ||
1915 | |||
1916 | |||
1917 | |||
1918 | /** | ||
1919 | * Simple, useful default implementation of a directory scanner | ||
1920 | * (GNUNET_FS_DirectoryScanner). This implementation expects to get a | ||
1921 | * UNIX filename, will publish all files in the directory except hidden | ||
1922 | * files (those starting with a "."). Metadata will be extracted | ||
1923 | * using GNU libextractor; the specific list of plugins should be | ||
1924 | * specified in "cls", passing NULL will disable (!) metadata | ||
1925 | * extraction. Keywords will be derived from the metadata and | ||
1926 | * associated with directories as appropriate. This is strictly a | ||
1927 | * convenience function (however, if all tools use it, there will | ||
1928 | * be less of a chance of distinguishing users by the specific | ||
1929 | * user-interface they were using). | ||
1930 | * | ||
1931 | * @param cls must be of type "struct EXTRACTOR_Extractor*" | ||
1932 | * @param h handle to the file sharing subsystem | ||
1933 | * @param dirname name of the directory to scan | ||
1934 | * @param do_index should files be indexed or inserted | ||
1935 | * @param bo block options | ||
1936 | * @param proc function called on each entry | ||
1937 | * @param proc_cls closure for proc | ||
1938 | * @param emsg where to store an error message (on errors) | ||
1939 | * @return GNUNET_OK on success | ||
1940 | */ | ||
1941 | int | ||
1942 | GNUNET_FS_directory_scanner_default (void *cls, struct GNUNET_FS_Handle *h, | ||
1943 | const char *dirname, int do_index, | ||
1944 | const struct GNUNET_FS_BlockOptions *bo, | ||
1945 | GNUNET_FS_FileProcessor proc, | ||
1946 | void *proc_cls, char **emsg); | ||
1947 | |||
1948 | |||
1949 | /** | ||
1950 | * Create a publish-structure from an existing file hierarchy, inferring | ||
1951 | * and organizing keywords and metadata as much as possible. This | ||
1952 | * function primarily performs the recursive build and re-organizes | ||
1953 | * keywords and metadata; for automatically getting metadata | ||
1954 | * extraction, scanning of directories and creation of the respective | ||
1955 | * GNUNET_FS_FileInformation entries the default scanner should be | ||
1956 | * passed (GNUNET_FS_directory_scanner_default). This is strictly a | ||
1957 | * convenience function. | ||
1958 | * | ||
1959 | * @param h handle to the file sharing subsystem | ||
1960 | * @param client_info initial client-info value for this entry | ||
1961 | * @param filename name of the top-level file or directory | ||
1962 | * @param scanner function used to get a list of files in a directory | ||
1963 | * @param scanner_cls closure for scanner | ||
1964 | * @param do_index should files in the hierarchy be indexed? | ||
1965 | * @param bo block options | ||
1966 | * @param emsg where to store an error message | ||
1967 | * @return publish structure entry for the directory, NULL on error | ||
1968 | */ | ||
1969 | struct GNUNET_FS_FileInformation * | ||
1970 | GNUNET_FS_file_information_create_from_directory (struct GNUNET_FS_Handle *h, | ||
1971 | void *client_info, | ||
1972 | const char *filename, | ||
1973 | GNUNET_FS_DirectoryScanner | ||
1974 | scanner, void *scanner_cls, | ||
1975 | int do_index, | ||
1976 | const struct | ||
1977 | GNUNET_FS_BlockOptions *bo, | ||
1978 | char **emsg); | ||
1979 | |||
1980 | |||
1981 | /** | ||
1982 | * Create an entry for an empty directory in a publish-structure. | 1886 | * Create an entry for an empty directory in a publish-structure. |
1983 | * This function should be used by applications for which the | 1887 | * This function should be used by applications for which the |
1984 | * use of "GNUNET_FS_file_information_create_from_directory" | 1888 | * use of "GNUNET_FS_file_information_create_from_directory" |
@@ -2692,22 +2596,69 @@ GNUNET_FS_directory_builder_finish (struct GNUNET_FS_DirectoryBuilder *bld, | |||
2692 | 2596 | ||
2693 | /* ******************** DirScanner API *********************** */ | 2597 | /* ******************** DirScanner API *********************** */ |
2694 | 2598 | ||
2599 | /** | ||
2600 | * Progress reasons of the directory scanner. | ||
2601 | */ | ||
2695 | enum GNUNET_FS_DirScannerProgressUpdateReason | 2602 | enum GNUNET_FS_DirScannerProgressUpdateReason |
2696 | { | 2603 | { |
2604 | /** | ||
2605 | * FIXME | ||
2606 | */ | ||
2697 | GNUNET_FS_DIRSCANNER_FIRST = 0, | 2607 | GNUNET_FS_DIRSCANNER_FIRST = 0, |
2608 | |||
2609 | /** | ||
2610 | * FIXME | ||
2611 | */ | ||
2698 | GNUNET_FS_DIRSCANNER_NEW_FILE = 1, | 2612 | GNUNET_FS_DIRSCANNER_NEW_FILE = 1, |
2613 | |||
2614 | /** | ||
2615 | * FIXME | ||
2616 | */ | ||
2699 | GNUNET_FS_DIRSCANNER_DOES_NOT_EXIST = 2, | 2617 | GNUNET_FS_DIRSCANNER_DOES_NOT_EXIST = 2, |
2618 | |||
2619 | /** | ||
2620 | * FIXME | ||
2621 | */ | ||
2700 | GNUNET_FS_DIRSCANNER_ASKED_TO_STOP = 3, | 2622 | GNUNET_FS_DIRSCANNER_ASKED_TO_STOP = 3, |
2623 | |||
2624 | /** | ||
2625 | * FIXME | ||
2626 | */ | ||
2701 | GNUNET_FS_DIRSCANNER_FINISHED = 4, | 2627 | GNUNET_FS_DIRSCANNER_FINISHED = 4, |
2628 | |||
2629 | /** | ||
2630 | * FIXME | ||
2631 | */ | ||
2702 | GNUNET_FS_DIRSCANNER_PROTOCOL_ERROR = 5, | 2632 | GNUNET_FS_DIRSCANNER_PROTOCOL_ERROR = 5, |
2633 | |||
2634 | /** | ||
2635 | * FIXME | ||
2636 | */ | ||
2703 | GNUNET_FS_DIRSCANNER_SHUTDOWN = 6, | 2637 | GNUNET_FS_DIRSCANNER_SHUTDOWN = 6, |
2638 | |||
2639 | /** | ||
2640 | * FIXME | ||
2641 | */ | ||
2704 | GNUNET_FS_DIRSCANNER_LAST = 7 | 2642 | GNUNET_FS_DIRSCANNER_LAST = 7 |
2705 | }; | 2643 | }; |
2706 | 2644 | ||
2707 | 2645 | ||
2708 | typedef int (* GNUNET_FS_DirScannerProgressCallback) ( | 2646 | /** |
2709 | void *cls, struct GNUNET_FS_DirScanner *ds, const char *filename, | 2647 | * Function called over time as the directory scanner makes |
2710 | char is_directory, enum GNUNET_FS_DirScannerProgressUpdateReason reason); | 2648 | * progress on the job at hand. |
2649 | * | ||
2650 | * @param cls closure | ||
2651 | * @param ds handle to the directory scanner (NEEDED!?) | ||
2652 | * @param filename which file we are making progress on | ||
2653 | * @param is_directory GNUNET_YES if this is a directory | ||
2654 | * @param reason kind of progress we are making | ||
2655 | */ | ||
2656 | typedef int (*GNUNET_FS_DirScannerProgressCallback) (void *cls, | ||
2657 | struct GNUNET_FS_DirScanner *ds, | ||
2658 | const char *filename, | ||
2659 | int is_directory, | ||
2660 | enum GNUNET_FS_DirScannerProgressUpdateReason reason); | ||
2661 | |||
2711 | 2662 | ||
2712 | /** | 2663 | /** |
2713 | * A node of a directory tree (produced by dirscanner) | 2664 | * A node of a directory tree (produced by dirscanner) |
@@ -2763,24 +2714,46 @@ struct GNUNET_FS_ShareTreeItem | |||
2763 | char *short_filename; | 2714 | char *short_filename; |
2764 | 2715 | ||
2765 | /** | 2716 | /** |
2766 | * 1 if this is a directory | 2717 | * Size of the file (if it is a file), in bytes |
2767 | */ | 2718 | */ |
2768 | char is_directory; | 2719 | uint64_t file_size; |
2769 | 2720 | ||
2770 | /** | 2721 | /** |
2771 | * Size of the file (if it's a file), in bytes | 2722 | * GNUNET_YES if this is a directory |
2772 | */ | 2723 | */ |
2773 | uint64_t file_size; | 2724 | int is_directory; |
2725 | |||
2774 | }; | 2726 | }; |
2775 | 2727 | ||
2776 | /* opaqe */ | 2728 | |
2729 | /** | ||
2730 | * Opaqe handle to an asynchronous directory scanning activity. | ||
2731 | */ | ||
2777 | struct GNUNET_FS_DirScanner; | 2732 | struct GNUNET_FS_DirScanner; |
2778 | 2733 | ||
2734 | |||
2735 | /** | ||
2736 | * Start a directory scanner thread. | ||
2737 | * | ||
2738 | * @param filename name of the directory to scan | ||
2739 | * @param GNUNET_YES to not to run libextractor on files (only build a tree) | ||
2740 | * @param ex if not NULL, must be a list of extra plugins for extractor | ||
2741 | * @param cb the callback to call when there are scanning progress messages | ||
2742 | * @param cb_cls closure for 'cb' | ||
2743 | * @return directory scanner object to be used for controlling the scanner | ||
2744 | */ | ||
2745 | struct GNUNET_FS_DirScanner * | ||
2746 | GNUNET_FS_directory_scan_start (const char *filename, | ||
2747 | int disable_extractor, | ||
2748 | const char *ex, | ||
2749 | GNUNET_FS_DirScannerProgressCallback cb, | ||
2750 | void *cb_cls); | ||
2751 | |||
2752 | |||
2779 | /** | 2753 | /** |
2780 | * Signals the scanner to finish the scan as fast as possible. | 2754 | * Signals the scanner to finish the scan as fast as possible. Does |
2781 | * Does not block. | 2755 | * not block. Can close the pipe if asked to, but that is only used |
2782 | * Can close the pipe if asked to, but that is only used by the | 2756 | * by the internal call to this function during cleanup. The client |
2783 | * internal call to this function during cleanup. The client | ||
2784 | * must understand the consequences of closing the pipe too early. | 2757 | * must understand the consequences of closing the pipe too early. |
2785 | * | 2758 | * |
2786 | * @param ds directory scanner structure | 2759 | * @param ds directory scanner structure |
@@ -2788,7 +2761,8 @@ struct GNUNET_FS_DirScanner; | |||
2788 | */ | 2761 | */ |
2789 | void | 2762 | void |
2790 | GNUNET_FS_directory_scan_finish (struct GNUNET_FS_DirScanner *ds, | 2763 | GNUNET_FS_directory_scan_finish (struct GNUNET_FS_DirScanner *ds, |
2791 | int close_pipe); | 2764 | int close_pipe); |
2765 | |||
2792 | 2766 | ||
2793 | /** | 2767 | /** |
2794 | * Signals the scanner thread to finish (in case it isn't finishing | 2768 | * Signals the scanner thread to finish (in case it isn't finishing |
@@ -2804,23 +2778,10 @@ GNUNET_FS_directory_scan_finish (struct GNUNET_FS_DirScanner *ds, | |||
2804 | struct GNUNET_FS_ShareTreeItem * | 2778 | struct GNUNET_FS_ShareTreeItem * |
2805 | GNUNET_FS_directory_scan_cleanup (struct GNUNET_FS_DirScanner *ds); | 2779 | GNUNET_FS_directory_scan_cleanup (struct GNUNET_FS_DirScanner *ds); |
2806 | 2780 | ||
2781 | |||
2807 | /** | 2782 | /** |
2808 | * Start a directory scanner thread. | 2783 | * opaque |
2809 | * | ||
2810 | * @param filename name of the directory to scan | ||
2811 | * @param GNUNET_YES to not to run libextractor on files (only build a tree) | ||
2812 | * @param ex if not NULL, must be a list of extra plugins for extractor | ||
2813 | * @param cb the callback to call when there are scanning progress messages | ||
2814 | * @param cls closure for 'cb' | ||
2815 | * @return directory scanner object to be used for controlling the scanner | ||
2816 | */ | 2784 | */ |
2817 | struct GNUNET_FS_DirScanner * | ||
2818 | GNUNET_FS_directory_scan_start (const char *filename, | ||
2819 | int disable_extractor, const char *ex, | ||
2820 | GNUNET_FS_DirScannerProgressCallback cb, void *cls); | ||
2821 | |||
2822 | |||
2823 | /* opaque */ | ||
2824 | struct GNUNET_FS_ProcessMetadataContext; | 2785 | struct GNUNET_FS_ProcessMetadataContext; |
2825 | 2786 | ||
2826 | /** | 2787 | /** |
@@ -2830,10 +2791,12 @@ struct GNUNET_FS_ProcessMetadataContext; | |||
2830 | * @param toplevel toplevel directory in the tree, returned by the scanner | 2791 | * @param toplevel toplevel directory in the tree, returned by the scanner |
2831 | * @param cb called after processing is done | 2792 | * @param cb called after processing is done |
2832 | * @param cls closure for 'cb' | 2793 | * @param cls closure for 'cb' |
2794 | * @return FIXME: what would this handle be used for? | ||
2833 | */ | 2795 | */ |
2834 | struct GNUNET_FS_ProcessMetadataContext * | 2796 | struct GNUNET_FS_ProcessMetadataContext * |
2835 | GNUNET_FS_trim_share_tree (struct GNUNET_FS_ShareTreeItem *toplevel, | 2797 | GNUNET_FS_trim_share_tree (struct GNUNET_FS_ShareTreeItem *toplevel, |
2836 | GNUNET_SCHEDULER_Task cb, void *cls); | 2798 | GNUNET_SCHEDULER_Task cb, void *cls); |
2799 | |||
2837 | 2800 | ||
2838 | 2801 | ||
2839 | #if 0 /* keep Emacsens' auto-indent happy */ | 2802 | #if 0 /* keep Emacsens' auto-indent happy */ |