[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20220520132404.25853-3-adrian.hunter@intel.com>
Date: Fri, 20 May 2022 16:24:01 +0300
From: Adrian Hunter <adrian.hunter@...el.com>
To: Arnaldo Carvalho de Melo <acme@...nel.org>
Cc: Jiri Olsa <jolsa@...nel.org>, Namhyung Kim <namhyung@...nel.org>,
linux-kernel@...r.kernel.org
Subject: [PATCH 2/5] libperf: Add preadn()
Add preadn() to provide pread() and readn() semantics.
Signed-off-by: Adrian Hunter <adrian.hunter@...el.com>
---
tools/lib/perf/include/internal/lib.h | 2 ++
tools/lib/perf/lib.c | 20 ++++++++++++++++++++
2 files changed, 22 insertions(+)
diff --git a/tools/lib/perf/include/internal/lib.h b/tools/lib/perf/include/internal/lib.h
index 5175d491b2d4..85471a4b900f 100644
--- a/tools/lib/perf/include/internal/lib.h
+++ b/tools/lib/perf/include/internal/lib.h
@@ -9,4 +9,6 @@ extern unsigned int page_size;
ssize_t readn(int fd, void *buf, size_t n);
ssize_t writen(int fd, const void *buf, size_t n);
+ssize_t preadn(int fd, void *buf, size_t n, off_t offs);
+
#endif /* __LIBPERF_INTERNAL_CPUMAP_H */
diff --git a/tools/lib/perf/lib.c b/tools/lib/perf/lib.c
index 18658931fc71..ecc8035a3ae3 100644
--- a/tools/lib/perf/lib.c
+++ b/tools/lib/perf/lib.c
@@ -38,6 +38,26 @@ ssize_t readn(int fd, void *buf, size_t n)
return ion(true, fd, buf, n);
}
+ssize_t preadn(int fd, void *buf, size_t n, off_t offs)
+{
+ ssize_t ret;
+ off_t cur;
+
+ cur = lseek(fd, 0, SEEK_CUR);
+ if (cur < 0)
+ return -1;
+
+ if (lseek(fd, offs, SEEK_SET) < 0)
+ return -1;
+
+ ret = readn(fd, buf, n);
+
+ if (lseek(fd, cur, SEEK_CUR) < 0)
+ return -1;
+
+ return ret;
+}
+
/*
* Write exactly 'n' bytes or return an error.
*/
--
2.25.1
Powered by blists - more mailing lists