Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 5 | * This source code is licensed under both the BSD-style license (found in the |
| 6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 7 | * in the COPYING file in the root directory of this source tree). |
Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 9 | */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 10 | |
Yann Collet | 8dafb1a | 2017-01-25 17:01:13 -0800 | [diff] [blame] | 11 | |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 12 | /* ************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 13 | * Compiler Options |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 14 | ***************************************/ |
Yann Collet | 94ca85d | 2016-08-14 01:19:12 +0200 | [diff] [blame] | 15 | #ifdef _MSC_VER /* Visual */ |
Przemyslaw Skibinski | e679741 | 2016-12-21 13:47:11 +0100 | [diff] [blame] | 16 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
Yann Collet | 94ca85d | 2016-08-14 01:19:12 +0200 | [diff] [blame] | 17 | # pragma warning(disable : 4204) /* non-constant aggregate initializer */ |
| 18 | #endif |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 19 | #if defined(__MINGW32__) && !defined(_POSIX_SOURCE) |
| 20 | # define _POSIX_SOURCE 1 /* disable %llu warnings with MinGW on Windows */ |
| 21 | #endif |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 22 | |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 23 | /*-************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 24 | * Includes |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 25 | ***************************************/ |
Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 26 | #include "platform.h" /* Large Files support, SET_BINARY_MODE */ |
shakeelrao | dca73db | 2019-03-28 17:50:34 -0700 | [diff] [blame] | 27 | #include "util.h" /* UTIL_getFileSize, UTIL_isRegularFile, UTIL_isSameFile */ |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 28 | #include <stdio.h> /* fprintf, open, fdopen, fread, _fileno, stdin, stdout */ |
Yann Collet | 9f43292 | 2015-11-09 17:42:17 +0100 | [diff] [blame] | 29 | #include <stdlib.h> /* malloc, free */ |
| 30 | #include <string.h> /* strcmp, strlen */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 31 | #include <time.h> /* clock_t, to measure process time */ |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 32 | #include <fcntl.h> /* O_WRONLY */ |
Yann Collet | 3ca6261 | 2018-10-02 15:59:11 -0700 | [diff] [blame] | 33 | #include <assert.h> |
Yann Collet | 9f43292 | 2015-11-09 17:42:17 +0100 | [diff] [blame] | 34 | #include <errno.h> /* errno */ |
Nick Magerko | 2d39b43 | 2019-08-19 16:49:25 -0700 | [diff] [blame] | 35 | #include <limits.h> /* INT_MAX */ |
Casey McGinty | d4337b6 | 2018-09-11 11:39:49 -0700 | [diff] [blame] | 36 | #include <signal.h> |
Yann Collet | 59a7116 | 2019-04-10 12:37:03 -0700 | [diff] [blame] | 37 | #include "timefn.h" /* UTIL_getTime, UTIL_clockSpanMicro */ |
inikep | d5ff2c3 | 2016-04-28 14:40:45 +0200 | [diff] [blame] | 38 | |
Sean Purcell | 279be20 | 2017-04-06 12:56:40 -0700 | [diff] [blame] | 39 | #if defined (_MSC_VER) |
| 40 | # include <sys/stat.h> |
| 41 | # include <io.h> |
| 42 | #endif |
| 43 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 44 | #include "fileio.h" |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 45 | #include "fileio_asyncio.h" |
| 46 | #include "fileio_common.h" |
| 47 | |
| 48 | FIO_display_prefs_t g_display_prefs = {2, FIO_ps_auto}; |
| 49 | UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; |
Yann Collet | 2bfc79a | 2018-02-01 16:13:04 -0800 | [diff] [blame] | 50 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 51 | #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_magicNumber, ZSTD_frameHeaderSize_max */ |
W. Felix Handte | 7dcca6b | 2020-05-01 16:20:40 -0400 | [diff] [blame] | 52 | #include "../lib/zstd.h" |
Nick Terrell | 09149be | 2021-04-30 15:02:12 -0700 | [diff] [blame] | 53 | #include "../lib/zstd_errors.h" /* ZSTD_error_frameParameter_windowTooLarge */ |
Yann Collet | 2bfc79a | 2018-02-01 16:13:04 -0800 | [diff] [blame] | 54 | |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 55 | #if defined(ZSTD_GZCOMPRESS) || defined(ZSTD_GZDECOMPRESS) |
Nick Terrell | 2cb8ee8 | 2017-02-06 11:32:13 -0800 | [diff] [blame] | 56 | # include <zlib.h> |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 57 | # if !defined(z_const) |
| 58 | # define z_const |
| 59 | # endif |
Przemyslaw Skibinski | abfb51f | 2016-11-30 15:05:54 +0100 | [diff] [blame] | 60 | #endif |
Yann Collet | 2bfc79a | 2018-02-01 16:13:04 -0800 | [diff] [blame] | 61 | |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 62 | #if defined(ZSTD_LZMACOMPRESS) || defined(ZSTD_LZMADECOMPRESS) |
| 63 | # include <lzma.h> |
| 64 | #endif |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 65 | |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 66 | #define LZ4_MAGICNUMBER 0x184D2204 |
| 67 | #if defined(ZSTD_LZ4COMPRESS) || defined(ZSTD_LZ4DECOMPRESS) |
W. Felix Handte | 3602387 | 2017-09-21 11:29:35 -0700 | [diff] [blame] | 68 | # define LZ4F_ENABLE_OBSOLETE_ENUMS |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 69 | # include <lz4frame.h> |
Sean Purcell | 2c4b6fe | 2017-04-25 11:00:54 -0700 | [diff] [blame] | 70 | # include <lz4.h> |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 71 | #endif |
| 72 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 73 | char const* FIO_zlibVersion(void) |
| 74 | { |
| 75 | #if defined(ZSTD_GZCOMPRESS) || defined(ZSTD_GZDECOMPRESS) |
| 76 | return zlibVersion(); |
| 77 | #else |
| 78 | return "Unsupported"; |
| 79 | #endif |
| 80 | } |
| 81 | |
| 82 | char const* FIO_lz4Version(void) |
| 83 | { |
| 84 | #if defined(ZSTD_LZ4COMPRESS) || defined(ZSTD_LZ4DECOMPRESS) |
| 85 | /* LZ4_versionString() added in v1.7.3 */ |
| 86 | # if LZ4_VERSION_NUMBER >= 10703 |
| 87 | return LZ4_versionString(); |
| 88 | # else |
| 89 | # define ZSTD_LZ4_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE |
| 90 | # define ZSTD_LZ4_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LZ4_VERSION) |
| 91 | return ZSTD_LZ4_VERSION_STRING; |
| 92 | # endif |
| 93 | #else |
| 94 | return "Unsupported"; |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | char const* FIO_lzmaVersion(void) |
| 99 | { |
| 100 | #if defined(ZSTD_LZMACOMPRESS) || defined(ZSTD_LZMADECOMPRESS) |
| 101 | return lzma_version_string(); |
| 102 | #else |
| 103 | return "Unsupported"; |
| 104 | #endif |
| 105 | } |
| 106 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 107 | |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 108 | /*-************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 109 | * Constants |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 110 | ***************************************/ |
Yann Collet | 0853f86 | 2018-08-13 13:10:42 -0700 | [diff] [blame] | 111 | #define ADAPT_WINDOWLOG_DEFAULT 23 /* 8 MB */ |
Yann Collet | 0e30059 | 2017-04-11 14:41:02 -0700 | [diff] [blame] | 112 | #define DICTSIZE_MAX (32 MB) /* protection against large input (attack scenario) */ |
Yann Collet | 6f3acba | 2016-02-12 20:19:48 +0100 | [diff] [blame] | 113 | |
inikep | 3c7c352 | 2016-04-22 13:59:05 +0200 | [diff] [blame] | 114 | #define FNSPACE 30 |
| 115 | |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 116 | /* Default file permissions 0666 (modulated by umask) */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 117 | /* Temporary restricted file permissions are used when we're going to |
| 118 | * chmod/chown at the end of the operation. */ |
W. Felix Handte | da61918 | 2021-04-06 11:29:28 -0400 | [diff] [blame] | 119 | #if !defined(_WIN32) |
| 120 | /* These macros aren't defined on windows. */ |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 121 | #define DEFAULT_FILE_PERMISSIONS (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 122 | #define TEMPORARY_FILE_PERMISSIONS (S_IRUSR|S_IWUSR) |
W. Felix Handte | 45c4918 | 2021-03-09 01:24:11 -0500 | [diff] [blame] | 123 | #else |
W. Felix Handte | da61918 | 2021-04-06 11:29:28 -0400 | [diff] [blame] | 124 | #define DEFAULT_FILE_PERMISSIONS (0666) |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 125 | #define TEMPORARY_FILE_PERMISSIONS (0600) |
W. Felix Handte | 45c4918 | 2021-03-09 01:24:11 -0500 | [diff] [blame] | 126 | #endif |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 127 | |
Yann Collet | 00fc1ba | 2017-10-01 12:10:26 -0700 | [diff] [blame] | 128 | /*-************************************ |
| 129 | * Signal (Ctrl-C trapping) |
| 130 | **************************************/ |
Yann Collet | 7f580f9 | 2017-10-02 11:39:05 -0700 | [diff] [blame] | 131 | static const char* g_artefact = NULL; |
| 132 | static void INThandler(int sig) |
Yann Collet | 00fc1ba | 2017-10-01 12:10:26 -0700 | [diff] [blame] | 133 | { |
Yann Collet | 6e7ba3d | 2017-10-02 00:19:47 -0700 | [diff] [blame] | 134 | assert(sig==SIGINT); (void)sig; |
Yann Collet | fe5444b | 2017-10-02 02:02:16 -0700 | [diff] [blame] | 135 | #if !defined(_MSC_VER) |
| 136 | signal(sig, SIG_IGN); /* this invocation generates a buggy warning in Visual Studio */ |
Yann Collet | 82bc200 | 2017-10-02 00:02:24 -0700 | [diff] [blame] | 137 | #endif |
Nick Terrell | 282ad05 | 2018-01-05 11:44:45 -0800 | [diff] [blame] | 138 | if (g_artefact) { |
| 139 | assert(UTIL_isRegularFile(g_artefact)); |
| 140 | remove(g_artefact); |
| 141 | } |
Yann Collet | 00fc1ba | 2017-10-01 12:10:26 -0700 | [diff] [blame] | 142 | DISPLAY("\n"); |
Yann Collet | bd18095 | 2017-10-01 15:32:48 -0700 | [diff] [blame] | 143 | exit(2); |
Yann Collet | 00fc1ba | 2017-10-01 12:10:26 -0700 | [diff] [blame] | 144 | } |
Nick Terrell | a6052af | 2017-11-17 16:38:56 -0800 | [diff] [blame] | 145 | static void addHandler(char const* dstFileName) |
| 146 | { |
| 147 | if (UTIL_isRegularFile(dstFileName)) { |
| 148 | g_artefact = dstFileName; |
| 149 | signal(SIGINT, INThandler); |
| 150 | } else { |
| 151 | g_artefact = NULL; |
| 152 | } |
| 153 | } |
| 154 | /* Idempotent */ |
| 155 | static void clearHandler(void) |
| 156 | { |
| 157 | if (g_artefact) signal(SIGINT, SIG_DFL); |
| 158 | g_artefact = NULL; |
| 159 | } |
Yann Collet | 00fc1ba | 2017-10-01 12:10:26 -0700 | [diff] [blame] | 160 | |
| 161 | |
Casey McGinty | d4337b6 | 2018-09-11 11:39:49 -0700 | [diff] [blame] | 162 | /*-********************************************************* |
| 163 | * Termination signal trapping (Print debug stack trace) |
| 164 | ***********************************************************/ |
Julian Fessard | 0ea286f | 2018-10-09 17:24:48 -0700 | [diff] [blame] | 165 | #if defined(__has_feature) && !defined(BACKTRACE_ENABLE) /* Clang compiler */ |
| 166 | # if (__has_feature(address_sanitizer)) |
| 167 | # define BACKTRACE_ENABLE 0 |
| 168 | # endif /* __has_feature(address_sanitizer) */ |
| 169 | #elif defined(__SANITIZE_ADDRESS__) && !defined(BACKTRACE_ENABLE) /* GCC compiler */ |
| 170 | # define BACKTRACE_ENABLE 0 |
| 171 | #endif |
| 172 | |
Yann Collet | b304b67 | 2018-10-09 17:56:59 -0700 | [diff] [blame] | 173 | #if !defined(BACKTRACE_ENABLE) |
| 174 | /* automatic detector : backtrace enabled by default on linux+glibc and osx */ |
Rosen Penev | 23b5ee1 | 2019-05-06 15:02:47 -0700 | [diff] [blame] | 175 | # if (defined(__linux__) && (defined(__GLIBC__) && !defined(__UCLIBC__))) \ |
Yann Collet | b304b67 | 2018-10-09 17:56:59 -0700 | [diff] [blame] | 176 | || (defined(__APPLE__) && defined(__MACH__)) |
| 177 | # define BACKTRACE_ENABLE 1 |
| 178 | # else |
| 179 | # define BACKTRACE_ENABLE 0 |
| 180 | # endif |
Yann Collet | e0ab6b6 | 2018-10-09 17:12:21 -0700 | [diff] [blame] | 181 | #endif |
| 182 | |
Yann Collet | b304b67 | 2018-10-09 17:56:59 -0700 | [diff] [blame] | 183 | /* note : after this point, BACKTRACE_ENABLE is necessarily defined */ |
Yann Collet | e0ab6b6 | 2018-10-09 17:12:21 -0700 | [diff] [blame] | 184 | |
Yann Collet | b304b67 | 2018-10-09 17:56:59 -0700 | [diff] [blame] | 185 | |
| 186 | #if BACKTRACE_ENABLE |
Yann Collet | 54001f3 | 2018-09-21 14:46:09 -0700 | [diff] [blame] | 187 | |
Yann Collet | e0ab6b6 | 2018-10-09 17:12:21 -0700 | [diff] [blame] | 188 | #include <execinfo.h> /* backtrace, backtrace_symbols */ |
| 189 | |
Casey McGinty | d4337b6 | 2018-09-11 11:39:49 -0700 | [diff] [blame] | 190 | #define MAX_STACK_FRAMES 50 |
| 191 | |
Casey McGinty | d4337b6 | 2018-09-11 11:39:49 -0700 | [diff] [blame] | 192 | static void ABRThandler(int sig) { |
Casey McGinty | b9118ec | 2018-09-11 14:49:47 -0700 | [diff] [blame] | 193 | const char* name; |
| 194 | void* addrlist[MAX_STACK_FRAMES]; |
| 195 | char** symbollist; |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 196 | int addrlen, i; |
Casey McGinty | d4337b6 | 2018-09-11 11:39:49 -0700 | [diff] [blame] | 197 | |
Casey McGinty | b9118ec | 2018-09-11 14:49:47 -0700 | [diff] [blame] | 198 | switch (sig) { |
| 199 | case SIGABRT: name = "SIGABRT"; break; |
| 200 | case SIGFPE: name = "SIGFPE"; break; |
| 201 | case SIGILL: name = "SIGILL"; break; |
| 202 | case SIGINT: name = "SIGINT"; break; |
| 203 | case SIGSEGV: name = "SIGSEGV"; break; |
| 204 | default: name = "UNKNOWN"; |
| 205 | } |
Casey McGinty | d4337b6 | 2018-09-11 11:39:49 -0700 | [diff] [blame] | 206 | |
Casey McGinty | b9118ec | 2018-09-11 14:49:47 -0700 | [diff] [blame] | 207 | DISPLAY("Caught %s signal, printing stack:\n", name); |
| 208 | /* Retrieve current stack addresses. */ |
| 209 | addrlen = backtrace(addrlist, MAX_STACK_FRAMES); |
| 210 | if (addrlen == 0) { |
| 211 | DISPLAY("\n"); |
| 212 | return; |
| 213 | } |
| 214 | /* Create readable strings to each frame. */ |
| 215 | symbollist = backtrace_symbols(addrlist, addrlen); |
| 216 | /* Print the stack trace, excluding calls handling the signal. */ |
| 217 | for (i = ZSTD_START_SYMBOLLIST_FRAME; i < addrlen; i++) { |
| 218 | DISPLAY("%s\n", symbollist[i]); |
| 219 | } |
| 220 | free(symbollist); |
| 221 | /* Reset and raise the signal so default handler runs. */ |
| 222 | signal(sig, SIG_DFL); |
| 223 | raise(sig); |
Casey McGinty | d4337b6 | 2018-09-11 11:39:49 -0700 | [diff] [blame] | 224 | } |
| 225 | #endif |
| 226 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 227 | void FIO_addAbortHandler(void) |
Casey McGinty | d4337b6 | 2018-09-11 11:39:49 -0700 | [diff] [blame] | 228 | { |
Yann Collet | b304b67 | 2018-10-09 17:56:59 -0700 | [diff] [blame] | 229 | #if BACKTRACE_ENABLE |
Casey McGinty | d4337b6 | 2018-09-11 11:39:49 -0700 | [diff] [blame] | 230 | signal(SIGABRT, ABRThandler); |
| 231 | signal(SIGFPE, ABRThandler); |
| 232 | signal(SIGILL, ABRThandler); |
| 233 | signal(SIGSEGV, ABRThandler); |
| 234 | signal(SIGBUS, ABRThandler); |
| 235 | #endif |
| 236 | } |
| 237 | |
senhuang42 | d54566f | 2020-08-28 11:01:04 -0400 | [diff] [blame] | 238 | /*-************************************* |
senhuang42 | 3a7d625 | 2020-09-01 12:52:18 -0400 | [diff] [blame] | 239 | * Parameters: FIO_ctx_t |
senhuang42 | d54566f | 2020-08-28 11:01:04 -0400 | [diff] [blame] | 240 | ***************************************/ |
| 241 | |
| 242 | /* typedef'd to FIO_ctx_t within fileio.h */ |
| 243 | struct FIO_ctx_s { |
| 244 | |
senhuang42 | a480b02 | 2020-09-03 09:26:30 -0400 | [diff] [blame] | 245 | /* file i/o info */ |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 246 | int nbFilesTotal; |
senhuang42 | 432186c | 2020-09-24 15:55:30 -0400 | [diff] [blame] | 247 | int hasStdinInput; |
senhuang42 | 1ebe360 | 2020-10-07 13:42:34 -0400 | [diff] [blame] | 248 | int hasStdoutOutput; |
senhuang42 | a480b02 | 2020-09-03 09:26:30 -0400 | [diff] [blame] | 249 | |
| 250 | /* file i/o state */ |
senhuang42 | b6abbc3 | 2020-08-26 11:35:07 -0400 | [diff] [blame] | 251 | int currFileIdx; |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 252 | int nbFilesProcessed; |
senhuang42 | d54566f | 2020-08-28 11:01:04 -0400 | [diff] [blame] | 253 | size_t totalBytesInput; |
| 254 | size_t totalBytesOutput; |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 255 | }; |
| 256 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 257 | static int FIO_shouldDisplayFileSummary(FIO_ctx_t const* fCtx) |
| 258 | { |
| 259 | return fCtx->nbFilesTotal <= 1 || g_display_prefs.displayLevel >= 3; |
| 260 | } |
| 261 | |
| 262 | static int FIO_shouldDisplayMultipleFileSummary(FIO_ctx_t const* fCtx) |
| 263 | { |
| 264 | int const shouldDisplay = (fCtx->nbFilesProcessed >= 1 && fCtx->nbFilesTotal > 1); |
| 265 | assert(shouldDisplay || FIO_shouldDisplayFileSummary(fCtx) || fCtx->nbFilesProcessed == 0); |
| 266 | return shouldDisplay; |
| 267 | } |
| 268 | |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 269 | |
| 270 | /*-************************************* |
| 271 | * Parameters: Initialization |
| 272 | ***************************************/ |
| 273 | |
| 274 | #define FIO_OVERLAP_LOG_NOTSET 9999 |
| 275 | #define FIO_LDM_PARAM_NOTSET 9999 |
| 276 | |
| 277 | |
| 278 | FIO_prefs_t* FIO_createPreferences(void) |
| 279 | { |
| 280 | FIO_prefs_t* const ret = (FIO_prefs_t*)malloc(sizeof(FIO_prefs_t)); |
| 281 | if (!ret) EXM_THROW(21, "Allocation error : not enough memory"); |
| 282 | |
| 283 | ret->compressionType = FIO_zstdCompression; |
| 284 | ret->overwrite = 0; |
| 285 | ret->sparseFileSupport = ZSTD_SPARSE_DEFAULT; |
| 286 | ret->dictIDFlag = 1; |
| 287 | ret->checksumFlag = 1; |
| 288 | ret->removeSrcFile = 0; |
| 289 | ret->memLimit = 0; |
| 290 | ret->nbWorkers = 1; |
| 291 | ret->blockSize = 0; |
| 292 | ret->overlapLog = FIO_OVERLAP_LOG_NOTSET; |
| 293 | ret->adaptiveMode = 0; |
| 294 | ret->rsyncable = 0; |
| 295 | ret->minAdaptLevel = -50; /* initializing this value requires a constant, so ZSTD_minCLevel() doesn't work */ |
| 296 | ret->maxAdaptLevel = 22; /* initializing this value requires a constant, so ZSTD_maxCLevel() doesn't work */ |
| 297 | ret->ldmFlag = 0; |
| 298 | ret->ldmHashLog = 0; |
| 299 | ret->ldmMinMatch = 0; |
| 300 | ret->ldmBucketSizeLog = FIO_LDM_PARAM_NOTSET; |
| 301 | ret->ldmHashRateLog = FIO_LDM_PARAM_NOTSET; |
Nick Magerko | af0c950 | 2019-08-15 23:57:55 -0700 | [diff] [blame] | 302 | ret->streamSrcSize = 0; |
Ephraim Park | 9007701 | 2019-06-24 13:40:52 -0700 | [diff] [blame] | 303 | ret->targetCBlockSize = 0; |
Nick Magerko | dffbac5 | 2019-08-19 08:52:08 -0700 | [diff] [blame] | 304 | ret->srcSizeHint = 0; |
Yann Collet | 0ee3609 | 2019-10-17 16:09:53 -0700 | [diff] [blame] | 305 | ret->testMode = 0; |
senhuang42 | b5c35d7 | 2021-09-20 09:04:07 -0400 | [diff] [blame] | 306 | ret->literalCompressionMode = ZSTD_ps_auto; |
Shashank Tavildar | 0f2bff2 | 2019-10-28 18:21:47 -0700 | [diff] [blame] | 307 | ret->excludeCompressedFiles = 0; |
W. Felix Handte | 33f3e29 | 2021-05-04 16:24:46 -0400 | [diff] [blame] | 308 | ret->allowBlockDevices = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 309 | ret->asyncIO = AIO_supported(); |
| 310 | ret->passThrough = -1; |
senhuang42 | d54566f | 2020-08-28 11:01:04 -0400 | [diff] [blame] | 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | FIO_ctx_t* FIO_createContext(void) |
| 315 | { |
| 316 | FIO_ctx_t* const ret = (FIO_ctx_t*)malloc(sizeof(FIO_ctx_t)); |
| 317 | if (!ret) EXM_THROW(21, "Allocation error : not enough memory"); |
| 318 | |
senhuang42 | b6abbc3 | 2020-08-26 11:35:07 -0400 | [diff] [blame] | 319 | ret->currFileIdx = 0; |
senhuang42 | 432186c | 2020-09-24 15:55:30 -0400 | [diff] [blame] | 320 | ret->hasStdinInput = 0; |
senhuang42 | 1ebe360 | 2020-10-07 13:42:34 -0400 | [diff] [blame] | 321 | ret->hasStdoutOutput = 0; |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 322 | ret->nbFilesTotal = 1; |
| 323 | ret->nbFilesProcessed = 0; |
senhuang42 | d54566f | 2020-08-28 11:01:04 -0400 | [diff] [blame] | 324 | ret->totalBytesInput = 0; |
| 325 | ret->totalBytesOutput = 0; |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | void FIO_freePreferences(FIO_prefs_t* const prefs) |
| 330 | { |
| 331 | free(prefs); |
| 332 | } |
| 333 | |
senhuang42 | d54566f | 2020-08-28 11:01:04 -0400 | [diff] [blame] | 334 | void FIO_freeContext(FIO_ctx_t* const fCtx) |
| 335 | { |
| 336 | free(fCtx); |
| 337 | } |
| 338 | |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 339 | |
| 340 | /*-************************************* |
| 341 | * Parameters: Display Options |
| 342 | ***************************************/ |
| 343 | |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 344 | void FIO_setNotificationLevel(int level) { g_display_prefs.displayLevel=level; } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 345 | |
sen | 6030cdf | 2021-05-06 14:50:28 -0400 | [diff] [blame] | 346 | void FIO_setProgressSetting(FIO_progressSetting_e setting) { g_display_prefs.progressSetting = setting; } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 347 | |
| 348 | |
| 349 | /*-************************************* |
| 350 | * Parameters: Setters |
| 351 | ***************************************/ |
| 352 | |
senhuang42 | d54566f | 2020-08-28 11:01:04 -0400 | [diff] [blame] | 353 | /* FIO_prefs_t functions */ |
| 354 | |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 355 | void FIO_setCompressionType(FIO_prefs_t* const prefs, FIO_compressionType_t compressionType) { prefs->compressionType = compressionType; } |
| 356 | |
| 357 | void FIO_overwriteMode(FIO_prefs_t* const prefs) { prefs->overwrite = 1; } |
| 358 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 359 | void FIO_setSparseWrite(FIO_prefs_t* const prefs, int sparse) { prefs->sparseFileSupport = sparse; } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 360 | |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 361 | void FIO_setDictIDFlag(FIO_prefs_t* const prefs, int dictIDFlag) { prefs->dictIDFlag = dictIDFlag; } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 362 | |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 363 | void FIO_setChecksumFlag(FIO_prefs_t* const prefs, int checksumFlag) { prefs->checksumFlag = checksumFlag; } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 364 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 365 | void FIO_setRemoveSrcFile(FIO_prefs_t* const prefs, int flag) { prefs->removeSrcFile = (flag!=0); } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 366 | |
| 367 | void FIO_setMemLimit(FIO_prefs_t* const prefs, unsigned memLimit) { prefs->memLimit = memLimit; } |
| 368 | |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 369 | void FIO_setNbWorkers(FIO_prefs_t* const prefs, int nbWorkers) { |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 370 | #ifndef ZSTD_MULTITHREAD |
Yann Collet | 209df52 | 2018-02-01 19:29:30 -0800 | [diff] [blame] | 371 | if (nbWorkers > 0) DISPLAYLEVEL(2, "Note : multi-threading is disabled \n"); |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 372 | #endif |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 373 | prefs->nbWorkers = nbWorkers; |
Yann Collet | 500014a | 2017-01-19 16:59:56 -0800 | [diff] [blame] | 374 | } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 375 | |
Shashank Tavildar | 0f2bff2 | 2019-10-28 18:21:47 -0700 | [diff] [blame] | 376 | void FIO_setExcludeCompressedFile(FIO_prefs_t* const prefs, int excludeCompressedFiles) { prefs->excludeCompressedFiles = excludeCompressedFiles; } |
| 377 | |
W. Felix Handte | 33f3e29 | 2021-05-04 16:24:46 -0400 | [diff] [blame] | 378 | void FIO_setAllowBlockDevices(FIO_prefs_t* const prefs, int allowBlockDevices) { prefs->allowBlockDevices = allowBlockDevices; } |
| 379 | |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 380 | void FIO_setBlockSize(FIO_prefs_t* const prefs, int blockSize) { |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 381 | if (blockSize && prefs->nbWorkers==0) |
Yann Collet | 512cbe8 | 2017-01-24 17:02:26 -0800 | [diff] [blame] | 382 | DISPLAYLEVEL(2, "Setting block size is useless in single-thread mode \n"); |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 383 | prefs->blockSize = blockSize; |
Yann Collet | 512cbe8 | 2017-01-24 17:02:26 -0800 | [diff] [blame] | 384 | } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 385 | |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 386 | void FIO_setOverlapLog(FIO_prefs_t* const prefs, int overlapLog){ |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 387 | if (overlapLog && prefs->nbWorkers==0) |
Yann Collet | 6be2337 | 2017-01-30 11:17:26 -0800 | [diff] [blame] | 388 | DISPLAYLEVEL(2, "Setting overlapLog is useless in single-thread mode \n"); |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 389 | prefs->overlapLog = overlapLog; |
Yann Collet | 6be2337 | 2017-01-30 11:17:26 -0800 | [diff] [blame] | 390 | } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 391 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 392 | void FIO_setAdaptiveMode(FIO_prefs_t* const prefs, int adapt) { |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 393 | if ((adapt>0) && (prefs->nbWorkers==0)) |
Yann Collet | 89bc309 | 2018-09-19 14:49:13 -0700 | [diff] [blame] | 394 | EXM_THROW(1, "Adaptive mode is not compatible with single thread mode \n"); |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 395 | prefs->adaptiveMode = adapt; |
Yann Collet | 89bc309 | 2018-09-19 14:49:13 -0700 | [diff] [blame] | 396 | } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 397 | |
Nick Terrell | 4694423 | 2020-11-02 17:52:29 -0800 | [diff] [blame] | 398 | void FIO_setUseRowMatchFinder(FIO_prefs_t* const prefs, int useRowMatchFinder) { |
| 399 | prefs->useRowMatchFinder = useRowMatchFinder; |
| 400 | } |
| 401 | |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 402 | void FIO_setRsyncable(FIO_prefs_t* const prefs, int rsyncable) { |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 403 | if ((rsyncable>0) && (prefs->nbWorkers==0)) |
Nick Terrell | f9a671a | 2018-11-12 19:59:42 -0800 | [diff] [blame] | 404 | EXM_THROW(1, "Rsyncable mode is not compatible with single thread mode \n"); |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 405 | prefs->rsyncable = rsyncable; |
Nick Terrell | f9a671a | 2018-11-12 19:59:42 -0800 | [diff] [blame] | 406 | } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 407 | |
Nick Magerko | af0c950 | 2019-08-15 23:57:55 -0700 | [diff] [blame] | 408 | void FIO_setStreamSrcSize(FIO_prefs_t* const prefs, size_t streamSrcSize) { |
| 409 | prefs->streamSrcSize = streamSrcSize; |
| 410 | } |
| 411 | |
Ephraim Park | 9007701 | 2019-06-24 13:40:52 -0700 | [diff] [blame] | 412 | void FIO_setTargetCBlockSize(FIO_prefs_t* const prefs, size_t targetCBlockSize) { |
| 413 | prefs->targetCBlockSize = targetCBlockSize; |
| 414 | } |
| 415 | |
Nick Magerko | dffbac5 | 2019-08-19 08:52:08 -0700 | [diff] [blame] | 416 | void FIO_setSrcSizeHint(FIO_prefs_t* const prefs, size_t srcSizeHint) { |
Nick Magerko | 2d39b43 | 2019-08-19 16:49:25 -0700 | [diff] [blame] | 417 | prefs->srcSizeHint = (int)MIN((size_t)INT_MAX, srcSizeHint); |
Nick Magerko | dffbac5 | 2019-08-19 08:52:08 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Yann Collet | 0ee3609 | 2019-10-17 16:09:53 -0700 | [diff] [blame] | 420 | void FIO_setTestMode(FIO_prefs_t* const prefs, int testMode) { |
| 421 | prefs->testMode = (testMode!=0); |
| 422 | } |
| 423 | |
Nick Terrell | 0c53c5a | 2019-02-15 14:15:36 -0800 | [diff] [blame] | 424 | void FIO_setLiteralCompressionMode( |
| 425 | FIO_prefs_t* const prefs, |
senhuang42 | b5c35d7 | 2021-09-20 09:04:07 -0400 | [diff] [blame] | 426 | ZSTD_paramSwitch_e mode) { |
Nick Terrell | 0c53c5a | 2019-02-15 14:15:36 -0800 | [diff] [blame] | 427 | prefs->literalCompressionMode = mode; |
| 428 | } |
| 429 | |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 430 | void FIO_setAdaptMin(FIO_prefs_t* const prefs, int minCLevel) |
Yann Collet | 6c51bf4 | 2018-09-24 18:16:08 -0700 | [diff] [blame] | 431 | { |
| 432 | #ifndef ZSTD_NOCOMPRESS |
| 433 | assert(minCLevel >= ZSTD_minCLevel()); |
| 434 | #endif |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 435 | prefs->minAdaptLevel = minCLevel; |
Yann Collet | 6c51bf4 | 2018-09-24 18:16:08 -0700 | [diff] [blame] | 436 | } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 437 | |
| 438 | void FIO_setAdaptMax(FIO_prefs_t* const prefs, int maxCLevel) |
Yann Collet | 6c51bf4 | 2018-09-24 18:16:08 -0700 | [diff] [blame] | 439 | { |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 440 | prefs->maxAdaptLevel = maxCLevel; |
Yann Collet | 6c51bf4 | 2018-09-24 18:16:08 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 443 | void FIO_setLdmFlag(FIO_prefs_t* const prefs, unsigned ldmFlag) { |
| 444 | prefs->ldmFlag = (ldmFlag>0); |
Stella Lau | a1f04d5 | 2017-09-01 14:52:51 -0700 | [diff] [blame] | 445 | } |
Stella Lau | 67d4a61 | 2017-09-02 21:10:36 -0700 | [diff] [blame] | 446 | |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 447 | void FIO_setLdmHashLog(FIO_prefs_t* const prefs, int ldmHashLog) { |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 448 | prefs->ldmHashLog = ldmHashLog; |
Stella Lau | 67d4a61 | 2017-09-02 21:10:36 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 451 | void FIO_setLdmMinMatch(FIO_prefs_t* const prefs, int ldmMinMatch) { |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 452 | prefs->ldmMinMatch = ldmMinMatch; |
Nick Terrell | bdfcaec | 2018-12-13 17:17:32 -0800 | [diff] [blame] | 453 | } |
Stella Lau | a1f04d5 | 2017-09-01 14:52:51 -0700 | [diff] [blame] | 454 | |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 455 | void FIO_setLdmBucketSizeLog(FIO_prefs_t* const prefs, int ldmBucketSizeLog) { |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 456 | prefs->ldmBucketSizeLog = ldmBucketSizeLog; |
| 457 | } |
| 458 | |
| 459 | |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 460 | void FIO_setLdmHashRateLog(FIO_prefs_t* const prefs, int ldmHashRateLog) { |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 461 | prefs->ldmHashRateLog = ldmHashRateLog; |
| 462 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 463 | |
Bimba Shrestha | f25a6e9 | 2020-01-10 14:25:24 -0800 | [diff] [blame] | 464 | void FIO_setPatchFromMode(FIO_prefs_t* const prefs, int value) |
| 465 | { |
| 466 | prefs->patchFromMode = value != 0; |
| 467 | } |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 468 | |
Bimba Shrestha | 6d8e761 | 2020-03-09 14:12:52 -0500 | [diff] [blame] | 469 | void FIO_setContentSize(FIO_prefs_t* const prefs, int value) |
Bimba Shrestha | 167244a | 2020-03-09 13:07:29 -0500 | [diff] [blame] | 470 | { |
Bimba Shrestha | 6d8e761 | 2020-03-09 14:12:52 -0500 | [diff] [blame] | 471 | prefs->contentSize = value != 0; |
Bimba Shrestha | 167244a | 2020-03-09 13:07:29 -0500 | [diff] [blame] | 472 | } |
| 473 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 474 | void FIO_setAsyncIOFlag(FIO_prefs_t* const prefs, int value) { |
| 475 | #ifdef ZSTD_MULTITHREAD |
| 476 | prefs->asyncIO = value; |
| 477 | #else |
| 478 | (void) prefs; |
| 479 | (void) value; |
| 480 | DISPLAYLEVEL(2, "Note : asyncio is disabled (lack of multithreading support) \n"); |
| 481 | #endif |
| 482 | } |
| 483 | |
| 484 | void FIO_setPassThroughFlag(FIO_prefs_t* const prefs, int value) { |
| 485 | prefs->passThrough = (value != 0); |
| 486 | } |
| 487 | |
| 488 | void FIO_setMMapDict(FIO_prefs_t* const prefs, ZSTD_paramSwitch_e value) |
| 489 | { |
| 490 | prefs->mmapDict = value; |
| 491 | } |
| 492 | |
senhuang42 | d54566f | 2020-08-28 11:01:04 -0400 | [diff] [blame] | 493 | /* FIO_ctx_t functions */ |
senhuang42 | da38891 | 2020-08-25 16:46:47 -0400 | [diff] [blame] | 494 | |
senhuang42 | 1ebe360 | 2020-10-07 13:42:34 -0400 | [diff] [blame] | 495 | void FIO_setHasStdoutOutput(FIO_ctx_t* const fCtx, int value) { |
| 496 | fCtx->hasStdoutOutput = value; |
| 497 | } |
| 498 | |
senhuang42 | 3a7d625 | 2020-09-01 12:52:18 -0400 | [diff] [blame] | 499 | void FIO_setNbFilesTotal(FIO_ctx_t* const fCtx, int value) |
senhuang42 | b6abbc3 | 2020-08-26 11:35:07 -0400 | [diff] [blame] | 500 | { |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 501 | fCtx->nbFilesTotal = value; |
senhuang42 | b6abbc3 | 2020-08-26 11:35:07 -0400 | [diff] [blame] | 502 | } |
| 503 | |
senhuang42 | 432186c | 2020-09-24 15:55:30 -0400 | [diff] [blame] | 504 | void FIO_determineHasStdinInput(FIO_ctx_t* const fCtx, const FileNamesTable* const filenames) { |
senhuang42 | 9f7212a | 2020-09-24 16:44:33 -0400 | [diff] [blame] | 505 | size_t i = 0; |
senhuang42 | 432186c | 2020-09-24 15:55:30 -0400 | [diff] [blame] | 506 | for ( ; i < filenames->tableSize; ++i) { |
| 507 | if (!strcmp(stdinmark, filenames->fileNames[i])) { |
| 508 | fCtx->hasStdinInput = 1; |
| 509 | return; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 514 | /*-************************************* |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 515 | * Functions |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 516 | ***************************************/ |
W. Felix Handte | b02cdf6 | 2020-08-10 15:39:14 -0400 | [diff] [blame] | 517 | /** FIO_removeFile() : |
Sean Purcell | 279be20 | 2017-04-06 12:56:40 -0700 | [diff] [blame] | 518 | * @result : Unlink `fileName`, even if it's read-only */ |
W. Felix Handte | b02cdf6 | 2020-08-10 15:39:14 -0400 | [diff] [blame] | 519 | static int FIO_removeFile(const char* path) |
Sean Purcell | 279be20 | 2017-04-06 12:56:40 -0700 | [diff] [blame] | 520 | { |
W. Felix Handte | c144914 | 2020-08-05 12:10:42 -0400 | [diff] [blame] | 521 | stat_t statbuf; |
| 522 | if (!UTIL_stat(path, &statbuf)) { |
| 523 | DISPLAYLEVEL(2, "zstd: Failed to stat %s while trying to remove it\n", path); |
| 524 | return 0; |
| 525 | } |
| 526 | if (!UTIL_isRegularFileStat(&statbuf)) { |
| 527 | DISPLAYLEVEL(2, "zstd: Refusing to remove non-regular file %s\n", path); |
Nick Terrell | 82bc8fe | 2017-12-13 12:04:46 -0800 | [diff] [blame] | 528 | return 0; |
| 529 | } |
Sean Purcell | 279be20 | 2017-04-06 12:56:40 -0700 | [diff] [blame] | 530 | #if defined(_WIN32) || defined(WIN32) |
Yann Collet | 01a1abf | 2017-05-05 19:15:24 -0700 | [diff] [blame] | 531 | /* windows doesn't allow remove read-only files, |
| 532 | * so try to make it writable first */ |
W. Felix Handte | 953f0a0 | 2020-08-10 17:28:34 -0400 | [diff] [blame] | 533 | if (!(statbuf.st_mode & _S_IWRITE)) { |
W. Felix Handte | c144914 | 2020-08-05 12:10:42 -0400 | [diff] [blame] | 534 | UTIL_chmod(path, &statbuf, _S_IWRITE); |
| 535 | } |
Sean Purcell | 279be20 | 2017-04-06 12:56:40 -0700 | [diff] [blame] | 536 | #endif |
| 537 | return remove(path); |
| 538 | } |
| 539 | |
Yann Collet | cdff19c | 2016-11-11 17:26:54 -0800 | [diff] [blame] | 540 | /** FIO_openSrcFile() : |
W. Felix Handte | 33f3e29 | 2021-05-04 16:24:46 -0400 | [diff] [blame] | 541 | * condition : `srcFileName` must be non-NULL. `prefs` may be NULL. |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 542 | * @result : FILE* to `srcFileName`, or NULL if it fails */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 543 | static FILE* FIO_openSrcFile(const FIO_prefs_t* const prefs, const char* srcFileName, stat_t* statbuf) |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 544 | { |
W. Felix Handte | 33f3e29 | 2021-05-04 16:24:46 -0400 | [diff] [blame] | 545 | int allowBlockDevices = prefs != NULL ? prefs->allowBlockDevices : 0; |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 546 | assert(srcFileName != NULL); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 547 | assert(statbuf != NULL); |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 548 | if (!strcmp (srcFileName, stdinmark)) { |
Yann Collet | 105fa95 | 2018-12-20 09:16:40 -0800 | [diff] [blame] | 549 | DISPLAYLEVEL(4,"Using stdin for input \n"); |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 550 | SET_BINARY_MODE(stdin); |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 551 | return stdin; |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 554 | if (!UTIL_stat(srcFileName, statbuf)) { |
Yann Collet | 105fa95 | 2018-12-20 09:16:40 -0800 | [diff] [blame] | 555 | DISPLAYLEVEL(1, "zstd: can't stat %s : %s -- ignored \n", |
| 556 | srcFileName, strerror(errno)); |
Yann Collet | 173ef9d | 2018-12-19 18:30:57 -0800 | [diff] [blame] | 557 | return NULL; |
| 558 | } |
| 559 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 560 | if (!UTIL_isRegularFileStat(statbuf) |
| 561 | && !UTIL_isFIFOStat(statbuf) |
| 562 | && !(allowBlockDevices && UTIL_isBlockDevStat(statbuf)) |
Bimba Shrestha | 0b52d87 | 2019-10-25 14:06:50 -0700 | [diff] [blame] | 563 | ) { |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 564 | DISPLAYLEVEL(1, "zstd: %s is not a regular file -- ignored \n", |
| 565 | srcFileName); |
| 566 | return NULL; |
| 567 | } |
| 568 | |
| 569 | { FILE* const f = fopen(srcFileName, "rb"); |
| 570 | if (f == NULL) |
| 571 | DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno)); |
| 572 | return f; |
| 573 | } |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 574 | } |
| 575 | |
Yann Collet | 43eeea4 | 2016-09-15 15:38:44 +0200 | [diff] [blame] | 576 | /** FIO_openDstFile() : |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 577 | * condition : `dstFileName` must be non-NULL. |
Yann Collet | 43eeea4 | 2016-09-15 15:38:44 +0200 | [diff] [blame] | 578 | * @result : FILE* to `dstFileName`, or NULL if it fails */ |
Yann Collet | caf40d0 | 2019-10-17 16:58:49 -0700 | [diff] [blame] | 579 | static FILE* |
senhuang42 | 0e8ac6b | 2020-09-24 15:49:30 -0400 | [diff] [blame] | 580 | FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs, |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 581 | const char* srcFileName, const char* dstFileName, |
| 582 | const int mode) |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 583 | { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 584 | int isDstRegFile; |
| 585 | |
Yann Collet | 0a24d4e | 2019-10-17 16:39:47 -0700 | [diff] [blame] | 586 | if (prefs->testMode) return NULL; /* do not open file in test mode */ |
| 587 | |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 588 | assert(dstFileName != NULL); |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 589 | if (!strcmp (dstFileName, stdoutmark)) { |
Yann Collet | 105fa95 | 2018-12-20 09:16:40 -0800 | [diff] [blame] | 590 | DISPLAYLEVEL(4,"Using stdout for output \n"); |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 591 | SET_BINARY_MODE(stdout); |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 592 | if (prefs->sparseFileSupport == 1) { |
| 593 | prefs->sparseFileSupport = 0; |
Yann Collet | 75424d1 | 2016-05-23 16:56:56 +0200 | [diff] [blame] | 594 | DISPLAYLEVEL(4, "Sparse File Support is automatically disabled on stdout ; try --sparse \n"); |
| 595 | } |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 596 | return stdout; |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 597 | } |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 598 | |
shakeelrao | 1290933 | 2019-03-23 21:53:13 -0700 | [diff] [blame] | 599 | /* ensure dst is not the same as src */ |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 600 | if (srcFileName != NULL && UTIL_isSameFile(srcFileName, dstFileName)) { |
| 601 | DISPLAYLEVEL(1, "zstd: Refusing to open an output file which will overwrite the input file \n"); |
| 602 | return NULL; |
Yann Collet | 6b7a1d6 | 2018-12-26 15:51:34 -0800 | [diff] [blame] | 603 | } |
Yann Collet | b71adf4 | 2016-07-02 01:05:31 +0200 | [diff] [blame] | 604 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 605 | isDstRegFile = UTIL_isRegularFile(dstFileName); /* invoke once */ |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 606 | if (prefs->sparseFileSupport == 1) { |
Yann Collet | 2e29728 | 2023-04-03 09:45:11 -0700 | [diff] [blame] | 607 | prefs->sparseFileSupport = ZSTD_SPARSE_DEFAULT; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 608 | if (!isDstRegFile) { |
| 609 | prefs->sparseFileSupport = 0; |
| 610 | DISPLAYLEVEL(4, "Sparse File Support is disabled when output is not a file \n"); |
| 611 | } |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 614 | if (isDstRegFile) { |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 615 | /* Check if destination file already exists */ |
Yann Collet | 0a24d4e | 2019-10-17 16:39:47 -0700 | [diff] [blame] | 616 | #if !defined(_WIN32) |
| 617 | /* this test does not work on Windows : |
| 618 | * `NUL` and `nul` are detected as regular files */ |
Yann Collet | 105fa95 | 2018-12-20 09:16:40 -0800 | [diff] [blame] | 619 | if (!strcmp(dstFileName, nulmark)) { |
| 620 | EXM_THROW(40, "%s is unexpectedly categorized as a regular file", |
| 621 | dstFileName); |
| 622 | } |
Yann Collet | 0a24d4e | 2019-10-17 16:39:47 -0700 | [diff] [blame] | 623 | #endif |
W. Felix Handte | 1fb10ba | 2021-03-08 17:49:20 -0500 | [diff] [blame] | 624 | if (!prefs->overwrite) { |
| 625 | if (g_display_prefs.displayLevel <= 1) { |
| 626 | /* No interaction possible */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 627 | DISPLAYLEVEL(1, "zstd: %s already exists; not overwritten \n", |
W. Felix Handte | 1fb10ba | 2021-03-08 17:49:20 -0500 | [diff] [blame] | 628 | dstFileName); |
| 629 | return NULL; |
senhuang42 | aab11ce | 2020-08-25 11:25:49 -0400 | [diff] [blame] | 630 | } |
W. Felix Handte | 1fb10ba | 2021-03-08 17:49:20 -0500 | [diff] [blame] | 631 | DISPLAY("zstd: %s already exists; ", dstFileName); |
| 632 | if (UTIL_requireUserConfirmation("overwrite (y/n) ? ", "Not overwritten \n", "yY", fCtx->hasStdinInput)) |
| 633 | return NULL; |
| 634 | } |
| 635 | /* need to unlink */ |
| 636 | FIO_removeFile(dstFileName); |
| 637 | } |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 638 | |
W. Felix Handte | 45c4918 | 2021-03-09 01:24:11 -0500 | [diff] [blame] | 639 | { |
| 640 | #if defined(_WIN32) |
| 641 | /* Windows requires opening the file as a "binary" file to avoid |
| 642 | * mangling. This macro doesn't exist on unix. */ |
| 643 | const int openflags = O_WRONLY|O_CREAT|O_TRUNC|O_BINARY; |
W. Felix Handte | 4f9c6fd | 2021-05-05 13:13:56 -0400 | [diff] [blame] | 644 | const int fd = _open(dstFileName, openflags, mode); |
| 645 | FILE* f = NULL; |
| 646 | if (fd != -1) { |
| 647 | f = _fdopen(fd, "wb"); |
| 648 | } |
W. Felix Handte | 45c4918 | 2021-03-09 01:24:11 -0500 | [diff] [blame] | 649 | #else |
| 650 | const int openflags = O_WRONLY|O_CREAT|O_TRUNC; |
W. Felix Handte | 45c4918 | 2021-03-09 01:24:11 -0500 | [diff] [blame] | 651 | const int fd = open(dstFileName, openflags, mode); |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 652 | FILE* f = NULL; |
| 653 | if (fd != -1) { |
| 654 | f = fdopen(fd, "wb"); |
| 655 | } |
W. Felix Handte | 4f9c6fd | 2021-05-05 13:13:56 -0400 | [diff] [blame] | 656 | #endif |
Mike Swanson | af80f6d | 2019-06-09 01:52:45 -0700 | [diff] [blame] | 657 | if (f == NULL) { |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 658 | DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno)); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 659 | } else { |
| 660 | /* An increased buffer size can provide a significant performance |
| 661 | * boost on some platforms. Note that providing a NULL buf with a |
| 662 | * size that's not 0 is not defined in ANSI C, but is defined in an |
| 663 | * extension. There are three possibilities here: |
| 664 | * 1. Libc supports the extended version and everything is good. |
| 665 | * 2. Libc ignores the size when buf is NULL, in which case |
| 666 | * everything will continue as if we didn't call `setvbuf()`. |
| 667 | * 3. We fail the call and execution continues but a warning |
| 668 | * message might be shown. |
| 669 | * In all cases due execution continues. For now, I believe that |
| 670 | * this is a more cost-effective solution than managing the buffers |
| 671 | * allocations ourselves (will require an API change). |
| 672 | */ |
| 673 | if (setvbuf(f, NULL, _IOFBF, 1 MB)) { |
| 674 | DISPLAYLEVEL(2, "Warning: setvbuf failed for %s\n", dstFileName); |
| 675 | } |
Mike Swanson | af80f6d | 2019-06-09 01:52:45 -0700 | [diff] [blame] | 676 | } |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 677 | return f; |
| 678 | } |
Yann Collet | f062436 | 2016-02-12 15:56:46 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 681 | |
| 682 | /* FIO_getDictFileStat() : |
| 683 | */ |
| 684 | static void FIO_getDictFileStat(const char* fileName, stat_t* dictFileStat) { |
| 685 | assert(dictFileStat != NULL); |
| 686 | if (fileName == NULL) return; |
| 687 | |
| 688 | if (!UTIL_stat(fileName, dictFileStat)) { |
| 689 | EXM_THROW(31, "Stat failed on dictionary file %s: %s", fileName, strerror(errno)); |
| 690 | } |
| 691 | |
| 692 | if (!UTIL_isRegularFileStat(dictFileStat)) { |
| 693 | EXM_THROW(32, "Dictionary %s must be a regular file.", fileName); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* FIO_setDictBufferMalloc() : |
| 698 | * allocates a buffer, pointed by `dict->dictBuffer`, |
Yann Collet | 0e30059 | 2017-04-11 14:41:02 -0700 | [diff] [blame] | 699 | * loads `filename` content into it, up to DICTSIZE_MAX bytes. |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 700 | * @return : loaded size |
Yann Collet | 0e30059 | 2017-04-11 14:41:02 -0700 | [diff] [blame] | 701 | * if fileName==NULL, returns 0 and a NULL pointer |
| 702 | */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 703 | static size_t FIO_setDictBufferMalloc(FIO_Dict_t* dict, const char* fileName, FIO_prefs_t* const prefs, stat_t* dictFileStat) |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 704 | { |
| 705 | FILE* fileHandle; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 706 | U64 fileSize; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 707 | void** bufferPtr = &dict->dictBuffer; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 708 | |
Yann Collet | eac4253 | 2017-10-19 11:56:14 -0700 | [diff] [blame] | 709 | assert(bufferPtr != NULL); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 710 | assert(dictFileStat != NULL); |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 711 | *bufferPtr = NULL; |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 712 | if (fileName == NULL) return 0; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 713 | |
| 714 | DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName); |
Yann Collet | ed2fb6b | 2018-12-20 17:20:07 -0800 | [diff] [blame] | 715 | |
W. Felix Handte | 9985e10 | 2021-12-06 13:47:18 -0500 | [diff] [blame] | 716 | fileHandle = fopen(fileName, "rb"); |
| 717 | |
| 718 | if (fileHandle == NULL) { |
| 719 | EXM_THROW(33, "Couldn't open dictionary %s: %s", fileName, strerror(errno)); |
| 720 | } |
| 721 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 722 | fileSize = UTIL_getFileSizeStat(dictFileStat); |
Bimba Shrestha | f25a6e9 | 2020-01-10 14:25:24 -0800 | [diff] [blame] | 723 | { |
| 724 | size_t const dictSizeMax = prefs->patchFromMode ? prefs->memLimit : DICTSIZE_MAX; |
| 725 | if (fileSize > dictSizeMax) { |
W. Felix Handte | 9985e10 | 2021-12-06 13:47:18 -0500 | [diff] [blame] | 726 | EXM_THROW(34, "Dictionary file %s is too large (> %u bytes)", |
Bimba Shrestha | f25a6e9 | 2020-01-10 14:25:24 -0800 | [diff] [blame] | 727 | fileName, (unsigned)dictSizeMax); /* avoid extreme cases */ |
| 728 | } |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 729 | } |
Yann Collet | b71adf4 | 2016-07-02 01:05:31 +0200 | [diff] [blame] | 730 | *bufferPtr = malloc((size_t)fileSize); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 731 | if (*bufferPtr==NULL) EXM_THROW(34, "%s", strerror(errno)); |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 732 | { size_t const readSize = fread(*bufferPtr, 1, (size_t)fileSize, fileHandle); |
W. Felix Handte | 9985e10 | 2021-12-06 13:47:18 -0500 | [diff] [blame] | 733 | if (readSize != fileSize) { |
Yann Collet | 0f2d443 | 2018-12-19 17:25:58 -0800 | [diff] [blame] | 734 | EXM_THROW(35, "Error reading dictionary file %s : %s", |
| 735 | fileName, strerror(errno)); |
W. Felix Handte | 9985e10 | 2021-12-06 13:47:18 -0500 | [diff] [blame] | 736 | } |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 737 | } |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 738 | fclose(fileHandle); |
| 739 | return (size_t)fileSize; |
| 740 | } |
| 741 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 742 | #if (PLATFORM_POSIX_VERSION > 0) |
| 743 | #include <sys/mman.h> |
| 744 | static void FIO_munmap(FIO_Dict_t* dict) |
| 745 | { |
| 746 | munmap(dict->dictBuffer, dict->dictBufferSize); |
| 747 | dict->dictBuffer = NULL; |
| 748 | dict->dictBufferSize = 0; |
| 749 | } |
| 750 | static size_t FIO_setDictBufferMMap(FIO_Dict_t* dict, const char* fileName, FIO_prefs_t* const prefs, stat_t* dictFileStat) |
| 751 | { |
| 752 | int fileHandle; |
| 753 | U64 fileSize; |
| 754 | void** bufferPtr = &dict->dictBuffer; |
| 755 | |
| 756 | assert(bufferPtr != NULL); |
| 757 | assert(dictFileStat != NULL); |
| 758 | *bufferPtr = NULL; |
| 759 | if (fileName == NULL) return 0; |
| 760 | |
| 761 | DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName); |
| 762 | |
| 763 | fileHandle = open(fileName, O_RDONLY); |
| 764 | |
| 765 | if (fileHandle == -1) { |
| 766 | EXM_THROW(33, "Couldn't open dictionary %s: %s", fileName, strerror(errno)); |
| 767 | } |
| 768 | |
| 769 | fileSize = UTIL_getFileSizeStat(dictFileStat); |
| 770 | { |
| 771 | size_t const dictSizeMax = prefs->patchFromMode ? prefs->memLimit : DICTSIZE_MAX; |
| 772 | if (fileSize > dictSizeMax) { |
| 773 | EXM_THROW(34, "Dictionary file %s is too large (> %u bytes)", |
| 774 | fileName, (unsigned)dictSizeMax); /* avoid extreme cases */ |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | *bufferPtr = mmap(NULL, (size_t)fileSize, PROT_READ, MAP_PRIVATE, fileHandle, 0); |
| 779 | if (*bufferPtr==NULL) EXM_THROW(34, "%s", strerror(errno)); |
| 780 | |
| 781 | close(fileHandle); |
| 782 | return (size_t)fileSize; |
| 783 | } |
| 784 | #elif defined(_MSC_VER) || defined(_WIN32) |
| 785 | #include <windows.h> |
| 786 | static void FIO_munmap(FIO_Dict_t* dict) |
| 787 | { |
| 788 | UnmapViewOfFile(dict->dictBuffer); |
| 789 | CloseHandle(dict->dictHandle); |
| 790 | dict->dictBuffer = NULL; |
| 791 | dict->dictBufferSize = 0; |
| 792 | } |
| 793 | static size_t FIO_setDictBufferMMap(FIO_Dict_t* dict, const char* fileName, FIO_prefs_t* const prefs, stat_t* dictFileStat) |
| 794 | { |
| 795 | HANDLE fileHandle, mapping; |
| 796 | U64 fileSize; |
| 797 | void** bufferPtr = &dict->dictBuffer; |
| 798 | |
| 799 | assert(bufferPtr != NULL); |
| 800 | assert(dictFileStat != NULL); |
| 801 | *bufferPtr = NULL; |
| 802 | if (fileName == NULL) return 0; |
| 803 | |
| 804 | DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName); |
| 805 | |
| 806 | fileHandle = CreateFileA(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL); |
| 807 | |
| 808 | if (fileHandle == INVALID_HANDLE_VALUE) { |
| 809 | EXM_THROW(33, "Couldn't open dictionary %s: %s", fileName, strerror(errno)); |
| 810 | } |
| 811 | |
| 812 | fileSize = UTIL_getFileSizeStat(dictFileStat); |
| 813 | { |
| 814 | size_t const dictSizeMax = prefs->patchFromMode ? prefs->memLimit : DICTSIZE_MAX; |
| 815 | if (fileSize > dictSizeMax) { |
| 816 | EXM_THROW(34, "Dictionary file %s is too large (> %u bytes)", |
| 817 | fileName, (unsigned)dictSizeMax); /* avoid extreme cases */ |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | mapping = CreateFileMapping(fileHandle, NULL, PAGE_READONLY, 0, 0, NULL); |
| 822 | if (mapping == NULL) { |
| 823 | EXM_THROW(35, "Couldn't map dictionary %s: %s", fileName, strerror(errno)); |
| 824 | } |
| 825 | |
| 826 | *bufferPtr = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, (DWORD)fileSize); /* we can only cast to DWORD here because dictSize <= 2GB */ |
| 827 | if (*bufferPtr==NULL) EXM_THROW(36, "%s", strerror(errno)); |
| 828 | |
| 829 | dict->dictHandle = fileHandle; |
| 830 | return (size_t)fileSize; |
| 831 | } |
| 832 | #else |
| 833 | static size_t FIO_setDictBufferMMap(FIO_Dict_t* dict, const char* fileName, FIO_prefs_t* const prefs, stat_t* dictFileStat) |
| 834 | { |
| 835 | return FIO_setDictBufferMalloc(dict, fileName, prefs, dictFileStat); |
| 836 | } |
| 837 | static void FIO_munmap(FIO_Dict_t* dict) { |
| 838 | free(dict->dictBuffer); |
| 839 | dict->dictBuffer = NULL; |
| 840 | dict->dictBufferSize = 0; |
| 841 | } |
| 842 | #endif |
| 843 | |
| 844 | static void FIO_freeDict(FIO_Dict_t* dict) { |
| 845 | if (dict->dictBufferType == FIO_mallocDict) { |
| 846 | free(dict->dictBuffer); |
| 847 | dict->dictBuffer = NULL; |
| 848 | dict->dictBufferSize = 0; |
| 849 | } else if (dict->dictBufferType == FIO_mmapDict) { |
| 850 | FIO_munmap(dict); |
| 851 | } else { |
| 852 | assert(0); /* Should not reach this case */ |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | static void FIO_initDict(FIO_Dict_t* dict, const char* fileName, FIO_prefs_t* const prefs, stat_t* dictFileStat, FIO_dictBufferType_t dictBufferType) { |
| 857 | dict->dictBufferType = dictBufferType; |
| 858 | if (dict->dictBufferType == FIO_mallocDict) { |
| 859 | dict->dictBufferSize = FIO_setDictBufferMalloc(dict, fileName, prefs, dictFileStat); |
| 860 | } else if (dict->dictBufferType == FIO_mmapDict) { |
| 861 | dict->dictBufferSize = FIO_setDictBufferMMap(dict, fileName, prefs, dictFileStat); |
| 862 | } else { |
| 863 | assert(0); /* Should not reach this case */ |
| 864 | } |
| 865 | } |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 866 | |
| 867 | |
| 868 | /* FIO_checkFilenameCollisions() : |
| 869 | * Checks for and warns if there are any files that would have the same output path |
| 870 | */ |
| 871 | int FIO_checkFilenameCollisions(const char** filenameTable, unsigned nbFiles) { |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 872 | const char **filenameTableSorted, *prevElem, *filename; |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 873 | unsigned u; |
| 874 | |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 875 | filenameTableSorted = (const char**) malloc(sizeof(char*) * nbFiles); |
| 876 | if (!filenameTableSorted) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 877 | DISPLAYLEVEL(1, "Allocation error during filename collision checking \n"); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 878 | return 1; |
| 879 | } |
Yann Collet | 1795133 | 2019-10-17 15:32:03 -0700 | [diff] [blame] | 880 | |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 881 | for (u = 0; u < nbFiles; ++u) { |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 882 | filename = strrchr(filenameTable[u], PATH_SEP); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 883 | if (filename == NULL) { |
| 884 | filenameTableSorted[u] = filenameTable[u]; |
| 885 | } else { |
| 886 | filenameTableSorted[u] = filename+1; |
| 887 | } |
| 888 | } |
| 889 | |
Sen Huang | 6e406b5 | 2019-10-08 09:54:59 -0400 | [diff] [blame] | 890 | qsort((void*)filenameTableSorted, nbFiles, sizeof(char*), UTIL_compareStr); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 891 | prevElem = filenameTableSorted[0]; |
| 892 | for (u = 1; u < nbFiles; ++u) { |
| 893 | if (strcmp(prevElem, filenameTableSorted[u]) == 0) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 894 | DISPLAYLEVEL(2, "WARNING: Two files have same filename: %s\n", prevElem); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 895 | } |
| 896 | prevElem = filenameTableSorted[u]; |
| 897 | } |
| 898 | |
Sen Huang | 6e406b5 | 2019-10-08 09:54:59 -0400 | [diff] [blame] | 899 | free((void*)filenameTableSorted); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 900 | return 0; |
| 901 | } |
| 902 | |
Yann Collet | ad86a5d | 2019-10-18 11:15:10 -0700 | [diff] [blame] | 903 | static const char* |
| 904 | extractFilename(const char* path, char separator) |
| 905 | { |
| 906 | const char* search = strrchr(path, separator); |
| 907 | if (search == NULL) return path; |
| 908 | return search+1; |
| 909 | } |
| 910 | |
Sen Huang | 6b81bfb | 2019-10-03 15:23:49 -0400 | [diff] [blame] | 911 | /* FIO_createFilename_fromOutDir() : |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 912 | * Takes a source file name and specified output directory, and |
| 913 | * allocates memory for and returns a pointer to final path. |
| 914 | * This function never returns an error (it may abort() in case of pb) |
| 915 | */ |
| 916 | static char* |
Yann Collet | ad86a5d | 2019-10-18 11:15:10 -0700 | [diff] [blame] | 917 | FIO_createFilename_fromOutDir(const char* path, const char* outDirName, const size_t suffixLen) |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 918 | { |
Yann Collet | ad86a5d | 2019-10-18 11:15:10 -0700 | [diff] [blame] | 919 | const char* filenameStart; |
| 920 | char separator; |
| 921 | char* result; |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 922 | |
Yann Collet | ad86a5d | 2019-10-18 11:15:10 -0700 | [diff] [blame] | 923 | #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__) /* windows support */ |
| 924 | separator = '\\'; |
| 925 | #else |
| 926 | separator = '/'; |
| 927 | #endif |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 928 | |
Yann Collet | ad86a5d | 2019-10-18 11:15:10 -0700 | [diff] [blame] | 929 | filenameStart = extractFilename(path, separator); |
| 930 | #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__) /* windows support */ |
| 931 | filenameStart = extractFilename(filenameStart, '/'); /* sometimes, '/' separator is also used on Windows (mingw+msys2) */ |
| 932 | #endif |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 933 | |
Yann Collet | ad86a5d | 2019-10-18 11:15:10 -0700 | [diff] [blame] | 934 | result = (char*) calloc(1, strlen(outDirName) + 1 + strlen(filenameStart) + suffixLen + 1); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 935 | if (!result) { |
Yann Collet | ad86a5d | 2019-10-18 11:15:10 -0700 | [diff] [blame] | 936 | EXM_THROW(30, "zstd: FIO_createFilename_fromOutDir: %s", strerror(errno)); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 937 | } |
| 938 | |
Yann Collet | ad86a5d | 2019-10-18 11:15:10 -0700 | [diff] [blame] | 939 | memcpy(result, outDirName, strlen(outDirName)); |
| 940 | if (outDirName[strlen(outDirName)-1] == separator) { |
| 941 | memcpy(result + strlen(outDirName), filenameStart, strlen(filenameStart)); |
Yann Collet | 1795133 | 2019-10-17 15:32:03 -0700 | [diff] [blame] | 942 | } else { |
Yann Collet | ad86a5d | 2019-10-18 11:15:10 -0700 | [diff] [blame] | 943 | memcpy(result + strlen(outDirName), &separator, 1); |
| 944 | memcpy(result + strlen(outDirName) + 1, filenameStart, strlen(filenameStart)); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 945 | } |
| 946 | |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 947 | return result; |
| 948 | } |
| 949 | |
Bimba Shrestha | f25a6e9 | 2020-01-10 14:25:24 -0800 | [diff] [blame] | 950 | /* FIO_highbit64() : |
| 951 | * gives position of highest bit. |
| 952 | * note : only works for v > 0 ! |
| 953 | */ |
| 954 | static unsigned FIO_highbit64(unsigned long long v) |
| 955 | { |
| 956 | unsigned count = 0; |
| 957 | assert(v != 0); |
| 958 | v >>= 1; |
| 959 | while (v) { v >>= 1; count++; } |
| 960 | return count; |
| 961 | } |
| 962 | |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 963 | static void FIO_adjustMemLimitForPatchFromMode(FIO_prefs_t* const prefs, |
Bimba Shrestha | 659ff85 | 2020-04-21 21:12:50 -0500 | [diff] [blame] | 964 | unsigned long long const dictSize, |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 965 | unsigned long long const maxSrcFileSize) |
| 966 | { |
| 967 | unsigned long long maxSize = MAX(prefs->memLimit, MAX(dictSize, maxSrcFileSize)); |
Bimba Shrestha | f847909 | 2020-05-26 09:23:26 -0700 | [diff] [blame] | 968 | unsigned const maxWindowSize = (1U << ZSTD_WINDOWLOG_MAX); |
Bimba Shrestha | b067108 | 2020-06-18 09:31:06 -0700 | [diff] [blame] | 969 | if (maxSize == UTIL_FILESIZE_UNKNOWN) |
| 970 | EXM_THROW(42, "Using --patch-from with stdin requires --stream-size"); |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 971 | assert(maxSize != UTIL_FILESIZE_UNKNOWN); |
Bimba Shrestha | f847909 | 2020-05-26 09:23:26 -0700 | [diff] [blame] | 972 | if (maxSize > maxWindowSize) |
| 973 | EXM_THROW(42, "Can't handle files larger than %u GB\n", maxWindowSize/(1 GB)); |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 974 | FIO_setMemLimit(prefs, (unsigned)maxSize); |
| 975 | } |
Bimba Shrestha | f25a6e9 | 2020-01-10 14:25:24 -0800 | [diff] [blame] | 976 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 977 | /* FIO_multiFilesConcatWarning() : |
| 978 | * This function handles logic when processing multiple files with -o or -c, displaying the appropriate warnings/prompts. |
senhuang42 | 7991c55 | 2020-08-26 16:50:20 -0400 | [diff] [blame] | 979 | * Returns 1 if the console should abort, 0 if console should proceed. |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 980 | * |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 981 | * If output is stdout or test mode is active, check that `--rm` disabled. |
| 982 | * |
| 983 | * If there is just 1 file to process, zstd will proceed as usual. |
| 984 | * If each file get processed into its own separate destination file, proceed as usual. |
| 985 | * |
| 986 | * When multiple files are processed into a single output, |
| 987 | * display a warning message, then disable --rm if it's set. |
| 988 | * |
| 989 | * If -f is specified or if output is stdout, just proceed. |
| 990 | * If output is set with -o, prompt for confirmation. |
senhuang42 | 7991c55 | 2020-08-26 16:50:20 -0400 | [diff] [blame] | 991 | */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 992 | static int FIO_multiFilesConcatWarning(const FIO_ctx_t* fCtx, FIO_prefs_t* prefs, const char* outFileName, int displayLevelCutoff) |
senhuang42 | 7991c55 | 2020-08-26 16:50:20 -0400 | [diff] [blame] | 993 | { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 994 | if (fCtx->hasStdoutOutput) { |
| 995 | if (prefs->removeSrcFile) |
| 996 | /* this should not happen ; hard fail, to protect user's data |
| 997 | * note: this should rather be an assert(), but we want to be certain that user's data will not be wiped out in case it nonetheless happen */ |
| 998 | EXM_THROW(43, "It's not allowed to remove input files when processed output is piped to stdout. " |
| 999 | "This scenario is not supposed to be possible. " |
| 1000 | "This is a programming error. File an issue for it to be fixed."); |
Yann Collet | 02434e0 | 2023-01-25 16:18:20 -0800 | [diff] [blame] | 1001 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1002 | if (prefs->testMode) { |
| 1003 | if (prefs->removeSrcFile) |
| 1004 | /* this should not happen ; hard fail, to protect user's data |
| 1005 | * note: this should rather be an assert(), but we want to be certain that user's data will not be wiped out in case it nonetheless happen */ |
| 1006 | EXM_THROW(43, "Test mode shall not remove input files! " |
| 1007 | "This scenario is not supposed to be possible. " |
| 1008 | "This is a programming error. File an issue for it to be fixed."); |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | if (fCtx->nbFilesTotal == 1) return 0; |
| 1013 | assert(fCtx->nbFilesTotal > 1); |
| 1014 | |
| 1015 | if (!outFileName) return 0; |
| 1016 | |
| 1017 | if (fCtx->hasStdoutOutput) { |
| 1018 | DISPLAYLEVEL(2, "zstd: WARNING: all input files will be processed and concatenated into stdout. \n"); |
| 1019 | } else { |
| 1020 | DISPLAYLEVEL(2, "zstd: WARNING: all input files will be processed and concatenated into a single output file: %s \n", outFileName); |
| 1021 | } |
| 1022 | DISPLAYLEVEL(2, "The concatenated output CANNOT regenerate original file names nor directory structure. \n") |
| 1023 | |
| 1024 | /* multi-input into single output : --rm is not allowed */ |
| 1025 | if (prefs->removeSrcFile) { |
| 1026 | DISPLAYLEVEL(2, "Since it's a destructive operation, input files will not be removed. \n"); |
| 1027 | prefs->removeSrcFile = 0; |
| 1028 | } |
| 1029 | |
| 1030 | if (fCtx->hasStdoutOutput) return 0; |
| 1031 | if (prefs->overwrite) return 0; |
| 1032 | |
| 1033 | /* multiple files concatenated into single destination file using -o without -f */ |
| 1034 | if (g_display_prefs.displayLevel <= displayLevelCutoff) { |
| 1035 | /* quiet mode => no prompt => fail automatically */ |
| 1036 | DISPLAYLEVEL(1, "Concatenating multiple processed inputs into a single output loses file metadata. \n"); |
| 1037 | DISPLAYLEVEL(1, "Aborting. \n"); |
| 1038 | return 1; |
| 1039 | } |
| 1040 | /* normal mode => prompt */ |
| 1041 | return UTIL_requireUserConfirmation("Proceed? (y/n): ", "Aborting...", "yY", fCtx->hasStdinInput); |
| 1042 | } |
| 1043 | |
| 1044 | static ZSTD_inBuffer setInBuffer(const void* buf, size_t s, size_t pos) |
| 1045 | { |
| 1046 | ZSTD_inBuffer i; |
| 1047 | i.src = buf; |
| 1048 | i.size = s; |
| 1049 | i.pos = pos; |
| 1050 | return i; |
| 1051 | } |
| 1052 | |
| 1053 | static ZSTD_outBuffer setOutBuffer(void* buf, size_t s, size_t pos) |
| 1054 | { |
| 1055 | ZSTD_outBuffer o; |
| 1056 | o.dst = buf; |
| 1057 | o.size = s; |
| 1058 | o.pos = pos; |
| 1059 | return o; |
Yann Collet | ea684c3 | 2023-01-18 15:38:36 -0800 | [diff] [blame] | 1060 | } |
| 1061 | |
inikep | 3c7c352 | 2016-04-22 13:59:05 +0200 | [diff] [blame] | 1062 | #ifndef ZSTD_NOCOMPRESS |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1063 | |
Yann Collet | 01082a3 | 2018-03-22 17:49:46 -0700 | [diff] [blame] | 1064 | /* ********************************************************************** |
| 1065 | * Compression |
| 1066 | ************************************************************************/ |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1067 | typedef struct { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1068 | FIO_Dict_t dict; |
shakeelrao | 1290933 | 2019-03-23 21:53:13 -0700 | [diff] [blame] | 1069 | const char* dictFileName; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1070 | stat_t dictFileStat; |
Yann Collet | 6263ba5 | 2016-08-13 23:45:45 +0200 | [diff] [blame] | 1071 | ZSTD_CStream* cctx; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1072 | WritePoolCtx_t *writeCtx; |
| 1073 | ReadPoolCtx_t *readCtx; |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1074 | } cRess_t; |
| 1075 | |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 1076 | /** ZSTD_cycleLog() : |
| 1077 | * condition for correct operation : hashLog > 1 */ |
| 1078 | static U32 ZSTD_cycleLog(U32 hashLog, ZSTD_strategy strat) |
| 1079 | { |
| 1080 | U32 const btScale = ((U32)strat >= (U32)ZSTD_btlazy2); |
| 1081 | assert(hashLog > 1); |
| 1082 | return hashLog - btScale; |
| 1083 | } |
| 1084 | |
Bimba Shrestha | 659ff85 | 2020-04-21 21:12:50 -0500 | [diff] [blame] | 1085 | static void FIO_adjustParamsForPatchFromMode(FIO_prefs_t* const prefs, |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1086 | ZSTD_compressionParameters* comprParams, |
Bimba Shrestha | 659ff85 | 2020-04-21 21:12:50 -0500 | [diff] [blame] | 1087 | unsigned long long const dictSize, |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1088 | unsigned long long const maxSrcFileSize, |
| 1089 | int cLevel) |
| 1090 | { |
| 1091 | unsigned const fileWindowLog = FIO_highbit64(maxSrcFileSize) + 1; |
| 1092 | ZSTD_compressionParameters const cParams = ZSTD_getCParams(cLevel, (size_t)maxSrcFileSize, (size_t)dictSize); |
| 1093 | FIO_adjustMemLimitForPatchFromMode(prefs, dictSize, maxSrcFileSize); |
| 1094 | if (fileWindowLog > ZSTD_WINDOWLOG_MAX) |
| 1095 | DISPLAYLEVEL(1, "Max window log exceeded by file (compression ratio will suffer)\n"); |
Olivier Perret | d4548c9 | 2021-05-12 22:11:15 +0200 | [diff] [blame] | 1096 | comprParams->windowLog = MAX(ZSTD_WINDOWLOG_MIN, MIN(ZSTD_WINDOWLOG_MAX, fileWindowLog)); |
senhuang42 | a39614d | 2020-10-13 13:00:27 -0400 | [diff] [blame] | 1097 | if (fileWindowLog > ZSTD_cycleLog(cParams.chainLog, cParams.strategy)) { |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1098 | if (!prefs->ldmFlag) |
Bimba Shrestha | e2838d9 | 2020-06-05 03:19:30 -0500 | [diff] [blame] | 1099 | DISPLAYLEVEL(1, "long mode automatically triggered\n"); |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1100 | FIO_setLdmFlag(prefs, 1); |
| 1101 | } |
| 1102 | if (cParams.strategy >= ZSTD_btopt) { |
| 1103 | DISPLAYLEVEL(1, "[Optimal parser notes] Consider the following to improve patch size at the cost of speed:\n"); |
Bimba Shrestha | 587a20a | 2020-04-20 10:25:58 -0700 | [diff] [blame] | 1104 | DISPLAYLEVEL(1, "- Use --single-thread mode in the zstd cli\n"); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1105 | DISPLAYLEVEL(1, "- Set a larger targetLength (e.g. --zstd=targetLength=4096)\n"); |
| 1106 | DISPLAYLEVEL(1, "- Set a larger chainLog (e.g. --zstd=chainLog=%u)\n", ZSTD_CHAINLOG_MAX); |
senhuang42 | 30fe49a | 2021-09-07 10:08:35 -0400 | [diff] [blame] | 1107 | DISPLAYLEVEL(1, "Also consider playing around with searchLog and hashLog\n"); |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1108 | } |
| 1109 | } |
| 1110 | |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 1111 | static cRess_t FIO_createCResources(FIO_prefs_t* const prefs, |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1112 | const char* dictFileName, unsigned long long const maxSrcFileSize, |
Bimba Shrestha | f25a6e9 | 2020-01-10 14:25:24 -0800 | [diff] [blame] | 1113 | int cLevel, ZSTD_compressionParameters comprParams) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1114 | int useMMap = prefs->mmapDict == ZSTD_ps_enable; |
| 1115 | int forceNoUseMMap = prefs->mmapDict == ZSTD_ps_disable; |
| 1116 | FIO_dictBufferType_t dictBufferType; |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1117 | cRess_t ress; |
Yann Collet | 5e80dd3 | 2016-07-13 17:38:39 +0200 | [diff] [blame] | 1118 | memset(&ress, 0, sizeof(ress)); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1119 | |
Yann Collet | 6a9b41b | 2018-03-11 19:56:48 -0700 | [diff] [blame] | 1120 | DISPLAYLEVEL(6, "FIO_createCResources \n"); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 1121 | ress.cctx = ZSTD_createCCtx(); |
| 1122 | if (ress.cctx == NULL) |
Yann Collet | 0f2d443 | 2018-12-19 17:25:58 -0800 | [diff] [blame] | 1123 | EXM_THROW(30, "allocation error (%s): can't create ZSTD_CCtx", |
| 1124 | strerror(errno)); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1125 | |
| 1126 | FIO_getDictFileStat(dictFileName, &ress.dictFileStat); |
Danielle Rozenblit | 8a189b1 | 2023-02-13 15:23:06 -0800 | [diff] [blame] | 1127 | |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1128 | /* need to update memLimit before calling createDictBuffer |
| 1129 | * because of memLimit check inside it */ |
Bimba Shrestha | 6653321 | 2020-06-18 09:28:18 -0700 | [diff] [blame] | 1130 | if (prefs->patchFromMode) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1131 | U64 const dictSize = UTIL_getFileSizeStat(&ress.dictFileStat); |
Bimba Shrestha | 6653321 | 2020-06-18 09:28:18 -0700 | [diff] [blame] | 1132 | unsigned long long const ssSize = (unsigned long long)prefs->streamSrcSize; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1133 | useMMap |= dictSize > prefs->memLimit; |
| 1134 | FIO_adjustParamsForPatchFromMode(prefs, &comprParams, dictSize, ssSize > 0 ? ssSize : maxSrcFileSize, cLevel); |
Bimba Shrestha | 6653321 | 2020-06-18 09:28:18 -0700 | [diff] [blame] | 1135 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1136 | |
| 1137 | dictBufferType = (useMMap && !forceNoUseMMap) ? FIO_mmapDict : FIO_mallocDict; |
| 1138 | FIO_initDict(&ress.dict, dictFileName, prefs, &ress.dictFileStat, dictBufferType); /* works with dictFileName==NULL */ |
| 1139 | |
| 1140 | ress.writeCtx = AIO_WritePool_create(prefs, ZSTD_CStreamOutSize()); |
| 1141 | ress.readCtx = AIO_ReadPool_create(prefs, ZSTD_CStreamInSize()); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1142 | |
Yann Collet | 4b6a94f | 2018-02-01 17:07:27 -0800 | [diff] [blame] | 1143 | /* Advanced parameters, including dictionary */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1144 | if (dictFileName && (ress.dict.dictBuffer==NULL)) |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1145 | EXM_THROW(32, "allocation error : can't create dictBuffer"); |
| 1146 | ress.dictFileName = dictFileName; |
Yann Collet | 9e6a2ea | 2017-06-11 18:39:46 -0700 | [diff] [blame] | 1147 | |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1148 | if (prefs->adaptiveMode && !prefs->ldmFlag && !comprParams.windowLog) |
| 1149 | comprParams.windowLog = ADAPT_WINDOWLOG_DEFAULT; |
Yann Collet | 0853f86 | 2018-08-13 13:10:42 -0700 | [diff] [blame] | 1150 | |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1151 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_contentSizeFlag, prefs->contentSize) ); /* always enable content size when available (note: supposed to be default) */ |
| 1152 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_dictIDFlag, prefs->dictIDFlag) ); |
| 1153 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_checksumFlag, prefs->checksumFlag) ); |
| 1154 | /* compression level */ |
| 1155 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_compressionLevel, cLevel) ); |
| 1156 | /* max compressed block size */ |
| 1157 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_targetCBlockSize, (int)prefs->targetCBlockSize) ); |
| 1158 | /* source size hint */ |
| 1159 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_srcSizeHint, (int)prefs->srcSizeHint) ); |
| 1160 | /* long distance matching */ |
| 1161 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_enableLongDistanceMatching, prefs->ldmFlag) ); |
| 1162 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_ldmHashLog, prefs->ldmHashLog) ); |
| 1163 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_ldmMinMatch, prefs->ldmMinMatch) ); |
| 1164 | if (prefs->ldmBucketSizeLog != FIO_LDM_PARAM_NOTSET) { |
| 1165 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_ldmBucketSizeLog, prefs->ldmBucketSizeLog) ); |
Yann Collet | 43eeea4 | 2016-09-15 15:38:44 +0200 | [diff] [blame] | 1166 | } |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1167 | if (prefs->ldmHashRateLog != FIO_LDM_PARAM_NOTSET) { |
| 1168 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_ldmHashRateLog, prefs->ldmHashRateLog) ); |
| 1169 | } |
Nick Terrell | 4694423 | 2020-11-02 17:52:29 -0800 | [diff] [blame] | 1170 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_useRowMatchFinder, prefs->useRowMatchFinder)); |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1171 | /* compression parameters */ |
| 1172 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_windowLog, (int)comprParams.windowLog) ); |
| 1173 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_chainLog, (int)comprParams.chainLog) ); |
| 1174 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_hashLog, (int)comprParams.hashLog) ); |
| 1175 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_searchLog, (int)comprParams.searchLog) ); |
| 1176 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_minMatch, (int)comprParams.minMatch) ); |
| 1177 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_targetLength, (int)comprParams.targetLength) ); |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 1178 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_strategy, (int)comprParams.strategy) ); |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1179 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_literalCompressionMode, (int)prefs->literalCompressionMode) ); |
W. Felix Handte | d463060 | 2020-08-17 12:37:58 -0400 | [diff] [blame] | 1180 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_enableDedicatedDictSearch, 1) ); |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1181 | /* multi-threading */ |
| 1182 | #ifdef ZSTD_MULTITHREAD |
| 1183 | DISPLAYLEVEL(5,"set nb workers = %u \n", prefs->nbWorkers); |
| 1184 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_nbWorkers, prefs->nbWorkers) ); |
| 1185 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_jobSize, prefs->blockSize) ); |
| 1186 | if (prefs->overlapLog != FIO_OVERLAP_LOG_NOTSET) { |
| 1187 | DISPLAYLEVEL(3,"set overlapLog = %u \n", prefs->overlapLog); |
| 1188 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_overlapLog, prefs->overlapLog) ); |
| 1189 | } |
| 1190 | CHECK( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_rsyncable, prefs->rsyncable) ); |
| 1191 | #endif |
| 1192 | /* dictionary */ |
| 1193 | if (prefs->patchFromMode) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1194 | CHECK( ZSTD_CCtx_refPrefix(ress.cctx, ress.dict.dictBuffer, ress.dict.dictBufferSize) ); |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1195 | } else { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1196 | CHECK( ZSTD_CCtx_loadDictionary_byReference(ress.cctx, ress.dict.dictBuffer, ress.dict.dictBufferSize) ); |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 1197 | } |
Bimba Shrestha | 659ff85 | 2020-04-21 21:12:50 -0500 | [diff] [blame] | 1198 | |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1199 | return ress; |
| 1200 | } |
| 1201 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1202 | static void FIO_freeCResources(cRess_t* const ress) |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1203 | { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1204 | FIO_freeDict(&(ress->dict)); |
| 1205 | AIO_WritePool_free(ress->writeCtx); |
| 1206 | AIO_ReadPool_free(ress->readCtx); |
senhuang42 | 043b934 | 2020-10-14 20:19:46 -0400 | [diff] [blame] | 1207 | ZSTD_freeCStream(ress->cctx); /* never fails */ |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1211 | #ifdef ZSTD_GZCOMPRESS |
Yann Collet | 89bc309 | 2018-09-19 14:49:13 -0700 | [diff] [blame] | 1212 | static unsigned long long |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1213 | FIO_compressGzFrame(const cRess_t* ress, /* buffers & handlers are used, but not changed */ |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1214 | const char* srcFileName, U64 const srcFileSize, |
| 1215 | int compressionLevel, U64* readsize) |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1216 | { |
| 1217 | unsigned long long inFileSize = 0, outFileSize = 0; |
| 1218 | z_stream strm; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1219 | IOJob_t *writeJob = NULL; |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1220 | |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1221 | if (compressionLevel > Z_BEST_COMPRESSION) |
| 1222 | compressionLevel = Z_BEST_COMPRESSION; |
Przemyslaw Skibinski | 64f7221 | 2017-02-13 21:00:41 +0100 | [diff] [blame] | 1223 | |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1224 | strm.zalloc = Z_NULL; |
| 1225 | strm.zfree = Z_NULL; |
| 1226 | strm.opaque = Z_NULL; |
Przemyslaw Skibinski | cb56306 | 2017-02-08 17:37:14 +0100 | [diff] [blame] | 1227 | |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1228 | { int const ret = deflateInit2(&strm, compressionLevel, Z_DEFLATED, |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1229 | 15 /* maxWindowLogSize */ + 16 /* gzip only */, |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1230 | 8, Z_DEFAULT_STRATEGY); /* see https://www.zlib.net/manual.html */ |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1231 | if (ret != Z_OK) { |
| 1232 | EXM_THROW(71, "zstd: %s: deflateInit2 error %d \n", srcFileName, ret); |
| 1233 | } } |
Przemyslaw Skibinski | cb56306 | 2017-02-08 17:37:14 +0100 | [diff] [blame] | 1234 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1235 | writeJob = AIO_WritePool_acquireJob(ress->writeCtx); |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1236 | strm.next_in = 0; |
Przemyslaw Skibinski | 862698f | 2017-02-27 13:21:05 +0100 | [diff] [blame] | 1237 | strm.avail_in = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1238 | strm.next_out = (Bytef*)writeJob->buffer; |
| 1239 | strm.avail_out = (uInt)writeJob->bufferSize; |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1240 | |
| 1241 | while (1) { |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1242 | int ret; |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1243 | if (strm.avail_in == 0) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1244 | AIO_ReadPool_fillBuffer(ress->readCtx, ZSTD_CStreamInSize()); |
| 1245 | if (ress->readCtx->srcBufferLoaded == 0) break; |
| 1246 | inFileSize += ress->readCtx->srcBufferLoaded; |
| 1247 | strm.next_in = (z_const unsigned char*)ress->readCtx->srcBuffer; |
| 1248 | strm.avail_in = (uInt)ress->readCtx->srcBufferLoaded; |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1249 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1250 | |
| 1251 | { |
| 1252 | size_t const availBefore = strm.avail_in; |
| 1253 | ret = deflate(&strm, Z_NO_FLUSH); |
| 1254 | AIO_ReadPool_consumeBytes(ress->readCtx, availBefore - strm.avail_in); |
| 1255 | } |
| 1256 | |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1257 | if (ret != Z_OK) |
| 1258 | EXM_THROW(72, "zstd: %s: deflate error %d \n", srcFileName, ret); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1259 | { size_t const cSize = writeJob->bufferSize - strm.avail_out; |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1260 | if (cSize) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1261 | writeJob->usedBufferSize = cSize; |
| 1262 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1263 | outFileSize += cSize; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1264 | strm.next_out = (Bytef*)writeJob->buffer; |
| 1265 | strm.avail_out = (uInt)writeJob->bufferSize; |
| 1266 | } } |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1267 | if (srcFileSize == UTIL_FILESIZE_UNKNOWN) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1268 | DISPLAYUPDATE_PROGRESS( |
| 1269 | "\rRead : %u MB ==> %.2f%% ", |
| 1270 | (unsigned)(inFileSize>>20), |
| 1271 | (double)outFileSize/(double)inFileSize*100) |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1272 | } else { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1273 | DISPLAYUPDATE_PROGRESS( |
| 1274 | "\rRead : %u / %u MB ==> %.2f%% ", |
| 1275 | (unsigned)(inFileSize>>20), (unsigned)(srcFileSize>>20), |
| 1276 | (double)outFileSize/(double)inFileSize*100); |
Nick Terrell | fbff782 | 2022-01-07 15:07:28 -0800 | [diff] [blame] | 1277 | } } |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1278 | |
| 1279 | while (1) { |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1280 | int const ret = deflate(&strm, Z_FINISH); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1281 | { size_t const cSize = writeJob->bufferSize - strm.avail_out; |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1282 | if (cSize) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1283 | writeJob->usedBufferSize = cSize; |
| 1284 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1285 | outFileSize += cSize; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1286 | strm.next_out = (Bytef*)writeJob->buffer; |
| 1287 | strm.avail_out = (uInt)writeJob->bufferSize; |
| 1288 | } } |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1289 | if (ret == Z_STREAM_END) break; |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1290 | if (ret != Z_BUF_ERROR) |
| 1291 | EXM_THROW(77, "zstd: %s: deflate error %d \n", srcFileName, ret); |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1292 | } |
| 1293 | |
Yann Collet | 632e077 | 2019-10-21 12:14:59 -0700 | [diff] [blame] | 1294 | { int const ret = deflateEnd(&strm); |
| 1295 | if (ret != Z_OK) { |
| 1296 | EXM_THROW(79, "zstd: %s: deflateEnd error %d \n", srcFileName, ret); |
| 1297 | } } |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1298 | *readsize = inFileSize; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1299 | AIO_WritePool_releaseIoJob(writeJob); |
| 1300 | AIO_WritePool_sparseWriteEnd(ress->writeCtx); |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1301 | return outFileSize; |
| 1302 | } |
| 1303 | #endif |
| 1304 | |
| 1305 | |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1306 | #ifdef ZSTD_LZMACOMPRESS |
Yann Collet | 89bc309 | 2018-09-19 14:49:13 -0700 | [diff] [blame] | 1307 | static unsigned long long |
| 1308 | FIO_compressLzmaFrame(cRess_t* ress, |
| 1309 | const char* srcFileName, U64 const srcFileSize, |
| 1310 | int compressionLevel, U64* readsize, int plain_lzma) |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1311 | { |
| 1312 | unsigned long long inFileSize = 0, outFileSize = 0; |
| 1313 | lzma_stream strm = LZMA_STREAM_INIT; |
| 1314 | lzma_action action = LZMA_RUN; |
| 1315 | lzma_ret ret; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1316 | IOJob_t *writeJob = NULL; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1317 | |
| 1318 | if (compressionLevel < 0) compressionLevel = 0; |
| 1319 | if (compressionLevel > 9) compressionLevel = 9; |
| 1320 | |
| 1321 | if (plain_lzma) { |
| 1322 | lzma_options_lzma opt_lzma; |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1323 | if (lzma_lzma_preset(&opt_lzma, compressionLevel)) |
Yann Collet | caf40d0 | 2019-10-17 16:58:49 -0700 | [diff] [blame] | 1324 | EXM_THROW(81, "zstd: %s: lzma_lzma_preset error", srcFileName); |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1325 | ret = lzma_alone_encoder(&strm, &opt_lzma); /* LZMA */ |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1326 | if (ret != LZMA_OK) |
Yann Collet | caf40d0 | 2019-10-17 16:58:49 -0700 | [diff] [blame] | 1327 | EXM_THROW(82, "zstd: %s: lzma_alone_encoder error %d", srcFileName, ret); |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1328 | } else { |
| 1329 | ret = lzma_easy_encoder(&strm, compressionLevel, LZMA_CHECK_CRC64); /* XZ */ |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1330 | if (ret != LZMA_OK) |
Yann Collet | caf40d0 | 2019-10-17 16:58:49 -0700 | [diff] [blame] | 1331 | EXM_THROW(83, "zstd: %s: lzma_easy_encoder error %d", srcFileName, ret); |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1334 | writeJob =AIO_WritePool_acquireJob(ress->writeCtx); |
| 1335 | strm.next_out = (BYTE*)writeJob->buffer; |
| 1336 | strm.avail_out = writeJob->bufferSize; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1337 | strm.next_in = 0; |
| 1338 | strm.avail_in = 0; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1339 | |
| 1340 | while (1) { |
| 1341 | if (strm.avail_in == 0) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1342 | size_t const inSize = AIO_ReadPool_fillBuffer(ress->readCtx, ZSTD_CStreamInSize()); |
| 1343 | if (ress->readCtx->srcBufferLoaded == 0) action = LZMA_FINISH; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1344 | inFileSize += inSize; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1345 | strm.next_in = (BYTE const*)ress->readCtx->srcBuffer; |
| 1346 | strm.avail_in = ress->readCtx->srcBufferLoaded; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1349 | { |
| 1350 | size_t const availBefore = strm.avail_in; |
| 1351 | ret = lzma_code(&strm, action); |
| 1352 | AIO_ReadPool_consumeBytes(ress->readCtx, availBefore - strm.avail_in); |
| 1353 | } |
| 1354 | |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1355 | |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1356 | if (ret != LZMA_OK && ret != LZMA_STREAM_END) |
Yann Collet | caf40d0 | 2019-10-17 16:58:49 -0700 | [diff] [blame] | 1357 | EXM_THROW(84, "zstd: %s: lzma_code encoding error %d", srcFileName, ret); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1358 | { size_t const compBytes = writeJob->bufferSize - strm.avail_out; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1359 | if (compBytes) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1360 | writeJob->usedBufferSize = compBytes; |
| 1361 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1362 | outFileSize += compBytes; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1363 | strm.next_out = (BYTE*)writeJob->buffer; |
| 1364 | strm.avail_out = writeJob->bufferSize; |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1365 | } } |
Yann Collet | 300e1df | 2017-10-18 11:41:52 -0700 | [diff] [blame] | 1366 | if (srcFileSize == UTIL_FILESIZE_UNKNOWN) |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1367 | DISPLAYUPDATE_PROGRESS("\rRead : %u MB ==> %.2f%%", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1368 | (unsigned)(inFileSize>>20), |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1369 | (double)outFileSize/(double)inFileSize*100) |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1370 | else |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1371 | DISPLAYUPDATE_PROGRESS("\rRead : %u / %u MB ==> %.2f%%", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1372 | (unsigned)(inFileSize>>20), (unsigned)(srcFileSize>>20), |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1373 | (double)outFileSize/(double)inFileSize*100); |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1374 | if (ret == LZMA_STREAM_END) break; |
| 1375 | } |
| 1376 | |
| 1377 | lzma_end(&strm); |
| 1378 | *readsize = inFileSize; |
| 1379 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1380 | AIO_WritePool_releaseIoJob(writeJob); |
| 1381 | AIO_WritePool_sparseWriteEnd(ress->writeCtx); |
| 1382 | |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 1383 | return outFileSize; |
| 1384 | } |
| 1385 | #endif |
| 1386 | |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1387 | #ifdef ZSTD_LZ4COMPRESS |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 1388 | |
W. Felix Handte | baff9dd | 2017-10-17 01:19:29 -0400 | [diff] [blame] | 1389 | #if LZ4_VERSION_NUMBER <= 10600 |
| 1390 | #define LZ4F_blockLinked blockLinked |
| 1391 | #define LZ4F_max64KB max64KB |
| 1392 | #endif |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 1393 | |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1394 | static int FIO_LZ4_GetBlockSize_FromBlockId (int id) { return (1 << (8 + (2 * id))); } |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 1395 | |
Yann Collet | 89bc309 | 2018-09-19 14:49:13 -0700 | [diff] [blame] | 1396 | static unsigned long long |
| 1397 | FIO_compressLz4Frame(cRess_t* ress, |
| 1398 | const char* srcFileName, U64 const srcFileSize, |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 1399 | int compressionLevel, int checksumFlag, |
| 1400 | U64* readsize) |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1401 | { |
W. Felix Handte | baff9dd | 2017-10-17 01:19:29 -0400 | [diff] [blame] | 1402 | const size_t blockSize = FIO_LZ4_GetBlockSize_FromBlockId(LZ4F_max64KB); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1403 | unsigned long long inFileSize = 0, outFileSize = 0; |
| 1404 | |
| 1405 | LZ4F_preferences_t prefs; |
Sean Purcell | 2c4b6fe | 2017-04-25 11:00:54 -0700 | [diff] [blame] | 1406 | LZ4F_compressionContext_t ctx; |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1407 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1408 | IOJob_t* writeJob = AIO_WritePool_acquireJob(ress->writeCtx); |
| 1409 | |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1410 | LZ4F_errorCode_t const errorCode = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION); |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1411 | if (LZ4F_isError(errorCode)) |
| 1412 | EXM_THROW(31, "zstd: failed to create lz4 compression context"); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1413 | |
| 1414 | memset(&prefs, 0, sizeof(prefs)); |
| 1415 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1416 | assert(blockSize <= ress->readCtx->base.jobBufferSize); |
Sean Purcell | eab41c1 | 2017-04-26 10:17:38 -0700 | [diff] [blame] | 1417 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1418 | /* autoflush off to mitigate a bug in lz4<=1.9.3 for compression level 12 */ |
| 1419 | prefs.autoFlush = 0; |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1420 | prefs.compressionLevel = compressionLevel; |
W. Felix Handte | baff9dd | 2017-10-17 01:19:29 -0400 | [diff] [blame] | 1421 | prefs.frameInfo.blockMode = LZ4F_blockLinked; |
| 1422 | prefs.frameInfo.blockSizeID = LZ4F_max64KB; |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 1423 | prefs.frameInfo.contentChecksumFlag = (contentChecksum_t)checksumFlag; |
Sean Purcell | 2c4b6fe | 2017-04-25 11:00:54 -0700 | [diff] [blame] | 1424 | #if LZ4_VERSION_NUMBER >= 10600 |
Yann Collet | 18b7953 | 2017-10-17 16:14:25 -0700 | [diff] [blame] | 1425 | prefs.frameInfo.contentSize = (srcFileSize==UTIL_FILESIZE_UNKNOWN) ? 0 : srcFileSize; |
Sean Purcell | 2c4b6fe | 2017-04-25 11:00:54 -0700 | [diff] [blame] | 1426 | #endif |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1427 | assert(LZ4F_compressBound(blockSize, &prefs) <= writeJob->bufferSize); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1428 | |
W. Felix Handte | baff9dd | 2017-10-17 01:19:29 -0400 | [diff] [blame] | 1429 | { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1430 | size_t headerSize = LZ4F_compressBegin(ctx, writeJob->buffer, writeJob->bufferSize, &prefs); |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1431 | if (LZ4F_isError(headerSize)) |
| 1432 | EXM_THROW(33, "File header generation failed : %s", |
| 1433 | LZ4F_getErrorName(headerSize)); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1434 | writeJob->usedBufferSize = headerSize; |
| 1435 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1436 | outFileSize += headerSize; |
| 1437 | |
| 1438 | /* Read first block */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1439 | inFileSize += AIO_ReadPool_fillBuffer(ress->readCtx, blockSize); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1440 | |
| 1441 | /* Main Loop */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1442 | while (ress->readCtx->srcBufferLoaded) { |
| 1443 | size_t inSize = MIN(blockSize, ress->readCtx->srcBufferLoaded); |
| 1444 | size_t const outSize = LZ4F_compressUpdate(ctx, writeJob->buffer, writeJob->bufferSize, |
| 1445 | ress->readCtx->srcBuffer, inSize, NULL); |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1446 | if (LZ4F_isError(outSize)) |
| 1447 | EXM_THROW(35, "zstd: %s: lz4 compression failed : %s", |
| 1448 | srcFileName, LZ4F_getErrorName(outSize)); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1449 | outFileSize += outSize; |
Yann Collet | 173ef9d | 2018-12-19 18:30:57 -0800 | [diff] [blame] | 1450 | if (srcFileSize == UTIL_FILESIZE_UNKNOWN) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1451 | DISPLAYUPDATE_PROGRESS("\rRead : %u MB ==> %.2f%%", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1452 | (unsigned)(inFileSize>>20), |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1453 | (double)outFileSize/(double)inFileSize*100) |
Yann Collet | 173ef9d | 2018-12-19 18:30:57 -0800 | [diff] [blame] | 1454 | } else { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1455 | DISPLAYUPDATE_PROGRESS("\rRead : %u / %u MB ==> %.2f%%", |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1456 | (unsigned)(inFileSize>>20), (unsigned)(srcFileSize>>20), |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1457 | (double)outFileSize/(double)inFileSize*100); |
Yann Collet | 173ef9d | 2018-12-19 18:30:57 -0800 | [diff] [blame] | 1458 | } |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1459 | |
| 1460 | /* Write Block */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1461 | writeJob->usedBufferSize = outSize; |
| 1462 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1463 | |
| 1464 | /* Read next block */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1465 | AIO_ReadPool_consumeBytes(ress->readCtx, inSize); |
| 1466 | inFileSize += AIO_ReadPool_fillBuffer(ress->readCtx, blockSize); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1467 | } |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1468 | |
| 1469 | /* End of Stream mark */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1470 | headerSize = LZ4F_compressEnd(ctx, writeJob->buffer, writeJob->bufferSize, NULL); |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 1471 | if (LZ4F_isError(headerSize)) |
| 1472 | EXM_THROW(38, "zstd: %s: lz4 end of file generation failed : %s", |
| 1473 | srcFileName, LZ4F_getErrorName(headerSize)); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1474 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1475 | writeJob->usedBufferSize = headerSize; |
| 1476 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1477 | outFileSize += headerSize; |
| 1478 | } |
| 1479 | |
| 1480 | *readsize = inFileSize; |
| 1481 | LZ4F_freeCompressionContext(ctx); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1482 | AIO_WritePool_releaseIoJob(writeJob); |
| 1483 | AIO_WritePool_sparseWriteEnd(ress->writeCtx); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 1484 | |
| 1485 | return outFileSize; |
| 1486 | } |
| 1487 | #endif |
| 1488 | |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1489 | static unsigned long long |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 1490 | FIO_compressZstdFrame(FIO_ctx_t* const fCtx, |
| 1491 | FIO_prefs_t* const prefs, |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 1492 | const cRess_t* ressPtr, |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1493 | const char* srcFileName, U64 fileSize, |
| 1494 | int compressionLevel, U64* readsize) |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1495 | { |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1496 | cRess_t const ress = *ressPtr; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1497 | IOJob_t *writeJob = AIO_WritePool_acquireJob(ressPtr->writeCtx); |
| 1498 | |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1499 | U64 compressedfilesize = 0; |
Nick Terrell | f48d34e | 2017-12-14 13:00:20 -0800 | [diff] [blame] | 1500 | ZSTD_EndDirective directive = ZSTD_e_continue; |
Kevin Svetlitski | b388819 | 2021-11-01 13:31:03 -0700 | [diff] [blame] | 1501 | U64 pledgedSrcSize = ZSTD_CONTENTSIZE_UNKNOWN; |
Yann Collet | 9d26cb6 | 2018-08-09 17:44:30 -0700 | [diff] [blame] | 1502 | |
Yann Collet | e7a49c6 | 2018-08-11 20:48:06 -0700 | [diff] [blame] | 1503 | /* stats */ |
Yann Collet | ca02ebe | 2018-09-19 15:09:45 -0700 | [diff] [blame] | 1504 | ZSTD_frameProgression previous_zfp_update = { 0, 0, 0, 0, 0, 0 }; |
| 1505 | ZSTD_frameProgression previous_zfp_correction = { 0, 0, 0, 0, 0, 0 }; |
Yann Collet | 9d26cb6 | 2018-08-09 17:44:30 -0700 | [diff] [blame] | 1506 | typedef enum { noChange, slower, faster } speedChange_e; |
| 1507 | speedChange_e speedChange = noChange; |
Yann Collet | 105677c | 2018-08-17 18:11:54 -0700 | [diff] [blame] | 1508 | unsigned flushWaiting = 0; |
Yann Collet | e7a49c6 | 2018-08-11 20:48:06 -0700 | [diff] [blame] | 1509 | unsigned inputPresented = 0; |
Yann Collet | 2dd7603 | 2018-08-09 15:51:30 -0700 | [diff] [blame] | 1510 | unsigned inputBlocked = 0; |
| 1511 | unsigned lastJobID = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1512 | UTIL_time_t lastAdaptTime = UTIL_getTime(); |
| 1513 | U64 const adaptEveryMicro = REFRESH_RATE; |
| 1514 | |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 1515 | UTIL_HumanReadableSize_t const file_hrs = UTIL_makeHumanReadableSize(fileSize); |
Yann Collet | 2dd7603 | 2018-08-09 15:51:30 -0700 | [diff] [blame] | 1516 | |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1517 | DISPLAYLEVEL(6, "compression using zstd format \n"); |
Przemyslaw Skibinski | 02018c8 | 2017-02-08 16:54:23 +0100 | [diff] [blame] | 1518 | |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1519 | /* init */ |
Nick Terrell | 3841dba | 2018-06-14 16:24:18 -0700 | [diff] [blame] | 1520 | if (fileSize != UTIL_FILESIZE_UNKNOWN) { |
Kevin Svetlitski | b388819 | 2021-11-01 13:31:03 -0700 | [diff] [blame] | 1521 | pledgedSrcSize = fileSize; |
Nick Terrell | 3841dba | 2018-06-14 16:24:18 -0700 | [diff] [blame] | 1522 | CHECK(ZSTD_CCtx_setPledgedSrcSize(ress.cctx, fileSize)); |
Nick Magerko | c403b12 | 2019-08-19 09:01:31 -0700 | [diff] [blame] | 1523 | } else if (prefs->streamSrcSize > 0) { |
Nick Magerko | 30bfa22 | 2019-08-19 11:20:28 -0700 | [diff] [blame] | 1524 | /* unknown source size; use the declared stream size */ |
Kevin Svetlitski | b388819 | 2021-11-01 13:31:03 -0700 | [diff] [blame] | 1525 | pledgedSrcSize = prefs->streamSrcSize; |
Nick Magerko | c403b12 | 2019-08-19 09:01:31 -0700 | [diff] [blame] | 1526 | CHECK( ZSTD_CCtx_setPledgedSrcSize(ress.cctx, prefs->streamSrcSize) ); |
Nick Terrell | 3841dba | 2018-06-14 16:24:18 -0700 | [diff] [blame] | 1527 | } |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 1528 | |
Kevin Svetlitski | b388819 | 2021-11-01 13:31:03 -0700 | [diff] [blame] | 1529 | { |
| 1530 | int windowLog; |
| 1531 | UTIL_HumanReadableSize_t windowSize; |
| 1532 | CHECK(ZSTD_CCtx_getParameter(ress.cctx, ZSTD_c_windowLog, &windowLog)); |
| 1533 | if (windowLog == 0) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1534 | if (prefs->ldmFlag) { |
| 1535 | /* If long mode is set without a window size libzstd will set this size internally */ |
| 1536 | windowLog = ZSTD_WINDOWLOG_LIMIT_DEFAULT; |
| 1537 | } else { |
| 1538 | const ZSTD_compressionParameters cParams = ZSTD_getCParams(compressionLevel, fileSize, 0); |
| 1539 | windowLog = (int)cParams.windowLog; |
| 1540 | } |
Kevin Svetlitski | b388819 | 2021-11-01 13:31:03 -0700 | [diff] [blame] | 1541 | } |
| 1542 | windowSize = UTIL_makeHumanReadableSize(MAX(1ULL, MIN(1ULL << windowLog, pledgedSrcSize))); |
| 1543 | DISPLAYLEVEL(4, "Decompression will require %.*f%s of memory\n", windowSize.precision, windowSize.value, windowSize.suffix); |
| 1544 | } |
Yann Collet | ca02ebe | 2018-09-19 15:09:45 -0700 | [diff] [blame] | 1545 | (void)srcFileName; |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1546 | |
| 1547 | /* Main compression loop */ |
Nick Terrell | f48d34e | 2017-12-14 13:00:20 -0800 | [diff] [blame] | 1548 | do { |
Yann Collet | 2dd7603 | 2018-08-09 15:51:30 -0700 | [diff] [blame] | 1549 | size_t stillToFlush; |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1550 | /* Fill input Buffer */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1551 | size_t const inSize = AIO_ReadPool_fillBuffer(ress.readCtx, ZSTD_CStreamInSize()); |
| 1552 | ZSTD_inBuffer inBuff = setInBuffer( ress.readCtx->srcBuffer, ress.readCtx->srcBufferLoaded, 0 ); |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 1553 | DISPLAYLEVEL(6, "fread %u bytes from source \n", (unsigned)inSize); |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1554 | *readsize += inSize; |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1555 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1556 | if ((ress.readCtx->srcBufferLoaded == 0) || (*readsize == fileSize)) |
Nick Terrell | f48d34e | 2017-12-14 13:00:20 -0800 | [diff] [blame] | 1557 | directive = ZSTD_e_end; |
| 1558 | |
Yann Collet | 2dd7603 | 2018-08-09 15:51:30 -0700 | [diff] [blame] | 1559 | stillToFlush = 1; |
Yann Collet | 79a35ac | 2018-08-09 15:16:31 -0700 | [diff] [blame] | 1560 | while ((inBuff.pos != inBuff.size) /* input buffer must be entirely ingested */ |
Yann Collet | 2dd7603 | 2018-08-09 15:51:30 -0700 | [diff] [blame] | 1561 | || (directive == ZSTD_e_end && stillToFlush != 0) ) { |
Yann Collet | 9d26cb6 | 2018-08-09 17:44:30 -0700 | [diff] [blame] | 1562 | |
Yann Collet | 2dd7603 | 2018-08-09 15:51:30 -0700 | [diff] [blame] | 1563 | size_t const oldIPos = inBuff.pos; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1564 | ZSTD_outBuffer outBuff = setOutBuffer( writeJob->buffer, writeJob->bufferSize, 0 ); |
Yann Collet | 105677c | 2018-08-17 18:11:54 -0700 | [diff] [blame] | 1565 | size_t const toFlushNow = ZSTD_toFlushNow(ress.cctx); |
Yann Collet | d8e215c | 2018-11-30 11:16:26 -0800 | [diff] [blame] | 1566 | CHECK_V(stillToFlush, ZSTD_compressStream2(ress.cctx, &outBuff, &inBuff, directive)); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1567 | AIO_ReadPool_consumeBytes(ress.readCtx, inBuff.pos - oldIPos); |
Yann Collet | 2dd7603 | 2018-08-09 15:51:30 -0700 | [diff] [blame] | 1568 | |
| 1569 | /* count stats */ |
Yann Collet | e7a49c6 | 2018-08-11 20:48:06 -0700 | [diff] [blame] | 1570 | inputPresented++; |
Yann Collet | 89bc309 | 2018-09-19 14:49:13 -0700 | [diff] [blame] | 1571 | if (oldIPos == inBuff.pos) inputBlocked++; /* input buffer is full and can't take any more : input speed is faster than consumption rate */ |
Yann Collet | 105677c | 2018-08-17 18:11:54 -0700 | [diff] [blame] | 1572 | if (!toFlushNow) flushWaiting = 1; |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1573 | |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 1574 | /* Write compressed stream */ |
Yann Collet | 2dd7603 | 2018-08-09 15:51:30 -0700 | [diff] [blame] | 1575 | DISPLAYLEVEL(6, "ZSTD_compress_generic(end:%u) => input pos(%u)<=(%u)size ; output generated %u bytes \n", |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1576 | (unsigned)directive, (unsigned)inBuff.pos, (unsigned)inBuff.size, (unsigned)outBuff.pos); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 1577 | if (outBuff.pos) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1578 | writeJob->usedBufferSize = outBuff.pos; |
| 1579 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 1580 | compressedfilesize += outBuff.pos; |
Nick Terrell | f48d34e | 2017-12-14 13:00:20 -0800 | [diff] [blame] | 1581 | } |
Yann Collet | 9d26cb6 | 2018-08-09 17:44:30 -0700 | [diff] [blame] | 1582 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1583 | /* adaptive mode : statistics measurement and speed correction */ |
| 1584 | if (prefs->adaptiveMode && UTIL_clockSpanMicro(lastAdaptTime) > adaptEveryMicro) { |
| 1585 | ZSTD_frameProgression const zfp = ZSTD_getFrameProgression(ress.cctx); |
| 1586 | |
| 1587 | lastAdaptTime = UTIL_getTime(); |
| 1588 | |
| 1589 | /* check output speed */ |
| 1590 | if (zfp.currentJobID > 1) { /* only possible if nbWorkers >= 1 */ |
| 1591 | |
| 1592 | unsigned long long newlyProduced = zfp.produced - previous_zfp_update.produced; |
| 1593 | unsigned long long newlyFlushed = zfp.flushed - previous_zfp_update.flushed; |
| 1594 | assert(zfp.produced >= previous_zfp_update.produced); |
| 1595 | assert(prefs->nbWorkers >= 1); |
| 1596 | |
| 1597 | /* test if compression is blocked |
| 1598 | * either because output is slow and all buffers are full |
| 1599 | * or because input is slow and no job can start while waiting for at least one buffer to be filled. |
| 1600 | * note : exclude starting part, since currentJobID > 1 */ |
| 1601 | if ( (zfp.consumed == previous_zfp_update.consumed) /* no data compressed : no data available, or no more buffer to compress to, OR compression is really slow (compression of a single block is slower than update rate)*/ |
| 1602 | && (zfp.nbActiveWorkers == 0) /* confirmed : no compression ongoing */ |
| 1603 | ) { |
| 1604 | DISPLAYLEVEL(6, "all buffers full : compression stopped => slow down \n") |
| 1605 | speedChange = slower; |
| 1606 | } |
| 1607 | |
| 1608 | previous_zfp_update = zfp; |
| 1609 | |
| 1610 | if ( (newlyProduced > (newlyFlushed * 9 / 8)) /* compression produces more data than output can flush (though production can be spiky, due to work unit : (N==4)*block sizes) */ |
| 1611 | && (flushWaiting == 0) /* flush speed was never slowed by lack of production, so it's operating at max capacity */ |
| 1612 | ) { |
| 1613 | DISPLAYLEVEL(6, "compression faster than flush (%llu > %llu), and flushed was never slowed down by lack of production => slow down \n", newlyProduced, newlyFlushed); |
| 1614 | speedChange = slower; |
| 1615 | } |
| 1616 | flushWaiting = 0; |
| 1617 | } |
| 1618 | |
| 1619 | /* course correct only if there is at least one new job completed */ |
| 1620 | if (zfp.currentJobID > lastJobID) { |
| 1621 | DISPLAYLEVEL(6, "compression level adaptation check \n") |
| 1622 | |
| 1623 | /* check input speed */ |
| 1624 | if (zfp.currentJobID > (unsigned)(prefs->nbWorkers+1)) { /* warm up period, to fill all workers */ |
| 1625 | if (inputBlocked <= 0) { |
| 1626 | DISPLAYLEVEL(6, "input is never blocked => input is slower than ingestion \n"); |
| 1627 | speedChange = slower; |
| 1628 | } else if (speedChange == noChange) { |
| 1629 | unsigned long long newlyIngested = zfp.ingested - previous_zfp_correction.ingested; |
| 1630 | unsigned long long newlyConsumed = zfp.consumed - previous_zfp_correction.consumed; |
| 1631 | unsigned long long newlyProduced = zfp.produced - previous_zfp_correction.produced; |
| 1632 | unsigned long long newlyFlushed = zfp.flushed - previous_zfp_correction.flushed; |
| 1633 | previous_zfp_correction = zfp; |
| 1634 | assert(inputPresented > 0); |
| 1635 | DISPLAYLEVEL(6, "input blocked %u/%u(%.2f) - ingested:%u vs %u:consumed - flushed:%u vs %u:produced \n", |
| 1636 | inputBlocked, inputPresented, (double)inputBlocked/inputPresented*100, |
| 1637 | (unsigned)newlyIngested, (unsigned)newlyConsumed, |
| 1638 | (unsigned)newlyFlushed, (unsigned)newlyProduced); |
| 1639 | if ( (inputBlocked > inputPresented / 8) /* input is waiting often, because input buffers is full : compression or output too slow */ |
| 1640 | && (newlyFlushed * 33 / 32 > newlyProduced) /* flush everything that is produced */ |
| 1641 | && (newlyIngested * 33 / 32 > newlyConsumed) /* input speed as fast or faster than compression speed */ |
| 1642 | ) { |
| 1643 | DISPLAYLEVEL(6, "recommend faster as in(%llu) >= (%llu)comp(%llu) <= out(%llu) \n", |
| 1644 | newlyIngested, newlyConsumed, newlyProduced, newlyFlushed); |
| 1645 | speedChange = faster; |
| 1646 | } |
| 1647 | } |
| 1648 | inputBlocked = 0; |
| 1649 | inputPresented = 0; |
| 1650 | } |
| 1651 | |
| 1652 | if (speedChange == slower) { |
| 1653 | DISPLAYLEVEL(6, "slower speed , higher compression \n") |
| 1654 | compressionLevel ++; |
| 1655 | if (compressionLevel > ZSTD_maxCLevel()) compressionLevel = ZSTD_maxCLevel(); |
| 1656 | if (compressionLevel > prefs->maxAdaptLevel) compressionLevel = prefs->maxAdaptLevel; |
| 1657 | compressionLevel += (compressionLevel == 0); /* skip 0 */ |
| 1658 | ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_compressionLevel, compressionLevel); |
| 1659 | } |
| 1660 | if (speedChange == faster) { |
| 1661 | DISPLAYLEVEL(6, "faster speed , lighter compression \n") |
| 1662 | compressionLevel --; |
| 1663 | if (compressionLevel < prefs->minAdaptLevel) compressionLevel = prefs->minAdaptLevel; |
| 1664 | compressionLevel -= (compressionLevel == 0); /* skip 0 */ |
| 1665 | ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_compressionLevel, compressionLevel); |
| 1666 | } |
| 1667 | speedChange = noChange; |
| 1668 | |
| 1669 | lastJobID = zfp.currentJobID; |
| 1670 | } /* if (zfp.currentJobID > lastJobID) */ |
| 1671 | } /* if (prefs->adaptiveMode && UTIL_clockSpanMicro(lastAdaptTime) > adaptEveryMicro) */ |
| 1672 | |
| 1673 | /* display notification */ |
| 1674 | if (SHOULD_DISPLAY_PROGRESS() && READY_FOR_UPDATE()) { |
Yann Collet | 70f81d6 | 2018-01-19 10:01:40 -0800 | [diff] [blame] | 1675 | ZSTD_frameProgression const zfp = ZSTD_getFrameProgression(ress.cctx); |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 1676 | double const cShare = (double)zfp.produced / (double)(zfp.consumed + !zfp.consumed/*avoid div0*/) * 100; |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 1677 | UTIL_HumanReadableSize_t const buffered_hrs = UTIL_makeHumanReadableSize(zfp.ingested - zfp.consumed); |
| 1678 | UTIL_HumanReadableSize_t const consumed_hrs = UTIL_makeHumanReadableSize(zfp.consumed); |
| 1679 | UTIL_HumanReadableSize_t const produced_hrs = UTIL_makeHumanReadableSize(zfp.produced); |
Yann Collet | 2dd7603 | 2018-08-09 15:51:30 -0700 | [diff] [blame] | 1680 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1681 | DELAY_NEXT_UPDATE(); |
| 1682 | |
Yann Collet | ca02ebe | 2018-09-19 15:09:45 -0700 | [diff] [blame] | 1683 | /* display progress notifications */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1684 | DISPLAY_PROGRESS("\r%79s\r", ""); /* Clear out the current displayed line */ |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 1685 | if (g_display_prefs.displayLevel >= 3) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1686 | /* Verbose progress update */ |
| 1687 | DISPLAY_PROGRESS( |
| 1688 | "(L%i) Buffered:%5.*f%s - Consumed:%5.*f%s - Compressed:%5.*f%s => %.2f%% ", |
| 1689 | compressionLevel, |
| 1690 | buffered_hrs.precision, buffered_hrs.value, buffered_hrs.suffix, |
| 1691 | consumed_hrs.precision, consumed_hrs.value, consumed_hrs.suffix, |
| 1692 | produced_hrs.precision, produced_hrs.value, produced_hrs.suffix, |
| 1693 | cShare ); |
| 1694 | } else { |
sen | 6030cdf | 2021-05-06 14:50:28 -0400 | [diff] [blame] | 1695 | /* Require level 2 or forcibly displayed progress counter for summarized updates */ |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 1696 | if (fCtx->nbFilesTotal > 1) { |
senhuang42 | cad6bf9 | 2020-09-15 13:01:46 -0400 | [diff] [blame] | 1697 | size_t srcFileNameSize = strlen(srcFileName); |
| 1698 | /* Ensure that the string we print is roughly the same size each time */ |
senhuang42 | ab0d332 | 2020-09-15 15:53:32 -0400 | [diff] [blame] | 1699 | if (srcFileNameSize > 18) { |
| 1700 | const char* truncatedSrcFileName = srcFileName + srcFileNameSize - 15; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1701 | DISPLAY_PROGRESS("Compress: %u/%u files. Current: ...%s ", |
sen | 6030cdf | 2021-05-06 14:50:28 -0400 | [diff] [blame] | 1702 | fCtx->currFileIdx+1, fCtx->nbFilesTotal, truncatedSrcFileName); |
senhuang42 | cad6bf9 | 2020-09-15 13:01:46 -0400 | [diff] [blame] | 1703 | } else { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1704 | DISPLAY_PROGRESS("Compress: %u/%u files. Current: %*s ", |
sen | 6030cdf | 2021-05-06 14:50:28 -0400 | [diff] [blame] | 1705 | fCtx->currFileIdx+1, fCtx->nbFilesTotal, (int)(18-srcFileNameSize), srcFileName); |
senhuang42 | cad6bf9 | 2020-09-15 13:01:46 -0400 | [diff] [blame] | 1706 | } |
senhuang42 | b6abbc3 | 2020-08-26 11:35:07 -0400 | [diff] [blame] | 1707 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1708 | DISPLAY_PROGRESS("Read:%6.*f%4s ", consumed_hrs.precision, consumed_hrs.value, consumed_hrs.suffix); |
Yann Collet | ca02ebe | 2018-09-19 15:09:45 -0700 | [diff] [blame] | 1709 | if (fileSize != UTIL_FILESIZE_UNKNOWN) |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1710 | DISPLAY_PROGRESS("/%6.*f%4s", file_hrs.precision, file_hrs.value, file_hrs.suffix); |
| 1711 | DISPLAY_PROGRESS(" ==> %2.f%%", cShare); |
Yann Collet | 2dd7603 | 2018-08-09 15:51:30 -0700 | [diff] [blame] | 1712 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1713 | } /* if (SHOULD_DISPLAY_PROGRESS() && READY_FOR_UPDATE()) */ |
Yann Collet | ca02ebe | 2018-09-19 15:09:45 -0700 | [diff] [blame] | 1714 | } /* while ((inBuff.pos != inBuff.size) */ |
Nick Terrell | f48d34e | 2017-12-14 13:00:20 -0800 | [diff] [blame] | 1715 | } while (directive != ZSTD_e_end); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1716 | |
Nick Terrell | 4e706d7 | 2018-07-17 14:57:27 -0700 | [diff] [blame] | 1717 | if (fileSize != UTIL_FILESIZE_UNKNOWN && *readsize != fileSize) { |
| 1718 | EXM_THROW(27, "Read error : Incomplete read : %llu / %llu B", |
| 1719 | (unsigned long long)*readsize, (unsigned long long)fileSize); |
| 1720 | } |
| 1721 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1722 | AIO_WritePool_releaseIoJob(writeJob); |
| 1723 | AIO_WritePool_sparseWriteEnd(ressPtr->writeCtx); |
| 1724 | |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1725 | return compressedfilesize; |
| 1726 | } |
| 1727 | |
| 1728 | /*! FIO_compressFilename_internal() : |
| 1729 | * same as FIO_compressFilename_extRess(), with `ress.desFile` already opened. |
| 1730 | * @return : 0 : compression completed correctly, |
| 1731 | * 1 : missing or pb opening srcFileName |
| 1732 | */ |
| 1733 | static int |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 1734 | FIO_compressFilename_internal(FIO_ctx_t* const fCtx, |
| 1735 | FIO_prefs_t* const prefs, |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 1736 | cRess_t ress, |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1737 | const char* dstFileName, const char* srcFileName, |
| 1738 | int compressionLevel) |
| 1739 | { |
Ephraim Park | a38601f | 2019-06-04 09:25:16 -0700 | [diff] [blame] | 1740 | UTIL_time_t const timeStart = UTIL_getTime(); |
Ephraim Park | 5fe9742 | 2019-06-04 09:04:35 -0700 | [diff] [blame] | 1741 | clock_t const cpuStart = clock(); |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1742 | U64 readsize = 0; |
| 1743 | U64 compressedfilesize = 0; |
Bimba Shrestha | 9388dac | 2020-03-09 15:40:18 -0500 | [diff] [blame] | 1744 | U64 const fileSize = UTIL_getFileSize(srcFileName); |
Yann Collet | 9fb4a42 | 2021-03-20 17:29:41 -0700 | [diff] [blame] | 1745 | DISPLAYLEVEL(5, "%s: %llu bytes \n", srcFileName, (unsigned long long)fileSize); |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1746 | |
| 1747 | /* compression format selection */ |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 1748 | switch (prefs->compressionType) { |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1749 | default: |
| 1750 | case FIO_zstdCompression: |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 1751 | compressedfilesize = FIO_compressZstdFrame(fCtx, prefs, &ress, srcFileName, fileSize, compressionLevel, &readsize); |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1752 | break; |
| 1753 | |
| 1754 | case FIO_gzipCompression: |
| 1755 | #ifdef ZSTD_GZCOMPRESS |
| 1756 | compressedfilesize = FIO_compressGzFrame(&ress, srcFileName, fileSize, compressionLevel, &readsize); |
| 1757 | #else |
| 1758 | (void)compressionLevel; |
| 1759 | EXM_THROW(20, "zstd: %s: file cannot be compressed as gzip (zstd compiled without ZSTD_GZCOMPRESS) -- ignored \n", |
| 1760 | srcFileName); |
| 1761 | #endif |
| 1762 | break; |
| 1763 | |
| 1764 | case FIO_xzCompression: |
| 1765 | case FIO_lzmaCompression: |
| 1766 | #ifdef ZSTD_LZMACOMPRESS |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 1767 | compressedfilesize = FIO_compressLzmaFrame(&ress, srcFileName, fileSize, compressionLevel, &readsize, prefs->compressionType==FIO_lzmaCompression); |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1768 | #else |
| 1769 | (void)compressionLevel; |
| 1770 | EXM_THROW(20, "zstd: %s: file cannot be compressed as xz/lzma (zstd compiled without ZSTD_LZMACOMPRESS) -- ignored \n", |
| 1771 | srcFileName); |
| 1772 | #endif |
| 1773 | break; |
| 1774 | |
| 1775 | case FIO_lz4Compression: |
| 1776 | #ifdef ZSTD_LZ4COMPRESS |
Yann Collet | 07e0478 | 2019-01-25 14:42:44 -0800 | [diff] [blame] | 1777 | compressedfilesize = FIO_compressLz4Frame(&ress, srcFileName, fileSize, compressionLevel, prefs->checksumFlag, &readsize); |
Yann Collet | 90eca31 | 2018-02-02 14:24:56 -0800 | [diff] [blame] | 1778 | #else |
| 1779 | (void)compressionLevel; |
| 1780 | EXM_THROW(20, "zstd: %s: file cannot be compressed as lz4 (zstd compiled without ZSTD_LZ4COMPRESS) -- ignored \n", |
| 1781 | srcFileName); |
| 1782 | #endif |
| 1783 | break; |
| 1784 | } |
| 1785 | |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1786 | /* Status */ |
senhuang42 | 28a9dc7 | 2020-09-03 20:23:30 -0400 | [diff] [blame] | 1787 | fCtx->totalBytesInput += (size_t)readsize; |
| 1788 | fCtx->totalBytesOutput += (size_t)compressedfilesize; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1789 | DISPLAY_PROGRESS("\r%79s\r", ""); |
| 1790 | if (FIO_shouldDisplayFileSummary(fCtx)) { |
W. Felix Handte | bc46b6e | 2021-06-09 16:04:10 -0400 | [diff] [blame] | 1791 | UTIL_HumanReadableSize_t hr_isize = UTIL_makeHumanReadableSize((U64) readsize); |
| 1792 | UTIL_HumanReadableSize_t hr_osize = UTIL_makeHumanReadableSize((U64) compressedfilesize); |
senhuang42 | 1ebe360 | 2020-10-07 13:42:34 -0400 | [diff] [blame] | 1793 | if (readsize == 0) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1794 | DISPLAY_SUMMARY("%-20s : (%6.*f%s => %6.*f%s, %s) \n", |
senhuang42 | 1ebe360 | 2020-10-07 13:42:34 -0400 | [diff] [blame] | 1795 | srcFileName, |
W. Felix Handte | bc46b6e | 2021-06-09 16:04:10 -0400 | [diff] [blame] | 1796 | hr_isize.precision, hr_isize.value, hr_isize.suffix, |
| 1797 | hr_osize.precision, hr_osize.value, hr_osize.suffix, |
senhuang42 | 1ebe360 | 2020-10-07 13:42:34 -0400 | [diff] [blame] | 1798 | dstFileName); |
| 1799 | } else { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1800 | DISPLAY_SUMMARY("%-20s :%6.2f%% (%6.*f%s => %6.*f%s, %s) \n", |
senhuang42 | 1ebe360 | 2020-10-07 13:42:34 -0400 | [diff] [blame] | 1801 | srcFileName, |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 1802 | (double)compressedfilesize / (double)readsize * 100, |
W. Felix Handte | bbb81c8 | 2021-06-09 13:05:44 -0400 | [diff] [blame] | 1803 | hr_isize.precision, hr_isize.value, hr_isize.suffix, |
| 1804 | hr_osize.precision, hr_osize.value, hr_osize.suffix, |
senhuang42 | 1ebe360 | 2020-10-07 13:42:34 -0400 | [diff] [blame] | 1805 | dstFileName); |
senhuang42 | da38891 | 2020-08-25 16:46:47 -0400 | [diff] [blame] | 1806 | } |
Bimba Shrestha | d0412f3 | 2020-04-03 12:10:02 -0700 | [diff] [blame] | 1807 | } |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1808 | |
Ephraim Park | 5fe9742 | 2019-06-04 09:04:35 -0700 | [diff] [blame] | 1809 | /* Elapsed Time and CPU Load */ |
| 1810 | { clock_t const cpuEnd = clock(); |
| 1811 | double const cpuLoad_s = (double)(cpuEnd - cpuStart) / CLOCKS_PER_SEC; |
| 1812 | U64 const timeLength_ns = UTIL_clockSpanNano(timeStart); |
| 1813 | double const timeLength_s = (double)timeLength_ns / 1000000000; |
| 1814 | double const cpuLoad_pct = (cpuLoad_s / timeLength_s) * 100; |
Ephraim Park | e498bb6 | 2019-06-04 09:42:18 -0700 | [diff] [blame] | 1815 | DISPLAYLEVEL(4, "%-20s : Completed in %.2f sec (cpu load : %.0f%%)\n", |
Ephraim Park | 5fe9742 | 2019-06-04 09:04:35 -0700 | [diff] [blame] | 1816 | srcFileName, timeLength_s, cpuLoad_pct); |
| 1817 | } |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 1818 | return 0; |
| 1819 | } |
| 1820 | |
| 1821 | |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1822 | /*! FIO_compressFilename_dstFile() : |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1823 | * open dstFileName, or pass-through if ress.file != NULL, |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1824 | * then start compression with FIO_compressFilename_internal(). |
| 1825 | * Manages source removal (--rm) and file permissions transfer. |
| 1826 | * note : ress.srcFile must be != NULL, |
| 1827 | * so reach this function through FIO_compressFilename_srcFile(). |
| 1828 | * @return : 0 : compression completed correctly, |
| 1829 | * 1 : pb |
| 1830 | */ |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 1831 | static int FIO_compressFilename_dstFile(FIO_ctx_t* const fCtx, |
| 1832 | FIO_prefs_t* const prefs, |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 1833 | cRess_t ress, |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1834 | const char* dstFileName, |
| 1835 | const char* srcFileName, |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1836 | const stat_t* srcFileStat, |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1837 | int compressionLevel) |
| 1838 | { |
| 1839 | int closeDstFile = 0; |
| 1840 | int result; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1841 | int transferStat = 0; |
| 1842 | FILE *dstFile; |
| 1843 | int dstFd = -1; |
| 1844 | |
| 1845 | assert(AIO_ReadPool_getFile(ress.readCtx) != NULL); |
| 1846 | if (AIO_WritePool_getFile(ress.writeCtx) == NULL) { |
| 1847 | int dstFileInitialPermissions = DEFAULT_FILE_PERMISSIONS; |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 1848 | if ( strcmp (srcFileName, stdinmark) |
Mike Gilbert | 57a86d9 | 2022-01-13 16:47:18 -0500 | [diff] [blame] | 1849 | && strcmp (dstFileName, stdoutmark) |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1850 | && UTIL_isRegularFileStat(srcFileStat) ) { |
| 1851 | transferStat = 1; |
| 1852 | dstFileInitialPermissions = TEMPORARY_FILE_PERMISSIONS; |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 1853 | } |
| 1854 | |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1855 | closeDstFile = 1; |
Yann Collet | 7aaac3f | 2019-11-25 10:35:36 -0800 | [diff] [blame] | 1856 | DISPLAYLEVEL(6, "FIO_compressFilename_dstFile: opening dst: %s \n", dstFileName); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1857 | dstFile = FIO_openDstFile(fCtx, prefs, srcFileName, dstFileName, dstFileInitialPermissions); |
| 1858 | if (dstFile==NULL) return 1; /* could not open dstFileName */ |
| 1859 | dstFd = fileno(dstFile); |
| 1860 | AIO_WritePool_setFile(ress.writeCtx, dstFile); |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1861 | /* Must only be added after FIO_openDstFile() succeeds. |
| 1862 | * Otherwise we may delete the destination file if it already exists, |
| 1863 | * and the user presses Ctrl-C when asked if they wish to overwrite. |
| 1864 | */ |
| 1865 | addHandler(dstFileName); |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1866 | } |
| 1867 | |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 1868 | result = FIO_compressFilename_internal(fCtx, prefs, ress, dstFileName, srcFileName, compressionLevel); |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1869 | |
| 1870 | if (closeDstFile) { |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1871 | clearHandler(); |
| 1872 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1873 | if (transferStat) { |
| 1874 | UTIL_setFDStat(dstFd, dstFileName, srcFileStat); |
| 1875 | } |
| 1876 | |
Yann Collet | 7aaac3f | 2019-11-25 10:35:36 -0800 | [diff] [blame] | 1877 | DISPLAYLEVEL(6, "FIO_compressFilename_dstFile: closing dst: %s \n", dstFileName); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1878 | if (AIO_WritePool_closeFile(ress.writeCtx)) { /* error closing file */ |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1879 | DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno)); |
| 1880 | result=1; |
| 1881 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1882 | |
| 1883 | if (transferStat) { |
| 1884 | UTIL_utime(dstFileName, srcFileStat); |
W. Felix Handte | 9cd6c1f | 2021-08-04 14:49:56 -0400 | [diff] [blame] | 1885 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1886 | |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1887 | if ( (result != 0) /* operation failure */ |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1888 | && strcmp(dstFileName, stdoutmark) /* special case : don't remove() stdout */ |
| 1889 | ) { |
W. Felix Handte | b02cdf6 | 2020-08-10 15:39:14 -0400 | [diff] [blame] | 1890 | FIO_removeFile(dstFileName); /* remove compression artefact; note don't do anything special if remove() fails */ |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1891 | } |
| 1892 | } |
Ephraim Park | 5fe9742 | 2019-06-04 09:04:35 -0700 | [diff] [blame] | 1893 | |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1894 | return result; |
| 1895 | } |
| 1896 | |
Shashank Tavildar | 9ab6a74 | 2019-10-29 12:27:54 -0700 | [diff] [blame] | 1897 | /* List used to compare file extensions (used with --exclude-compressed flag) |
| 1898 | * Different from the suffixList and should only apply to ZSTD compress operationResult |
| 1899 | */ |
Shashank Tavildar | 0f2bff2 | 2019-10-28 18:21:47 -0700 | [diff] [blame] | 1900 | static const char *compressedFileExtensions[] = { |
| 1901 | ZSTD_EXTENSION, |
| 1902 | TZSTD_EXTENSION, |
| 1903 | GZ_EXTENSION, |
| 1904 | TGZ_EXTENSION, |
| 1905 | LZMA_EXTENSION, |
| 1906 | XZ_EXTENSION, |
| 1907 | TXZ_EXTENSION, |
| 1908 | LZ4_EXTENSION, |
| 1909 | TLZ4_EXTENSION, |
| 1910 | NULL |
| 1911 | }; |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1912 | |
Yann Collet | b71adf4 | 2016-07-02 01:05:31 +0200 | [diff] [blame] | 1913 | /*! FIO_compressFilename_srcFile() : |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 1914 | * @return : 0 : compression completed correctly, |
| 1915 | * 1 : missing or pb opening srcFileName |
| 1916 | */ |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1917 | static int |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 1918 | FIO_compressFilename_srcFile(FIO_ctx_t* const fCtx, |
| 1919 | FIO_prefs_t* const prefs, |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 1920 | cRess_t ress, |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1921 | const char* dstFileName, |
| 1922 | const char* srcFileName, |
| 1923 | int compressionLevel) |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 1924 | { |
| 1925 | int result; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1926 | FILE* srcFile; |
| 1927 | stat_t srcFileStat; |
| 1928 | U64 fileSize = UTIL_FILESIZE_UNKNOWN; |
Yann Collet | 7aaac3f | 2019-11-25 10:35:36 -0800 | [diff] [blame] | 1929 | DISPLAYLEVEL(6, "FIO_compressFilename_srcFile: %s \n", srcFileName); |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 1930 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1931 | if (strcmp(srcFileName, stdinmark)) { |
| 1932 | if (UTIL_stat(srcFileName, &srcFileStat)) { |
| 1933 | /* failure to stat at all is handled during opening */ |
Przemyslaw Skibinski | 64fa2db | 2017-01-25 13:02:33 +0100 | [diff] [blame] | 1934 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1935 | /* ensure src is not a directory */ |
| 1936 | if (UTIL_isDirectoryStat(&srcFileStat)) { |
| 1937 | DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName); |
| 1938 | return 1; |
| 1939 | } |
| 1940 | |
| 1941 | /* ensure src is not the same as dict (if present) */ |
| 1942 | if (ress.dictFileName != NULL && UTIL_isSameFileStat(srcFileName, ress.dictFileName, &srcFileStat, &ress.dictFileStat)) { |
| 1943 | DISPLAYLEVEL(1, "zstd: cannot use %s as an input file and dictionary \n", srcFileName); |
| 1944 | return 1; |
| 1945 | } |
| 1946 | } |
shakeelrao | 1290933 | 2019-03-23 21:53:13 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Shashank Tavildar | 0f2bff2 | 2019-10-28 18:21:47 -0700 | [diff] [blame] | 1949 | /* Check if "srcFile" is compressed. Only done if --exclude-compressed flag is used |
| 1950 | * YES => ZSTD will skip compression of the file and will return 0. |
| 1951 | * NO => ZSTD will resume with compress operation. |
| 1952 | */ |
| 1953 | if (prefs->excludeCompressedFiles == 1 && UTIL_isCompressedFile(srcFileName, compressedFileExtensions)) { |
| 1954 | DISPLAYLEVEL(4, "File is already compressed : %s \n", srcFileName); |
| 1955 | return 0; |
| 1956 | } |
| 1957 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1958 | srcFile = FIO_openSrcFile(prefs, srcFileName, &srcFileStat); |
| 1959 | if (srcFile == NULL) return 1; /* srcFile could not be opened */ |
Shashank Tavildar | 02433e0 | 2019-10-28 14:54:54 -0700 | [diff] [blame] | 1960 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1961 | /* Don't use AsyncIO for small files */ |
| 1962 | if (strcmp(srcFileName, stdinmark)) /* Stdin doesn't have stats */ |
| 1963 | fileSize = UTIL_getFileSizeStat(&srcFileStat); |
| 1964 | if(fileSize != UTIL_FILESIZE_UNKNOWN && fileSize < ZSTD_BLOCKSIZE_MAX * 3) { |
| 1965 | AIO_ReadPool_setAsync(ress.readCtx, 0); |
| 1966 | AIO_WritePool_setAsync(ress.writeCtx, 0); |
| 1967 | } else { |
| 1968 | AIO_ReadPool_setAsync(ress.readCtx, 1); |
| 1969 | AIO_WritePool_setAsync(ress.writeCtx, 1); |
| 1970 | } |
Yonatan Komornik | 79bdb8c | 2023-02-02 15:19:22 -0800 | [diff] [blame] | 1971 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1972 | AIO_ReadPool_setFile(ress.readCtx, srcFile); |
| 1973 | result = FIO_compressFilename_dstFile( |
| 1974 | fCtx, prefs, ress, |
| 1975 | dstFileName, srcFileName, |
| 1976 | &srcFileStat, compressionLevel); |
| 1977 | AIO_ReadPool_closeFile(ress.readCtx); |
| 1978 | |
| 1979 | if ( prefs->removeSrcFile /* --rm */ |
| 1980 | && result == 0 /* success */ |
| 1981 | && strcmp(srcFileName, stdinmark) /* exception : don't erase stdin */ |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 1982 | ) { |
Nick Terrell | a6052af | 2017-11-17 16:38:56 -0800 | [diff] [blame] | 1983 | /* We must clear the handler, since after this point calling it would |
| 1984 | * delete both the source and destination files. |
| 1985 | */ |
| 1986 | clearHandler(); |
W. Felix Handte | b02cdf6 | 2020-08-10 15:39:14 -0400 | [diff] [blame] | 1987 | if (FIO_removeFile(srcFileName)) |
Yann Collet | dccd6b6 | 2017-02-27 15:57:50 -0800 | [diff] [blame] | 1988 | EXM_THROW(1, "zstd: %s: %s", srcFileName, strerror(errno)); |
| 1989 | } |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 1990 | return result; |
| 1991 | } |
| 1992 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1993 | static const char* |
| 1994 | checked_index(const char* options[], size_t length, size_t index) { |
Kevin Svetlitski | 0665d4c | 2021-11-05 12:01:20 -0700 | [diff] [blame] | 1995 | assert(index < length); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 1996 | /* Necessary to avoid warnings since -O3 will omit the above `assert` */ |
Kevin Svetlitski | 7fbd126 | 2021-11-11 14:37:02 -0800 | [diff] [blame] | 1997 | (void) length; |
Kevin Svetlitski | 0665d4c | 2021-11-05 12:01:20 -0700 | [diff] [blame] | 1998 | return options[index]; |
| 1999 | } |
| 2000 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2001 | #define INDEX(options, index) checked_index((options), sizeof(options) / sizeof(char*), (size_t)(index)) |
Kevin Svetlitski | 0665d4c | 2021-11-05 12:01:20 -0700 | [diff] [blame] | 2002 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2003 | void FIO_displayCompressionParameters(const FIO_prefs_t* prefs) |
| 2004 | { |
Kevin Svetlitski | 0665d4c | 2021-11-05 12:01:20 -0700 | [diff] [blame] | 2005 | static const char* formatOptions[5] = {ZSTD_EXTENSION, GZ_EXTENSION, XZ_EXTENSION, |
| 2006 | LZMA_EXTENSION, LZ4_EXTENSION}; |
| 2007 | static const char* sparseOptions[3] = {" --no-sparse", "", " --sparse"}; |
| 2008 | static const char* checkSumOptions[3] = {" --no-check", "", " --check"}; |
| 2009 | static const char* rowMatchFinderOptions[3] = {"", " --no-row-match-finder", " --row-match-finder"}; |
| 2010 | static const char* compressLiteralsOptions[3] = {"", " --compress-literals", " --no-compress-literals"}; |
| 2011 | |
| 2012 | assert(g_display_prefs.displayLevel >= 4); |
| 2013 | |
| 2014 | DISPLAY("--format=%s", formatOptions[prefs->compressionType]); |
| 2015 | DISPLAY("%s", INDEX(sparseOptions, prefs->sparseFileSupport)); |
| 2016 | DISPLAY("%s", prefs->dictIDFlag ? "" : " --no-dictID"); |
| 2017 | DISPLAY("%s", INDEX(checkSumOptions, prefs->checksumFlag)); |
| 2018 | DISPLAY(" --block-size=%d", prefs->blockSize); |
| 2019 | if (prefs->adaptiveMode) |
| 2020 | DISPLAY(" --adapt=min=%d,max=%d", prefs->minAdaptLevel, prefs->maxAdaptLevel); |
| 2021 | DISPLAY("%s", INDEX(rowMatchFinderOptions, prefs->useRowMatchFinder)); |
| 2022 | DISPLAY("%s", prefs->rsyncable ? " --rsyncable" : ""); |
| 2023 | if (prefs->streamSrcSize) |
Kevin Svetlitski | 375e3aa | 2021-11-11 13:17:30 -0800 | [diff] [blame] | 2024 | DISPLAY(" --stream-size=%u", (unsigned) prefs->streamSrcSize); |
Kevin Svetlitski | 0665d4c | 2021-11-05 12:01:20 -0700 | [diff] [blame] | 2025 | if (prefs->srcSizeHint) |
| 2026 | DISPLAY(" --size-hint=%d", prefs->srcSizeHint); |
| 2027 | if (prefs->targetCBlockSize) |
Kevin Svetlitski | 375e3aa | 2021-11-11 13:17:30 -0800 | [diff] [blame] | 2028 | DISPLAY(" --target-compressed-block-size=%u", (unsigned) prefs->targetCBlockSize); |
Kevin Svetlitski | 0665d4c | 2021-11-05 12:01:20 -0700 | [diff] [blame] | 2029 | DISPLAY("%s", INDEX(compressLiteralsOptions, prefs->literalCompressionMode)); |
| 2030 | DISPLAY(" --memory=%u", prefs->memLimit ? prefs->memLimit : 128 MB); |
| 2031 | DISPLAY(" --threads=%d", prefs->nbWorkers); |
| 2032 | DISPLAY("%s", prefs->excludeCompressedFiles ? " --exclude-compressed" : ""); |
| 2033 | DISPLAY(" --%scontent-size", prefs->contentSize ? "" : "no-"); |
| 2034 | DISPLAY("\n"); |
| 2035 | } |
| 2036 | |
| 2037 | #undef INDEX |
| 2038 | |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 2039 | int FIO_compressFilename(FIO_ctx_t* const fCtx, FIO_prefs_t* const prefs, const char* dstFileName, |
Sen Huang | 7f98b46 | 2019-09-05 16:03:35 -0700 | [diff] [blame] | 2040 | const char* srcFileName, const char* dictFileName, |
Yann Collet | 458a1a1 | 2020-04-13 10:13:29 -0700 | [diff] [blame] | 2041 | int compressionLevel, ZSTD_compressionParameters comprParams) |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 2042 | { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2043 | cRess_t ress = FIO_createCResources(prefs, dictFileName, UTIL_getFileSize(srcFileName), compressionLevel, comprParams); |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 2044 | int const result = FIO_compressFilename_srcFile(fCtx, prefs, ress, dstFileName, srcFileName, compressionLevel); |
Yann Collet | 9d90922 | 2015-12-17 14:09:55 +0100 | [diff] [blame] | 2045 | |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 2046 | #define DISPLAY_LEVEL_DEFAULT 2 |
Yann Collet | b71adf4 | 2016-07-02 01:05:31 +0200 | [diff] [blame] | 2047 | |
senhuang42 | 043b934 | 2020-10-14 20:19:46 -0400 | [diff] [blame] | 2048 | FIO_freeCResources(&ress); |
Yann Collet | b71adf4 | 2016-07-02 01:05:31 +0200 | [diff] [blame] | 2049 | return result; |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 2050 | } |
| 2051 | |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2052 | /* FIO_determineCompressedName() : |
| 2053 | * create a destination filename for compressed srcFileName. |
| 2054 | * @return a pointer to it. |
| 2055 | * This function never returns an error (it may abort() in case of pb) |
| 2056 | */ |
| 2057 | static const char* |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2058 | FIO_determineCompressedName(const char* srcFileName, const char* outDirName, const char* suffix) |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2059 | { |
| 2060 | static size_t dfnbCapacity = 0; |
| 2061 | static char* dstFileNameBuffer = NULL; /* using static allocation : this function cannot be multi-threaded */ |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2062 | char* outDirFilename = NULL; |
| 2063 | size_t sfnSize = strlen(srcFileName); |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2064 | size_t const srcSuffixLen = strlen(suffix); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2065 | |
| 2066 | if(!strcmp(srcFileName, stdinmark)) { |
| 2067 | return stdoutmark; |
| 2068 | } |
| 2069 | |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2070 | if (outDirName) { |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2071 | outDirFilename = FIO_createFilename_fromOutDir(srcFileName, outDirName, srcSuffixLen); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2072 | sfnSize = strlen(outDirFilename); |
| 2073 | assert(outDirFilename != NULL); |
| 2074 | } |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2075 | |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2076 | if (dfnbCapacity <= sfnSize+srcSuffixLen+1) { |
Yann Collet | 433059b | 2018-10-10 17:06:25 -0700 | [diff] [blame] | 2077 | /* resize buffer for dstName */ |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2078 | free(dstFileNameBuffer); |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2079 | dfnbCapacity = sfnSize + srcSuffixLen + 30; |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2080 | dstFileNameBuffer = (char*)malloc(dfnbCapacity); |
| 2081 | if (!dstFileNameBuffer) { |
| 2082 | EXM_THROW(30, "zstd: %s", strerror(errno)); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2083 | } |
| 2084 | } |
Yann Collet | 3ca6261 | 2018-10-02 15:59:11 -0700 | [diff] [blame] | 2085 | assert(dstFileNameBuffer != NULL); |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2086 | |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2087 | if (outDirFilename) { |
| 2088 | memcpy(dstFileNameBuffer, outDirFilename, sfnSize); |
| 2089 | free(outDirFilename); |
| 2090 | } else { |
| 2091 | memcpy(dstFileNameBuffer, srcFileName, sfnSize); |
| 2092 | } |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2093 | memcpy(dstFileNameBuffer+sfnSize, suffix, srcSuffixLen+1 /* Include terminating null */); |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2094 | return dstFileNameBuffer; |
| 2095 | } |
| 2096 | |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 2097 | static unsigned long long FIO_getLargestFileSize(const char** inFileNames, unsigned nbFiles) |
Bimba Shrestha | f25a6e9 | 2020-01-10 14:25:24 -0800 | [diff] [blame] | 2098 | { |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 2099 | size_t i; |
| 2100 | unsigned long long fileSize, maxFileSize = 0; |
Bimba Shrestha | f25a6e9 | 2020-01-10 14:25:24 -0800 | [diff] [blame] | 2101 | for (i = 0; i < nbFiles; i++) { |
Bimba Shrestha | 5b0a452 | 2020-04-17 15:58:53 -0500 | [diff] [blame] | 2102 | fileSize = UTIL_getFileSize(inFileNames[i]); |
Bimba Shrestha | f25a6e9 | 2020-01-10 14:25:24 -0800 | [diff] [blame] | 2103 | maxFileSize = fileSize > maxFileSize ? fileSize : maxFileSize; |
| 2104 | } |
| 2105 | return maxFileSize; |
| 2106 | } |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2107 | |
| 2108 | /* FIO_compressMultipleFilenames() : |
| 2109 | * compress nbFiles files |
Sen Huang | 7f98b46 | 2019-09-05 16:03:35 -0700 | [diff] [blame] | 2110 | * into either one destination (outFileName), |
| 2111 | * or into one file each (outFileName == NULL, but suffix != NULL), |
| 2112 | * or into a destination folder (specified with -O) |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2113 | */ |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 2114 | int FIO_compressMultipleFilenames(FIO_ctx_t* const fCtx, |
| 2115 | FIO_prefs_t* const prefs, |
senhuang42 | b6abbc3 | 2020-08-26 11:35:07 -0400 | [diff] [blame] | 2116 | const char** inFileNamesTable, |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 2117 | const char* outMirroredRootDirName, |
Yann Collet | 1795133 | 2019-10-17 15:32:03 -0700 | [diff] [blame] | 2118 | const char* outDirName, |
Nick Terrell | 4680e85 | 2017-12-12 18:32:50 -0800 | [diff] [blame] | 2119 | const char* outFileName, const char* suffix, |
Przemyslaw Skibinski | 8349d67 | 2016-12-13 13:24:59 +0100 | [diff] [blame] | 2120 | const char* dictFileName, int compressionLevel, |
Yann Collet | 33f7709 | 2018-08-13 13:02:03 -0700 | [diff] [blame] | 2121 | ZSTD_compressionParameters comprParams) |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 2122 | { |
senhuang42 | 202b295 | 2020-09-03 09:28:40 -0400 | [diff] [blame] | 2123 | int status; |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2124 | int error = 0; |
Bimba Shrestha | f25a6e9 | 2020-01-10 14:25:24 -0800 | [diff] [blame] | 2125 | cRess_t ress = FIO_createCResources(prefs, dictFileName, |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 2126 | FIO_getLargestFileSize(inFileNamesTable, (unsigned)fCtx->nbFilesTotal), |
Bimba Shrestha | f25a6e9 | 2020-01-10 14:25:24 -0800 | [diff] [blame] | 2127 | compressionLevel, comprParams); |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 2128 | |
| 2129 | /* init */ |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2130 | assert(outFileName != NULL || suffix != NULL); |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2131 | if (outFileName != NULL) { /* output into a single destination (stdout typically) */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2132 | FILE *dstFile; |
| 2133 | if (FIO_multiFilesConcatWarning(fCtx, prefs, outFileName, 1 /* displayLevelCutoff */)) { |
senhuang42 | 043b934 | 2020-10-14 20:19:46 -0400 | [diff] [blame] | 2134 | FIO_freeCResources(&ress); |
senhuang42 | 7991c55 | 2020-08-26 16:50:20 -0400 | [diff] [blame] | 2135 | return 1; |
senhuang42 | 7e867ad | 2020-08-26 18:52:32 -0400 | [diff] [blame] | 2136 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2137 | dstFile = FIO_openDstFile(fCtx, prefs, NULL, outFileName, DEFAULT_FILE_PERMISSIONS); |
| 2138 | if (dstFile == NULL) { /* could not open outFileName */ |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2139 | error = 1; |
Pádraig Brady | e059671 | 2018-01-02 15:17:32 +0000 | [diff] [blame] | 2140 | } else { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2141 | AIO_WritePool_setFile(ress.writeCtx, dstFile); |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 2142 | for (; fCtx->currFileIdx < fCtx->nbFilesTotal; ++fCtx->currFileIdx) { |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 2143 | status = FIO_compressFilename_srcFile(fCtx, prefs, ress, outFileName, inFileNamesTable[fCtx->currFileIdx], compressionLevel); |
senhuang42 | 7842f43 | 2020-09-03 09:22:07 -0400 | [diff] [blame] | 2144 | if (!status) fCtx->nbFilesProcessed++; |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 2145 | error |= status; |
senhuang42 | da38891 | 2020-08-25 16:46:47 -0400 | [diff] [blame] | 2146 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2147 | if (AIO_WritePool_closeFile(ress.writeCtx)) |
Yann Collet | 0f2d443 | 2018-12-19 17:25:58 -0800 | [diff] [blame] | 2148 | EXM_THROW(29, "Write error (%s) : cannot properly close %s", |
| 2149 | strerror(errno), outFileName); |
Pádraig Brady | e059671 | 2018-01-02 15:17:32 +0000 | [diff] [blame] | 2150 | } |
Yann Collet | 459a6b7 | 2016-02-15 20:37:23 +0100 | [diff] [blame] | 2151 | } else { |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 2152 | if (outMirroredRootDirName) |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 2153 | UTIL_mirrorSourceFilesDirectories(inFileNamesTable, (unsigned)fCtx->nbFilesTotal, outMirroredRootDirName); |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 2154 | |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 2155 | for (; fCtx->currFileIdx < fCtx->nbFilesTotal; ++fCtx->currFileIdx) { |
senhuang42 | d54566f | 2020-08-28 11:01:04 -0400 | [diff] [blame] | 2156 | const char* const srcFileName = inFileNamesTable[fCtx->currFileIdx]; |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 2157 | const char* dstFileName = NULL; |
| 2158 | if (outMirroredRootDirName) { |
| 2159 | char* validMirroredDirName = UTIL_createMirroredDestDirName(srcFileName, outMirroredRootDirName); |
| 2160 | if (validMirroredDirName) { |
| 2161 | dstFileName = FIO_determineCompressedName(srcFileName, validMirroredDirName, suffix); |
| 2162 | free(validMirroredDirName); |
| 2163 | } else { |
| 2164 | DISPLAYLEVEL(2, "zstd: --output-dir-mirror cannot compress '%s' into '%s' \n", srcFileName, outMirroredRootDirName); |
| 2165 | error=1; |
| 2166 | continue; |
| 2167 | } |
| 2168 | } else { |
| 2169 | dstFileName = FIO_determineCompressedName(srcFileName, outDirName, suffix); /* cannot fail */ |
| 2170 | } |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 2171 | status = FIO_compressFilename_srcFile(fCtx, prefs, ress, dstFileName, srcFileName, compressionLevel); |
senhuang42 | 7842f43 | 2020-09-03 09:22:07 -0400 | [diff] [blame] | 2172 | if (!status) fCtx->nbFilesProcessed++; |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 2173 | error |= status; |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2174 | } |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 2175 | |
Sen Huang | c5ebb37 | 2019-10-09 09:39:52 -0400 | [diff] [blame] | 2176 | if (outDirName) |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 2177 | FIO_checkFilenameCollisions(inFileNamesTable , (unsigned)fCtx->nbFilesTotal); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2178 | } |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 2179 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2180 | if (FIO_shouldDisplayMultipleFileSummary(fCtx)) { |
W. Felix Handte | bbb81c8 | 2021-06-09 13:05:44 -0400 | [diff] [blame] | 2181 | UTIL_HumanReadableSize_t hr_isize = UTIL_makeHumanReadableSize((U64) fCtx->totalBytesInput); |
| 2182 | UTIL_HumanReadableSize_t hr_osize = UTIL_makeHumanReadableSize((U64) fCtx->totalBytesOutput); |
Scott Baker | 77001f0 | 2021-06-04 22:21:00 -0700 | [diff] [blame] | 2183 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2184 | DISPLAY_PROGRESS("\r%79s\r", ""); |
| 2185 | if (fCtx->totalBytesInput == 0) { |
| 2186 | DISPLAY_SUMMARY("%3d files compressed : (%6.*f%4s => %6.*f%4s)\n", |
| 2187 | fCtx->nbFilesProcessed, |
| 2188 | hr_isize.precision, hr_isize.value, hr_isize.suffix, |
| 2189 | hr_osize.precision, hr_osize.value, hr_osize.suffix); |
| 2190 | } else { |
| 2191 | DISPLAY_SUMMARY("%3d files compressed : %.2f%% (%6.*f%4s => %6.*f%4s)\n", |
| 2192 | fCtx->nbFilesProcessed, |
| 2193 | (double)fCtx->totalBytesOutput/((double)fCtx->totalBytesInput)*100, |
| 2194 | hr_isize.precision, hr_isize.value, hr_isize.suffix, |
| 2195 | hr_osize.precision, hr_osize.value, hr_osize.suffix); |
| 2196 | } |
senhuang42 | ab0d332 | 2020-09-15 15:53:32 -0400 | [diff] [blame] | 2197 | } |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 2198 | |
senhuang42 | 043b934 | 2020-10-14 20:19:46 -0400 | [diff] [blame] | 2199 | FIO_freeCResources(&ress); |
Yann Collet | 9012b6c | 2018-10-01 17:16:34 -0700 | [diff] [blame] | 2200 | return error; |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 2201 | } |
| 2202 | |
Yann Collet | f849462 | 2016-05-07 22:43:40 +0200 | [diff] [blame] | 2203 | #endif /* #ifndef ZSTD_NOCOMPRESS */ |
inikep | 3c7c352 | 2016-04-22 13:59:05 +0200 | [diff] [blame] | 2204 | |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 2205 | |
inikep | db39643 | 2016-04-22 18:22:30 +0200 | [diff] [blame] | 2206 | |
| 2207 | #ifndef ZSTD_NODECOMPRESS |
| 2208 | |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 2209 | /* ************************************************************************** |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 2210 | * Decompression |
| 2211 | ***************************************************************************/ |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2212 | typedef struct { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2213 | FIO_Dict_t dict; |
Yann Collet | 6263ba5 | 2016-08-13 23:45:45 +0200 | [diff] [blame] | 2214 | ZSTD_DStream* dctx; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2215 | WritePoolCtx_t *writeCtx; |
| 2216 | ReadPoolCtx_t *readCtx; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2217 | } dRess_t; |
| 2218 | |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 2219 | static dRess_t FIO_createDResources(FIO_prefs_t* const prefs, const char* dictFileName) |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2220 | { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2221 | int useMMap = prefs->mmapDict == ZSTD_ps_enable; |
| 2222 | int forceNoUseMMap = prefs->mmapDict == ZSTD_ps_disable; |
| 2223 | stat_t statbuf; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2224 | dRess_t ress; |
Yann Collet | 5e80dd3 | 2016-07-13 17:38:39 +0200 | [diff] [blame] | 2225 | memset(&ress, 0, sizeof(ress)); |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2226 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2227 | FIO_getDictFileStat(dictFileName, &statbuf); |
| 2228 | |
| 2229 | if (prefs->patchFromMode){ |
| 2230 | U64 const dictSize = UTIL_getFileSizeStat(&statbuf); |
| 2231 | useMMap |= dictSize > prefs->memLimit; |
| 2232 | FIO_adjustMemLimitForPatchFromMode(prefs, dictSize, 0 /* just use the dict size */); |
| 2233 | } |
Bimba Shrestha | 659ff85 | 2020-04-21 21:12:50 -0500 | [diff] [blame] | 2234 | |
Yann Collet | 5e80dd3 | 2016-07-13 17:38:39 +0200 | [diff] [blame] | 2235 | /* Allocation */ |
Yann Collet | 6263ba5 | 2016-08-13 23:45:45 +0200 | [diff] [blame] | 2236 | ress.dctx = ZSTD_createDStream(); |
Yann Collet | 0f2d443 | 2018-12-19 17:25:58 -0800 | [diff] [blame] | 2237 | if (ress.dctx==NULL) |
| 2238 | EXM_THROW(60, "Error: %s : can't create ZSTD_DStream", strerror(errno)); |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 2239 | CHECK( ZSTD_DCtx_setMaxWindowSize(ress.dctx, prefs->memLimit) ); |
senhuang42 | a030560 | 2020-08-24 17:28:00 -0400 | [diff] [blame] | 2240 | CHECK( ZSTD_DCtx_setParameter(ress.dctx, ZSTD_d_forceIgnoreChecksum, !prefs->checksumFlag)); |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 2241 | |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2242 | /* dictionary */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2243 | { |
| 2244 | FIO_dictBufferType_t dictBufferType = (useMMap && !forceNoUseMMap) ? FIO_mmapDict : FIO_mallocDict; |
| 2245 | FIO_initDict(&ress.dict, dictFileName, prefs, &statbuf, dictBufferType); |
| 2246 | |
| 2247 | CHECK(ZSTD_DCtx_reset(ress.dctx, ZSTD_reset_session_only) ); |
| 2248 | |
| 2249 | if (prefs->patchFromMode){ |
| 2250 | CHECK(ZSTD_DCtx_refPrefix(ress.dctx, ress.dict.dictBuffer, ress.dict.dictBufferSize)); |
| 2251 | } else { |
| 2252 | CHECK(ZSTD_DCtx_loadDictionary_byReference(ress.dctx, ress.dict.dictBuffer, ress.dict.dictBufferSize)); |
| 2253 | } |
Yann Collet | 3ecbe6a | 2016-09-14 17:26:59 +0200 | [diff] [blame] | 2254 | } |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2255 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2256 | ress.writeCtx = AIO_WritePool_create(prefs, ZSTD_DStreamOutSize()); |
| 2257 | ress.readCtx = AIO_ReadPool_create(prefs, ZSTD_DStreamInSize()); |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2258 | return ress; |
| 2259 | } |
| 2260 | |
| 2261 | static void FIO_freeDResources(dRess_t ress) |
| 2262 | { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2263 | FIO_freeDict(&(ress.dict)); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 2264 | CHECK( ZSTD_freeDStream(ress.dctx) ); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2265 | AIO_WritePool_free(ress.writeCtx); |
| 2266 | AIO_ReadPool_free(ress.readCtx); |
Yonatan Komornik | 1598e6c | 2022-01-21 13:55:41 -0800 | [diff] [blame] | 2267 | } |
Yann Collet | 4f13703 | 2015-12-17 02:23:58 +0100 | [diff] [blame] | 2268 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2269 | /* FIO_passThrough() : just copy input into output, for compatibility with gzip -df mode |
| 2270 | * @return : 0 (no error) */ |
| 2271 | static int FIO_passThrough(dRess_t *ress) |
Evan Witt | 7993493 | 2023-09-06 22:46:11 +0000 | [diff] [blame] | 2272 | { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2273 | size_t const blockSize = MIN(MIN(64 KB, ZSTD_DStreamInSize()), ZSTD_DStreamOutSize()); |
| 2274 | IOJob_t *writeJob = AIO_WritePool_acquireJob(ress->writeCtx); |
| 2275 | AIO_ReadPool_fillBuffer(ress->readCtx, blockSize); |
Evan Witt | 7993493 | 2023-09-06 22:46:11 +0000 | [diff] [blame] | 2276 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2277 | while(ress->readCtx->srcBufferLoaded) { |
| 2278 | size_t writeSize; |
| 2279 | writeSize = MIN(blockSize, ress->readCtx->srcBufferLoaded); |
| 2280 | assert(writeSize <= writeJob->bufferSize); |
| 2281 | memcpy(writeJob->buffer, ress->readCtx->srcBuffer, writeSize); |
| 2282 | writeJob->usedBufferSize = writeSize; |
| 2283 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
| 2284 | AIO_ReadPool_consumeBytes(ress->readCtx, writeSize); |
| 2285 | AIO_ReadPool_fillBuffer(ress->readCtx, blockSize); |
Yann Collet | de95f96 | 2016-05-23 19:46:47 +0200 | [diff] [blame] | 2286 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2287 | assert(ress->readCtx->reachedEof); |
| 2288 | AIO_WritePool_releaseIoJob(writeJob); |
| 2289 | AIO_WritePool_sparseWriteEnd(ress->writeCtx); |
Yann Collet | de95f96 | 2016-05-23 19:46:47 +0200 | [diff] [blame] | 2290 | return 0; |
| 2291 | } |
| 2292 | |
Yann Collet | 2bfc79a | 2018-02-01 16:13:04 -0800 | [diff] [blame] | 2293 | /* FIO_zstdErrorHelp() : |
| 2294 | * detailed error message when requested window size is too large */ |
Yann Collet | 0ee3609 | 2019-10-17 16:09:53 -0700 | [diff] [blame] | 2295 | static void |
| 2296 | FIO_zstdErrorHelp(const FIO_prefs_t* const prefs, |
| 2297 | const dRess_t* ress, |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2298 | size_t err, |
| 2299 | const char* srcFileName) |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 2300 | { |
| 2301 | ZSTD_frameHeader header; |
Yann Collet | 2bfc79a | 2018-02-01 16:13:04 -0800 | [diff] [blame] | 2302 | |
| 2303 | /* Help message only for one specific error */ |
| 2304 | if (ZSTD_getErrorCode(err) != ZSTD_error_frameParameter_windowTooLarge) |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 2305 | return; |
Yann Collet | 2bfc79a | 2018-02-01 16:13:04 -0800 | [diff] [blame] | 2306 | |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 2307 | /* Try to decode the frame header */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2308 | err = ZSTD_getFrameHeader(&header, ress->readCtx->srcBuffer, ress->readCtx->srcBufferLoaded); |
Yann Collet | 2bfc79a | 2018-02-01 16:13:04 -0800 | [diff] [blame] | 2309 | if (err == 0) { |
Yann Collet | 6c492af | 2018-02-01 20:16:00 -0800 | [diff] [blame] | 2310 | unsigned long long const windowSize = header.windowSize; |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2311 | unsigned const windowLog = FIO_highbit64(windowSize) + ((windowSize & (windowSize - 1)) != 0); |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 2312 | assert(prefs->memLimit > 0); |
Yann Collet | 458a1a1 | 2020-04-13 10:13:29 -0700 | [diff] [blame] | 2313 | DISPLAYLEVEL(1, "%s : Window size larger than maximum : %llu > %u \n", |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 2314 | srcFileName, windowSize, prefs->memLimit); |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 2315 | if (windowLog <= ZSTD_WINDOWLOG_MAX) { |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 2316 | unsigned const windowMB = (unsigned)((windowSize >> 20) + ((windowSize & ((1 MB) - 1)) != 0)); |
Yann Collet | b1407f9 | 2018-10-03 12:43:59 -0700 | [diff] [blame] | 2317 | assert(windowSize < (U64)(1ULL << 52)); /* ensure now overflow for windowMB */ |
Yann Collet | 458a1a1 | 2020-04-13 10:13:29 -0700 | [diff] [blame] | 2318 | DISPLAYLEVEL(1, "%s : Use --long=%u or --memory=%uMB \n", |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 2319 | srcFileName, windowLog, windowMB); |
| 2320 | return; |
Yann Collet | 458a1a1 | 2020-04-13 10:13:29 -0700 | [diff] [blame] | 2321 | } } |
| 2322 | DISPLAYLEVEL(1, "%s : Window log larger than ZSTD_WINDOWLOG_MAX=%u; not supported \n", |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 2323 | srcFileName, ZSTD_WINDOWLOG_MAX); |
| 2324 | } |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2325 | |
| 2326 | /** FIO_decompressFrame() : |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2327 | * @return : size of decoded zstd frame, or an error code |
Yann Collet | 458a1a1 | 2020-04-13 10:13:29 -0700 | [diff] [blame] | 2328 | */ |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2329 | #define FIO_ERROR_FRAME_DECODING ((unsigned long long)(-2)) |
Yann Collet | 0ee3609 | 2019-10-17 16:09:53 -0700 | [diff] [blame] | 2330 | static unsigned long long |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2331 | FIO_decompressZstdFrame(FIO_ctx_t* const fCtx, dRess_t* ress, |
Yann Collet | 458a1a1 | 2020-04-13 10:13:29 -0700 | [diff] [blame] | 2332 | const FIO_prefs_t* const prefs, |
| 2333 | const char* srcFileName, |
| 2334 | U64 alreadyDecoded) /* for multi-frames streams */ |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2335 | { |
| 2336 | U64 frameSize = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2337 | IOJob_t *writeJob = AIO_WritePool_acquireJob(ress->writeCtx); |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2338 | |
Yann Collet | 458a1a1 | 2020-04-13 10:13:29 -0700 | [diff] [blame] | 2339 | /* display last 20 characters only */ |
| 2340 | { size_t const srcFileLength = strlen(srcFileName); |
| 2341 | if (srcFileLength>20) srcFileName += srcFileLength-20; |
| 2342 | } |
Yann Collet | 8afcc80 | 2017-09-29 15:54:09 -0700 | [diff] [blame] | 2343 | |
Stephen Kitt | adb5429 | 2021-02-20 17:28:19 +0100 | [diff] [blame] | 2344 | ZSTD_DCtx_reset(ress->dctx, ZSTD_reset_session_only); |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2345 | |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 2346 | /* Header loading : ensures ZSTD_getFrameHeader() will succeed */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2347 | AIO_ReadPool_fillBuffer(ress->readCtx, ZSTD_FRAMEHEADERSIZE_MAX); |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2348 | |
| 2349 | /* Main decompression Loop */ |
| 2350 | while (1) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2351 | ZSTD_inBuffer inBuff = setInBuffer( ress->readCtx->srcBuffer, ress->readCtx->srcBufferLoaded, 0 ); |
| 2352 | ZSTD_outBuffer outBuff= setOutBuffer( writeJob->buffer, writeJob->bufferSize, 0 ); |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2353 | size_t const readSizeHint = ZSTD_decompressStream(ress->dctx, &outBuff, &inBuff); |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 2354 | UTIL_HumanReadableSize_t const hrs = UTIL_makeHumanReadableSize(alreadyDecoded+frameSize); |
Yann Collet | 01a1abf | 2017-05-05 19:15:24 -0700 | [diff] [blame] | 2355 | if (ZSTD_isError(readSizeHint)) { |
| 2356 | DISPLAYLEVEL(1, "%s : Decoding error (36) : %s \n", |
| 2357 | srcFileName, ZSTD_getErrorName(readSizeHint)); |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 2358 | FIO_zstdErrorHelp(prefs, ress, readSizeHint, srcFileName); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2359 | AIO_WritePool_releaseIoJob(writeJob); |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2360 | return FIO_ERROR_FRAME_DECODING; |
Yann Collet | 01a1abf | 2017-05-05 19:15:24 -0700 | [diff] [blame] | 2361 | } |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2362 | |
| 2363 | /* Write block */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2364 | writeJob->usedBufferSize = outBuff.pos; |
| 2365 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2366 | frameSize += outBuff.pos; |
Binh Vo | d2f31b6 | 2021-06-07 11:50:22 -0400 | [diff] [blame] | 2367 | if (fCtx->nbFilesTotal > 1) { |
| 2368 | size_t srcFileNameSize = strlen(srcFileName); |
| 2369 | if (srcFileNameSize > 18) { |
| 2370 | const char* truncatedSrcFileName = srcFileName + srcFileNameSize - 15; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2371 | DISPLAYUPDATE_PROGRESS( |
| 2372 | "\rDecompress: %2u/%2u files. Current: ...%s : %.*f%s... ", |
| 2373 | fCtx->currFileIdx+1, fCtx->nbFilesTotal, truncatedSrcFileName, hrs.precision, hrs.value, hrs.suffix); |
senhuang42 | 1d5c6fd | 2020-09-16 10:28:45 -0400 | [diff] [blame] | 2374 | } else { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2375 | DISPLAYUPDATE_PROGRESS("\rDecompress: %2u/%2u files. Current: %s : %.*f%s... ", |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 2376 | fCtx->currFileIdx+1, fCtx->nbFilesTotal, srcFileName, hrs.precision, hrs.value, hrs.suffix); |
senhuang42 | 1d5c6fd | 2020-09-16 10:28:45 -0400 | [diff] [blame] | 2377 | } |
Binh Vo | d2f31b6 | 2021-06-07 11:50:22 -0400 | [diff] [blame] | 2378 | } else { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2379 | DISPLAYUPDATE_PROGRESS("\r%-20.20s : %.*f%s... ", |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 2380 | srcFileName, hrs.precision, hrs.value, hrs.suffix); |
senhuang42 | b6abbc3 | 2020-08-26 11:35:07 -0400 | [diff] [blame] | 2381 | } |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2382 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2383 | AIO_ReadPool_consumeBytes(ress->readCtx, inBuff.pos); |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2384 | |
| 2385 | if (readSizeHint == 0) break; /* end of frame */ |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2386 | |
| 2387 | /* Fill input buffer */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2388 | { size_t const toDecode = MIN(readSizeHint, ZSTD_DStreamInSize()); /* support large skippable frames */ |
| 2389 | if (ress->readCtx->srcBufferLoaded < toDecode) { |
| 2390 | size_t const readSize = AIO_ReadPool_fillBuffer(ress->readCtx, toDecode); |
Yann Collet | 8afcc80 | 2017-09-29 15:54:09 -0700 | [diff] [blame] | 2391 | if (readSize==0) { |
| 2392 | DISPLAYLEVEL(1, "%s : Read error (39) : premature end \n", |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2393 | srcFileName); |
| 2394 | AIO_WritePool_releaseIoJob(writeJob); |
Yann Collet | 8afcc80 | 2017-09-29 15:54:09 -0700 | [diff] [blame] | 2395 | return FIO_ERROR_FRAME_DECODING; |
| 2396 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2397 | } } } |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2398 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2399 | AIO_WritePool_releaseIoJob(writeJob); |
| 2400 | AIO_WritePool_sparseWriteEnd(ress->writeCtx); |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2401 | |
| 2402 | return frameSize; |
| 2403 | } |
| 2404 | |
| 2405 | |
Przemyslaw Skibinski | 19aad42 | 2016-12-01 11:56:31 +0100 | [diff] [blame] | 2406 | #ifdef ZSTD_GZDECOMPRESS |
Yann Collet | 0ee3609 | 2019-10-17 16:09:53 -0700 | [diff] [blame] | 2407 | static unsigned long long |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2408 | FIO_decompressGzFrame(dRess_t* ress, const char* srcFileName) |
Przemyslaw Skibinski | b0f2ef2 | 2016-12-02 13:50:29 +0100 | [diff] [blame] | 2409 | { |
Yann Collet | 5bd4237 | 2016-12-02 12:40:57 -0800 | [diff] [blame] | 2410 | unsigned long long outFileSize = 0; |
Przemyslaw Skibinski | c5eebca | 2016-12-02 15:01:31 +0100 | [diff] [blame] | 2411 | z_stream strm; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2412 | int flush = Z_NO_FLUSH; |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2413 | int decodingError = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2414 | IOJob_t *writeJob = NULL; |
Yann Collet | 5bd4237 | 2016-12-02 12:40:57 -0800 | [diff] [blame] | 2415 | |
Przemyslaw Skibinski | c5eebca | 2016-12-02 15:01:31 +0100 | [diff] [blame] | 2416 | strm.zalloc = Z_NULL; |
| 2417 | strm.zfree = Z_NULL; |
| 2418 | strm.opaque = Z_NULL; |
| 2419 | strm.next_in = 0; |
Przemyslaw Skibinski | 862698f | 2017-02-27 13:21:05 +0100 | [diff] [blame] | 2420 | strm.avail_in = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2421 | /* see https://www.zlib.net/manual.html */ |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 2422 | if (inflateInit2(&strm, 15 /* maxWindowLogSize */ + 16 /* gzip only */) != Z_OK) |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2423 | return FIO_ERROR_FRAME_DECODING; |
Yann Collet | 5bd4237 | 2016-12-02 12:40:57 -0800 | [diff] [blame] | 2424 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2425 | writeJob = AIO_WritePool_acquireJob(ress->writeCtx); |
| 2426 | strm.next_out = (Bytef*)writeJob->buffer; |
| 2427 | strm.avail_out = (uInt)writeJob->bufferSize; |
| 2428 | strm.avail_in = (uInt)ress->readCtx->srcBufferLoaded; |
| 2429 | strm.next_in = (z_const unsigned char*)ress->readCtx->srcBuffer; |
Przemyslaw Skibinski | 4b504f1 | 2016-12-02 13:11:39 +0100 | [diff] [blame] | 2430 | |
Przemyslaw Skibinski | c5eebca | 2016-12-02 15:01:31 +0100 | [diff] [blame] | 2431 | for ( ; ; ) { |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2432 | int ret; |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2433 | if (strm.avail_in == 0) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2434 | AIO_ReadPool_consumeAndRefill(ress->readCtx); |
| 2435 | if (ress->readCtx->srcBufferLoaded == 0) flush = Z_FINISH; |
| 2436 | strm.next_in = (z_const unsigned char*)ress->readCtx->srcBuffer; |
| 2437 | strm.avail_in = (uInt)ress->readCtx->srcBufferLoaded; |
Przemyslaw Skibinski | 4b504f1 | 2016-12-02 13:11:39 +0100 | [diff] [blame] | 2438 | } |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2439 | ret = inflate(&strm, flush); |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2440 | if (ret == Z_BUF_ERROR) { |
| 2441 | DISPLAYLEVEL(1, "zstd: %s: premature gz end \n", srcFileName); |
| 2442 | decodingError = 1; break; |
| 2443 | } |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 2444 | if (ret != Z_OK && ret != Z_STREAM_END) { |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2445 | DISPLAYLEVEL(1, "zstd: %s: inflate error %d \n", srcFileName, ret); |
| 2446 | decodingError = 1; break; |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 2447 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2448 | { size_t const decompBytes = writeJob->bufferSize - strm.avail_out; |
Yann Collet | 5bd4237 | 2016-12-02 12:40:57 -0800 | [diff] [blame] | 2449 | if (decompBytes) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2450 | writeJob->usedBufferSize = decompBytes; |
| 2451 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
Yann Collet | 5bd4237 | 2016-12-02 12:40:57 -0800 | [diff] [blame] | 2452 | outFileSize += decompBytes; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2453 | strm.next_out = (Bytef*)writeJob->buffer; |
| 2454 | strm.avail_out = (uInt)writeJob->bufferSize; |
Przemyslaw Skibinski | b493e3b | 2016-12-05 17:39:38 +0100 | [diff] [blame] | 2455 | } |
| 2456 | } |
| 2457 | if (ret == Z_STREAM_END) break; |
| 2458 | } |
Przemyslaw Skibinski | 19aad42 | 2016-12-01 11:56:31 +0100 | [diff] [blame] | 2459 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2460 | AIO_ReadPool_consumeBytes(ress->readCtx, ress->readCtx->srcBufferLoaded - strm.avail_in); |
| 2461 | |
Yann Collet | c9f21c8 | 2017-07-03 13:45:09 -0700 | [diff] [blame] | 2462 | if ( (inflateEnd(&strm) != Z_OK) /* release resources ; error detected */ |
| 2463 | && (decodingError==0) ) { |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2464 | DISPLAYLEVEL(1, "zstd: %s: inflateEnd error \n", srcFileName); |
| 2465 | decodingError = 1; |
| 2466 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2467 | AIO_WritePool_releaseIoJob(writeJob); |
| 2468 | AIO_WritePool_sparseWriteEnd(ress->writeCtx); |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2469 | return decodingError ? FIO_ERROR_FRAME_DECODING : outFileSize; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2470 | } |
| 2471 | #endif |
| 2472 | |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2473 | #ifdef ZSTD_LZMADECOMPRESS |
Yann Collet | 0ee3609 | 2019-10-17 16:09:53 -0700 | [diff] [blame] | 2474 | static unsigned long long |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2475 | FIO_decompressLzmaFrame(dRess_t* ress, |
Yann Collet | 0ee3609 | 2019-10-17 16:09:53 -0700 | [diff] [blame] | 2476 | const char* srcFileName, int plain_lzma) |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2477 | { |
| 2478 | unsigned long long outFileSize = 0; |
| 2479 | lzma_stream strm = LZMA_STREAM_INIT; |
| 2480 | lzma_action action = LZMA_RUN; |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2481 | lzma_ret initRet; |
| 2482 | int decodingError = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2483 | IOJob_t *writeJob = NULL; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2484 | |
| 2485 | strm.next_in = 0; |
| 2486 | strm.avail_in = 0; |
| 2487 | if (plain_lzma) { |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2488 | initRet = lzma_alone_decoder(&strm, UINT64_MAX); /* LZMA */ |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2489 | } else { |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2490 | initRet = lzma_stream_decoder(&strm, UINT64_MAX, 0); /* XZ */ |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2491 | } |
| 2492 | |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2493 | if (initRet != LZMA_OK) { |
| 2494 | DISPLAYLEVEL(1, "zstd: %s: %s error %d \n", |
| 2495 | plain_lzma ? "lzma_alone_decoder" : "lzma_stream_decoder", |
| 2496 | srcFileName, initRet); |
| 2497 | return FIO_ERROR_FRAME_DECODING; |
| 2498 | } |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2499 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2500 | writeJob = AIO_WritePool_acquireJob(ress->writeCtx); |
| 2501 | strm.next_out = (BYTE*)writeJob->buffer; |
| 2502 | strm.avail_out = writeJob->bufferSize; |
| 2503 | strm.next_in = (BYTE const*)ress->readCtx->srcBuffer; |
| 2504 | strm.avail_in = ress->readCtx->srcBufferLoaded; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2505 | |
| 2506 | for ( ; ; ) { |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2507 | lzma_ret ret; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2508 | if (strm.avail_in == 0) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2509 | AIO_ReadPool_consumeAndRefill(ress->readCtx); |
| 2510 | if (ress->readCtx->srcBufferLoaded == 0) action = LZMA_FINISH; |
| 2511 | strm.next_in = (BYTE const*)ress->readCtx->srcBuffer; |
| 2512 | strm.avail_in = ress->readCtx->srcBufferLoaded; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2513 | } |
| 2514 | ret = lzma_code(&strm, action); |
| 2515 | |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2516 | if (ret == LZMA_BUF_ERROR) { |
| 2517 | DISPLAYLEVEL(1, "zstd: %s: premature lzma end \n", srcFileName); |
| 2518 | decodingError = 1; break; |
| 2519 | } |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 2520 | if (ret != LZMA_OK && ret != LZMA_STREAM_END) { |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2521 | DISPLAYLEVEL(1, "zstd: %s: lzma_code decoding error %d \n", |
| 2522 | srcFileName, ret); |
| 2523 | decodingError = 1; break; |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 2524 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2525 | { size_t const decompBytes = writeJob->bufferSize - strm.avail_out; |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2526 | if (decompBytes) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2527 | writeJob->usedBufferSize = decompBytes; |
| 2528 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2529 | outFileSize += decompBytes; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2530 | strm.next_out = (BYTE*)writeJob->buffer; |
| 2531 | strm.avail_out = writeJob->bufferSize; |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 2532 | } } |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2533 | if (ret == LZMA_STREAM_END) break; |
| 2534 | } |
| 2535 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2536 | AIO_ReadPool_consumeBytes(ress->readCtx, ress->readCtx->srcBufferLoaded - strm.avail_in); |
Nick Terrell | aa8bcf3 | 2017-03-13 18:11:07 -0700 | [diff] [blame] | 2537 | lzma_end(&strm); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2538 | AIO_WritePool_releaseIoJob(writeJob); |
| 2539 | AIO_WritePool_sparseWriteEnd(ress->writeCtx); |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2540 | return decodingError ? FIO_ERROR_FRAME_DECODING : outFileSize; |
Przemyslaw Skibinski | 19aad42 | 2016-12-01 11:56:31 +0100 | [diff] [blame] | 2541 | } |
| 2542 | #endif |
| 2543 | |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2544 | #ifdef ZSTD_LZ4DECOMPRESS |
Yann Collet | 0ee3609 | 2019-10-17 16:09:53 -0700 | [diff] [blame] | 2545 | static unsigned long long |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2546 | FIO_decompressLz4Frame(dRess_t* ress, const char* srcFileName) |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2547 | { |
| 2548 | unsigned long long filesize = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2549 | LZ4F_errorCode_t nextToLoad = 4; |
Sean Purcell | 2c4b6fe | 2017-04-25 11:00:54 -0700 | [diff] [blame] | 2550 | LZ4F_decompressionContext_t dCtx; |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2551 | LZ4F_errorCode_t const errorCode = LZ4F_createDecompressionContext(&dCtx, LZ4F_VERSION); |
Yann Collet | e97ff3b | 2017-07-03 11:27:29 -0700 | [diff] [blame] | 2552 | int decodingError = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2553 | IOJob_t *writeJob = NULL; |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2554 | |
Yann Collet | e97ff3b | 2017-07-03 11:27:29 -0700 | [diff] [blame] | 2555 | if (LZ4F_isError(errorCode)) { |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2556 | DISPLAYLEVEL(1, "zstd: failed to create lz4 decompression context \n"); |
Yann Collet | e97ff3b | 2017-07-03 11:27:29 -0700 | [diff] [blame] | 2557 | return FIO_ERROR_FRAME_DECODING; |
| 2558 | } |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2559 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2560 | writeJob = AIO_WritePool_acquireJob(ress->writeCtx); |
Yonatan Komornik | 70df5de | 2022-01-24 14:43:02 -0800 | [diff] [blame] | 2561 | |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2562 | /* Main Loop */ |
| 2563 | for (;nextToLoad;) { |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2564 | size_t pos = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2565 | size_t decodedBytes = writeJob->bufferSize; |
| 2566 | int fullBufferDecoded = 0; |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2567 | |
| 2568 | /* Read input */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2569 | AIO_ReadPool_fillBuffer(ress->readCtx, nextToLoad); |
| 2570 | if(!ress->readCtx->srcBufferLoaded) break; /* reached end of file */ |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2571 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2572 | while ((pos < ress->readCtx->srcBufferLoaded) || fullBufferDecoded) { /* still to read, or still to flush */ |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2573 | /* Decode Input (at least partially) */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2574 | size_t remaining = ress->readCtx->srcBufferLoaded - pos; |
| 2575 | decodedBytes = writeJob->bufferSize; |
| 2576 | nextToLoad = LZ4F_decompress(dCtx, writeJob->buffer, &decodedBytes, (char*)(ress->readCtx->srcBuffer)+pos, |
| 2577 | &remaining, NULL); |
Yann Collet | e97ff3b | 2017-07-03 11:27:29 -0700 | [diff] [blame] | 2578 | if (LZ4F_isError(nextToLoad)) { |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2579 | DISPLAYLEVEL(1, "zstd: %s: lz4 decompression error : %s \n", |
Yann Collet | e12ae02 | 2017-05-16 17:32:33 -0700 | [diff] [blame] | 2580 | srcFileName, LZ4F_getErrorName(nextToLoad)); |
cyan4973 | 62487b5 | 2018-04-23 18:50:16 -0700 | [diff] [blame] | 2581 | decodingError = 1; nextToLoad = 0; break; |
Yann Collet | e97ff3b | 2017-07-03 11:27:29 -0700 | [diff] [blame] | 2582 | } |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2583 | pos += remaining; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2584 | assert(pos <= ress->readCtx->srcBufferLoaded); |
| 2585 | fullBufferDecoded = decodedBytes == writeJob->bufferSize; |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2586 | |
| 2587 | /* Write Block */ |
| 2588 | if (decodedBytes) { |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 2589 | UTIL_HumanReadableSize_t hrs; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2590 | writeJob->usedBufferSize = decodedBytes; |
| 2591 | AIO_WritePool_enqueueAndReacquireWriteJob(&writeJob); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2592 | filesize += decodedBytes; |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 2593 | hrs = UTIL_makeHumanReadableSize(filesize); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2594 | DISPLAYUPDATE_PROGRESS("\rDecompressed : %.*f%s ", hrs.precision, hrs.value, hrs.suffix); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | if (!nextToLoad) break; |
| 2598 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2599 | AIO_ReadPool_consumeBytes(ress->readCtx, pos); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2600 | } |
Yann Collet | e97ff3b | 2017-07-03 11:27:29 -0700 | [diff] [blame] | 2601 | if (nextToLoad!=0) { |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2602 | DISPLAYLEVEL(1, "zstd: %s: unfinished lz4 stream \n", srcFileName); |
Yann Collet | e97ff3b | 2017-07-03 11:27:29 -0700 | [diff] [blame] | 2603 | decodingError=1; |
| 2604 | } |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2605 | |
| 2606 | LZ4F_freeDecompressionContext(dCtx); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2607 | AIO_WritePool_releaseIoJob(writeJob); |
| 2608 | AIO_WritePool_sparseWriteEnd(ress->writeCtx); |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2609 | |
Yann Collet | e97ff3b | 2017-07-03 11:27:29 -0700 | [diff] [blame] | 2610 | return decodingError ? FIO_ERROR_FRAME_DECODING : filesize; |
Sean Purcell | 4de8632 | 2017-04-24 16:48:25 -0700 | [diff] [blame] | 2611 | } |
| 2612 | #endif |
| 2613 | |
| 2614 | |
Przemyslaw Skibinski | 19aad42 | 2016-12-01 11:56:31 +0100 | [diff] [blame] | 2615 | |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2616 | /** FIO_decompressFrames() : |
| 2617 | * Find and decode frames inside srcFile |
| 2618 | * srcFile presumed opened and valid |
| 2619 | * @return : 0 : OK |
| 2620 | * 1 : error |
| 2621 | */ |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 2622 | static int FIO_decompressFrames(FIO_ctx_t* const fCtx, |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2623 | dRess_t ress, const FIO_prefs_t* const prefs, |
| 2624 | const char* dstFileName, const char* srcFileName) |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2625 | { |
| 2626 | unsigned readSomething = 0; |
| 2627 | unsigned long long filesize = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2628 | int passThrough = prefs->passThrough; |
| 2629 | |
| 2630 | if (passThrough == -1) { |
| 2631 | /* If pass-through mode is not explicitly enabled or disabled, |
| 2632 | * default to the legacy behavior of enabling it if we are writing |
| 2633 | * to stdout with the overwrite flag enabled. |
| 2634 | */ |
| 2635 | passThrough = prefs->overwrite && !strcmp(dstFileName, stdoutmark); |
| 2636 | } |
| 2637 | assert(passThrough == 0 || passThrough == 1); |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2638 | |
| 2639 | /* for each frame */ |
| 2640 | for ( ; ; ) { |
| 2641 | /* check magic number -> version */ |
| 2642 | size_t const toRead = 4; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2643 | const BYTE* buf; |
| 2644 | AIO_ReadPool_fillBuffer(ress.readCtx, toRead); |
| 2645 | buf = (const BYTE*)ress.readCtx->srcBuffer; |
| 2646 | if (ress.readCtx->srcBufferLoaded==0) { |
Yann Collet | b8280fe | 2017-07-03 15:14:55 -0700 | [diff] [blame] | 2647 | if (readSomething==0) { /* srcFile is empty (which is invalid) */ |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2648 | DISPLAYLEVEL(1, "zstd: %s: unexpected end of file \n", srcFileName); |
| 2649 | return 1; |
Yann Collet | b8280fe | 2017-07-03 15:14:55 -0700 | [diff] [blame] | 2650 | } /* else, just reached frame boundary */ |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2651 | break; /* no more input */ |
| 2652 | } |
| 2653 | readSomething = 1; /* there is at least 1 byte in srcFile */ |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2654 | if (ress.readCtx->srcBufferLoaded < toRead) { /* not enough input to check magic number */ |
| 2655 | if (passThrough) { |
| 2656 | return FIO_passThrough(&ress); |
| 2657 | } |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2658 | DISPLAYLEVEL(1, "zstd: %s: unknown header \n", srcFileName); |
| 2659 | return 1; |
| 2660 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2661 | if (ZSTD_isFrame(buf, ress.readCtx->srcBufferLoaded)) { |
| 2662 | unsigned long long const frameSize = FIO_decompressZstdFrame(fCtx, &ress, prefs, srcFileName, filesize); |
Yann Collet | b8280fe | 2017-07-03 15:14:55 -0700 | [diff] [blame] | 2663 | if (frameSize == FIO_ERROR_FRAME_DECODING) return 1; |
| 2664 | filesize += frameSize; |
| 2665 | } else if (buf[0] == 31 && buf[1] == 139) { /* gz magic number */ |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2666 | #ifdef ZSTD_GZDECOMPRESS |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2667 | unsigned long long const frameSize = FIO_decompressGzFrame(&ress, srcFileName); |
Yann Collet | 368b974 | 2017-07-03 13:47:46 -0700 | [diff] [blame] | 2668 | if (frameSize == FIO_ERROR_FRAME_DECODING) return 1; |
| 2669 | filesize += frameSize; |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2670 | #else |
| 2671 | DISPLAYLEVEL(1, "zstd: %s: gzip file cannot be uncompressed (zstd compiled without HAVE_ZLIB) -- ignored \n", srcFileName); |
| 2672 | return 1; |
| 2673 | #endif |
| 2674 | } else if ((buf[0] == 0xFD && buf[1] == 0x37) /* xz magic number */ |
| 2675 | || (buf[0] == 0x5D && buf[1] == 0x00)) { /* lzma header (no magic number) */ |
| 2676 | #ifdef ZSTD_LZMADECOMPRESS |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2677 | unsigned long long const frameSize = FIO_decompressLzmaFrame(&ress, srcFileName, buf[0] != 0xFD); |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2678 | if (frameSize == FIO_ERROR_FRAME_DECODING) return 1; |
| 2679 | filesize += frameSize; |
| 2680 | #else |
| 2681 | DISPLAYLEVEL(1, "zstd: %s: xz/lzma file cannot be uncompressed (zstd compiled without HAVE_LZMA) -- ignored \n", srcFileName); |
| 2682 | return 1; |
| 2683 | #endif |
| 2684 | } else if (MEM_readLE32(buf) == LZ4_MAGICNUMBER) { |
| 2685 | #ifdef ZSTD_LZ4DECOMPRESS |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2686 | unsigned long long const frameSize = FIO_decompressLz4Frame(&ress, srcFileName); |
Yann Collet | e97ff3b | 2017-07-03 11:27:29 -0700 | [diff] [blame] | 2687 | if (frameSize == FIO_ERROR_FRAME_DECODING) return 1; |
| 2688 | filesize += frameSize; |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2689 | #else |
| 2690 | DISPLAYLEVEL(1, "zstd: %s: lz4 file cannot be uncompressed (zstd compiled without HAVE_LZ4) -- ignored \n", srcFileName); |
| 2691 | return 1; |
| 2692 | #endif |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2693 | } else if (passThrough) { |
| 2694 | return FIO_passThrough(&ress); |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2695 | } else { |
| 2696 | DISPLAYLEVEL(1, "zstd: %s: unsupported format \n", srcFileName); |
| 2697 | return 1; |
| 2698 | } } /* for each frame */ |
| 2699 | |
| 2700 | /* Final Status */ |
senhuang42 | 28a9dc7 | 2020-09-03 20:23:30 -0400 | [diff] [blame] | 2701 | fCtx->totalBytesOutput += (size_t)filesize; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2702 | DISPLAY_PROGRESS("\r%79s\r", ""); |
| 2703 | if (FIO_shouldDisplayFileSummary(fCtx)) |
| 2704 | DISPLAY_SUMMARY("%-20s: %llu bytes \n", srcFileName, filesize); |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2705 | |
| 2706 | return 0; |
| 2707 | } |
| 2708 | |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2709 | /** FIO_decompressDstFile() : |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2710 | open `dstFileName`, or pass-through if writeCtx's file is already != 0, |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2711 | then start decompression process (FIO_decompressFrames()). |
| 2712 | @return : 0 : OK |
| 2713 | 1 : operation aborted |
| 2714 | */ |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 2715 | static int FIO_decompressDstFile(FIO_ctx_t* const fCtx, |
| 2716 | FIO_prefs_t* const prefs, |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2717 | dRess_t ress, |
| 2718 | const char* dstFileName, |
| 2719 | const char* srcFileName, |
| 2720 | const stat_t* srcFileStat) |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2721 | { |
| 2722 | int result; |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2723 | int releaseDstFile = 0; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2724 | int transferStat = 0; |
| 2725 | int dstFd = 0; |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2726 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2727 | if ((AIO_WritePool_getFile(ress.writeCtx) == NULL) && (prefs->testMode == 0)) { |
| 2728 | FILE *dstFile; |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 2729 | int dstFilePermissions = DEFAULT_FILE_PERMISSIONS; |
| 2730 | if ( strcmp(srcFileName, stdinmark) /* special case : don't transfer permissions from stdin */ |
Mike Gilbert | 57a86d9 | 2022-01-13 16:47:18 -0500 | [diff] [blame] | 2731 | && strcmp(dstFileName, stdoutmark) |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2732 | && UTIL_isRegularFileStat(srcFileStat) ) { |
| 2733 | transferStat = 1; |
| 2734 | dstFilePermissions = TEMPORARY_FILE_PERMISSIONS; |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 2735 | } |
| 2736 | |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2737 | releaseDstFile = 1; |
| 2738 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2739 | dstFile = FIO_openDstFile(fCtx, prefs, srcFileName, dstFileName, dstFilePermissions); |
| 2740 | if (dstFile==NULL) return 1; |
| 2741 | dstFd = fileno(dstFile); |
| 2742 | AIO_WritePool_setFile(ress.writeCtx, dstFile); |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2743 | |
| 2744 | /* Must only be added after FIO_openDstFile() succeeds. |
| 2745 | * Otherwise we may delete the destination file if it already exists, |
| 2746 | * and the user presses Ctrl-C when asked if they wish to overwrite. |
| 2747 | */ |
| 2748 | addHandler(dstFileName); |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2749 | } |
| 2750 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2751 | result = FIO_decompressFrames(fCtx, ress, prefs, dstFileName, srcFileName); |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2752 | |
| 2753 | if (releaseDstFile) { |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2754 | clearHandler(); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2755 | |
| 2756 | if (transferStat) { |
| 2757 | UTIL_setFDStat(dstFd, dstFileName, srcFileStat); |
| 2758 | } |
| 2759 | |
| 2760 | if (AIO_WritePool_closeFile(ress.writeCtx)) { |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2761 | DISPLAYLEVEL(1, "zstd: %s: %s \n", dstFileName, strerror(errno)); |
| 2762 | result = 1; |
| 2763 | } |
| 2764 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2765 | if (transferStat) { |
| 2766 | UTIL_utime(dstFileName, srcFileStat); |
W. Felix Handte | 9cd6c1f | 2021-08-04 14:49:56 -0400 | [diff] [blame] | 2767 | } |
| 2768 | |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2769 | if ( (result != 0) /* operation failure */ |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2770 | && strcmp(dstFileName, stdoutmark) /* special case : don't remove() stdout */ |
| 2771 | ) { |
W. Felix Handte | b02cdf6 | 2020-08-10 15:39:14 -0400 | [diff] [blame] | 2772 | FIO_removeFile(dstFileName); /* remove decompression artefact; note: don't do anything special if remove() fails */ |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2773 | } |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | return result; |
| 2777 | } |
| 2778 | |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2779 | |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 2780 | /** FIO_decompressSrcFile() : |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2781 | Open `srcFileName`, transfer control to decompressDstFile() |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 2782 | @return : 0 : OK |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2783 | 1 : error |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 2784 | */ |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 2785 | static int FIO_decompressSrcFile(FIO_ctx_t* const fCtx, FIO_prefs_t* const prefs, dRess_t ress, const char* dstFileName, const char* srcFileName) |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 2786 | { |
Przemyslaw Skibinski | b0f2ef2 | 2016-12-02 13:50:29 +0100 | [diff] [blame] | 2787 | FILE* srcFile; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2788 | stat_t srcFileStat; |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2789 | int result; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2790 | U64 fileSize = UTIL_FILESIZE_UNKNOWN; |
Przemyslaw Skibinski | 0e14675 | 2016-11-30 13:34:21 +0100 | [diff] [blame] | 2791 | |
Yann Collet | b09b12c | 2016-06-09 22:59:51 +0200 | [diff] [blame] | 2792 | if (UTIL_isDirectory(srcFileName)) { |
| 2793 | DISPLAYLEVEL(1, "zstd: %s is a directory -- ignored \n", srcFileName); |
| 2794 | return 1; |
| 2795 | } |
Przemyslaw Skibinski | 0e14675 | 2016-11-30 13:34:21 +0100 | [diff] [blame] | 2796 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2797 | srcFile = FIO_openSrcFile(prefs, srcFileName, &srcFileStat); |
Yann Collet | 0b9b894 | 2017-02-27 00:27:30 -0800 | [diff] [blame] | 2798 | if (srcFile==NULL) return 1; |
Yonatan Komornik | 79bdb8c | 2023-02-02 15:19:22 -0800 | [diff] [blame] | 2799 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2800 | /* Don't use AsyncIO for small files */ |
| 2801 | if (strcmp(srcFileName, stdinmark)) /* Stdin doesn't have stats */ |
| 2802 | fileSize = UTIL_getFileSizeStat(&srcFileStat); |
| 2803 | if(fileSize != UTIL_FILESIZE_UNKNOWN && fileSize < ZSTD_BLOCKSIZE_MAX * 3) { |
| 2804 | AIO_ReadPool_setAsync(ress.readCtx, 0); |
| 2805 | AIO_WritePool_setAsync(ress.writeCtx, 0); |
| 2806 | } else { |
| 2807 | AIO_ReadPool_setAsync(ress.readCtx, 1); |
| 2808 | AIO_WritePool_setAsync(ress.writeCtx, 1); |
| 2809 | } |
| 2810 | |
| 2811 | AIO_ReadPool_setFile(ress.readCtx, srcFile); |
| 2812 | |
| 2813 | result = FIO_decompressDstFile(fCtx, prefs, ress, dstFileName, srcFileName, &srcFileStat); |
| 2814 | |
| 2815 | AIO_ReadPool_setFile(ress.readCtx, NULL); |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 2816 | |
Przemyslaw Skibinski | b0f2ef2 | 2016-12-02 13:50:29 +0100 | [diff] [blame] | 2817 | /* Close file */ |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2818 | if (fclose(srcFile)) { |
Yann Collet | 9288970 | 2017-09-18 13:41:54 -0700 | [diff] [blame] | 2819 | DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno)); /* error should not happen */ |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2820 | return 1; |
| 2821 | } |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 2822 | if ( prefs->removeSrcFile /* --rm */ |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2823 | && (result==0) /* decompression successful */ |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2824 | && strcmp(srcFileName, stdinmark) ) /* not stdin */ { |
Nick Terrell | a6052af | 2017-11-17 16:38:56 -0800 | [diff] [blame] | 2825 | /* We must clear the handler, since after this point calling it would |
| 2826 | * delete both the source and destination files. |
| 2827 | */ |
| 2828 | clearHandler(); |
W. Felix Handte | b02cdf6 | 2020-08-10 15:39:14 -0400 | [diff] [blame] | 2829 | if (FIO_removeFile(srcFileName)) { |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2830 | /* failed to remove src file */ |
| 2831 | DISPLAYLEVEL(1, "zstd: %s: %s \n", srcFileName, strerror(errno)); |
| 2832 | return 1; |
| 2833 | } } |
Yann Collet | c34185d | 2017-07-03 10:27:16 -0700 | [diff] [blame] | 2834 | return result; |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 2835 | } |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 2836 | |
Yann Collet | 1f1f239 | 2016-02-12 18:33:26 +0100 | [diff] [blame] | 2837 | |
Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 2838 | |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 2839 | int FIO_decompressFilename(FIO_ctx_t* const fCtx, FIO_prefs_t* const prefs, |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 2840 | const char* dstFileName, const char* srcFileName, |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2841 | const char* dictFileName) |
| 2842 | { |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 2843 | dRess_t const ress = FIO_createDResources(prefs, dictFileName); |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2844 | |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 2845 | int const decodingError = FIO_decompressSrcFile(fCtx, prefs, ress, dstFileName, srcFileName); |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2846 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2847 | |
| 2848 | |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2849 | FIO_freeDResources(ress); |
Yann Collet | 6c35112 | 2017-07-03 13:24:50 -0700 | [diff] [blame] | 2850 | return decodingError; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2851 | } |
| 2852 | |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2853 | static const char *suffixList[] = { |
| 2854 | ZSTD_EXTENSION, |
| 2855 | TZSTD_EXTENSION, |
senhuang42 | 6b6cc80 | 2020-09-18 12:49:51 -0400 | [diff] [blame] | 2856 | #ifndef ZSTD_NODECOMPRESS |
| 2857 | ZSTD_ALT_EXTENSION, |
| 2858 | #endif |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2859 | #ifdef ZSTD_GZDECOMPRESS |
| 2860 | GZ_EXTENSION, |
| 2861 | TGZ_EXTENSION, |
| 2862 | #endif |
| 2863 | #ifdef ZSTD_LZMADECOMPRESS |
| 2864 | LZMA_EXTENSION, |
| 2865 | XZ_EXTENSION, |
| 2866 | TXZ_EXTENSION, |
| 2867 | #endif |
| 2868 | #ifdef ZSTD_LZ4DECOMPRESS |
| 2869 | LZ4_EXTENSION, |
| 2870 | TLZ4_EXTENSION, |
| 2871 | #endif |
| 2872 | NULL |
| 2873 | }; |
| 2874 | |
| 2875 | static const char *suffixListStr = |
| 2876 | ZSTD_EXTENSION "/" TZSTD_EXTENSION |
| 2877 | #ifdef ZSTD_GZDECOMPRESS |
| 2878 | "/" GZ_EXTENSION "/" TGZ_EXTENSION |
| 2879 | #endif |
| 2880 | #ifdef ZSTD_LZMADECOMPRESS |
| 2881 | "/" LZMA_EXTENSION "/" XZ_EXTENSION "/" TXZ_EXTENSION |
| 2882 | #endif |
| 2883 | #ifdef ZSTD_LZ4DECOMPRESS |
| 2884 | "/" LZ4_EXTENSION "/" TLZ4_EXTENSION |
| 2885 | #endif |
| 2886 | ; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2887 | |
Yann Collet | 1ab71a8 | 2018-09-28 18:19:23 -0700 | [diff] [blame] | 2888 | /* FIO_determineDstName() : |
| 2889 | * create a destination filename from a srcFileName. |
| 2890 | * @return a pointer to it. |
| 2891 | * @return == NULL if there is an error */ |
| 2892 | static const char* |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2893 | FIO_determineDstName(const char* srcFileName, const char* outDirName) |
Yann Collet | 1ab71a8 | 2018-09-28 18:19:23 -0700 | [diff] [blame] | 2894 | { |
| 2895 | static size_t dfnbCapacity = 0; |
| 2896 | static char* dstFileNameBuffer = NULL; /* using static allocation : this function cannot be multi-threaded */ |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2897 | size_t dstFileNameEndPos; |
Sen Huang | 6b81bfb | 2019-10-03 15:23:49 -0400 | [diff] [blame] | 2898 | char* outDirFilename = NULL; |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2899 | const char* dstSuffix = ""; |
| 2900 | size_t dstSuffixLen = 0; |
Sergey Ponomarev | b804dd3 | 2019-09-14 21:14:43 +0300 | [diff] [blame] | 2901 | |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2902 | size_t sfnSize = strlen(srcFileName); |
Yann Collet | 1795133 | 2019-10-17 15:32:03 -0700 | [diff] [blame] | 2903 | |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2904 | size_t srcSuffixLen; |
| 2905 | const char* const srcSuffix = strrchr(srcFileName, '.'); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2906 | |
| 2907 | if(!strcmp(srcFileName, stdinmark)) { |
| 2908 | return stdoutmark; |
| 2909 | } |
| 2910 | |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2911 | if (srcSuffix == NULL) { |
| 2912 | DISPLAYLEVEL(1, |
| 2913 | "zstd: %s: unknown suffix (%s expected). " |
| 2914 | "Can't derive the output file name. " |
| 2915 | "Specify it with -o dstFileName. Ignoring.\n", |
| 2916 | srcFileName, suffixListStr); |
Yann Collet | 1ab71a8 | 2018-09-28 18:19:23 -0700 | [diff] [blame] | 2917 | return NULL; |
| 2918 | } |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2919 | srcSuffixLen = strlen(srcSuffix); |
Yann Collet | 1ab71a8 | 2018-09-28 18:19:23 -0700 | [diff] [blame] | 2920 | |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2921 | { |
| 2922 | const char** matchedSuffixPtr; |
| 2923 | for (matchedSuffixPtr = suffixList; *matchedSuffixPtr != NULL; matchedSuffixPtr++) { |
| 2924 | if (!strcmp(*matchedSuffixPtr, srcSuffix)) { |
| 2925 | break; |
| 2926 | } |
| 2927 | } |
| 2928 | |
| 2929 | /* check suffix is authorized */ |
| 2930 | if (sfnSize <= srcSuffixLen || *matchedSuffixPtr == NULL) { |
| 2931 | DISPLAYLEVEL(1, |
| 2932 | "zstd: %s: unknown suffix (%s expected). " |
| 2933 | "Can't derive the output file name. " |
| 2934 | "Specify it with -o dstFileName. Ignoring.\n", |
| 2935 | srcFileName, suffixListStr); |
| 2936 | return NULL; |
| 2937 | } |
| 2938 | |
| 2939 | if ((*matchedSuffixPtr)[1] == 't') { |
| 2940 | dstSuffix = ".tar"; |
| 2941 | dstSuffixLen = strlen(dstSuffix); |
| 2942 | } |
Yann Collet | 1ab71a8 | 2018-09-28 18:19:23 -0700 | [diff] [blame] | 2943 | } |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2944 | |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2945 | if (outDirName) { |
Sen Huang | 6b81bfb | 2019-10-03 15:23:49 -0400 | [diff] [blame] | 2946 | outDirFilename = FIO_createFilename_fromOutDir(srcFileName, outDirName, 0); |
| 2947 | sfnSize = strlen(outDirFilename); |
| 2948 | assert(outDirFilename != NULL); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2949 | } |
Yann Collet | 1ab71a8 | 2018-09-28 18:19:23 -0700 | [diff] [blame] | 2950 | |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2951 | if (dfnbCapacity+srcSuffixLen <= sfnSize+1+dstSuffixLen) { |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2952 | /* allocate enough space to write dstFilename into it */ |
Yann Collet | 1ab71a8 | 2018-09-28 18:19:23 -0700 | [diff] [blame] | 2953 | free(dstFileNameBuffer); |
| 2954 | dfnbCapacity = sfnSize + 20; |
| 2955 | dstFileNameBuffer = (char*)malloc(dfnbCapacity); |
| 2956 | if (dstFileNameBuffer==NULL) |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2957 | EXM_THROW(74, "%s : not enough memory for dstFileName", |
| 2958 | strerror(errno)); |
Yann Collet | 1ab71a8 | 2018-09-28 18:19:23 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | /* return dst name == src name truncated from suffix */ |
Yann Collet | 3ca6261 | 2018-10-02 15:59:11 -0700 | [diff] [blame] | 2962 | assert(dstFileNameBuffer != NULL); |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2963 | dstFileNameEndPos = sfnSize - srcSuffixLen; |
Sen Huang | 6b81bfb | 2019-10-03 15:23:49 -0400 | [diff] [blame] | 2964 | if (outDirFilename) { |
Felix Handte | 506e1a1 | 2019-10-24 17:49:34 -0400 | [diff] [blame] | 2965 | memcpy(dstFileNameBuffer, outDirFilename, dstFileNameEndPos); |
Sen Huang | 6b81bfb | 2019-10-03 15:23:49 -0400 | [diff] [blame] | 2966 | free(outDirFilename); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2967 | } else { |
Felix Handte | 506e1a1 | 2019-10-24 17:49:34 -0400 | [diff] [blame] | 2968 | memcpy(dstFileNameBuffer, srcFileName, dstFileNameEndPos); |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2969 | } |
W. Felix Handte | 91c3f54 | 2019-10-24 20:18:57 -0400 | [diff] [blame] | 2970 | |
| 2971 | /* The short tar extensions tzst, tgz, txz and tlz4 files should have "tar" |
| 2972 | * extension on decompression. Also writes terminating null. */ |
| 2973 | strcpy(dstFileNameBuffer + dstFileNameEndPos, dstSuffix); |
Yann Collet | 1ab71a8 | 2018-09-28 18:19:23 -0700 | [diff] [blame] | 2974 | return dstFileNameBuffer; |
| 2975 | |
| 2976 | /* note : dstFileNameBuffer memory is not going to be free */ |
| 2977 | } |
| 2978 | |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2979 | int |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 2980 | FIO_decompressMultipleFilenames(FIO_ctx_t* const fCtx, |
| 2981 | FIO_prefs_t* const prefs, |
senhuang42 | b6abbc3 | 2020-08-26 11:35:07 -0400 | [diff] [blame] | 2982 | const char** srcNamesTable, |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 2983 | const char* outMirroredRootDirName, |
Sen Huang | 64bc441 | 2019-10-03 13:53:04 -0400 | [diff] [blame] | 2984 | const char* outDirName, const char* outFileName, |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2985 | const char* dictFileName) |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2986 | { |
senhuang42 | 202b295 | 2020-09-03 09:28:40 -0400 | [diff] [blame] | 2987 | int status; |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 2988 | int error = 0; |
Karl Ostmo | 5e220bf | 2019-01-22 17:31:13 -0800 | [diff] [blame] | 2989 | dRess_t ress = FIO_createDResources(prefs, dictFileName); |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 2990 | |
Nick Terrell | 4680e85 | 2017-12-12 18:32:50 -0800 | [diff] [blame] | 2991 | if (outFileName) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2992 | if (FIO_multiFilesConcatWarning(fCtx, prefs, outFileName, 1 /* displayLevelCutoff */)) { |
senhuang42 | 7e867ad | 2020-08-26 18:52:32 -0400 | [diff] [blame] | 2993 | FIO_freeDResources(ress); |
senhuang42 | 7991c55 | 2020-08-26 16:50:20 -0400 | [diff] [blame] | 2994 | return 1; |
senhuang42 | 7e867ad | 2020-08-26 18:52:32 -0400 | [diff] [blame] | 2995 | } |
Yann Collet | caf40d0 | 2019-10-17 16:58:49 -0700 | [diff] [blame] | 2996 | if (!prefs->testMode) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2997 | FILE* dstFile = FIO_openDstFile(fCtx, prefs, NULL, outFileName, DEFAULT_FILE_PERMISSIONS); |
| 2998 | if (dstFile == 0) EXM_THROW(19, "cannot open %s", outFileName); |
| 2999 | AIO_WritePool_setFile(ress.writeCtx, dstFile); |
Yann Collet | caf40d0 | 2019-10-17 16:58:49 -0700 | [diff] [blame] | 3000 | } |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 3001 | for (; fCtx->currFileIdx < fCtx->nbFilesTotal; fCtx->currFileIdx++) { |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 3002 | status = FIO_decompressSrcFile(fCtx, prefs, ress, outFileName, srcNamesTable[fCtx->currFileIdx]); |
senhuang42 | 7842f43 | 2020-09-03 09:22:07 -0400 | [diff] [blame] | 3003 | if (!status) fCtx->nbFilesProcessed++; |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 3004 | error |= status; |
senhuang42 | a3401ca | 2020-08-25 17:23:47 -0400 | [diff] [blame] | 3005 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3006 | if ((!prefs->testMode) && (AIO_WritePool_closeFile(ress.writeCtx))) |
Yann Collet | 0f2d443 | 2018-12-19 17:25:58 -0800 | [diff] [blame] | 3007 | EXM_THROW(72, "Write error : %s : cannot properly close output file", |
| 3008 | strerror(errno)); |
Yann Collet | accfd80 | 2016-02-15 19:33:16 +0100 | [diff] [blame] | 3009 | } else { |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 3010 | if (outMirroredRootDirName) |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 3011 | UTIL_mirrorSourceFilesDirectories(srcNamesTable, (unsigned)fCtx->nbFilesTotal, outMirroredRootDirName); |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 3012 | |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 3013 | for (; fCtx->currFileIdx < fCtx->nbFilesTotal; fCtx->currFileIdx++) { /* create dstFileName */ |
| 3014 | const char* const srcFileName = srcNamesTable[fCtx->currFileIdx]; |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 3015 | const char* dstFileName = NULL; |
| 3016 | if (outMirroredRootDirName) { |
| 3017 | char* validMirroredDirName = UTIL_createMirroredDestDirName(srcFileName, outMirroredRootDirName); |
| 3018 | if (validMirroredDirName) { |
| 3019 | dstFileName = FIO_determineDstName(srcFileName, validMirroredDirName); |
| 3020 | free(validMirroredDirName); |
| 3021 | } else { |
| 3022 | DISPLAYLEVEL(2, "zstd: --output-dir-mirror cannot decompress '%s' into '%s'\n", srcFileName, outMirroredRootDirName); |
| 3023 | } |
| 3024 | } else { |
| 3025 | dstFileName = FIO_determineDstName(srcFileName, outDirName); |
| 3026 | } |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 3027 | if (dstFileName == NULL) { error=1; continue; } |
senhuang42 | 5123496 | 2020-09-07 13:13:05 -0400 | [diff] [blame] | 3028 | status = FIO_decompressSrcFile(fCtx, prefs, ress, dstFileName, srcFileName); |
senhuang42 | 7842f43 | 2020-09-03 09:22:07 -0400 | [diff] [blame] | 3029 | if (!status) fCtx->nbFilesProcessed++; |
senhuang42 | a6414f1 | 2020-09-01 12:32:18 -0400 | [diff] [blame] | 3030 | error |= status; |
Yann Collet | 8b23eea | 2016-05-10 05:37:43 +0200 | [diff] [blame] | 3031 | } |
Sen Huang | c5ebb37 | 2019-10-09 09:39:52 -0400 | [diff] [blame] | 3032 | if (outDirName) |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 3033 | FIO_checkFilenameCollisions(srcNamesTable , (unsigned)fCtx->nbFilesTotal); |
Yann Collet | 8b23eea | 2016-05-10 05:37:43 +0200 | [diff] [blame] | 3034 | } |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 3035 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3036 | if (FIO_shouldDisplayMultipleFileSummary(fCtx)) { |
| 3037 | DISPLAY_PROGRESS("\r%79s\r", ""); |
| 3038 | DISPLAY_SUMMARY("%d files decompressed : %6llu bytes total \n", |
| 3039 | fCtx->nbFilesProcessed, (unsigned long long)fCtx->totalBytesOutput); |
| 3040 | } |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 3041 | |
| 3042 | FIO_freeDResources(ress); |
Yann Collet | c7bd6a4 | 2018-10-01 14:04:00 -0700 | [diff] [blame] | 3043 | return error; |
Yann Collet | deb078b | 2015-12-17 20:30:14 +0100 | [diff] [blame] | 3044 | } |
Yann Collet | accfd80 | 2016-02-15 19:33:16 +0100 | [diff] [blame] | 3045 | |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3046 | /* ************************************************************************** |
| 3047 | * .zst file info (--list command) |
| 3048 | ***************************************************************************/ |
| 3049 | |
| 3050 | typedef struct { |
Nick Terrell | 6dd958e | 2017-10-04 12:23:23 -0700 | [diff] [blame] | 3051 | U64 decompressedSize; |
| 3052 | U64 compressedSize; |
| 3053 | U64 windowSize; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3054 | int numActualFrames; |
| 3055 | int numSkippableFrames; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3056 | int decompUnavailable; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3057 | int usesCheck; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3058 | BYTE checksum[4]; |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3059 | U32 nbFiles; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3060 | unsigned dictID; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3061 | } fileInfo_t; |
| 3062 | |
Karl Ostmo | 4fa585a | 2019-01-14 17:58:46 -0800 | [diff] [blame] | 3063 | typedef enum { |
| 3064 | info_success=0, |
| 3065 | info_frame_error=1, |
| 3066 | info_not_zstd=2, |
| 3067 | info_file_error=3, |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3068 | info_truncated_input=4 |
Karl Ostmo | 4fa585a | 2019-01-14 17:58:46 -0800 | [diff] [blame] | 3069 | } InfoError; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3070 | |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3071 | #define ERROR_IF(c,n,...) { \ |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3072 | if (c) { \ |
| 3073 | DISPLAYLEVEL(1, __VA_ARGS__); \ |
| 3074 | DISPLAYLEVEL(1, " \n"); \ |
| 3075 | return n; \ |
| 3076 | } \ |
| 3077 | } |
| 3078 | |
| 3079 | static InfoError |
| 3080 | FIO_analyzeFrames(fileInfo_t* info, FILE* const srcFile) |
| 3081 | { |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3082 | /* begin analyzing frame */ |
| 3083 | for ( ; ; ) { |
| 3084 | BYTE headerBuffer[ZSTD_FRAMEHEADERSIZE_MAX]; |
| 3085 | size_t const numBytesRead = fread(headerBuffer, 1, sizeof(headerBuffer), srcFile); |
Nick Terrell | b1ec94e | 2019-10-21 19:42:14 -0700 | [diff] [blame] | 3086 | if (numBytesRead < ZSTD_FRAMEHEADERSIZE_MIN(ZSTD_f_zstd1)) { |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3087 | if ( feof(srcFile) |
| 3088 | && (numBytesRead == 0) |
Yann Collet | 18b7953 | 2017-10-17 16:14:25 -0700 | [diff] [blame] | 3089 | && (info->compressedSize > 0) |
| 3090 | && (info->compressedSize != UTIL_FILESIZE_UNKNOWN) ) { |
Karl Ostmo | 4fa585a | 2019-01-14 17:58:46 -0800 | [diff] [blame] | 3091 | unsigned long long file_position = (unsigned long long) LONG_TELL(srcFile); |
| 3092 | unsigned long long file_size = (unsigned long long) info->compressedSize; |
| 3093 | ERROR_IF(file_position != file_size, info_truncated_input, |
| 3094 | "Error: seeked to position %llu, which is beyond file size of %llu\n", |
| 3095 | file_position, |
| 3096 | file_size); |
Yann Collet | d987ab5 | 2018-09-28 09:34:16 -0700 | [diff] [blame] | 3097 | break; /* correct end of file => success */ |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3098 | } |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3099 | ERROR_IF(feof(srcFile), info_not_zstd, "Error: reached end of file with incomplete frame"); |
| 3100 | ERROR_IF(1, info_frame_error, "Error: did not reach end of file but ran out of frames"); |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3101 | } |
| 3102 | { U32 const magicNumber = MEM_readLE32(headerBuffer); |
| 3103 | /* Zstandard frame */ |
| 3104 | if (magicNumber == ZSTD_MAGICNUMBER) { |
Nick Terrell | 6dd958e | 2017-10-04 12:23:23 -0700 | [diff] [blame] | 3105 | ZSTD_frameHeader header; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3106 | U64 const frameContentSize = ZSTD_getFrameContentSize(headerBuffer, numBytesRead); |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3107 | if ( frameContentSize == ZSTD_CONTENTSIZE_ERROR |
| 3108 | || frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN ) { |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3109 | info->decompUnavailable = 1; |
| 3110 | } else { |
| 3111 | info->decompressedSize += frameContentSize; |
| 3112 | } |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3113 | ERROR_IF(ZSTD_getFrameHeader(&header, headerBuffer, numBytesRead) != 0, |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3114 | info_frame_error, "Error: could not decode frame header"); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3115 | if (info->dictID != 0 && info->dictID != header.dictID) { |
| 3116 | DISPLAY("WARNING: File contains multiple frames with different dictionary IDs. Showing dictID 0 instead"); |
| 3117 | info->dictID = 0; |
| 3118 | } else { |
| 3119 | info->dictID = header.dictID; |
| 3120 | } |
Nick Terrell | 6dd958e | 2017-10-04 12:23:23 -0700 | [diff] [blame] | 3121 | info->windowSize = header.windowSize; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3122 | /* move to the end of the frame header */ |
| 3123 | { size_t const headerSize = ZSTD_frameHeaderSize(headerBuffer, numBytesRead); |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3124 | ERROR_IF(ZSTD_isError(headerSize), info_frame_error, "Error: could not determine frame header size"); |
| 3125 | ERROR_IF(fseek(srcFile, ((long)headerSize)-((long)numBytesRead), SEEK_CUR) != 0, |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3126 | info_frame_error, "Error: could not move to end of frame header"); |
| 3127 | } |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3128 | |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3129 | /* skip all blocks in the frame */ |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3130 | { int lastBlock = 0; |
| 3131 | do { |
| 3132 | BYTE blockHeaderBuffer[3]; |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3133 | ERROR_IF(fread(blockHeaderBuffer, 1, 3, srcFile) != 3, |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3134 | info_frame_error, "Error while reading block header"); |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3135 | { U32 const blockHeader = MEM_readLE24(blockHeaderBuffer); |
| 3136 | U32 const blockTypeID = (blockHeader >> 1) & 3; |
| 3137 | U32 const isRLE = (blockTypeID == 1); |
| 3138 | U32 const isWrongBlock = (blockTypeID == 3); |
| 3139 | long const blockSize = isRLE ? 1 : (long)(blockHeader >> 3); |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3140 | ERROR_IF(isWrongBlock, info_frame_error, "Error: unsupported block type"); |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3141 | lastBlock = blockHeader & 1; |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3142 | ERROR_IF(fseek(srcFile, blockSize, SEEK_CUR) != 0, |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3143 | info_frame_error, "Error: could not skip to end of block"); |
| 3144 | } |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3145 | } while (lastBlock != 1); |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | /* check if checksum is used */ |
| 3149 | { BYTE const frameHeaderDescriptor = headerBuffer[4]; |
| 3150 | int const contentChecksumFlag = (frameHeaderDescriptor & (1 << 2)) >> 2; |
| 3151 | if (contentChecksumFlag) { |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3152 | info->usesCheck = 1; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3153 | ERROR_IF(fread(info->checksum, 1, 4, srcFile) != 4, |
| 3154 | info_frame_error, "Error: could not read checksum"); |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3155 | } } |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3156 | info->numActualFrames++; |
| 3157 | } |
| 3158 | /* Skippable frame */ |
Yann Collet | 2c8fde5 | 2018-11-13 17:36:35 -0800 | [diff] [blame] | 3159 | else if ((magicNumber & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START) { |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3160 | U32 const frameSize = MEM_readLE32(headerBuffer + 4); |
| 3161 | long const seek = (long)(8 + frameSize - numBytesRead); |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3162 | ERROR_IF(LONG_SEEK(srcFile, seek, SEEK_CUR) != 0, |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3163 | info_frame_error, "Error: could not find end of skippable frame"); |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3164 | info->numSkippableFrames++; |
| 3165 | } |
| 3166 | /* unknown content */ |
| 3167 | else { |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3168 | return info_not_zstd; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3169 | } |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3170 | } /* magic number analysis */ |
| 3171 | } /* end analyzing frames */ |
| 3172 | return info_success; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3173 | } |
| 3174 | |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3175 | |
| 3176 | static InfoError |
| 3177 | getFileInfo_fileConfirmed(fileInfo_t* info, const char* inFileName) |
Yann Collet | 18b7953 | 2017-10-17 16:14:25 -0700 | [diff] [blame] | 3178 | { |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3179 | InfoError status; |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3180 | stat_t srcFileStat; |
| 3181 | FILE* const srcFile = FIO_openSrcFile(NULL, inFileName, &srcFileStat); |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3182 | ERROR_IF(srcFile == NULL, info_file_error, "Error: could not open source file %s", inFileName); |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3183 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3184 | info->compressedSize = UTIL_getFileSizeStat(&srcFileStat); |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3185 | status = FIO_analyzeFrames(info, srcFile); |
| 3186 | |
| 3187 | fclose(srcFile); |
| 3188 | info->nbFiles = 1; |
| 3189 | return status; |
| 3190 | } |
| 3191 | |
| 3192 | |
| 3193 | /** getFileInfo() : |
| 3194 | * Reads information from file, stores in *info |
| 3195 | * @return : InfoError status |
| 3196 | */ |
| 3197 | static InfoError |
| 3198 | getFileInfo(fileInfo_t* info, const char* srcFileName) |
| 3199 | { |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3200 | ERROR_IF(!UTIL_isRegularFile(srcFileName), |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3201 | info_file_error, "Error : %s is not a file", srcFileName); |
Yann Collet | 18b7953 | 2017-10-17 16:14:25 -0700 | [diff] [blame] | 3202 | return getFileInfo_fileConfirmed(info, srcFileName); |
| 3203 | } |
| 3204 | |
| 3205 | |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3206 | static void |
| 3207 | displayInfo(const char* inFileName, const fileInfo_t* info, int displayLevel) |
| 3208 | { |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3209 | UTIL_HumanReadableSize_t const window_hrs = UTIL_makeHumanReadableSize(info->windowSize); |
| 3210 | UTIL_HumanReadableSize_t const compressed_hrs = UTIL_makeHumanReadableSize(info->compressedSize); |
| 3211 | UTIL_HumanReadableSize_t const decompressed_hrs = UTIL_makeHumanReadableSize(info->decompressedSize); |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 3212 | double const ratio = (info->compressedSize == 0) ? 0 : ((double)info->decompressedSize)/(double)info->compressedSize; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3213 | const char* const checkString = (info->usesCheck ? "XXH64" : "None"); |
| 3214 | if (displayLevel <= 2) { |
| 3215 | if (!info->decompUnavailable) { |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3216 | DISPLAYOUT("%6d %5d %6.*f%4s %8.*f%4s %5.3f %5s %s\n", |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3217 | info->numSkippableFrames + info->numActualFrames, |
| 3218 | info->numSkippableFrames, |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3219 | compressed_hrs.precision, compressed_hrs.value, compressed_hrs.suffix, |
| 3220 | decompressed_hrs.precision, decompressed_hrs.value, decompressed_hrs.suffix, |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3221 | ratio, checkString, inFileName); |
| 3222 | } else { |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3223 | DISPLAYOUT("%6d %5d %6.*f%4s %5s %s\n", |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3224 | info->numSkippableFrames + info->numActualFrames, |
| 3225 | info->numSkippableFrames, |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3226 | compressed_hrs.precision, compressed_hrs.value, compressed_hrs.suffix, |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3227 | checkString, inFileName); |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3228 | } |
| 3229 | } else { |
Yann Collet | 9ef32b3 | 2017-10-14 00:02:32 -0700 | [diff] [blame] | 3230 | DISPLAYOUT("%s \n", inFileName); |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3231 | DISPLAYOUT("# Zstandard Frames: %d\n", info->numActualFrames); |
Yann Collet | 9ef32b3 | 2017-10-14 00:02:32 -0700 | [diff] [blame] | 3232 | if (info->numSkippableFrames) |
| 3233 | DISPLAYOUT("# Skippable Frames: %d\n", info->numSkippableFrames); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3234 | DISPLAYOUT("DictID: %u\n", info->dictID); |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3235 | DISPLAYOUT("Window Size: %.*f%s (%llu B)\n", |
| 3236 | window_hrs.precision, window_hrs.value, window_hrs.suffix, |
Nick Terrell | 6dd958e | 2017-10-04 12:23:23 -0700 | [diff] [blame] | 3237 | (unsigned long long)info->windowSize); |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3238 | DISPLAYOUT("Compressed Size: %.*f%s (%llu B)\n", |
| 3239 | compressed_hrs.precision, compressed_hrs.value, compressed_hrs.suffix, |
Yann Collet | 3095ca8 | 2017-09-26 13:53:50 -0700 | [diff] [blame] | 3240 | (unsigned long long)info->compressedSize); |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3241 | if (!info->decompUnavailable) { |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3242 | DISPLAYOUT("Decompressed Size: %.*f%s (%llu B)\n", |
| 3243 | decompressed_hrs.precision, decompressed_hrs.value, decompressed_hrs.suffix, |
Yann Collet | 3095ca8 | 2017-09-26 13:53:50 -0700 | [diff] [blame] | 3244 | (unsigned long long)info->decompressedSize); |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3245 | DISPLAYOUT("Ratio: %.4f\n", ratio); |
| 3246 | } |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3247 | |
| 3248 | if (info->usesCheck && info->numActualFrames == 1) { |
| 3249 | DISPLAYOUT("Check: %s %02x%02x%02x%02x\n", checkString, |
| 3250 | info->checksum[3], info->checksum[2], |
| 3251 | info->checksum[1], info->checksum[0] |
| 3252 | ); |
| 3253 | } else { |
| 3254 | DISPLAYOUT("Check: %s\n", checkString); |
| 3255 | } |
| 3256 | |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3257 | DISPLAYOUT("\n"); |
| 3258 | } |
| 3259 | } |
| 3260 | |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3261 | static fileInfo_t FIO_addFInfo(fileInfo_t fi1, fileInfo_t fi2) |
| 3262 | { |
| 3263 | fileInfo_t total; |
Conrad Meyer | 6063742 | 2018-02-28 14:16:30 -0800 | [diff] [blame] | 3264 | memset(&total, 0, sizeof(total)); |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3265 | total.numActualFrames = fi1.numActualFrames + fi2.numActualFrames; |
| 3266 | total.numSkippableFrames = fi1.numSkippableFrames + fi2.numSkippableFrames; |
| 3267 | total.compressedSize = fi1.compressedSize + fi2.compressedSize; |
| 3268 | total.decompressedSize = fi1.decompressedSize + fi2.decompressedSize; |
| 3269 | total.decompUnavailable = fi1.decompUnavailable | fi2.decompUnavailable; |
| 3270 | total.usesCheck = fi1.usesCheck & fi2.usesCheck; |
| 3271 | total.nbFiles = fi1.nbFiles + fi2.nbFiles; |
| 3272 | return total; |
| 3273 | } |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3274 | |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3275 | static int |
| 3276 | FIO_listFile(fileInfo_t* total, const char* inFileName, int displayLevel) |
| 3277 | { |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3278 | fileInfo_t info; |
| 3279 | memset(&info, 0, sizeof(info)); |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3280 | { InfoError const error = getFileInfo(&info, inFileName); |
Karl Ostmo | 4fa585a | 2019-01-14 17:58:46 -0800 | [diff] [blame] | 3281 | switch (error) { |
| 3282 | case info_frame_error: |
| 3283 | /* display error, but provide output */ |
| 3284 | DISPLAYLEVEL(1, "Error while parsing \"%s\" \n", inFileName); |
| 3285 | break; |
| 3286 | case info_not_zstd: |
| 3287 | DISPLAYOUT("File \"%s\" not compressed by zstd \n", inFileName); |
| 3288 | if (displayLevel > 2) DISPLAYOUT("\n"); |
| 3289 | return 1; |
| 3290 | case info_file_error: |
| 3291 | /* error occurred while opening the file */ |
| 3292 | if (displayLevel > 2) DISPLAYOUT("\n"); |
| 3293 | return 1; |
| 3294 | case info_truncated_input: |
| 3295 | DISPLAYOUT("File \"%s\" is truncated \n", inFileName); |
| 3296 | if (displayLevel > 2) DISPLAYOUT("\n"); |
| 3297 | return 1; |
| 3298 | case info_success: |
| 3299 | default: |
| 3300 | break; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3301 | } |
Karl Ostmo | 4fa585a | 2019-01-14 17:58:46 -0800 | [diff] [blame] | 3302 | |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3303 | displayInfo(inFileName, &info, displayLevel); |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3304 | *total = FIO_addFInfo(*total, info); |
Yann Collet | 0ed8ee4 | 2018-12-20 14:46:23 -0800 | [diff] [blame] | 3305 | assert(error == info_success || error == info_frame_error); |
Yann Collet | 458a1a1 | 2020-04-13 10:13:29 -0700 | [diff] [blame] | 3306 | return (int)error; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3307 | } |
| 3308 | } |
| 3309 | |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3310 | int FIO_listMultipleFiles(unsigned numFiles, const char** filenameTable, int displayLevel) |
| 3311 | { |
| 3312 | /* ensure no specified input is stdin (needs fseek() capability) */ |
| 3313 | { unsigned u; |
| 3314 | for (u=0; u<numFiles;u++) { |
Yann Collet | ec1cb8e | 2018-09-28 16:04:00 -0700 | [diff] [blame] | 3315 | ERROR_IF(!strcmp (filenameTable[u], stdinmark), |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3316 | 1, "zstd: --list does not support reading from standard input"); |
| 3317 | } } |
Topher Lubaway | 4c16608 | 2018-06-11 10:13:00 -0700 | [diff] [blame] | 3318 | |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3319 | if (numFiles == 0) { |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 3320 | if (!UTIL_isConsole(stdin)) { |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3321 | DISPLAYLEVEL(1, "zstd: --list does not support reading from standard input \n"); |
W. Felix Handte | 712a9fd | 2018-06-29 15:33:44 -0400 | [diff] [blame] | 3322 | } |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3323 | DISPLAYLEVEL(1, "No files given \n"); |
W. Felix Handte | 712a9fd | 2018-06-29 15:33:44 -0400 | [diff] [blame] | 3324 | return 1; |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3325 | } |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3326 | |
Nick Terrell | 6dd958e | 2017-10-04 12:23:23 -0700 | [diff] [blame] | 3327 | if (displayLevel <= 2) { |
| 3328 | DISPLAYOUT("Frames Skips Compressed Uncompressed Ratio Check Filename\n"); |
| 3329 | } |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3330 | { int error = 0; |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3331 | fileInfo_t total; |
| 3332 | memset(&total, 0, sizeof(total)); |
| 3333 | total.usesCheck = 1; |
Yann Collet | 9b45db7 | 2018-09-27 16:49:08 -0700 | [diff] [blame] | 3334 | /* --list each file, and check for any error */ |
| 3335 | { unsigned u; |
| 3336 | for (u=0; u<numFiles;u++) { |
| 3337 | error |= FIO_listFile(&total, filenameTable[u], displayLevel); |
| 3338 | } } |
Yann Collet | 9ef32b3 | 2017-10-14 00:02:32 -0700 | [diff] [blame] | 3339 | if (numFiles > 1 && displayLevel <= 2) { /* display total */ |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3340 | UTIL_HumanReadableSize_t const compressed_hrs = UTIL_makeHumanReadableSize(total.compressedSize); |
| 3341 | UTIL_HumanReadableSize_t const decompressed_hrs = UTIL_makeHumanReadableSize(total.decompressedSize); |
Yann Collet | 0d793a6 | 2021-01-06 01:35:52 -0800 | [diff] [blame] | 3342 | double const ratio = (total.compressedSize == 0) ? 0 : ((double)total.decompressedSize)/(double)total.compressedSize; |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3343 | const char* const checkString = (total.usesCheck ? "XXH64" : ""); |
| 3344 | DISPLAYOUT("----------------------------------------------------------------- \n"); |
| 3345 | if (total.decompUnavailable) { |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3346 | DISPLAYOUT("%6d %5d %6.*f%4s %5s %u files\n", |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3347 | total.numSkippableFrames + total.numActualFrames, |
| 3348 | total.numSkippableFrames, |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3349 | compressed_hrs.precision, compressed_hrs.value, compressed_hrs.suffix, |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 3350 | checkString, (unsigned)total.nbFiles); |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3351 | } else { |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3352 | DISPLAYOUT("%6d %5d %6.*f%4s %8.*f%4s %5.3f %5s %u files\n", |
Yann Collet | 56f1f0e | 2017-09-26 11:21:36 -0700 | [diff] [blame] | 3353 | total.numSkippableFrames + total.numActualFrames, |
| 3354 | total.numSkippableFrames, |
W. Felix Handte | 87e94e3 | 2021-06-10 12:31:42 -0400 | [diff] [blame] | 3355 | compressed_hrs.precision, compressed_hrs.value, compressed_hrs.suffix, |
| 3356 | decompressed_hrs.precision, decompressed_hrs.value, decompressed_hrs.suffix, |
Yann Collet | ededcfc | 2018-12-21 16:19:44 -0800 | [diff] [blame] | 3357 | ratio, checkString, (unsigned)total.nbFiles); |
Yann Collet | 9ef32b3 | 2017-10-14 00:02:32 -0700 | [diff] [blame] | 3358 | } } |
Yann Collet | 166645e | 2017-08-18 18:30:41 -0700 | [diff] [blame] | 3359 | return error; |
| 3360 | } |
| 3361 | } |
| 3362 | |
| 3363 | |
Yann Collet | 8b23eea | 2016-05-10 05:37:43 +0200 | [diff] [blame] | 3364 | #endif /* #ifndef ZSTD_NODECOMPRESS */ |