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 -> 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_package_id ( pxmlh ) ) {
228 p -> package_id = strdup ( pnd_pxml_get_package_id ( pxmlh ) );
230 if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
231 p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
233 if ( pnd_pxml_get_description_en ( pxmlh ) ) {
234 p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
236 if ( pnd_pxml_get_icon ( pxmlh ) ) {
237 p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
239 if ( pnd_pxml_get_exec ( pxmlh ) ) {
240 p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
242 if ( pnd_pxml_get_execargs ( pxmlh ) ) {
243 p -> execargs = strdup ( pnd_pxml_get_execargs ( pxmlh ) );
245 if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
246 p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
248 if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
249 p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
251 if ( pnd_pxml_get_appdata_dirname ( pxmlh ) ) {
252 p -> appdata_dirname = strdup ( pnd_pxml_get_appdata_dirname ( pxmlh ) );
254 if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
255 p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
257 if ( pnd_pxml_get_startdir ( pxmlh ) ) {
258 p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
261 if ( pnd_pxml_get_main_category ( pxmlh ) ) {
262 p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
264 if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
265 p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
267 if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
268 p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
270 if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
271 p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
273 if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
274 p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
276 if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
277 p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
280 if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
281 p -> preview_pic1 = strdup ( z );
283 if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
284 p -> preview_pic2 = strdup ( z );
287 if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
288 p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
291 if ( pnd_pxml_get_info_src ( pxmlh ) ) {
292 p -> info_filename = strdup ( pnd_pxml_get_info_src ( pxmlh ) );
294 if ( pnd_pxml_get_info_name ( pxmlh ) ) {
295 p -> info_name = strdup ( pnd_pxml_get_info_name ( pxmlh ) );
297 if ( pnd_pxml_get_info_type ( pxmlh ) ) {
298 p -> info_type = strdup ( pnd_pxml_get_info_type ( pxmlh ) );
301 // look for any PXML overrides, if requested
302 if ( disco_overrides ) {
303 pnd_pxml_merge_override ( pxmlh, disco_overrides );
306 // handle ovr overrides
307 // try to load a same-path-as-pnd override file
309 sprintf ( ovrfile, "%s/%s", p -> object_path, p -> object_filename );
310 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
312 strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
314 if ( stat ( ovrfile, &statbuf ) == 0 ) {
315 ovrh = pnd_conf_fetch_by_path ( ovrfile );
318 // couldn't pull conf out of file, so don't try again
323 ovrh = (void*)(-1); // not found, don't try again
329 if ( ovrh != 0 && ovrh != (void*)(-1) ) {
330 // pull in appropriate values
334 // set the flag regardless, so its for all subapps
335 p -> object_flags |= PND_DISCO_FLAG_OVR;
338 snprintf ( key, 100, "Application-%u.title", p -> subapp_number );
339 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
340 if ( p -> title_en ) {
341 free ( p -> title_en );
343 p -> title_en = strdup ( v );
347 snprintf ( key, 100, "Application-%u.clockspeed", p -> subapp_number );
348 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
349 if ( p -> clockspeed ) {
350 free ( p -> clockspeed );
352 p -> clockspeed = strdup ( v );
356 snprintf ( key, 100, "Application-%u.appdata", p -> subapp_number );
357 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
358 if ( p -> appdata_dirname ) {
359 free ( p -> appdata_dirname );
361 p -> appdata_dirname = strdup ( v );
365 snprintf ( key, 100, "Application-%u.maincategory", p -> subapp_number );
366 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
367 if ( p -> main_category ) {
368 free ( p -> main_category );
370 p -> main_category = strdup ( v );
372 snprintf ( key, 100, "Application-%u.maincategorysub1", p -> subapp_number );
373 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
374 if ( p -> main_category1 ) {
375 free ( p -> main_category1 );
376 p -> main_category1 = NULL;
378 if ( strcasecmp ( v, "NoSubcategory" ) != 0 ) {
379 p -> main_category1 = strdup ( v );
383 } // got ovr conf loaded?
386 //printf ( "Invalid PXML; skipping.\n" );
390 pnd_pxml_delete ( pxmlh );
392 } // while pxmlh is good
395 if ( ovrh != 0 && ovrh != (void*)(-1) ) {
396 pnd_box_delete ( ovrh );
399 // free up the applist
402 return ( FTW_CONTINUE ); // continue the tree walk
405 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
407 //printf ( "Searchpath to discover: '%s'\n", searchpath );
409 // alloc a container for the result set
410 disco_box = pnd_box_new ( "discovery" );
411 disco_overrides = overridespath;
413 /* iterate across the paths within the searchpath, attempting to locate applications
419 // invoke the dir walking function; thankfully Linux includes a pretty good one
420 nftw ( buffer, // path to descend
421 pnd_disco_callback, // callback to do processing
422 10, // no more than X open fd's at once
423 FTW_PHYS | FTW_ACTIONRETVAL ); // do not follow symlinks
428 // return whatever we found, or NULL if nada
429 if ( ! pnd_box_get_head ( disco_box ) ) {
430 pnd_box_delete ( disco_box );
434 return ( disco_box );
437 pnd_box_handle pnd_disco_file ( char *path, char *filename ) {
441 disco_overrides = NULL;
442 disco_box = pnd_box_new ( "discovery" );
445 char fullpath [ PATH_MAX ];
446 sprintf ( fullpath, "%s/%s", path, filename );
449 if ( stat ( fullpath, &statbuf ) < 0 ) {
454 ftw.base = strlen ( path );
457 pnd_disco_callback ( fullpath, &statbuf, FTW_F, &ftw );
459 // return whatever we found, or NULL if nada
460 if ( ! pnd_box_get_head ( disco_box ) ) {
461 pnd_box_delete ( disco_box );
465 return ( disco_box );