- clean up the receive switch case
[oweals/gnunet.git] / src / fs / fs_dirmetascan.c
1 /*
2      This file is part of GNUnet
3      (C) 2005-2012 Christian Grothoff (and other contributing authors)
4
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.
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      General Public License for more details.
14
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.
19 */
20
21 /**
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'.
25  * @author LRN
26  * @author Christian Grothoff
27  */
28 #include "platform.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_scheduler_lib.h"
31 #include <pthread.h>
32
33
34 /**
35  * An opaque structure a pointer to which is returned to the
36  * caller to be used to control the scanner.
37  */
38 struct GNUNET_FS_DirScanner
39 {
40
41   /**
42    * Helper process.
43    */
44   struct GNUNET_HELPER_Handle *helper;
45
46   /**
47    * Expanded filename (as given by the scan initiator).
48    * The scanner thread stores a copy here, and frees it when it finishes.
49    */
50   char *filename_expanded;
51
52   /**
53    * Second argument to helper process.
54    */
55   char *ex_arg;
56   
57   /**
58    * The function that will be called every time there's a progress
59    * message.
60    */
61   GNUNET_FS_DirScannerProgressCallback progress_callback;
62   
63   /**
64    * A closure for progress_callback.
65    */
66   void *progress_callback_cls;
67
68   /**
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
71    * scanner. 
72    */
73   struct GNUNET_FS_ShareTreeItem *toplevel;
74
75   /**
76    * Current position during processing.
77    */
78   struct GNUNET_FS_ShareTreeItem *pos;
79
80   /**
81    * Task scheduled when we are done.
82    */
83   GNUNET_SCHEDULER_TaskIdentifier stop_task;
84
85   /**
86    * Arguments for helper.
87    */
88   char *args[4];
89
90 };
91
92
93
94 /**
95  * Abort the scan.
96  *
97  * @param ds directory scanner structure
98  */
99 void
100 GNUNET_FS_directory_scan_abort (struct GNUNET_FS_DirScanner *ds)
101 {
102   /* terminate helper */
103   if (NULL != ds->helper)
104     GNUNET_HELPER_stop (ds->helper);
105   
106   /* free resources */
107   if (NULL != ds->toplevel)
108     GNUNET_FS_share_tree_free (ds->toplevel);
109   if (GNUNET_SCHEDULER_NO_TASK != ds->stop_task)
110     GNUNET_SCHEDULER_cancel (ds->stop_task);
111   GNUNET_free_non_null (ds->ex_arg);
112   GNUNET_free (ds->filename_expanded);
113   GNUNET_free (ds);
114 }
115
116
117 /**
118  * Obtain the result of the scan after the scan has signalled
119  * completion.  Must not be called prior to completion.  The 'ds' is
120  * freed as part of this call.
121  *
122  * @param ds directory scanner structure
123  * @return the results of the scan (a directory tree)
124  */
125 struct GNUNET_FS_ShareTreeItem *
126 GNUNET_FS_directory_scan_get_result (struct GNUNET_FS_DirScanner *ds)
127 {
128   struct GNUNET_FS_ShareTreeItem *result;
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; 
135   GNUNET_FS_directory_scan_abort (ds);
136   return result;
137 }
138
139
140 /**
141  * Move in the directory from the given position to the next file
142  * in DFS traversal.
143  *
144  * @param pos current position
145  * @return next file, NULL for none
146  */
147 static struct GNUNET_FS_ShareTreeItem *
148 advance (struct GNUNET_FS_ShareTreeItem *pos)
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) ||
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
183 /**
184  * Add another child node to the tree.
185  *
186  * @param parent parent of the child, NULL for top level
187  * @param filename name of the file or directory
188  * @param is_directory GNUNET_YES for directories
189  * @return new entry that was just created
190  */
191 static struct GNUNET_FS_ShareTreeItem *
192 expand_tree (struct GNUNET_FS_ShareTreeItem *parent,
193              const char *filename,
194              int is_directory)
195 {
196   struct GNUNET_FS_ShareTreeItem *chld;
197
198   chld = GNUNET_malloc (sizeof (struct GNUNET_FS_ShareTreeItem));
199   chld->parent = parent;
200   chld->filename = GNUNET_strdup (filename);
201   GNUNET_asprintf (&chld->short_filename,
202                    "%s%s",
203                    GNUNET_STRINGS_get_short_name (filename),
204                    is_directory ? "/" : "");
205   chld->is_directory = is_directory;
206   if (NULL != parent)
207       GNUNET_CONTAINER_DLL_insert (parent->children_head,
208                                    parent->children_tail,
209                                    chld);  
210   return chld;
211 }
212
213
214 /**
215  * Task run last to shut everything down.
216  *
217  * @param cls the 'struct GNUNET_FS_DirScanner'
218  * @param tc unused
219  */
220 static void
221 finish_scan (void *cls,
222              const struct GNUNET_SCHEDULER_TaskContext *tc)
223 {
224   struct GNUNET_FS_DirScanner *ds = cls;
225
226   ds->stop_task = GNUNET_SCHEDULER_NO_TASK;
227   GNUNET_HELPER_stop (ds->helper);
228   ds->helper = NULL;
229   ds->progress_callback (ds->progress_callback_cls, 
230                          NULL, GNUNET_SYSERR,
231                          GNUNET_FS_DIRSCANNER_FINISHED);    
232 }
233
234
235 /**
236  * Called every time there is data to read from the scanner.
237  * Calls the scanner progress handler.
238  *
239  * @param cls the closure (directory scanner object)
240  * @param client always NULL
241  * @param msg message from the helper process
242  */
243 static void
244 process_helper_msgs (void *cls, 
245                      void *client,
246                      const struct GNUNET_MessageHeader *msg)
247 {
248   struct GNUNET_FS_DirScanner *ds = cls;
249   const char *filename;
250   size_t left;
251
252   left = ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader);
253   filename = (const char*) &msg[1];
254   switch (ntohs (msg->type))
255   {
256   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_FILE:
257     if (filename[left-1] != '\0')
258     {
259       GNUNET_break (0);
260       break;
261     }
262     ds->progress_callback (ds->progress_callback_cls, 
263                            filename, GNUNET_NO,
264                            GNUNET_FS_DIRSCANNER_FILE_START);
265     if (NULL == ds->toplevel)
266       ds->toplevel = expand_tree (ds->pos,
267                                   filename, GNUNET_NO);
268     else
269       (void) expand_tree (ds->pos,
270                           filename, GNUNET_NO);
271     return;
272   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY:
273     if (filename[left-1] != '\0')
274     {
275       GNUNET_break (0);
276       break;
277     }
278     if (0 == strcmp ("..", filename))
279     {
280       if (NULL == ds->pos)
281       {
282         GNUNET_break (0);
283         break;
284       }
285       ds->pos = ds->pos->parent;
286       return;
287     }
288     ds->progress_callback (ds->progress_callback_cls, 
289                            filename, GNUNET_YES,
290                            GNUNET_FS_DIRSCANNER_FILE_START);
291     ds->pos = expand_tree (ds->pos,
292                            filename, GNUNET_YES);
293     if (NULL == ds->toplevel)
294       ds->toplevel = ds->pos;
295     return;
296   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR:
297     break;
298   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_SKIP_FILE:
299     if (filename[left-1] != '\0')
300       break;
301     ds->progress_callback (ds->progress_callback_cls, 
302                            filename, GNUNET_SYSERR,
303                            GNUNET_FS_DIRSCANNER_FILE_IGNORED);
304     return;
305   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE:
306     if (0 != left)
307     {
308       GNUNET_break (0);
309       break;
310     }
311     if (NULL == ds->toplevel)
312     {
313       GNUNET_break (0);
314       break;
315     }
316     ds->progress_callback (ds->progress_callback_cls, 
317                            NULL, GNUNET_SYSERR,
318                            GNUNET_FS_DIRSCANNER_ALL_COUNTED);
319     ds->pos = ds->toplevel;
320     if (ds->pos->is_directory)
321       ds->pos = advance (ds->pos);
322     return;
323   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA:
324     {
325       size_t nlen;
326       const char *end;
327       
328       if (NULL == ds->pos)
329       {
330         GNUNET_break (0);
331         break;
332       }
333       end = memchr (filename, 0, left);
334       if (NULL == end)
335       {
336         GNUNET_break (0);
337         break;
338       }
339       end++;
340       nlen = end - filename;
341       left -= nlen;
342       if (0 != strcmp (filename,
343                        ds->pos->filename))
344       {
345         GNUNET_break (0);
346         break;
347       }
348       ds->progress_callback (ds->progress_callback_cls, 
349                              filename, GNUNET_YES,
350                              GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED);
351       if (0 < left)
352       {
353         ds->pos->meta = GNUNET_CONTAINER_meta_data_deserialize (end, left);
354         if (NULL == ds->pos->meta)
355         {
356           GNUNET_break (0);
357           break;
358         }
359         /* having full filenames is too dangerous; always make sure we clean them up */
360         GNUNET_CONTAINER_meta_data_delete (ds->pos->meta, 
361                                            EXTRACTOR_METATYPE_FILENAME,
362                                            NULL, 0);
363         GNUNET_CONTAINER_meta_data_insert (ds->pos->meta, "<libgnunetfs>",
364                                            EXTRACTOR_METATYPE_FILENAME,
365                                            EXTRACTOR_METAFORMAT_UTF8, "text/plain",
366                                            ds->pos->short_filename, 
367                                            strlen (ds->pos->short_filename) + 1);
368       }
369       ds->pos = advance (ds->pos);      
370       return;
371     }
372   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED:
373     if (NULL != ds->pos)
374     {
375       GNUNET_break (0);
376       break;
377     }
378     if (0 != left)
379     {
380       GNUNET_break (0);
381       break;
382     }   
383     if (NULL == ds->toplevel)
384     {
385       GNUNET_break (0);
386       break;
387     }
388     ds->stop_task = GNUNET_SCHEDULER_add_now (&finish_scan,
389                                               ds);
390     return;
391   default:
392     GNUNET_break (0);
393     break;
394   }
395   ds->progress_callback (ds->progress_callback_cls, 
396                          NULL, GNUNET_SYSERR,
397                          GNUNET_FS_DIRSCANNER_INTERNAL_ERROR);
398 }
399
400
401 /**
402  * Start a directory scanner thread.
403  *
404  * @param filename name of the directory to scan
405  * @param GNUNET_YES to not to run libextractor on files (only build a tree)
406  * @param ex if not NULL, must be a list of extra plugins for extractor
407  * @param cb the callback to call when there are scanning progress messages
408  * @param cb_cls closure for 'cb'
409  * @return directory scanner object to be used for controlling the scanner
410  */
411 struct GNUNET_FS_DirScanner *
412 GNUNET_FS_directory_scan_start (const char *filename,
413                                 int disable_extractor, const char *ex,
414                                 GNUNET_FS_DirScannerProgressCallback cb, 
415                                 void *cb_cls)
416 {
417   struct stat sbuf;
418   char *filename_expanded;
419   struct GNUNET_FS_DirScanner *ds;
420
421   if (0 != STAT (filename, &sbuf))
422     return NULL;
423   filename_expanded = GNUNET_STRINGS_filename_expand (filename);
424   if (NULL == filename_expanded)
425     return NULL;
426   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427               "Starting to scan directory `%s'\n",
428               filename_expanded);
429   ds = GNUNET_malloc (sizeof (struct GNUNET_FS_DirScanner));
430   ds->progress_callback = cb;
431   ds->progress_callback_cls = cb_cls;
432   ds->filename_expanded = filename_expanded;
433   if (disable_extractor)  
434     ds->ex_arg = GNUNET_strdup ("-");
435   else 
436     ds->ex_arg = (NULL != ex) ? GNUNET_strdup (ex) : NULL;
437   ds->args[0] = "gnunet-helper-fs-publish";
438   ds->args[1] = ds->filename_expanded;
439   ds->args[2] = ds->ex_arg;
440   ds->args[3] = NULL;
441   ds->helper = GNUNET_HELPER_start ("gnunet-helper-fs-publish",
442                                     ds->args,
443                                     &process_helper_msgs,
444                                     ds);
445   if (NULL == ds->helper)
446   {
447     GNUNET_free (filename_expanded);
448     GNUNET_free (ds);
449     return NULL;
450   }
451   return ds;
452 }
453
454
455 /* end of fs_dirmetascan.c */