From patchwork Thu Apr 7 02:46:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Jiangang X-Patchwork-Id: 8768151 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 2E0D29F36E for ; Thu, 7 Apr 2016 02:49:30 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4E375201BC for ; Thu, 7 Apr 2016 02:49:29 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 36934201CD for ; Thu, 7 Apr 2016 02:49:28 +0000 (UTC) Received: from localhost ([::1]:47091 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ao00d-0006PW-Mu for patchwork-qemu-devel@patchwork.kernel.org; Wed, 06 Apr 2016 22:49:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46430) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1anzz5-0003eA-Qo for qemu-devel@nongnu.org; Wed, 06 Apr 2016 22:47:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1anzz4-00028n-D4 for qemu-devel@nongnu.org; Wed, 06 Apr 2016 22:47:51 -0400 Received: from [59.151.112.132] (port=50541 helo=heian.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1anzz3-00023u-Ok for qemu-devel@nongnu.org; Wed, 06 Apr 2016 22:47:50 -0400 X-IronPort-AV: E=Sophos;i="5.22,518,1449504000"; d="scan'208";a="5345495" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 07 Apr 2016 10:47:28 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 3D5FE42B55EB; Thu, 7 Apr 2016 10:47:26 +0800 (CST) Received: from localhost.localdomain (10.167.226.50) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server (TLS) id 14.3.279.2; Thu, 7 Apr 2016 10:47:25 +0800 From: Wei Jiangang To: Date: Thu, 7 Apr 2016 10:46:25 +0800 Message-ID: <1459997185-15669-4-git-send-email-weijg.fnst@cn.fujitsu.com> X-Mailer: git-send-email 1.9.3 In-Reply-To: <1459997185-15669-1-git-send-email-weijg.fnst@cn.fujitsu.com> References: <1459997185-15669-1-git-send-email-weijg.fnst@cn.fujitsu.com> MIME-Version: 1.0 X-Originating-IP: [10.167.226.50] X-yoursite-MailScanner-ID: 3D5FE42B55EB.A3DA1 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: weijg.fnst@cn.fujitsu.com X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 59.151.112.132 Cc: pbonzini@redhat.com, Wei Jiangang , peter.maydell@linaro.org Subject: [Qemu-devel] [PATCH 3/3] use g_path_get_basename instead of basename X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Using g_strdup and g_basename to get the last component of filename is not the best solution, Only g_path_get_basename can achive the purpose we want. Signed-off-by: Wei Jiangang --- fsdev/virtfs-proxy-helper.c | 6 +++++- hw/9pfs/9p-local.c | 6 +++--- hw/vfio/pci.c | 6 ++++-- hw/vfio/platform.c | 6 ++++-- qemu-io.c | 33 ++++++++++++++++++++------------- qemu-nbd.c | 4 +++- qga/commands-posix.c | 4 ++-- 7 files changed, 41 insertions(+), 24 deletions(-) diff --git a/fsdev/virtfs-proxy-helper.c b/fsdev/virtfs-proxy-helper.c index 54f7ad1..a0d6118 100644 --- a/fsdev/virtfs-proxy-helper.c +++ b/fsdev/virtfs-proxy-helper.c @@ -787,6 +787,8 @@ error: static void usage(char *prog) { + char *base_filename = g_path_get_basename(prog); + fprintf(stderr, "usage: %s\n" " -p|--path 9p path to export\n" " {-f|--fd } socket file descriptor to be used\n" @@ -795,7 +797,9 @@ static void usage(char *prog) " access to this socket\n" " \tNote: -s & -f can not be used together\n" " [-n|--nodaemon] Run as a normal program\n", - basename(prog)); + base_filename); + + g_free(base_filename); } static int process_reply(int sock, int type, diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c index 16f45f4..4e6c17a 100644 --- a/hw/9pfs/9p-local.c +++ b/hw/9pfs/9p-local.c @@ -160,17 +160,17 @@ static int local_create_mapped_attr_dir(FsContext *ctx, const char *path) { int err; char *attr_dir; - char *tmp_path = g_strdup(path); + char *base_filename = g_path_get_basename(path); attr_dir = g_strdup_printf("%s/%s/%s", - ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR); + ctx->fs_root, base_filename, VIRTFS_META_DIR); err = mkdir(attr_dir, 0700); if (err < 0 && errno == EEXIST) { err = 0; } g_free(attr_dir); - g_free(tmp_path); + g_free(base_filename); return err; } diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index d091d8c..d23b871 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -2413,7 +2413,7 @@ static int vfio_initfn(PCIDevice *pdev) return -errno; } - vdev->vbasedev.name = g_strdup(basename(vdev->vbasedev.sysfsdev)); + vdev->vbasedev.name = g_path_get_basename(vdev->vbasedev.sysfsdev); vdev->vbasedev.ops = &vfio_pci_ops; vdev->vbasedev.type = VFIO_DEVICE_TYPE_PCI; @@ -2428,11 +2428,13 @@ static int vfio_initfn(PCIDevice *pdev) group_path[len] = 0; - group_name = basename(group_path); + group_name = g_path_get_basename(group_path); if (sscanf(group_name, "%d", &groupid) != 1) { error_report("vfio: error reading %s: %m", group_path); + g_free(group_name); return -errno; } + g_free(group_name); trace_vfio_initfn(vdev->vbasedev.name, groupid); diff --git a/hw/vfio/platform.c b/hw/vfio/platform.c index 1798a00..47134db 100644 --- a/hw/vfio/platform.c +++ b/hw/vfio/platform.c @@ -557,7 +557,7 @@ static int vfio_base_device_init(VFIODevice *vbasedev) /* @sysfsdev takes precedence over @host */ if (vbasedev->sysfsdev) { g_free(vbasedev->name); - vbasedev->name = g_strdup(basename(vbasedev->sysfsdev)); + vbasedev->name = g_path_get_basename(vbasedev->sysfsdev); } else { if (!vbasedev->name || strchr(vbasedev->name, '/')) { return -EINVAL; @@ -584,11 +584,13 @@ static int vfio_base_device_init(VFIODevice *vbasedev) group_path[len] = 0; - group_name = basename(group_path); + group_name = g_path_get_basename(group_path); if (sscanf(group_name, "%d", &groupid) != 1) { error_report("vfio: error reading %s: %m", group_path); + g_free(group_name); return -errno; } + g_free(group_name); trace_vfio_platform_base_device_init(vbasedev->name, groupid); diff --git a/qemu-io.c b/qemu-io.c index 0a738f1..2f5c616 100644 --- a/qemu-io.c +++ b/qemu-io.c @@ -249,6 +249,12 @@ static char *get_prompt(void) return prompt; } +static void cleanup_and_exit(int status) +{ + g_free(progname); + exit(status); +} + static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque, const char *fmt, ...) { @@ -440,7 +446,7 @@ int main(int argc, char **argv) signal(SIGPIPE, SIG_IGN); #endif - progname = basename(argv[0]); + progname = g_path_get_basename(argv[0]); qemu_init_exec_dir(argv[0]); module_call_init(MODULE_INIT_QOM); @@ -459,7 +465,7 @@ int main(int argc, char **argv) case 'd': if (bdrv_parse_discard_flags(optarg, &flags) < 0) { error_report("Invalid discard option: %s", optarg); - exit(1); + cleanup_and_exit(1); } break; case 'f': @@ -480,26 +486,26 @@ int main(int argc, char **argv) case 't': if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) { error_report("Invalid cache option: %s", optarg); - exit(1); + cleanup_and_exit(1); } break; case 'T': if (!trace_init_backends()) { - exit(1); /* error message will have been printed */ + cleanup_and_exit(1); /* error message will have been printed */ } break; case 'V': printf("%s version %s\n", progname, QEMU_VERSION); - exit(0); + cleanup_and_exit(0); case 'h': usage(progname); - exit(0); + cleanup_and_exit(0); case OPTION_OBJECT: { QemuOpts *qopts; qopts = qemu_opts_parse_noisily(&qemu_object_opts, optarg, true); if (!qopts) { - exit(1); + cleanup_and_exit(1); } } break; case OPTION_IMAGE_OPTS: @@ -507,30 +513,30 @@ int main(int argc, char **argv) break; default: usage(progname); - exit(1); + cleanup_and_exit(1); } } if ((argc - optind) > 1) { usage(progname); - exit(1); + cleanup_and_exit(1); } if (format && imageOpts) { error_report("--image-opts and -f are mutually exclusive"); - exit(1); + cleanup_and_exit(1); } if (qemu_init_main_loop(&local_error)) { error_report_err(local_error); - exit(1); + cleanup_and_exit(1); } if (qemu_opts_foreach(&qemu_object_opts, user_creatable_add_opts_foreach, NULL, &local_error)) { error_report_err(local_error); - exit(1); + cleanup_and_exit(1); } /* initialize commands */ @@ -557,7 +563,7 @@ int main(int argc, char **argv) QemuOpts *qopts = NULL; qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false); if (!qopts) { - exit(1); + cleanup_and_exit(1); } opts = qemu_opts_to_qdict(qopts, NULL); openfile(NULL, flags, writethrough, opts); @@ -578,5 +584,6 @@ int main(int argc, char **argv) blk_unref(qemuio_blk); g_free(readline_state); + g_free(progname); return 0; } diff --git a/qemu-nbd.c b/qemu-nbd.c index ca4a724..c54d82f 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -808,7 +808,9 @@ int main(int argc, char **argv) if (device != NULL && sockpath == NULL) { sockpath = g_malloc(128); - snprintf(sockpath, 128, SOCKET_PATH, basename(device)); + char *base_filename = g_path_get_basename(device); + snprintf(sockpath, 128, SOCKET_PATH, base_filename); + g_free(base_filename); } saddr = nbd_build_socket_address(sockpath, bindto, port); diff --git a/qga/commands-posix.c b/qga/commands-posix.c index 2ae3725..cc8736b 100644 --- a/qga/commands-posix.c +++ b/qga/commands-posix.c @@ -805,7 +805,7 @@ static char *get_pci_driver(char const *syspath, int pathlen, Error **errp) len = readlink(dpath, buf, sizeof(buf) - 1); if (len != -1) { buf[len] = 0; - driver = g_strdup(basename(buf)); + driver = g_path_get_basename(buf); } g_free(dpath); g_free(path); @@ -1048,7 +1048,7 @@ static void build_guest_fsinfo_for_device(char const *devpath, } if (!fs->name) { - fs->name = g_strdup(basename(syspath)); + fs->name = g_path_get_basename(syspath); } g_debug(" parse sysfs path '%s'", syspath);