New exec(disco-t) for more flexible pnd exec. PXML may specify appdata dirname, and...
[pandora-libraries.git] / lib / pnd_discovery.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #include <unistd.h> /* for unlink */
5 #include <limits.h> /* for PATH_MAX */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8
9 #define __USE_GNU /* for strcasestr */
10 #include <string.h> /* for making ftw.h happy */
11
12 #define _XOPEN_SOURCE 500
13 #define __USE_XOPEN_EXTENDED
14 #include <ftw.h> /* for nftw, tree walker */
15
16 #include "pnd_container.h"
17 #include "pnd_pxml.h"
18 #include "pnd_discovery.h"
19 #include "pnd_pathiter.h"
20 #include "pnd_apps.h"
21 #include "pnd_pndfiles.h"
22 #include "pnd_logger.h"
23 #include "pnd_conf.h"
24
25 // need these 'globals' due to the way nftw and ftw work :/
26 static pnd_box_handle disco_box;
27 static char *disco_overrides = NULL;
28
29 void pnd_disco_destroy ( pnd_disco_t *p ) {
30
31   if ( p -> title_en ) {       free ( p -> title_en );    }
32   if ( p -> unique_id ) {      free ( p -> unique_id );   }
33   if ( p -> appdata_dirname ) { free ( p -> appdata_dirname );   }
34   if ( p -> icon )     {       free ( p -> icon );        }
35   if ( p -> exec )     {       free ( p -> exec );        }
36   if ( p -> execargs ) {       free ( p -> execargs );    }
37   if ( p -> clockspeed ) {     free ( p -> clockspeed );  }
38   if ( p -> startdir ) {       free ( p -> startdir );    }
39   if ( p -> option_no_x11 ) {  free ( p -> option_no_x11 );  }
40   if ( p -> main_category ) {  free ( p -> main_category );  }
41   if ( p -> main_category1 ) { free ( p -> main_category1 ); }
42   if ( p -> main_category2 ) { free ( p -> main_category2 ); }
43   if ( p -> alt_category ) {   free ( p -> alt_category );   }
44   if ( p -> alt_category1 ) {  free ( p -> alt_category1 );  }
45   if ( p -> alt_category2 ) {  free ( p -> alt_category2 );  }
46   if ( p -> mkdir_sp )      {  free ( p -> mkdir_sp );       }
47   if ( p -> info_name )     {  free ( p -> info_name );       }
48   if ( p -> info_type )     {  free ( p -> info_type );       }
49   if ( p -> info_filename ) {  free ( p -> info_filename );       }
50
51   return;
52 }
53
54 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
55                                 int typeflag, struct FTW *ftwbuf )
56 {
57   unsigned char valid = pnd_object_type_unknown;
58   pnd_pxml_handle pxmlh = 0;
59   pnd_pxml_handle *pxmlapps = NULL;
60   pnd_pxml_handle *pxmlappiter;
61   unsigned int pxml_close_pos = 0;
62   unsigned char logit = pnd_log_do_buried_logging();
63
64   if ( logit ) {
65     pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
66   }
67
68   // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
69   if ( typeflag & FTW_D ) {
70     if ( logit ) {
71       pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
72     }
73     return ( 0 ); // skip directories and other non-regular files
74   }
75
76   // PND/PNZ file and others may be valid as well .. but lets leave that for now
77   //   printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
78   if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
79     valid = pnd_object_type_directory;
80   } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
81     valid = pnd_object_type_pnd;
82   }
83
84   // if not a file of interest, just keep looking until we run out
85   if ( ! valid ) {
86     if ( logit ) {
87       pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
88     }
89     return ( 0 );
90   }
91
92   // potentially a valid application
93   if ( valid == pnd_object_type_directory ) {
94     // Plaintext PXML file
95     //printf ( "PXML: disco callback encountered '%s'\n", fpath );
96
97     // pick up the PXML if we can
98     pxmlapps = pnd_pxml_fetch ( (char*) fpath );
99
100   } else if ( valid == pnd_object_type_pnd ) {
101     // PND ... ??
102     FILE *f;
103     char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
104
105     //printf ( "PND: disco callback encountered '%s'\n", fpath );
106
107     // is this a valid .pnd file? The filename is a candidate already, but verify..
108     // .. presence of PXML appended, or at least contained within?
109     // .. presence of an icon appended after PXML?
110
111     // open it up..
112     f = fopen ( fpath, "r" );
113
114     // try to locate the PXML portion
115     if ( ! pnd_pnd_seek_pxml ( f ) ) {
116       fclose ( f );
117       return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
118     }
119
120     // accrue it into a buffer
121     if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
122       fclose ( f );
123       return ( 0 );
124     }
125
126     //printf ( "buffer is %s\n", pxmlbuf );
127     //fflush ( stdout );
128
129 #if 1 // icon
130     // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
131     {
132       unsigned int pos = ftell ( f );
133       char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
134       pngbuffer [ 0 ] = 137;      pngbuffer [ 1 ] = 80;      pngbuffer [ 2 ] = 78;      pngbuffer [ 3 ] = 71;
135       pngbuffer [ 4 ] = 13;       pngbuffer [ 5 ] = 10;       pngbuffer [ 6 ] = 26;      pngbuffer [ 7 ] = 10;
136
137       unsigned char padtests = 10;
138       unsigned int padstart = ftell ( f );
139       while ( padtests ) {
140
141         if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
142           if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
143             pxml_close_pos = pos;
144             break;
145           } // if
146           fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
147         } // if read
148
149         padtests --;
150       } // while
151
152       if ( ! padtests ) {
153         // no icon found, so back to where we started looking
154         fseek ( f, padstart, SEEK_SET );
155       }
156
157     } // icon
158 #endif
159
160     // by now, we have <PXML> .. </PXML>, try to parse..
161     pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
162
163     // done with file
164     fclose ( f );
165
166   }
167
168   // pxmlh is useful?
169   if ( ! pxmlapps ) {
170     return ( 0 ); // continue tree walk
171   }
172
173   // for ovr-file
174   char ovrfile [ PATH_MAX ];
175   pnd_box_handle ovrh = 0; // 0 didn't try, -1 tried and failed, >0 tried and got
176
177   // iterate across apps in the PXML
178   pxmlappiter = pxmlapps;
179   while ( 1 ) {
180     pxmlh = *pxmlappiter;
181     pxmlappiter++;
182
183     if ( ! pxmlh ) {
184       break; // all done
185     }
186
187     // check for validity and add to resultset if it looks executable
188     if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
189       pnd_disco_t *p;
190       char *fixpxml;
191       char *z;
192
193       //pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
194
195       p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
196
197       // base paths
198       p -> object_path = strdup ( fpath );
199
200       if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
201         *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
202       } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
203         *(fixpxml+1) = '\0'; // for pnd, lop off to last /
204       }
205
206       if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
207         p -> object_filename = strdup ( fixpxml + 1 );
208       }
209
210       // subapp-number
211       p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
212
213       // png icon path
214       p -> pnd_icon_pos = pxml_close_pos;
215
216       // type
217       p -> object_type = valid;
218
219       // PXML fields
220       if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
221         p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
222       }
223       if ( pnd_pxml_get_description_en ( pxmlh ) ) {
224         p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
225       }
226       if ( pnd_pxml_get_icon ( pxmlh ) ) {
227         p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
228       }
229       if ( pnd_pxml_get_exec ( pxmlh ) ) {
230         p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
231       }
232       if ( pnd_pxml_get_execargs ( pxmlh ) ) {
233         p -> execargs = strdup ( pnd_pxml_get_execargs ( pxmlh ) );
234       }
235       if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
236         p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
237       }
238       if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
239         p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
240       }
241       if ( pnd_pxml_get_appdata_dirname ( pxmlh ) ) {
242         p -> appdata_dirname = strdup ( pnd_pxml_get_appdata_dirname ( pxmlh ) );
243       }
244       if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
245         p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) ); 
246       }
247       if ( pnd_pxml_get_startdir ( pxmlh ) ) {
248         p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) ); 
249       }
250       // category kruft
251       if ( pnd_pxml_get_main_category ( pxmlh ) ) {
252         p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
253       }
254       if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
255         p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
256       }
257       if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
258         p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
259       }
260       if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
261         p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
262       }
263       if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
264         p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
265       }
266       if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
267         p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
268       }
269       // preview pics
270       if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
271         p -> preview_pic1 = strdup ( z );
272       }
273       if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
274         p -> preview_pic2 = strdup ( z );
275       }
276       // mkdirs
277       if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
278         p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
279       }
280       // info
281       if ( pnd_pxml_get_info_src ( pxmlh ) ) {
282         p -> info_filename = strdup ( pnd_pxml_get_info_src ( pxmlh ) );
283       }
284       if ( pnd_pxml_get_info_name ( pxmlh ) ) {
285         p -> info_name = strdup ( pnd_pxml_get_info_name ( pxmlh ) );
286       }
287       if ( pnd_pxml_get_info_type ( pxmlh ) ) {
288         p -> info_type = strdup ( pnd_pxml_get_info_type ( pxmlh ) );
289       }
290
291       // look for any PXML overrides, if requested
292       if ( disco_overrides ) {
293         pnd_pxml_merge_override ( pxmlh, disco_overrides );
294       }
295
296       // handle ovr overrides
297       // try to load a same-path-as-pnd override file
298       if ( ovrh == 0 ) {
299         sprintf ( ovrfile, "%s/%s", p -> object_path, p -> object_filename );
300         fixpxml = strcasestr ( ovrfile, PND_PACKAGE_FILEEXT );
301         if ( fixpxml ) {
302           strcpy ( fixpxml, PXML_SAMEPATH_OVERRIDE_FILEEXT );
303           struct stat statbuf;
304           if ( stat ( ovrfile, &statbuf ) == 0 ) {
305             ovrh = pnd_conf_fetch_by_path ( ovrfile );
306
307             if ( ! ovrh ) {
308               // couldn't pull conf out of file, so don't try again
309               ovrh = (void*)(-1);
310             }
311
312           } else {
313             ovrh = (void*)(-1); // not found, don't try again
314           } // stat
315         } // can find .pnd
316       } // tried ovr yet?
317
318       // is ovr file open?
319       if ( ovrh != 0 && ovrh != (void*)(-1) ) {
320         // pull in appropriate values
321         char key [ 100 ];
322         char *v;
323
324         // set the flag regardless, so its for all subapps
325         p -> object_flags |= PND_DISCO_FLAG_OVR;
326
327         // title
328         snprintf ( key, 100, "Application-%u.title", p -> subapp_number );
329         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
330           if ( p -> title_en ) {
331             free ( p -> title_en );
332           }
333           p -> title_en = strdup ( v );
334         }
335
336         // clockspeed
337         snprintf ( key, 100, "Application-%u.clockspeed", p -> subapp_number );
338         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
339           if ( p -> clockspeed ) {
340             free ( p -> clockspeed );
341           }
342           p -> clockspeed = strdup ( v );
343         }
344
345         // appdata dirname
346         snprintf ( key, 100, "Application-%u.appdata", p -> subapp_number );
347         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
348           if ( p -> appdata_dirname ) {
349             free ( p -> appdata_dirname );
350           }
351           p -> appdata_dirname = strdup ( v );
352         }
353
354         // categories
355         snprintf ( key, 100, "Application-%u.maincategory", p -> subapp_number );
356         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
357           if ( p -> main_category ) {
358             free ( p -> main_category );
359           }
360           p -> main_category = strdup ( v );
361         }
362         snprintf ( key, 100, "Application-%u.maincategorysub1", p -> subapp_number );
363         if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
364           if ( p -> main_category1 ) {
365             free ( p -> main_category1 );
366           }
367           p -> main_category1 = strdup ( v );
368         }
369
370       } // got ovr conf loaded?
371
372     } else {
373       //printf ( "Invalid PXML; skipping.\n" );
374     }
375
376     // ditch pxml
377     pnd_pxml_delete ( pxmlh );
378
379   } // while pxmlh is good
380
381   // free up ovr
382   if ( ovrh != 0 && ovrh != (void*)(-1) ) {
383     pnd_box_delete ( ovrh );
384   }
385
386   // free up the applist
387   free ( pxmlapps );
388
389   return ( 0 ); // continue the tree walk
390 }
391
392 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
393
394   //printf ( "Searchpath to discover: '%s'\n", searchpath );
395
396   // alloc a container for the result set
397   disco_box = pnd_box_new ( "discovery" );
398   disco_overrides = overridespath;
399
400   /* iterate across the paths within the searchpath, attempting to locate applications
401    */
402
403   SEARCHPATH_PRE
404   {
405
406     // invoke the dir walking function; thankfully Linux includes a pretty good one
407     nftw ( buffer,               // path to descend
408            pnd_disco_callback,   // callback to do processing
409            10,                   // no more than X open fd's at once
410            FTW_PHYS );           // do not follow symlinks
411
412   }
413   SEARCHPATH_POST
414
415   // return whatever we found, or NULL if nada
416   if ( ! pnd_box_get_head ( disco_box ) ) {
417     pnd_box_delete ( disco_box );
418     disco_box = NULL;
419   }
420
421   return ( disco_box );
422 }