Index: qfile.c =================================================================== RCS file: /var/cvsroot/gentoo-projects/portage-utils/qfile.c,v retrieving revision 1.52 diff -u -r1.52 qfile.c --- qfile.c 7 Apr 2010 05:58:16 -0000 1.52 +++ qfile.c 8 Jun 2010 19:23:21 -0000 @@ -51,6 +51,7 @@ char *bn_firstchars; short *non_orphans; char *real_root; + size_t real_root_length; char *exclude_pkg; char *exclude_slot; } qfile_args_t; @@ -75,12 +76,20 @@ int i, path_ok; char bn_firstchar; char *real_root = args->real_root; + size_t real_root_len = args->real_root_length; char **base_names = args->basenames; char **dir_names = args->dirnames; char **real_dir_names = args->realdirnames; char *bn_firstchars = args->bn_firstchars; short *non_orphans = args->non_orphans; + // these variables are for checking realpath(dirname(CONTENTS)) + char rpath[_Q_PATH_MAX + 1]; + char *_rpath = rpath + real_root_len; + char fullpath[_Q_PATH_MAX + 1]; + memcpy(fullpath, real_root, real_root_len); + fullpath[real_root_len] = '\0'; + if ((dir = opendir(path)) == NULL) { warnp("opendir(%s) failed", path); return; @@ -186,15 +195,21 @@ /* real_dir_name == dirname(CONTENTS) */ path_ok = 1; - else if (real_root[0]) { - char rpath[_Q_PATH_MAX + 1], *_rpath; - char *fullpath; - size_t real_root_len = strlen(real_root); - - xasprintf(&fullpath, "%s%s", real_root, e->name); - fullpath[real_root_len + dirname_len] = '\0'; - _rpath = rpath + real_root_len; - if (realpath(fullpath, rpath) == NULL) { + else { + /* reuse realpath(dirname(...)) from a previous CONTENTS entry + * if directory has not changed yet */ + if ((fullpath[0] == '\0') || + (fullpath[real_root_len + dirname_len] != '\0') || + (strncmp(fullpath + real_root_len, e->name, dirname_len))) + { + rpath[0] = '\0'; + memcpy(fullpath + real_root_len, e->name, dirname_len); + fullpath[real_root_len + dirname_len] = '\0'; + if (realpath(fullpath, rpath) == NULL) + rpath[0] = '\0'; + } + + if (rpath[0] == '\0') { if (verbose) { warnp("Could not read real path of \"%s\" (from %s)", fullpath, pkg); warn("We'll never know whether \"%s\" was a result for your query...", @@ -212,7 +227,6 @@ /* real_dir_name == realpath(dirname(CONTENTS)) */ path_ok = 1; } - free(fullpath); } } if (!path_ok) @@ -311,7 +325,7 @@ int i; int nb_of_queries = argc; char *pwd = NULL; - int real_root_length; + size_t real_root_length; char *real_root = NULL; char **basenames = NULL; char **dirnames = NULL; @@ -455,6 +469,7 @@ free(pwd); qfile_args->real_root = real_root; + qfile_args->real_root_length = real_root_length; qfile_args->basenames = basenames; qfile_args->dirnames = dirnames; qfile_args->realdirnames = realdirnames;