1 |
commit: be66cbd9a8e495972f1e85398ce6bde98c210c7b |
2 |
Author: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
3 |
AuthorDate: Fri Nov 27 20:14:07 2015 +0000 |
4 |
Commit: Mike Frysinger <vapier <AT> gentoo <DOT> org> |
5 |
CommitDate: Fri Nov 27 20:14:07 2015 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=be66cbd9 |
7 |
|
8 |
qcache: rewrite arch.list loading to use getline() |
9 |
|
10 |
This applet carries its own file reading utils that are inefficient |
11 |
(they read 1 byte off the disk at a time) and are used by only one |
12 |
piece of code -- the arch.list reader. Punt the funcs so we can use |
13 |
getline directly. |
14 |
|
15 |
qcache.c | 146 +++++++++++++++------------------------------------------------ |
16 |
1 file changed, 33 insertions(+), 113 deletions(-) |
17 |
|
18 |
diff --git a/qcache.c b/qcache.c |
19 |
index fed3534..af64bc5 100644 |
20 |
--- a/qcache.c |
21 |
+++ b/qcache.c |
22 |
@@ -204,105 +204,6 @@ int read_keywords(char *s, int *keywords) |
23 |
return 0; |
24 |
} |
25 |
|
26 |
-/********************************************************************/ |
27 |
-/* File reading helper functions */ |
28 |
-/********************************************************************/ |
29 |
- |
30 |
-/* |
31 |
- * inline unsigned int qcache_count_lines(char *filename); |
32 |
- * |
33 |
- * Count the number of new line characters '\n' in a file. |
34 |
- * |
35 |
- * IN: |
36 |
- * char *filename - name of the file to read. |
37 |
- * OUT: |
38 |
- * unsigned int count - number of new lines counted. |
39 |
- * ERR: |
40 |
- * -1 is returned if the file cannot be read. |
41 |
- */ |
42 |
-_q_static |
43 |
-unsigned int qcache_count_lines(char *filename) |
44 |
-{ |
45 |
- int count, fd; |
46 |
- char c; |
47 |
- |
48 |
- if ((fd = open(filename, O_RDONLY|O_CLOEXEC)) != -1) { |
49 |
- count = 0; |
50 |
- |
51 |
- while (read(fd, &c, 1) == 1) |
52 |
- if (c == '\n') |
53 |
- count++; |
54 |
- |
55 |
- close(fd); |
56 |
- return count; |
57 |
- } |
58 |
- |
59 |
- return -1; |
60 |
-} |
61 |
- |
62 |
-/* |
63 |
- * char **qcache_read_lines(char *filename); |
64 |
- * |
65 |
- * Reads in every line contained in a file |
66 |
- * |
67 |
- * IN: |
68 |
- * char *filename - name of the file to read. |
69 |
- * OUT: |
70 |
- * char **lines - number of new lines counted. |
71 |
- * ERR: |
72 |
- * NULL is returned if an error occurs. |
73 |
- */ |
74 |
-_q_static |
75 |
-char **qcache_read_lines(char *filename) |
76 |
-{ |
77 |
- int len, fd, count, i, num_lines; |
78 |
- char **lines, c; |
79 |
- |
80 |
- if (-1 == (num_lines = qcache_count_lines(filename))) |
81 |
- return NULL; |
82 |
- |
83 |
- len = sizeof(char*) * (num_lines + 1); |
84 |
- lines = xzalloc(len); |
85 |
- |
86 |
- if ((fd = open(filename, O_RDONLY|O_CLOEXEC)) != -1) { |
87 |
- for (i = 0; i < num_lines; i++) { |
88 |
- count = 0; |
89 |
- |
90 |
- /* determine the space needed for storing the line */ |
91 |
- while (read(fd, &c, 1) == 1 && c != '\n') |
92 |
- count++; |
93 |
- lseek(fd, (lseek(fd, 0, SEEK_CUR) - count - 1), SEEK_SET); |
94 |
- |
95 |
- lines[i] = xzalloc(sizeof(char) * (count+1)); |
96 |
- |
97 |
- /* copy the line into lines[i] */ |
98 |
- assert(read(fd, lines[i], count) == count); |
99 |
- assert(read(fd, &c, 1) == 1); /* skip '\n' */ |
100 |
- } |
101 |
- |
102 |
- close(fd); |
103 |
- return lines; |
104 |
- } |
105 |
- |
106 |
- return NULL; |
107 |
-} |
108 |
- |
109 |
-/* |
110 |
- * void qcache_free_lines(char **lines); |
111 |
- * |
112 |
- * free()'s memory allocated by qcache_read_lines |
113 |
- */ |
114 |
-_q_static |
115 |
-void qcache_free_lines(char **lines) |
116 |
-{ |
117 |
- int i; |
118 |
- |
119 |
- for (i = 0; lines[i]; i++) |
120 |
- free(lines[i]); |
121 |
- |
122 |
- free(lines); |
123 |
-} |
124 |
- |
125 |
/* |
126 |
* portage_cache *qcache_read_cache_file(const char *file); |
127 |
* |
128 |
@@ -884,25 +785,40 @@ void qcache_testing_only(qcache_data *data) |
129 |
* -1 is returned on error. |
130 |
*/ |
131 |
_q_static |
132 |
-int qcache_init(void) |
133 |
+bool qcache_init(void) |
134 |
{ |
135 |
- char *filename; |
136 |
- int len; |
137 |
+ bool ret = false; |
138 |
+ FILE *fp; |
139 |
+ char *filename, *s; |
140 |
+ size_t buflen, linelen; |
141 |
+ char *buf; |
142 |
|
143 |
xasprintf(&filename, "%s/profiles/arch.list", portdir); |
144 |
+ fp = fopen(filename, "re"); |
145 |
+ if (!fp) |
146 |
+ goto done; |
147 |
|
148 |
- if (NULL == (archlist = qcache_read_lines(filename))) { |
149 |
- free(filename); |
150 |
- return -1; |
151 |
- } |
152 |
+ archlist_count = 0; |
153 |
+ buf = NULL; |
154 |
+ while ((linelen = getline(&buf, &buflen, fp)) != -1) { |
155 |
+ rmspace_len(buf, linelen); |
156 |
|
157 |
- len = 0; |
158 |
- while (archlist[len]) |
159 |
- ++len; |
160 |
- archlist_count = len; |
161 |
+ if ((s = strchr(buf, '#')) != NULL) |
162 |
+ *s = '\0'; |
163 |
+ if (buf[0] == '\0') |
164 |
+ continue; |
165 |
+ |
166 |
+ ++archlist_count; |
167 |
+ archlist = xrealloc_array(archlist, sizeof(*archlist), archlist_count); |
168 |
+ archlist[archlist_count - 1] = xstrdup(buf); |
169 |
+ } |
170 |
+ free(buf); |
171 |
|
172 |
+ ret = true; |
173 |
+ fclose(fp); |
174 |
+ done: |
175 |
free(filename); |
176 |
- return 0; |
177 |
+ return ret; |
178 |
} |
179 |
|
180 |
/* |
181 |
@@ -913,7 +829,11 @@ int qcache_init(void) |
182 |
_q_static |
183 |
void qcache_free(void) |
184 |
{ |
185 |
- qcache_free_lines(archlist); |
186 |
+ size_t a; |
187 |
+ |
188 |
+ for (a = 0; a < archlist_count; ++a) |
189 |
+ free(archlist[a]); |
190 |
+ free(archlist); |
191 |
} |
192 |
|
193 |
/********************************************************************/ |
194 |
@@ -946,7 +866,7 @@ int qcache_main(int argc, char **argv) |
195 |
} |
196 |
} |
197 |
|
198 |
- if (-1 == qcache_init()) |
199 |
+ if (!qcache_init()) |
200 |
err("Could not initialize arch list"); |
201 |
|
202 |
if (optind < argc) |