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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file src/fs/gnunet-helper-fs-publish.c
23 * @brief Tool to help extract meta data asynchronously
24 * @author Christian Grothoff
26 * This program will scan a directory for files with meta data
27 * and report the results to stdout.
30 #include "gnunet_fs_service.h"
34 * A node of a directory tree.
39 * This is a doubly-linked list
41 struct ScanTreeNode *next;
44 * This is a doubly-linked list
46 struct ScanTreeNode *prev;
49 * Parent of this node, NULL for top-level entries.
51 struct ScanTreeNode *parent;
54 * This is a doubly-linked tree
55 * NULL for files and empty directories
57 struct ScanTreeNode *children_head;
60 * This is a doubly-linked tree
61 * NULL for files and empty directories
63 struct ScanTreeNode *children_tail;
66 * Name of the file/directory
71 * Size of the file (if it is a file), in bytes.
72 * At the moment it is set to 0 for directories.
77 * #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,
142 (void) GNUNET_CONTAINER_meta_data_insert (md,
158 * Free memory of the @a tree structure
160 * @param tree tree to free
163 free_tree (struct ScanTreeNode *tree)
165 struct ScanTreeNode *pos;
167 while (NULL != (pos = tree->children_head))
169 if (NULL != tree->parent)
170 GNUNET_CONTAINER_DLL_remove (tree->parent->children_head,
171 tree->parent->children_tail,
173 GNUNET_free (tree->filename);
179 * Write @a size bytes from @a buf into the #output_stream.
181 * @param buf buffer with data to write
182 * @param size number of bytes to write
183 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
186 write_all (const void *buf, size_t size)
188 const char *cbuf = buf;
195 wr = write (output_stream, &cbuf[total], size - total);
199 while ((wr > 0) && (total < size));
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "Failed to write to stdout: %s\n",
204 return (total == size) ? GNUNET_OK : GNUNET_SYSERR;
209 * Write message to the master process.
211 * @param message_type message type to use
212 * @param data data to append, NULL for none
213 * @param data_length number of bytes in @a data
214 * @return #GNUNET_SYSERR to stop scanning (the pipe was broken somehow)
217 write_message (uint16_t message_type, const char *data, size_t data_length)
219 struct GNUNET_MessageHeader hdr;
223 "Helper sends %u-byte message of type %u\n",
224 (unsigned int) (sizeof(struct GNUNET_MessageHeader) + data_length),
225 (unsigned int) message_type);
227 hdr.type = htons (message_type);
228 hdr.size = htons (sizeof(struct GNUNET_MessageHeader) + data_length);
229 if ((GNUNET_OK != write_all (&hdr, sizeof(hdr))) ||
230 (GNUNET_OK != write_all (data, data_length)))
231 return GNUNET_SYSERR;
237 * Function called to (recursively) add all of the files in the
238 * directory to the tree. Called by the directory scanner to initiate
239 * the scan. Does NOT yet add any metadata.
241 * @param filename file or directory to scan
242 * @param dst where to store the resulting share tree item;
243 * NULL is stored in @a dst upon recoverable errors (#GNUNET_OK is returned)
244 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
247 preprocess_file (const char *filename, struct ScanTreeNode **dst);
251 * Closure for the 'scan_callback'
253 struct RecursionContext
256 * Parent to add the files to.
258 struct ScanTreeNode *parent;
261 * Flag to set to GNUNET_YES on serious errors.
268 * Function called by the directory iterator to (recursively) add all
269 * of the files in the directory to the tree. Called by the directory
270 * scanner to initiate the scan. Does NOT yet add any metadata.
272 * @param cls the `struct RecursionContext`
273 * @param filename file or directory to scan
274 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
277 scan_callback (void *cls, const char *filename)
279 struct RecursionContext *rc = cls;
280 struct ScanTreeNode *chld;
282 if (GNUNET_OK != preprocess_file (filename, &chld))
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, struct ScanTreeNode **dst)
310 struct ScanTreeNode *item;
314 if ((0 != stat (filename, &sbuf)) ||
315 ((! S_ISDIR (sbuf.st_mode)) &&
317 GNUNET_DISK_file_size (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,
324 strlen (filename) + 1))
325 return GNUNET_SYSERR;
326 /* recoverable error, store 'NULL' in *dst */
331 /* Report the progress */
334 write_message (S_ISDIR (sbuf.st_mode)
335 ? GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY
336 : GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_FILE,
338 strlen (filename) + 1))
339 return GNUNET_SYSERR;
340 item = GNUNET_new (struct ScanTreeNode);
341 item->filename = GNUNET_strdup (filename);
342 item->is_directory = (S_ISDIR (sbuf.st_mode)) ? GNUNET_YES : GNUNET_NO;
343 item->file_size = fsize;
344 if (GNUNET_YES == item->is_directory)
346 struct RecursionContext rc;
350 GNUNET_DISK_directory_scan (filename, &scan_callback, &rc);
352 (GNUNET_YES == rc.stop) ||
354 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY,
359 return GNUNET_SYSERR;
368 * Extract metadata from files.
370 * @param item entry we are processing
371 * @return #GNUNET_OK on success, #GNUNET_SYSERR on fatal errors
374 extract_files (struct ScanTreeNode *item)
376 struct GNUNET_CONTAINER_MetaData *meta;
380 if (GNUNET_YES == item->is_directory)
382 /* for directories, we simply only descent, no extraction, no
383 progress reporting */
384 struct ScanTreeNode *pos;
386 for (pos = item->children_head; NULL != pos; pos = pos->next)
387 if (GNUNET_OK != extract_files (pos))
388 return GNUNET_SYSERR;
392 /* this is the expensive operation, *afterwards* we'll check for aborts */
393 meta = GNUNET_CONTAINER_meta_data_create ();
394 #if HAVE_LIBEXTRACTOR
395 EXTRACTOR_extract (plugins, item->filename, NULL, 0, &add_to_md, meta);
397 slen = strlen (item->filename) + 1;
398 size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
402 GNUNET_CONTAINER_meta_data_destroy (meta);
404 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA,
407 return GNUNET_SYSERR;
410 else if (size > (UINT16_MAX - sizeof(struct GNUNET_MessageHeader) - slen))
412 /* We can't transfer more than 64k bytes in one message. */
413 size = UINT16_MAX - sizeof(struct GNUNET_MessageHeader) - slen;
416 char buf[size + slen];
417 char *dst = &buf[slen];
419 GNUNET_memcpy (buf, item->filename, slen);
420 size = GNUNET_CONTAINER_meta_data_serialize (
424 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
430 GNUNET_CONTAINER_meta_data_destroy (meta);
432 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA,
435 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",
466 * Turn the given file descriptor in to '/dev/null'.
468 * @param fd fd to bind to /dev/null
469 * @param flags flags to use (O_RDONLY or O_WRONLY)
472 make_dev_zero (int fd, int flags)
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));
487 * Main function of the helper process to extract meta data.
489 * @param argc should be 3
490 * @param argv [0] our binary name
491 * [1] name of the file or directory to process
492 * [2] "-" to disable extraction, NULL for defaults,
493 * otherwise custom plugins to load from LE
494 * @return 0 on success
497 main (int argc, char *const *argv)
499 const char *filename_expanded;
501 struct ScanTreeNode *root;
504 /* move stdout to some other FD for IPC, bind
505 stdout/stderr to /dev/null */
506 output_stream = dup (1);
507 make_dev_zero (1, O_WRONLY);
508 make_dev_zero (2, O_WRONLY);
510 /* parse command line */
511 if ((3 != argc) && (2 != argc))
515 "gnunet-helper-fs-publish needs exactly one or two arguments\n");
518 filename_expanded = argv[1];
520 if ((NULL == ex) || (0 != strcmp (ex, "-")))
522 #if HAVE_LIBEXTRACTOR
523 plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
525 plugins = EXTRACTOR_plugin_add_config (plugins,
527 EXTRACTOR_OPTION_DEFAULT_POLICY);
531 /* scan tree to find out how much work there is to be done */
532 if (GNUNET_OK != preprocess_file (filename_expanded, &root))
534 (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR, NULL, 0);
535 #if HAVE_LIBEXTRACTOR
536 EXTRACTOR_plugin_remove_all (plugins);
540 /* signal that we're done counting files, so that a percentage of
541 progress can now be calculated */
543 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE,
547 #if HAVE_LIBEXTRACTOR
548 EXTRACTOR_plugin_remove_all (plugins);
554 if (GNUNET_OK != extract_files (root))
556 (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR,
560 #if HAVE_LIBEXTRACTOR
561 EXTRACTOR_plugin_remove_all (plugins);
567 /* enable "clean" shutdown by telling parent that we are done */
568 (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED,
571 #if HAVE_LIBEXTRACTOR
572 EXTRACTOR_plugin_remove_all (plugins);
578 /* end of gnunet-helper-fs-publish.c */