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