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 * Arguments for helper.
92 * @param ds directory scanner structure
95 GNUNET_FS_directory_scan_abort (struct GNUNET_FS_DirScanner *ds)
97 /* terminate helper */
98 GNUNET_HELPER_stop (ds->helper);
101 if (NULL != ds->toplevel)
102 GNUNET_FS_share_tree_free (ds->toplevel);
103 GNUNET_free (ds->ex_arg);
104 GNUNET_free (ds->filename_expanded);
110 * Obtain the result of the scan after the scan has signalled
111 * completion. Must not be called prior to completion. The 'ds' is
112 * freed as part of this call.
114 * @param ds directory scanner structure
115 * @return the results of the scan (a directory tree)
117 struct GNUNET_FS_ShareTreeItem *
118 GNUNET_FS_directory_scan_get_result (struct GNUNET_FS_DirScanner *ds)
120 struct GNUNET_FS_ShareTreeItem *result;
122 /* check that we're actually done */
123 GNUNET_assert (NULL == ds->helper);
124 /* preserve result */
125 result = ds->toplevel;
127 GNUNET_FS_directory_scan_abort (ds);
133 * Move in the directory from the given position to the next file
136 * @param pos current position
137 * @return next file, NULL for none
139 static struct GNUNET_FS_ShareTreeItem *
140 advance (struct GNUNET_FS_ShareTreeItem *pos)
144 GNUNET_assert (NULL != pos);
145 moved = 0; /* must not terminate, even on file, otherwise "normal" */
146 while ( (pos->is_directory) ||
149 if ( (moved != -1) &&
150 (NULL != pos->children_head) )
152 pos = pos->children_head;
153 moved = 1; /* can terminate if file */
156 if (NULL != pos->next)
159 moved = 1; /* can terminate if file */
162 if (NULL != pos->parent)
165 moved = -1; /* force move to 'next' or 'parent' */
168 /* no more options, end of traversal */
176 * Add another child node to the tree.
178 * @param parent parent of the child, NULL for top level
179 * @param filename name of the file or directory
180 * @param is_directory GNUNET_YES for directories
181 * @return new entry that was just created
183 static struct GNUNET_FS_ShareTreeItem *
184 expand_tree (struct GNUNET_FS_ShareTreeItem *parent,
185 const char *filename,
188 struct GNUNET_FS_ShareTreeItem *chld;
190 chld = GNUNET_malloc (sizeof (struct GNUNET_FS_ShareTreeItem));
191 chld->parent = parent;
192 chld->filename = GNUNET_strdup (filename);
193 chld->is_directory = is_directory;
195 GNUNET_CONTAINER_DLL_insert (parent->children_head,
196 parent->children_tail,
203 * Called every time there is data to read from the scanner.
204 * Calls the scanner progress handler.
206 * @param cls the closure (directory scanner object)
207 * @param client always NULL
208 * @param msg message from the helper process
211 process_helper_msgs (void *cls,
213 const struct GNUNET_MessageHeader *msg)
215 struct GNUNET_FS_DirScanner *ds = cls;
216 const char *filename;
219 left = ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader);
220 filename = (const char*) &msg[1];
221 switch (ntohs (msg->type))
223 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_FILE:
224 if (filename[left-1] != '\0')
229 ds->progress_callback (ds->progress_callback_cls,
231 GNUNET_FS_DIRSCANNER_FILE_START);
232 expand_tree (ds->pos,
233 filename, GNUNET_NO);
235 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY:
236 if (filename[left-1] != '\0')
241 if (0 == strcmp ("..", filename))
248 ds->pos = ds->pos->parent;
251 ds->progress_callback (ds->progress_callback_cls,
252 filename, GNUNET_YES,
253 GNUNET_FS_DIRSCANNER_FILE_START);
254 ds->pos = expand_tree (ds->pos,
255 filename, GNUNET_YES);
256 if (NULL == ds->toplevel)
257 ds->toplevel = ds->pos;
259 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR:
261 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_SKIP_FILE:
262 if (filename[left-1] != '\0')
264 ds->progress_callback (ds->progress_callback_cls,
265 filename, GNUNET_SYSERR,
266 GNUNET_FS_DIRSCANNER_FILE_IGNORED);
268 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE:
274 ds->progress_callback (ds->progress_callback_cls,
276 GNUNET_FS_DIRSCANNER_ALL_COUNTED);
277 ds->pos = ds->toplevel;
278 if (ds->pos->is_directory)
279 ds->pos = advance (ds->pos);
281 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA:
291 end = memchr (filename, 0, left);
298 nlen = end - filename;
300 if (0 != strcmp (filename,
306 ds->progress_callback (ds->progress_callback_cls,
307 filename, GNUNET_YES,
308 GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED);
311 ds->pos->meta = GNUNET_CONTAINER_meta_data_deserialize (end, left);
312 if (NULL == ds->pos->meta)
317 /* having full filenames is too dangerous; always make sure we clean them up */
318 ds->pos->short_filename = GNUNET_strdup (GNUNET_STRINGS_get_short_name (filename));
319 GNUNET_CONTAINER_meta_data_delete (ds->pos->meta,
320 EXTRACTOR_METATYPE_FILENAME,
322 GNUNET_CONTAINER_meta_data_insert (ds->pos->meta, "<libgnunetfs>",
323 EXTRACTOR_METATYPE_FILENAME,
324 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
325 ds->pos->short_filename,
326 strlen (ds->pos->short_filename) + 1);
328 ds->pos = advance (ds->pos);
331 case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED:
342 ds->progress_callback (ds->progress_callback_cls,
344 GNUNET_FS_DIRSCANNER_INTERNAL_ERROR);
351 ds->progress_callback (ds->progress_callback_cls,
353 GNUNET_FS_DIRSCANNER_INTERNAL_ERROR);
359 * Start a directory scanner thread.
361 * @param filename name of the directory to scan
362 * @param GNUNET_YES to not to run libextractor on files (only build a tree)
363 * @param ex if not NULL, must be a list of extra plugins for extractor
364 * @param cb the callback to call when there are scanning progress messages
365 * @param cb_cls closure for 'cb'
366 * @return directory scanner object to be used for controlling the scanner
368 struct GNUNET_FS_DirScanner *
369 GNUNET_FS_directory_scan_start (const char *filename,
370 int disable_extractor, const char *ex,
371 GNUNET_FS_DirScannerProgressCallback cb,
375 char *filename_expanded;
376 struct GNUNET_FS_DirScanner *ds;
378 if (0 != STAT (filename, &sbuf))
380 filename_expanded = GNUNET_STRINGS_filename_expand (filename);
381 if (NULL == filename_expanded)
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
384 "Starting to scan directory `%s'\n",
386 ds = GNUNET_malloc (sizeof (struct GNUNET_FS_DirScanner));
387 ds->progress_callback = cb;
388 ds->progress_callback_cls = cb_cls;
389 ds->filename_expanded = filename_expanded;
390 if (disable_extractor)
391 ds->ex_arg = GNUNET_strdup ("-");
393 ds->ex_arg = (NULL != ex) ? GNUNET_strdup (ex) : NULL;
394 ds->args[0] = "gnunet-helper-fs-publish";
395 ds->args[1] = ds->filename_expanded;
396 ds->args[2] = ds->ex_arg;
398 ds->helper = GNUNET_HELPER_start ("gnunet-helper-fs-publish",
400 &process_helper_msgs,
402 if (NULL == ds->helper)
404 GNUNET_free (filename_expanded);
412 /* end of fs_dirmetascan.c */