2 This file is part of GNUnet
\r
3 (C) 2005-2012 Christian Grothoff (and other contributing authors)
\r
5 GNUnet is free software; you can redistribute it and/or modify
\r
6 it under the terms of the GNU General Public License as published
\r
7 by the Free Software Foundation; either version 2, or (at your
\r
8 option) any later version.
\r
10 GNUnet is distributed in the hope that it will be useful, but
\r
11 WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
13 General Public License for more details.
\r
15 You should have received a copy of the GNU General Public License
\r
16 along with GNUnet; see the file COPYING. If not, write to the
\r
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
\r
18 Boston, MA 02111-1307, USA.
\r
21 #include "platform.h"
\r
22 #include "gnunet_fs_service.h"
\r
23 #include "gnunet_scheduler_lib.h"
\r
26 * Entry for each unique keyword to track how often
\r
27 * it occured. Contains the keyword and the counter.
\r
29 struct KeywordCounter
\r
33 * Keyword that was found.
\r
38 * How many files have this keyword?
\r
43 * This is a doubly-linked list
\r
45 struct KeywordCounter *prev;
\r
48 * This is a doubly-linked list
\r
50 struct KeywordCounter *next;
\r
54 * Aggregate information we keep for meta data in each directory.
\r
59 * The actual meta data.
\r
64 * Number of bytes in 'data'.
\r
69 * Name of the plugin that provided that piece of metadata
\r
71 const char *plugin_name;
\r
76 enum EXTRACTOR_MetaType type;
\r
79 * Format of the data
\r
81 enum EXTRACTOR_MetaFormat format;
\r
84 * MIME-type of the metadata itself
\r
86 const char *data_mime_type;
\r
89 * How many files have meta entries matching this value?
\r
90 * (type and format do not have to match).
\r
95 * This is a doubly-linked list
\r
97 struct MetaCounter *prev;
\r
100 * This is a doubly-linked list
\r
102 struct MetaCounter *next;
\r
106 * Execution context for 'add_dir'
\r
107 * Owned by the initiator thread.
\r
109 struct AddDirContext
\r
112 * Parent directory (used to access keyword and metadata counters,
\r
114 * After the scan is finished, it will contain a pointer to the
\r
115 * top-level directory entry in the directory tree built by the
\r
118 struct ShareTreeItem *parent;
\r
121 * Expanded filename (as given by the scan initiator).
\r
122 * The scanner thread stores a copy here, and frees it when it finishes.
\r
124 char *filename_expanded;
\r
127 * A synchronization privitive. Whenever its state is altered,
\r
128 * it means that the initiator wants the scanner to wrap up.
\r
129 * It is owned by the initiator thread.
\r
138 * 1 if the scanner should stop, 0 otherwise. Set in response
\r
139 * to communication errors or when the initiator wants the scanning
\r
145 * Handle of the pipe end into which the progress messages are written
\r
146 * The pipe is owned by the initiator thread, and there's no way to
\r
147 * close this end without having access to the pipe, so it won't
\r
148 * be closed by the scanner thread.
\r
149 * The initiator MUST keep it alive until the scanner thread is finished.
\r
151 const struct GNUNET_DISK_FileHandle *progress_write;
\r
155 * List of libextractor plugins to use for extracting.
\r
156 * Initialized when the scan starts, removed when it finishes.
\r
158 struct EXTRACTOR_PluginList *plugins;
\r
162 * An opaque structure a pointer to which is returned to the
\r
163 * caller to be used to control the scanner.
\r
165 struct GNUNET_FS_DirScanner
\r
168 * A synchronization privitive that is used to signal the scanner to stop.
\r
169 * Owned by the initiator thread.
\r
178 * A thread object for the scanner thread.
\r
179 * Owned by the initiator thread.
\r
188 * A task for reading progress messages from the scanner.
\r
190 GNUNET_SCHEDULER_TaskIdentifier progress_read_task;
\r
193 * The end of the pipe that is used to read progress messages.
\r
195 const struct GNUNET_DISK_FileHandle *progress_read;
\r
198 * The pipe that is used to read progress messages.
\r
199 * Owned (along with both of its ends) by the initiator thread.
\r
200 * Only closed after the scanner thread is finished.
\r
202 struct GNUNET_DISK_PipeHandle *progress_pipe;
\r
205 * The function that will be called every time there's a progress
\r
208 GNUNET_FS_DirScannerProgressCallback progress_callback;
\r
211 * A closure for progress_callback.
\r
216 * A pointer to the context of the scanner.
\r
217 * Owned by the initiator thread.
\r
218 * Initiator thread shouldn't touch it until the scanner thread
\r
221 struct AddDirContext *adc;
\r
225 * A structure that forms a singly-linked list that serves as a stack
\r
226 * for metadata-processing function.
\r
228 struct ProcessMetadataStackItem
\r
231 * A pointer to metadata-processing context.
\r
232 * The same in every stack item.
\r
234 struct ProcessMetadataContext *ctx;
\r
237 * This is a singly-linked list. A pointer to its end is kept, and
\r
238 * this pointer is used to walk it backwards.
\r
240 struct ProcessMetadataStackItem *parent;
\r
243 * Map from the hash over the keyword to an 'struct KeywordCounter *'
\r
244 * counter that says how often this keyword was
\r
245 * encountered in the current directory.
\r
247 struct GNUNET_CONTAINER_MultiHashMap *keywordcounter;
\r
250 * Map from the hash over the metadata to an 'struct MetaCounter *'
\r
251 * counter that says how often this metadata was
\r
252 * encountered in the current directory.
\r
254 struct GNUNET_CONTAINER_MultiHashMap *metacounter;
\r
257 * Number of files in the current directory.
\r
259 unsigned int dir_entry_count;
\r
262 * Keywords to exclude from using for KSK since they'll be associated
\r
263 * with the parent as well. NULL for nothing blocked.
\r
265 struct GNUNET_FS_Uri *exclude_ksk;
\r
268 * A share tree item that is being processed.
\r
270 struct ShareTreeItem *item;
\r
273 * Set to GNUNET_YES to indicate that the directory pointer by 'item'
\r
274 * was processed, and we should move on to the next.
\r
275 * Otherwise the directory will be recursed into.
\r
282 * The structure to keep the state of metadata processing
\r
284 struct ProcessMetadataContext
\r
287 * The top of the stack.
\r
289 struct ProcessMetadataStackItem *stack;
\r
292 * Callback to invoke when processing is finished
\r
294 GNUNET_SCHEDULER_Task cb;
\r
302 * Toplevel directory item of the tree to process.
\r
304 struct ShareTreeItem *toplevel;
\r
308 * Called every now and then by the scanner.
\r
309 * Checks the synchronization privitive.
\r
310 * Returns 1 if the scanner should stop, 0 otherwise.
\r
313 should_stop (struct AddDirContext *adc)
\r
316 if (WaitForSingleObject (adc->stop, 0) == WAIT_TIMEOUT)
\r
322 sem_getvalue(adc->stop, &value);
\r
333 * Write progress message.
\r
335 * <reason><filename length><filename><directory flag>
\r
336 * If filename is NULL, filename is not written, and its length
\r
337 * is written as 0, and nothing else is written. It signals the initiator
\r
338 * thread that the scanner is finished, and that it can now join its thread.
\r
340 * Also checks if the initiator thread wants the scanner to stop,
\r
341 * Returns 1 to stop scanning (if the signal was received, or
\r
342 * if the pipe was broken somehow), 0 otherwise.
\r
345 write_progress (struct AddDirContext *adc, const char *filename,
\r
346 char is_directory, enum GNUNET_DirScannerProgressUpdateReason reason)
\r
348 size_t filename_len;
\r
350 size_t total_write;
\r
351 if ((adc->do_stop || should_stop (adc)) && reason != GNUNET_DIR_SCANNER_ASKED_TO_STOP
\r
352 && reason != GNUNET_DIR_SCANNER_FINISHED)
\r
354 total_write = wr = GNUNET_DISK_file_write (adc->progress_write,
\r
355 &reason, sizeof (reason));
\r
356 while (wr > 0 && total_write < sizeof (reason))
\r
358 total_write = wr = GNUNET_DISK_file_write (adc->progress_write,
\r
359 &((char *)&reason)[total_write], sizeof (reason) - total_write);
\r
363 if (sizeof (reason) != wr)
\r
366 filename_len = strlen (filename) + 1;
\r
369 total_write = wr = GNUNET_DISK_file_write (adc->progress_write,
\r
370 &filename_len, sizeof (size_t));
\r
371 while (wr > 0 && total_write < sizeof (size_t))
\r
373 total_write = wr = GNUNET_DISK_file_write (adc->progress_write,
\r
374 &((char *)&filename_len)[total_write], sizeof (size_t) - total_write);
\r
378 if (sizeof (size_t) != wr)
\r
382 total_write = wr = GNUNET_DISK_file_write (adc->progress_write,
\r
383 filename, filename_len);
\r
384 while (wr > 0 && total_write < filename_len)
\r
386 total_write = wr = GNUNET_DISK_file_write (adc->progress_write,
\r
387 &((char *)filename)[total_write], filename_len - total_write);
\r
391 if (filename_len != wr)
\r
393 total_write = wr = GNUNET_DISK_file_write (adc->progress_write,
\r
394 &is_directory, sizeof (char));
\r
395 while (wr > 0 && total_write < sizeof (char))
\r
397 total_write = wr = GNUNET_DISK_file_write (adc->progress_write,
\r
398 &((char *)&is_directory)[total_write], sizeof (char) - total_write);
\r
402 if (sizeof (char) != wr)
\r
409 * Add the given keyword to the
\r
410 * keyword statistics tracker.
\r
412 * @param cls closure (user-defined)
\r
413 * @param keyword the keyword to count
\r
414 * @param is_mandatory ignored
\r
415 * @return always GNUNET_OK
\r
418 add_to_keyword_counter (void *cls, const char *keyword, int is_mandatory)
\r
420 struct GNUNET_CONTAINER_MultiHashMap *mcm = cls;
\r
421 struct KeywordCounter *cnt, *first_cnt;
\r
422 GNUNET_HashCode hc;
\r
425 klen = strlen (keyword) + 1;
\r
426 GNUNET_CRYPTO_hash (keyword, klen - 1, &hc);
\r
427 /* Since the map might contain multiple values per keyword, we only
\r
428 * store one value, and attach all other to it, forming a linked list.
\r
429 * Somewhat easier than retrieving multiple items via callback.
\r
431 first_cnt = GNUNET_CONTAINER_multihashmap_get (mcm, &hc);
\r
432 for (cnt = first_cnt; cnt && strcmp (cnt->value, keyword) != 0; cnt = cnt->next);
\r
435 cnt = GNUNET_malloc (sizeof (struct KeywordCounter) + klen);
\r
436 cnt->value = (const char *) &cnt[1];
\r
437 memcpy (&cnt[1], keyword, klen);
\r
438 if (first_cnt != NULL)
\r
440 if (first_cnt->prev != NULL)
\r
442 first_cnt->prev->next = cnt;
\r
443 cnt->prev = first_cnt->prev;
\r
445 first_cnt->prev = cnt;
\r
446 cnt->next = first_cnt;
\r
449 GNUNET_CONTAINER_multihashmap_put (mcm, &hc, cnt,
\r
450 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
\r
457 * Type of a function that libextractor calls for each
\r
458 * meta data item found.
\r
460 * @param cls the container multihashmap to update
\r
461 * @param plugin_name name of the plugin that produced this value;
\r
462 * special values can be used (i.e. '<zlib>' for zlib being
\r
463 * used in the main libextractor library and yielding
\r
465 * @param type libextractor-type describing the meta data
\r
466 * @param format basic format information about data
\r
467 * @param data_mime_type mime-type of data (not of the original file);
\r
468 * can be NULL (if mime-type is not known)
\r
469 * @param data actual meta-data found
\r
470 * @param data_len number of bytes in data
\r
471 * @return GNUNET_OK to continue extracting / iterating
\r
474 add_to_meta_counter (void *cls, const char *plugin_name,
\r
475 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
\r
476 const char *data_mime_type, const char *data, size_t data_len)
\r
478 struct GNUNET_CONTAINER_MultiHashMap *map = cls;
\r
479 GNUNET_HashCode key;
\r
480 struct MetaCounter *cnt, *first_cnt;
\r
482 GNUNET_CRYPTO_hash (data, data_len, &key);
\r
483 first_cnt = GNUNET_CONTAINER_multihashmap_get (map, &key);
\r
484 for (cnt = first_cnt; cnt
\r
485 && cnt->data_size != data_len
\r
486 && memcmp (cnt->data, data, cnt->data_size) != 0; cnt = cnt->next);
\r
489 cnt = GNUNET_malloc (sizeof (struct MetaCounter));
\r
491 cnt->data_size = data_len;
\r
492 cnt->plugin_name = plugin_name;
\r
494 cnt->format = format;
\r
495 cnt->data_mime_type = data_mime_type;
\r
497 if (first_cnt != NULL)
\r
499 if (first_cnt->prev != NULL)
\r
501 first_cnt->prev->next = cnt;
\r
502 cnt->prev = first_cnt->prev;
\r
504 first_cnt->prev = cnt;
\r
505 cnt->next = first_cnt;
\r
508 GNUNET_CONTAINER_multihashmap_put (map, &key, cnt,
\r
509 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
\r
516 * Allocates a struct ShareTreeItem and adds it to its parent.
\r
518 static struct ShareTreeItem *
\r
519 make_item (struct ShareTreeItem *parent)
\r
521 struct ShareTreeItem *item;
\r
522 item = GNUNET_malloc (sizeof (struct ShareTreeItem));
\r
524 item->parent = parent;
\r
526 GNUNET_CONTAINER_DLL_insert (parent->children_head, parent->children_tail,
\r
532 * Extract metadata from a file and add it to the share tree
\r
534 * @param adc context to modify
\r
535 * @param filename name of the file to process
\r
538 extract_file (struct AddDirContext *adc, const char *filename)
\r
540 struct ShareTreeItem *item;
\r
541 const char *short_fn;
\r
543 item = make_item (adc->parent);
\r
545 GNUNET_DISK_file_size (filename, &item->file_size, GNUNET_YES);
\r
546 item->is_directory = GNUNET_NO;
\r
548 item->meta = GNUNET_CONTAINER_meta_data_create ();
\r
549 GNUNET_FS_meta_data_extract_from_file (item->meta, filename,
\r
551 GNUNET_CONTAINER_meta_data_delete (item->meta, EXTRACTOR_METATYPE_FILENAME,
\r
553 short_fn = GNUNET_STRINGS_get_short_name (filename);
\r
555 item->filename = GNUNET_strdup (filename);
\r
556 item->short_filename = GNUNET_strdup (short_fn);
\r
558 GNUNET_CONTAINER_meta_data_insert (item->meta, "<libgnunetfs>",
\r
559 EXTRACTOR_METATYPE_FILENAME,
\r
560 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
\r
561 short_fn, strlen (short_fn) + 1);
\r
565 * Remove the keyword from the ksk URI.
\r
567 * @param cls the ksk uri
\r
568 * @param keyword the word to remove
\r
569 * @param is_mandatory ignored
\r
570 * @return always GNUNET_OK
\r
573 remove_keyword (void *cls, const char *keyword, int is_mandatory)
\r
575 struct GNUNET_FS_Uri *ksk = cls;
\r
577 GNUNET_FS_uri_ksk_remove_keyword (ksk, keyword);
\r
582 * Remove keywords from current directory's children, if they are
\r
583 * in the exluded keywords list of that directory.
\r
585 * @param cls the ksk uri
\r
586 * @param keyword the word to remove
\r
587 * @param is_mandatory ignored
\r
588 * @return always GNUNET_OK
\r
591 remove_keywords (struct ProcessMetadataStackItem *stack, struct ShareTreeItem *dir)
\r
593 struct ShareTreeItem *item;
\r
595 for (item = dir->children_head; item; item = item->next)
\r
597 if (stack->exclude_ksk != NULL)
\r
598 GNUNET_FS_uri_ksk_get_keywords (stack->exclude_ksk, &remove_keyword, item->ksk_uri);
\r
604 * Context passed to 'migrate_and_drop'.
\r
606 struct KeywordProcessContext
\r
609 * All the keywords we migrated to the parent.
\r
611 struct GNUNET_FS_Uri *ksk;
\r
614 * How often does a keyword have to occur to be
\r
615 * migrated to the parent?
\r
617 unsigned int threshold;
\r
621 * Context passed to 'migrate_and_drop'.
\r
623 struct MetaProcessContext
\r
626 * All the metadata we copy to the parent.
\r
628 struct GNUNET_CONTAINER_MetaData *meta;
\r
631 * How often does a metadata have to occur to be
\r
632 * migrated to the parent?
\r
634 unsigned int threshold;
\r
639 * Move "frequent" keywords over to the
\r
640 * target ksk uri, free the counters.
\r
644 migrate_and_drop (void *cls, const GNUNET_HashCode * key, void *value)
\r
646 struct KeywordProcessContext *kpc = cls;
\r
647 struct KeywordCounter *counter = value;
\r
649 if (counter->count >= kpc->threshold && counter->count > 1)
\r
651 GNUNET_FS_uri_ksk_add_keyword (kpc->ksk, counter->value, GNUNET_NO);
\r
653 GNUNET_free (counter);
\r
657 * Copy "frequent" metadata items over to the
\r
658 * target metadata container, free the counters.
\r
662 migrate_and_drop_metadata (void *cls, const GNUNET_HashCode * key, void *value)
\r
664 struct MetaProcessContext *mpc = cls;
\r
665 struct MetaCounter *counter = value;
\r
667 if (counter->count >= mpc->threshold && counter->count > 1)
\r
669 GNUNET_CONTAINER_meta_data_insert (mpc->meta,
\r
670 counter->plugin_name,
\r
673 counter->data_mime_type, counter->data,
\r
674 counter->data_size);
\r
676 GNUNET_free (counter);
\r
681 * Go over the collected keywords from all entries in the
\r
682 * directory and push common keywords up one level (by
\r
683 * adding it to the returned struct). Do the same for metadata.
\r
684 * Destroys keywordcounter and metacoutner for current directory.
\r
686 * @param adc collection of child meta data
\r
687 * @param exclude_ksk pointer to where moveable keywords will be stored
\r
688 * @param copy_meta pointer to where copyable metadata will be stored
\r
691 process_keywords_and_metadata (struct ProcessMetadataStackItem *stack,
\r
692 struct GNUNET_FS_Uri **exclude_ksk,
\r
693 struct GNUNET_CONTAINER_MetaData **copy_meta)
\r
695 struct KeywordProcessContext kpc;
\r
696 struct MetaProcessContext mpc;
\r
697 struct GNUNET_CONTAINER_MetaData *tmp;
\r
699 /* Surprisingly, it's impossible to create a ksk with 0 keywords directly.
\r
700 * But we can create one from an empty metadata set
\r
702 tmp = GNUNET_CONTAINER_meta_data_create ();
\r
703 kpc.ksk = GNUNET_FS_uri_ksk_create_from_meta_data (tmp);
\r
704 GNUNET_CONTAINER_meta_data_destroy (tmp);
\r
705 mpc.meta = GNUNET_CONTAINER_meta_data_create ();
\r
707 kpc.threshold = mpc.threshold = (stack->dir_entry_count + 1) / 2; /* 50% */
\r
709 GNUNET_CONTAINER_multihashmap_iterate (stack->keywordcounter,
\r
710 &migrate_and_drop, &kpc);
\r
711 GNUNET_CONTAINER_multihashmap_iterate (stack->metacounter,
\r
712 &migrate_and_drop_metadata, &mpc);
\r
714 GNUNET_CONTAINER_multihashmap_destroy (stack->keywordcounter);
\r
715 GNUNET_CONTAINER_multihashmap_destroy (stack->metacounter);
\r
716 *exclude_ksk = kpc.ksk;
\r
717 *copy_meta = mpc.meta;
\r
721 * Function called by the directory iterator to
\r
722 * (recursively) add all of the files in the
\r
723 * directory to the tree.
\r
724 * Called by the directory scanner to initiate the
\r
726 * TODO: find a way to make it non-recursive.
\r
728 * @param cls the 'struct AddDirContext*' we're in
\r
729 * @param filename file or directory to scan
\r
732 scan_directory (void *cls, const char *filename)
\r
734 struct AddDirContext *adc = cls, recurse_adc;
\r
736 struct ShareTreeItem *item;
\r
737 const char *short_fn;
\r
742 return GNUNET_SYSERR;
\r
744 /* If the file doesn't exist (or is not statable for any other reason,
\r
745 * skip it, and report it.
\r
747 if (0 != STAT (filename, &sbuf))
\r
749 do_stop = write_progress (adc, filename, S_ISDIR (sbuf.st_mode),
\r
750 GNUNET_DIR_SCANNER_DOES_NOT_EXIST);
\r
754 /* Report the progress */
\r
755 do_stop = write_progress (adc, filename, S_ISDIR (sbuf.st_mode),
\r
756 GNUNET_DIR_SCANNER_NEW_FILE);
\r
759 /* We were asked to stop, acknowledge that and return */
\r
760 do_stop = write_progress (adc, filename, S_ISDIR (sbuf.st_mode),
\r
761 GNUNET_DIR_SCANNER_ASKED_TO_STOP);
\r
762 return GNUNET_SYSERR;
\r
765 if (!S_ISDIR (sbuf.st_mode))
\r
766 extract_file (adc, filename);
\r
769 item = make_item (adc->parent);
\r
770 item->meta = GNUNET_CONTAINER_meta_data_create ();
\r
772 item->is_directory = GNUNET_YES;
\r
774 /* copy fields from adc */
\r
775 recurse_adc = *adc;
\r
776 /* replace recurse_adc contents with the ones for this directory */
\r
777 recurse_adc.parent = item;
\r
779 /* recurse into directory */
\r
780 GNUNET_DISK_directory_scan (filename, &scan_directory, &recurse_adc);
\r
782 short_fn = GNUNET_STRINGS_get_short_name (filename);
\r
784 item->filename = GNUNET_strdup (filename);
\r
785 item->short_filename = GNUNET_strdup (short_fn);
\r
787 if (adc->parent == NULL)
\r
789 /* we're finished with the scan, make sure caller gets the top-level
\r
790 * directory pointer
\r
792 adc->parent = item;
\r
799 * Signals the scanner to finish the scan as fast as possible.
\r
801 * Can close the pipe if asked to, but that is only used by the
\r
802 * internal call to this function during cleanup. The client
\r
803 * must understand the consequences of closing the pipe too early.
\r
805 * @param ds directory scanner structure
\r
806 * @param close_pipe GNUNET_YES to close
\r
809 GNUNET_FS_directory_scan_finish (struct GNUNET_FS_DirScanner *ds,
\r
813 SetEvent (ds->stop);
\r
815 sem_post (&ds->stop);
\r
819 if (ds->progress_read_task != GNUNET_SCHEDULER_NO_TASK)
\r
821 GNUNET_SCHEDULER_cancel (ds->progress_read_task);
\r
822 ds->progress_read_task = GNUNET_SCHEDULER_NO_TASK;
\r
824 GNUNET_DISK_pipe_close_end (ds->progress_pipe, GNUNET_DISK_PIPE_END_READ);
\r
825 ds->progress_read = NULL;
\r
830 * Signals the scanner thread to finish (in case it isn't finishing
\r
831 * already) and joins the scanner thread. Closes the pipes, frees the
\r
832 * scanner contexts (both of them), returns the results of the scan.
\r
833 * Results are valid (and have to be freed) even if the scanner had
\r
834 * an error or was rushed to finish prematurely.
\r
835 * Blocks until the scanner is finished.
\r
837 * @param ds directory scanner structure
\r
838 * @return the results of the scan (a directory tree)
\r
840 struct ShareTreeItem *
\r
841 GNUNET_FS_directory_scan_cleanup (struct GNUNET_FS_DirScanner *ds)
\r
843 struct ShareTreeItem *result;
\r
845 GNUNET_FS_directory_scan_finish (ds, GNUNET_YES);
\r
847 WaitForSingleObject (ds->thread, INFINITE);
\r
848 CloseHandle (ds->stop);
\r
849 CloseHandle (ds->thread);
\r
851 pthread_join (ds->thread, NULL);
\r
852 sem_destroy (&ds->stop);
\r
853 pthread_detach (ds->thread);
\r
856 GNUNET_DISK_pipe_close (ds->progress_pipe);
\r
857 result = ds->adc->parent;
\r
858 GNUNET_free (ds->adc);
\r
864 * The function from which the scanner thread starts
\r
871 run_directory_scan_thread (struct AddDirContext *adc)
\r
873 scan_directory (adc, adc->filename_expanded);
\r
874 GNUNET_free (adc->filename_expanded);
\r
875 if (adc->plugins != NULL)
\r
876 EXTRACTOR_plugin_remove_all (adc->plugins);
\r
877 /* Tell the initiator that we're finished, it can now join the thread */
\r
878 write_progress (adc, NULL, 0, GNUNET_DIR_SCANNER_FINISHED);
\r
883 * Called every time there is data to read from the scanner.
\r
884 * Calls the scanner progress handler.
\r
886 * @param cls the closure (directory scanner object)
\r
887 * @param tc task context in which the task is running
\r
890 read_progress_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
\r
892 struct GNUNET_FS_DirScanner *ds;
\r
894 enum GNUNET_DirScannerProgressUpdateReason reason;
\r
896 ssize_t total_read;
\r
898 size_t filename_len;
\r
904 ds->progress_read_task = GNUNET_SCHEDULER_NO_TASK;
\r
906 if (!(tc->reason & GNUNET_SCHEDULER_REASON_READ_READY))
\r
908 ds->progress_callback (ds->cls, ds, NULL, 0, GNUNET_DIR_SCANNER_SHUTDOWN);
\r
912 /* Read one message. If message is malformed or can't be read, end the scanner */
\r
913 total_read = rd = GNUNET_DISK_file_read (ds->progress_read, &reason, sizeof (reason));
\r
914 while (rd > 0 && total_read < sizeof (reason))
\r
916 rd = GNUNET_DISK_file_read (ds->progress_read,
\r
917 &((char *) &reason)[total_read],
\r
918 sizeof (reason) - total_read);
\r
922 if (total_read != sizeof (reason)
\r
923 || reason <= GNUNET_DIR_SCANNER_FIRST
\r
924 || reason >= GNUNET_DIR_SCANNER_LAST)
\r
927 reason = GNUNET_DIR_SCANNER_PROTOCOL_ERROR;
\r
932 total_read = rd = GNUNET_DISK_file_read (ds->progress_read, &filename_len,
\r
934 while (rd > 0 && total_read < sizeof (size_t))
\r
936 rd = GNUNET_DISK_file_read (ds->progress_read,
\r
937 &((char *) &filename_len)[total_read],
\r
938 sizeof (size_t) - total_read);
\r
942 if (rd != sizeof (size_t))
\r
945 reason = GNUNET_DIR_SCANNER_PROTOCOL_ERROR;
\r
950 if (filename_len == 0)
\r
952 else if (filename_len > MAX_PATH)
\r
955 reason = GNUNET_DIR_SCANNER_PROTOCOL_ERROR;
\r
960 filename = GNUNET_malloc (filename_len);
\r
961 total_read = rd = GNUNET_DISK_file_read (ds->progress_read, filename,
\r
963 while (rd > 0 && total_read < filename_len)
\r
965 rd = GNUNET_DISK_file_read (ds->progress_read, &filename[total_read],
\r
966 filename_len - total_read);
\r
970 if (rd != filename_len)
\r
972 GNUNET_free (filename);
\r
973 reason = GNUNET_DIR_SCANNER_PROTOCOL_ERROR;
\r
977 if (!end_it && filename_len > 0)
\r
979 total_read = rd = GNUNET_DISK_file_read (ds->progress_read, &is_directory,
\r
981 while (rd > 0 && total_read < sizeof (char))
\r
983 rd = GNUNET_DISK_file_read (ds->progress_read, &(&is_directory)[total_read],
\r
984 sizeof (char) - total_read);
\r
988 if (rd != sizeof (char))
\r
990 GNUNET_free (filename);
\r
991 reason = GNUNET_DIR_SCANNER_PROTOCOL_ERROR;
\r
997 end_it = ds->progress_callback (ds->cls, ds, (const char *) filename, is_directory, reason);
\r
998 GNUNET_free (filename);
\r
1001 ds->progress_read_task = GNUNET_SCHEDULER_add_read_file (
\r
1002 GNUNET_TIME_UNIT_FOREVER_REL, ds->progress_read, &read_progress_task,
\r
1008 ds->progress_callback (ds->cls, ds, NULL, 0, reason);
\r
1014 * Start a directory scanner thread.
\r
1016 * @param filename name of the directory to scan
\r
1017 * @param GNUNET_YES to not to run libextractor on files (only build a tree)
\r
1018 * @param ex if not NULL, must be a list of extra plugins for extractor
\r
1019 * @param cb the callback to call when there are scanning progress messages
\r
1020 * @param cls closure for 'cb'
\r
1021 * @return directory scanner object to be used for controlling the scanner
\r
1023 struct GNUNET_FS_DirScanner *
\r
1024 GNUNET_FS_directory_scan_start (const char *filename,
\r
1025 int disable_extractor, const char *ex,
\r
1026 GNUNET_FS_DirScannerProgressCallback cb, void *cls)
\r
1029 struct AddDirContext *adc;
\r
1030 char *filename_expanded;
\r
1031 struct GNUNET_FS_DirScanner *ds;
\r
1032 struct GNUNET_DISK_PipeHandle *progress_pipe;
\r
1035 if (0 != STAT (filename, &sbuf))
\r
1037 /* TODO: consider generalizing this for files too! */
\r
1038 if (!S_ISDIR (sbuf.st_mode))
\r
1043 /* scan_directory() is guaranteed to be given expanded filenames,
\r
1044 * so expand we will!
\r
1046 filename_expanded = GNUNET_STRINGS_filename_expand (filename);
\r
1047 if (filename_expanded == NULL)
\r
1050 progress_pipe = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
\r
1051 if (progress_pipe == NULL)
\r
1053 GNUNET_free (filename_expanded);
\r
1057 adc = GNUNET_malloc (sizeof (struct AddDirContext));
\r
1059 ds = GNUNET_malloc (sizeof (struct GNUNET_FS_DirScanner));
\r
1064 ds->stop = CreateEvent (NULL, TRUE, FALSE, NULL);
\r
1065 adc->stop = ds->stop;
\r
1066 ok = ds->stop != INVALID_HANDLE_VALUE;
\r
1068 ok = !sem_init (&ds->stop, 0, 0);
\r
1073 GNUNET_free (adc);
\r
1075 GNUNET_free (filename_expanded);
\r
1076 GNUNET_DISK_pipe_close (progress_pipe);
\r
1080 adc->plugins = NULL;
\r
1081 if (!disable_extractor)
\r
1083 adc->plugins = EXTRACTOR_plugin_add_defaults (
\r
1084 EXTRACTOR_OPTION_DEFAULT_POLICY);
\r
1085 if (ex && strlen (ex) > 0)
\r
1086 adc->plugins = EXTRACTOR_plugin_add_config (adc->plugins, ex,
\r
1087 EXTRACTOR_OPTION_DEFAULT_POLICY);
\r
1090 adc->filename_expanded = filename_expanded;
\r
1091 adc->progress_write = GNUNET_DISK_pipe_handle (progress_pipe,
\r
1092 GNUNET_DISK_PIPE_END_WRITE);
\r
1095 ds->progress_read = GNUNET_DISK_pipe_handle (progress_pipe,
\r
1096 GNUNET_DISK_PIPE_END_READ);
\r
1099 ds->thread = CreateThread (NULL, 0,
\r
1100 (LPTHREAD_START_ROUTINE) &run_directory_scan_thread, (LPVOID) adc,
\r
1102 ok = ds->thread != NULL;
\r
1104 ok = !pthread_create (&ds->thread, NULL, &run_directory_scan_thread,
\r
1109 GNUNET_free (adc);
\r
1110 GNUNET_free (filename_expanded);
\r
1111 GNUNET_DISK_pipe_close (progress_pipe);
\r
1116 ds->progress_callback = cb;
\r
1119 ds->progress_pipe = progress_pipe;
\r
1121 ds->progress_read_task = GNUNET_SCHEDULER_add_read_file (
\r
1122 GNUNET_TIME_UNIT_FOREVER_REL, ds->progress_read, &read_progress_task,
\r
1129 * Task that post-processes the share item tree.
\r
1130 * This processing has to be done in the main thread, because
\r
1131 * it requires access to libgcrypt's hashing functions, and
\r
1132 * libgcrypt is not thread-safe without some special magic.
\r
1134 * @param cls top of the stack
\r
1135 * @param tc task context
\r
1138 trim_share_tree_task (void *cls,
\r
1139 const struct GNUNET_SCHEDULER_TaskContext *tc)
\r
1141 struct ProcessMetadataStackItem *stack = cls;
\r
1142 struct ProcessMetadataStackItem *next = stack;
\r
1143 /* FIXME: figure out what to do when tc says we're shutting down */
\r
1145 /* item == NULL means that we've just finished going over the children of
\r
1146 * current directory.
\r
1148 if (stack->item == NULL)
\r
1150 if (stack->parent->item != NULL)
\r
1152 /* end of a directory */
\r
1153 struct GNUNET_FS_Uri *ksk;
\r
1155 /* use keyword and metadata counters to create lists of keywords to move
\r
1156 * and metadata to copy.
\r
1158 process_keywords_and_metadata (stack, &stack->parent->exclude_ksk, &stack->parent->item->meta);
\r
1160 /* create keywords from metadata (copies all text-metadata as keywords,
\r
1161 * AND parses the directory name we've just added, producing even more
\r
1163 * then merge these keywords with the ones moved from children.
\r
1165 ksk = GNUNET_FS_uri_ksk_create_from_meta_data (stack->parent->item->meta);
\r
1166 stack->parent->item->ksk_uri = GNUNET_FS_uri_ksk_merge (ksk, stack->parent->exclude_ksk);
\r
1167 GNUNET_FS_uri_destroy (ksk);
\r
1169 /* remove moved keywords from children (complete the move) */
\r
1170 remove_keywords (stack->parent, stack->parent->item);
\r
1171 GNUNET_FS_uri_destroy (stack->parent->exclude_ksk);
\r
1173 /* go up the stack */
\r
1174 next = stack->parent;
\r
1175 GNUNET_free (stack);
\r
1176 next->end_directory = GNUNET_YES;
\r
1180 /* we've just finished processing the toplevel directory */
\r
1181 struct ProcessMetadataContext *ctx = stack->ctx;
\r
1183 GNUNET_SCHEDULER_add_continuation (ctx->cb, ctx->cls,
\r
1184 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
\r
1185 GNUNET_free (stack->parent);
\r
1186 GNUNET_free (stack);
\r
1187 GNUNET_free (ctx);
\r
1190 else if (stack->item->is_directory
\r
1191 && !stack->end_directory
\r
1192 && stack->item->children_head != NULL)
\r
1194 /* recurse into subdirectory */
\r
1195 next = GNUNET_malloc (sizeof (struct ProcessMetadataStackItem));
\r
1196 next->ctx = stack->ctx;
\r
1197 next->item = stack->item->children_head;
\r
1198 next->keywordcounter = GNUNET_CONTAINER_multihashmap_create (1024);
\r
1199 next->metacounter = GNUNET_CONTAINER_multihashmap_create (1024);
\r
1200 next->dir_entry_count = 0;
\r
1201 next->parent = stack;
\r
1205 /* process a child entry (a file or a directory) and move to the next one*/
\r
1206 if (stack->item->is_directory)
\r
1207 stack->end_directory = GNUNET_NO;
\r
1208 stack->dir_entry_count++;
\r
1209 GNUNET_CONTAINER_meta_data_iterate (stack->item->meta, &add_to_meta_counter, stack->metacounter);
\r
1211 if (stack->item->is_directory)
\r
1213 char *user = getenv ("USER");
\r
1214 if ((user == NULL) || (0 != strncasecmp (user, stack->item->short_filename, strlen(user))))
\r
1216 /* only use filename if it doesn't match $USER */
\r
1217 GNUNET_CONTAINER_meta_data_insert (stack->item->meta, "<libgnunetfs>",
\r
1218 EXTRACTOR_METATYPE_FILENAME,
\r
1219 EXTRACTOR_METAFORMAT_UTF8,
\r
1220 "text/plain", stack->item->short_filename,
\r
1221 strlen (stack->item->short_filename) + 1);
\r
1222 GNUNET_CONTAINER_meta_data_insert (stack->item->meta, "<libgnunetfs>",
\r
1223 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
\r
1224 EXTRACTOR_METAFORMAT_UTF8,
\r
1225 "text/plain", stack->item->short_filename,
\r
1226 strlen (stack->item->short_filename) + 1);
\r
1230 stack->item->ksk_uri = GNUNET_FS_uri_ksk_create_from_meta_data (stack->item->meta);
\r
1231 GNUNET_FS_uri_ksk_get_keywords (stack->item->ksk_uri, &add_to_keyword_counter, stack->keywordcounter);
\r
1232 stack->item = stack->item->next;
\r
1234 /* Call this task again later, if there are more entries to process */
\r
1236 GNUNET_SCHEDULER_add_continuation (&trim_share_tree_task, next,
\r
1237 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
\r
1241 * Process a share item tree, moving frequent keywords up and
\r
1242 * copying frequent metadata up.
\r
1244 * @param toplevel toplevel directory in the tree, returned by the scanner
\r
1245 * @param cb called after processing is done
\r
1246 * @param cls closure for 'cb'
\r
1248 struct ProcessMetadataContext *
\r
1249 GNUNET_FS_trim_share_tree (struct ShareTreeItem *toplevel,
\r
1250 GNUNET_SCHEDULER_Task cb, void *cls)
\r
1252 struct ProcessMetadataContext *ret;
\r
1254 if (toplevel == NULL)
\r
1256 struct GNUNET_SCHEDULER_TaskContext tc;
\r
1257 tc.reason = GNUNET_SCHEDULER_REASON_PREREQ_DONE;
\r
1262 ret = GNUNET_malloc (sizeof (struct ProcessMetadataContext));
\r
1263 ret->toplevel = toplevel;
\r
1264 ret->stack = GNUNET_malloc (sizeof (struct ProcessMetadataStackItem));
\r
1265 ret->stack->ctx = ret;
\r
1266 ret->stack->item = toplevel;
\r
1267 ret->stack->keywordcounter = GNUNET_CONTAINER_multihashmap_create (1024);
\r
1268 ret->stack->metacounter = GNUNET_CONTAINER_multihashmap_create (1024);
\r
1269 ret->stack->dir_entry_count = 0;
\r
1270 ret->stack->end_directory = GNUNET_NO;
\r
1272 /* dummy stack entry that tells us we're at the top of the stack */
\r
1273 ret->stack->parent = GNUNET_malloc (sizeof (struct ProcessMetadataStackItem));
\r
1274 ret->stack->parent->ctx = ret;
\r
1279 GNUNET_SCHEDULER_add_continuation (&trim_share_tree_task, ret->stack,
\r
1280 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
\r