Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Add gzip_is_compressed function

Add implementation of the is_compressed callback for gzip.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Michael Petlan <mpetlan@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/20180817094813.15086-13-jolsa@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Jiri Olsa and committed by
Arnaldo Carvalho de Melo
88c74dc7 4b57fd44

+14 -2
+14 -2
tools/perf/util/zlib.c
··· 6 6 #include <sys/mman.h> 7 7 #include <zlib.h> 8 8 #include <linux/compiler.h> 9 + #include <unistd.h> 9 10 10 11 #include "util/compress.h" 11 12 #include "util/util.h" ··· 82 81 return ret == Z_STREAM_END ? 0 : -1; 83 82 } 84 83 85 - bool gzip_is_compressed(const char *input __maybe_unused) 84 + bool gzip_is_compressed(const char *input) 86 85 { 87 - return true; 86 + int fd = open(input, O_RDONLY); 87 + const uint8_t magic[2] = { 0x1f, 0x8b }; 88 + char buf[2] = { 0 }; 89 + ssize_t rc; 90 + 91 + if (fd < 0) 92 + return -1; 93 + 94 + rc = read(fd, buf, sizeof(buf)); 95 + close(fd); 96 + return rc == sizeof(buf) ? 97 + memcmp(buf, magic, sizeof(buf)) == 0 : false; 88 98 }