From nobody Wed May 8 05:52:48 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1555479249; cv=none; d=zoho.com; s=zohoarc; b=AMADlxt1F4BRE2BQnBrlzoB7v16Phb5S+lM8rI3mRjbj1A5yiMmPmhgw/5FzGUAZwW5hCEIQH8VWMRaDUM7OtMvI4HjHk0u5sERllMZZstzYvpkbMpbcZIokL1Tk5oSpP3nai88FKIFwNSYIMn2NIcC2dVLnzsCg6a0CsNN3D60= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1555479249; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=edHYvGkb/ztc15P53t8TcKmKqwAq0u9n6VpHb5vlBY8=; b=SeI5wkOoR2EqgobA9a+EznbSpBHfQ514mCzOnIn6W67tbM1nuRjcOFTwwYdJRhbG+bs7FISnRMMQ1yKI1J6iI+v1ry8k+DEDutpy4q1advjbCEolLzS8eTzp0mwHZB7hIzLpjMfscUnAAis8EztYFGof5VEmM1xBTGpLtjARzKc= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1555479249343437.8849092436544; Tue, 16 Apr 2019 22:34:09 -0700 (PDT) Received: from localhost ([127.0.0.1]:47238 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hGdCo-0008WI-3N for importer@patchew.org; Wed, 17 Apr 2019 01:33:58 -0400 Received: from eggs.gnu.org ([209.51.188.92]:51346) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hGdBV-0007vy-MS for qemu-devel@nongnu.org; Wed, 17 Apr 2019 01:32:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hGdBT-0002ge-Ok for qemu-devel@nongnu.org; Wed, 17 Apr 2019 01:32:37 -0400 Received: from mail-pf1-x441.google.com ([2607:f8b0:4864:20::441]:41850) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hGdBQ-0002dl-4c for qemu-devel@nongnu.org; Wed, 17 Apr 2019 01:32:33 -0400 Received: by mail-pf1-x441.google.com with SMTP id 188so11528350pfd.8 for ; Tue, 16 Apr 2019 22:32:31 -0700 (PDT) Received: from localhost.localdomain (rrcs-66-91-136-155.west.biz.rr.com. [66.91.136.155]) by smtp.gmail.com with ESMTPSA id d129sm84513383pfa.142.2019.04.16.22.32.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Apr 2019 22:32:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=edHYvGkb/ztc15P53t8TcKmKqwAq0u9n6VpHb5vlBY8=; b=CTvnW+8aWgt9HYn7zQN3TWTOHwJ93OdE5lcjtTwMOomxbPPSd+Vb1HwZaeLLwrTW1o FzpYDWiMxG38dlw9rWT1Uom84YrJnROrG3VX5Wdfgy6+DqqUFHbqfN20FQoxKHbtBef4 EXAVDHoB2WXFej9F2YRGumP8SMTB2wvoxlTMCxuNC4EpLxnqcap0+BFNJr0ZoM7gfZij tlltGqoqa0W8udL7tMGM5CkwlAj6PSbLFLrC2Z4/JL/6Vpa2ZrCimew5hNK4YQQiVNVq a2lVONQIojUc06ER+AQCxRfj/zNRP5J+FGWt6kv7XHFWXv8R9XvLLLI0DYs3CIZlZDSX JOxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=edHYvGkb/ztc15P53t8TcKmKqwAq0u9n6VpHb5vlBY8=; b=hdDA1zhcZgmHFh/vm1g36vfH6A6lQVTVb79qml0M9IOmjpum+I6l9edNykF9S5Ype2 NKU+GGOesjdyXRAJ0S2TpbXaM+fGBn6Ub+J2e8fLQo9erXnT2AeBBw2Wwj62hQP2w3N8 YjvuHTR3cL+T3mEvJxuqWucKX9qujvMfuQ/ZBalx5bpThYGq4QrQzgSWtDFCHdupqBbR lilUNU02l96LAnCTh7F//WhN6YXJT1Knrd/jiVHgg1w0CR3eiif6FeF+4plWqREUjRxk 8oC3q0Qwa2bOBWSrTtX6fbg9xyyLkNBW9wcuG6SnBw4y/c15dMjTz6CmFjrdD4guztBA LTOA== X-Gm-Message-State: APjAAAU6oQdXxfhrHUQw/m5jalWS3+ARaYxv9xExIH3qNLYyn8q4F2Ib SD4TN7C4W5JVtajn9f0++t27yayN/fY= X-Google-Smtp-Source: APXvYqymUCBY5JvEBmt44v1KihIORl6lfG4cGqYZbyXGfwxnNuWbgMxPE25bs6bavt67uHkmNn+BIw== X-Received: by 2002:aa7:81d0:: with SMTP id c16mr88394466pfn.132.1555479150489; Tue, 16 Apr 2019 22:32:30 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Tue, 16 Apr 2019 19:32:25 -1000 Message-Id: <20190417053225.27505-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190417053225.27505-1-richard.henderson@linaro.org> References: <20190417053225.27505-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::441 Subject: [Qemu-devel] [PATCH 1/1] util/path: Do not cache all filenames at startup X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, laurent@vivier.eu, evgreen@chromium.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If one uses -L $PATH to point to a full chroot, the startup time is significant. In addition, the existing probing algorithm fails to handle symlink loops. Instead, probe individual paths on demand. Cache both positive and negative results within $PATH, so that any one filename is probed only once. Use glib filename functions for clarity. Signed-off-by: Richard Henderson --- util/path.c | 211 ++++++++++++++-------------------------------------- 1 file changed, 57 insertions(+), 154 deletions(-) diff --git a/util/path.c b/util/path.c index 7f9fc272fb..09f75f100a 100644 --- a/util/path.c +++ b/util/path.c @@ -8,170 +8,73 @@ #include #include "qemu/cutils.h" #include "qemu/path.h" +#include "qemu/thread.h" =20 -struct pathelem -{ - /* Name of this, eg. lib */ - char *name; - /* Full path name, eg. /usr/gnemul/x86-linux/lib. */ - char *pathname; - struct pathelem *parent; - /* Children */ - unsigned int num_entries; - struct pathelem *entries[0]; -}; - -static struct pathelem *base; - -/* First N chars of S1 match S2, and S2 is N chars long. */ -static int strneq(const char *s1, unsigned int n, const char *s2) -{ - unsigned int i; - - for (i =3D 0; i < n; i++) - if (s1[i] !=3D s2[i]) - return 0; - return s2[i] =3D=3D 0; -} - -static struct pathelem *add_entry(struct pathelem *root, const char *name, - unsigned type); - -static struct pathelem *new_entry(const char *root, - struct pathelem *parent, - const char *name) -{ - struct pathelem *new =3D g_malloc(sizeof(*new)); - new->name =3D g_strdup(name); - new->pathname =3D g_strdup_printf("%s/%s", root, name); - new->num_entries =3D 0; - return new; -} - -#define streq(a,b) (strcmp((a), (b)) =3D=3D 0) - -/* Not all systems provide this feature */ -#if defined(DT_DIR) && defined(DT_UNKNOWN) && defined(DT_LNK) -# define dirent_type(dirent) ((dirent)->d_type) -# define is_dir_maybe(type) \ - ((type) =3D=3D DT_DIR || (type) =3D=3D DT_UNKNOWN || (type) =3D=3D DT_= LNK) -#else -# define dirent_type(dirent) (1) -# define is_dir_maybe(type) (type) -#endif - -static struct pathelem *add_dir_maybe(struct pathelem *path) -{ - DIR *dir; - - if ((dir =3D opendir(path->pathname)) !=3D NULL) { - struct dirent *dirent; - - while ((dirent =3D readdir(dir)) !=3D NULL) { - if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){ - path =3D add_entry(path, dirent->d_name, dirent_type(diren= t)); - } - } - closedir(dir); - } - return path; -} - -static struct pathelem *add_entry(struct pathelem *root, const char *name, - unsigned type) -{ - struct pathelem **e; - - root->num_entries++; - - root =3D g_realloc(root, sizeof(*root) - + sizeof(root->entries[0])*root->num_entries); - e =3D &root->entries[root->num_entries-1]; - - *e =3D new_entry(root->pathname, root, name); - if (is_dir_maybe(type)) { - *e =3D add_dir_maybe(*e); - } - - return root; -} - -/* This needs to be done after tree is stabilized (ie. no more reallocs!).= */ -static void set_parents(struct pathelem *child, struct pathelem *parent) -{ - unsigned int i; - - child->parent =3D parent; - for (i =3D 0; i < child->num_entries; i++) - set_parents(child->entries[i], child); -} - -/* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */ -static const char * -follow_path(const struct pathelem *cursor, const char *name) -{ - unsigned int i, namelen; - - name +=3D strspn(name, "/"); - namelen =3D strcspn(name, "/"); - - if (namelen =3D=3D 0) - return cursor->pathname; - - if (strneq(name, namelen, "..")) - return follow_path(cursor->parent, name + namelen); - - if (strneq(name, namelen, ".")) - return follow_path(cursor, name + namelen); - - for (i =3D 0; i < cursor->num_entries; i++) - if (strneq(name, namelen, cursor->entries[i]->name)) - return follow_path(cursor->entries[i], name + namelen); - - /* Not found */ - return NULL; -} +static const char *base; +static GHashTable *hash; +static QemuMutex lock; =20 void init_paths(const char *prefix) { - char pref_buf[PATH_MAX]; - - if (prefix[0] =3D=3D '\0' || - !strcmp(prefix, "/")) + if (prefix[0] =3D=3D '\0' || !strcmp(prefix, "/")) { return; - - if (prefix[0] !=3D '/') { - char *cwd =3D getcwd(NULL, 0); - size_t pref_buf_len =3D sizeof(pref_buf); - - if (!cwd) - abort(); - pstrcpy(pref_buf, sizeof(pref_buf), cwd); - pstrcat(pref_buf, pref_buf_len, "/"); - pstrcat(pref_buf, pref_buf_len, prefix); - free(cwd); - } else - pstrcpy(pref_buf, sizeof(pref_buf), prefix + 1); - - base =3D new_entry("", NULL, pref_buf); - base =3D add_dir_maybe(base); - if (base->num_entries =3D=3D 0) { - g_free(base->pathname); - g_free(base->name); - g_free(base); - base =3D NULL; - } else { - set_parents(base, base); } + +#if GLIB_CHECK_VERSION(2, 58, 0) + base =3D g_canonicalize_filename(prefix, NULL); +#else + if (prefix[0] !=3D '/') { + char *cwd =3D g_get_current_dir(); + base =3D g_build_filename(cwd, prefix, NULL); + g_free(cwd); + } else { + base =3D g_strdup(prefix); + } +#endif + + hash =3D g_hash_table_new(g_str_hash, g_str_equal); + qemu_mutex_init(&lock); } =20 /* Look for path in emulation dir, otherwise return name. */ const char *path(const char *name) { - /* Only do absolute paths: quick and dirty, but should mostly be OK. - Could do relative by tracking cwd. */ - if (!base || !name || name[0] !=3D '/') - return name; + gpointer key, value; + char *ret; =20 - return follow_path(base, name) ?: name; + /* Only do absolute paths: quick and dirty, but should mostly be OK. = */ + if (!base || !name || name[0] !=3D '/') { + return name; + } + + qemu_mutex_lock(&lock); + + /* Have we looked up this file before? */ + if (g_hash_table_lookup_extended(hash, name, &key, &value)) { + ret =3D value ? value : name; + } else { + char *full_name, *save_name; + + save_name =3D g_strdup(name); +#if GLIB_CHECK_VERSION(2, 58, 0) + full_name =3D g_canonicalize_filename(g_path_skip_root(name), base= ); +#else + full_name =3D g_build_filename(base, name, NULL); +#endif + + /* Look for the path; record the result, pass or fail. */ + if (access(full_name, F_OK) =3D=3D 0) { + /* Exists. */ + g_hash_table_insert(hash, save_name, full_name); + ret =3D full_name; + } else { + /* Does not exist. */ + g_free(full_name); + g_hash_table_insert(hash, save_name, NULL); + ret =3D name; + } + } + + qemu_mutex_unlock(&lock); + return ret; } --=20 2.17.1