From nobody Sun Nov 16 14:28:24 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600533376; cv=none; d=zohomail.com; s=zohoarc; b=KVcUO/GZPtd7BoRlfsIbYK8BjlbYpT7GFiizFrmrhG2wBlNNuzCmr8Y8XBq9kNnx0Cx27v6wkDQkBiequnHEWsPsJrbRbfD+I3oMsD8TH6iQ2XIio7Q20YZuFxxIz6KWlP3/rdReTFOmpzQNBEjHVJYugxq2ecLrRTwyl2T2oM0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600533376; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=XnSf1LAuLV+MUmLsNDrigQ0JXDrj9izEtgHHuUjdSPY=; b=BgUwn5Rb4sy/dBjzel5hWzteWqQcwdU+9uMv2HSSPlUh9J4HGU/Cgrwpu7jbNTFp76kjbr917VZ6hDu/vzi54T9Mc3VJDjyJYxXtfbvs1CeSG6Ksc1hF/z9upeancekgFQEjkKU6qBiny8kPaPDoHV+e40XIREUq22aNoNiMrpk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1600533376315824.8300948282596; Sat, 19 Sep 2020 09:36:16 -0700 (PDT) Received: from localhost ([::1]:49982 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kJfqQ-0000a0-UZ for importer@patchew.org; Sat, 19 Sep 2020 12:36:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:51820) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kJfHd-0005mm-Gf for qemu-devel@nongnu.org; Sat, 19 Sep 2020 12:00:18 -0400 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:22710) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kJfHG-0007Rm-SA for qemu-devel@nongnu.org; Sat, 19 Sep 2020 12:00:17 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-208-XKuddB2gNL6PicyhRwQy3g-1; Sat, 19 Sep 2020 11:59:51 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2DFCE8712D9 for ; Sat, 19 Sep 2020 15:59:50 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id F18845576A for ; Sat, 19 Sep 2020 15:59:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600531193; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XnSf1LAuLV+MUmLsNDrigQ0JXDrj9izEtgHHuUjdSPY=; b=IhZHhmVvJRw2KJTibc5F/FK6whbMX0xFWhHEKJenV5C2KMfdLWSp2XCuVjYPqiifVUgPSh pjgiM+I+kyYOE4eHx3B0ijAMDCKhyLQhFFmi004uXuB9ZvWoJQOHEgK7CsjuECZ3kVyr+W /tfMiEcII9vZnOgRTxoSo8pE9aRPyFs= X-MC-Unique: XKuddB2gNL6PicyhRwQy3g-1 From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 44/57] cutils: introduce get_relocated_path Date: Sat, 19 Sep 2020 11:59:03 -0400 Message-Id: <20200919155916.1046398-45-pbonzini@redhat.com> In-Reply-To: <20200919155916.1046398-1-pbonzini@redhat.com> References: <20200919155916.1046398-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=pbonzini@redhat.com X-Mimecast-Spam-Score: 0.001 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.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; Received-SPF: pass client-ip=63.128.21.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/19 11:03:07 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -50 X-Spam_score: -5.1 X-Spam_bar: ----- X-Spam_report: (-5.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Add the function that will compute a relocated version of the directories in CONFIG_QEMU_*DIR and CONFIG_QEMU_*PATH. Signed-off-by: Paolo Bonzini --- include/qemu/cutils.h | 12 +++++++++ meson.build | 4 +-- util/cutils.c | 61 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h index eb59852dfd..3a86ec0321 100644 --- a/include/qemu/cutils.h +++ b/include/qemu/cutils.h @@ -184,4 +184,16 @@ int uleb128_decode_small(const uint8_t *in, uint32_t *= n); */ int qemu_pstrcmp0(const char **str1, const char **str2); =20 + +/** + * get_relocated_path: + * @dir: the directory (typically a `CONFIG_*DIR` variable) to be relocate= d. + * + * Returns a path for @dir that uses the directory of the running executab= le + * as the prefix. For example, if `bindir` is `/usr/bin` and @dir is + * `/usr/share/qemu`, the function will append `../share/qemu` to the + * directory that contains the running executable and return the result. + */ +char *get_relocated_path(const char *dir); + #endif diff --git a/meson.build b/meson.build index 1f8487eacf..1b1cde8725 100644 --- a/meson.build +++ b/meson.build @@ -566,9 +566,9 @@ config_host_data.set('QEMU_VERSION_MINOR', meson.projec= t_version().split('.')[1] config_host_data.set('QEMU_VERSION_MICRO', meson.project_version().split('= .')[2]) =20 arrays =3D ['CONFIG_AUDIO_DRIVERS', 'CONFIG_BDRV_RW_WHITELIST', 'CONFIG_BD= RV_RO_WHITELIST'] -strings =3D ['HOST_DSOSUF', 'CONFIG_IASL', 'bindir', 'qemu_confdir', 'qemu= _datadir', +strings =3D ['HOST_DSOSUF', 'CONFIG_IASL', 'bindir', 'prefix', 'qemu_confd= ir', 'qemu_datadir', 'qemu_moddir', 'qemu_localstatedir', 'qemu_helperdir', 'qemu_lo= caledir', - 'qemu_icondir', 'qemu_desktopdir', 'qemu_firmwarepath'] + 'qemu_icondir', 'qemu_desktopdir', 'qemu_firmwarepath', 'syscon= fdir'] foreach k, v: config_host if arrays.contains(k) if v !=3D '' diff --git a/util/cutils.c b/util/cutils.c index 36ce712271..8da34e04b0 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -889,3 +889,64 @@ int qemu_pstrcmp0(const char **str1, const char **str2) { return g_strcmp0(*str1, *str2); } + +static inline bool starts_with_prefix(const char *dir) +{ + size_t prefix_len =3D strlen(CONFIG_PREFIX); + return !memcmp(dir, CONFIG_PREFIX, prefix_len) && + (!dir[prefix_len] || G_IS_DIR_SEPARATOR(dir[prefix_len])); +} + +/* Return the next path component in dir, and store its length in *p_len. = */ +static inline const char *next_component(const char *dir, int *p_len) +{ + int len; + while (*dir && G_IS_DIR_SEPARATOR(*dir)) { + dir++; + } + len =3D 0; + while (dir[len] && !G_IS_DIR_SEPARATOR(dir[len])) { + len++; + } + *p_len =3D len; + return dir; +} + +char *get_relocated_path(const char *dir) +{ + size_t prefix_len =3D strlen(CONFIG_PREFIX); + const char *bindir =3D CONFIG_BINDIR; + const char *exec_dir =3D qemu_get_exec_dir(); + GString *result; + int len_dir, len_bindir; + + /* Fail if qemu_init_exec_dir was not called. */ + assert(exec_dir[0]); + if (!starts_with_prefix(dir) || !starts_with_prefix(bindir)) { + return strdup(dir); + } + + result =3D g_string_new(exec_dir); + + /* Advance over common components. */ + len_dir =3D len_bindir =3D prefix_len; + do { + dir +=3D len_dir; + bindir +=3D len_bindir; + dir =3D next_component(dir, &len_dir); + bindir =3D next_component(bindir, &len_bindir); + } while (len_dir =3D=3D len_bindir && !memcmp(dir, bindir, len_dir)); + + /* Ascend from bindir to the common prefix with dir. */ + while (len_bindir) { + bindir +=3D len_bindir; + g_string_append(result, "/.."); + bindir =3D next_component(bindir, &len_bindir); + } + + if (*dir) { + assert(G_IS_DIR_SEPARATOR(dir[-1])); + g_string_append(result, dir - 1); + } + return result->str; +} --=20 2.26.2