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 ) {
32 if ( p -> title_en ) { free ( p -> title_en ); }
33 if ( p -> unique_id ) { free ( p -> unique_id ); }
34 if ( p -> appdata_dirname ) { free ( p -> appdata_dirname ); }
35 if ( p -> icon ) { free ( p -> icon ); }
36 if ( p -> exec ) { free ( p -> exec ); }
37 if ( p -> execargs ) { free ( p -> execargs ); }
38 if ( p -> clockspeed ) { free ( p -> clockspeed ); }
39 if ( p -> startdir ) { free ( p -> startdir ); }
40 if ( p -> option_no_x11 ) { free ( p -> option_no_x11 ); }
41 if ( p -> main_category ) { free ( p -> main_category ); }
42 if ( p -> main_category1 ) { free ( p -> main_category1 ); }
43 if ( p -> main_category2 ) { free ( p -> main_category2 ); }
44 if ( p -> alt_category ) { free ( p -> alt_category ); }
45 if ( p -> alt_category1 ) { free ( p -> alt_category1 ); }
46 if ( p -> alt_category2 ) { free ( p -> alt_category2 ); }
47 if ( p -> mkdir_sp ) { free ( p -> mkdir_sp ); }
48 if ( p -> info_name ) { free ( p -> info_name ); }
49 if ( p -> info_type ) { free ( p -> info_type ); }
50 if ( p -> info_filename ) { free ( p -> info_filename ); }
55 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
56 int typeflag, struct FTW *ftwbuf )
58 unsigned char valid = pnd_object_type_unknown;
59 pnd_pxml_handle pxmlh = 0;
60 pnd_pxml_handle *pxmlapps = NULL;
61 pnd_pxml_handle *pxmlappiter;
62 unsigned int pxml_close_pos = 0;
63 unsigned char logit = pnd_log_do_buried_logging();
66 pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
69 // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
70 if ( typeflag & FTW_D ) {
72 pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
74 if ( ftwbuf -> level >= pathiter_depthlimit ) {
75 return ( FTW_SKIP_SUBTREE );
77 return ( FTW_CONTINUE ); // skip directories and other non-regular files
80 // PND/PNZ file and others may be valid as well .. but lets leave that for now
81 // printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
82 if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
83 valid = pnd_object_type_directory;
84 } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
85 valid = pnd_object_type_pnd;
88 // if not a file of interest, just keep looking until we run out
91 pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
93 return ( FTW_CONTINUE );
96 // potentially a valid application
97 if ( valid == pnd_object_type_directory ) {
98 // Plaintext PXML file
99 //printf ( "PXML: disco callback encountered '%s'\n", fpath );
101 // pick up the PXML if we can
102 pxmlapps = pnd_pxml_fetch ( (char*) fpath );
104 } else if ( valid == pnd_object_type_pnd ) {
107 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
109 //printf ( "PND: disco callback encountered '%s'\n", fpath );
111 // is this a valid .pnd file? The filename is a candidate already, but verify..
112 // .. presence of PXML appended, or at least contained within?
113 // .. presence of an icon appended after PXML?
116 f = fopen ( fpath, "r" );
118 // try to locate the PXML portion
119 if ( ! pnd_pnd_seek_pxml ( f ) ) {
121 return ( FTW_CONTINUE ); // pnd or not, but not to spec. Pwn'd the pnd?
124 // accrue it into a buffer
125 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
127 return ( FTW_CONTINUE );
130 //printf ( "buffer is %s\n", pxmlbuf );
134 // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
136 char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
137 pngbuffer [ 0 ] = 137; pngbuffer [ 1 ] = 80; pngbuffer [ 2 ] = 78; pngbuffer [ 3 ] = 71;
138 pngbuffer [ 4 ] = 13; pngbuffer [ 5 ] = 10; pngbuffer [ 6 ] = 26; pngbuffer [ 7 ] = 10;
140 unsigned char padtests = 20;
141 unsigned int padstart = ftell ( f );
143 // seek back 10 (should be back into the /PXML> part) to catch any appending-icon-no-line-endings funny business
144 fseek ( f, -10, SEEK_CUR );
148 if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
149 if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
150 pxml_close_pos = ftell ( f ) - 8;
153 fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
160 // no icon found, so back to where we started looking
161 fseek ( f, padstart, SEEK_SET );
167 // by now, we have <PXML> .. </PXML>, try to parse..
168 pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
177 return ( FTW_CONTINUE ); // continue tree walk
181 char ovrfile [ PATH_MAX ];
182 pnd_box_handle ovrh = 0; // 0 didn't try, -1 tried and failed, >0 tried and got
184 // iterate across apps in the PXML
185 pxmlappiter = pxmlapps;
187 pxmlh = *pxmlappiter;
194 // check for validity and add to resultset if it looks executable
195 if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
200 //pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
202 p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
205 p -> object_path = strdup ( fpath );
207 if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
208 *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
209 } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
210 *(fixpxml+1) = '\0'; // for pnd, lop off to last /
213 if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
214 p -> object_filename = strdup ( fixpxml + 1 );
218 p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
221 p -> pnd_icon_pos = pxml_close_pos;
224 p -> object_type = valid;
227 if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
228 p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
230 if ( pnd_pxml_get_description_en ( pxmlh ) ) {
231 p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
233 if ( pnd_pxml_get_icon ( pxmlh ) ) {
234 p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
236 if ( pnd_pxml_get_exec ( pxmlh ) ) {
237 p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
239 if ( pnd_pxml_get_execargs ( pxmlh ) ) {
240 p -> execargs = strdup ( pnd_pxml_get_execargs ( pxmlh ) );
242 if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
243 p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
245 if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
246 p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
248 if ( pnd_pxml_get_appdata_dirname ( pxmlh ) ) {
249 p -> appdata_dirname = strdup ( pnd_pxml_get_appdata_dirname ( pxmlh ) );
251 if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
252 p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
254 if ( pnd_pxml_get_startdir ( pxmlh ) ) {
255 p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
258 if ( pnd_pxml_get_main_category ( pxmlh ) ) {
259 p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
261 if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
262 p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
264 if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
265 p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
267 if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
268 p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
270 if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
271 p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
273 if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
274 p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
277 if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
278 p -> preview_pic1 = strdup ( z );
280 if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
281 p -> preview_pic2 = strdup ( z );
284 if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
285 p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
288 if ( pnd_pxml_get_info_src ( pxmlh ) ) {
289 p -> info_filename = strdup ( pnd_pxml_get_info_src ( pxmlh ) );
291 if ( pnd_pxml_get_info_name ( pxmlh ) ) {
292 p -> info_name = strdup ( pnd_pxml_get_info_name ( pxmlh ) );
294 if ( pnd_pxml_get_info_type ( pxmlh ) ) {
295 p -> info_type = strdup ( pnd_pxml_get_info_type ( pxmlh ) );
298 // look for any PXML overrides, if requested
299 if ( disco_overrides ) {
300 pnd_pxml_merge_override ( pxmlh, disco_overrides );
303 // handle ovr overrides
304 // try to load a same-path-as-pnd override file
306 sprintf ( ovrfile, "%s/%s", p -> object_path, p -> object_filename );
307 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
309 strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
311 if ( stat ( ovrfile, &statbuf ) == 0 ) {
312 ovrh = pnd_conf_fetch_by_path ( ovrfile );
315 // couldn't pull conf out of file, so don't try again
320 ovrh = (void*)(-1); // not found, don't try again
326 if ( ovrh != 0 && ovrh != (void*)(-1) ) {
327 // pull in appropriate values
331 // set the flag regardless, so its for all subapps
332 p -> object_flags |= PND_DISCO_FLAG_OVR;
335 snprintf ( key, 100, "Application-%u.title", p -> subapp_number );
336 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
337 if ( p -> title_en ) {
338 free ( p -> title_en );
340 p -> title_en = strdup ( v );
344 snprintf ( key, 100, "Application-%u.clockspeed", p -> subapp_number );
345 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
346 if ( p -> clockspeed ) {
347 free ( p -> clockspeed );
349 p -> clockspeed = strdup ( v );
353 snprintf ( key, 100, "Application-%u.appdata", p -> subapp_number );
354 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
355 if ( p -> appdata_dirname ) {
356 free ( p -> appdata_dirname );
358 p -> appdata_dirname = strdup ( v );
362 snprintf ( key, 100, "Application-%u.maincategory", p -> subapp_number );
363 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
364 if ( p -> main_category ) {
365 free ( p -> main_category );
367 p -> main_category = strdup ( v );
369 snprintf ( key, 100, "Application-%u.maincategorysub1", p -> subapp_number );
370 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
371 if ( p -> main_category1 ) {
372 free ( p -> main_category1 );
373 p -> main_category1 = NULL;
375 if ( strcasecmp ( v, "NoSubcategory" ) != 0 ) {
376 p -> main_category1 = strdup ( v );
380 } // got ovr conf loaded?
383 //printf ( "Invalid PXML; skipping.\n" );
387 pnd_pxml_delete ( pxmlh );
389 } // while pxmlh is good
392 if ( ovrh != 0 && ovrh != (void*)(-1) ) {
393 pnd_box_delete ( ovrh );
396 // free up the applist
399 return ( FTW_CONTINUE ); // continue the tree walk
402 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
404 //printf ( "Searchpath to discover: '%s'\n", searchpath );
406 // alloc a container for the result set
407 disco_box = pnd_box_new ( "discovery" );
408 disco_overrides = overridespath;
410 /* iterate across the paths within the searchpath, attempting to locate applications
416 // invoke the dir walking function; thankfully Linux includes a pretty good one
417 nftw ( buffer, // path to descend
418 pnd_disco_callback, // callback to do processing
419 10, // no more than X open fd's at once
420 FTW_PHYS | FTW_ACTIONRETVAL ); // do not follow symlinks
425 // return whatever we found, or NULL if nada
426 if ( ! pnd_box_get_head ( disco_box ) ) {
427 pnd_box_delete ( disco_box );
431 return ( disco_box );
434 pnd_box_handle pnd_disco_file ( char *path, char *filename ) {
438 disco_overrides = NULL;
439 disco_box = pnd_box_new ( "discovery" );
442 char fullpath [ PATH_MAX ];
443 sprintf ( fullpath, "%s/%s", path, filename );
446 if ( stat ( fullpath, &statbuf ) < 0 ) {
451 ftw.base = strlen ( path );
454 pnd_disco_callback ( fullpath, &statbuf, FTW_F, &ftw );
456 // return whatever we found, or NULL if nada
457 if ( ! pnd_box_get_head ( disco_box ) ) {
458 pnd_box_delete ( disco_box );
462 return ( disco_box );