2 This file is part of GNUnet.
3 Copyright (C) 2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file src/fs/gnunet-helper-fs-publish.c
21 * @brief Tool to help extract meta data asynchronously
22 * @author Christian Grothoff
24 * This program will scan a directory for files with meta data
25 * and report the results to stdout.
28 #include "gnunet_fs_service.h"
32 * A node of a directory tree.
38 * This is a doubly-linked list
40 struct ScanTreeNode *next;
43 * This is a doubly-linked list
45 struct ScanTreeNode *prev;
48 * Parent of this node, NULL for top-level entries.
50 struct ScanTreeNode *parent;
53 * This is a doubly-linked tree
54 * NULL for files and empty directories
56 struct ScanTreeNode *children_head;
59 * This is a doubly-linked tree
60 * NULL for files and empty directories
62 struct ScanTreeNode *children_tail;
65 * Name of the file/directory
70 * Size of the file (if it is a file), in bytes.
71 * At the moment it is set to 0 for directories.
76 * #GNUNET_YES if this is a directory
85 * List of libextractor plugins to use for extracting.
87 static struct EXTRACTOR_PluginList *plugins;
91 * File descriptor we use for IPC with the parent.
93 static int output_stream;
98 * Add meta data that libextractor finds to our meta data
101 * @param cls closure, our meta data container
102 * @param plugin_name name of the plugin that produced this value;
103 * special values can be used (i.e. '<zlib>' for zlib being
104 * used in the main libextractor library and yielding
106 * @param type libextractor-type describing the meta data
107 * @param format basic format information about data
108 * @param data_mime_type mime-type of data (not of the original file);
109 * can be NULL (if mime-type is not known)
110 * @param data actual meta-data found
111 * @param data_len number of bytes in @a data
112 * @return always 0 to continue extracting
115 add_to_md (void *cls,
116 const char *plugin_name,
117 enum EXTRACTOR_MetaType type,
118 enum EXTRACTOR_MetaFormat format,
119 const char *data_mime_type,
123 struct GNUNET_CONTAINER_MetaData *md = cls;
125 if ( ((EXTRACTOR_METAFORMAT_UTF8 == format) ||
126 (EXTRACTOR_METAFORMAT_C_STRING == format)) &&
127 ('\0' != data[data_len - 1]) )
129 char zdata[data_len + 1];
130 GNUNET_memcpy (zdata, data, data_len);
131 zdata[data_len] = '\0';
132 (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format,
133 data_mime_type, zdata, data_len + 1);
137 (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format,
138 data_mime_type, data, data_len);
146 * Free memory of the @a tree structure
148 * @param tree tree to free
151 free_tree (struct ScanTreeNode *tree)
153 struct ScanTreeNode *pos;
155 while (NULL != (pos = tree->children_head))
157 if (NULL != tree->parent)
158 GNUNET_CONTAINER_DLL_remove (tree->parent->children_head,
159 tree->parent->children_tail,
161 GNUNET_free (tree->filename);
167 * Write @a size bytes from @a buf into the #output_stream.
169 * @param buf buffer with data to write
170 * @param size number of bytes to write
171 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
174 write_all (const void *buf,
177 const char *cbuf = buf;
184 wr = write (output_stream,
189 } while ( (wr > 0) && (total < size) );
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
192 "Failed to write to stdout: %s\n",
194 return (total == size) ? GNUNET_OK : GNUNET_SYSERR;
199 * Write message to the master process.
201 * @param message_type message type to use
202 * @param data data to append, NULL for none
203 * @param data_length number of bytes in @a data
204 * @return #GNUNET_SYSERR to stop scanning (the pipe was broken somehow)
207 write_message (uint16_t message_type,
211 struct GNUNET_MessageHeader hdr;
215 "Helper sends %u-byte message of type %u\n",
216 (unsigned int) (sizeof (struct GNUNET_MessageHeader) + data_length),
217 (unsigned int) message_type);
219 hdr.type = htons (message_type);
220 hdr.size = htons (sizeof (struct GNUNET_MessageHeader) + data_length);
227 return GNUNET_SYSERR;
233 * Function called to (recursively) add all of the files in the
234 * directory to the tree. Called by the directory scanner to initiate
235 * the scan. Does NOT yet add any metadata.
237 * @param filename file or directory to scan
238 * @param dst where to store the resulting share tree item;
239 * NULL is stored in @a dst upon recoverable errors (#GNUNET_OK is returned)
240 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
243 preprocess_file (const char *filename,
244 struct ScanTreeNode **dst);
248 * Closure for the 'scan_callback'
250 struct RecursionContext
253 * Parent to add the files to.
255 struct ScanTreeNode *parent;
258 * Flag to set to GNUNET_YES on serious errors.
265 * Function called by the directory iterator to (recursively) add all
266 * of the files in the directory to the tree. Called by the directory
267 * scanner to initiate the scan. Does NOT yet add any metadata.
269 * @param cls the `struct RecursionContext`
270 * @param filename file or directory to scan
271 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
274 scan_callback (void *cls,
275 const char *filename)
277 struct RecursionContext *rc = cls;
278 struct ScanTreeNode *chld;
281 preprocess_file (filename,
284 rc->stop = GNUNET_YES;
285 return GNUNET_SYSERR;
289 chld->parent = rc->parent;
290 GNUNET_CONTAINER_DLL_insert (rc->parent->children_head,
291 rc->parent->children_tail,
298 * Function called to (recursively) add all of the files in the
299 * directory to the tree. Called by the directory scanner to initiate
300 * the scan. Does NOT yet add any metadata.
302 * @param filename file or directory to scan
303 * @param dst where to store the resulting share tree item;
304 * NULL is stored in @a dst upon recoverable errors (#GNUNET_OK is returned)
305 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
308 preprocess_file (const char *filename,
309 struct ScanTreeNode **dst)
311 struct ScanTreeNode *item;
315 if ((0 != STAT (filename, &sbuf)) ||
316 ((!S_ISDIR (sbuf.st_mode)) && (GNUNET_OK != GNUNET_DISK_file_size (
317 filename, &fsize, GNUNET_NO, GNUNET_YES))))
319 /* If the file doesn't exist (or is not stat-able for any other reason)
320 skip it (but report it), but do continue. */
322 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_SKIP_FILE,
323 filename, strlen (filename) + 1))
324 return GNUNET_SYSERR;
325 /* recoverable error, store 'NULL' in *dst */
330 /* Report the progress */
332 write_message (S_ISDIR (sbuf.st_mode)
333 ? GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY
334 : GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_FILE,
335 filename, strlen (filename) + 1))
336 return GNUNET_SYSERR;
337 item = GNUNET_new (struct ScanTreeNode);
338 item->filename = GNUNET_strdup (filename);
339 item->is_directory = (S_ISDIR (sbuf.st_mode)) ? GNUNET_YES : GNUNET_NO;
340 item->file_size = fsize;
341 if (GNUNET_YES == item->is_directory)
343 struct RecursionContext rc;
347 GNUNET_DISK_directory_scan (filename,
350 if ( (GNUNET_YES == rc.stop) ||
352 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY,
356 return GNUNET_SYSERR;
365 * Extract metadata from files.
367 * @param item entry we are processing
368 * @return #GNUNET_OK on success, #GNUNET_SYSERR on fatal errors
371 extract_files (struct ScanTreeNode *item)
373 struct GNUNET_CONTAINER_MetaData *meta;
377 if (GNUNET_YES == item->is_directory)
379 /* for directories, we simply only descent, no extraction, no
380 progress reporting */
381 struct ScanTreeNode *pos;
383 for (pos = item->children_head; NULL != pos; pos = pos->next)
386 return GNUNET_SYSERR;
390 /* this is the expensive operation, *afterwards* we'll check for aborts */
391 meta = GNUNET_CONTAINER_meta_data_create ();
392 #if HAVE_LIBEXTRACTOR
393 EXTRACTOR_extract (plugins,
399 slen = strlen (item->filename) + 1;
400 size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
404 GNUNET_CONTAINER_meta_data_destroy (meta);
406 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA,
407 item->filename, slen))
408 return GNUNET_SYSERR;
411 else if (size > (UINT16_MAX - sizeof (struct GNUNET_MessageHeader) - slen))
413 /* We can't transfer more than 64k bytes in one message. */
414 size = UINT16_MAX - sizeof (struct GNUNET_MessageHeader) - slen;
417 char buf[size + slen];
418 char *dst = &buf[slen];
420 GNUNET_memcpy (buf, item->filename, slen);
421 size = GNUNET_CONTAINER_meta_data_serialize (meta,
423 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
429 GNUNET_CONTAINER_meta_data_destroy (meta);
431 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA,
434 return GNUNET_SYSERR;
442 * Install a signal handler to ignore SIGPIPE.
447 struct sigaction oldsig;
448 struct sigaction sig;
450 memset (&sig, 0, sizeof (struct sigaction));
451 sig.sa_handler = SIG_IGN;
452 sigemptyset (&sig.sa_mask);
454 sig.sa_flags = SA_INTERRUPT; /* SunOS */
456 sig.sa_flags = SA_RESTART;
458 if (0 != sigaction (SIGPIPE, &sig, &oldsig))
460 "Failed to install SIGPIPE handler: %s\n", strerror (errno));
465 * Turn the given file descriptor in to '/dev/null'.
467 * @param fd fd to bind to /dev/null
468 * @param flags flags to use (O_RDONLY or O_WRONLY)
471 make_dev_zero (int fd,
476 GNUNET_assert (0 == close (fd));
477 z = open ("/dev/null", flags);
478 GNUNET_assert (-1 != z);
481 GNUNET_break (fd == dup2 (z, fd));
482 GNUNET_assert (0 == close (z));
489 * Main function of the helper process to extract meta data.
491 * @param argc should be 3
492 * @param argv [0] our binary name
493 * [1] name of the file or directory to process
494 * [2] "-" to disable extraction, NULL for defaults,
495 * otherwise custom plugins to load from LE
496 * @return 0 on success
502 const char *filename_expanded;
504 struct ScanTreeNode *root;
507 /* We're using stdout to communicate binary data back to the parent; use
510 _setmode (1, _O_BINARY);
511 /* Get utf-8-encoded arguments */
512 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
514 output_stream = 1; /* stdout */
517 /* move stdout to some other FD for IPC, bind
518 stdout/stderr to /dev/null */
519 output_stream = dup (1);
520 make_dev_zero (1, O_WRONLY);
521 make_dev_zero (2, O_WRONLY);
524 /* parse command line */
525 if ( (3 != argc) && (2 != argc) )
529 "gnunet-helper-fs-publish needs exactly one or two arguments\n");
531 GNUNET_free ((void*) argv);
535 filename_expanded = argv[1];
538 (0 != strcmp (ex, "-")) )
540 #if HAVE_LIBEXTRACTOR
541 plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
543 plugins = EXTRACTOR_plugin_add_config (plugins, ex,
544 EXTRACTOR_OPTION_DEFAULT_POLICY);
548 /* scan tree to find out how much work there is to be done */
549 if (GNUNET_OK != preprocess_file (filename_expanded,
552 (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR, NULL, 0);
553 #if HAVE_LIBEXTRACTOR
554 EXTRACTOR_plugin_remove_all (plugins);
557 GNUNET_free ((void*) argv);
561 /* signal that we're done counting files, so that a percentage of
562 progress can now be calculated */
564 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE, NULL, 0))
566 #if HAVE_LIBEXTRACTOR
567 EXTRACTOR_plugin_remove_all (plugins);
570 GNUNET_free ((void*) argv);
577 extract_files (root))
579 (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR, NULL, 0);
581 #if HAVE_LIBEXTRACTOR
582 EXTRACTOR_plugin_remove_all (plugins);
585 GNUNET_free ((void*) argv);
591 /* enable "clean" shutdown by telling parent that we are done */
592 (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED, NULL, 0);
593 #if HAVE_LIBEXTRACTOR
594 EXTRACTOR_plugin_remove_all (plugins);
597 GNUNET_free ((void*) argv);
602 /* end of gnunet-helper-fs-publish.c */