2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #include <unistd.h> /* for unlink */
6 #define __USE_GNU /* for strcasestr */
7 #include <string.h> /* for making ftw.h happy */
9 #define _XOPEN_SOURCE 500
10 #define __USE_XOPEN_EXTENDED
11 #include <ftw.h> /* for nftw, tree walker */
13 #include "pnd_container.h"
15 #include "pnd_discovery.h"
16 #include "pnd_pathiter.h"
18 #include "pnd_pndfiles.h"
19 #include "pnd_logger.h"
21 // need these 'globals' due to the way nftw and ftw work :/
22 static pnd_box_handle disco_box;
23 static char *disco_overrides = NULL;
25 void pnd_disco_destroy ( pnd_disco_t *p ) {
27 if ( p -> title_en ) { free ( p -> title_en ); }
28 if ( p -> unique_id ) { free ( p -> unique_id ); }
29 if ( p -> icon ) { free ( p -> icon ); }
30 if ( p -> exec ) { free ( p -> exec ); }
31 if ( p -> execargs ) { free ( p -> execargs ); }
32 if ( p -> clockspeed ) { free ( p -> clockspeed ); }
33 if ( p -> startdir ) { free ( p -> startdir ); }
34 if ( p -> option_no_x11 ) { free ( p -> option_no_x11 ); }
35 if ( p -> main_category ) { free ( p -> main_category ); }
36 if ( p -> main_category1 ) { free ( p -> main_category1 ); }
37 if ( p -> main_category2 ) { free ( p -> main_category2 ); }
38 if ( p -> alt_category ) { free ( p -> alt_category ); }
39 if ( p -> alt_category1 ) { free ( p -> alt_category1 ); }
40 if ( p -> alt_category2 ) { free ( p -> alt_category2 ); }
41 if ( p -> mkdir_sp ) { free ( p -> mkdir_sp ); }
42 if ( p -> info_name ) { free ( p -> info_name ); }
43 if ( p -> info_type ) { free ( p -> info_type ); }
44 if ( p -> info_filename ) { free ( p -> info_filename ); }
49 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
50 int typeflag, struct FTW *ftwbuf )
52 unsigned char valid = pnd_object_type_unknown;
53 pnd_pxml_handle pxmlh = 0;
54 pnd_pxml_handle *pxmlapps = NULL;
55 pnd_pxml_handle *pxmlappiter;
56 unsigned int pxml_close_pos = 0;
57 unsigned char logit = pnd_log_do_buried_logging();
60 pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
63 // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
64 if ( typeflag & FTW_D ) {
66 pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
68 return ( 0 ); // skip directories and other non-regular files
71 // PND/PNZ file and others may be valid as well .. but lets leave that for now
72 // printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
73 if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
74 valid = pnd_object_type_directory;
75 } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
76 valid = pnd_object_type_pnd;
79 // if not a file of interest, just keep looking until we run out
82 pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
87 // potentially a valid application
88 if ( valid == pnd_object_type_directory ) {
89 // Plaintext PXML file
90 //printf ( "PXML: disco callback encountered '%s'\n", fpath );
92 // pick up the PXML if we can
93 pxmlapps = pnd_pxml_fetch ( (char*) fpath );
95 } else if ( valid == pnd_object_type_pnd ) {
98 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
100 //printf ( "PND: disco callback encountered '%s'\n", fpath );
102 // is this a valid .pnd file? The filename is a candidate already, but verify..
103 // .. presence of PXML appended, or at least contained within?
104 // .. presence of an icon appended after PXML?
107 f = fopen ( fpath, "r" );
109 // try to locate the PXML portion
110 if ( ! pnd_pnd_seek_pxml ( f ) ) {
112 return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
115 // accrue it into a buffer
116 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
121 //printf ( "buffer is %s\n", pxmlbuf );
125 // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
127 unsigned int pos = ftell ( f );
128 char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
129 pngbuffer [ 0 ] = 137; pngbuffer [ 1 ] = 80; pngbuffer [ 2 ] = 78; pngbuffer [ 3 ] = 71;
130 pngbuffer [ 4 ] = 13; pngbuffer [ 5 ] = 10; pngbuffer [ 6 ] = 26; pngbuffer [ 7 ] = 10;
132 unsigned char padtests = 10;
133 unsigned int padstart = ftell ( f );
136 if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
137 if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
138 pxml_close_pos = pos;
141 fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
148 // no icon found, so back to where we started looking
149 fseek ( f, padstart, SEEK_SET );
155 // by now, we have <PXML> .. </PXML>, try to parse..
156 pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
165 return ( 0 ); // continue tree walk
168 // iterate across apps in the PXML
169 pxmlappiter = pxmlapps;
171 pxmlh = *pxmlappiter;
178 // look for any overrides, if requested
179 if ( disco_overrides ) {
180 pnd_pxml_merge_override ( pxmlh, disco_overrides );
183 // check for validity and add to resultset if it looks executable
184 if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
189 //pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
191 p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
194 p -> object_path = strdup ( fpath );
196 if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
197 *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
198 } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
199 *(fixpxml+1) = '\0'; // for pnd, lop off to last /
202 if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
203 p -> object_filename = strdup ( fixpxml + 1 );
207 p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
210 p -> pnd_icon_pos = pxml_close_pos;
213 p -> object_type = valid;
216 if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
217 p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
219 if ( pnd_pxml_get_description_en ( pxmlh ) ) {
220 p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
222 if ( pnd_pxml_get_icon ( pxmlh ) ) {
223 p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
225 if ( pnd_pxml_get_exec ( pxmlh ) ) {
226 p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
228 if ( pnd_pxml_get_execargs ( pxmlh ) ) {
229 p -> execargs = strdup ( pnd_pxml_get_execargs ( pxmlh ) );
231 if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
232 p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
234 if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
235 p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
237 if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
238 p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
240 if ( pnd_pxml_get_startdir ( pxmlh ) ) {
241 p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
244 if ( pnd_pxml_get_main_category ( pxmlh ) ) {
245 p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
247 if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
248 p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
250 if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
251 p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
253 if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
254 p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
256 if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
257 p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
259 if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
260 p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
263 if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
264 p -> preview_pic1 = strdup ( z );
266 if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
267 p -> preview_pic2 = strdup ( z );
270 if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
271 p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
274 if ( pnd_pxml_get_info_src ( pxmlh ) ) {
275 p -> info_filename = strdup ( pnd_pxml_get_info_src ( pxmlh ) );
277 if ( pnd_pxml_get_info_name ( pxmlh ) ) {
278 p -> info_name = strdup ( pnd_pxml_get_info_name ( pxmlh ) );
280 if ( pnd_pxml_get_info_type ( pxmlh ) ) {
281 p -> info_type = strdup ( pnd_pxml_get_info_type ( pxmlh ) );
285 //printf ( "Invalid PXML; skipping.\n" );
289 pnd_pxml_delete ( pxmlh );
291 } // while pxmlh is good
293 // free up the applist
296 return ( 0 ); // continue the tree walk
299 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
301 //printf ( "Searchpath to discover: '%s'\n", searchpath );
303 // alloc a container for the result set
304 disco_box = pnd_box_new ( "discovery" );
305 disco_overrides = overridespath;
307 /* iterate across the paths within the searchpath, attempting to locate applications
313 // invoke the dir walking function; thankfully Linux includes a pretty good one
314 nftw ( buffer, // path to descend
315 pnd_disco_callback, // callback to do processing
316 10, // no more than X open fd's at once
317 FTW_PHYS ); // do not follow symlinks
322 // return whatever we found, or NULL if nada
323 if ( ! pnd_box_get_head ( disco_box ) ) {
324 pnd_box_delete ( disco_box );
328 return ( disco_box );