Large change, to support multiple applications within a single PXML.xml file.
[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
6 #define __USE_GNU /* for strcasestr */
7 #include <string.h> /* for making ftw.h happy */
8
9 #define _XOPEN_SOURCE 500
10 #define __USE_XOPEN_EXTENDED
11 #include <ftw.h> /* for nftw, tree walker */
12
13 #include "pnd_container.h"
14 #include "pnd_pxml.h"
15 #include "pnd_discovery.h"
16 #include "pnd_pathiter.h"
17 #include "pnd_apps.h"
18 #include "pnd_pndfiles.h"
19 #include "pnd_logger.h"
20
21 // need these 'globals' due to the way nftw and ftw work :/
22 static pnd_box_handle disco_box;
23 static char *disco_overrides = NULL;
24
25 void pnd_disco_destroy ( pnd_disco_t *p ) {
26
27   if ( p -> title_en ) {       free ( p -> title_en );    }
28   if ( p -> unique_id ) {      free ( p -> unique_id );   }
29   if ( p -> icon )     {       free ( p -> icon );        }
30   if ( p -> exec )     {       free ( p -> exec );        }
31   if ( p -> clockspeed ) {     free ( p -> clockspeed );  }
32   if ( p -> startdir ) {       free ( p -> startdir );    }
33   if ( p -> option_no_x11 ) {  free ( p -> option_no_x11 );  }
34   if ( p -> main_category ) {  free ( p -> main_category );  }
35   if ( p -> main_category1 ) { free ( p -> main_category1 ); }
36   if ( p -> main_category2 ) { free ( p -> main_category2 ); }
37   if ( p -> alt_category ) {   free ( p -> alt_category );   }
38   if ( p -> alt_category1 ) {  free ( p -> alt_category1 );  }
39   if ( p -> alt_category2 ) {  free ( p -> alt_category2 );  }
40   if ( p -> mkdir_sp )      {  free ( p -> mkdir_sp );       }
41
42   return;
43 }
44
45 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
46                                 int typeflag, struct FTW *ftwbuf )
47 {
48   unsigned char valid = pnd_object_type_unknown;
49   pnd_pxml_handle pxmlh = 0;
50   pnd_pxml_handle *pxmlapps = NULL;
51   pnd_pxml_handle *pxmlappiter;
52   unsigned int pxml_close_pos = 0;
53   unsigned char logit = pnd_log_do_buried_logging();
54
55   if ( logit ) {
56     pnd_log ( PND_LOG_DEFAULT, "disco callback encountered '%s'\n", fpath );
57   }
58
59   // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
60   if ( typeflag & FTW_D ) {
61     if ( logit ) {
62       pnd_log ( PND_LOG_DEFAULT, " .. is dir, skipping\n" );
63     }
64     return ( 0 ); // skip directories and other non-regular files
65   }
66
67   // PND/PNZ file and others may be valid as well .. but lets leave that for now
68   //   printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
69   if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
70     valid = pnd_object_type_directory;
71   } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
72     valid = pnd_object_type_pnd;
73   }
74
75   // if not a file of interest, just keep looking until we run out
76   if ( ! valid ) {
77     if ( logit ) {
78       pnd_log ( PND_LOG_DEFAULT, " .. bad filename, skipping\n" );
79     }
80     return ( 0 );
81   }
82
83   // potentially a valid application
84   if ( valid == pnd_object_type_directory ) {
85     // Plaintext PXML file
86     //printf ( "PXML: disco callback encountered '%s'\n", fpath );
87
88     // pick up the PXML if we can
89     pxmlapps = pnd_pxml_fetch ( (char*) fpath );
90
91   } else if ( valid == pnd_object_type_pnd ) {
92     // PND ... ??
93     FILE *f;
94     char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
95
96     //printf ( "PND: disco callback encountered '%s'\n", fpath );
97
98     // is this a valid .pnd file? The filename is a candidate already, but verify..
99     // .. presence of PXML appended, or at least contained within?
100     // .. presence of an icon appended after PXML?
101
102     // open it up..
103     f = fopen ( fpath, "r" );
104
105     // try to locate the PXML portion
106     if ( ! pnd_pnd_seek_pxml ( f ) ) {
107       fclose ( f );
108       return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
109     }
110
111     // accrue it into a buffer
112     if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
113       fclose ( f );
114       return ( 0 );
115     }
116
117     //printf ( "buffer is %s\n", pxmlbuf );
118     //fflush ( stdout );
119
120 #if 1 // icon
121     // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
122     {
123       unsigned int pos = ftell ( f );
124       char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
125       pngbuffer [ 0 ] = 137;      pngbuffer [ 1 ] = 80;      pngbuffer [ 2 ] = 78;      pngbuffer [ 3 ] = 71;
126       pngbuffer [ 4 ] = 13;       pngbuffer [ 5 ] = 10;       pngbuffer [ 6 ] = 26;      pngbuffer [ 7 ] = 10;
127
128       unsigned char padtests = 10;
129       unsigned int padstart = ftell ( f );
130       while ( padtests ) {
131
132         if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
133           if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
134             pxml_close_pos = pos;
135             break;
136           } // if
137           fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
138         } // if read
139
140         padtests --;
141       } // while
142
143       if ( ! padtests ) {
144         // no icon found, so back to where we started looking
145         fseek ( f, padstart, SEEK_SET );
146       }
147
148     } // icon
149 #endif
150
151     // by now, we have <PXML> .. </PXML>, try to parse..
152     pxmlapps = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
153
154     // done with file
155     fclose ( f );
156
157   }
158
159   // pxmlh is useful?
160   if ( ! pxmlapps ) {
161     return ( 0 ); // continue tree walk
162   }
163
164   // iterate across apps in the PXML
165   pxmlappiter = pxmlapps;
166   while ( 1 ) {
167     pxmlh = *pxmlappiter;
168     pxmlappiter++;
169
170     if ( ! pxmlh ) {
171       break; // all done
172     }
173
174     // look for any overrides, if requested
175     pnd_pxml_merge_override ( pxmlh, disco_overrides );
176
177     // check for validity and add to resultset if it looks executable
178     if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
179       pnd_disco_t *p;
180       char *fixpxml;
181       char *z;
182
183       pnd_log ( PND_LOG_DEFAULT, "Setting up discovered app %u\n", ((pnd_pxml_t*) pxmlh) -> subapp_number );
184
185       p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
186
187       // base paths
188       p -> object_path = strdup ( fpath );
189
190       if ( ( fixpxml = strcasestr ( p -> object_path, PXML_FILENAME ) ) ) {
191         *fixpxml = '\0'; // if this is not a .pnd, lop off the PXML.xml at the end
192       } else if ( ( fixpxml = strrchr ( p -> object_path, '/' ) ) ) {
193         *(fixpxml+1) = '\0'; // for pnd, lop off to last /
194       }
195
196       if ( ( fixpxml = strrchr ( fpath, '/' ) ) ) {
197         p -> object_filename = strdup ( fixpxml + 1 );
198       }
199
200       // subapp-number
201       p -> subapp_number = ((pnd_pxml_t*) pxmlh) -> subapp_number;
202
203       // png icon path
204       p -> pnd_icon_pos = pxml_close_pos;
205
206       // type
207       p -> object_type = valid;
208
209       // PXML fields
210       if ( pnd_pxml_get_app_name_en ( pxmlh ) ) {
211         p -> title_en = strdup ( pnd_pxml_get_app_name_en ( pxmlh ) );
212       }
213       if ( pnd_pxml_get_description_en ( pxmlh ) ) {
214         p -> desc_en = strdup ( pnd_pxml_get_description_en ( pxmlh ) );
215       }
216       if ( pnd_pxml_get_icon ( pxmlh ) ) {
217         p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
218       }
219       if ( pnd_pxml_get_exec ( pxmlh ) ) {
220         p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
221       }
222       if ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) ) {
223         p -> option_no_x11 = strdup ( pnd_pxml_get_exec_option_no_x11 ( pxmlh ) );
224       }
225       if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
226         p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
227       }
228       if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
229         p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) ); 
230       }
231       if ( pnd_pxml_get_startdir ( pxmlh ) ) {
232         p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) ); 
233       }
234       // category kruft
235       if ( pnd_pxml_get_main_category ( pxmlh ) ) {
236         p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
237       }
238       if ( pnd_pxml_get_subcategory1 ( pxmlh ) ) {
239         p -> main_category1 = strdup ( pnd_pxml_get_subcategory1 ( pxmlh ) );
240       }
241       if ( pnd_pxml_get_subcategory2 ( pxmlh ) ) {
242         p -> main_category2 = strdup ( pnd_pxml_get_subcategory2 ( pxmlh ) );
243       }
244       if ( pnd_pxml_get_altcategory ( pxmlh ) ) {
245         p -> alt_category = strdup ( pnd_pxml_get_altcategory ( pxmlh ) );
246       }
247       if ( pnd_pxml_get_altsubcategory1 ( pxmlh ) ) {
248         p -> alt_category1 = strdup ( pnd_pxml_get_altsubcategory1 ( pxmlh ) );
249       }
250       if ( pnd_pxml_get_altsubcategory2 ( pxmlh ) ) {
251         p -> alt_category2 = strdup ( pnd_pxml_get_altsubcategory2 ( pxmlh ) );
252       }
253       // preview pics
254       if ( ( z = pnd_pxml_get_previewpic1 ( pxmlh ) ) ) {
255         p -> preview_pic1 = strdup ( z );
256       }
257       if ( ( z = pnd_pxml_get_previewpic2 ( pxmlh ) ) ) {
258         p -> preview_pic2 = strdup ( z );
259       }
260       // mkdirs
261       if ( pnd_pxml_get_mkdir ( pxmlh ) ) {
262         p -> mkdir_sp = strdup ( pnd_pxml_get_mkdir ( pxmlh ) );
263       }
264
265     } else {
266       //printf ( "Invalid PXML; skipping.\n" );
267     }
268
269     // ditch pxml
270     pnd_pxml_delete ( pxmlh );
271
272   } // while pxmlh is good
273
274   // free up the applist
275   free ( pxmlapps );
276
277   return ( 0 ); // continue the tree walk
278 }
279
280 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
281
282   //printf ( "Searchpath to discover: '%s'\n", searchpath );
283
284   // alloc a container for the result set
285   disco_box = pnd_box_new ( "discovery" );
286   disco_overrides = overridespath;
287
288   /* iterate across the paths within the searchpath, attempting to locate applications
289    */
290
291   SEARCHPATH_PRE
292   {
293
294     // invoke the dir walking function; thankfully Linux includes a pretty good one
295     nftw ( buffer,               // path to descend
296            pnd_disco_callback,   // callback to do processing
297            10,                   // no more than X open fd's at once
298            FTW_PHYS );           // do not follow symlinks
299
300   }
301   SEARCHPATH_POST
302
303   // return whatever we found, or NULL if nada
304   if ( ! pnd_box_get_head ( disco_box ) ) {
305     pnd_box_delete ( disco_box );
306     disco_box = NULL;
307   }
308
309   return ( disco_box );
310 }