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