5ffa69229e45c8cb71e888417c42ea5837660ca0
[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    * Arguments for helper.
82    */
83   char *args[4];
84
85 };
86
87
88
89 /**
90  * Abort the scan.
91  *
92  * @param ds directory scanner structure
93  */
94 void
95 GNUNET_FS_directory_scan_abort (struct GNUNET_FS_DirScanner *ds)
96 {
97   /* terminate helper */
98   if (NULL != ds->helper)
99     GNUNET_HELPER_stop (ds->helper);
100   
101   /* free resources */
102   if (NULL != ds->toplevel)
103     GNUNET_FS_share_tree_free (ds->toplevel);
104   GNUNET_free_non_null (ds->ex_arg);
105   GNUNET_free (ds->filename_expanded);
106   GNUNET_free (ds);
107 }
108
109
110 /**
111  * Obtain the result of the scan after the scan has signalled
112  * completion.  Must not be called prior to completion.  The 'ds' is
113  * freed as part of this call.
114  *
115  * @param ds directory scanner structure
116  * @return the results of the scan (a directory tree)
117  */
118 struct GNUNET_FS_ShareTreeItem *
119 GNUNET_FS_directory_scan_get_result (struct GNUNET_FS_DirScanner *ds)
120 {
121   struct GNUNET_FS_ShareTreeItem *result;
122
123   /* check that we're actually done */
124   GNUNET_assert (NULL == ds->helper);
125   /* preserve result */
126   result = ds->toplevel;
127   ds->toplevel = NULL; 
128   GNUNET_FS_directory_scan_abort (ds);
129   return result;
130 }
131
132
133 /**
134  * Move in the directory from the given position to the next file
135  * in DFS traversal.
136  *
137  * @param pos current position
138  * @return next file, NULL for none
139  */
140 static struct GNUNET_FS_ShareTreeItem *
141 advance (struct GNUNET_FS_ShareTreeItem *pos)
142 {
143   int moved;
144   
145   GNUNET_assert (NULL != pos);
146   moved = 0; /* must not terminate, even on file, otherwise "normal" */
147   while ( (pos->is_directory) ||
148           (0 == moved) )
149   {
150     if ( (moved != -1) &&
151          (NULL != pos->children_head) )
152     {
153       pos = pos->children_head;
154       moved = 1; /* can terminate if file */
155       continue;
156     }
157     if (NULL != pos->next)
158     {
159       pos = pos->next;
160       moved = 1; /* can terminate if file */
161       continue;
162     }
163     if (NULL != pos->parent)
164     {
165       pos = pos->parent;
166       moved = -1; /* force move to 'next' or 'parent' */
167       continue;
168     }
169     /* no more options, end of traversal */
170     return NULL;
171   }
172   return pos;
173 }
174
175
176 /**
177  * Add another child node to the tree.
178  *
179  * @param parent parent of the child, NULL for top level
180  * @param filename name of the file or directory
181  * @param is_directory GNUNET_YES for directories
182  * @return new entry that was just created
183  */
184 static struct GNUNET_FS_ShareTreeItem *
185 expand_tree (struct GNUNET_FS_ShareTreeItem *parent,
186              const char *filename,
187              int is_directory)
188 {
189   struct GNUNET_FS_ShareTreeItem *chld;
190
191   chld = GNUNET_malloc (sizeof (struct GNUNET_FS_ShareTreeItem));
192   chld->parent = parent;
193   chld->filename = GNUNET_strdup (filename);
194   chld->short_filename = GNUNET_strdup (GNUNET_STRINGS_get_short_name (filename));
195   chld->is_directory = is_directory;
196   if (NULL != parent)
197       GNUNET_CONTAINER_DLL_insert (parent->children_head,
198                                    parent->children_tail,
199                                    chld);  
200   return chld;
201 }
202
203
204 /**
205  * Called every time there is data to read from the scanner.
206  * Calls the scanner progress handler.
207  *
208  * @param cls the closure (directory scanner object)
209  * @param client always NULL
210  * @param msg message from the helper process
211  */
212 static void
213 process_helper_msgs (void *cls, 
214                      void *client,
215                      const struct GNUNET_MessageHeader *msg)
216 {
217   struct GNUNET_FS_DirScanner *ds = cls;
218   const char *filename;
219   size_t left;
220
221   left = ntohs (msg->size) - sizeof (struct GNUNET_MessageHeader);
222   filename = (const char*) &msg[1];
223   switch (ntohs (msg->type))
224   {
225   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_FILE:
226     if (filename[left-1] != '\0')
227     {
228       GNUNET_break (0);
229       break;
230     }
231     ds->progress_callback (ds->progress_callback_cls, 
232                            filename, GNUNET_NO,
233                            GNUNET_FS_DIRSCANNER_FILE_START);
234     expand_tree (ds->pos,
235                  filename, GNUNET_NO);
236     return;
237   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_PROGRESS_DIRECTORY:
238     if (filename[left-1] != '\0')
239     {
240       GNUNET_break (0);
241       break;
242     }
243     if (0 == strcmp ("..", filename))
244     {
245       if (NULL == ds->pos)
246       {
247         GNUNET_break (0);
248         break;
249       }
250       ds->pos = ds->pos->parent;
251       return;
252     }
253     ds->progress_callback (ds->progress_callback_cls, 
254                            filename, GNUNET_YES,
255                            GNUNET_FS_DIRSCANNER_FILE_START);
256     ds->pos = expand_tree (ds->pos,
257                            filename, GNUNET_YES);
258     if (NULL == ds->toplevel)
259       ds->toplevel = ds->pos;
260     return;
261   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_ERROR:
262     break;
263   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_SKIP_FILE:
264     if (filename[left-1] != '\0')
265       break;
266     ds->progress_callback (ds->progress_callback_cls, 
267                            filename, GNUNET_SYSERR,
268                            GNUNET_FS_DIRSCANNER_FILE_IGNORED);
269     return;
270   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_COUNTING_DONE:
271     if (0 != left)
272     {
273       GNUNET_break (0);
274       break;
275     }
276     ds->progress_callback (ds->progress_callback_cls, 
277                            NULL, GNUNET_SYSERR,
278                            GNUNET_FS_DIRSCANNER_ALL_COUNTED);
279     ds->pos = ds->toplevel;
280     if (ds->pos->is_directory)
281       ds->pos = advance (ds->pos);
282     return;
283   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_META_DATA:
284     {
285       size_t nlen;
286       const char *end;
287       
288       if (NULL == ds->pos)
289       {
290         GNUNET_break (0);
291         break;
292       }
293       end = memchr (filename, 0, left);
294       if (NULL == end)
295       {
296         GNUNET_break (0);
297         break;
298       }
299       end++;
300       nlen = end - filename;
301       left -= nlen;
302       if (0 != strcmp (filename,
303                        ds->pos->filename))
304       {
305         GNUNET_break (0);
306         break;
307       }
308       ds->progress_callback (ds->progress_callback_cls, 
309                              filename, GNUNET_YES,
310                              GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED);
311       if (0 < left)
312       {
313         ds->pos->meta = GNUNET_CONTAINER_meta_data_deserialize (end, left);
314         if (NULL == ds->pos->meta)
315         {
316           GNUNET_break (0);
317           break;
318         }
319         /* having full filenames is too dangerous; always make sure we clean them up */
320         GNUNET_CONTAINER_meta_data_delete (ds->pos->meta, 
321                                            EXTRACTOR_METATYPE_FILENAME,
322                                            NULL, 0);
323         GNUNET_CONTAINER_meta_data_insert (ds->pos->meta, "<libgnunetfs>",
324                                            EXTRACTOR_METATYPE_FILENAME,
325                                            EXTRACTOR_METAFORMAT_UTF8, "text/plain",
326                                            ds->pos->short_filename, 
327                                            strlen (ds->pos->short_filename) + 1);
328       }
329       ds->pos = advance (ds->pos);      
330       return;
331     }
332   case GNUNET_MESSAGE_TYPE_FS_PUBLISH_HELPER_FINISHED:
333     if (NULL != ds->pos)
334     {
335       GNUNET_break (0);
336       break;
337     }
338     if (0 != left)
339     {
340       GNUNET_break (0);
341       break;
342     }
343     GNUNET_HELPER_stop (ds->helper);
344     ds->helper = NULL;
345     ds->progress_callback (ds->progress_callback_cls, 
346                            NULL, GNUNET_SYSERR,
347                            GNUNET_FS_DIRSCANNER_FINISHED);    
348     return;
349   default:
350     GNUNET_break (0);
351     break;
352   }
353   ds->progress_callback (ds->progress_callback_cls, 
354                          NULL, GNUNET_SYSERR,
355                          GNUNET_FS_DIRSCANNER_INTERNAL_ERROR);
356
357 }
358
359
360 /**
361  * Start a directory scanner thread.
362  *
363  * @param filename name of the directory to scan
364  * @param GNUNET_YES to not to run libextractor on files (only build a tree)
365  * @param ex if not NULL, must be a list of extra plugins for extractor
366  * @param cb the callback to call when there are scanning progress messages
367  * @param cb_cls closure for 'cb'
368  * @return directory scanner object to be used for controlling the scanner
369  */
370 struct GNUNET_FS_DirScanner *
371 GNUNET_FS_directory_scan_start (const char *filename,
372                                 int disable_extractor, const char *ex,
373                                 GNUNET_FS_DirScannerProgressCallback cb, 
374                                 void *cb_cls)
375 {
376   struct stat sbuf;
377   char *filename_expanded;
378   struct GNUNET_FS_DirScanner *ds;
379
380   if (0 != STAT (filename, &sbuf))
381     return NULL;
382   filename_expanded = GNUNET_STRINGS_filename_expand (filename);
383   if (NULL == filename_expanded)
384     return NULL;
385   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386               "Starting to scan directory `%s'\n",
387               filename_expanded);
388   ds = GNUNET_malloc (sizeof (struct GNUNET_FS_DirScanner));
389   ds->progress_callback = cb;
390   ds->progress_callback_cls = cb_cls;
391   ds->filename_expanded = filename_expanded;
392   if (disable_extractor)  
393     ds->ex_arg = GNUNET_strdup ("-");
394   else 
395     ds->ex_arg = (NULL != ex) ? GNUNET_strdup (ex) : NULL;
396   ds->args[0] = "gnunet-helper-fs-publish";
397   ds->args[1] = ds->filename_expanded;
398   ds->args[2] = ds->ex_arg;
399   ds->args[3] = NULL;
400   ds->helper = GNUNET_HELPER_start ("gnunet-helper-fs-publish",
401                                     ds->args,
402                                     &process_helper_msgs,
403                                     ds);
404   if (NULL == ds->helper)
405   {
406     GNUNET_free (filename_expanded);
407     GNUNET_free (ds);
408     return NULL;
409   }
410   return ds;
411 }
412
413
414 /* end of fs_dirmetascan.c */