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