From mboxrd@z Thu Jan 1 00:00:00 1970 From: Arnaldo Carvalho de Melo Subject: [PATCH 16/21] perf tools: Add gzip_is_compressed function Date: Mon, 20 Aug 2018 13:15:57 -0300 Message-ID: <20180820161602.6830-17-acme@kernel.org> References: <20180820161602.6830-1-acme@kernel.org> Return-path: In-Reply-To: <20180820161602.6830-1-acme@kernel.org> Sender: linux-kernel-owner@vger.kernel.org To: Ingo Molnar Cc: Clark Williams , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Jiri Olsa , Alexander Shishkin , David Ahern , Michael Petlan , Namhyung Kim , Peter Zijlstra , Arnaldo Carvalho de Melo List-Id: linux-perf-users.vger.kernel.org From: Jiri Olsa Add implementation of the is_compressed callback for gzip. Signed-off-by: Jiri Olsa Cc: Alexander Shishkin Cc: David Ahern Cc: Michael Petlan Cc: Namhyung Kim Cc: Peter Zijlstra Link: http://lkml.kernel.org/r/20180817094813.15086-13-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/zlib.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/zlib.c b/tools/perf/util/zlib.c index e68317214679..902ce6384f57 100644 --- a/tools/perf/util/zlib.c +++ b/tools/perf/util/zlib.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "util/compress.h" #include "util/util.h" @@ -81,7 +82,18 @@ int gzip_decompress_to_file(const char *input, int output_fd) return ret == Z_STREAM_END ? 0 : -1; } -bool gzip_is_compressed(const char *input __maybe_unused) +bool gzip_is_compressed(const char *input) { - return true; + int fd = open(input, O_RDONLY); + const uint8_t magic[2] = { 0x1f, 0x8b }; + char buf[2] = { 0 }; + ssize_t rc; + + if (fd < 0) + return -1; + + rc = read(fd, buf, sizeof(buf)); + close(fd); + return rc == sizeof(buf) ? + memcmp(buf, magic, sizeof(buf)) == 0 : false; } -- 2.14.4