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