1 --- a/libopkg/opkg_utils.c
2 +++ b/libopkg/opkg_utils.c
7 -char **read_raw_pkgs_from_file(const char *file_name)
12 - if(!(fp = fopen(file_name, "r"))){
13 - fprintf(stderr, "can't get %s open for read\n", file_name);
17 - ret = read_raw_pkgs_from_stream(fp);
24 -char **read_raw_pkgs_from_stream(FILE *fp)
26 - char **raw = NULL, *buf, *scout;
30 - buf = calloc (1, size);
32 - while (fgets(buf, size, fp)) {
33 - while (strlen (buf) == (size - 1)
34 - && buf[size-2] != '\n') {
35 - size_t o = size - 1;
37 - buf = realloc (buf, size);
38 - if (fgets (buf + o, size - o, fp) == NULL)
43 - raw = realloc(raw, (count + 50) * sizeof(char *));
45 - if((scout = strchr(buf, '\n')))
48 - raw[count++] = strdup(buf);
51 - raw = realloc(raw, (count + 1) * sizeof(char *));
59 /* something to remove whitespace, a hash pooper */
60 char *trim_alloc(char *line)
74 int pkg_init_from_file(pkg_t *pkg, const char *filename)
82 if (err) { return err; }
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);
93 void set_flags_from_control(opkg_conf_t *conf, pkg_t *pkg){
96 - char **raw_start=NULL;
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 @@
104 sprintf( temp_str,"%s/%s.control",pkg->dest->info_dir,pkg->name);
106 - raw = raw_start = read_raw_pkgs_from_file(temp_str);
108 - opkg_message(conf, OPKG_ERROR, "Unable to open the control file in %s\n", __FUNCTION__);
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);
122 + if( (fd = open(temp_str, O_RDONLY)) > 0 )
124 + if( pkg_valorize_other_field(pkg, fd) )
126 + opkg_message(conf, OPKG_DEBUG, "unable to read control file for %s. May be empty\n", pkg->name);
134 + opkg_message(conf, OPKG_ERROR, "Unable to open the control file in %s\n", __FUNCTION__);
142 #define CHECK_BUFF_SIZE(buff, line, buf_size, page_size) do { \
143 --- a/libopkg/pkg_hash.c
144 +++ b/libopkg/pkg_hash.c
152 #include "hash_table.h"
154 @@ -112,43 +114,50 @@
155 int pkg_hash_add_from_file(opkg_conf_t *conf, const char *file_name,
156 pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
158 - hash_table_t *hash = &conf->pkg_hash;
163 - raw = raw_start = read_raw_pkgs_from_file(file_name);
167 - while(*raw){ /* don't worry, we'll increment raw in the parsing function */
172 - if (pkg_parse_raw(pkg, &raw, src, dest) == 0) {
173 - if (!pkg->architecture) {
174 - char *version_str = pkg_version_str_alloc(pkg);
175 - pkg->architecture = pkg_get_default_arch(conf);
176 - opkg_message(conf, OPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
177 - pkg->name, version_str, pkg->architecture);
180 - hash_insert_pkg(hash, pkg, is_status_file,conf);
186 + hash_table_t *hash = &conf->pkg_hash;
189 - /* XXX: CLEANUP: I'd like a cleaner interface for cleaning up
190 - memory after read_raw_pkgs_from_file */
200 + if( (fd = open(file_name, O_RDONLY)) > 0 )
210 + if (pkg_parse_fd(pkg, fd, src, dest) == 0) {
211 + if (!pkg->architecture) {
212 + char *version_str = pkg_version_str_alloc(pkg);
213 + pkg->architecture = pkg_get_default_arch(conf);
214 + opkg_message(conf, OPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
215 + pkg->name, version_str, pkg->architecture);
219 + hash_insert_pkg(hash, pkg, is_status_file, conf);
231 + opkg_message (conf, OPKG_ERROR,
232 + "Unable to open package list %s\n", file_name);
240 abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name)
241 --- a/libopkg/pkg_parse.c
242 +++ b/libopkg/pkg_parse.c
243 @@ -191,214 +191,297 @@
247 -/* Some random thoughts from Carl:
249 - This function could be considerably simplified if we just kept
250 - an array of all the generic string-valued field names, and looped
251 - through those looking for a match. Also, these fields could perhaps
252 - be stored in the package as an array as well, (or, probably better,
253 - as an nv_pair_list_t).
255 - Fields which require special parsing or storage, (such as Depends:
256 - and Status:) could be handled as they are now.
258 -/* XXX: FEATURE: The Suggests: field needs to be changed from a string
259 - to a dependency list. And, since we already have
260 - Depends/Pre-Depends and need to add Conflicts, Recommends, and
261 - Enhances, perhaps we could generalize all of these and save some
264 -int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest)
265 +int pkg_parse_fd(pkg_t *pkg, int fd, pkg_src_t *src, pkg_dest_t *dest)
267 - int reading_conffiles, reading_description;
268 - int pkg_false_provides=1;
270 - char * provide=NULL;
275 - reading_conffiles = reading_description = 0;
277 - for (lines = *raw; *lines; lines++) {
278 - /* fprintf(stderr, "PARSING %s\n", *lines);*/
281 - if(isGenericFieldType("Package:", *lines))
282 - pkg->name = parseGenericFieldType("Package", *lines);
283 - else if(isGenericFieldType("Priority:", *lines))
284 - pkg->priority = parseGenericFieldType("Priority", *lines);
285 - else if(isGenericFieldType("Provides", *lines)){
286 -/* Here we add the internal_use to align the off by one problem between provides_str and provides */
287 - provide = (char * ) calloc(1, strlen(*lines)+ 35 ); /* Preparing the space for the new opkg_internal_use_only */
288 - if ( alterProvidesLine(*lines,provide) ){
291 - pkg->provides_str = parseDependsString( provide, &pkg->provides_count);
292 -/* Let's try to hack a bit here.
293 - The idea is that if a package has no Provides, we would add one generic, to permit the check of dependencies
294 - in alot of other places. We will remove it before writing down the status database */
295 - pkg_false_provides=0;
298 - else if(isGenericFieldType("Pre-Depends", *lines))
299 - pkg->pre_depends_str = parseDependsString(*lines, &pkg->pre_depends_count);
303 - if(isGenericFieldType("Architecture:", *lines))
304 - pkg->architecture = parseGenericFieldType("Architecture", *lines);
305 - else if(isGenericFieldType("Auto-Installed:", *lines)) {
306 - char *auto_installed_value;
307 - auto_installed_value = parseGenericFieldType("Auto-Installed:", *lines);
308 - if (strcmp(auto_installed_value, "yes") == 0) {
309 - pkg->auto_installed = 1;
311 - free(auto_installed_value);
316 - if(isGenericFieldType("Filename:", *lines))
317 - pkg->filename = parseGenericFieldType("Filename", *lines);
321 - if(isGenericFieldType("Section:", *lines))
322 - pkg->section = parseGenericFieldType("Section", *lines);
323 - else if(isGenericFieldType("Size:", *lines))
324 - pkg->size = parseGenericFieldType("Size", *lines);
325 - else if(isGenericFieldType("Source:", *lines))
326 - pkg->source = parseGenericFieldType("Source", *lines);
327 - else if(isGenericFieldType("Status", *lines))
328 - parseStatus(pkg, *lines);
329 - else if(isGenericFieldType("Suggests", *lines))
330 - pkg->suggests_str = parseDependsString(*lines, &pkg->suggests_count);
334 - if(isGenericFieldType("Tags:", *lines))
335 - pkg->tags = parseGenericFieldType("Tags", *lines);
339 - if(isGenericFieldType("MD5sum:", *lines))
340 - pkg->md5sum = parseGenericFieldType("MD5sum", *lines);
341 - /* The old opkg wrote out status files with the wrong case for MD5sum,
342 - let's parse it either way */
343 - else if(isGenericFieldType("MD5Sum:", *lines))
344 - pkg->md5sum = parseGenericFieldType("MD5Sum", *lines);
345 - else if(isGenericFieldType("Maintainer", *lines))
346 - pkg->maintainer = parseGenericFieldType("Maintainer", *lines);
350 - if(isGenericFieldType("Installed-Size:", *lines))
351 - pkg->installed_size = parseGenericFieldType("Installed-Size", *lines);
352 - else if(isGenericFieldType("Installed-Time:", *lines)) {
353 - char *time_str = parseGenericFieldType("Installed-Time", *lines);
354 - pkg->installed_time = strtoul(time_str, NULL, 0);
360 - if(isGenericFieldType("Essential:", *lines)) {
361 - char *essential_value;
362 - essential_value = parseGenericFieldType("Essential", *lines);
363 - if (strcmp(essential_value, "yes") == 0) {
364 - pkg->essential = 1;
366 - free(essential_value);
371 - if(isGenericFieldType("Version", *lines))
372 - parseVersion(pkg, *lines);
376 - if(isGenericFieldType("Conffiles", *lines)){
377 - parseConffiles(pkg, *lines);
378 - reading_conffiles = 1;
380 - else if(isGenericFieldType("Conflicts", *lines))
381 - pkg->conflicts_str = parseDependsString(*lines, &pkg->conflicts_count);
385 - if(isGenericFieldType("Description", *lines)) {
386 - pkg->description = parseGenericFieldType("Description", *lines);
387 - reading_conffiles = 0;
388 - reading_description = 1;
390 - else if(isGenericFieldType("Depends", *lines))
391 - pkg->depends_str = parseDependsString(*lines, &pkg->depends_count);
395 - if(isGenericFieldType("Recommends", *lines))
396 - pkg->recommends_str = parseDependsString(*lines, &pkg->recommends_count);
397 - else if(isGenericFieldType("Replaces", *lines))
398 - pkg->replaces_str = parseDependsString(*lines, &pkg->replaces_count);
403 - if(reading_description) {
404 - /* we already know it's not blank, so the rest of description */
405 - pkg->description = realloc(pkg->description,
406 - strlen(pkg->description)
407 - + 1 + strlen(*lines) + 1);
408 - strcat(pkg->description, "\n");
409 - strcat(pkg->description, (*lines));
411 - else if(reading_conffiles)
412 - parseConffiles(pkg, *lines);
417 - if(line_is_blank(*lines)) {
428 + int reading_conffiles, reading_description;
429 + int pkg_false_provides=1;
430 + char *provide = NULL;
435 + reading_conffiles = reading_description = 0;
437 + memset(buf, 0, sizeof(buf));
439 + while(!eof || (bsz > 0))
443 + rv = read(fd, &buf[bsz], sizeof(buf) - bsz - 1);
457 + /*opkg_message(conf, OPKG_ERROR, "I/O error while parsing package list\n");*/
458 + printf("I/O error while parsing package list\n");
470 + if( (nl = strchr(buf, '\n')) != NULL )
472 + bsz -= (int)(nl - buf) + 1;
474 + memset(line, 0, sizeof(line));
475 + memcpy(line, buf, (int)(nl - buf));
476 + memmove(buf, &buf[(int)(nl - buf) + 1], bsz);
481 + if(isGenericFieldType("Package:", line))
482 + pkg->name = parseGenericFieldType("Package", line);
483 + else if(isGenericFieldType("Priority:", line))
484 + pkg->priority = parseGenericFieldType("Priority", line);
485 + else if(isGenericFieldType("Provides", line)){
486 + /* Here we add the internal_use to align the off by one problem between provides_str and provides */
487 + provide = (char * ) calloc(1, strlen(line)+ 35 ); /* Preparing the space for the new opkg_internal_use_only */
488 + if ( alterProvidesLine(line,provide) ){
492 + pkg->provides_str = parseDependsString( provide, &pkg->provides_count);
493 + /* Let's try to hack a bit here.
494 + The idea is that if a package has no Provides, we would add one generic, to permit the check of dependencies
495 + in alot of other places. We will remove it before writing down the status database */
496 + pkg_false_provides=0;
499 + else if(isGenericFieldType("Pre-Depends", line))
500 + pkg->pre_depends_str = parseDependsString(line, &pkg->pre_depends_count);
504 + if(isGenericFieldType("Architecture:", line))
505 + pkg->architecture = parseGenericFieldType("Architecture", line);
506 + else if(isGenericFieldType("Auto-Installed:", line)) {
507 + char *auto_installed_value;
508 + auto_installed_value = parseGenericFieldType("Auto-Installed:", line);
509 + if (strcmp(auto_installed_value, "yes") == 0) {
510 + pkg->auto_installed = 1;
512 + free(auto_installed_value);
517 + if(isGenericFieldType("Filename:", line))
518 + pkg->filename = parseGenericFieldType("Filename", line);
522 + if(isGenericFieldType("Section:", line))
523 + pkg->section = parseGenericFieldType("Section", line);
524 + else if(isGenericFieldType("Size:", line))
525 + pkg->size = parseGenericFieldType("Size", line);
526 + else if(isGenericFieldType("Source:", line))
527 + pkg->source = parseGenericFieldType("Source", line);
528 + else if(isGenericFieldType("Status", line))
529 + parseStatus(pkg, line);
530 + else if(isGenericFieldType("Suggests", line))
531 + pkg->suggests_str = parseDependsString(line, &pkg->suggests_count);
535 + if(isGenericFieldType("Tags:", line))
536 + pkg->tags = parseGenericFieldType("Tags", line);
540 + if(isGenericFieldType("MD5sum:", line))
541 + pkg->md5sum = parseGenericFieldType("MD5sum", line);
542 + /* The old opkg wrote out status files with the wrong case for MD5sum,
543 + let's parse it either way */
544 + else if(isGenericFieldType("MD5Sum:", line))
545 + pkg->md5sum = parseGenericFieldType("MD5Sum", line);
546 + else if(isGenericFieldType("Maintainer", line))
547 + pkg->maintainer = parseGenericFieldType("Maintainer", line);
551 + if(isGenericFieldType("Installed-Size:", line))
552 + pkg->installed_size = parseGenericFieldType("Installed-Size", line);
553 + else if(isGenericFieldType("Installed-Time:", line)) {
554 + char *time_str = parseGenericFieldType("Installed-Time", line);
555 + pkg->installed_time = strtoul(time_str, NULL, 0);
561 + if(isGenericFieldType("Essential:", line)) {
562 + char *essential_value;
563 + essential_value = parseGenericFieldType("Essential", line);
564 + if (strcmp(essential_value, "yes") == 0) {
565 + pkg->essential = 1;
567 + free(essential_value);
572 + if(isGenericFieldType("Version", line))
573 + parseVersion(pkg, line);
577 + if(isGenericFieldType("Conffiles", line)){
578 + parseConffiles(pkg, line);
579 + reading_conffiles = 1;
581 + else if(isGenericFieldType("Conflicts", line))
582 + pkg->conflicts_str = parseDependsString(line, &pkg->conflicts_count);
586 + if(isGenericFieldType("Description", line)) {
587 + pkg->description = parseGenericFieldType("Description", line);
588 + reading_conffiles = 0;
589 + reading_description = 1;
591 + else if(isGenericFieldType("Depends", line))
592 + pkg->depends_str = parseDependsString(line, &pkg->depends_count);
596 + if(isGenericFieldType("Recommends", line))
597 + pkg->recommends_str = parseDependsString(line, &pkg->recommends_count);
598 + else if(isGenericFieldType("Replaces", line))
599 + pkg->replaces_str = parseDependsString(line, &pkg->replaces_count);
603 + if(reading_description) {
604 + /* we already know it's not blank, so the rest of description */
605 + pkg->description = realloc(pkg->description,
606 + strlen(pkg->description) + 1 + strlen(line) + 1);
607 + strcat(pkg->description, "\n");
608 + strcat(pkg->description, (line));
610 + else if(reading_conffiles)
611 + parseConffiles(pkg, line);
615 + if(line_is_blank(line))
621 + /*opkg_message(conf, OPKG_ERROR, "Buffer exceeded while parsing line:\n[%s]\n", buf);*/
622 + printf("Buffer exceeded while parsing line:\n[%s]\n", buf);
631 -/* If the opk has not a Provides line, we insert our false line */
632 - if ( pkg_false_provides==1)
634 - pkg->provides_count = 1;
635 - pkg->provides_str = calloc (1, sizeof (char*));
636 - pkg->provides_str[0] = strdup ("opkg_internal_use_only");
648 + lseek(fd, -(off_t)bsz, SEEK_CUR);
650 + if (!rv && pkg->name)
656 -int pkg_valorize_other_field(pkg_t *pkg, char ***raw)
657 +int pkg_valorize_other_field(pkg_t *pkg, int fd)
667 + memset(buf, 0, sizeof(buf));
669 + while(!eof || (bsz > 0))
673 + rv = read(fd, &buf[bsz], sizeof(buf) - bsz - 1);
698 - for (lines = *raw; *lines; lines++) {
699 - if(isGenericFieldType("Essential:", *lines)) {
700 - char *essential_value;
701 - essential_value = parseGenericFieldType("Essential", *lines);
702 - if (strcmp(essential_value, "yes") == 0) {
703 - pkg->essential = 1;
705 - free(essential_value);
706 + if( (nl = strchr(buf, '\n')) != NULL )
708 + bsz -= (int)(nl - buf) + 1;
710 + memset(line, 0, sizeof(line));
711 + memcpy(line, buf, (int)(nl - buf));
712 + memmove(buf, &buf[(int)(nl - buf) + 1], bsz);
714 + if(isGenericFieldType("Essential:", line))
716 + char *essential_value;
717 + essential_value = parseGenericFieldType("Essential", line);
718 + if (strcmp(essential_value, "yes") == 0) {
719 + pkg->essential = 1;
721 + free(essential_value);
735 + lseek(fd, -(off_t)bsz, SEEK_CUR);
737 + if (!rv && pkg->name)
743 --- a/libopkg/pkg_parse.h
744 +++ b/libopkg/pkg_parse.h
746 char ** parseDependsString(char * raw, int * depends_count);
747 int parseVersion(pkg_t *pkg, char *raw);
748 void parseConffiles(pkg_t * pkg, char * raw);
749 -int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest);
750 -int pkg_valorize_other_field(pkg_t *pkg, char ***raw);
751 +int pkg_parse_fd(pkg_t *pkg, int fd, pkg_src_t *src, pkg_dest_t *dest);
752 +int pkg_valorize_other_field(pkg_t *pkg, int fd);
755 --- a/libopkg/opkg_utils.h
756 +++ b/libopkg/opkg_utils.h
758 void free_error_list();
760 long unsigned int get_available_blocks(char * filesystem);
761 -char **read_raw_pkgs_from_file(const char *file_name);
762 -char **read_raw_pkgs_from_stream(FILE *fp);
763 char *trim_alloc(char * line);
764 int line_is_blank(const char *line);