aboutsummaryrefslogtreecommitdiff
path: root/src/fs/fs_dirmetascan.c
blob: 4e5354e802c204167321f895d9f63aa1ba30bc00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
     This file is part of GNUnet
     (C) 2005-2012 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 2, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file fs/fs_dirmetascan.c
 * @brief code to asynchronously build a 'struct GNUNET_FS_ShareTreeItem'
 *        from an on-disk directory for publishing; use the 'gnunet-helper-fs-publish'.
 * @author LRN
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_fs_service.h"
#include "gnunet_scheduler_lib.h"
#include <pthread.h>


/**
 * An opaque structure a pointer to which is returned to the
 * caller to be used to control the scanner.
 */
struct GNUNET_FS_DirScanner
{

  /**
   * Helper process.
   */
  struct GNUNET_HELPER_Handle *helper;

  /**
   * Expanded filename (as given by the scan initiator).
   * The scanner thread stores a copy here, and frees it when it finishes.
   */
  char *filename_expanded;

  /**
   * Second argument to helper process.
   */
  char *ex_arg;
  
  /**
   * The function that will be called every time there's a progress
   * message.
   */
  GNUNET_FS_DirScannerProgressCallback progress_callback;
  
  /**
   * A closure for progress_callback.
   */
  void *progress_callback_cls;

  /**
   * After the scan is finished, it will contain a pointer to the
   * top-level directory entry in the directory tree built by the
   * scanner. 
   */
  struct GNUNET_FS_ShareTreeItem *toplevel;

  /**
   * Current position during processing.
   */
  struct GNUNET_FS_ShareTreeItem *pos;

  /**
   * Task scheduled when we are done.
   */
  GNUNET_SCHEDULER_TaskIdentifier stop_task;

  /**
   * Arguments for helper.
   */
  char *args[4];

};



/**
 * Abort the scan.  Must not be called from within the progress_callback
 * function.
 *
 * @param ds directory scanner structure
 */
void
GNUNET_FS_directory_scan_abort (struct GNUNET_FS_DirScanner *ds)
{
  /* terminate helper */
  if (NULL != ds->helper)
    GNUNET_HELPER_stop (ds->helper);
  
  /* free resources */
  if (NULL != ds->toplevel)
    GNUNET_FS_share_tree_free (ds->toplevel);
  if (GNUNET_SCHEDULER_NO_TASK != ds->stop_task)
    GNUNET_SCHEDULER_cancel (ds->stop_task);
  GNUNET_free_non_null (ds->ex_arg);
  GNUNET_free (ds->filename_expanded);
  GNUNET_free (ds);
}


/**
 * Obtain the result of the scan after the scan has signalled
 * completion.  Must not be called prior to completion.  The 'ds' is
 * freed as part of this call.
 *
 * @param ds directory scanner structure
 * @return the results of the scan (a directory tree)
 */
struct GNUNET_FS_ShareTreeItem *
GNUNET_FS_directory_scan_get_result (struct GNUNET_FS_DirScanner *ds)
{
  struct GNUNET_FS_ShareTreeItem *result;

  /* check that we're actually done */
  GNUNET_assert (NULL == ds->helper);
  /* preserve result */
  result = ds->toplevel;
  ds->toplevel = NULL; 
  GNUNET_FS_directory_scan_abort (ds);
  return result;
}


/**
 * Move in the directory from the given position to the next file
 * in DFS traversal.
 *
 * @param pos current position
 * @return next file, NULL for none
 */
static struct GNUNET_FS_ShareTreeItem *
advance (struct GNUNET_FS_ShareTreeItem *pos)
{
  int moved;
  
  GNUNET_assert (NULL != pos);
  moved = 0; /* must not terminate, even on file, otherwise "normal" */
  while ( (pos->is_directory == GNUNET_YES) ||
	  (0 == moved) )
  {
    if ( (moved != -1) &&
	 (NULL != pos->children_head) )
    {
      pos = pos->children_head;
      moved = 1; /* can terminate if file */
      continue;
    }
    if (NULL != pos->next)
    {
      pos = pos->next;
      moved = 1; /* can terminate if file */
      continue;
    }
    if (NULL != pos->parent)
    {
      pos = pos->parent;
      moved = -1; /* force move to 'next' or 'parent' */
      continue;
    }
    /* no more options, end of traversal */
    return NULL;
  }
  return pos;
}


/**
 * Add another child node to the tree.
 *
 * @param parent parent of the child, NULL for top level
 * @param filename name of the file or directory
 * @param is_directory GNUNET_YES for directories
 * @return new entry that was just created
 */
static struct GNUNET_FS_ShareTreeItem *
expand_tree (struct GNUNET_FS_ShareTreeItem *parent,
	     const char *filename,
	     int is_directory)
{
  struct GNUNET_FS_ShareTreeItem *chld;
  size_t slen;

  chld = GNUNET_malloc (sizeof (struct GNUNET_FS_ShareTreeItem));
  chld->parent = parent;
  chld->filename = GNUNET_strdup (filename);
  GNUNET_asprintf (&chld->short_filename,
		   "%s%s",
		   GNUNET_STRINGS_get_short_name (filename),
		   is_directory == GNUNET_YES ? "/" : "");
  /* make sure we do not end with '//' */
  slen = strlen (chld->short_filename);
  if ( (slen >= 2) &&
       (chld->short_filename[slen-1] == '/') &&
       (chld->short_filename[slen-2] == '/') )
    chld->short_filename[slen-1] = '\0';
  chld->is_directory = is_directory;
  if (NULL != parent)
      GNUNET_CONTAINER_DLL_insert (parent->children_head,
				   parent->children_tail,
				   chld);  
  return chld;
}


/**
 * Task run last to shut everything down.
 *
 * @param cls the 'struct GNUNET_FS_DirScanner'
 * @param tc unused
 */
static void
finish_scan (void *cls,
	     const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_FS_DirScanner *ds = cls;

  ds->stop_task = GNUNET_SCHEDULER_NO_TASK;
  GNUNET_HELPER_stop (ds->helper);
  ds->helper = NULL;
  ds->progress_callback (ds->progress_callback_cls, 
			 NULL, GNUNET_SYSERR,
			 GNUNET_FS_DIRSCANNER_FINISHED);    
}


/**
 * Called every time there is data to read from the scanner.
 * Calls the scanner progress handler.
 *
 * @param cls the closure (directory scanner object)
 * @param client always NULL
 * @param msg message from the helper process
 */
static void
process_helper_msgs (void *cls, 
		     void *client,
		     const struct GNUNET_MessageHeader *msg)
{
  struct GNUNET_FS_DirScanner *ds = cls;
  const char *filename;
  size_t left;

  left = ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader);
  filename = (const char*) &msg[1];
  switch (ntohs (msg->type))
  {
  case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_FILE:
    if (filename[left-1] != '\0')
    {
      GNUNET_break (0);
      break;
    }
    ds->progress_callback (ds->progress_callback_cls, 
			   filename, GNUNET_NO,
			   GNUNET_FS_DIRSCANNER_FILE_START);
    if (NULL == ds->toplevel)
      ds->toplevel = expand_tree (ds->pos,
				  filename, GNUNET_NO);
    else
      (void) expand_tree (ds->pos,
			  filename, GNUNET_NO);
    return;
  case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY:
    if (filename[left-1] != '\0')
    {
      GNUNET_break (0);
      break;
    }
    if (0 == strcmp ("..", filename))
    {
      if (NULL == ds->pos)
      {
	GNUNET_break (0);
	break;
      }
      ds->pos = ds->pos->parent;
      return;
    }
    ds->progress_callback (ds->progress_callback_cls, 
			   filename, GNUNET_YES,
			   GNUNET_FS_DIRSCANNER_FILE_START);
    ds->pos = expand_tree (ds->pos,
			   filename, GNUNET_YES);
    if (NULL == ds->toplevel)
      ds->toplevel = ds->pos;
    return;
  case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR:
    break;
  case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_SKIP_FILE:
    if (filename[left-1] != '\0')
      break;
    ds->progress_callback (ds->progress_callback_cls, 
			   filename, GNUNET_SYSERR,
			   GNUNET_FS_DIRSCANNER_FILE_IGNORED);
    return;
  case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE:
    if (0 != left)
    {
      GNUNET_break (0);
      break;
    }
    if (NULL == ds->toplevel)
    {
      GNUNET_break (0);
      break;
    }
    ds->progress_callback (ds->progress_callback_cls, 
			   NULL, GNUNET_SYSERR,
			   GNUNET_FS_DIRSCANNER_ALL_COUNTED);
    ds->pos = ds->toplevel;
    if (ds->pos->is_directory == GNUNET_YES)
      ds->pos = advance (ds->pos);
    return;
  case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA:
    {
      size_t nlen;
      const char *end;
      
      if (NULL == ds->pos)
      {
	GNUNET_break (0);
	break;
      }
      end = memchr (filename, 0, left);
      if (NULL == end)
      {
	GNUNET_break (0);
	break;
      }
      end++;
      nlen = end - filename;
      left -= nlen;
      if (0 != strcmp (filename,
		       ds->pos->filename))
      {
	GNUNET_break (0);
	break;
      }
      ds->progress_callback (ds->progress_callback_cls, 
			     filename, GNUNET_YES,
			     GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED);
      if (0 < left)
      {
	ds->pos->meta = GNUNET_CONTAINER_meta_data_deserialize (end, left);
	if (NULL == ds->pos->meta)
	{
	  GNUNET_break (0);
	  break;
	}
	/* having full filenames is too dangerous; always make sure we clean them up */
	GNUNET_CONTAINER_meta_data_delete (ds->pos->meta, 
					   EXTRACTOR_METATYPE_FILENAME,
					   NULL, 0);
	/* instead, put in our 'safer' original filename */
	GNUNET_CONTAINER_meta_data_insert (ds->pos->meta, "<libgnunetfs>",
					   EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
					   EXTRACTOR_METAFORMAT_UTF8, "text/plain",
					   ds->pos->short_filename, 
					   strlen (ds->pos->short_filename) + 1);
      }
      ds->pos->ksk_uri = GNUNET_FS_uri_ksk_create_from_meta_data (ds->pos->meta);
      ds->pos = advance (ds->pos);      
      return;
    }
  case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED:
    if (NULL != ds->pos)
    {
      GNUNET_break (0);
      break;
    }
    if (0 != left)
    {
      GNUNET_break (0);
      break;
    }   
    if (NULL == ds->toplevel)
    {
      GNUNET_break (0);
      break;
    }
    ds->stop_task = GNUNET_SCHEDULER_add_now (&finish_scan,
					      ds);
    return;
  default:
    GNUNET_break (0);
    break;
  }
  ds->progress_callback (ds->progress_callback_cls, 
			 NULL, GNUNET_SYSERR,
			 GNUNET_FS_DIRSCANNER_INTERNAL_ERROR);
}


/**
 * Start a directory scanner thread.
 *
 * @param filename name of the directory to scan
 * @param disable_extractor GNUNET_YES to not to run libextractor on files (only build a tree)
 * @param ex if not NULL, must be a list of extra plugins for extractor
 * @param cb the callback to call when there are scanning progress messages
 * @param cb_cls closure for 'cb'
 * @return directory scanner object to be used for controlling the scanner
 */
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)
{
  struct stat sbuf;
  char *filename_expanded;
  struct GNUNET_FS_DirScanner *ds;

  if (0 != STAT (filename, &sbuf))
    return NULL;
  filename_expanded = GNUNET_STRINGS_filename_expand (filename);
  if (NULL == filename_expanded)
    return NULL;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Starting to scan directory `%s'\n",
	      filename_expanded);
  ds = GNUNET_malloc (sizeof (struct GNUNET_FS_DirScanner));
  ds->progress_callback = cb;
  ds->progress_callback_cls = cb_cls;
  ds->filename_expanded = filename_expanded;
  if (disable_extractor)  
    ds->ex_arg = GNUNET_strdup ("-");
  else 
    ds->ex_arg = (NULL != ex) ? GNUNET_strdup (ex) : NULL;
  ds->args[0] = "gnunet-helper-fs-publish";
  ds->args[1] = ds->filename_expanded;
  ds->args[2] = ds->ex_arg;
  ds->args[3] = NULL;
  ds->helper = GNUNET_HELPER_start ("gnunet-helper-fs-publish",
				    ds->args,
				    &process_helper_msgs,
				    ds);
  if (NULL == ds->helper)
  {
    GNUNET_free (filename_expanded);
    GNUNET_free (ds);
    return NULL;
  }
  return ds;
}


/* end of fs_dirmetascan.c */