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 ); }
39 if ( p -> mkdir_sp ) { free ( p -> mkdir_sp ); }
44 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
45 int typeflag, struct FTW *ftwbuf )
47 unsigned char valid = pnd_object_type_unknown;
48 pnd_pxml_handle pxmlh = 0;
49 unsigned int pxml_close_pos = 0;
51 //printf ( "disco root callback encountered '%s'\n", fpath );
53 // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
54 if ( typeflag & FTW_D ) {
55 //printf ( " .. is dir, skipping\n" );
56 return ( 0 ); // skip directories and other non-regular files
59 // PND/PNZ file and others may be valid as well .. but lets leave that for now
60 // printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
61 if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
62 valid = pnd_object_type_directory;
63 } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
64 valid = pnd_object_type_pnd;
67 // if not a file of interest, just keep looking until we run out
69 //printf ( " .. bad filename, skipping\n" );
73 // potentially a valid application
74 if ( valid == pnd_object_type_directory ) {
75 // Plaintext PXML file
76 //printf ( "PXML: disco callback encountered '%s'\n", fpath );
78 // pick up the PXML if we can
79 pxmlh = pnd_pxml_fetch ( (char*) fpath );
81 } else if ( valid == pnd_object_type_pnd ) {
84 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
86 //printf ( "PND: disco callback encountered '%s'\n", fpath );
88 // is this a valid .pnd file? The filename is a candidate already, but verify..
89 // .. presence of PXML appended, or at least contained within?
90 // .. presence of an icon appended after PXML?
93 f = fopen ( fpath, "r" );
95 // try to locate the PXML portion
96 if ( ! pnd_pnd_seek_pxml ( f ) ) {
98 return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
101 // accrue it into a buffer
102 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
107 //printf ( "buffer is %s\n", pxmlbuf );
111 // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
113 unsigned int pos = ftell ( f );
114 char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
115 pngbuffer [ 0 ] = 137; pngbuffer [ 1 ] = 80; pngbuffer [ 2 ] = 78; pngbuffer [ 3 ] = 71;
116 pngbuffer [ 4 ] = 13; pngbuffer [ 5 ] = 10; pngbuffer [ 6 ] = 26; pngbuffer [ 7 ] = 10;
117 if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
118 if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
119 pxml_close_pos = pos;
125 // by now, we have <PXML> .. </PXML>, try to parse..
126 pxmlh = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
136 // look for any overrides, if requested
137 pnd_pxml_merge_override ( pxmlh, disco_overrides );
139 // check for validity and add to resultset if it looks executable
140 if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
145 p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
148 p -> object_path = strdup ( fpath );
150 if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
151 *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
152 } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
153 *(fixpxml+1) = '\0'; // for pnd, lop off to last /
156 if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
157 p -> object_filename = strdup ( fixpxml + 1 );
161 p -> pnd_icon_pos = pxml_close_pos;
164 p -> object_type = valid;
167 if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
168 p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
170 if ( pnd_pxml_get_description_en ( pxmlh ) ) {
171 p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
173 if ( pnd_pxml_get_icon ( pxmlh ) ) {
174 p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
176 if ( pnd_pxml_get_exec ( pxmlh ) ) {
177 p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
179 if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
180 p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
182 if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
183 p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
185 if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
186 p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
188 if ( pnd_pxml_get_startdir ( pxmlh ) ) {
189 p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
192 if ( pnd_pxml_get_main_category ( pxmlh ) ) {
193 p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
195 if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
196 p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
198 if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
199 p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
201 if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
202 p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
204 if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
205 p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
207 if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
208 p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
211 if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
212 p -> preview_pic1 = strdup ( z );
214 if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
215 p -> preview_pic2 = strdup ( z );
218 if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
219 p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
223 //printf ( "Invalid PXML; skipping.\n" );
227 pnd_pxml_delete ( pxmlh );
231 return ( 0 ); // continue the tree walk
234 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
236 //printf ( "Searchpath to discover: '%s'\n", searchpath );
238 // alloc a container for the result set
239 disco_box = pnd_box_new ( "discovery" );
240 disco_overrides = overridespath;
242 /* iterate across the paths within the searchpath, attempting to locate applications
248 // invoke the dir walking function; thankfully Linux includes a pretty good one
249 nftw ( buffer, // path to descend
250 pnd_disco_callback, // callback to do processing
251 10, // no more than X open fd's at once
252 FTW_PHYS ); // do not follow symlinks
257 // return whatever we found, or NULL if nada
258 if ( ! pnd_box_get_head ( disco_box ) ) {
259 pnd_box_delete ( disco_box );
263 return ( disco_box );