2 This file is part of GNUnet
3 Copyright (C) 2005-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 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
43 struct GNUNET_HELPER_Handle *helper;
46 * Expanded filename (as given by the scan initiator).
47 * The scanner thread stores a copy here, and frees it when it finishes.
49 char *filename_expanded;
52 * Second argument to helper process.
57 * The function that will be called every time there's a progress
60 GNUNET_FS_DirScannerProgressCallback progress_callback;
63 * A closure for progress_callback.
65 void *progress_callback_cls;
68 * After the scan is finished, it will contain a pointer to the
69 * top-level directory entry in the directory tree built by the
72 struct GNUNET_FS_ShareTreeItem *toplevel;
75 * Current position during processing.
77 struct GNUNET_FS_ShareTreeItem *pos;
80 * Task scheduled when we are done.
82 struct GNUNET_SCHEDULER_Task *stop_task;
85 * Arguments for helper.
92 * Abort the scan. Must not be called from within the progress_callback
95 * @param ds directory scanner structure
98 GNUNET_FS_directory_scan_abort (struct GNUNET_FS_DirScanner *ds)
100 /* terminate helper */
101 if (NULL != ds->helper)
102 GNUNET_HELPER_stop (ds->helper, GNUNET_NO);
105 if (NULL != ds->toplevel)
106 GNUNET_FS_share_tree_free (ds->toplevel);
107 if (NULL != ds->stop_task)
108 GNUNET_SCHEDULER_cancel (ds->stop_task);
109 GNUNET_free_non_null (ds->ex_arg);
110 GNUNET_free (ds->filename_expanded);
116 * Obtain the result of the scan after the scan has signalled
117 * completion. Must not be called prior to completion. The 'ds' is
118 * freed as part of this call.
120 * @param ds directory scanner structure
121 * @return the results of the scan (a directory tree)
123 struct GNUNET_FS_ShareTreeItem *
124 GNUNET_FS_directory_scan_get_result (struct GNUNET_FS_DirScanner *ds)
126 struct GNUNET_FS_ShareTreeItem *result;
128 /* check that we're actually done */
129 GNUNET_assert (NULL == ds->helper);
130 /* preserve result */
131 result = ds->toplevel;
133 GNUNET_FS_directory_scan_abort (ds);
139 * Move in the directory from the given position to the next file
142 * @param pos current position
143 * @return next file, NULL for none
145 static struct GNUNET_FS_ShareTreeItem *
146 advance (struct GNUNET_FS_ShareTreeItem *pos)
150 GNUNET_assert (NULL != pos);
151 moved = 0; /* must not terminate, even on file, otherwise "normal" */
152 while ((pos->is_directory == GNUNET_YES) || (0 == moved))
154 if ((moved != -1) && (NULL != pos->children_head))
156 pos = pos->children_head;
157 moved = 1; /* can terminate if file */
160 if (NULL != pos->next)
163 moved = 1; /* can terminate if file */
166 if (NULL != pos->parent)
169 moved = -1; /* force move to 'next' or 'parent' */
172 /* no more options, end of traversal */
180 * Add another child node to the tree.
182 * @param parent parent of the child, NULL for top level
183 * @param filename name of the file or directory
184 * @param is_directory GNUNET_YES for directories
185 * @return new entry that was just created
187 static struct GNUNET_FS_ShareTreeItem *
188 expand_tree (struct GNUNET_FS_ShareTreeItem *parent,
189 const char *filename,
192 struct GNUNET_FS_ShareTreeItem *chld;
195 chld = GNUNET_new (struct GNUNET_FS_ShareTreeItem);
196 chld->parent = parent;
197 chld->filename = GNUNET_strdup (filename);
198 GNUNET_asprintf (&chld->short_filename,
200 GNUNET_STRINGS_get_short_name (filename),
201 is_directory == GNUNET_YES ? "/" : "");
202 /* make sure we do not end with '//' */
203 slen = strlen (chld->short_filename);
204 if ((slen >= 2) && (chld->short_filename[slen - 1] == '/') &&
205 (chld->short_filename[slen - 2] == '/'))
206 chld->short_filename[slen - 1] = '\0';
207 chld->is_directory = is_directory;
209 GNUNET_CONTAINER_DLL_insert (parent->children_head,
210 parent->children_tail,
217 * Task run last to shut everything down.
219 * @param cls the 'struct GNUNET_FS_DirScanner'
222 finish_scan (void *cls)
224 struct GNUNET_FS_DirScanner *ds = cls;
226 ds->stop_task = NULL;
227 if (NULL != ds->helper)
229 GNUNET_HELPER_stop (ds->helper, GNUNET_NO);
232 ds->progress_callback (ds->progress_callback_cls,
235 GNUNET_FS_DIRSCANNER_FINISHED);
240 * Called every time there is data to read from the scanner.
241 * Calls the scanner progress handler.
243 * @param cls the closure (directory scanner object)
244 * @param msg message from the helper process
245 * @return #GNUNET_OK on success,
246 * #GNUNET_NO to stop further processing (no error)
247 * #GNUNET_SYSERR to stop further processing with error
250 process_helper_msgs (void *cls, const struct GNUNET_MessageHeader *msg)
252 struct GNUNET_FS_DirScanner *ds = cls;
253 const char *filename;
258 "DMS parses %u-byte message of type %u\n",
259 (unsigned int) ntohs (msg->size),
260 (unsigned int) ntohs (msg->type));
262 left = ntohs (msg->size) - sizeof(struct GNUNET_MessageHeader);
263 filename = (const char *) &msg[1];
264 switch (ntohs (msg->type))
266 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_FILE:
267 if (filename[left - 1] != '\0')
272 ds->progress_callback (ds->progress_callback_cls,
275 GNUNET_FS_DIRSCANNER_FILE_START);
276 if (NULL == ds->toplevel)
278 ds->toplevel = expand_tree (ds->pos, filename, GNUNET_NO);
282 GNUNET_assert (NULL != ds->pos);
283 (void) expand_tree (ds->pos, filename, GNUNET_NO);
287 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY:
288 if (filename[left - 1] != '\0')
293 if (0 == strcmp ("..", filename))
300 ds->pos = ds->pos->parent;
303 ds->progress_callback (ds->progress_callback_cls,
306 GNUNET_FS_DIRSCANNER_FILE_START);
307 ds->pos = expand_tree (ds->pos, filename, GNUNET_YES);
308 if (NULL == ds->toplevel)
309 ds->toplevel = ds->pos;
312 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR:
315 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_SKIP_FILE:
316 if ('\0' != filename[left - 1])
318 ds->progress_callback (ds->progress_callback_cls,
321 GNUNET_FS_DIRSCANNER_FILE_IGNORED);
324 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE:
330 if (NULL == ds->toplevel)
332 ds->progress_callback (ds->progress_callback_cls,
335 GNUNET_FS_DIRSCANNER_ALL_COUNTED);
336 ds->pos = ds->toplevel;
337 if (GNUNET_YES == ds->pos->is_directory)
338 ds->pos = advance (ds->pos);
341 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA: {
350 end = memchr (filename, 0, left);
357 nlen = end - filename;
359 if (0 != strcmp (filename, ds->pos->filename))
364 ds->progress_callback (ds->progress_callback_cls,
367 GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED);
370 ds->pos->meta = GNUNET_CONTAINER_meta_data_deserialize (end, left);
371 if (NULL == ds->pos->meta)
376 /* having full filenames is too dangerous; always make sure we clean them up */
377 GNUNET_CONTAINER_meta_data_delete (ds->pos->meta,
378 EXTRACTOR_METATYPE_FILENAME,
381 /* instead, put in our 'safer' original filename */
382 GNUNET_CONTAINER_meta_data_insert (ds->pos->meta,
384 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
385 EXTRACTOR_METAFORMAT_UTF8,
387 ds->pos->short_filename,
388 strlen (ds->pos->short_filename)
391 ds->pos->ksk_uri = GNUNET_FS_uri_ksk_create_from_meta_data (
393 ds->pos = advance (ds->pos);
397 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED:
408 if (NULL == ds->toplevel)
410 ds->stop_task = GNUNET_SCHEDULER_add_now (&finish_scan, ds);
417 ds->progress_callback (ds->progress_callback_cls,
420 GNUNET_FS_DIRSCANNER_INTERNAL_ERROR);
426 * Function called if our helper process died.
428 * @param cls the 'struct GNUNET_FS_DirScanner' callback.
431 helper_died_cb (void *cls)
433 struct GNUNET_FS_DirScanner *ds = cls;
436 if (NULL != ds->stop_task)
437 return; /* normal death, was finished */
438 ds->progress_callback (ds->progress_callback_cls,
441 GNUNET_FS_DIRSCANNER_INTERNAL_ERROR);
446 * Start a directory scanner thread.
448 * @param filename name of the directory to scan
449 * @param disable_extractor #GNUNET_YES to not run libextractor on files (only
451 * @param ex if not NULL, must be a list of extra plugins for extractor
452 * @param cb the callback to call when there are scanning progress messages
453 * @param cb_cls closure for 'cb'
454 * @return directory scanner object to be used for controlling the scanner
456 struct GNUNET_FS_DirScanner *
457 GNUNET_FS_directory_scan_start (const char *filename,
458 int disable_extractor,
460 GNUNET_FS_DirScannerProgressCallback cb,
464 char *filename_expanded;
465 struct GNUNET_FS_DirScanner *ds;
467 if (0 != stat (filename, &sbuf))
469 filename_expanded = GNUNET_STRINGS_filename_expand (filename);
470 if (NULL == filename_expanded)
472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
473 "Starting to scan directory `%s'\n",
475 ds = GNUNET_new (struct GNUNET_FS_DirScanner);
476 ds->progress_callback = cb;
477 ds->progress_callback_cls = cb_cls;
478 ds->filename_expanded = filename_expanded;
479 if (disable_extractor)
480 ds->ex_arg = GNUNET_strdup ("-");
482 ds->ex_arg = (NULL != ex) ? GNUNET_strdup (ex) : NULL;
483 ds->args[0] = "gnunet-helper-fs-publish";
484 ds->args[1] = ds->filename_expanded;
485 ds->args[2] = ds->ex_arg;
487 ds->helper = GNUNET_HELPER_start (GNUNET_NO,
488 "gnunet-helper-fs-publish",
490 &process_helper_msgs,
493 if (NULL == ds->helper)
495 GNUNET_free (filename_expanded);
503 /* end of fs_dirmetascan.c */