03e03a86f065f1e0fbaafcad2cdf14cafc9d9c31
[openwrt.git] / package / opkg / patches / 008-fix_parsing_insanity.patch
1 --- a/libopkg/opkg_utils.c
2 +++ b/libopkg/opkg_utils.c
3 @@ -44,58 +44,6 @@
4      return 0;
5  }
6  
7 -char **read_raw_pkgs_from_file(const char *file_name)
8 -{
9 -     FILE *fp; 
10 -     char **ret;
11 -    
12 -     if(!(fp = fopen(file_name, "r"))){
13 -         fprintf(stderr, "can't get %s open for read\n", file_name);
14 -         return NULL;
15 -     }
16 -
17 -     ret = read_raw_pkgs_from_stream(fp);
18 -
19 -     fclose(fp);
20 -
21 -     return ret;
22 -}
23 -
24 -char **read_raw_pkgs_from_stream(FILE *fp)
25 -{    
26 -     char **raw = NULL, *buf, *scout;
27 -     int count = 0;
28 -     size_t size = 512;
29 -     
30 -     buf = calloc (1, size);
31 -
32 -     while (fgets(buf, size, fp)) {
33 -         while (strlen (buf) == (size - 1)
34 -                && buf[size-2] != '\n') {
35 -              size_t o = size - 1;
36 -              size *= 2;
37 -              buf = realloc (buf, size);
38 -              if (fgets (buf + o, size - o, fp) == NULL)
39 -                   break;
40 -         }
41 -         
42 -         if(!(count % 50))
43 -              raw = realloc(raw, (count + 50) * sizeof(char *));
44 -       
45 -         if((scout = strchr(buf, '\n')))
46 -              *scout = '\0';
47 -
48 -         raw[count++] = strdup(buf);
49 -     }
50 -    
51 -     raw = realloc(raw, (count + 1) * sizeof(char *));
52 -     raw[count] = NULL;
53 -
54 -     free (buf);
55 -    
56 -     return raw;
57 -}
58 -
59  /* something to remove whitespace, a hash pooper */
60  char *trim_alloc(char *line)
61  {
62 --- a/libopkg/pkg.c
63 +++ b/libopkg/pkg.c
64 @@ -20,6 +20,8 @@
65  #include <alloca.h>
66  #include <string.h>
67  #include <stdbool.h>
68 +#include <unistd.h>
69 +#include <fcntl.h>
70  #include <errno.h>
71  
72  #include "pkg.h"
73 @@ -277,7 +279,6 @@
74  int pkg_init_from_file(pkg_t *pkg, const char *filename)
75  {
76       int err;
77 -     char **raw;
78       FILE *control_file;
79  
80       err = pkg_init(pkg);
81 @@ -290,8 +291,7 @@
82       if (err) { return err; }
83  
84       rewind(control_file);
85 -     raw = read_raw_pkgs_from_stream(control_file);
86 -     pkg_parse_raw(pkg, &raw, NULL, NULL);
87 +     pkg_parse_fd(pkg, fileno(control_file), NULL, NULL, 0);
88  
89       fclose(control_file);
90  
91 @@ -459,8 +459,7 @@
92  
93  void set_flags_from_control(opkg_conf_t *conf, pkg_t *pkg){
94       char * temp_str;
95 -     char **raw =NULL;
96 -     char **raw_start=NULL; 
97 +     int fd;
98  
99       size_t str_size = strlen(pkg->dest->info_dir)+strlen(pkg->name)+12;
100       temp_str = (char *) alloca (str_size);
101 @@ -471,28 +470,23 @@
102          return;
103       }
104       sprintf( temp_str,"%s/%s.control",pkg->dest->info_dir,pkg->name);
105 -   
106 -     raw = raw_start = read_raw_pkgs_from_file(temp_str);
107 -     if (raw == NULL ){
108 -        opkg_message(conf, OPKG_ERROR, "Unable to open the control file in  %s\n", __FUNCTION__);
109 -        return;
110 -     }
111  
112 -     while(*raw){
113 -        if (!pkg_valorize_other_field(pkg, &raw ) == 0) {
114 -            opkg_message(conf, OPKG_DEBUG, "unable to read control file for %s. May be empty\n", pkg->name);
115 -        }
116 -     }
117 -     raw = raw_start;
118 -     while (*raw) {
119 -        if (raw!=NULL)
120 -          free(*raw++);
121 -     }
122 +       if( (fd = open(temp_str, O_RDONLY)) > 0 )
123 +       {
124 +               if( pkg_valorize_other_field(pkg, fd) )
125 +               {
126 +                       opkg_message(conf, OPKG_DEBUG, "unable to read control file for %s. May be empty\n", pkg->name);
127 +               }
128  
129 -     free(raw_start); 
130 +               close(fd);
131 +       }
132 +       else
133 +       {
134 +               opkg_message(conf, OPKG_ERROR, "Unable to open the control file in  %s\n", __FUNCTION__);
135 +               return;
136 +       }
137  
138       return ;
139 -
140  }
141  
142  #define CHECK_BUFF_SIZE(buff, line, buf_size, page_size) do { \
143 --- a/libopkg/pkg_hash.c
144 +++ b/libopkg/pkg_hash.c
145 @@ -20,6 +20,8 @@
146  #include <ctype.h>
147  #include <stdlib.h>
148  #include <string.h>
149 +#include <unistd.h>
150 +#include <fcntl.h>
151  
152  #include "hash_table.h"
153  #include "pkg.h"
154 @@ -110,45 +112,52 @@
155  }
156  
157  int pkg_hash_add_from_file(opkg_conf_t *conf, const char *file_name,
158 -                          pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
159 +                          pkg_src_t *src, pkg_dest_t *dest, int is_status_file, int no_desc)
160  {
161 -     hash_table_t *hash = &conf->pkg_hash;
162 -     char **raw;
163 -     char **raw_start;
164 -     pkg_t *pkg;
165 -    
166 -     raw = raw_start = read_raw_pkgs_from_file(file_name);
167 -     if (!raw)
168 -        return -ENOMEM;
169 -
170 -     while(*raw){         /* don't worry, we'll increment raw in the parsing function */
171 -         pkg = pkg_new();
172 -         if (!pkg)
173 -              return -ENOMEM;
174 -
175 -         if (pkg_parse_raw(pkg, &raw, src, dest) == 0) {
176 -              if (!pkg->architecture) {
177 -                   char *version_str = pkg_version_str_alloc(pkg);
178 -                   pkg->architecture = pkg_get_default_arch(conf);
179 -                   opkg_message(conf, OPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
180 -                                pkg->name, version_str, pkg->architecture);
181 -                   free(version_str);
182 -              }
183 -              hash_insert_pkg(hash, pkg, is_status_file,conf);
184 -         } else {
185 -              pkg_deinit (pkg);
186 -              free(pkg);
187 -         }
188 -     }
189 +       hash_table_t *hash = &conf->pkg_hash;
190 +       pkg_t *pkg;
191  
192 -     /* XXX: CLEANUP: I'd like a cleaner interface for cleaning up
193 -       memory after read_raw_pkgs_from_file */
194 -     raw = raw_start;
195 -     while (*raw) {
196 -         free(*raw++);
197 -     }
198 -     free(raw_start);
199 -     return 0;
200 +       int fd;
201 +       int rv = 0;
202 +
203 +       if( (fd = open(file_name, O_RDONLY)) > 0 )
204 +       {
205 +               while(1)
206 +               {
207 +                       pkg = pkg_new();
208 +                       if(!pkg) {
209 +                               rv = -ENOMEM;
210 +                               break;
211 +                       }
212 +
213 +                       if (pkg_parse_fd(pkg, fd, src, dest, no_desc) == 0) {
214 +                               if (!pkg->architecture) {
215 +                                       char *version_str = pkg_version_str_alloc(pkg);
216 +                                       pkg->architecture = pkg_get_default_arch(conf);
217 +                                       opkg_message(conf, OPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
218 +                                       pkg->name, version_str, pkg->architecture);
219 +                                       free(version_str);
220 +                               }
221 +
222 +                               hash_insert_pkg(hash, pkg, is_status_file, conf);
223 +                       } else {
224 +                               pkg_deinit (pkg);
225 +                               free(pkg);
226 +                               break;
227 +                       }
228 +               }
229 +
230 +               close(fd);
231 +       }
232 +       else
233 +       {
234 +               opkg_message (conf, OPKG_ERROR,
235 +                       "Unable to open package list %s\n", file_name);
236 +
237 +               rv = -EINVAL;
238 +       }
239
240 +       return rv;
241  }
242  
243  abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name)
244 --- a/libopkg/pkg_parse.c
245 +++ b/libopkg/pkg_parse.c
246 @@ -191,214 +191,301 @@
247   
248  }
249  
250 -/* Some random thoughts from Carl:
251 -
252 -   This function could be considerably simplified if we just kept
253 -   an array of all the generic string-valued field names, and looped
254 -   through those looking for a match. Also, these fields could perhaps
255 -   be stored in the package as an array as well, (or, probably better,
256 -   as an nv_pair_list_t).
257 -
258 -   Fields which require special parsing or storage, (such as Depends:
259 -   and Status:) could be handled as they are now. 
260 -*/
261 -/* XXX: FEATURE: The Suggests: field needs to be changed from a string
262 -   to a dependency list. And, since we already have
263 -   Depends/Pre-Depends and need to add Conflicts, Recommends, and
264 -   Enhances, perhaps we could generalize all of these and save some
265 -   code duplication.
266 -*/
267 -int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest)
268 +int pkg_parse_fd(pkg_t *pkg, int fd, pkg_src_t *src, pkg_dest_t *dest, int no_desc)
269  {
270 -    int reading_conffiles, reading_description;
271 -    int pkg_false_provides=1;
272 -    char ** lines;
273 -    char * provide=NULL;
274 -
275 -    pkg->src = src;
276 -    pkg->dest = dest;
277 -
278 -    reading_conffiles = reading_description = 0;
279 -
280 -    for (lines = *raw; *lines; lines++) {
281 -       /*      fprintf(stderr, "PARSING %s\n", *lines);*/
282 -       switch (**lines) {
283 -       case 'P':
284 -           if(isGenericFieldType("Package:", *lines)) 
285 -               pkg->name = parseGenericFieldType("Package", *lines);
286 -           else if(isGenericFieldType("Priority:", *lines))
287 -               pkg->priority = parseGenericFieldType("Priority", *lines);
288 -           else if(isGenericFieldType("Provides", *lines)){
289 -/* Here we add the internal_use to align the off by one problem between provides_str and provides */
290 -               provide = (char * ) calloc(1, strlen(*lines)+ 35 ); /* Preparing the space for the new opkg_internal_use_only */
291 -               if ( alterProvidesLine(*lines,provide) ){
292 -                   return EINVAL;
293 -               }
294 -               pkg->provides_str = parseDependsString( provide, &pkg->provides_count);
295 -/* Let's try to hack a bit here.
296 -   The idea is that if a package has no Provides, we would add one generic, to permit the check of dependencies
297 -   in alot of other places. We will remove it before writing down the status database */
298 -               pkg_false_provides=0;
299 -               free(provide);
300 -           } 
301 -           else if(isGenericFieldType("Pre-Depends", *lines))
302 -               pkg->pre_depends_str = parseDependsString(*lines, &pkg->pre_depends_count);
303 -           break;
304 -
305 -       case 'A':
306 -           if(isGenericFieldType("Architecture:", *lines))
307 -               pkg->architecture = parseGenericFieldType("Architecture", *lines);
308 -           else if(isGenericFieldType("Auto-Installed:", *lines)) {
309 -               char *auto_installed_value;
310 -               auto_installed_value = parseGenericFieldType("Auto-Installed:", *lines);
311 -               if (strcmp(auto_installed_value, "yes") == 0) {
312 -                   pkg->auto_installed = 1;
313 -               }
314 -               free(auto_installed_value);
315 -           }
316 -           break;
317 -
318 -       case 'F':
319 -           if(isGenericFieldType("Filename:", *lines))
320 -               pkg->filename = parseGenericFieldType("Filename", *lines);
321 -           break;
322 -
323 -       case 'S':
324 -           if(isGenericFieldType("Section:", *lines))
325 -               pkg->section = parseGenericFieldType("Section", *lines);
326 -           else if(isGenericFieldType("Size:", *lines))
327 -               pkg->size = parseGenericFieldType("Size", *lines);
328 -           else if(isGenericFieldType("Source:", *lines))
329 -               pkg->source = parseGenericFieldType("Source", *lines);
330 -           else if(isGenericFieldType("Status", *lines))
331 -               parseStatus(pkg, *lines);
332 -           else if(isGenericFieldType("Suggests", *lines))
333 -               pkg->suggests_str = parseDependsString(*lines, &pkg->suggests_count);
334 -           break;
335 -
336 -       case 'T':
337 -           if(isGenericFieldType("Tags:", *lines))
338 -               pkg->tags = parseGenericFieldType("Tags", *lines);
339 -           break;
340 -
341 -       case 'M':
342 -           if(isGenericFieldType("MD5sum:", *lines))
343 -               pkg->md5sum = parseGenericFieldType("MD5sum", *lines);
344 -           /* The old opkg wrote out status files with the wrong case for MD5sum,
345 -               let's parse it either way */
346 -           else if(isGenericFieldType("MD5Sum:", *lines))
347 -               pkg->md5sum = parseGenericFieldType("MD5Sum", *lines);
348 -           else if(isGenericFieldType("Maintainer", *lines))
349 -               pkg->maintainer = parseGenericFieldType("Maintainer", *lines);
350 -           break;
351 -
352 -       case 'I':
353 -           if(isGenericFieldType("Installed-Size:", *lines))
354 -               pkg->installed_size = parseGenericFieldType("Installed-Size", *lines);
355 -           else if(isGenericFieldType("Installed-Time:", *lines)) {
356 -               char *time_str = parseGenericFieldType("Installed-Time", *lines);
357 -               pkg->installed_time = strtoul(time_str, NULL, 0);
358 -               free (time_str);
359 -           }       
360 -           break;
361 -
362 -       case 'E':
363 -           if(isGenericFieldType("Essential:", *lines)) {
364 -               char *essential_value;
365 -               essential_value = parseGenericFieldType("Essential", *lines);
366 -               if (strcmp(essential_value, "yes") == 0) {
367 -                   pkg->essential = 1;
368 -               }
369 -               free(essential_value);
370 -           }
371 -           break;
372 -
373 -       case 'V':
374 -           if(isGenericFieldType("Version", *lines))
375 -               parseVersion(pkg, *lines);
376 -           break;
377 -
378 -       case 'C':
379 -           if(isGenericFieldType("Conffiles", *lines)){
380 -               parseConffiles(pkg, *lines);
381 -               reading_conffiles = 1;
382 -           }
383 -           else if(isGenericFieldType("Conflicts", *lines))
384 -               pkg->conflicts_str = parseDependsString(*lines, &pkg->conflicts_count);
385 -           break;
386 -
387 -       case 'D':
388 -           if(isGenericFieldType("Description", *lines)) {
389 -               pkg->description = parseGenericFieldType("Description", *lines);
390 -               reading_conffiles = 0;
391 -               reading_description = 1;
392 -           }
393 -           else if(isGenericFieldType("Depends", *lines))
394 -               pkg->depends_str = parseDependsString(*lines, &pkg->depends_count);
395 -           break;
396 -
397 -       case 'R':
398 -           if(isGenericFieldType("Recommends", *lines))
399 -               pkg->recommends_str = parseDependsString(*lines, &pkg->recommends_count);
400 -           else if(isGenericFieldType("Replaces", *lines))
401 -               pkg->replaces_str = parseDependsString(*lines, &pkg->replaces_count);
402 -           
403 -           break;
404 -
405 -       case ' ':
406 -           if(reading_description) {
407 -               /* we already know it's not blank, so the rest of description */      
408 -               pkg->description = realloc(pkg->description,
409 -                                          strlen(pkg->description)
410 -                                          + 1 + strlen(*lines) + 1);
411 -               strcat(pkg->description, "\n");
412 -               strcat(pkg->description, (*lines));
413 -           }
414 -           else if(reading_conffiles)
415 -               parseConffiles(pkg, *lines);
416 -               
417 -           break;
418 -
419 -       default:
420 -           if(line_is_blank(*lines)) {
421 -               lines++;
422 -               goto out;
423 -           }
424 +       char buf[4096];
425 +       char line[4096];
426 +       char *nl;
427 +       int bsz = 0;
428 +       int eof = 0;
429 +       int rv = EINVAL;
430 +
431 +       int reading_conffiles, reading_description;
432 +       int pkg_false_provides=1;
433 +       char *provide = NULL;
434 +
435 +       pkg->src = src;
436 +       pkg->dest = dest;
437 +
438 +       reading_conffiles = reading_description = 0;
439 +
440 +       memset(buf, 0, sizeof(buf));
441 +
442 +       while(!eof || (bsz > 0))
443 +       {
444 +               if(!eof)
445 +               {
446 +                       rv = read(fd, &buf[bsz], sizeof(buf) - bsz - 1);
447 +
448 +                       if( rv == 0 )
449 +                       {
450 +                               eof = 1;
451 +
452 +                               if( bsz == 0 )
453 +                               {
454 +                                       rv = EINVAL;
455 +                                       break;
456 +                               }
457 +                       }
458 +                       else if( rv < 0 )
459 +                       {
460 +                               /*opkg_message(conf, OPKG_ERROR, "I/O error while parsing package list\n");*/
461 +                               printf("I/O error while parsing package list\n");
462 +                               rv = EINVAL;
463 +                               break;
464 +                       }
465 +                       else
466 +                       {
467 +                               bsz += rv;
468 +                               buf[bsz] = 0;
469 +                               rv = 0;
470 +                       }
471 +               }
472 +
473 +               if( (nl = strchr(buf, '\n')) != NULL )
474 +               {
475 +                       bsz -= (int)(nl - buf) + 1;
476 +
477 +                       memset(line, 0, sizeof(line));
478 +                       memcpy(line, buf, (int)(nl - buf));
479 +                       memmove(buf, &buf[(int)(nl - buf) + 1], bsz);
480 +
481 +                       switch(line[0])
482 +                       {
483 +                               case 'P':
484 +                                       if(isGenericFieldType("Package:", line)) 
485 +                                               pkg->name = parseGenericFieldType("Package", line);
486 +                                       else if(isGenericFieldType("Priority:", line))
487 +                                               pkg->priority = parseGenericFieldType("Priority", line);
488 +                                       else if(isGenericFieldType("Provides", line)){
489 +                                               /* Here we add the internal_use to align the off by one problem between provides_str and provides */
490 +                                               provide = (char * ) calloc(1, strlen(line)+ 35 ); /* Preparing the space for the new opkg_internal_use_only */
491 +                                               if ( alterProvidesLine(line,provide) ){
492 +                                                       rv = EINVAL;
493 +                                                       break;
494 +                                               }
495 +                                               pkg->provides_str = parseDependsString( provide, &pkg->provides_count);
496 +                                               /* Let's try to hack a bit here.
497 +                                                  The idea is that if a package has no Provides, we would add one generic, to permit the check of dependencies
498 +                                                  in alot of other places. We will remove it before writing down the status database */
499 +                                               pkg_false_provides=0;
500 +                                               free(provide);
501 +                                       }
502 +                                       else if(isGenericFieldType("Pre-Depends", line))
503 +                                               pkg->pre_depends_str = parseDependsString(line, &pkg->pre_depends_count);
504 +                                               break;
505 +
506 +                               case 'A':
507 +                                       if(isGenericFieldType("Architecture:", line))
508 +                                               pkg->architecture = parseGenericFieldType("Architecture", line);
509 +                                       else if(isGenericFieldType("Auto-Installed:", line)) {
510 +                                               char *auto_installed_value;
511 +                                               auto_installed_value = parseGenericFieldType("Auto-Installed:", line);
512 +                                               if (strcmp(auto_installed_value, "yes") == 0) {
513 +                                                       pkg->auto_installed = 1;
514 +                                               }
515 +                                               free(auto_installed_value);
516 +                                       }
517 +                                       break;
518 +
519 +                               case 'F':
520 +                                       if(isGenericFieldType("Filename:", line))
521 +                                               pkg->filename = parseGenericFieldType("Filename", line);
522 +                                       break;
523 +
524 +                               case 'S':
525 +                                       if(isGenericFieldType("Section:", line))
526 +                                               pkg->section = parseGenericFieldType("Section", line);
527 +                                       else if(isGenericFieldType("Size:", line))
528 +                                               pkg->size = parseGenericFieldType("Size", line);
529 +                                       else if(isGenericFieldType("Source:", line))
530 +                                               pkg->source = parseGenericFieldType("Source", line);
531 +                                       else if(isGenericFieldType("Status", line))
532 +                                               parseStatus(pkg, line);
533 +                                       else if(isGenericFieldType("Suggests", line))
534 +                                               pkg->suggests_str = parseDependsString(line, &pkg->suggests_count);
535 +                                       break;
536 +
537 +                               case 'T':
538 +                                       if(isGenericFieldType("Tags:", line))
539 +                                               pkg->tags = parseGenericFieldType("Tags", line);
540 +                                       break;
541 +
542 +                               case 'M':
543 +                                       if(isGenericFieldType("MD5sum:", line))
544 +                                               pkg->md5sum = parseGenericFieldType("MD5sum", line);
545 +                                       /* The old opkg wrote out status files with the wrong case for MD5sum,
546 +                                          let's parse it either way */
547 +                                       else if(isGenericFieldType("MD5Sum:", line))
548 +                                               pkg->md5sum = parseGenericFieldType("MD5Sum", line);
549 +                                       else if(isGenericFieldType("Maintainer", line))
550 +                                               pkg->maintainer = parseGenericFieldType("Maintainer", line);
551 +                                       break;
552 +
553 +                               case 'I':
554 +                                       if(isGenericFieldType("Installed-Size:", line))
555 +                                               pkg->installed_size = parseGenericFieldType("Installed-Size", line);
556 +                                       else if(isGenericFieldType("Installed-Time:", line)) {
557 +                                               char *time_str = parseGenericFieldType("Installed-Time", line);
558 +                                               pkg->installed_time = strtoul(time_str, NULL, 0);
559 +                                               free (time_str);
560 +                                       }           
561 +                                       break;
562 +
563 +                               case 'E':
564 +                                       if(isGenericFieldType("Essential:", line)) {
565 +                                               char *essential_value;
566 +                                               essential_value = parseGenericFieldType("Essential", line);
567 +                                               if (strcmp(essential_value, "yes") == 0) {
568 +                                                       pkg->essential = 1;
569 +                                               }
570 +                                               free(essential_value);
571 +                                       }
572 +                                       break;
573 +
574 +                               case 'V':
575 +                                       if(isGenericFieldType("Version", line))
576 +                                               parseVersion(pkg, line);
577 +                                       break;
578 +
579 +                               case 'C':
580 +                                       if(isGenericFieldType("Conffiles", line)){
581 +                                               parseConffiles(pkg, line);
582 +                                               reading_conffiles = 1;
583 +                                       }
584 +                                       else if(isGenericFieldType("Conflicts", line))
585 +                                               pkg->conflicts_str = parseDependsString(line, &pkg->conflicts_count);
586 +                                       break;
587 +
588 +                               case 'D':
589 +                                       if(isGenericFieldType("Description", line)) {
590 +                                               if(!no_desc)
591 +                                                       pkg->description = parseGenericFieldType("Description", line);
592 +                                               reading_conffiles = 0;
593 +                                               reading_description = 1;
594 +                                       }
595 +                                       else if(isGenericFieldType("Depends", line))
596 +                                               pkg->depends_str = parseDependsString(line, &pkg->depends_count);
597 +                                       break;
598 +
599 +                               case 'R':
600 +                                       if(isGenericFieldType("Recommends", line))
601 +                                               pkg->recommends_str = parseDependsString(line, &pkg->recommends_count);
602 +                                       else if(isGenericFieldType("Replaces", line))
603 +                                               pkg->replaces_str = parseDependsString(line, &pkg->replaces_count);
604 +                                       break;
605 +
606 +                               case ' ':
607 +                                       if(reading_description) {
608 +                                               /* we already know it's not blank, so the rest of description */
609 +                                               if(!no_desc)
610 +                                               {
611 +                                                       pkg->description = realloc(pkg->description,
612 +                                                               strlen(pkg->description) + 1 + strlen(line) + 1);
613 +                                                       strcat(pkg->description, "\n");
614 +                                                       strcat(pkg->description, (line));
615 +                                               }
616 +                                       }
617 +                                       else if(reading_conffiles)
618 +                                               parseConffiles(pkg, line);
619 +                                       break;
620 +
621 +                               default:
622 +                                       if(line_is_blank(line))
623 +                                               goto out;
624 +                       }
625 +               }
626 +               else
627 +               {
628 +                       /*opkg_message(conf, OPKG_ERROR, "Buffer exceeded while parsing line:\n[%s]\n", buf);*/
629 +                       printf("Buffer exceeded while parsing line:\n[%s]\n", buf);
630 +                       rv = EINVAL;
631 +                       break;
632 +               }
633         }
634 -    }
635 -out:;
636 -    
637 -    *raw = lines;
638 -/* If the opk has not a Provides line, we insert our false line */ 
639 -    if ( pkg_false_provides==1)
640 -    {
641 -       pkg->provides_count = 1;
642 -       pkg->provides_str = calloc (1, sizeof (char*));
643 -       pkg->provides_str[0] = strdup ("opkg_internal_use_only");
644 -    }
645 -
646 -    if (pkg->name) {
647 -       return 0;
648 -    } else {
649 -       return EINVAL;
650 -    }
651 +
652 +       out:
653 +
654 +       if (bsz)
655 +               lseek(fd, -(off_t)bsz, SEEK_CUR);
656 +
657 +       if (!rv && pkg->name)
658 +               return 0;
659 +       else
660 +               return EINVAL;
661  }
662  
663 -int pkg_valorize_other_field(pkg_t *pkg, char ***raw)
664 +int pkg_valorize_other_field(pkg_t *pkg, int fd)
665  {
666 -    char ** lines;
667 +       char buf[4096];
668 +       char line[4096];
669 +       char *nl;
670 +       int bsz = 0;
671 +       int eof = 0;
672 +       int rv = EINVAL;
673 +
674 +       memset(buf, 0, sizeof(buf));
675 +
676 +       while(!eof || (bsz > 0))
677 +       {
678 +               if(!eof)
679 +               {
680 +                       rv = read(fd, &buf[bsz], sizeof(buf) - bsz - 1);
681 +
682 +                       if( rv == 0 )
683 +                       {
684 +                               eof = 1;
685 +
686 +                               if( bsz == 0 )
687 +                               {
688 +                                       rv = EINVAL;
689 +                                       break;
690 +                               }
691 +                       }
692 +                       else if( rv < 0 )
693 +                       {
694 +                               rv = EINVAL;
695 +                               break;
696 +                       }
697 +                       else
698 +                       {
699 +                               bsz += rv;
700 +                               buf[bsz] = 0;
701 +                               rv = 0;
702 +                       }
703 +               }
704  
705 -    for (lines = *raw; *lines; lines++) {
706 -       if(isGenericFieldType("Essential:", *lines)) {
707 -           char *essential_value;
708 -           essential_value = parseGenericFieldType("Essential", *lines);
709 -           if (strcmp(essential_value, "yes") == 0) {
710 -               pkg->essential = 1;
711 -           }
712 -           free(essential_value);
713 +               if( (nl = strchr(buf, '\n')) != NULL )
714 +               {
715 +                       bsz -= (int)(nl - buf) + 1;
716 +
717 +                       memset(line, 0, sizeof(line));
718 +                       memcpy(line, buf, (int)(nl - buf));
719 +                       memmove(buf, &buf[(int)(nl - buf) + 1], bsz);
720 +
721 +                       if(isGenericFieldType("Essential:", line))
722 +                       {
723 +                               char *essential_value;
724 +                               essential_value = parseGenericFieldType("Essential", line);
725 +                               if (strcmp(essential_value, "yes") == 0) {
726 +                                       pkg->essential = 1;
727 +                               }
728 +                               free(essential_value);
729 +                       }
730 +               }
731 +               else
732 +               {
733 +                       rv = EINVAL;
734 +                       break;
735 +               }
736         }
737 -    }
738 -    *raw = lines;
739  
740 -    return 0;
741 +       if (bsz)
742 +               lseek(fd, -(off_t)bsz, SEEK_CUR);
743 +
744 +       if (!rv && pkg->name)
745 +               return 0;
746 +       else
747 +               return EINVAL;
748  }
749 +
750 --- a/libopkg/pkg_parse.h
751 +++ b/libopkg/pkg_parse.h
752 @@ -25,7 +25,7 @@
753  char ** parseDependsString(char * raw, int * depends_count);
754  int parseVersion(pkg_t *pkg, char *raw);
755  void parseConffiles(pkg_t * pkg, char * raw);
756 -int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest);
757 -int pkg_valorize_other_field(pkg_t *pkg, char ***raw);
758 +int pkg_parse_fd(pkg_t *pkg, int fd, pkg_src_t *src, pkg_dest_t *dest, int no_desc);
759 +int pkg_valorize_other_field(pkg_t *pkg, int fd);
760  
761  #endif
762 --- a/libopkg/opkg_utils.h
763 +++ b/libopkg/opkg_utils.h
764 @@ -26,8 +26,6 @@
765  void free_error_list();
766  
767  long unsigned int get_available_blocks(char * filesystem);
768 -char **read_raw_pkgs_from_file(const char *file_name);
769 -char **read_raw_pkgs_from_stream(FILE *fp);
770  char *trim_alloc(char * line);
771  int line_is_blank(const char *line);
772  
773 --- a/libopkg/libopkg.c
774 +++ b/libopkg/libopkg.c
775 @@ -88,6 +88,7 @@
776         char *cmd_name;
777         opkg_cmd_t *cmd;
778         opkg_conf_t opkg_conf;
779 +       int no_desc = 1;
780  
781         args_init (&args);
782  
783 @@ -122,12 +123,18 @@
784               !strcmp(cmd_name,"status") )
785             args.noreadfeedsfile = 1;
786  
787 +       if( !strcmp(cmd_name,"list") ||
788 +           !strcmp(cmd_name,"list-installed") ||
789 +           !strcmp(cmd_name,"list_installed") ||
790 +           !strcmp(cmd_name,"search") )
791 +          no_desc = 0;
792 +
793         opkg_cb_message = default_opkg_message_callback;
794         opkg_cb_response = default_opkg_response_callback;
795         opkg_cb_status = default_opkg_status_callback;
796  
797  
798 -       err = opkg_conf_init (&opkg_conf, &args);
799 +       err = opkg_conf_init (&opkg_conf, &args, no_desc);
800         if (err)
801         {
802                 opkg_print_error_list (&opkg_conf);
803 --- a/libopkg/opkg.c
804 +++ b/libopkg/opkg.c
805 @@ -205,7 +205,7 @@
806    }
807  
808    opkg->conf = calloc (1, sizeof (opkg_conf_t));
809 -  err = opkg_conf_init (opkg->conf, opkg->args);
810 +  err = opkg_conf_init (opkg->conf, opkg->args, 0);
811    if (err)
812    {
813      free (opkg->conf);
814 @@ -286,7 +286,7 @@
815  
816    /* throw away old opkg_conf and start again */
817    opkg_conf_deinit (opkg->conf);
818 -  opkg_conf_init (opkg->conf, opkg->args);
819 +  opkg_conf_init (opkg->conf, opkg->args, 0);
820  
821    free (opkg->options);
822    opkg_init_options_array (opkg->conf, &opkg->options);
823 --- a/libopkg/opkg_conf.c
824 +++ b/libopkg/opkg_conf.c
825 @@ -44,9 +44,9 @@
826  static int opkg_conf_set_default_dest(opkg_conf_t *conf,
827                                       const char *default_dest_name);
828  static int set_and_load_pkg_src_list(opkg_conf_t *conf,
829 -                                    pkg_src_list_t *nv_pair_list);
830 +                                    pkg_src_list_t *nv_pair_list, int no_desc);
831  static int set_and_load_pkg_dest_list(opkg_conf_t *conf,
832 -                                     nv_pair_list_t *nv_pair_list, char * lists_dir);
833 +                                     nv_pair_list_t *nv_pair_list, char * lists_dir, int no_desc);
834  
835  int opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options)
836  {
837 @@ -106,7 +106,7 @@
838       }
839  }
840  
841 -int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
842 +int opkg_conf_init(opkg_conf_t *conf, const args_t *args, int no_desc)
843  {
844       int err;
845       char *tmp_dir_base;
846 @@ -294,12 +294,12 @@
847       if ( !(args->nocheckfordirorfile)){
848          /* need to run load the source list before dest list -Jamey */
849          if ( !(args->noreadfeedsfile))
850 -           set_and_load_pkg_src_list(conf, &conf->pkg_src_list);
851 +           set_and_load_pkg_src_list(conf, &conf->pkg_src_list, no_desc);
852     
853          /* Now that we have resolved conf->offline_root, we can commit to
854            the directory names for the dests and load in all the package
855            lists. */
856 -        set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list,lists_dir);
857 +        set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list,lists_dir, no_desc);
858     
859          if (args->dest) {
860              err = opkg_conf_set_default_dest(conf, args->dest);
861 @@ -409,7 +409,7 @@
862       return 1;
863  }
864  
865 -static int set_and_load_pkg_src_list(opkg_conf_t *conf, pkg_src_list_t *pkg_src_list)
866 +static int set_and_load_pkg_src_list(opkg_conf_t *conf, pkg_src_list_t *pkg_src_list, int no_desc)
867  {
868       pkg_src_list_elt_t *iter;
869       pkg_src_t *src;
870 @@ -426,7 +426,7 @@
871                           src->name);
872  
873           if (file_exists(list_file)) {
874 -              pkg_hash_add_from_file(conf, list_file, src, NULL, 0);
875 +              pkg_hash_add_from_file(conf, list_file, src, NULL, 0, no_desc);
876           }
877           free(list_file);
878       }
879 @@ -434,7 +434,7 @@
880       return 0;
881  }
882  
883 -static int set_and_load_pkg_dest_list(opkg_conf_t *conf, nv_pair_list_t *nv_pair_list, char *lists_dir )
884 +static int set_and_load_pkg_dest_list(opkg_conf_t *conf, nv_pair_list_t *nv_pair_list, char *lists_dir, int no_desc)
885  {
886       nv_pair_list_elt_t *iter;
887       nv_pair_t *nv_pair;
888 @@ -459,7 +459,7 @@
889           }
890           if (file_exists(dest->status_file_name)) {
891                pkg_hash_add_from_file(conf, dest->status_file_name,
892 -                                     NULL, dest, 1);
893 +                                     NULL, dest, 1, no_desc);
894           }
895       }
896  
897 --- a/libopkg/opkg_conf.h
898 +++ b/libopkg/opkg_conf.h
899 @@ -102,7 +102,7 @@
900       const void *value;
901  };
902  
903 -int opkg_conf_init(opkg_conf_t *conf, const args_t *args);
904 +int opkg_conf_init(opkg_conf_t *conf, const args_t *args, int no_desc);
905  void opkg_conf_deinit(opkg_conf_t *conf);
906  
907  int opkg_conf_write_status_files(opkg_conf_t *conf);
908 --- a/tests/opkg_hash_test.c
909 +++ b/tests/opkg_hash_test.c
910 @@ -33,8 +33,8 @@
911      }
912      pkg_hash_init("test", hash, 1024);
913  
914 -    pkg_hash_add_from_file(&conf, argv[1], NULL, NULL, 0);
915 -    pkg_hash_add_from_file(&conf, argv[2], NULL, NULL, 0);
916 +    pkg_hash_add_from_file(&conf, argv[1], NULL, NULL, 0, 0);
917 +    pkg_hash_add_from_file(&conf, argv[2], NULL, NULL, 0, 0);
918  
919      if (argc < 4) {
920         pkg_print_info( pkg_hash_fetch_by_name_version(hash, "libc6", "2.2.3-2"), stdout);
921 --- a/libopkg/pkg_hash.h
922 +++ b/libopkg/pkg_hash.h
923 @@ -33,7 +33,7 @@
924  void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *available);
925  
926  int pkg_hash_add_from_file(opkg_conf_t *conf, const char *file_name,
927 -                          pkg_src_t *src, pkg_dest_t *dest, int is_status_file);
928 +                          pkg_src_t *src, pkg_dest_t *dest, int is_status_file, int no_desc);
929  pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,opkg_conf_t *conf);
930  
931  abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);