Split disco_t -> path_to_object into object_path and object_filename so it is more...
[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 ) {
27     free ( p -> title_en );
28   }
29
30   if ( p -> icon ) {
31     free ( p -> icon );
32   }
33
34   if ( p -> exec ) {
35     free ( p -> exec );
36   }
37
38   if ( p -> unique_id ) {
39     free ( p -> unique_id );
40   }
41
42   if ( p -> main_category ) {
43     free ( p -> main_category );
44   }
45
46   if ( p -> clockspeed ) {
47     free ( p -> clockspeed );
48   }
49
50   return;
51 }
52
53 static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
54                                 int typeflag, struct FTW *ftwbuf )
55 {
56   unsigned char valid = pnd_object_type_unknown;
57   pnd_pxml_handle pxmlh = 0;
58   unsigned int pxml_close_pos = 0;
59
60   //printf ( "disco root callback encountered '%s'\n", fpath );
61
62   // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
63   if ( typeflag & FTW_D ) {
64     //printf ( " .. is dir, skipping\n" );
65     return ( 0 ); // skip directories and other non-regular files
66   }
67
68   // PND/PNZ file and others may be valid as well .. but lets leave that for now
69   //   printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
70   if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
71     valid = pnd_object_type_directory;
72   } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
73     valid = pnd_object_type_pnd;
74   }
75
76   // if not a file of interest, just keep looking until we run out
77   if ( ! valid ) {
78     //printf ( " .. bad filename, skipping\n" );
79     return ( 0 );
80   }
81
82   // potentially a valid application
83   if ( valid == pnd_object_type_directory ) {
84     // Plaintext PXML file
85     //printf ( "PXML: disco callback encountered '%s'\n", fpath );
86
87     // pick up the PXML if we can
88     pxmlh = pnd_pxml_fetch ( (char*) fpath );
89
90   } else if ( valid == pnd_object_type_pnd ) {
91     // PND ... ??
92     FILE *f;
93     char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
94
95     //printf ( "PND: disco callback encountered '%s'\n", fpath );
96
97     // is this a valid .pnd file? The filename is a candidate already, but verify..
98     // .. presence of PXML appended, or at least contained within?
99     // .. presence of an icon appended after PXML?
100
101     // open it up..
102     f = fopen ( fpath, "r" );
103
104     // try to locate the PXML portion
105     if ( ! pnd_pnd_seek_pxml ( f ) ) {
106       fclose ( f );
107       return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
108     }
109
110     // accrue it into a buffer
111     if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
112       fclose ( f );
113       return ( 0 );
114     }
115
116     //printf ( "buffer is %s\n", pxmlbuf );
117     //fflush ( stdout );
118
119 #if 1 // icon
120     // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
121     {
122       unsigned int pos = ftell ( f );
123       char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
124       pngbuffer [ 0 ] = 137;      pngbuffer [ 1 ] = 80;      pngbuffer [ 2 ] = 78;      pngbuffer [ 3 ] = 71;
125       pngbuffer [ 4 ] = 13;       pngbuffer [ 5 ] = 10;       pngbuffer [ 6 ] = 26;      pngbuffer [ 7 ] = 10;
126       if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
127         if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
128           pxml_close_pos = pos;
129         }
130       }
131     } // icon
132 #endif
133
134     // by now, we have <PXML> .. </PXML>, try to parse..
135     pxmlh = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
136
137     // done with file
138     fclose ( f );
139
140   }
141
142   // pxmlh is useful?
143   if ( pxmlh ) {
144
145     // look for any overrides, if requested
146     pnd_pxml_merge_override ( pxmlh, disco_overrides );
147
148     // check for validity and add to resultset if it looks executable
149     if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
150       pnd_disco_t *p;
151       char *fixpxml;
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_icon ( pxmlh ) ) {
179         p -> icon = strdup ( pnd_pxml_get_icon ( pxmlh ) );
180       }
181       if ( pnd_pxml_get_exec ( pxmlh ) ) {
182         p -> exec = strdup ( pnd_pxml_get_exec ( pxmlh ) );
183       }
184       if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
185         p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
186       }
187       if ( pnd_pxml_get_main_category ( pxmlh ) ) {
188         p -> main_category = strdup ( pnd_pxml_get_main_category ( pxmlh ) );
189       }
190       if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
191         p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) ); 
192       }
193       if ( pnd_pxml_get_startdir ( pxmlh ) ) {
194         p -> startdir = strdup ( pnd_pxml_get_startdir ( pxmlh ) ); 
195       }
196
197     } else {
198       //printf ( "Invalid PXML; skipping.\n" );
199     }
200
201     // ditch pxml
202     pnd_pxml_delete ( pxmlh );
203
204   } // got a pxmlh
205
206   return ( 0 ); // continue the tree walk
207 }
208
209 pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
210
211   //printf ( "Searchpath to discover: '%s'\n", searchpath );
212
213   // alloc a container for the result set
214   disco_box = pnd_box_new ( "discovery" );
215   disco_overrides = overridespath;
216
217   /* iterate across the paths within the searchpath, attempting to locate applications
218    */
219
220   SEARCHPATH_PRE
221   {
222
223     // invoke the dir walking function; thankfully Linux includes a pretty good one
224     nftw ( buffer,               // path to descend
225            pnd_disco_callback,   // callback to do processing
226            10,                   // no more than X open fd's at once
227            FTW_PHYS );           // do not follow symlinks
228
229   }
230   SEARCHPATH_POST
231
232   // return whatever we found, or NULL if nada
233   if ( ! pnd_box_get_head ( disco_box ) ) {
234     pnd_box_delete ( disco_box );
235     disco_box = NULL;
236   }
237
238   return ( disco_box );
239 }
240
241 unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t *p ) {
242   char filename [ FILENAME_MAX ];
243   char buffer [ 1024 ];
244   FILE *f;
245
246   // specification
247   // http://standards.freedesktop.org/desktop-entry-spec
248
249   // validation
250
251   if ( ! p -> unique_id ) {
252     return ( 0 );
253   }
254
255   if ( ! p -> exec ) {
256     return ( 0 );
257   }
258
259   // set up
260
261   sprintf ( filename, "%s/%s.desktop", targetpath, p -> unique_id );
262
263   // emit
264
265   //printf ( "EMIT DOTDESKTOP '%s'\n", filename );
266
267   f = fopen ( filename, "w" );
268
269   if ( ! f ) {
270     return ( 0 );
271   }
272
273   if ( p -> title_en ) {
274     snprintf ( buffer, 1020, "Name=%s\n", p -> title_en );
275     fprintf ( f, "%s", buffer );
276   }
277
278   fprintf ( f, "Type=Application\n" );
279   fprintf ( f, "Version=1.0\n" );
280
281   if ( p -> icon ) {
282     snprintf ( buffer, 1020, "Icon=%s\n", p -> icon );
283     fprintf ( f, "%s", buffer );
284   }
285
286 #if 0
287   if ( p -> description_en ) {
288     snprintf ( buffer, 1020, "Comment=%s\n", p -> description_en );
289     fprintf ( f, "%s", buffer );
290   }
291 #endif
292
293 #if 0
294   if ( p -> startdir ) {
295     snprintf ( buffer, 1020, "Path=%s\n", p -> startdir );
296     fprintf ( f, "%s", buffer );
297   } else {
298     fprintf ( f, "Path=%s\n", PND_DEFAULT_WORKDIR );
299   }
300 #endif
301
302   if ( p -> exec ) {
303     if ( p -> object_type == pnd_object_type_directory ) {
304       snprintf ( buffer, 1020, "Exec=%s -p %s -e %s -u\n", pndrun, p -> object_path, p -> exec );
305     } else if ( p -> object_type == pnd_object_type_pnd ) {
306       snprintf ( buffer, 1020, "Exec=%s -p %s/%s -e %s -u\n", pndrun, p -> object_path, p -> object_filename, p -> exec );
307     }
308     fprintf ( f, "%s", buffer );
309   }
310
311   fprintf ( f, "_Source=libpnd\n" ); // should we need to know 'who' created the file during trimming
312
313   fclose ( f );
314
315   return ( 1 );
316 }
317
318 unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p ) {
319   char buffer [ FILENAME_MAX ]; // target filename
320   char from [ FILENAME_MAX ];   // source filename
321   char bits [ 8 * 1024 ];
322   unsigned int bitlen;
323   FILE *pnd, *target;
324
325   // prelim
326   if ( ( p -> object_type == pnd_object_type_pnd ) &&
327        ( ! p -> pnd_icon_pos ) )
328   {
329     return ( 0 ); // discover code didn't find it, so FAIL
330   }
331
332   // filenames
333   sprintf ( buffer, "%s/%s.png", targetpath, p -> unique_id ); // target
334
335   // first.. are we looking through a pnd file or a dir?
336   if ( p -> object_type == pnd_object_type_directory ) {
337     // if we can find icon, copy it in from directory to destination
338
339   } else if ( p -> object_type == pnd_object_type_pnd ) {
340     // if we can get it from pnd file, copy it into destination
341
342     unsigned int len;
343
344     sprintf ( from, "%s/%s", p -> object_path, p -> object_filename ); // target
345
346     pnd = fopen ( from, "r" );
347
348     if ( ! pnd ) {
349       return ( 0 );
350     }
351
352     target = fopen ( buffer, "wb" );
353
354     if ( ! target ) {
355       fclose ( pnd );
356       return ( 0 );
357     }
358
359     fseek ( pnd, 0, SEEK_END );
360     len = ftell ( pnd );
361     //fseek ( pnd, 0, SEEK_SET );
362
363     fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
364
365     len -= p -> pnd_icon_pos;
366
367     while ( len ) {
368
369       if ( len > (8*1024) ) {
370         bitlen = (8*1024);
371       } else {
372         bitlen = len;
373       }
374
375       if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
376         fclose ( pnd );
377         fclose ( target );
378         unlink ( buffer );
379         return ( 0 );
380       }
381
382       if ( fwrite ( bits, bitlen, 1, target ) != 1 ) {
383         fclose ( pnd );
384         fclose ( target );
385         unlink ( buffer );
386         return ( 0 );
387       }
388
389       len -= bitlen;
390     } // while
391
392     fclose ( pnd );
393     fclose ( target );
394
395   }
396
397   return ( 1 );
398 }