[PATCH] perf: Add build_id_cahce__read_build_id helper

From: Shang XiaoJing
Date: Thu Sep 01 2022 - 21:36:58 EST


Wrap repeated code in helper function build_id_cahce__read_build_id, which
gets build id if success, or return -1 for failed to read build id.

Signed-off-by: Shang XiaoJing <shangxiaojing@xxxxxxxxxx>
---
tools/perf/builtin-buildid-cache.c | 41 +++++++++++++++---------------
1 file changed, 20 insertions(+), 21 deletions(-)

diff --git a/tools/perf/builtin-buildid-cache.c b/tools/perf/builtin-buildid-cache.c
index cd381693658b..a55d1c6496ea 100644
--- a/tools/perf/builtin-buildid-cache.c
+++ b/tools/perf/builtin-buildid-cache.c
@@ -172,20 +172,31 @@ static int build_id_cache__add_kcore(const char *filename, bool force)
return 0;
}

-static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi)
+static int build_id_cahce__read_build_id(const char *filename, struct nsinfo *nsi,
+ struct build_id *bid)
{
- char sbuild_id[SBUILD_ID_SIZE];
- struct build_id bid;
- int err;
struct nscookie nsc;
+ int err;

nsinfo__mountns_enter(nsi, &nsc);
- err = filename__read_build_id(filename, &bid);
+ err = filename__read_build_id(filename, bid);
nsinfo__mountns_exit(&nsc);
if (err < 0) {
pr_debug("Couldn't read a build-id in %s\n", filename);
return -1;
}
+ return err;
+}
+
+static int build_id_cache__add_file(const char *filename, struct nsinfo *nsi)
+{
+ char sbuild_id[SBUILD_ID_SIZE];
+ struct build_id bid;
+ int err;
+
+ err = build_id_cahce__read_build_id(filename, nsi, &bid);
+ if (err < 0)
+ return -1;

build_id__sprintf(&bid, sbuild_id);
err = build_id_cache__add_s(sbuild_id, filename, nsi,
@@ -199,17 +210,11 @@ static int build_id_cache__remove_file(const char *filename, struct nsinfo *nsi)
{
char sbuild_id[SBUILD_ID_SIZE];
struct build_id bid;
- struct nscookie nsc;
-
int err;

- nsinfo__mountns_enter(nsi, &nsc);
- err = filename__read_build_id(filename, &bid);
- nsinfo__mountns_exit(&nsc);
- if (err < 0) {
- pr_debug("Couldn't read a build-id in %s\n", filename);
+ err = build_id_cahce__read_build_id(filename, nsi, &bid);
+ if (err < 0)
return -1;
- }

build_id__sprintf(&bid, sbuild_id);
err = build_id_cache__remove_s(sbuild_id);
@@ -302,17 +307,11 @@ static int build_id_cache__update_file(const char *filename, struct nsinfo *nsi)
{
char sbuild_id[SBUILD_ID_SIZE];
struct build_id bid;
- struct nscookie nsc;
-
int err;

- nsinfo__mountns_enter(nsi, &nsc);
- err = filename__read_build_id(filename, &bid);
- nsinfo__mountns_exit(&nsc);
- if (err < 0) {
- pr_debug("Couldn't read a build-id in %s\n", filename);
+ err = build_id_cahce__read_build_id(filename, nsi, &bid);
+ if (err < 0)
return -1;
- }
err = 0;

build_id__sprintf(&bid, sbuild_id);
--
2.17.1