From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id C54971384B4 for ; Sat, 28 Nov 2015 02:44:48 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 8A79B21C042; Sat, 28 Nov 2015 02:44:41 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id E2CF821C089 for ; Sat, 28 Nov 2015 02:44:39 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id CF39F3406F3 for ; Sat, 28 Nov 2015 02:44:38 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id E2304E82 for ; Sat, 28 Nov 2015 02:44:34 +0000 (UTC) From: "Mike Frysinger" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Mike Frysinger" Message-ID: <1448655247.be66cbd9a8e495972f1e85398ce6bde98c210c7b.vapier@gentoo> Subject: [gentoo-commits] proj/portage-utils:master commit in: / X-VCS-Repository: proj/portage-utils X-VCS-Files: qcache.c X-VCS-Directories: / X-VCS-Committer: vapier X-VCS-Committer-Name: Mike Frysinger X-VCS-Revision: be66cbd9a8e495972f1e85398ce6bde98c210c7b X-VCS-Branch: master Date: Sat, 28 Nov 2015 02:44:34 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 52894c5e-8fd9-49e1-9a5e-93d96c4b28e9 X-Archives-Hash: d85111051c5208ec283646a92b58e272 commit: be66cbd9a8e495972f1e85398ce6bde98c210c7b Author: Mike Frysinger gentoo org> AuthorDate: Fri Nov 27 20:14:07 2015 +0000 Commit: Mike Frysinger gentoo org> CommitDate: Fri Nov 27 20:14:07 2015 +0000 URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=be66cbd9 qcache: rewrite arch.list loading to use getline() This applet carries its own file reading utils that are inefficient (they read 1 byte off the disk at a time) and are used by only one piece of code -- the arch.list reader. Punt the funcs so we can use getline directly. qcache.c | 146 +++++++++++++++------------------------------------------------ 1 file changed, 33 insertions(+), 113 deletions(-) diff --git a/qcache.c b/qcache.c index fed3534..af64bc5 100644 --- a/qcache.c +++ b/qcache.c @@ -204,105 +204,6 @@ int read_keywords(char *s, int *keywords) return 0; } -/********************************************************************/ -/* File reading helper functions */ -/********************************************************************/ - -/* - * inline unsigned int qcache_count_lines(char *filename); - * - * Count the number of new line characters '\n' in a file. - * - * IN: - * char *filename - name of the file to read. - * OUT: - * unsigned int count - number of new lines counted. - * ERR: - * -1 is returned if the file cannot be read. - */ -_q_static -unsigned int qcache_count_lines(char *filename) -{ - int count, fd; - char c; - - if ((fd = open(filename, O_RDONLY|O_CLOEXEC)) != -1) { - count = 0; - - while (read(fd, &c, 1) == 1) - if (c == '\n') - count++; - - close(fd); - return count; - } - - return -1; -} - -/* - * char **qcache_read_lines(char *filename); - * - * Reads in every line contained in a file - * - * IN: - * char *filename - name of the file to read. - * OUT: - * char **lines - number of new lines counted. - * ERR: - * NULL is returned if an error occurs. - */ -_q_static -char **qcache_read_lines(char *filename) -{ - int len, fd, count, i, num_lines; - char **lines, c; - - if (-1 == (num_lines = qcache_count_lines(filename))) - return NULL; - - len = sizeof(char*) * (num_lines + 1); - lines = xzalloc(len); - - if ((fd = open(filename, O_RDONLY|O_CLOEXEC)) != -1) { - for (i = 0; i < num_lines; i++) { - count = 0; - - /* determine the space needed for storing the line */ - while (read(fd, &c, 1) == 1 && c != '\n') - count++; - lseek(fd, (lseek(fd, 0, SEEK_CUR) - count - 1), SEEK_SET); - - lines[i] = xzalloc(sizeof(char) * (count+1)); - - /* copy the line into lines[i] */ - assert(read(fd, lines[i], count) == count); - assert(read(fd, &c, 1) == 1); /* skip '\n' */ - } - - close(fd); - return lines; - } - - return NULL; -} - -/* - * void qcache_free_lines(char **lines); - * - * free()'s memory allocated by qcache_read_lines - */ -_q_static -void qcache_free_lines(char **lines) -{ - int i; - - for (i = 0; lines[i]; i++) - free(lines[i]); - - free(lines); -} - /* * portage_cache *qcache_read_cache_file(const char *file); * @@ -884,25 +785,40 @@ void qcache_testing_only(qcache_data *data) * -1 is returned on error. */ _q_static -int qcache_init(void) +bool qcache_init(void) { - char *filename; - int len; + bool ret = false; + FILE *fp; + char *filename, *s; + size_t buflen, linelen; + char *buf; xasprintf(&filename, "%s/profiles/arch.list", portdir); + fp = fopen(filename, "re"); + if (!fp) + goto done; - if (NULL == (archlist = qcache_read_lines(filename))) { - free(filename); - return -1; - } + archlist_count = 0; + buf = NULL; + while ((linelen = getline(&buf, &buflen, fp)) != -1) { + rmspace_len(buf, linelen); - len = 0; - while (archlist[len]) - ++len; - archlist_count = len; + if ((s = strchr(buf, '#')) != NULL) + *s = '\0'; + if (buf[0] == '\0') + continue; + + ++archlist_count; + archlist = xrealloc_array(archlist, sizeof(*archlist), archlist_count); + archlist[archlist_count - 1] = xstrdup(buf); + } + free(buf); + ret = true; + fclose(fp); + done: free(filename); - return 0; + return ret; } /* @@ -913,7 +829,11 @@ int qcache_init(void) _q_static void qcache_free(void) { - qcache_free_lines(archlist); + size_t a; + + for (a = 0; a < archlist_count; ++a) + free(archlist[a]); + free(archlist); } /********************************************************************/ @@ -946,7 +866,7 @@ int qcache_main(int argc, char **argv) } } - if (-1 == qcache_init()) + if (!qcache_init()) err("Could not initialize arch list"); if (optind < argc)