From: Jiri Olsa Date: Fri, 17 Aug 2018 09:48:12 +0000 (+0200) Subject: perf tools: Add gzip_is_compressed function X-Git-Tag: android-x86-8.1-r1~341^2^2~5 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=88c74dc76a30b9242c2df687deb44788d93fee6b;p=android-x86%2Fkernel.git perf tools: Add gzip_is_compressed function 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 --- 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 @@ out_close: 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; }