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

perf tools: Add LZMA decompression from FILE

Internally lzma_decompress_to_file() creates a FILE from the filename.
Add an API that takes an existing FILE directly. This allows
decompressing already-open files and even buffers opened by fmemopen().
It is necessary for supporting .gnu_debugdata in the next patch.

Signed-off-by: Stephen Brennan <stephen.s.brennan@oracle.com>
Reviewed-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Link: https://lore.kernel.org/r/20250307232206.2102440-3-stephen.s.brennan@oracle.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>

authored by

Stephen Brennan and committed by
Namhyung Kim
71fa411f 20ef7231

+26 -11
+8
tools/perf/util/compress.h
··· 4 4 5 5 #include <stdbool.h> 6 6 #include <stddef.h> 7 + #include <stdio.h> 7 8 #include <sys/types.h> 8 9 #include <linux/compiler.h> 9 10 #ifdef HAVE_ZSTD_SUPPORT ··· 17 16 #endif 18 17 19 18 #ifdef HAVE_LZMA_SUPPORT 19 + int lzma_decompress_stream_to_file(FILE *input, int output_fd); 20 20 int lzma_decompress_to_file(const char *input, int output_fd); 21 21 bool lzma_is_compressed(const char *input); 22 22 #else 23 + static inline 24 + int lzma_decompress_stream_to_file(FILE *input __maybe_unused, 25 + int output_fd __maybe_unused) 26 + { 27 + return -1; 28 + } 23 29 static inline 24 30 int lzma_decompress_to_file(const char *input __maybe_unused, 25 31 int output_fd __maybe_unused)
+18 -11
tools/perf/util/lzma.c
··· 32 32 } 33 33 } 34 34 35 - int lzma_decompress_to_file(const char *input, int output_fd) 35 + int lzma_decompress_stream_to_file(FILE *infile, int output_fd) 36 36 { 37 37 lzma_action action = LZMA_RUN; 38 38 lzma_stream strm = LZMA_STREAM_INIT; ··· 41 41 42 42 u8 buf_in[BUFSIZE]; 43 43 u8 buf_out[BUFSIZE]; 44 - FILE *infile; 45 - 46 - infile = fopen(input, "rb"); 47 - if (!infile) { 48 - pr_debug("lzma: fopen failed on %s: '%s'\n", input, strerror(errno)); 49 - return -1; 50 - } 51 44 52 45 ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED); 53 46 if (ret != LZMA_OK) { 54 47 pr_debug("lzma: lzma_stream_decoder failed %s (%d)\n", lzma_strerror(ret), ret); 55 - goto err_fclose; 48 + return err; 56 49 } 57 50 58 51 strm.next_in = NULL; ··· 93 100 err = 0; 94 101 err_lzma_end: 95 102 lzma_end(&strm); 96 - err_fclose: 97 - fclose(infile); 98 103 return err; 104 + } 105 + 106 + int lzma_decompress_to_file(const char *input, int output_fd) 107 + { 108 + FILE *infile; 109 + int ret; 110 + 111 + infile = fopen(input, "rb"); 112 + if (!infile) { 113 + pr_debug("lzma: fopen failed on %s: '%s'\n", input, strerror(errno)); 114 + return -1; 115 + } 116 + 117 + ret = lzma_decompress_stream_to_file(infile, output_fd); 118 + fclose(infile); 119 + return ret; 99 120 } 100 121 101 122 bool lzma_is_compressed(const char *input)