5aaee7608708ce362ef7ca4f46043c72c5d19f90
[librecmc/librecmc.git] / package / busybox / patches / 524-memory_usage.patch
1 --- a/archival/libipkg/pkg.c
2 +++ b/archival/libipkg/pkg.c
3 @@ -224,8 +224,7 @@
4       if (err) { return err; }
5  
6       rewind(control_file);
7 -     raw = read_raw_pkgs_from_stream(control_file);
8 -     pkg_parse_raw(pkg, &raw, NULL, NULL);
9 +        pkg_parse_stream(pkg, control_file, NULL, NULL);
10  
11       fclose(control_file);
12  
13 --- a/archival/libipkg/pkg_hash.c
14 +++ b/archival/libipkg/pkg_hash.c
15 @@ -89,20 +89,20 @@
16                            pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
17  {
18       hash_table_t *hash = &conf->pkg_hash;
19 -     char **raw;
20 -     char **raw_start;
21 +     FILE *fp;
22       pkg_t *pkg;
23      
24 -     raw = raw_start = read_raw_pkgs_from_file(file_name);
25 -     if (!raw)
26 -        return -ENOMEM;
27 +     if(!(fp = fopen(file_name, "r"))){
28 +         fprintf(stderr, "can't get %s open for read\n", file_name);
29 +         return NULL;
30 +     }
31  
32 -     while(*raw){         /* don't worry, we'll increment raw in the parsing function */
33 +     while(!feof(fp)) {         /* don't worry, we'll increment raw in the parsing function */
34           pkg = pkg_new();
35           if (!pkg)
36                return -ENOMEM;
37  
38 -         if (pkg_parse_raw(pkg, &raw, src, dest) == 0) {
39 +         if (pkg_parse_stream(pkg, fp, src, dest) == 0) {
40                if (!pkg->architecture) {
41                     char *version_str = pkg_version_str_alloc(pkg);
42                     pkg->architecture = pkg_get_default_arch(conf);
43 @@ -116,13 +116,6 @@
44           }
45       }
46  
47 -     /* XXX: CLEANUP: I'd like a cleaner interface for cleaning up
48 -       memory after read_raw_pkgs_from_file */
49 -     raw = raw_start;
50 -     while (*raw) {
51 -         free(*raw++);
52 -     }
53 -     free(raw_start);
54       return 0;
55  }
56  
57 --- a/archival/libipkg/pkg_parse.c
58 +++ b/archival/libipkg/pkg_parse.c
59 @@ -224,6 +224,161 @@
60     Enhances, perhaps we could generalize all of these and save some
61     code duplication.
62  */
63 +int pkg_parse_stream(pkg_t *pkg, FILE *stream, pkg_src_t *src, pkg_dest_t *dest)
64 +{
65 +    int reading_conffiles, reading_description;
66 +    int pkg_false_provides=1;
67 +    char ** lines;
68 +    char *provide=NULL;
69 +    char *buf, *scout;
70 +    int count = 0;
71 +    size_t size = 512;
72 +
73 +    buf = malloc (size);
74 +
75 +    pkg->src = src;
76 +    pkg->dest = dest;
77 +
78 +    reading_conffiles = reading_description = 0;
79 +
80 +    while (fgets(buf, size, stream)) {
81 +         while (strlen (buf) == (size - 1)
82 +                && buf[size-2] != '\n') {
83 +              size_t o = size - 1;
84 +              size *= 2;
85 +              buf = realloc (buf, size);
86 +              if (fgets (buf + o, size - o, stream) == NULL)
87 +                   break;
88 +         }
89 +
90 +         if((scout = strchr(buf, '\n')))
91 +              *scout = '\0';
92 +
93 +         lines = &buf;
94 +       /*      fprintf(stderr, "PARSING %s\n", *lines);*/
95 +       if(isGenericFieldType("Package:", *lines)) 
96 +           pkg->name = parseGenericFieldType("Package", *lines);
97 +       else if(isGenericFieldType("Architecture:", *lines))
98 +           pkg->architecture = parseGenericFieldType("Architecture", *lines);
99 +       else if(isGenericFieldType("Filename:", *lines))
100 +           pkg->filename = parseGenericFieldType("Filename", *lines);
101 +       else if(isGenericFieldType("Section:", *lines))
102 +           pkg->section = parseGenericFieldType("Section", *lines);
103 +       else if(isGenericFieldType("MD5sum:", *lines))
104 +           pkg->md5sum = parseGenericFieldType("MD5sum", *lines);
105 +       /* The old ipkg wrote out status files with the wrong case for MD5sum,
106 +          let's parse it either way */
107 +       else if(isGenericFieldType("MD5Sum:", *lines))
108 +           pkg->md5sum = parseGenericFieldType("MD5Sum", *lines);
109 +       else if(isGenericFieldType("Size:", *lines))
110 +           pkg->size = parseGenericFieldType("Size", *lines);
111 +       else if(isGenericFieldType("Source:", *lines))
112 +           pkg->source = parseGenericFieldType("Source", *lines);
113 +       else if(isGenericFieldType("Installed-Size:", *lines))
114 +           pkg->installed_size = parseGenericFieldType("Installed-Size", *lines);
115 +       else if(isGenericFieldType("Installed-Time:", *lines)) {
116 +            char *time_str = parseGenericFieldType("Installed-Time", *lines);
117 +            pkg->installed_time = strtoul(time_str, NULL, 0);
118 +       } else if(isGenericFieldType("Priority:", *lines))
119 +           pkg->priority = parseGenericFieldType("Priority", *lines);
120 +       else if(isGenericFieldType("Essential:", *lines)) {
121 +           char *essential_value;
122 +           essential_value = parseGenericFieldType("Essential", *lines);
123 +           if (strcmp(essential_value, "yes") == 0) {
124 +               pkg->essential = 1;
125 +           }
126 +           free(essential_value);
127 +       }
128 +       else if(isGenericFieldType("Status", *lines))
129 +           parseStatus(pkg, *lines);
130 +       else if(isGenericFieldType("Version", *lines))
131 +           parseVersion(pkg, *lines);
132 +       else if(isGenericFieldType("Maintainer", *lines))
133 +           pkg->maintainer = parseGenericFieldType("Maintainer", *lines);
134 +       else if(isGenericFieldType("Conffiles", *lines)){
135 +           parseConffiles(pkg, *lines);
136 +           reading_conffiles = 1;
137 +       }
138 +       else if(isGenericFieldType("Description", *lines)) {
139 +           pkg->description = parseGenericFieldType("Description", *lines);
140 +           reading_conffiles = 0;
141 +           reading_description = 1;
142 +       }
143 +
144 +       else if(isGenericFieldType("Provides", *lines)){
145 +/* Here we add the internal_use to align the off by one problem between provides_str and provides */
146 +            provide = (char * ) malloc(strlen(*lines)+ 35 ); /* Preparing the space for the new ipkg_internal_use_only */
147 +            if ( alterProvidesLine(*lines,provide) ){
148 +               return EINVAL;
149 +            }
150 +           pkg->provides_str = parseDependsString( provide, &pkg->provides_count);
151 +/* Let's try to hack a bit here.
152 +   The idea is that if a package has no Provides, we would add one generic, to permit the check of dependencies
153 +   in alot of other places. We will remove it before writing down the status database */
154 +            pkg_false_provides=0;
155 +            free(provide);
156 +        } 
157 +
158 +       else if(isGenericFieldType("Depends", *lines))
159 +           pkg->depends_str = parseDependsString(*lines, &pkg->depends_count);
160 +       else if(isGenericFieldType("Pre-Depends", *lines))
161 +           pkg->pre_depends_str = parseDependsString(*lines, &pkg->pre_depends_count);
162 +       else if(isGenericFieldType("Recommends", *lines))
163 +           pkg->recommends_str = parseDependsString(*lines, &pkg->recommends_count);
164 +       else if(isGenericFieldType("Suggests", *lines))
165 +           pkg->suggests_str = parseDependsString(*lines, &pkg->suggests_count);
166 +       /* Abhaya: support for conflicts */
167 +       else if(isGenericFieldType("Conflicts", *lines))
168 +           pkg->conflicts_str = parseDependsString(*lines, &pkg->conflicts_count);
169 +       else if(isGenericFieldType("Replaces", *lines))
170 +           pkg->replaces_str = parseDependsString(*lines, &pkg->replaces_count);
171 +       else if(line_is_blank(*lines)) {
172 +           break;
173 +       }
174 +       else if(**lines == ' '){
175 +           if(reading_description) {
176 +               /* we already know it's not blank, so the rest of description */      
177 +               pkg->description = realloc(pkg->description,
178 +                                          strlen(pkg->description)
179 +                                          + 1 + strlen(*lines) + 1);
180 +               strcat(pkg->description, "\n");
181 +               strcat(pkg->description, (*lines));
182 +           }
183 +           else if(reading_conffiles)
184 +               parseConffiles(pkg, *lines);
185 +       }
186 +    }
187 +/* If the ipk has not a Provides line, we insert our false line */ 
188 +    if ( pkg_false_provides==1)
189 +       pkg->provides_str = parseDependsString ((char *)"Provides: ipkg_internal_use_only ", &pkg->provides_count);
190 +
191 +    free(buf);
192 +    if (pkg->name) {
193 +       return 0;
194 +    } else {
195 +       return EINVAL;
196 +    }
197 +}
198 +
199 +#if 0
200 +
201 +/* Some random thoughts from Carl:
202 +
203 +   This function could be considerably simplified if we just kept
204 +   an array of all the generic string-valued field names, and looped
205 +   through those looking for a match. Also, these fields could perhaps
206 +   be stored in the package as an array as well, (or, probably better,
207 +   as an nv_pair_list_t).
208 +
209 +   Fields which require special parsing or storage, (such as Depends:
210 +   and Status:) could be handled as they are now. 
211 +*/
212 +/* XXX: FEATURE: The Suggests: field needs to be changed from a string
213 +   to a dependency list. And, since we already have
214 +   Depends/Pre-Depends and need to add Conflicts, Recommends, and
215 +   Enhances, perhaps we could generalize all of these and save some
216 +   code duplication.
217 +*/
218  int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest)
219  {
220      int reading_conffiles, reading_description;
221 @@ -342,6 +497,7 @@
222         return EINVAL;
223      }
224  }
225 +#endif
226  
227  int pkg_valorize_other_field(pkg_t *pkg, char ***raw)
228  {
229 --- a/archival/libipkg/pkg_parse.h
230 +++ b/archival/libipkg/pkg_parse.h
231 @@ -25,7 +25,10 @@
232  char ** parseDependsString(char * raw, int * depends_count);
233  int parseVersion(pkg_t *pkg, char *raw);
234  void parseConffiles(pkg_t * pkg, char * raw);
235 +#if 0
236  int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest);
237 +#endif
238 +int pkg_parse_stream(pkg_t *pkg, FILE *stream, pkg_src_t *src, pkg_dest_t *dest);
239  int pkg_valorize_other_field(pkg_t *pkg, char ***raw);
240  
241  #endif