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
14 #include <ftw.h> /* for nftw, tree walker */
16 #include "pnd_container.h"
18 #include "pnd_discovery.h"
19 #include "pnd_pathiter.h"
21 #include "pnd_pndfiles.h"
22 #include "pnd_logger.h"
25 // need these 'globals' due to the way nftw and ftw work :/
26 static pnd_box_handle disco_box;
27 static char *disco_overrides = NULL;
29 void pnd_disco_destroy ( pnd_disco_t *p ) {
31 if ( p -> title_en ) { free ( p -> title_en ); }
32 if ( p -> unique_id ) { free ( p -> unique_id ); }
33 if ( p -> appdata_dirname ) { free ( p -> appdata_dirname ); }
34 if ( p -> icon ) { free ( p -> icon ); }
35 if ( p -> exec ) { free ( p -> exec ); }
36 if ( p -> execargs ) { free ( p -> execargs ); }
37 if ( p -> clockspeed ) { free ( p -> clockspeed ); }
38 if ( p -> startdir ) { free ( p -> startdir ); }
39 if ( p -> option_no_x11 ) { free ( p -> option_no_x11 ); }
40 if ( p -> main_category ) { free ( p -> main_category ); }
41 if ( p -> main_category1 ) { free ( p -> main_category1 ); }
42 if ( p -> main_category2 ) { free ( p -> main_category2 ); }
43 if ( p -> alt_category ) { free ( p -> alt_category ); }
44 if ( p -> alt_category1 ) { free ( p -> alt_category1 ); }
45 if ( p -> alt_category2 ) { free ( p -> alt_category2 ); }
46 if ( p -> mkdir_sp ) { free ( p -> mkdir_sp ); }
47 if ( p -> info_name ) { free ( p -> info_name ); }
48 if ( p -> info_type ) { free ( p -> info_type ); }
49 if ( p -> info_filename ) { free ( p -> info_filename ); }
54 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
55 int typeflag, struct FTW *ftwbuf )
57 unsigned char valid = pnd_object_type_unknown;
58 pnd_pxml_handle pxmlh = 0;
59 pnd_pxml_handle *pxmlapps = NULL;
60 pnd_pxml_handle *pxmlappiter;
61 unsigned int pxml_close_pos = 0;
62 unsigned char logit = pnd_log_do_buried_logging();
65 pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
68 // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
69 if ( typeflag & FTW_D ) {
71 pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
73 return ( 0 ); // skip directories and other non-regular files
76 // PND/PNZ file and others may be valid as well .. but lets leave that for now
77 // printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
78 if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
79 valid = pnd_object_type_directory;
80 } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
81 valid = pnd_object_type_pnd;
84 // if not a file of interest, just keep looking until we run out
87 pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
92 // potentially a valid application
93 if ( valid == pnd_object_type_directory ) {
94 // Plaintext PXML file
95 //printf ( "PXML: disco callback encountered '%s'\n", fpath );
97 // pick up the PXML if we can
98 pxmlapps = pnd_pxml_fetch ( (char*) fpath );
100 } else if ( valid == pnd_object_type_pnd ) {
103 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
105 //printf ( "PND: disco callback encountered '%s'\n", fpath );
107 // is this a valid .pnd file? The filename is a candidate already, but verify..
108 // .. presence of PXML appended, or at least contained within?
109 // .. presence of an icon appended after PXML?
112 f = fopen ( fpath, "r" );
114 // try to locate the PXML portion
115 if ( ! pnd_pnd_seek_pxml ( f ) ) {
117 return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
120 // accrue it into a buffer
121 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
126 //printf ( "buffer is %s\n", pxmlbuf );
130 // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
132 unsigned int pos = ftell ( f );
133 char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
134 pngbuffer [ 0 ] = 137; pngbuffer [ 1 ] = 80; pngbuffer [ 2 ] = 78; pngbuffer [ 3 ] = 71;
135 pngbuffer [ 4 ] = 13; pngbuffer [ 5 ] = 10; pngbuffer [ 6 ] = 26; pngbuffer [ 7 ] = 10;
137 unsigned char padtests = 20;
138 unsigned int padstart = ftell ( f );
140 // seek back 10 (should be back into the /PXML> part) to catch any appending-icon-no-line-endings funny business
141 fseek ( f, -10, SEEK_CUR );
145 if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
146 if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
147 pxml_close_pos = pos;
150 fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
157 // no icon found, so back to where we started looking
158 fseek ( f, padstart, SEEK_SET );
164 // by now, we have <PXML> .. </PXML>, try to parse..
165 pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
174 return ( 0 ); // continue tree walk
178 char ovrfile [ PATH_MAX ];
179 pnd_box_handle ovrh = 0; // 0 didn't try, -1 tried and failed, >0 tried and got
181 // iterate across apps in the PXML
182 pxmlappiter = pxmlapps;
184 pxmlh = *pxmlappiter;
191 // check for validity and add to resultset if it looks executable
192 if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
197 //pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
199 p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
202 p -> object_path = strdup ( fpath );
204 if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
205 *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
206 } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
207 *(fixpxml+1) = '\0'; // for pnd, lop off to last /
210 if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
211 p -> object_filename = strdup ( fixpxml + 1 );
215 p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
218 p -> pnd_icon_pos = pxml_close_pos;
221 p -> object_type = valid;
224 if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
225 p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
227 if ( pnd_pxml_get_description_en ( pxmlh ) ) {
228 p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
230 if ( pnd_pxml_get_icon ( pxmlh ) ) {
231 p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
233 if ( pnd_pxml_get_exec ( pxmlh ) ) {
234 p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
236 if ( pnd_pxml_get_execargs ( pxmlh ) ) {
237 p -> execargs = strdup ( pnd_pxml_get_execargs ( pxmlh ) );
239 if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
240 p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
242 if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
243 p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
245 if ( pnd_pxml_get_appdata_dirname ( pxmlh ) ) {
246 p -> appdata_dirname = strdup ( pnd_pxml_get_appdata_dirname ( pxmlh ) );
248 if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
249 p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
251 if ( pnd_pxml_get_startdir ( pxmlh ) ) {
252 p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
255 if ( pnd_pxml_get_main_category ( pxmlh ) ) {
256 p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
258 if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
259 p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
261 if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
262 p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
264 if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
265 p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
267 if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
268 p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
270 if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
271 p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
274 if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
275 p -> preview_pic1 = strdup ( z );
277 if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
278 p -> preview_pic2 = strdup ( z );
281 if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
282 p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
285 if ( pnd_pxml_get_info_src ( pxmlh ) ) {
286 p -> info_filename = strdup ( pnd_pxml_get_info_src ( pxmlh ) );
288 if ( pnd_pxml_get_info_name ( pxmlh ) ) {
289 p -> info_name = strdup ( pnd_pxml_get_info_name ( pxmlh ) );
291 if ( pnd_pxml_get_info_type ( pxmlh ) ) {
292 p -> info_type = strdup ( pnd_pxml_get_info_type ( pxmlh ) );
295 // look for any PXML overrides, if requested
296 if ( disco_overrides ) {
297 pnd_pxml_merge_override ( pxmlh, disco_overrides );
300 // handle ovr overrides
301 // try to load a same-path-as-pnd override file
303 sprintf ( ovrfile, "%s/%s", p -> object_path, p -> object_filename );
304 fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
306 strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
308 if ( stat ( ovrfile, &statbuf ) == 0 ) {
309 ovrh = pnd_conf_fetch_by_path ( ovrfile );
312 // couldn't pull conf out of file, so don't try again
317 ovrh = (void*)(-1); // not found, don't try again
323 if ( ovrh != 0 && ovrh != (void*)(-1) ) {
324 // pull in appropriate values
328 // set the flag regardless, so its for all subapps
329 p -> object_flags |= PND_DISCO_FLAG_OVR;
332 snprintf ( key, 100, "Application-%u.title", p -> subapp_number );
333 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
334 if ( p -> title_en ) {
335 free ( p -> title_en );
337 p -> title_en = strdup ( v );
341 snprintf ( key, 100, "Application-%u.clockspeed", p -> subapp_number );
342 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
343 if ( p -> clockspeed ) {
344 free ( p -> clockspeed );
346 p -> clockspeed = strdup ( v );
350 snprintf ( key, 100, "Application-%u.appdata", p -> subapp_number );
351 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
352 if ( p -> appdata_dirname ) {
353 free ( p -> appdata_dirname );
355 p -> appdata_dirname = strdup ( v );
359 snprintf ( key, 100, "Application-%u.maincategory", p -> subapp_number );
360 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
361 if ( p -> main_category ) {
362 free ( p -> main_category );
364 p -> main_category = strdup ( v );
366 snprintf ( key, 100, "Application-%u.maincategorysub1", p -> subapp_number );
367 if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
368 if ( p -> main_category1 ) {
369 free ( p -> main_category1 );
371 p -> main_category1 = strdup ( v );
374 } // got ovr conf loaded?
377 //printf ( "Invalid PXML; skipping.\n" );
381 pnd_pxml_delete ( pxmlh );
383 } // while pxmlh is good
386 if ( ovrh != 0 && ovrh != (void*)(-1) ) {
387 pnd_box_delete ( ovrh );
390 // free up the applist
393 return ( 0 ); // continue the tree walk
396 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
398 //printf ( "Searchpath to discover: '%s'\n", searchpath );
400 // alloc a container for the result set
401 disco_box = pnd_box_new ( "discovery" );
402 disco_overrides = overridespath;
404 /* iterate across the paths within the searchpath, attempting to locate applications
410 // invoke the dir walking function; thankfully Linux includes a pretty good one
411 nftw ( buffer, // path to descend
412 pnd_disco_callback, // callback to do processing
413 10, // no more than X open fd's at once
414 FTW_PHYS ); // do not follow symlinks
419 // return whatever we found, or NULL if nada
420 if ( ! pnd_box_get_head ( disco_box ) ) {
421 pnd_box_delete ( disco_box );
425 return ( disco_box );
428 pnd_box_handle pnd_disco_file ( char *path, char *filename ) {
432 disco_overrides = NULL;
433 disco_box = pnd_box_new ( "discovery" );
436 char fullpath [ PATH_MAX ];
437 sprintf ( fullpath, "%s/%s", path, filename );
440 if ( stat ( fullpath, &statbuf ) < 0 ) {
445 ftw.base = strlen ( path );
448 pnd_disco_callback ( fullpath, &statbuf, FTW_F, &ftw );
450 // return whatever we found, or NULL if nada
451 if ( ! pnd_box_get_head ( disco_box ) ) {
452 pnd_box_delete ( disco_box );
456 return ( disco_box );