GNUnet  0.10.x
fs_dirmetascan.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2005-2012 GNUnet e.V.
4 
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.
9 
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.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
28 #include "platform.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_scheduler_lib.h"
31 #include <pthread.h>
32 
33 
39 {
40 
45 
51 
55  char *ex_arg;
56 
62 
67 
74 
79 
84 
88  char *args[4];
89 
90 };
91 
92 
99 void
101 {
102  /* terminate helper */
103  if (NULL != ds->helper)
105 
106  /* free resources */
107  if (NULL != ds->toplevel)
109  if (NULL != ds->stop_task)
113  GNUNET_free (ds);
114 }
115 
116 
127 {
129 
130  /* check that we're actually done */
131  GNUNET_assert (NULL == ds->helper);
132  /* preserve result */
133  result = ds->toplevel;
134  ds->toplevel = NULL;
136  return result;
137 }
138 
139 
147 static struct GNUNET_FS_ShareTreeItem *
149 {
150  int moved;
151 
152  GNUNET_assert (NULL != pos);
153  moved = 0; /* must not terminate, even on file, otherwise "normal" */
154  while ( (pos->is_directory == GNUNET_YES) ||
155  (0 == moved) )
156  {
157  if ( (moved != -1) &&
158  (NULL != pos->children_head) )
159  {
160  pos = pos->children_head;
161  moved = 1; /* can terminate if file */
162  continue;
163  }
164  if (NULL != pos->next)
165  {
166  pos = pos->next;
167  moved = 1; /* can terminate if file */
168  continue;
169  }
170  if (NULL != pos->parent)
171  {
172  pos = pos->parent;
173  moved = -1; /* force move to 'next' or 'parent' */
174  continue;
175  }
176  /* no more options, end of traversal */
177  return NULL;
178  }
179  return pos;
180 }
181 
182 
191 static struct GNUNET_FS_ShareTreeItem *
193  const char *filename,
194  int is_directory)
195 {
196  struct GNUNET_FS_ShareTreeItem *chld;
197  size_t slen;
198 
199  chld = GNUNET_new (struct GNUNET_FS_ShareTreeItem);
200  chld->parent = parent;
201  chld->filename = GNUNET_strdup (filename);
203  "%s%s",
205  is_directory == GNUNET_YES ? "/" : "");
206  /* make sure we do not end with '//' */
207  slen = strlen (chld->short_filename);
208  if ( (slen >= 2) &&
209  (chld->short_filename[slen-1] == '/') &&
210  (chld->short_filename[slen-2] == '/') )
211  chld->short_filename[slen-1] = '\0';
212  chld->is_directory = is_directory;
213  if (NULL != parent)
215  parent->children_tail,
216  chld);
217  return chld;
218 }
219 
220 
226 static void
227 finish_scan (void *cls)
228 {
229  struct GNUNET_FS_DirScanner *ds = cls;
230 
231  ds->stop_task = NULL;
232  if (NULL != ds->helper)
233  {
235  ds->helper = NULL;
236  }
238  NULL, GNUNET_SYSERR,
240 }
241 
242 
253 static int
255  const struct GNUNET_MessageHeader *msg)
256 {
257  struct GNUNET_FS_DirScanner *ds = cls;
258  const char *filename;
259  size_t left;
260 
261 #if 0
262  fprintf (stderr,
263  "DMS parses %u-byte message of type %u\n",
264  (unsigned int) ntohs (msg->size),
265  (unsigned int) ntohs (msg->type));
266 #endif
267  left = ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader);
268  filename = (const char*) &msg[1];
269  switch (ntohs (msg->type))
270  {
272  if (filename[left-1] != '\0')
273  {
274  GNUNET_break (0);
275  break;
276  }
278  filename, GNUNET_NO,
280  if (NULL == ds->toplevel)
281  {
282  ds->toplevel = expand_tree (ds->pos,
283  filename,
284  GNUNET_NO);
285  }
286  else
287  {
288  GNUNET_assert (NULL != ds->pos);
289  (void) expand_tree (ds->pos,
290  filename,
291  GNUNET_NO);
292  }
293  return GNUNET_OK;
295  if (filename[left-1] != '\0')
296  {
297  GNUNET_break (0);
298  break;
299  }
300  if (0 == strcmp ("..", filename))
301  {
302  if (NULL == ds->pos)
303  {
304  GNUNET_break (0);
305  break;
306  }
307  ds->pos = ds->pos->parent;
308  return GNUNET_OK;
309  }
311  filename, GNUNET_YES,
313  ds->pos = expand_tree (ds->pos,
314  filename,
315  GNUNET_YES);
316  if (NULL == ds->toplevel)
317  ds->toplevel = ds->pos;
318  return GNUNET_OK;
320  break;
322  if ('\0' != filename[left-1])
323  break;
325  filename, GNUNET_SYSERR,
327  return GNUNET_OK;
329  if (0 != left)
330  {
331  GNUNET_break (0);
332  break;
333  }
334  if (NULL == ds->toplevel)
335  break;
337  NULL, GNUNET_SYSERR,
339  ds->pos = ds->toplevel;
340  if (GNUNET_YES == ds->pos->is_directory)
341  ds->pos = advance (ds->pos);
342  return GNUNET_OK;
344  {
345  size_t nlen;
346  const char *end;
347 
348  if (NULL == ds->pos)
349  {
350  GNUNET_break (0);
351  break;
352  }
353  end = memchr (filename, 0, left);
354  if (NULL == end)
355  {
356  GNUNET_break (0);
357  break;
358  }
359  end++;
360  nlen = end - filename;
361  left -= nlen;
362  if (0 != strcmp (filename,
363  ds->pos->filename))
364  {
365  GNUNET_break (0);
366  break;
367  }
369  filename,
370  GNUNET_YES,
372  if (0 < left)
373  {
375  left);
376  if (NULL == ds->pos->meta)
377  {
378  GNUNET_break (0);
379  break;
380  }
381  /* having full filenames is too dangerous; always make sure we clean them up */
384  NULL, 0);
385  /* instead, put in our 'safer' original filename */
386  GNUNET_CONTAINER_meta_data_insert (ds->pos->meta, "<libgnunetfs>",
388  EXTRACTOR_METAFORMAT_UTF8, "text/plain",
389  ds->pos->short_filename,
390  strlen (ds->pos->short_filename) + 1);
391  }
393  ds->pos = advance (ds->pos);
394  return GNUNET_OK;
395  }
397  if (NULL != ds->pos)
398  {
399  GNUNET_break (0);
400  break;
401  }
402  if (0 != left)
403  {
404  GNUNET_break (0);
405  break;
406  }
407  if (NULL == ds->toplevel)
408  break;
410  ds);
411  return GNUNET_OK;
412  default:
413  GNUNET_break (0);
414  break;
415  }
417  NULL, GNUNET_SYSERR,
419  return GNUNET_OK;
420 }
421 
422 
428 static void
429 helper_died_cb (void *cls)
430 {
431  struct GNUNET_FS_DirScanner *ds = cls;
432 
433  ds->helper = NULL;
434  if (NULL != ds->stop_task)
435  return; /* normal death, was finished */
437  NULL, GNUNET_SYSERR,
439 }
440 
441 
453 struct GNUNET_FS_DirScanner *
455  int disable_extractor, const char *ex,
457  void *cb_cls)
458 {
459  struct stat sbuf;
460  char *filename_expanded;
461  struct GNUNET_FS_DirScanner *ds;
462 
463  if (0 != STAT (filename, &sbuf))
464  return NULL;
465  filename_expanded = GNUNET_STRINGS_filename_expand (filename);
466  if (NULL == filename_expanded)
467  return NULL;
469  "Starting to scan directory `%s'\n",
470  filename_expanded);
471  ds = GNUNET_new (struct GNUNET_FS_DirScanner);
472  ds->progress_callback = cb;
473  ds->progress_callback_cls = cb_cls;
475  if (disable_extractor)
476  ds->ex_arg = GNUNET_strdup ("-");
477  else
478  ds->ex_arg = (NULL != ex) ? GNUNET_strdup (ex) : NULL;
479  ds->args[0] = "gnunet-helper-fs-publish";
480  ds->args[1] = ds->filename_expanded;
481  ds->args[2] = ds->ex_arg;
482  ds->args[3] = NULL;
484  "gnunet-helper-fs-publish",
485  ds->args,
487  &helper_died_cb, ds);
488  if (NULL == ds->helper)
489  {
490  GNUNET_free (filename_expanded);
491  GNUNET_free (ds);
492  return NULL;
493  }
494  return ds;
495 }
496 
497 
498 /* end of fs_dirmetascan.c */
static void finish_scan(void *cls)
Task run last to shut everything down.
static void helper_died_cb(void *cls)
Function called if our helper process died.
struct GNUNET_HELPER_Handle * helper
Helper process.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
void(* GNUNET_FS_DirScannerProgressCallback)(void *cls, const char *filename, int is_directory, enum GNUNET_FS_DirScannerProgressUpdateReason reason)
Function called over time as the directory scanner makes progress on the job at hand.
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR
Error signal from the helper.
struct GNUNET_FS_DirScanner * GNUNET_FS_directory_scan_start(const char *filename, int disable_extractor, const char *ex, GNUNET_FS_DirScannerProgressCallback cb, void *cb_cls)
Start a directory scanner thread.
static struct GNUNET_FS_ShareTreeItem * expand_tree(struct GNUNET_FS_ShareTreeItem *parent, const char *filename, int is_directory)
Add another child node to the tree.
The handle to a helper process.
Definition: helper.c:80
struct GNUNET_HELPER_Handle * GNUNET_HELPER_start(int with_control_pipe, const char *binary_name, char *const binary_argv[], GNUNET_MessageTokenizerCallback cb, GNUNET_HELPER_ExceptionCallback exp_cb, void *cb_cls)
Starts a helper and begins reading from it.
Definition: helper.c:491
void * progress_callback_cls
A closure for progress_callback.
char * args[4]
Arguments for helper.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_FS_ShareTreeItem * parent
This is a doubly-linked tree NULL for top-level entries.
struct GNUNET_FS_ShareTreeItem * children_tail
This is a doubly-linked tree NULL for files and empty directories.
void GNUNET_HELPER_stop(struct GNUNET_HELPER_Handle *h, int soft_kill)
Kills the helper, closes the pipe, frees the handle and calls wait() on the helper process...
Definition: helper.c:569
struct GNUNET_FS_Uri * ksk_uri
Keywords for this file or directory (derived from metadata).
struct GNUNET_CONTAINER_MetaData * GNUNET_CONTAINER_meta_data_deserialize(const char *input, size_t size)
Deserialize meta-data.
0-terminated, UTF-8 encoded string.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
An opaque structure a pointer to which is returned to the caller to be used to control the scanner...
#define GNUNET_free_non_null(ptr)
Free the memory pointed to by ptr if ptr is not NULL.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
char * filename_expanded
Expanded filename (as given by the scan initiator).
#define STAT(p, b)
Definition: plibc.h:663
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
struct GNUNET_SCHEDULER_Task * stop_task
Task scheduled when we are done.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
We&#39;ve found all files (in the pre-pass).
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_FS_ShareTreeItem * next
This is a doubly-linked list.
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
A node of a directory tree (produced by dirscanner)
struct GNUNET_FS_ShareTreeItem * children_head
This is a doubly-linked tree NULL for files and empty directories.
GNUNET_FS_DirScannerProgressCallback progress_callback
The function that will be called every time there&#39;s a progress message.
struct GNUNET_FS_ShareTreeItem * pos
Current position during processing.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1273
static int disable_extractor
Disable extractor option to use for publishing.
static char * filename
static int result
Global testing status.
void GNUNET_FS_directory_scan_abort(struct GNUNET_FS_DirScanner *ds)
Abort the scan.
There was an internal error.
int GNUNET_CONTAINER_meta_data_insert(struct GNUNET_CONTAINER_MetaData *md, const char *plugin_name, enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format, const char *data_mime_type, const char *data, size_t data_size)
Extend metadata.
int GNUNET_CONTAINER_meta_data_delete(struct GNUNET_CONTAINER_MetaData *md, enum EXTRACTOR_MetaType type, const char *data, size_t data_size)
Remove an item.
char * short_filename
Base name of the file/directory.
const char * GNUNET_STRINGS_get_short_name(const char *filename)
"man basename" Returns a pointer to a part of filename (allocates nothing)!
Definition: strings.c:845
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition: strings.c:602
Last call to the progress function: we have finished scanning the directory.
static int process_helper_msgs(void *cls, const struct GNUNET_MessageHeader *msg)
Called every time there is data to read from the scanner.
int is_directory
GNUNET_YES if this is a directory
char * filename
Name of the file/directory.
static struct GNUNET_FS_ShareTreeItem * advance(struct GNUNET_FS_ShareTreeItem *pos)
Move in the directory from the given position to the next file in DFS traversal.
#define GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA
Extracted meta data from the helper.
struct GNUNET_CONTAINER_MetaData * meta
Metadata for this file or directory.
#define GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY
Progress information from the helper: found a directory.
We&#39;ve started processing a file or directory.
struct GNUNET_FS_Uri * GNUNET_FS_uri_ksk_create_from_meta_data(const struct GNUNET_CONTAINER_MetaData *md)
Construct a keyword-URI from meta-data (take all entries in the meta-data and construct one large key...
Definition: fs_uri.c:1758
void GNUNET_FS_share_tree_free(struct GNUNET_FS_ShareTreeItem *toplevel)
Release memory of a share item tree.
Definition: fs_sharetree.c:432
#define GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE
Signal that helper is done scanning the directory tree.
#define GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_FILE
Progress information from the helper: found a file.
#define GNUNET_log(kind,...)
#define GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_SKIP_FILE
Signal that helper skipped a file.
Entry in list of pending tasks.
Definition: scheduler.c:134
We&#39;ve finished extracting meta data from a file.
#define GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED
Signal that helper is done.
char * ex_arg
Second argument to helper process.
struct GNUNET_FS_ShareTreeItem * toplevel
After the scan is finished, it will contain a pointer to the top-level directory entry in the directo...
Header for all communications.
struct GNUNET_FS_ShareTreeItem * GNUNET_FS_directory_scan_get_result(struct GNUNET_FS_DirScanner *ds)
Obtain the result of the scan after the scan has signalled completion.
#define GNUNET_YES
Definition: gnunet_common.h:80
static struct GNUNET_FS_DirScanner * ds
Handle to the directory scanner (for recursive insertions).
#define GNUNET_free(ptr)
Wrapper around free.
We&#39;re having trouble accessing a file (soft-error); it will be ignored.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965