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