pndnotifyd: fix some crashes
[pandora-libraries.git] / lib / pnd_discovery.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #include <unistd.h> /* for unlink */
5 #include <limits.h> /* for PATH_MAX */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8
9 #define __USE_GNU /* for strcasestr */
10 #include <string.h> /* for making ftw.h happy */
11
12 #define _XOPEN_SOURCE 500
13 #define __USE_XOPEN_EXTENDED
14 #define _GNU_SOURCE
15 #include <ftw.h> /* for nftw, tree walker */
16
17 #include "pnd_container.h"
18 #include "pnd_pxml.h"
19 #include "pnd_discovery.h"
20 #include "pnd_pathiter.h"
21 #include "pnd_apps.h"
22 #include "pnd_pndfiles.h"
23 #include "pnd_logger.h"
24 #include "pnd_conf.h"
25
26 // need these 'globals' due to the way nftw and ftw work :/
27 static pnd_box_handle disco_box;
28 static char *disco_overrides = NULL;
29
30 void pnd_disco_destroy ( pnd_disco_t *p ) {
31   if ( p -> package_id ) {     free ( p -> package_id);   }
32   if ( p -> title_en ) {       free ( p -> title_en );    }
33   if ( p -> desc_en ) {        free ( p -> desc_en );    }
34   if ( p -> unique_id ) {      free ( p -> unique_id );   }
35   if ( p -> appdata_dirname ) { free ( p -> appdata_dirname );   }
36   if ( p -> icon )     {       free ( p -> icon );        }
37   if ( p -> exec )     {       free ( p -> exec );        }
38   if ( p -> execargs ) {       free ( p -> execargs );    }
39   if ( p -> clockspeed ) {     free ( p -> clockspeed );  }
40   if ( p -> startdir ) {       free ( p -> startdir );    }
41   if ( p -> option_no_x11 ) {  free ( p -> option_no_x11 );  }
42   if ( p -> main_category ) {  free ( p -> main_category );  }
43   if ( p -> main_category1 ) { free ( p -> main_category1 ); }
44   if ( p -> main_category2 ) { free ( p -> main_category2 ); }
45   if ( p -> alt_category ) {   free ( p -> alt_category );   }
46   if ( p -> alt_category1 ) {  free ( p -> alt_category1 );  }
47   if ( p -> alt_category2 ) {  free ( p -> alt_category2 );  }
48   if ( p -> object_filename ) { free ( p -> object_filename ); }
49   if ( p -> object_path )     { free ( p -> object_path ); }
50   if ( p -> mkdir_sp )      {  free ( p -> mkdir_sp );       }
51   if ( p -> info_name )     {  free ( p -> info_name );       }
52   if ( p -> info_type )     {  free ( p -> info_type );       }
53   if ( p -> info_filename ) {  free ( p -> info_filename );       }
54   if ( p -> preview_pic1 )  {  free ( p -> preview_pic1 );     }
55   if ( p -> preview_pic2 )  {  free ( p -> preview_pic2 );     }
56   if ( p -> version_major ) {  free ( p -> version_major );    }
57   if ( p -> version_minor ) {  free ( p -> version_minor );    }
58   if ( p -> version_release ) {free ( p -> version_release );  }
59   if ( p -> version_build ) {  free ( p -> version_build );    }
60   if ( p -> package_version_major ) { free ( p -> package_version_major ); }
61   if ( p -> package_version_minor ) { free ( p -> package_version_minor ); }
62   if ( p -> package_version_release ) { free ( p -> package_version_release ); }
63   if ( p -> package_version_build ) { free ( p -> package_version_build ); }
64   if ( p -> associationitem1_name ) { free ( p -> associationitem1_name ); }
65   if ( p -> associationitem1_filetype ) { free ( p -> associationitem1_filetype ); }
66   if ( p -> exec_dashdash_args ) { free ( p -> exec_dashdash_args ); }
67
68   return;
69 }
70
71 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
72                                 int typeflag, struct FTW *ftwbuf )
73 {
74   unsigned char valid = pnd_object_type_unknown;
75   pnd_pxml_handle pxmlh = 0;
76   pnd_pxml_handle *pxmlapps = NULL;
77   pnd_pxml_handle *pxmlappiter;
78   off_t pxml_close_pos = 0;
79   unsigned char logit = pnd_log_do_buried_logging();
80
81   if ( logit ) {
82     pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
83   }
84
85   // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
86   if ( typeflag & FTW_D ) {
87     if ( logit ) {
88       pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
89     }
90     if ( ftwbuf -> level >= pathiter_depthlimit ) {
91       return ( FTW_SKIP_SUBTREE );
92     }
93     return ( FTW_CONTINUE ); // skip directories and other non-regular files
94   }
95
96   // PND/PNZ file and others may be valid as well .. but lets leave that for now
97   //   printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
98   if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
99     valid = pnd_object_type_directory;
100   } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
101     valid = pnd_object_type_pnd;
102   }
103
104   // if not a file of interest, just keep looking until we run out
105   if ( ! valid ) {
106     if ( logit ) {
107       pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
108     }
109     return ( FTW_CONTINUE );
110   }
111
112   // potentially a valid application
113   if ( valid == pnd_object_type_directory ) {
114     // Plaintext PXML file
115     //printf ( "PXML: disco callback encountered '%s'\n", fpath );
116
117     // pick up the PXML if we can
118     pxmlapps = pnd_pxml_fetch ( (char*) fpath );
119
120   } else if ( valid == pnd_object_type_pnd ) {
121     // PND ... ??
122     FILE *f;
123     char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
124
125     //printf ( "PND: disco callback encountered '%s'\n", fpath );
126
127     // is this a valid .pnd file? The filename is a candidate already, but verify..
128     // .. presence of PXML appended, or at least contained within?
129     // .. presence of an icon appended after PXML?
130
131     // open it up..
132     f = fopen ( fpath, "r" );
133
134     // try to locate the PXML portion
135     if ( ! pnd_pnd_seek_pxml ( f ) ) {
136       fclose ( f );
137       return ( FTW_CONTINUE ); // pnd or not, but not to spec. Pwn'd the pnd?
138     }
139
140     // accrue it into a buffer
141     if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
142       fclose ( f );
143       return ( FTW_CONTINUE );
144     }
145
146     //printf ( "buffer is %s\n", pxmlbuf );
147     //fflush ( stdout );
148
149 #if 1 // icon
150     // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
151     {
152       char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
153       pngbuffer [ 0 ] = 137;      pngbuffer [ 1 ] = 80;      pngbuffer [ 2 ] = 78;      pngbuffer [ 3 ] = 71;
154       pngbuffer [ 4 ] = 13;       pngbuffer [ 5 ] = 10;       pngbuffer [ 6 ] = 26;      pngbuffer [ 7 ] = 10;
155
156       unsigned char padtests = 20;
157       off_t padstart = ftello ( f );
158
159       // seek back 10 (should be back into the /PXML> part) to catch any appending-icon-no-line-endings funny business
160       fseek ( f, -10, SEEK_CUR );
161
162       while ( padtests ) {
163
164         if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
165           if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
166             pxml_close_pos = ftello ( f ) - 8;
167             break;
168           } // if
169           fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
170         } // if read
171
172         padtests --;
173       } // while
174
175       if ( ! padtests ) {
176         // no icon found, so back to where we started looking
177         fseeko ( f, padstart, SEEK_SET );
178       }
179
180     } // icon
181 #endif
182
183     // by now, we have <PXML> .. </PXML>, try to parse..
184     pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
185
186     // done with file
187     fclose ( f );
188
189   }
190
191   // pxmlh is useful?
192   if ( ! pxmlapps ) {
193     return ( FTW_CONTINUE ); // continue tree walk
194   }
195
196   // for ovr-file
197   char ovrfile [ PATH_MAX ];
198   pnd_box_handle ovrh = 0; // 0 didn't try, -1 tried and failed, >0 tried and got
199
200   // iterate across apps in the PXML
201   pxmlappiter = pxmlapps;
202   while ( 1 ) {
203     pxmlh = *pxmlappiter;
204     pxmlappiter++;
205
206     if ( ! pxmlh ) {
207       break; // all done
208     }
209
210     // check for validity and add to resultset if it looks executable
211     if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
212       pnd_disco_t *p;
213       char *fixpxml;
214       char *z;
215
216       //pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
217
218       p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
219
220       // base paths
221       p -> object_path = strdup ( fpath );
222
223       if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
224         *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
225       } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
226         *(fixpxml+1) = '\0'; // for pnd, lop off to last /
227       }
228
229       if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
230         p -> object_filename = strdup ( fixpxml + 1 );
231       }
232
233       // subapp-number
234       p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
235
236       // png icon path
237       p -> pnd_icon_pos64 = pxml_close_pos;
238       p -> pnd_icon_pos = pxml_close_pos;
239       if ( (off_t) p -> pnd_icon_pos != pxml_close_pos ) {
240         // pnd_icon_pos is an int and the offset doesn't fit, bad luck...
241         p -> pnd_icon_pos = 0;
242       }
243
244       // type
245       p -> object_type = valid;
246
247       // PXML fields
248       if ( pnd_pxml_get_package_id ( pxmlh ) ) {
249         p -> package_id = strdup ( pnd_pxml_get_package_id ( pxmlh ) );
250       }
251       char *name_en = pnd_pxml_get_app_name_en ( pxmlh );
252       if (name_en) {
253         p -> title_en = name_en; /* already strdupped */
254       }
255       char *desc_en = pnd_pxml_get_description_en ( pxmlh );
256       if ( desc_en ) {
257         p -> desc_en = desc_en; /* already strdupped */
258       }
259       if ( pnd_pxml_get_icon ( pxmlh ) ) {
260         p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
261       }
262       if ( pnd_pxml_get_exec ( pxmlh ) ) {
263         p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
264       }
265       if ( pnd_pxml_get_execargs ( pxmlh ) ) {
266         p -> execargs = strdup ( pnd_pxml_get_execargs ( pxmlh ) );
267       }
268       if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
269         p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
270       }
271       if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
272         p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
273       }
274       if ( pnd_pxml_get_appdata_dirname ( pxmlh ) ) {
275         p -> appdata_dirname = strdup ( pnd_pxml_get_appdata_dirname ( pxmlh ) );
276       }
277       if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
278         p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
279       }
280       if ( pnd_pxml_get_startdir ( pxmlh ) ) {
281         p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
282       }
283       // category kruft
284       if ( pnd_pxml_get_main_category ( pxmlh ) ) {
285         p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
286       }
287       if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
288         p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
289       }
290       if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
291         p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
292       }
293       if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
294         p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
295       }
296       if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
297         p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
298       }
299       if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
300         p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
301       }
302       // preview pics
303       if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
304         p -> preview_pic1 = strdup ( z );
305       }
306       if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
307         p -> preview_pic2 = strdup ( z );
308       }
309       // mkdirs
310       if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
311         p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
312       }
313       // info
314       if ( pnd_pxml_get_info_src ( pxmlh ) ) {
315         p -> info_filename = strdup ( pnd_pxml_get_info_src ( pxmlh ) );
316       }
317       if ( pnd_pxml_get_info_name ( pxmlh ) ) {
318         p -> info_name = strdup ( pnd_pxml_get_info_name ( pxmlh ) );
319       }
320       if ( pnd_pxml_get_info_type ( pxmlh ) ) {
321         p -> info_type = strdup ( pnd_pxml_get_info_type ( pxmlh ) );
322       }
323       if ( pnd_pxml_get_version_major ( pxmlh ) ) {
324    p -> version_major = strdup ( pnd_pxml_get_version_major ( pxmlh ) );
325       }
326       if ( pnd_pxml_get_version_minor ( pxmlh ) ) {
327    p -> version_minor = strdup ( pnd_pxml_get_version_minor ( pxmlh ) );
328       }
329       if ( pnd_pxml_get_version_release ( pxmlh ) ) {
330    p -> version_release = strdup ( pnd_pxml_get_version_release ( pxmlh ) );
331       }
332       if ( pnd_pxml_get_version_build ( pxmlh ) ) {
333    p -> version_build = strdup ( pnd_pxml_get_version_build ( pxmlh ) );
334       }
335       if ( pnd_pxml_get_package_version_major ( pxmlh ) ) {
336         p -> package_version_major = strdup ( pnd_pxml_get_package_version_major ( pxmlh ) );
337       }
338       if ( pnd_pxml_get_package_version_minor ( pxmlh ) ) {
339    p -> package_version_minor = strdup ( pnd_pxml_get_package_version_minor ( pxmlh ) );
340       }
341       if ( pnd_pxml_get_package_version_release ( pxmlh ) ) {
342    p -> package_version_release = strdup ( pnd_pxml_get_package_version_release ( pxmlh ) );
343       }
344       if ( pnd_pxml_get_package_version_build ( pxmlh ) ) {
345    p -> package_version_build = strdup ( pnd_pxml_get_package_version_build ( pxmlh ) );
346       }
347 #if 1
348       // file associations
349       if ( pnd_pxml_get_associationitem1_name ( pxmlh ) ) {
350         p -> associationitem1_name = strdup ( pnd_pxml_get_associationitem1_name ( pxmlh ) );
351         p -> associationitem1_filetype = strdup ( pnd_pxml_get_associationitem1_filetype ( pxmlh ) );
352         //pnd_log ( PND_LOG_DEFAULT, "  Disco: Found file association request in PXML (%s)\n", p -> title_en );
353       }
354 #endif
355       if ( pnd_pxml_get_execdashdashargs ( pxmlh ) ) {
356         p -> exec_dashdash_args = strdup ( pnd_pxml_get_execdashdashargs ( pxmlh ) );
357       }
358
359       // look for any PXML overrides, if requested
360       if ( disco_overrides ) {
361         pnd_pxml_merge_override ( pxmlh, disco_overrides );
362       }
363
364       // handle ovr overrides
365       // try to load a same-path-as-pnd override file
366       if ( ovrh == 0 ) {
367         sprintf ( ovrfile, "%s/%s", p -> object_path, p -> object_filename );
368         fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
369         if ( fixpxml ) {
370           strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
371           struct stat statbuf;
372           if ( stat ( ovrfile, &statbuf ) == 0 ) {
373             ovrh = pnd_conf_fetch_by_path ( ovrfile );
374
375             if ( ! ovrh ) {
376               // couldn't pull conf out of file, so don't try again
377               ovrh = (void*)(-1);
378             }
379
380           } else {
381             ovrh = (void*)(-1); // not found, don't try again
382           } // stat
383         } // can find .pnd
384       } // tried ovr yet?
385
386       // is ovr file open?
387       if ( ovrh != 0 && ovrh != (void*)(-1) ) {
388         // pull in appropriate values
389         char key [ 100 ];
390         char *v;
391
392         // set the flag regardless, so its for all subapps
393         p -> object_flags |= PND_DISCO_FLAG_OVR;
394
395         // title
396         snprintf ( key, 100, "Application-%u.title", p -> subapp_number );
397         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
398           if ( p -> title_en ) {
399             free ( p -> title_en );
400           }
401           p -> title_en = strdup ( v );
402         }
403
404         // clockspeed
405         snprintf ( key, 100, "Application-%u.clockspeed", p -> subapp_number );
406         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
407           if ( p -> clockspeed ) {
408             free ( p -> clockspeed );
409           }
410           p -> clockspeed = strdup ( v );
411         }
412
413         // appdata dirname
414         snprintf ( key, 100, "Application-%u.appdata", p -> subapp_number );
415         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
416           if ( p -> appdata_dirname ) {
417             free ( p -> appdata_dirname );
418           }
419           p -> appdata_dirname = strdup ( v );
420         }
421
422         // categories
423         snprintf ( key, 100, "Application-%u.maincategory", p -> subapp_number );
424         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
425           if ( p -> main_category ) {
426             free ( p -> main_category );
427           }
428           // the override file cannot suppress the main category
429           p -> main_category = strdup ( v );
430         }
431         snprintf ( key, 100, "Application-%u.maincategorysub1", p -> subapp_number );
432         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
433           if ( p -> main_category1 ) {
434             free ( p -> main_category1 );
435             p -> main_category1 = NULL;
436           }
437           if ( strcasecmp ( v, "NoSubcategory" ) != 0 ) {
438             p -> main_category1 = strdup ( v );
439           }
440         }
441         snprintf ( key, 100, "Application-%u.maincategorysub2", p -> subapp_number );
442         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
443           if ( p -> main_category2 ) {
444             free ( p -> main_category2 );
445             p -> main_category2 = NULL;
446           }
447           if ( strcasecmp ( v, "NoSubcategory" ) != 0 ) {
448             p -> main_category2 = strdup ( v );
449           }
450         }
451         // alt categories
452         snprintf ( key, 100, "Application-%u.altcategory", p -> subapp_number );
453         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
454           if ( p -> alt_category ) {
455             free ( p -> alt_category );
456             p -> alt_category = NULL;
457           }
458           // but it makes sense to allow full suppression of the alternate category
459           if ( strcasecmp ( v, "NoCategory" ) != 0 ) {
460             p -> alt_category = strdup ( v );
461           }
462         }
463         snprintf ( key, 100, "Application-%u.altcategorysub1", p -> subapp_number );
464         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
465           if ( p -> alt_category1 ) {
466             free ( p -> alt_category1 );
467             p -> alt_category1 = NULL;
468           }
469           if ( strcasecmp ( v, "NoSubcategory" ) != 0 ) {
470             p -> alt_category1 = strdup ( v );
471           }
472         }
473         snprintf ( key, 100, "Application-%u.altcategorysub2", p -> subapp_number );
474         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
475           if ( p -> alt_category2 ) {
476             free ( p -> alt_category2 );
477             p -> alt_category2 = NULL;
478           }
479           if ( strcasecmp ( v, "NoSubcategory" ) != 0 ) {
480             p -> alt_category2 = strdup ( v );
481           }
482         }
483
484       } // got ovr conf loaded?
485
486     } else {
487       //printf ( "Invalid PXML; skipping.\n" );
488     }
489
490     // ditch pxml
491     pnd_pxml_delete ( pxmlh );
492
493   } // while pxmlh is good
494
495   // free up ovr
496   if ( ovrh != 0 && ovrh != (void*)(-1) ) {
497     pnd_box_delete ( ovrh );
498   }
499
500   // free up the applist
501   free ( pxmlapps );
502
503   return ( FTW_CONTINUE ); // continue the tree walk
504 }
505
506 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
507
508   //printf ( "Searchpath to discover: '%s'\n", searchpath );
509
510   // alloc a container for the result set
511   disco_box = pnd_box_new ( "discovery" );
512   disco_overrides = overridespath;
513
514   /* iterate across the paths within the searchpath, attempting to locate applications
515    */
516
517   SEARCHPATH_PRE
518   {
519
520     // invoke the dir walking function; thankfully Linux includes a pretty good one
521     nftw ( buffer,               // path to descend
522            pnd_disco_callback,   // callback to do processing
523            10,                   // no more than X open fd's at once
524            FTW_PHYS | FTW_ACTIONRETVAL );   // do not follow symlinks
525
526   }
527   SEARCHPATH_POST
528
529   // return whatever we found, or NULL if nada
530   if ( ! pnd_box_get_head ( disco_box ) ) {
531     pnd_box_delete ( disco_box );
532     disco_box = NULL;
533   }
534
535   return ( disco_box );
536 }
537
538 pnd_box_handle pnd_disco_file ( char *path, char *filename ) {
539   struct stat statbuf;
540
541   // set up container
542   disco_overrides = NULL;
543   disco_box = pnd_box_new ( "discovery" );
544
545   // path
546   char fullpath [ PATH_MAX ];
547   sprintf ( fullpath, "%s/%s", path, filename );
548
549   // fake it
550   if ( stat ( fullpath, &statbuf ) < 0 ) {
551     return ( 0 );
552   }
553
554   struct FTW ftw;
555   ftw.base = strlen ( path );
556   ftw.level = 0;
557
558   pnd_disco_callback ( fullpath, &statbuf, FTW_F, &ftw );
559
560   // return whatever we found, or NULL if nada
561   if ( ! pnd_box_get_head ( disco_box ) ) {
562     pnd_box_delete ( disco_box );
563     disco_box = NULL;
564   }
565
566   return ( disco_box );
567 }