2 This file is part of GNUnet.
3 (C) 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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.
40 * This is a doubly-linked list
42 struct ScanTreeNode *next;
45 * This is a doubly-linked list
47 struct ScanTreeNode *prev;
50 * Parent of this node, NULL for top-level entries.
52 struct ScanTreeNode *parent;
55 * This is a doubly-linked tree
56 * NULL for files and empty directories
58 struct ScanTreeNode *children_head;
61 * This is a doubly-linked tree
62 * NULL for files and empty directories
64 struct ScanTreeNode *children_tail;
67 * Name of the file/directory
72 * Size of the file (if it is a file), in bytes.
73 * At the moment it is set to 0 for directories.
78 * GNUNET_YES if this is a directory
86 * List of libextractor plugins to use for extracting.
88 static struct EXTRACTOR_PluginList *plugins;
92 * Add meta data that libextractor finds to our meta data
95 * @param cls closure, our meta data container
96 * @param plugin_name name of the plugin that produced this value;
97 * special values can be used (i.e. '<zlib>' for zlib being
98 * used in the main libextractor library and yielding
100 * @param type libextractor-type describing the meta data
101 * @param format basic format information about data
102 * @param data_mime_type mime-type of data (not of the original file);
103 * can be NULL (if mime-type is not known)
104 * @param data actual meta-data found
105 * @param data_len number of bytes in data
106 * @return always 0 to continue extracting
109 add_to_md (void *cls, const char *plugin_name, enum EXTRACTOR_MetaType type,
110 enum EXTRACTOR_MetaFormat format, const char *data_mime_type,
111 const char *data, size_t data_len)
113 struct GNUNET_CONTAINER_MetaData *md = cls;
115 (void) GNUNET_CONTAINER_meta_data_insert (md, plugin_name, type, format,
116 data_mime_type, data, data_len);
122 * Free memory of the 'tree' structure
124 * @param tree tree to free
127 free_tree (struct ScanTreeNode *tree)
129 struct ScanTreeNode *pos;
131 while (NULL != (pos = tree->children_head))
133 if (NULL != tree->parent)
134 GNUNET_CONTAINER_DLL_remove (tree->parent->children_head,
135 tree->parent->children_tail,
137 GNUNET_free (tree->filename);
143 * Write 'size' bytes from 'buf' into 'out'.
145 * @param buf buffer with data to write
146 * @param size number of bytes to write
147 * @return GNUNET_OK on success, GNUNET_SYSERR on error
150 write_all (const void *buf,
153 const char *cbuf = buf;
165 } while ( (wr > 0) && (total < size) );
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Failed to write to stdout: %s\n",
170 return (total == size) ? GNUNET_OK : GNUNET_SYSERR;
175 * Write message to the master process.
177 * @param message_type message type to use
178 * @param data data to append, NULL for none
179 * @param data_length number of bytes in data
180 * @return GNUNET_SYSERR to stop scanning (the pipe was broken somehow)
183 write_message (uint16_t message_type,
187 struct GNUNET_MessageHeader hdr;
190 fprintf (stderr, "Helper sends %u-byte message of type %u\n",
191 (unsigned int) (sizeof (struct GNUNET_MessageHeader) + data_length),
192 (unsigned int) message_type);
194 hdr.type = htons (message_type);
195 hdr.size = htons (sizeof (struct GNUNET_MessageHeader) + data_length);
202 return GNUNET_SYSERR;
208 * Function called to (recursively) add all of the files in the
209 * directory to the tree. Called by the directory scanner to initiate
210 * the scan. Does NOT yet add any metadata.
212 * @param filename file or directory to scan
213 * @param dst where to store the resulting share tree item
214 * @return GNUNET_OK on success, GNUNET_SYSERR on error
217 preprocess_file (const char *filename,
218 struct ScanTreeNode **dst);
222 * Closure for the 'scan_callback'
224 struct RecursionContext
227 * Parent to add the files to.
229 struct ScanTreeNode *parent;
232 * Flag to set to GNUNET_YES on serious errors.
239 * Function called by the directory iterator to (recursively) add all
240 * of the files in the directory to the tree. Called by the directory
241 * scanner to initiate the scan. Does NOT yet add any metadata.
243 * @param cls the 'struct RecursionContext'
244 * @param filename file or directory to scan
245 * @return GNUNET_OK on success, GNUNET_SYSERR on error
248 scan_callback (void *cls,
249 const char *filename)
251 struct RecursionContext *rc = cls;
252 struct ScanTreeNode *chld;
255 preprocess_file (filename,
258 rc->stop = GNUNET_YES;
259 return GNUNET_SYSERR;
261 chld->parent = rc->parent;
262 GNUNET_CONTAINER_DLL_insert (rc->parent->children_head,
263 rc->parent->children_tail,
270 * Function called to (recursively) add all of the files in the
271 * directory to the tree. Called by the directory scanner to initiate
272 * the scan. Does NOT yet add any metadata.
274 * @param filename file or directory to scan
275 * @param dst where to store the resulting share tree item
276 * @return GNUNET_OK on success, GNUNET_SYSERR on error
279 preprocess_file (const char *filename,
280 struct ScanTreeNode **dst)
282 struct ScanTreeNode *item;
286 if ((0 != STAT (filename, &sbuf)) ||
287 ((!S_ISDIR (sbuf.st_mode)) && (GNUNET_OK != GNUNET_DISK_file_size (
288 filename, &fsize, GNUNET_NO, GNUNET_YES))))
290 /* If the file doesn't exist (or is not stat-able for any other reason)
291 skip it (but report it), but do continue. */
293 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_SKIP_FILE,
294 filename, strlen (filename) + 1))
295 return GNUNET_SYSERR;
299 /* Report the progress */
301 write_message (S_ISDIR (sbuf.st_mode)
302 ? GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY
303 : GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_FILE,
304 filename, strlen (filename) + 1))
305 return GNUNET_SYSERR;
306 item = GNUNET_malloc (sizeof (struct ScanTreeNode));
307 item->filename = GNUNET_strdup (filename);
308 item->is_directory = (S_ISDIR (sbuf.st_mode)) ? GNUNET_YES : GNUNET_NO;
309 item->file_size = fsize;
310 if (item->is_directory == GNUNET_YES)
312 struct RecursionContext rc;
316 GNUNET_DISK_directory_scan (filename,
319 if ( (rc.stop == GNUNET_YES) ||
321 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY,
325 return GNUNET_SYSERR;
334 * Extract metadata from files.
336 * @param item entry we are processing
337 * @return GNUNET_OK on success, GNUNET_SYSERR on fatal errors
340 extract_files (struct ScanTreeNode *item)
342 struct GNUNET_CONTAINER_MetaData *meta;
346 if (GNUNET_YES == item->is_directory)
348 /* for directories, we simply only descent, no extraction, no
349 progress reporting */
350 struct ScanTreeNode *pos;
352 for (pos = item->children_head; NULL != pos; pos = pos->next)
355 return GNUNET_SYSERR;
359 /* this is the expensive operation, *afterwards* we'll check for aborts */
360 meta = GNUNET_CONTAINER_meta_data_create ();
362 EXTRACTOR_extract (plugins, item->filename, NULL, 0, &add_to_md, meta);
363 slen = strlen (item->filename) + 1;
364 size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
368 GNUNET_CONTAINER_meta_data_destroy (meta);
370 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA,
371 item->filename, slen))
372 return GNUNET_SYSERR;
376 char buf[size + slen];
377 char *dst = &buf[slen];
379 memcpy (buf, item->filename, slen);
380 size = GNUNET_CONTAINER_meta_data_serialize (meta,
382 GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
388 GNUNET_CONTAINER_meta_data_destroy (meta);
390 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA,
393 return GNUNET_SYSERR;
400 * Main function of the helper process to extract meta data.
402 * @param argc should be 3
403 * @param argv [0] our binary name
404 * [1] name of the file or directory to process
405 * [2] "-" to disable extraction, NULL for defaults,
406 * otherwise custom plugins to load from LE
407 * @return 0 on success
412 const char *filename_expanded;
414 struct ScanTreeNode *root;
417 /* We're using stdout to communicate binary data back to the parent; use
420 _setmode (1, _O_BINARY);
423 /* parse command line */
424 if ( (argc != 3) && (argc != 2) )
428 "gnunet-helper-fs-publish needs exactly one or two arguments\n");
431 filename_expanded = argv[1];
434 (0 != strcmp (ex, "-")) )
436 plugins = EXTRACTOR_plugin_add_defaults (EXTRACTOR_OPTION_DEFAULT_POLICY);
438 plugins = EXTRACTOR_plugin_add_config (plugins, ex,
439 EXTRACTOR_OPTION_DEFAULT_POLICY);
442 /* scan tree to find out how much work there is to be done */
443 if (GNUNET_OK != preprocess_file (filename_expanded,
446 (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR, NULL, 0);
449 /* signal that we're done counting files, so that a percentage of
450 progress can now be calculated */
452 write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE, NULL, 0))
455 extract_files (root))
457 (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR, NULL, 0);
462 /* enable "clean" shutdown by telling parent that we are done */
463 (void) write_message (GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED, NULL, 0);
465 EXTRACTOR_plugin_remove_all (plugins);
470 /* end of gnunet-helper-fs-publish.c */