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 ) {
27 free ( p -> title_en );
38 if ( p -> unique_id ) {
39 free ( p -> unique_id );
42 if ( p -> main_category ) {
43 free ( p -> main_category );
46 if ( p -> clockspeed ) {
47 free ( p -> clockspeed );
53 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
54 int typeflag, struct FTW *ftwbuf )
56 unsigned char valid = pnd_object_type_unknown;
57 pnd_pxml_handle pxmlh = 0;
58 unsigned int pxml_close_pos = 0;
60 //printf ( "disco root callback encountered '%s'\n", fpath );
62 // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
63 if ( typeflag & FTW_D ) {
64 //printf ( " .. is dir, skipping\n" );
65 return ( 0 ); // skip directories and other non-regular files
68 // PND/PNZ file and others may be valid as well .. but lets leave that for now
69 // printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
70 if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
71 valid = pnd_object_type_directory;
72 } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
73 valid = pnd_object_type_pnd;
76 // if not a file of interest, just keep looking until we run out
78 //printf ( " .. bad filename, skipping\n" );
82 // potentially a valid application
83 if ( valid == pnd_object_type_directory ) {
84 // Plaintext PXML file
85 //printf ( "PXML: disco callback encountered '%s'\n", fpath );
87 // pick up the PXML if we can
88 pxmlh = pnd_pxml_fetch ( (char*) fpath );
90 } else if ( valid == pnd_object_type_pnd ) {
93 char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
95 //printf ( "PND: disco callback encountered '%s'\n", fpath );
97 // is this a valid .pnd file? The filename is a candidate already, but verify..
98 // .. presence of PXML appended, or at least contained within?
99 // .. presence of an icon appended after PXML?
102 f = fopen ( fpath, "r" );
104 // try to locate the PXML portion
105 if ( ! pnd_pnd_seek_pxml ( f ) ) {
107 return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
110 // accrue it into a buffer
111 if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
116 //printf ( "buffer is %s\n", pxmlbuf );
120 // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
122 unsigned int pos = ftell ( f );
123 char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
124 pngbuffer [ 0 ] = 137; pngbuffer [ 1 ] = 80; pngbuffer [ 2 ] = 78; pngbuffer [ 3 ] = 71;
125 pngbuffer [ 4 ] = 13; pngbuffer [ 5 ] = 10; pngbuffer [ 6 ] = 26; pngbuffer [ 7 ] = 10;
126 if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
127 if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
128 pxml_close_pos = pos;
134 // by now, we have <PXML> .. </PXML>, try to parse..
135 pxmlh = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
145 // look for any overrides, if requested
146 pnd_pxml_merge_override ( pxmlh, disco_overrides );
148 // check for validity and add to resultset if it looks executable
149 if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
153 p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
156 p -> object_path = strdup ( fpath );
158 if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
159 *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
160 } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
161 *(fixpxml+1) = '\0'; // for pnd, lop off to last /
164 if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
165 p -> object_filename = strdup ( fixpxml + 1 );
169 p -> pnd_icon_pos = pxml_close_pos;
172 p -> object_type = valid;
175 if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
176 p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
178 if ( pnd_pxml_get_icon ( pxmlh ) ) {
179 p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
181 if ( pnd_pxml_get_exec ( pxmlh ) ) {
182 p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
184 if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
185 p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
187 if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
188 p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
190 if ( pnd_pxml_get_main_category ( pxmlh ) ) {
191 p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
193 if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
194 p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
196 if ( pnd_pxml_get_startdir ( pxmlh ) ) {
197 p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
201 //printf ( "Invalid PXML; skipping.\n" );
205 pnd_pxml_delete ( pxmlh );
209 return ( 0 ); // continue the tree walk
212 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
214 //printf ( "Searchpath to discover: '%s'\n", searchpath );
216 // alloc a container for the result set
217 disco_box = pnd_box_new ( "discovery" );
218 disco_overrides = overridespath;
220 /* iterate across the paths within the searchpath, attempting to locate applications
226 // invoke the dir walking function; thankfully Linux includes a pretty good one
227 nftw ( buffer, // path to descend
228 pnd_disco_callback, // callback to do processing
229 10, // no more than X open fd's at once
230 FTW_PHYS ); // do not follow symlinks
235 // return whatever we found, or NULL if nada
236 if ( ! pnd_box_get_head ( disco_box ) ) {
237 pnd_box_delete ( disco_box );
241 return ( disco_box );