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"
20 // need these 'globals' due to the way nftw and ftw work :/
21 static pnd_box_handle disco_box;
22 static char *disco_overrides = NULL;
24 void pnd_disco_destroy ( pnd_disco_t *p ) {
26 if ( p -> title_en ) { free ( p -> title_en ); }
27 if ( p -> unique_id ) { free ( p -> unique_id ); }
28 if ( p -> icon ) { free ( p -> icon ); }
29 if ( p -> exec ) { free ( p -> exec ); }
30 if ( p -> clockspeed ) { free ( p -> clockspeed ); }
31 if ( p -> startdir ) { free ( p -> startdir ); }
32 if ( p -> option_no_x11 ) { free ( p -> option_no_x11 ); }
33 if ( p -> main_category ) { free ( p -> main_category ); }
34 if ( p -> main_category1 ) { free ( p -> main_category1 ); }
35 if ( p -> main_category2 ) { free ( p -> main_category2 ); }
36 if ( p -> alt_category ) { free ( p -> alt_category ); }
37 if ( p -> alt_category1 ) { free ( p -> alt_category1 ); }
38 if ( p -> alt_category2 ) { free ( p -> alt_category2 ); }
43 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
44 int typeflag, struct FTW *ftwbuf )
46 unsigned char valid = pnd_object_type_unknown;
47 pnd_pxml_handle pxmlh = 0;
48 unsigned int pxml_close_pos = 0;
50 //printf ( "disco root callback encountered '%s'\n", fpath );
52 // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
53 if ( typeflag & FTW_D ) {
54 //printf ( " .. is dir, skipping\n" );
55 return ( 0 ); // skip directories and other non-regular files
58 // PND/PNZ file and others may be valid as well .. but lets leave that for now
59 // printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
60 if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
61 valid = pnd_object_type_directory;
62 } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
63 valid = pnd_object_type_pnd;
66 // if not a file of interest, just keep looking until we run out
68 //printf ( " .. bad filename, skipping\n" );
72 // potentially a valid application
73 if ( valid == pnd_object_type_directory ) {
74 // Plaintext PXML file
75 //printf ( "PXML: disco callback encountered '%s'\n", fpath );
77 // pick up the PXML if we can
78 pxmlh = pnd_pxml_fetch ( (char*) fpath );
80 } else if ( valid == pnd_object_type_pnd ) {
83 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
85 //printf ( "PND: disco callback encountered '%s'\n", fpath );
87 // is this a valid .pnd file? The filename is a candidate already, but verify..
88 // .. presence of PXML appended, or at least contained within?
89 // .. presence of an icon appended after PXML?
92 f = fopen ( fpath, "r" );
94 // try to locate the PXML portion
95 if ( ! pnd_pnd_seek_pxml ( f ) ) {
97 return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
100 // accrue it into a buffer
101 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
106 //printf ( "buffer is %s\n", pxmlbuf );
110 // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
112 unsigned int pos = ftell ( f );
113 char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
114 pngbuffer [ 0 ] = 137; pngbuffer [ 1 ] = 80; pngbuffer [ 2 ] = 78; pngbuffer [ 3 ] = 71;
115 pngbuffer [ 4 ] = 13; pngbuffer [ 5 ] = 10; pngbuffer [ 6 ] = 26; pngbuffer [ 7 ] = 10;
116 if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
117 if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
118 pxml_close_pos = pos;
124 // by now, we have <PXML> .. </PXML>, try to parse..
125 pxmlh = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
135 // look for any overrides, if requested
136 pnd_pxml_merge_override ( pxmlh, disco_overrides );
138 // check for validity and add to resultset if it looks executable
139 if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
144 p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
147 p -> object_path = strdup ( fpath );
149 if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
150 *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
151 } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
152 *(fixpxml+1) = '\0'; // for pnd, lop off to last /
155 if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
156 p -> object_filename = strdup ( fixpxml + 1 );
160 p -> pnd_icon_pos = pxml_close_pos;
163 p -> object_type = valid;
166 if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
167 p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
169 if ( pnd_pxml_get_description_en ( pxmlh ) ) {
170 p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
172 if ( pnd_pxml_get_icon ( pxmlh ) ) {
173 p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
175 if ( pnd_pxml_get_exec ( pxmlh ) ) {
176 p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
178 if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
179 p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
181 if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
182 p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
184 if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
185 p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
187 if ( pnd_pxml_get_startdir ( pxmlh ) ) {
188 p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
191 if ( pnd_pxml_get_main_category ( pxmlh ) ) {
192 p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
194 if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
195 p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
197 if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
198 p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
200 if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
201 p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
203 if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
204 p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
206 if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
207 p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
210 if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
211 p -> preview_pic1 = strdup ( z );
213 if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
214 p -> preview_pic2 = strdup ( z );
218 //printf ( "Invalid PXML; skipping.\n" );
222 pnd_pxml_delete ( pxmlh );
226 return ( 0 ); // continue the tree walk
229 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
231 //printf ( "Searchpath to discover: '%s'\n", searchpath );
233 // alloc a container for the result set
234 disco_box = pnd_box_new ( "discovery" );
235 disco_overrides = overridespath;
237 /* iterate across the paths within the searchpath, attempting to locate applications
243 // invoke the dir walking function; thankfully Linux includes a pretty good one
244 nftw ( buffer, // path to descend
245 pnd_disco_callback, // callback to do processing
246 10, // no more than X open fd's at once
247 FTW_PHYS ); // do not follow symlinks
252 // return whatever we found, or NULL if nada
253 if ( ! pnd_box_get_head ( disco_box ) ) {
254 pnd_box_delete ( disco_box );
258 return ( disco_box );