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