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 */
9 #define __USE_GNU /* for strcasestr */
10 #include <string.h> /* for making ftw.h happy */
12 #define _XOPEN_SOURCE 500
13 #define __USE_XOPEN_EXTENDED
15 #include <ftw.h> /* for nftw, tree walker */
17 #include "pnd_container.h"
19 #include "pnd_discovery.h"
20 #include "pnd_pathiter.h"
22 #include "pnd_pndfiles.h"
23 #include "pnd_logger.h"
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;
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 ); }
59 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
60 int typeflag, struct FTW *ftwbuf )
62 unsigned char valid = pnd_object_type_unknown;
63 pnd_pxml_handle pxmlh = 0;
64 pnd_pxml_handle *pxmlapps = NULL;
65 pnd_pxml_handle *pxmlappiter;
66 unsigned int pxml_close_pos = 0;
67 unsigned char logit = pnd_log_do_buried_logging();
70 pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
73 // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
74 if ( typeflag & FTW_D ) {
76 pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
78 if ( ftwbuf -> level >= pathiter_depthlimit ) {
79 return ( FTW_SKIP_SUBTREE );
81 return ( FTW_CONTINUE ); // skip directories and other non-regular files
84 // PND/PNZ file and others may be valid as well .. but lets leave that for now
85 // printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
86 if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
87 valid = pnd_object_type_directory;
88 } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
89 valid = pnd_object_type_pnd;
92 // if not a file of interest, just keep looking until we run out
95 pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
97 return ( FTW_CONTINUE );
100 // potentially a valid application
101 if ( valid == pnd_object_type_directory ) {
102 // Plaintext PXML file
103 //printf ( "PXML: disco callback encountered '%s'\n", fpath );
105 // pick up the PXML if we can
106 pxmlapps = pnd_pxml_fetch ( (char*) fpath );
108 } else if ( valid == pnd_object_type_pnd ) {
111 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
113 //printf ( "PND: disco callback encountered '%s'\n", fpath );
115 // is this a valid .pnd file? The filename is a candidate already, but verify..
116 // .. presence of PXML appended, or at least contained within?
117 // .. presence of an icon appended after PXML?
120 f = fopen ( fpath, "r" );
122 // try to locate the PXML portion
123 if ( ! pnd_pnd_seek_pxml ( f ) ) {
125 return ( FTW_CONTINUE ); // pnd or not, but not to spec. Pwn'd the pnd?
128 // accrue it into a buffer
129 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
131 return ( FTW_CONTINUE );
134 //printf ( "buffer is %s\n", pxmlbuf );
138 // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
140 char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
141 pngbuffer [ 0 ] = 137; pngbuffer [ 1 ] = 80; pngbuffer [ 2 ] = 78; pngbuffer [ 3 ] = 71;
142 pngbuffer [ 4 ] = 13; pngbuffer [ 5 ] = 10; pngbuffer [ 6 ] = 26; pngbuffer [ 7 ] = 10;
144 unsigned char padtests = 20;
145 unsigned int padstart = ftell ( f );
147 // seek back 10 (should be back into the /PXML> part) to catch any appending-icon-no-line-endings funny business
148 fseek ( f, -10, SEEK_CUR );
152 if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
153 if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
154 pxml_close_pos = ftell ( f ) - 8;
157 fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
164 // no icon found, so back to where we started looking
165 fseek ( f, padstart, SEEK_SET );
171 // by now, we have <PXML> .. </PXML>, try to parse..
172 pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
181 return ( FTW_CONTINUE ); // continue tree walk
185 char ovrfile [ PATH_MAX ];
186 pnd_box_handle ovrh = 0; // 0 didn't try, -1 tried and failed, >0 tried and got
188 // iterate across apps in the PXML
189 pxmlappiter = pxmlapps;
191 pxmlh = *pxmlappiter;
198 // check for validity and add to resultset if it looks executable
199 if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
204 //pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
206 p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
209 p -> object_path = strdup ( fpath );
211 if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
212 *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
213 } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
214 *(fixpxml+1) = '\0'; // for pnd, lop off to last /
217 if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
218 p -> object_filename = strdup ( fixpxml + 1 );
222 p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
225 p -> pnd_icon_pos = pxml_close_pos;
228 p -> object_type = valid;
231 if ( pnd_pxml_get_package_id ( pxmlh ) ) {
232 p -> package_id = strdup ( pnd_pxml_get_package_id ( pxmlh ) );
234 char *name_en = pnd_pxml_get_app_name_en ( pxmlh );
236 p -> title_en = name_en; /* already strdupped */
238 char *desc_en = pnd_pxml_get_description_en ( pxmlh );
240 p -> desc_en = desc_en; /* already strdupped */
242 if ( pnd_pxml_get_icon ( pxmlh ) ) {
243 p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
245 if ( pnd_pxml_get_exec ( pxmlh ) ) {
246 p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
248 if ( pnd_pxml_get_execargs ( pxmlh ) ) {
249 p -> execargs = strdup ( pnd_pxml_get_execargs ( pxmlh ) );
251 if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
252 p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
254 if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
255 p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
257 if ( pnd_pxml_get_appdata_dirname ( pxmlh ) ) {
258 p -> appdata_dirname = strdup ( pnd_pxml_get_appdata_dirname ( pxmlh ) );
260 if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
261 p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
263 if ( pnd_pxml_get_startdir ( pxmlh ) ) {
264 p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
267 if ( pnd_pxml_get_main_category ( pxmlh ) ) {
268 p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
270 if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
271 p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
273 if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
274 p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
276 if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
277 p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
279 if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
280 p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
282 if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
283 p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
286 if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
287 p -> preview_pic1 = strdup ( z );
289 if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
290 p -> preview_pic2 = strdup ( z );
293 if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
294 p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
297 if ( pnd_pxml_get_info_src ( pxmlh ) ) {
298 p -> info_filename = strdup ( pnd_pxml_get_info_src ( pxmlh ) );
300 if ( pnd_pxml_get_info_name ( pxmlh ) ) {
301 p -> info_name = strdup ( pnd_pxml_get_info_name ( pxmlh ) );
303 if ( pnd_pxml_get_info_type ( pxmlh ) ) {
304 p -> info_type = strdup ( pnd_pxml_get_info_type ( pxmlh ) );
307 // look for any PXML overrides, if requested
308 if ( disco_overrides ) {
309 pnd_pxml_merge_override ( pxmlh, disco_overrides );
312 // handle ovr overrides
313 // try to load a same-path-as-pnd override file
315 sprintf ( ovrfile, "%s/%s", p -> object_path, p -> object_filename );
316 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
318 strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
320 if ( stat ( ovrfile, &statbuf ) == 0 ) {
321 ovrh = pnd_conf_fetch_by_path ( ovrfile );
324 // couldn't pull conf out of file, so don't try again
329 ovrh = (void*)(-1); // not found, don't try again
335 if ( ovrh != 0 && ovrh != (void*)(-1) ) {
336 // pull in appropriate values
340 // set the flag regardless, so its for all subapps
341 p -> object_flags |= PND_DISCO_FLAG_OVR;
344 snprintf ( key, 100, "Application-%u.title", p -> subapp_number );
345 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
346 if ( p -> title_en ) {
347 free ( p -> title_en );
349 p -> title_en = strdup ( v );
353 snprintf ( key, 100, "Application-%u.clockspeed", p -> subapp_number );
354 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
355 if ( p -> clockspeed ) {
356 free ( p -> clockspeed );
358 p -> clockspeed = strdup ( v );
362 snprintf ( key, 100, "Application-%u.appdata", p -> subapp_number );
363 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
364 if ( p -> appdata_dirname ) {
365 free ( p -> appdata_dirname );
367 p -> appdata_dirname = strdup ( v );
371 snprintf ( key, 100, "Application-%u.maincategory", p -> subapp_number );
372 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
373 if ( p -> main_category ) {
374 free ( p -> main_category );
376 p -> main_category = strdup ( v );
378 snprintf ( key, 100, "Application-%u.maincategorysub1", p -> subapp_number );
379 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
380 if ( p -> main_category1 ) {
381 free ( p -> main_category1 );
382 p -> main_category1 = NULL;
384 if ( strcasecmp ( v, "NoSubcategory" ) != 0 ) {
385 p -> main_category1 = strdup ( v );
389 } // got ovr conf loaded?
392 //printf ( "Invalid PXML; skipping.\n" );
396 pnd_pxml_delete ( pxmlh );
398 } // while pxmlh is good
401 if ( ovrh != 0 && ovrh != (void*)(-1) ) {
402 pnd_box_delete ( ovrh );
405 // free up the applist
408 return ( FTW_CONTINUE ); // continue the tree walk
411 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
413 //printf ( "Searchpath to discover: '%s'\n", searchpath );
415 // alloc a container for the result set
416 disco_box = pnd_box_new ( "discovery" );
417 disco_overrides = overridespath;
419 /* iterate across the paths within the searchpath, attempting to locate applications
425 // invoke the dir walking function; thankfully Linux includes a pretty good one
426 nftw ( buffer, // path to descend
427 pnd_disco_callback, // callback to do processing
428 10, // no more than X open fd's at once
429 FTW_PHYS | FTW_ACTIONRETVAL ); // do not follow symlinks
434 // return whatever we found, or NULL if nada
435 if ( ! pnd_box_get_head ( disco_box ) ) {
436 pnd_box_delete ( disco_box );
440 return ( disco_box );
443 pnd_box_handle pnd_disco_file ( char *path, char *filename ) {
447 disco_overrides = NULL;
448 disco_box = pnd_box_new ( "discovery" );
451 char fullpath [ PATH_MAX ];
452 sprintf ( fullpath, "%s/%s", path, filename );
455 if ( stat ( fullpath, &statbuf ) < 0 ) {
460 ftw.base = strlen ( path );
463 pnd_disco_callback ( fullpath, &statbuf, FTW_F, &ftw );
465 // return whatever we found, or NULL if nada
466 if ( ! pnd_box_get_head ( disco_box ) ) {
467 pnd_box_delete ( disco_box );
471 return ( disco_box );