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 -> clockspeed ) { free ( p -> clockspeed ); }
32 if ( p -> startdir ) { free ( p -> startdir ); }
33 if ( p -> option_no_x11 ) { free ( p -> option_no_x11 ); }
34 if ( p -> main_category ) { free ( p -> main_category ); }
35 if ( p -> main_category1 ) { free ( p -> main_category1 ); }
36 if ( p -> main_category2 ) { free ( p -> main_category2 ); }
37 if ( p -> alt_category ) { free ( p -> alt_category ); }
38 if ( p -> alt_category1 ) { free ( p -> alt_category1 ); }
39 if ( p -> alt_category2 ) { free ( p -> alt_category2 ); }
40 if ( p -> mkdir_sp ) { free ( p -> mkdir_sp ); }
45 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
46 int typeflag, struct FTW *ftwbuf )
48 unsigned char valid = pnd_object_type_unknown;
49 pnd_pxml_handle pxmlh = 0;
50 pnd_pxml_handle *pxmlapps = NULL;
51 pnd_pxml_handle *pxmlappiter;
52 unsigned int pxml_close_pos = 0;
53 unsigned char logit = pnd_log_do_buried_logging();
56 pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
59 // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
60 if ( typeflag & FTW_D ) {
62 pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
64 return ( 0 ); // skip directories and other non-regular files
67 // PND/PNZ file and others may be valid as well .. but lets leave that for now
68 // printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
69 if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
70 valid = pnd_object_type_directory;
71 } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
72 valid = pnd_object_type_pnd;
75 // if not a file of interest, just keep looking until we run out
78 pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
83 // potentially a valid application
84 if ( valid == pnd_object_type_directory ) {
85 // Plaintext PXML file
86 //printf ( "PXML: disco callback encountered '%s'\n", fpath );
88 // pick up the PXML if we can
89 pxmlapps = pnd_pxml_fetch ( (char*) fpath );
91 } else if ( valid == pnd_object_type_pnd ) {
94 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
96 //printf ( "PND: disco callback encountered '%s'\n", fpath );
98 // is this a valid .pnd file? The filename is a candidate already, but verify..
99 // .. presence of PXML appended, or at least contained within?
100 // .. presence of an icon appended after PXML?
103 f = fopen ( fpath, "r" );
105 // try to locate the PXML portion
106 if ( ! pnd_pnd_seek_pxml ( f ) ) {
108 return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
111 // accrue it into a buffer
112 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
117 //printf ( "buffer is %s\n", pxmlbuf );
121 // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
123 unsigned int pos = ftell ( f );
124 char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
125 pngbuffer [ 0 ] = 137; pngbuffer [ 1 ] = 80; pngbuffer [ 2 ] = 78; pngbuffer [ 3 ] = 71;
126 pngbuffer [ 4 ] = 13; pngbuffer [ 5 ] = 10; pngbuffer [ 6 ] = 26; pngbuffer [ 7 ] = 10;
128 unsigned char padtests = 10;
129 unsigned int padstart = ftell ( f );
132 if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
133 if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
134 pxml_close_pos = pos;
137 fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
144 // no icon found, so back to where we started looking
145 fseek ( f, padstart, SEEK_SET );
151 // by now, we have <PXML> .. </PXML>, try to parse..
152 pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
161 return ( 0 ); // continue tree walk
164 // iterate across apps in the PXML
165 pxmlappiter = pxmlapps;
167 pxmlh = *pxmlappiter;
174 // look for any overrides, if requested
175 pnd_pxml_merge_override ( pxmlh, disco_overrides );
177 // check for validity and add to resultset if it looks executable
178 if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
183 pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
185 p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
188 p -> object_path = strdup ( fpath );
190 if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
191 *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
192 } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
193 *(fixpxml+1) = '\0'; // for pnd, lop off to last /
196 if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
197 p -> object_filename = strdup ( fixpxml + 1 );
201 p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
204 p -> pnd_icon_pos = pxml_close_pos;
207 p -> object_type = valid;
210 if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
211 p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
213 if ( pnd_pxml_get_description_en ( pxmlh ) ) {
214 p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
216 if ( pnd_pxml_get_icon ( pxmlh ) ) {
217 p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
219 if ( pnd_pxml_get_exec ( pxmlh ) ) {
220 p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
222 if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
223 p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
225 if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
226 p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
228 if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
229 p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
231 if ( pnd_pxml_get_startdir ( pxmlh ) ) {
232 p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
235 if ( pnd_pxml_get_main_category ( pxmlh ) ) {
236 p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
238 if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
239 p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
241 if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
242 p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
244 if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
245 p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
247 if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
248 p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
250 if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
251 p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
254 if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
255 p -> preview_pic1 = strdup ( z );
257 if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
258 p -> preview_pic2 = strdup ( z );
261 if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
262 p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
266 //printf ( "Invalid PXML; skipping.\n" );
270 pnd_pxml_delete ( pxmlh );
272 } // while pxmlh is good
274 // free up the applist
277 return ( 0 ); // continue the tree walk
280 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
282 //printf ( "Searchpath to discover: '%s'\n", searchpath );
284 // alloc a container for the result set
285 disco_box = pnd_box_new ( "discovery" );
286 disco_overrides = overridespath;
288 /* iterate across the paths within the searchpath, attempting to locate applications
294 // invoke the dir walking function; thankfully Linux includes a pretty good one
295 nftw ( buffer, // path to descend
296 pnd_disco_callback, // callback to do processing
297 10, // no more than X open fd's at once
298 FTW_PHYS ); // do not follow symlinks
303 // return whatever we found, or NULL if nada
304 if ( ! pnd_box_get_head ( disco_box ) ) {
305 pnd_box_delete ( disco_box );
309 return ( disco_box );