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_unique_id ( pxmlh ) ) {
185 p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
187 if ( pnd_pxml_get_main_category ( pxmlh ) ) {
188 p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
190 if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
191 p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) );
193 if ( pnd_pxml_get_startdir ( pxmlh ) ) {
194 p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) );
198 //printf ( "Invalid PXML; skipping.\n" );
202 pnd_pxml_delete ( pxmlh );
206 return ( 0 ); // continue the tree walk
209 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
211 //printf ( "Searchpath to discover: '%s'\n", searchpath );
213 // alloc a container for the result set
214 disco_box = pnd_box_new ( "discovery" );
215 disco_overrides = overridespath;
217 /* iterate across the paths within the searchpath, attempting to locate applications
223 // invoke the dir walking function; thankfully Linux includes a pretty good one
224 nftw ( buffer, // path to descend
225 pnd_disco_callback, // callback to do processing
226 10, // no more than X open fd's at once
227 FTW_PHYS ); // do not follow symlinks
232 // return whatever we found, or NULL if nada
233 if ( ! pnd_box_get_head ( disco_box ) ) {
234 pnd_box_delete ( disco_box );
238 return ( disco_box );
241 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
242 char filename [ FILENAME_MAX ];
243 char buffer [ 1024 ];
247 // http://standards.freedesktop.org/desktop-entry-spec
251 if ( ! p -> unique_id ) {
259 if ( ! targetpath ) {
269 sprintf ( filename, "%s/%s.desktop", targetpath, p -> unique_id );
273 //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
275 f = fopen ( filename, "w" );
281 fprintf ( f, "%s\n", PND_DOTDESKTOP_HEADER );
283 if ( p -> title_en ) {
284 snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
285 fprintf ( f, "%s", buffer );
288 fprintf ( f, "Type=Application\n" );
289 fprintf ( f, "Version=1.0\n" );
292 snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
293 fprintf ( f, "%s", buffer );
297 if ( p -> description_en ) {
298 snprintf ( buffer, 1020, "Comment=%s\n", p -> description_en );
299 fprintf ( f, "%s", buffer );
303 #if 0 // we let pnd_run.sh handle this
304 if ( p -> startdir ) {
305 snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
306 fprintf ( f, "%s", buffer );
308 fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
315 if ( p -> object_type == pnd_object_type_directory ) {
316 snprintf ( buffer, 1020, "Exec=%s -p %s -e %s -n", pndrun, p -> object_path, p -> exec );
317 } else if ( p -> object_type == pnd_object_type_pnd ) {
318 snprintf ( buffer, 1020, "Exec=%s -p %s/%s -e %s -n", pndrun, p -> object_path, p -> object_filename, p -> exec );
322 if ( p -> startdir ) {
323 strncat ( buffer, " -s ", 1020 );
324 strncat ( buffer, p -> startdir, 1020 );
328 strncat ( buffer, "\n", 1020 );
331 fprintf ( f, "%s", buffer );
335 fprintf ( f, "%s\n", "Categories=Application;Network;" );
338 fprintf ( f, "%s\n", PND_DOTDESKTOP_SOURCE ); // should we need to know 'who' created the file during trimming
345 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
346 char buffer [ FILENAME_MAX ]; // target filename
347 char from [ FILENAME_MAX ]; // source filename
348 char bits [ 8 * 1024 ];
352 // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
353 if ( ( p -> object_type == pnd_object_type_pnd ) &&
354 ( ! p -> pnd_icon_pos ) )
356 return ( 0 ); // discover code didn't find it, so FAIL
359 // determine filename for target
360 sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
362 /* first.. open the source file, by type of application:
363 * are we looking through a pnd file or a dir?
365 if ( p -> object_type == pnd_object_type_directory ) {
366 sprintf ( from, "%s/%s", p -> object_path, p -> icon );
367 } else if ( p -> object_type == pnd_object_type_pnd ) {
368 sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
371 pnd = fopen ( from, "r" );
379 target = fopen ( buffer, "wb" );
386 fseek ( pnd, 0, SEEK_END );
388 //fseek ( pnd, 0, SEEK_SET );
390 fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
392 len -= p -> pnd_icon_pos;
396 if ( len > (8*1024) ) {
402 if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
409 if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {