From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753474AbdJTUKj (ORCPT ); Fri, 20 Oct 2017 16:10:39 -0400 Received: from mga05.intel.com ([192.55.52.43]:58915 "EHLO mga05.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750740AbdJTUKV (ORCPT ); Fri, 20 Oct 2017 16:10:21 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.43,408,1503385200"; d="scan'208";a="325761699" From: kan.liang@intel.com To: acme@kernel.org, mingo@redhat.com, linux-kernel@vger.kernel.org Cc: peterz@infradead.org, jolsa@kernel.org, wangnan0@huawei.com, hekuang@huawei.com, namhyung@kernel.org, alexander.shishkin@linux.intel.com, adrian.hunter@intel.com, ak@linux.intel.com, Kan Liang Subject: [PATCH V3 4/6] perf tools: add perf_data_file__open_tmp Date: Fri, 20 Oct 2017 13:05:32 -0700 Message-Id: <1508529934-369393-5-git-send-email-kan.liang@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1508529934-369393-1-git-send-email-kan.liang@intel.com> References: <1508529934-369393-1-git-send-email-kan.liang@intel.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kan Liang And an interface for perf_data_file to open tmp file. Automatically generate the tmp file name if it's not assigned. The name cannot be const char. Introduce char *tmp_path for it. The tmp file will be deleted after close. It will be used as per-thread file to temporarily store event synthesizd result in the following patch. Signed-off-by: Kan Liang --- tools/perf/util/data.c | 26 ++++++++++++++++++++++++++ tools/perf/util/data.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/tools/perf/util/data.c b/tools/perf/util/data.c index 1123b30..cd6fdf9 100644 --- a/tools/perf/util/data.c +++ b/tools/perf/util/data.c @@ -139,9 +139,35 @@ int perf_data_file__open(struct perf_data_file *file) return open_file(file); } +int perf_data_file__open_tmp(struct perf_data_file *file) +{ + int fd; + + if (!file->tmp_path && + (asprintf(&file->tmp_path, "perf.tmp.XXXXXX") < 0)) + goto err; + + fd = mkstemp(file->tmp_path); + if (fd < 0) { + free(file->tmp_path); + goto err; + } + + file->fd = fd; + return 0; +err: + file->tmp_path = NULL; + return -1; +} + void perf_data_file__close(struct perf_data_file *file) { close(file->fd); + if (file->tmp_path) { + unlink(file->tmp_path); + free(file->tmp_path); + file->tmp_path = NULL; + } } ssize_t perf_data_file__write(struct perf_data_file *file, diff --git a/tools/perf/util/data.h b/tools/perf/util/data.h index ae510ce..892b3d5 100644 --- a/tools/perf/util/data.h +++ b/tools/perf/util/data.h @@ -10,6 +10,7 @@ enum perf_data_mode { struct perf_data_file { const char *path; + char *tmp_path; int fd; bool is_pipe; bool force; @@ -43,6 +44,7 @@ static inline unsigned long perf_data_file__size(struct perf_data_file *file) } int perf_data_file__open(struct perf_data_file *file); +int perf_data_file__open_tmp(struct perf_data_file *file); void perf_data_file__close(struct perf_data_file *file); ssize_t perf_data_file__write(struct perf_data_file *file, void *buf, size_t size); -- 2.7.4