2 This file is part of GNUnet
3 (C) 2005-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 2, 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 fs/fs_dirmetascan.c
23 * @brief code to asynchronously build a 'struct GNUNET_FS_ShareTreeItem'
24 * from an on-disk directory for publishing; use the 'gnunet-helper-fs-publish'.
26 * @author Christian Grothoff
29 #include "gnunet_fs_service.h"
30 #include "gnunet_scheduler_lib.h"
35 * An opaque structure a pointer to which is returned to the
36 * caller to be used to control the scanner.
38 struct GNUNET_FS_DirScanner
44 struct GNUNET_HELPER_Handle *helper;
47 * Expanded filename (as given by the scan initiator).
48 * The scanner thread stores a copy here, and frees it when it finishes.
50 char *filename_expanded;
53 * Second argument to helper process.
58 * The function that will be called every time there's a progress
61 GNUNET_FS_DirScannerProgressCallback progress_callback;
64 * A closure for progress_callback.
66 void *progress_callback_cls;
69 * After the scan is finished, it will contain a pointer to the
70 * top-level directory entry in the directory tree built by the
73 struct GNUNET_FS_ShareTreeItem *toplevel;
76 * Current position during processing.
78 struct GNUNET_FS_ShareTreeItem *pos;
81 * Task scheduled when we are done.
83 GNUNET_SCHEDULER_TaskIdentifier stop_task;
86 * Arguments for helper.
95 * Abort the scan. Must not be called from within the progress_callback
98 * @param ds directory scanner structure
101 GNUNET_FS_directory_scan_abort (struct GNUNET_FS_DirScanner *ds)
103 /* terminate helper */
104 if (NULL != ds->helper)
105 GNUNET_HELPER_stop (ds->helper);
108 if (NULL != ds->toplevel)
109 GNUNET_FS_share_tree_free (ds->toplevel);
110 if (GNUNET_SCHEDULER_NO_TASK != ds->stop_task)
111 GNUNET_SCHEDULER_cancel (ds->stop_task);
112 GNUNET_free_non_null (ds->ex_arg);
113 GNUNET_free (ds->filename_expanded);
119 * Obtain the result of the scan after the scan has signalled
120 * completion. Must not be called prior to completion. The 'ds' is
121 * freed as part of this call.
123 * @param ds directory scanner structure
124 * @return the results of the scan (a directory tree)
126 struct GNUNET_FS_ShareTreeItem *
127 GNUNET_FS_directory_scan_get_result (struct GNUNET_FS_DirScanner *ds)
129 struct GNUNET_FS_ShareTreeItem *result;
131 /* check that we're actually done */
132 GNUNET_assert (NULL == ds->helper);
133 /* preserve result */
134 result = ds->toplevel;
136 GNUNET_FS_directory_scan_abort (ds);
142 * Move in the directory from the given position to the next file
145 * @param pos current position
146 * @return next file, NULL for none
148 static struct GNUNET_FS_ShareTreeItem *
149 advance (struct GNUNET_FS_ShareTreeItem *pos)
153 GNUNET_assert (NULL != pos);
154 moved = 0; /* must not terminate, even on file, otherwise "normal" */
155 while ( (pos->is_directory == GNUNET_YES) ||
158 if ( (moved != -1) &&
159 (NULL != pos->children_head) )
161 pos = pos->children_head;
162 moved = 1; /* can terminate if file */
165 if (NULL != pos->next)
168 moved = 1; /* can terminate if file */
171 if (NULL != pos->parent)
174 moved = -1; /* force move to 'next' or 'parent' */
177 /* no more options, end of traversal */
185 * Add another child node to the tree.
187 * @param parent parent of the child, NULL for top level
188 * @param filename name of the file or directory
189 * @param is_directory GNUNET_YES for directories
190 * @return new entry that was just created
192 static struct GNUNET_FS_ShareTreeItem *
193 expand_tree (struct GNUNET_FS_ShareTreeItem *parent,
194 const char *filename,
197 struct GNUNET_FS_ShareTreeItem *chld;
200 chld = GNUNET_malloc (sizeof (struct GNUNET_FS_ShareTreeItem));
201 chld->parent = parent;
202 chld->filename = GNUNET_strdup (filename);
203 GNUNET_asprintf (&chld->short_filename,
205 GNUNET_STRINGS_get_short_name (filename),
206 is_directory == GNUNET_YES ? "/" : "");
207 /* make sure we do not end with '//' */
208 slen = strlen (chld->short_filename);
210 (chld->short_filename[slen-1] == '/') &&
211 (chld->short_filename[slen-2] == '/') )
212 chld->short_filename[slen-1] = '\0';
213 chld->is_directory = is_directory;
215 GNUNET_CONTAINER_DLL_insert (parent->children_head,
216 parent->children_tail,
223 * Task run last to shut everything down.
225 * @param cls the 'struct GNUNET_FS_DirScanner'
229 finish_scan (void *cls,
230 const struct GNUNET_SCHEDULER_TaskContext *tc)
232 struct GNUNET_FS_DirScanner *ds = cls;
234 ds->stop_task = GNUNET_SCHEDULER_NO_TASK;
235 if (NULL != ds->helper)
237 GNUNET_HELPER_stop (ds->helper);
240 ds->progress_callback (ds->progress_callback_cls,
242 GNUNET_FS_DIRSCANNER_FINISHED);
247 * Called every time there is data to read from the scanner.
248 * Calls the scanner progress handler.
250 * @param cls the closure (directory scanner object)
251 * @param client always NULL
252 * @param msg message from the helper process
255 process_helper_msgs (void *cls,
257 const struct GNUNET_MessageHeader *msg)
259 struct GNUNET_FS_DirScanner *ds = cls;
260 const char *filename;
264 fprintf (stderr, "DMS parses %u-byte message of type %u\n",
265 (unsigned int) ntohs (msg->size),
266 (unsigned int) ntohs (msg->type));
268 left = ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader);
269 filename = (const char*) &msg[1];
270 switch (ntohs (msg->type))
272 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_FILE:
273 if (filename[left-1] != '\0')
278 ds->progress_callback (ds->progress_callback_cls,
280 GNUNET_FS_DIRSCANNER_FILE_START);
281 if (NULL == ds->toplevel)
282 ds->toplevel = expand_tree (ds->pos,
283 filename, GNUNET_NO);
285 (void) expand_tree (ds->pos,
286 filename, GNUNET_NO);
288 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY:
289 if (filename[left-1] != '\0')
294 if (0 == strcmp ("..", filename))
301 ds->pos = ds->pos->parent;
304 ds->progress_callback (ds->progress_callback_cls,
305 filename, GNUNET_YES,
306 GNUNET_FS_DIRSCANNER_FILE_START);
307 ds->pos = expand_tree (ds->pos,
308 filename, GNUNET_YES);
309 if (NULL == ds->toplevel)
310 ds->toplevel = ds->pos;
312 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR:
314 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_SKIP_FILE:
315 if ('\0' != filename[left-1])
317 ds->progress_callback (ds->progress_callback_cls,
318 filename, GNUNET_SYSERR,
319 GNUNET_FS_DIRSCANNER_FILE_IGNORED);
321 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE:
327 if (NULL == ds->toplevel)
332 ds->progress_callback (ds->progress_callback_cls,
334 GNUNET_FS_DIRSCANNER_ALL_COUNTED);
335 ds->pos = ds->toplevel;
336 if (GNUNET_YES == ds->pos->is_directory)
337 ds->pos = advance (ds->pos);
339 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA:
349 end = memchr (filename, 0, left);
356 nlen = end - filename;
358 if (0 != strcmp (filename,
364 ds->progress_callback (ds->progress_callback_cls,
365 filename, GNUNET_YES,
366 GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED);
369 ds->pos->meta = GNUNET_CONTAINER_meta_data_deserialize (end, left);
370 if (NULL == ds->pos->meta)
375 /* having full filenames is too dangerous; always make sure we clean them up */
376 GNUNET_CONTAINER_meta_data_delete (ds->pos->meta,
377 EXTRACTOR_METATYPE_FILENAME,
379 /* instead, put in our 'safer' original filename */
380 GNUNET_CONTAINER_meta_data_insert (ds->pos->meta, "<libgnunetfs>",
381 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
382 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
383 ds->pos->short_filename,
384 strlen (ds->pos->short_filename) + 1);
386 ds->pos->ksk_uri = GNUNET_FS_uri_ksk_create_from_meta_data (ds->pos->meta);
387 ds->pos = advance (ds->pos);
390 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED:
401 if (NULL == ds->toplevel)
406 ds->stop_task = GNUNET_SCHEDULER_add_now (&finish_scan,
413 ds->progress_callback (ds->progress_callback_cls,
415 GNUNET_FS_DIRSCANNER_INTERNAL_ERROR);
421 * Function called if our helper process died.
423 * @param cls the 'struct GNUNET_FS_DirScanner' callback.
426 helper_died_cb (void *cls)
428 struct GNUNET_FS_DirScanner *ds = cls;
431 if (GNUNET_SCHEDULER_NO_TASK != ds->stop_task)
432 return; /* normal death, was finished */
433 ds->progress_callback (ds->progress_callback_cls,
435 GNUNET_FS_DIRSCANNER_INTERNAL_ERROR);
440 * Start a directory scanner thread.
442 * @param filename name of the directory to scan
443 * @param disable_extractor GNUNET_YES to not to run libextractor on files (only build a tree)
444 * @param ex if not NULL, must be a list of extra plugins for extractor
445 * @param cb the callback to call when there are scanning progress messages
446 * @param cb_cls closure for 'cb'
447 * @return directory scanner object to be used for controlling the scanner
449 struct GNUNET_FS_DirScanner *
450 GNUNET_FS_directory_scan_start (const char *filename,
451 int disable_extractor, const char *ex,
452 GNUNET_FS_DirScannerProgressCallback cb,
456 char *filename_expanded;
457 struct GNUNET_FS_DirScanner *ds;
459 if (0 != STAT (filename, &sbuf))
461 filename_expanded = GNUNET_STRINGS_filename_expand (filename);
462 if (NULL == filename_expanded)
464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465 "Starting to scan directory `%s'\n",
467 ds = GNUNET_malloc (sizeof (struct GNUNET_FS_DirScanner));
468 ds->progress_callback = cb;
469 ds->progress_callback_cls = cb_cls;
470 ds->filename_expanded = filename_expanded;
471 if (disable_extractor)
472 ds->ex_arg = GNUNET_strdup ("-");
474 ds->ex_arg = (NULL != ex) ? GNUNET_strdup (ex) : NULL;
475 ds->args[0] = "gnunet-helper-fs-publish";
476 ds->args[1] = ds->filename_expanded;
477 ds->args[2] = ds->ex_arg;
479 ds->helper = GNUNET_HELPER_start (GNUNET_NO,
480 "gnunet-helper-fs-publish",
482 &process_helper_msgs,
483 &helper_died_cb, ds);
484 if (NULL == ds->helper)
486 GNUNET_free (filename_expanded);
494 /* end of fs_dirmetascan.c */