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