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