2017-08-19 07:52:05 +08:00
/*
2021-03-30 05:23:36 +08:00
* Copyright ( c ) Yann Collet , Facebook , Inc .
2016-08-31 01:04:33 +08:00
* All rights reserved .
*
2017-08-19 07:52:05 +08:00
* This source code is licensed under both the BSD - style license ( found in the
* LICENSE file in the root directory of this source tree ) and the GPLv2 ( found
* in the COPYING file in the root directory of this source tree ) .
2017-09-08 15:09:23 +08:00
* You may select , at your option , one of the above - listed licenses .
2016-08-31 01:04:33 +08:00
*/
2016-08-29 01:00:49 +08:00
2016-05-29 11:16:05 +08:00
2016-07-13 20:15:08 +08:00
/*-************************************
* Tuning parameters
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2016-07-13 21:16:00 +08:00
# ifndef ZSTDCLI_CLEVEL_DEFAULT
# define ZSTDCLI_CLEVEL_DEFAULT 3
2016-07-13 20:15:08 +08:00
# endif
2016-08-13 00:04:15 +08:00
# ifndef ZSTDCLI_CLEVEL_MAX
2017-08-20 04:33:50 +08:00
# define ZSTDCLI_CLEVEL_MAX 19 /* without using --ultra */
2016-08-13 00:04:15 +08:00
# endif
2020-09-10 00:35:40 +08:00
# ifndef ZSTDCLI_NBTHREADS_DEFAULT
# define ZSTDCLI_NBTHREADS_DEFAULT 1
2020-09-08 06:11:42 +08:00
# endif
2017-01-26 09:01:13 +08:00
2016-05-29 11:16:05 +08:00
/*-************************************
2016-11-17 00:50:54 +08:00
* Dependencies
2016-05-29 11:16:05 +08:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2022-01-15 04:37:32 +08:00
# include "platform.h" /* PLATFORM_POSIX_VERSION */
# include "util.h" /* UTIL_HAS_CREATEFILELIST, UTIL_createFileList, UTIL_isConsole */
2018-12-20 05:26:27 +08:00
# include <stdlib.h> /* getenv */
2016-05-29 11:16:05 +08:00
# include <string.h> /* strcmp, strlen */
2019-12-03 06:28:18 +08:00
# include <stdio.h> /* fprintf(), stdin, stdout, stderr */
2016-07-05 00:16:16 +08:00
# include <errno.h> /* errno */
2019-12-03 06:28:18 +08:00
# include <assert.h> /* assert */
2017-08-20 04:33:50 +08:00
# include "fileio.h" /* stdinmark, stdoutmark, ZSTD_EXTENSION */
2016-05-29 11:16:05 +08:00
# ifndef ZSTD_NOBENCH
2018-11-14 03:01:59 +08:00
# include "benchzstd.h" /* BMK_benchFiles */
2016-05-29 11:16:05 +08:00
# endif
# ifndef ZSTD_NODICT
2017-08-20 04:33:50 +08:00
# include "dibio.h" /* ZDICT_cover_params_t, DiB_trainFromFiles() */
2016-05-29 11:16:05 +08:00
# endif
2021-02-04 11:53:00 +08:00
# ifndef ZSTD_NOTRACE
# include "zstdcli_trace.h"
# endif
2020-05-02 04:20:40 +08:00
# include "../lib/zstd.h" /* ZSTD_VERSION_STRING, ZSTD_minCLevel, ZSTD_maxCLevel */
2022-02-01 07:43:41 +08:00
# include "fileio_asyncio.h"
2016-05-29 11:16:05 +08:00
/*-************************************
* Constants
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# define COMPRESSOR_NAME "zstd command line interface"
# ifndef ZSTD_VERSION
# define ZSTD_VERSION "v" ZSTD_VERSION_STRING
# endif
# define AUTHOR "Yann Collet"
# define WELCOME_MESSAGE "*** %s %i-bits %s, by %s ***\n", COMPRESSOR_NAME, (int)(sizeof(size_t)*8), ZSTD_VERSION, AUTHOR
2017-04-15 02:33:04 +08:00
# define ZSTD_ZSTDMT "zstdmt"
2016-05-29 11:16:05 +08:00
# define ZSTD_UNZSTD "unzstd"
2017-02-15 02:45:19 +08:00
# define ZSTD_CAT "zstdcat"
2018-01-20 03:26:35 +08:00
# define ZSTD_ZCAT "zcat"
2017-02-15 02:45:19 +08:00
# define ZSTD_GZ "gzip"
# define ZSTD_GUNZIP "gunzip"
# define ZSTD_GZCAT "gzcat"
2017-03-14 09:11:07 +08:00
# define ZSTD_LZMA "lzma"
2017-06-27 02:24:36 +08:00
# define ZSTD_UNLZMA "unlzma"
2017-03-14 09:11:07 +08:00
# define ZSTD_XZ "xz"
2017-06-27 02:24:36 +08:00
# define ZSTD_UNXZ "unxz"
2017-09-29 07:18:15 +08:00
# define ZSTD_LZ4 "lz4"
# define ZSTD_UNLZ4 "unlz4"
2016-05-29 11:16:05 +08:00
# define KB *(1 <<10)
# define MB *(1 <<20)
# define GB *(1U<<30)
2017-08-20 04:33:50 +08:00
# define DISPLAY_LEVEL_DEFAULT 2
2016-08-13 05:49:05 +08:00
2016-05-29 11:16:05 +08:00
static const char * g_defaultDictName = " dictionary " ;
static const unsigned g_defaultMaxDictSize = 110 KB ;
2016-09-02 06:05:57 +08:00
static const int g_defaultDictCLevel = 3 ;
2016-05-29 11:16:05 +08:00
static const unsigned g_defaultSelectivityLevel = 9 ;
2017-09-23 05:04:39 +08:00
static const unsigned g_defaultMaxWindowLog = 27 ;
2017-01-31 06:37:08 +08:00
# define OVERLAP_LOG_DEFAULT 9999
2017-09-03 12:10:36 +08:00
# define LDM_PARAM_DEFAULT 9999 /* Default for parameters where 0 is valid */
2017-01-31 06:37:08 +08:00
static U32 g_overlapLog = OVERLAP_LOG_DEFAULT ;
2017-09-02 05:52:51 +08:00
static U32 g_ldmHashLog = 0 ;
static U32 g_ldmMinMatch = 0 ;
2018-11-22 06:36:57 +08:00
static U32 g_ldmHashRateLog = LDM_PARAM_DEFAULT ;
2017-09-03 12:10:36 +08:00
static U32 g_ldmBucketSizeLog = LDM_PARAM_DEFAULT ;
2016-05-29 11:16:05 +08:00
2018-08-24 03:06:20 +08:00
# define DEFAULT_ACCEL 1
typedef enum { cover , fastCover , legacy } dictType ;
2016-05-29 11:16:05 +08:00
/*-************************************
* Display Macros
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2019-12-03 08:08:08 +08:00
# define DISPLAY_F(f, ...) fprintf((f), __VA_ARGS__)
# define DISPLAYOUT(...) DISPLAY_F(stdout, __VA_ARGS__)
# define DISPLAY(...) DISPLAY_F(stderr, __VA_ARGS__)
2017-03-24 02:13:52 +08:00
# define DISPLAYLEVEL(l, ...) { if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); } }
2017-08-20 04:33:50 +08:00
static int g_displayLevel = DISPLAY_LEVEL_DEFAULT ; /* 0 : no display, 1: errors, 2 : + result + interaction + warnings, 3 : + progression, 4 : + information */
2016-05-29 11:16:05 +08:00
2021-01-08 02:37:27 +08:00
/*-************************************
* Check Version ( when CLI linked to dynamic library )
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* Due to usage of experimental symbols and capabilities by the CLI,
* the CLI must be linked against a dynamic library of same version */
static void checkLibVersion ( void )
{
if ( strcmp ( ZSTD_VERSION_STRING , ZSTD_versionString ( ) ) ) {
DISPLAYLEVEL ( 1 , " Error : incorrect library version (expecting : %s ; actual : %s ) \n " ,
ZSTD_VERSION_STRING , ZSTD_versionString ( ) ) ;
DISPLAYLEVEL ( 1 , " Please update library to version %s, or use stand-alone zstd binary \n " ,
ZSTD_VERSION_STRING ) ;
exit ( 1 ) ;
}
}
2022-02-09 04:32:42 +08:00
/*! exeNameMatch() :
@ return : a non - zero value if exeName matches test , excluding the extension
*/
static int exeNameMatch ( const char * exeName , const char * test )
{
return ! strncmp ( exeName , test , strlen ( test ) ) & &
( exeName [ strlen ( test ) ] = = ' \0 ' | | exeName [ strlen ( test ) ] = = ' . ' ) ;
}
2016-05-29 11:16:05 +08:00
/*-************************************
* Command Line
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2019-12-03 08:08:08 +08:00
/* print help either in `stderr` or `stdout` depending on originating request
* error ( badusage ) = > stderr
* help ( usage_advanced ) = > stdout
*/
static void usage ( FILE * f , const char * programName )
2016-05-29 11:16:05 +08:00
{
2022-12-21 04:34:02 +08:00
DISPLAY_F ( f , " Compress or decompress the INPUT file(s); reads from STDIN if INPUT is `-` or not provided. \n \n " ) ;
DISPLAY_F ( f , " Usage: %s [OPTIONS...] [INPUT... | -] [-o OUTPUT] \n \n " , programName ) ;
DISPLAY_F ( f , " Options: \n " ) ;
DISPLAY_F ( f , " -o OUTPUT Write output to a single file, OUTPUT. \n " ) ;
DISPLAY_F ( f , " -k, --keep Preserve INPUT file(s). [Default] \n " ) ;
DISPLAY_F ( f , " --rm Remove INPUT file(s) after successful (de)compression. \n " ) ;
2022-02-09 04:32:42 +08:00
# ifdef ZSTD_GZCOMPRESS
if ( exeNameMatch ( programName , ZSTD_GZ ) ) { /* behave like gzip */
2022-12-21 04:34:02 +08:00
DISPLAY_F ( f , " -n, --no-name Do not store original filename when compressing. \n \n " ) ;
2022-02-09 04:32:42 +08:00
}
# endif
2022-12-21 04:34:02 +08:00
DISPLAY_F ( f , " \n " ) ;
# ifndef ZSTD_NOCOMPRESS
DISPLAY_F ( f , " -# Desired compression level, where `#` is a number between 1 and %d; \n " , ZSTDCLI_CLEVEL_MAX ) ;
DISPLAY_F ( f , " lower numbers provide faster compression, higher numbers yield \n " ) ;
DISPLAY_F ( f , " better compression ratios. [Default: %d] \n \n " , ZSTDCLI_CLEVEL_DEFAULT ) ;
# endif
# ifndef ZSTD_NODECOMPRESS
DISPLAY_F ( f , " -d, --decompress Perform decompression. \n " ) ;
# endif
DISPLAY_F ( f , " -D DICT Use DICT as the dictionary for compression or decompression. \n \n " ) ;
DISPLAY_F ( f , " -f, --force Disable input and output checks. Allows overwriting existing files, \n " ) ;
DISPLAY_F ( f , " receiving input from the console, printing ouput to STDOUT, and \n " ) ;
DISPLAY_F ( f , " operating on links, block devices, etc. Unrecognized formats will be \n " ) ;
DISPLAY_F ( f , " passed-through through as-is. \n \n " ) ;
DISPLAY_F ( f , " -h Display short usage and exit. \n " ) ;
DISPLAY_F ( f , " -H, --help Display full help and exit. \n " ) ;
DISPLAY_F ( f , " -V, --version Display the program version and exit. \n " ) ;
2022-03-10 08:34:12 +08:00
DISPLAY_F ( f , " \n " ) ;
2016-05-29 11:16:05 +08:00
}
2019-12-03 08:08:08 +08:00
static void usage_advanced ( const char * programName )
2016-05-29 11:16:05 +08:00
{
2019-12-03 08:08:08 +08:00
DISPLAYOUT ( WELCOME_MESSAGE ) ;
usage ( stdout , programName ) ;
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " Advanced options: \n " ) ;
DISPLAYOUT ( " -c, --stdout Write to STDOUT (even if it is a console) and keep the INPUT file(s). \n \n " ) ;
2020-05-09 05:14:46 +08:00
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " -v, --verbose Enable verbose output; pass multiple times to increase verbosity. \n " ) ;
DISPLAYOUT ( " -q, --quiet Suppress warnings; pass twice to suppress errors. \n " ) ;
# ifndef ZSTD_NOTRACE
DISPLAYOUT ( " --trace LOG Log tracing information to LOG. \n " ) ;
# endif
DISPLAYOUT ( " \n " ) ;
DISPLAYOUT ( " --[no-]progress Forcibly show/hide the progress counter. NOTE: Any (de)compressed \n " ) ;
DISPLAYOUT ( " output to terminal will mix with progress counter text. \n \n " ) ;
2020-05-09 05:14:46 +08:00
# ifdef UTIL_HAS_CREATEFILELIST
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " -r Operate recursively on directories. \n " ) ;
DISPLAYOUT ( " --filelist LIST Read a list of files to operate on from LIST. \n " ) ;
DISPLAYOUT ( " --output-dir-flat DIR Store processed files in DIR. \n " ) ;
2020-05-09 05:14:46 +08:00
# endif
2020-06-20 10:35:51 +08:00
# ifdef UTIL_HAS_MIRRORFILELIST
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --output-dir-mirror DIR Store processed files in DIR, respecting original directory structure. \n " ) ;
2020-06-20 10:35:51 +08:00
# endif
2022-02-01 07:43:41 +08:00
if ( AIO_supported ( ) )
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --[no-]asyncio Use asynchronous IO. [Default: Enabled] \n " ) ;
2020-08-25 04:14:19 +08:00
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " \n " ) ;
2020-08-25 04:14:19 +08:00
# ifndef ZSTD_NOCOMPRESS
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --[no-]check Add XXH64 integrity checksums during compression. [Default: Add, Validate] \n " ) ;
2020-08-25 04:14:19 +08:00
# ifndef ZSTD_NODECOMPRESS
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " If `-d` is present, ignore/validate checksums during decompression. \n " ) ;
2020-08-25 04:14:19 +08:00
# endif
2020-08-25 05:36:36 +08:00
# else
2020-08-25 04:14:19 +08:00
# ifdef ZSTD_NOCOMPRESS
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --[no-]check Ignore/validate checksums during decompression. [Default: Validate] " ) ;
2020-08-25 04:14:19 +08:00
# endif
2020-08-25 05:36:36 +08:00
# endif /* ZSTD_NOCOMPRESS */
2021-02-04 11:53:00 +08:00
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " \n " ) ;
DISPLAYOUT ( " -- Treat remaining arguments after `--` as files. \n " ) ;
2020-05-09 05:14:46 +08:00
2016-05-29 11:16:05 +08:00
# ifndef ZSTD_NOCOMPRESS
2022-03-10 08:34:12 +08:00
DISPLAYOUT ( " \n " ) ;
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " Advanced compression options: \n " ) ;
DISPLAYOUT ( " --ultra Enable levels beyond %i, up to %i; requires more memory. \n " , ZSTDCLI_CLEVEL_MAX , ZSTD_maxCLevel ( ) ) ;
DISPLAYOUT ( " --fast[=#] Use to very fast compression levels. [Default: %u] \n " , 1 ) ;
2022-02-09 04:32:42 +08:00
# ifdef ZSTD_GZCOMPRESS
if ( exeNameMatch ( programName , ZSTD_GZ ) ) { /* behave like gzip */
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --best Compatibility alias for `-9`. \n " ) ;
2022-02-09 04:32:42 +08:00
}
# endif
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --adapt Dynamically adapt compression level to I/O conditions. \n " ) ;
DISPLAYOUT ( " --long[=#] Enable long distance matching with window log #. [Default: %u] \n " , g_defaultMaxWindowLog ) ;
DISPLAYOUT ( " --patch-from=REF Use REF as the reference point for Zstandard's diff engine. \n \n " ) ;
2019-12-03 08:08:08 +08:00
# ifdef ZSTD_MULTITHREAD
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " -T# Spawn # compression threads. [Default: 1; pass 0 for core count.] \n " ) ;
DISPLAYOUT ( " --single-thread Share a single thread for I/O and compression (slightly different than `-T1`). \n " ) ;
DISPLAYOUT ( " --auto-threads={physical|logical} \n " ) ;
DISPLAYOUT ( " Use physical/logical cores when using `-T0`. [Default: Physical] \n \n " ) ;
DISPLAYOUT ( " -B# Set job size to #. [Default: 0 (automatic)] \n " ) ;
DISPLAYOUT ( " --rsyncable Compress using a rsync-friendly method (`-B` sets block size). \n " ) ;
DISPLAYOUT ( " \n " ) ;
2019-12-03 08:08:08 +08:00
# endif
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --exclude-compressed Only compress files that are not already compressed. \n \n " ) ;
DISPLAYOUT ( " --stream-size=# Specify size of streaming input from STDIN. \n " ) ;
DISPLAYOUT ( " --size-hint=# Optimize compression parameters for streaming input of approximately size #. \n " ) ;
DISPLAYOUT ( " --target-compressed-block-size=# \n " ) ;
DISPLAYOUT ( " Generate compressed blocks of approximately # size. \n \n " ) ;
DISPLAYOUT ( " --no-dictID Don't write `dictID` into the header (dictionary compression only). \n " ) ;
DISPLAYOUT ( " --[no-]compress-literals Force (un)compressed literals. \n " ) ;
DISPLAYOUT ( " --[no-]row-match-finder Explicitly enable/disable the fast, row-based matchfinder for \n " ) ;
DISPLAYOUT ( " the 'greedy', 'lazy', and 'lazy2' strategies. \n " ) ;
DISPLAYOUT ( " \n " ) ;
DISPLAYOUT ( " --format=zstd Compress files to the `.zst` format. [Default] \n " ) ;
2017-02-08 22:17:55 +08:00
# ifdef ZSTD_GZCOMPRESS
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --format=gzip Compress files to the `.gz` format. \n " ) ;
2017-02-08 22:17:55 +08:00
# endif
2017-03-14 09:11:07 +08:00
# ifdef ZSTD_LZMACOMPRESS
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --format=xz Compress files to the `.xz` format. \n " ) ;
DISPLAYOUT ( " --format=lzma Compress files to the `.lzma` format. \n " ) ;
2017-03-14 09:11:07 +08:00
# endif
2017-04-25 07:48:25 +08:00
# ifdef ZSTD_LZ4COMPRESS
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --format=lz4 Compress files to the `.lz4` format. \n " ) ;
2017-04-25 07:48:25 +08:00
# endif
2020-05-09 05:14:46 +08:00
# endif /* !ZSTD_NOCOMPRESS */
2016-05-31 08:29:45 +08:00
# ifndef ZSTD_NODECOMPRESS
2022-03-10 08:34:12 +08:00
DISPLAYOUT ( " \n " ) ;
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " Advanced decompression options: \n " ) ;
DISPLAYOUT ( " -l Print information about Zstandard-compressed files. \n " ) ;
DISPLAYOUT ( " --test Test compressed file integrity. \n " ) ;
DISPLAYOUT ( " -M# Set the memory usage limit to # megabytes. \n " ) ;
2020-05-09 05:14:46 +08:00
# if ZSTD_SPARSE_DEFAULT
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --[no-]sparse Enable sparse mode. [Default: Enabled for files, disabled for STDOUT.] \n " ) ;
2020-05-09 05:14:46 +08:00
# else
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --[no-]sparse Enable sparse mode. [Default: Disabled] \n " ) ;
2020-05-09 05:14:46 +08:00
# endif
2022-08-05 08:15:59 +08:00
{
2022-12-21 04:34:02 +08:00
char const * passThroughDefault = " Disabled " ;
2022-08-05 08:15:59 +08:00
if ( exeNameMatch ( programName , ZSTD_CAT ) | |
exeNameMatch ( programName , ZSTD_ZCAT ) | |
exeNameMatch ( programName , ZSTD_GZCAT ) ) {
2022-12-21 04:34:02 +08:00
passThroughDefault = " Enabled " ;
2022-08-05 08:15:59 +08:00
}
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " --[no-]pass-through Pass through uncompressed files as-is. [Default: %s] \n " , passThroughDefault ) ;
2022-08-05 08:15:59 +08:00
}
2020-05-09 05:14:46 +08:00
# endif /* ZSTD_NODECOMPRESS */
2016-05-29 11:16:05 +08:00
# ifndef ZSTD_NODICT
2022-03-10 08:34:12 +08:00
DISPLAYOUT ( " \n " ) ;
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " Dictionary builder: \n " ) ;
DISPLAYOUT ( " --train Create a dictionary from a training set of files. \n \n " ) ;
DISPLAYOUT ( " --train-cover[=k=#,d=#,steps=#,split=#,shrink[=#]] \n " ) ;
DISPLAYOUT ( " Use the cover algorithm (with optional arguments). \n " ) ;
DISPLAYOUT ( " --train-fastcover[=k=#,d=#,f=#,steps=#,split=#,accel=#,shrink[=#]] \n " ) ;
DISPLAYOUT ( " Use the fast cover algorithm (with optional arguments). \n \n " ) ;
DISPLAYOUT ( " --train-legacy[=s=#] Use the legacy algorithm with selectivity #. [Default: %u] \n " , g_defaultSelectivityLevel ) ;
DISPLAYOUT ( " -o NAME Use NAME as dictionary name. [Default: %s] \n " , g_defaultDictName ) ;
DISPLAYOUT ( " --maxdict=# Limit dictionary to specified size #. [Default: %u] \n " , g_defaultMaxDictSize ) ;
DISPLAYOUT ( " --dictID=# Force dictionary ID to #. [Default: Random] \n " ) ;
2016-05-29 11:16:05 +08:00
# endif
2020-05-09 05:14:46 +08:00
2016-05-29 11:16:05 +08:00
# ifndef ZSTD_NOBENCH
2022-03-10 08:34:12 +08:00
DISPLAYOUT ( " \n " ) ;
2022-12-21 04:34:02 +08:00
DISPLAYOUT ( " Benchmark options: \n " ) ;
DISPLAYOUT ( " -b# Perform benchmarking with compression level #. [Default: %d] \n " , ZSTDCLI_CLEVEL_DEFAULT ) ;
DISPLAYOUT ( " -e# Test all compression levels up to #; starting level is `-b#`. [Default: 1] \n " ) ;
DISPLAYOUT ( " -i# Set the minimum evaluation to time # seconds. [Default: 3] \n " ) ;
DISPLAYOUT ( " -B# Cut file into independent chunks of size #. [Default: No chunking] \n " ) ;
DISPLAYOUT ( " -S Output one benchmark result per input file. [Default: Consolidated result] \n " ) ;
DISPLAYOUT ( " --priority=rt Set process priority to real-time. \n " ) ;
2016-05-29 11:16:05 +08:00
# endif
2020-05-09 05:14:46 +08:00
2016-05-29 11:16:05 +08:00
}
2019-12-03 08:08:08 +08:00
static void badusage ( const char * programName )
2016-05-29 11:16:05 +08:00
{
2019-12-03 08:08:08 +08:00
DISPLAYLEVEL ( 1 , " Incorrect parameters \n " ) ;
if ( g_displayLevel > = 2 ) usage ( stderr , programName ) ;
2016-05-29 11:16:05 +08:00
}
static void waitEnter ( void )
{
int unused ;
2019-12-03 08:08:08 +08:00
DISPLAY ( " Press enter to continue... \n " ) ;
2016-05-29 11:16:05 +08:00
unused = getchar ( ) ;
( void ) unused ;
}
2017-03-25 08:06:09 +08:00
static const char * lastNameFromPath ( const char * path )
{
const char * name = path ;
if ( strrchr ( name , ' / ' ) ) name = strrchr ( name , ' / ' ) + 1 ;
if ( strrchr ( name , ' \\ ' ) ) name = strrchr ( name , ' \\ ' ) + 1 ; /* windows */
return name ;
}
2018-05-13 05:29:33 +08:00
static void errorOut ( const char * msg )
{
DISPLAY ( " %s \n " , msg ) ; exit ( 1 ) ;
}
2018-12-20 15:41:18 +08:00
/*! readU32FromCharChecked() :
2018-12-20 08:45:42 +08:00
* @ return 0 if success , and store the result in * value .
2018-03-12 10:56:48 +08:00
* allows and interprets K , KB , KiB , M , MB and MiB suffix .
* Will also modify ` * stringPtr ` , advancing it to position where it stopped reading .
2018-12-20 08:45:42 +08:00
* @ return 1 if an overflow error occurs */
2018-12-20 15:41:18 +08:00
static int readU32FromCharChecked ( const char * * stringPtr , unsigned * value )
2016-06-03 21:14:09 +08:00
{
unsigned result = 0 ;
2018-05-13 05:29:33 +08:00
while ( ( * * stringPtr > = ' 0 ' ) & & ( * * stringPtr < = ' 9 ' ) ) {
2020-02-19 07:30:59 +08:00
unsigned const max = ( ( unsigned ) ( - 1 ) ) / 10 ;
unsigned last = result ;
2019-04-11 01:03:06 +08:00
if ( result > max ) return 1 ; /* overflow error */
result * = 10 ;
result + = ( unsigned ) ( * * stringPtr - ' 0 ' ) ;
2020-02-19 07:30:59 +08:00
if ( result < last ) return 1 ; /* overflow error */
2019-04-11 01:03:06 +08:00
( * stringPtr ) + + ;
2018-05-13 05:29:33 +08:00
}
2016-10-18 08:48:48 +08:00
if ( ( * * stringPtr = = ' K ' ) | | ( * * stringPtr = = ' M ' ) ) {
2018-05-13 05:29:33 +08:00
unsigned const maxK = ( ( unsigned ) ( - 1 ) ) > > 10 ;
2019-04-11 01:03:06 +08:00
if ( result > maxK ) return 1 ; /* overflow error */
2016-10-18 08:48:48 +08:00
result < < = 10 ;
2018-05-13 05:29:33 +08:00
if ( * * stringPtr = = ' M ' ) {
2019-04-11 01:03:06 +08:00
if ( result > maxK ) return 1 ; /* overflow error */
2018-05-13 05:29:33 +08:00
result < < = 10 ;
}
( * stringPtr ) + + ; /* skip `K` or `M` */
2016-10-18 08:48:48 +08:00
if ( * * stringPtr = = ' i ' ) ( * stringPtr ) + + ;
if ( * * stringPtr = = ' B ' ) ( * stringPtr ) + + ;
}
2018-12-20 08:45:42 +08:00
* value = result ;
return 0 ;
}
/*! readU32FromChar() :
* @ return : unsigned integer value read from input in ` char ` format .
* allows and interprets K , KB , KiB , M , MB and MiB suffix .
* Will also modify ` * stringPtr ` , advancing it to position where it stopped reading .
* Note : function will exit ( ) program if digit sequence overflows */
static unsigned readU32FromChar ( const char * * stringPtr ) {
2020-02-21 03:29:58 +08:00
static const char errorMsg [ ] = " error: numeric value overflows 32-bit unsigned int " ;
2018-12-20 08:45:42 +08:00
unsigned result ;
2018-12-20 15:41:18 +08:00
if ( readU32FromCharChecked ( stringPtr , & result ) ) { errorOut ( errorMsg ) ; }
2016-06-03 21:14:09 +08:00
return result ;
}
2021-06-10 04:49:36 +08:00
/*! readIntFromChar() :
* @ return : signed integer value read from input in ` char ` format .
* allows and interprets K , KB , KiB , M , MB and MiB suffix .
* Will also modify ` * stringPtr ` , advancing it to position where it stopped reading .
* Note : function will exit ( ) program if digit sequence overflows */
static int readIntFromChar ( const char * * stringPtr ) {
static const char errorMsg [ ] = " error: numeric value overflows 32-bit int " ;
int sign = 1 ;
unsigned result ;
if ( * * stringPtr = = ' - ' ) {
( * stringPtr ) + + ;
sign = - 1 ;
}
if ( readU32FromCharChecked ( stringPtr , & result ) ) { errorOut ( errorMsg ) ; }
return ( int ) result * sign ;
}
2020-02-21 03:29:58 +08:00
/*! readSizeTFromCharChecked() :
* @ return 0 if success , and store the result in * value .
* allows and interprets K , KB , KiB , M , MB and MiB suffix .
* Will also modify ` * stringPtr ` , advancing it to position where it stopped reading .
* @ return 1 if an overflow error occurs */
static int readSizeTFromCharChecked ( const char * * stringPtr , size_t * value )
{
size_t result = 0 ;
while ( ( * * stringPtr > = ' 0 ' ) & & ( * * stringPtr < = ' 9 ' ) ) {
size_t const max = ( ( size_t ) ( - 1 ) ) / 10 ;
size_t last = result ;
if ( result > max ) return 1 ; /* overflow error */
result * = 10 ;
result + = ( size_t ) ( * * stringPtr - ' 0 ' ) ;
if ( result < last ) return 1 ; /* overflow error */
( * stringPtr ) + + ;
}
if ( ( * * stringPtr = = ' K ' ) | | ( * * stringPtr = = ' M ' ) ) {
size_t const maxK = ( ( size_t ) ( - 1 ) ) > > 10 ;
if ( result > maxK ) return 1 ; /* overflow error */
result < < = 10 ;
if ( * * stringPtr = = ' M ' ) {
if ( result > maxK ) return 1 ; /* overflow error */
result < < = 10 ;
}
( * stringPtr ) + + ; /* skip `K` or `M` */
if ( * * stringPtr = = ' i ' ) ( * stringPtr ) + + ;
if ( * * stringPtr = = ' B ' ) ( * stringPtr ) + + ;
}
* value = result ;
return 0 ;
}
/*! readSizeTFromChar() :
* @ return : size_t value read from input in ` char ` format .
* allows and interprets K , KB , KiB , M , MB and MiB suffix .
* Will also modify ` * stringPtr ` , advancing it to position where it stopped reading .
* Note : function will exit ( ) program if digit sequence overflows */
static size_t readSizeTFromChar ( const char * * stringPtr ) {
static const char errorMsg [ ] = " error: numeric value overflows size_t " ;
size_t result ;
if ( readSizeTFromCharChecked ( stringPtr , & result ) ) { errorOut ( errorMsg ) ; }
return result ;
}
2016-12-03 07:18:57 +08:00
/** longCommandWArg() :
2017-01-31 05:07:24 +08:00
* check if * stringPtr is the same as longCommand .
2016-12-03 07:18:57 +08:00
* If yes , @ return 1 and advances * stringPtr to the position which immediately follows longCommand .
2018-03-12 10:56:48 +08:00
* @ return 0 and doesn ' t modify * stringPtr otherwise .
2016-12-03 07:18:57 +08:00
*/
2019-10-18 06:27:25 +08:00
static int longCommandWArg ( const char * * stringPtr , const char * longCommand )
2016-10-15 05:41:17 +08:00
{
size_t const comSize = strlen ( longCommand ) ;
2016-12-03 07:18:57 +08:00
int const result = ! strncmp ( * stringPtr , longCommand , comSize ) ;
2016-10-15 05:41:17 +08:00
if ( result ) * stringPtr + = comSize ;
return result ;
}
2016-12-13 19:18:07 +08:00
2017-01-01 13:08:24 +08:00
# ifndef ZSTD_NODICT
2019-07-19 02:44:59 +08:00
static const unsigned kDefaultRegression = 1 ;
2017-01-01 13:08:24 +08:00
/**
* parseCoverParameters ( ) :
2017-05-02 14:40:20 +08:00
* reads cover parameters from * stringPtr ( e . g . " --train-cover=k=48,d=8,steps=32 " ) into * params
2017-01-01 13:08:24 +08:00
* @ return 1 means that cover parameters were correct
* @ return 0 in case of malformed parameters
*/
2017-06-27 12:07:14 +08:00
static unsigned parseCoverParameters ( const char * stringPtr , ZDICT_cover_params_t * params )
2017-01-01 13:08:24 +08:00
{
memset ( params , 0 , sizeof ( * params ) ) ;
for ( ; ; ) {
2017-01-03 04:40:43 +08:00
if ( longCommandWArg ( & stringPtr , " k= " ) ) { params - > k = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
2017-01-01 13:08:24 +08:00
if ( longCommandWArg ( & stringPtr , " d= " ) ) { params - > d = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
2017-01-03 04:40:43 +08:00
if ( longCommandWArg ( & stringPtr , " steps= " ) ) { params - > steps = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
2018-06-30 08:54:41 +08:00
if ( longCommandWArg ( & stringPtr , " split= " ) ) {
2018-07-04 08:53:27 +08:00
unsigned splitPercentage = readU32FromChar ( & stringPtr ) ;
2018-06-30 08:54:41 +08:00
params - > splitPoint = ( double ) splitPercentage / 100.0 ;
if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ;
}
2019-06-28 07:26:57 +08:00
if ( longCommandWArg ( & stringPtr , " shrink " ) ) {
params - > shrinkDictMaxRegression = kDefaultRegression ;
params - > shrinkDict = 1 ;
if ( stringPtr [ 0 ] = = ' = ' ) {
stringPtr + + ;
params - > shrinkDictMaxRegression = readU32FromChar ( & stringPtr ) ;
}
if ( stringPtr [ 0 ] = = ' , ' ) {
stringPtr + + ;
continue ;
}
else break ;
}
2017-01-01 13:08:24 +08:00
return 0 ;
}
if ( stringPtr [ 0 ] ! = 0 ) return 0 ;
2019-06-28 07:26:57 +08:00
DISPLAYLEVEL ( 4 , " cover: k=%u \n d=%u \n steps=%u \n split=%u \n shrink%u \n " , params - > k , params - > d , params - > steps , ( unsigned ) ( params - > splitPoint * 100 ) , params - > shrinkDictMaxRegression ) ;
2017-01-01 13:08:24 +08:00
return 1 ;
}
2017-05-02 14:40:20 +08:00
2018-08-24 03:06:20 +08:00
/**
* parseFastCoverParameters ( ) :
* reads fastcover parameters from * stringPtr ( e . g . " --train-fastcover=k=48,d=8,f=20,steps=32,accel=2 " ) into * params
* @ return 1 means that fastcover parameters were correct
* @ return 0 in case of malformed parameters
*/
static unsigned parseFastCoverParameters ( const char * stringPtr , ZDICT_fastCover_params_t * params )
{
memset ( params , 0 , sizeof ( * params ) ) ;
for ( ; ; ) {
if ( longCommandWArg ( & stringPtr , " k= " ) ) { params - > k = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
if ( longCommandWArg ( & stringPtr , " d= " ) ) { params - > d = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
if ( longCommandWArg ( & stringPtr , " f= " ) ) { params - > f = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
if ( longCommandWArg ( & stringPtr , " steps= " ) ) { params - > steps = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
if ( longCommandWArg ( & stringPtr , " accel= " ) ) { params - > accel = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
if ( longCommandWArg ( & stringPtr , " split= " ) ) {
unsigned splitPercentage = readU32FromChar ( & stringPtr ) ;
params - > splitPoint = ( double ) splitPercentage / 100.0 ;
if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ;
}
2019-06-28 07:26:57 +08:00
if ( longCommandWArg ( & stringPtr , " shrink " ) ) {
params - > shrinkDictMaxRegression = kDefaultRegression ;
params - > shrinkDict = 1 ;
if ( stringPtr [ 0 ] = = ' = ' ) {
stringPtr + + ;
params - > shrinkDictMaxRegression = readU32FromChar ( & stringPtr ) ;
}
if ( stringPtr [ 0 ] = = ' , ' ) {
stringPtr + + ;
continue ;
}
else break ;
}
2018-08-24 03:06:20 +08:00
return 0 ;
}
if ( stringPtr [ 0 ] ! = 0 ) return 0 ;
2019-06-28 07:26:57 +08:00
DISPLAYLEVEL ( 4 , " cover: k=%u \n d=%u \n f=%u \n steps=%u \n split=%u \n accel=%u \n shrink=%u \n " , params - > k , params - > d , params - > f , params - > steps , ( unsigned ) ( params - > splitPoint * 100 ) , params - > accel , params - > shrinkDictMaxRegression ) ;
2018-08-24 03:06:20 +08:00
return 1 ;
}
2017-05-02 14:40:20 +08:00
/**
* parseLegacyParameters ( ) :
2019-04-13 02:18:11 +08:00
* reads legacy dictionary builder parameters from * stringPtr ( e . g . " --train-legacy=selectivity=8 " ) into * selectivity
2017-05-02 14:40:20 +08:00
* @ return 1 means that legacy dictionary builder parameters were correct
* @ return 0 in case of malformed parameters
*/
static unsigned parseLegacyParameters ( const char * stringPtr , unsigned * selectivity )
{
if ( ! longCommandWArg ( & stringPtr , " s= " ) & & ! longCommandWArg ( & stringPtr , " selectivity= " ) ) { return 0 ; }
* selectivity = readU32FromChar ( & stringPtr ) ;
if ( stringPtr [ 0 ] ! = 0 ) return 0 ;
DISPLAYLEVEL ( 4 , " legacy: selectivity=%u \n " , * selectivity ) ;
return 1 ;
}
2017-06-27 12:07:14 +08:00
static ZDICT_cover_params_t defaultCoverParams ( void )
2017-05-02 14:40:20 +08:00
{
2017-06-27 12:07:14 +08:00
ZDICT_cover_params_t params ;
2017-05-02 14:40:20 +08:00
memset ( & params , 0 , sizeof ( params ) ) ;
params . d = 8 ;
params . steps = 4 ;
2018-08-24 03:06:20 +08:00
params . splitPoint = 1.0 ;
2019-06-28 07:26:57 +08:00
params . shrinkDict = 0 ;
params . shrinkDictMaxRegression = kDefaultRegression ;
2018-08-24 03:06:20 +08:00
return params ;
}
static ZDICT_fastCover_params_t defaultFastCoverParams ( void )
{
ZDICT_fastCover_params_t params ;
memset ( & params , 0 , sizeof ( params ) ) ;
params . d = 8 ;
2018-09-05 08:12:35 +08:00
params . f = 20 ;
2018-08-24 03:06:20 +08:00
params . steps = 4 ;
params . splitPoint = 0.75 ; /* different from default splitPoint of cover */
params . accel = DEFAULT_ACCEL ;
2019-06-28 07:26:57 +08:00
params . shrinkDict = 0 ;
params . shrinkDictMaxRegression = kDefaultRegression ;
2017-05-02 14:40:20 +08:00
return params ;
}
2017-01-01 13:08:24 +08:00
# endif
2017-01-31 05:07:24 +08:00
2018-09-25 09:16:08 +08:00
/** parseAdaptParameters() :
* reads adapt parameters from * stringPtr ( e . g . " --zstd=min=1,max=19) and store them into adaptMinPtr and adaptMaxPtr.
* Both adaptMinPtr and adaptMaxPtr must be already allocated and correctly initialized .
* There is no guarantee that any of these values will be updated .
* @ return 1 means that parsing was successful ,
* @ return 0 in case of malformed parameters
*/
static unsigned parseAdaptParameters ( const char * stringPtr , int * adaptMinPtr , int * adaptMaxPtr )
{
for ( ; ; ) {
2021-06-10 04:49:36 +08:00
if ( longCommandWArg ( & stringPtr , " min= " ) ) { * adaptMinPtr = readIntFromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
if ( longCommandWArg ( & stringPtr , " max= " ) ) { * adaptMaxPtr = readIntFromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
2018-09-25 09:16:08 +08:00
DISPLAYLEVEL ( 4 , " invalid compression parameter \n " ) ;
return 0 ;
}
if ( stringPtr [ 0 ] ! = 0 ) return 0 ; /* check the end of string */
if ( * adaptMinPtr > * adaptMaxPtr ) {
DISPLAYLEVEL ( 4 , " incoherent adaptation limits \n " ) ;
return 0 ;
}
return 1 ;
}
2016-12-13 19:18:07 +08:00
/** parseCompressionParameters() :
2018-11-21 06:56:07 +08:00
* reads compression parameters from * stringPtr ( e . g . " --zstd=wlog=23,clog=23,hlog=22,slog=6,mml=3,tlen=48,strat=6 " ) into * params
2016-12-13 19:18:07 +08:00
* @ return 1 means that compression parameters were correct
* @ return 0 in case of malformed parameters
*/
static unsigned parseCompressionParameters ( const char * stringPtr , ZSTD_compressionParameters * params )
{
for ( ; ; ) {
if ( longCommandWArg ( & stringPtr , " windowLog= " ) | | longCommandWArg ( & stringPtr , " wlog= " ) ) { params - > windowLog = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
if ( longCommandWArg ( & stringPtr , " chainLog= " ) | | longCommandWArg ( & stringPtr , " clog= " ) ) { params - > chainLog = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
if ( longCommandWArg ( & stringPtr , " hashLog= " ) | | longCommandWArg ( & stringPtr , " hlog= " ) ) { params - > hashLog = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
if ( longCommandWArg ( & stringPtr , " searchLog= " ) | | longCommandWArg ( & stringPtr , " slog= " ) ) { params - > searchLog = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
2018-11-21 06:56:07 +08:00
if ( longCommandWArg ( & stringPtr , " minMatch= " ) | | longCommandWArg ( & stringPtr , " mml= " ) ) { params - > minMatch = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
2016-12-13 19:18:07 +08:00
if ( longCommandWArg ( & stringPtr , " targetLength= " ) | | longCommandWArg ( & stringPtr , " tlen= " ) ) { params - > targetLength = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
2017-06-21 05:11:49 +08:00
if ( longCommandWArg ( & stringPtr , " strategy= " ) | | longCommandWArg ( & stringPtr , " strat= " ) ) { params - > strategy = ( ZSTD_strategy ) ( readU32FromChar ( & stringPtr ) ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
2017-01-31 05:07:24 +08:00
if ( longCommandWArg ( & stringPtr , " overlapLog= " ) | | longCommandWArg ( & stringPtr , " ovlog= " ) ) { g_overlapLog = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
2018-11-21 06:56:07 +08:00
if ( longCommandWArg ( & stringPtr , " ldmHashLog= " ) | | longCommandWArg ( & stringPtr , " lhlog= " ) ) { g_ldmHashLog = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
if ( longCommandWArg ( & stringPtr , " ldmMinMatch= " ) | | longCommandWArg ( & stringPtr , " lmml= " ) ) { g_ldmMinMatch = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
if ( longCommandWArg ( & stringPtr , " ldmBucketSizeLog= " ) | | longCommandWArg ( & stringPtr , " lblog= " ) ) { g_ldmBucketSizeLog = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
2018-11-22 06:36:57 +08:00
if ( longCommandWArg ( & stringPtr , " ldmHashRateLog= " ) | | longCommandWArg ( & stringPtr , " lhrlog= " ) ) { g_ldmHashRateLog = readU32FromChar ( & stringPtr ) ; if ( stringPtr [ 0 ] = = ' , ' ) { stringPtr + + ; continue ; } else break ; }
2017-12-27 20:32:05 +08:00
DISPLAYLEVEL ( 4 , " invalid compression parameter \n " ) ;
2016-12-13 19:18:07 +08:00
return 0 ;
}
2017-12-27 20:32:05 +08:00
DISPLAYLEVEL ( 4 , " windowLog=%d, chainLog=%d, hashLog=%d, searchLog=%d \n " , params - > windowLog , params - > chainLog , params - > hashLog , params - > searchLog ) ;
2018-11-21 06:56:07 +08:00
DISPLAYLEVEL ( 4 , " minMatch=%d, targetLength=%d, strategy=%d \n " , params - > minMatch , params - > targetLength , params - > strategy ) ;
2016-12-13 19:18:07 +08:00
if ( stringPtr [ 0 ] ! = 0 ) return 0 ; /* check the end of string */
return 1 ;
}
2017-08-20 04:33:50 +08:00
static void printVersion ( void )
{
2020-06-08 20:06:37 +08:00
if ( g_displayLevel < DISPLAY_LEVEL_DEFAULT ) {
DISPLAYOUT ( " %s \n " , ZSTD_VERSION_STRING ) ;
return ;
}
2019-12-03 08:08:08 +08:00
DISPLAYOUT ( WELCOME_MESSAGE ) ;
if ( g_displayLevel > = 3 ) {
2017-08-20 04:33:50 +08:00
/* format support */
2019-12-03 08:08:08 +08:00
DISPLAYOUT ( " *** supports: zstd " ) ;
# if defined(ZSTD_LEGACY_SUPPORT) && (ZSTD_LEGACY_SUPPORT>0) && (ZSTD_LEGACY_SUPPORT<8)
DISPLAYOUT ( " , zstd legacy v0.%d+ " , ZSTD_LEGACY_SUPPORT ) ;
# endif
# ifdef ZSTD_GZCOMPRESS
DISPLAYOUT ( " , gzip " ) ;
# endif
# ifdef ZSTD_LZ4COMPRESS
DISPLAYOUT ( " , lz4 " ) ;
# endif
# ifdef ZSTD_LZMACOMPRESS
DISPLAYOUT ( " , lzma, xz " ) ;
# endif
DISPLAYOUT ( " \n " ) ;
if ( g_displayLevel > = 4 ) {
2022-01-27 09:55:26 +08:00
/* library versions */
DISPLAYOUT ( " zlib version %s \n " , FIO_zlibVersion ( ) ) ;
DISPLAYOUT ( " lz4 version %s \n " , FIO_lz4Version ( ) ) ;
DISPLAYOUT ( " lzma version %s \n " , FIO_lzmaVersion ( ) ) ;
2019-12-03 08:08:08 +08:00
/* posix support */
# ifdef _POSIX_C_SOURCE
DISPLAYOUT ( " _POSIX_C_SOURCE defined: %ldL \n " , ( long ) _POSIX_C_SOURCE ) ;
# endif
# ifdef _POSIX_VERSION
DISPLAYOUT ( " _POSIX_VERSION defined: %ldL \n " , ( long ) _POSIX_VERSION ) ;
# endif
# ifdef PLATFORM_POSIX_VERSION
DISPLAYOUT ( " PLATFORM_POSIX_VERSION defined: %ldL \n " , ( long ) PLATFORM_POSIX_VERSION ) ;
# endif
} }
2017-08-20 04:33:50 +08:00
}
2016-12-13 19:18:07 +08:00
2021-11-06 03:48:13 +08:00
# define ZSTD_NB_STRATEGIES 9
static const char * ZSTD_strategyMap [ ZSTD_NB_STRATEGIES + 1 ] = { " " , " ZSTD_fast " ,
" ZSTD_dfast " , " ZSTD_greedy " , " ZSTD_lazy " , " ZSTD_lazy2 " , " ZSTD_btlazy2 " ,
" ZSTD_btopt " , " ZSTD_btultra " , " ZSTD_btultra2 " } ;
2021-11-12 04:14:56 +08:00
# ifndef ZSTD_NOCOMPRESS
2021-11-06 03:48:13 +08:00
static void printDefaultCParams ( const char * filename , const char * dictFileName , int cLevel ) {
unsigned long long fileSize = UTIL_getFileSize ( filename ) ;
const size_t dictSize = dictFileName ! = NULL ? ( size_t ) UTIL_getFileSize ( dictFileName ) : 0 ;
const ZSTD_compressionParameters cParams = ZSTD_getCParams ( cLevel , fileSize , dictSize ) ;
if ( fileSize ! = UTIL_FILESIZE_UNKNOWN ) DISPLAY ( " %s (%u bytes) \n " , filename , ( unsigned ) fileSize ) ;
else DISPLAY ( " %s (src size unknown) \ n " , filename) ;
DISPLAY ( " - windowLog : %u \n " , cParams . windowLog ) ;
DISPLAY ( " - chainLog : %u \n " , cParams . chainLog ) ;
DISPLAY ( " - hashLog : %u \n " , cParams . hashLog ) ;
DISPLAY ( " - searchLog : %u \n " , cParams . searchLog ) ;
DISPLAY ( " - minMatch : %u \n " , cParams . minMatch ) ;
DISPLAY ( " - targetLength : %u \n " , cParams . targetLength ) ;
assert ( cParams . strategy < ZSTD_NB_STRATEGIES + 1 ) ;
DISPLAY ( " - strategy : %s (%u) \n " , ZSTD_strategyMap [ ( int ) cParams . strategy ] , ( unsigned ) cParams . strategy ) ;
}
static void printActualCParams ( const char * filename , const char * dictFileName , int cLevel , const ZSTD_compressionParameters * cParams ) {
unsigned long long fileSize = UTIL_getFileSize ( filename ) ;
const size_t dictSize = dictFileName ! = NULL ? ( size_t ) UTIL_getFileSize ( dictFileName ) : 0 ;
ZSTD_compressionParameters actualCParams = ZSTD_getCParams ( cLevel , fileSize , dictSize ) ;
assert ( g_displayLevel > = 4 ) ;
actualCParams . windowLog = cParams - > windowLog = = 0 ? actualCParams . windowLog : cParams - > windowLog ;
actualCParams . chainLog = cParams - > chainLog = = 0 ? actualCParams . chainLog : cParams - > chainLog ;
actualCParams . hashLog = cParams - > hashLog = = 0 ? actualCParams . hashLog : cParams - > hashLog ;
actualCParams . searchLog = cParams - > searchLog = = 0 ? actualCParams . searchLog : cParams - > searchLog ;
actualCParams . minMatch = cParams - > minMatch = = 0 ? actualCParams . minMatch : cParams - > minMatch ;
actualCParams . targetLength = cParams - > targetLength = = 0 ? actualCParams . targetLength : cParams - > targetLength ;
actualCParams . strategy = cParams - > strategy = = 0 ? actualCParams . strategy : cParams - > strategy ;
DISPLAY ( " --zstd=wlog=%d,clog=%d,hlog=%d,slog=%d,mml=%d,tlen=%d,strat=%d \n " ,
actualCParams . windowLog , actualCParams . chainLog , actualCParams . hashLog , actualCParams . searchLog ,
actualCParams . minMatch , actualCParams . targetLength , actualCParams . strategy ) ;
}
2021-11-12 04:14:56 +08:00
# endif
2018-12-20 05:26:27 +08:00
/* Environment variables for parameter setting */
# define ENV_CLEVEL "ZSTD_CLEVEL"
2020-09-10 00:35:40 +08:00
# define ENV_NBTHREADS "ZSTD_NBTHREADS" /* takes lower precedence than directly specifying -T# in the CLI */
2018-12-20 05:26:27 +08:00
2019-12-03 08:08:08 +08:00
/* pick up environment variable */
2018-12-20 09:56:45 +08:00
static int init_cLevel ( void ) {
2018-12-20 08:45:42 +08:00
const char * const env = getenv ( ENV_CLEVEL ) ;
2019-12-03 06:28:18 +08:00
if ( env ! = NULL ) {
const char * ptr = env ;
2018-12-20 05:26:27 +08:00
int sign = 1 ;
2018-12-20 08:45:42 +08:00
if ( * ptr = = ' - ' ) {
2018-12-20 05:26:27 +08:00
sign = - 1 ;
2018-12-20 08:45:42 +08:00
ptr + + ;
} else if ( * ptr = = ' + ' ) {
ptr + + ;
}
if ( ( * ptr > = ' 0 ' ) & & ( * ptr < = ' 9 ' ) ) {
unsigned absLevel ;
2019-04-11 01:03:06 +08:00
if ( readU32FromCharChecked ( & ptr , & absLevel ) ) {
2019-12-03 06:28:18 +08:00
DISPLAYLEVEL ( 2 , " Ignore environment variable setting %s=%s: numeric value too large \n " , ENV_CLEVEL , env ) ;
2018-12-20 08:45:42 +08:00
return ZSTDCLI_CLEVEL_DEFAULT ;
} else if ( * ptr = = 0 ) {
2019-10-18 06:27:25 +08:00
return sign * ( int ) absLevel ;
2019-12-03 06:28:18 +08:00
} }
2018-12-20 05:26:27 +08:00
2019-12-03 06:28:18 +08:00
DISPLAYLEVEL ( 2 , " Ignore environment variable setting %s=%s: not a valid integer value \n " , ENV_CLEVEL , env ) ;
2018-12-20 05:26:27 +08:00
}
return ZSTDCLI_CLEVEL_DEFAULT ;
}
2020-09-08 21:26:16 +08:00
# ifdef ZSTD_MULTITHREAD
2020-09-10 00:35:40 +08:00
static unsigned init_nbThreads ( void ) {
const char * const env = getenv ( ENV_NBTHREADS ) ;
2020-08-26 06:49:52 +08:00
if ( env ! = NULL ) {
const char * ptr = env ;
if ( ( * ptr > = ' 0 ' ) & & ( * ptr < = ' 9 ' ) ) {
2020-09-10 00:35:40 +08:00
unsigned nbThreads ;
if ( readU32FromCharChecked ( & ptr , & nbThreads ) ) {
DISPLAYLEVEL ( 2 , " Ignore environment variable setting %s=%s: numeric value too large \n " , ENV_NBTHREADS , env ) ;
return ZSTDCLI_NBTHREADS_DEFAULT ;
2020-08-26 06:49:52 +08:00
} else if ( * ptr = = 0 ) {
2020-09-10 00:35:40 +08:00
return nbThreads ;
2020-08-26 06:49:52 +08:00
}
}
2020-09-10 00:35:40 +08:00
DISPLAYLEVEL ( 2 , " Ignore environment variable setting %s=%s: not a valid unsigned value \n " , ENV_NBTHREADS , env ) ;
2020-08-26 06:49:52 +08:00
}
2020-09-10 00:35:40 +08:00
return ZSTDCLI_NBTHREADS_DEFAULT ;
2020-08-26 06:49:52 +08:00
}
2020-09-08 21:26:16 +08:00
# endif
2020-08-26 06:49:52 +08:00
2020-07-21 08:41:32 +08:00
# define NEXT_FIELD(ptr) { \
if ( * argument = = ' = ' ) { \
ptr = + + argument ; \
argument + = strlen ( ptr ) ; \
} else { \
argNb + + ; \
if ( argNb > = argCount ) { \
DISPLAY ( " error: missing command argument \n " ) ; \
CLEAN_RETURN ( 1 ) ; \
} \
ptr = argv [ argNb ] ; \
assert ( ptr ! = NULL ) ; \
if ( ptr [ 0 ] = = ' - ' ) { \
DISPLAY ( " error: command cannot be separated from its argument by another command \n " ) ; \
CLEAN_RETURN ( 1 ) ; \
} } }
# define NEXT_UINT32(val32) { \
const char * __nb ; \
NEXT_FIELD ( __nb ) ; \
val32 = readU32FromChar ( & __nb ) ; \
2022-09-22 04:20:01 +08:00
if ( * __nb ! = 0 ) { \
errorOut ( " error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB are allowed " ) ; \
} \
}
# define NEXT_TSIZE(valTsize) { \
const char * __nb ; \
NEXT_FIELD ( __nb ) ; \
valTsize = readSizeTFromChar ( & __nb ) ; \
if ( * __nb ! = 0 ) { \
errorOut ( " error: only numeric values with optional suffixes K, KB, KiB, M, MB, MiB are allowed " ) ; \
} \
2020-07-18 03:46:36 +08:00
}
2020-02-01 02:47:17 +08:00
2017-06-06 05:45:31 +08:00
typedef enum { zom_compress , zom_decompress , zom_test , zom_bench , zom_train , zom_list } zstd_operation_mode ;
2016-05-29 11:16:05 +08:00
# define CLEAN_RETURN(i) { operationResult = (i); goto _end; }
2018-09-25 09:16:08 +08:00
# ifdef ZSTD_NOCOMPRESS
/* symbols from compression library are not defined and should not be invoked */
2019-12-03 06:28:18 +08:00
# define MINCLEVEL -99
2018-09-25 09:16:08 +08:00
# define MAXCLEVEL 22
# else
# define MINCLEVEL ZSTD_minCLevel()
# define MAXCLEVEL ZSTD_maxCLevel()
# endif
2021-07-15 00:55:47 +08:00
int main ( int argCount , const char * argv [ ] )
2016-05-29 11:16:05 +08:00
{
int argNb ,
2018-02-14 06:56:35 +08:00
followLinks = 0 ,
2021-05-05 04:24:46 +08:00
allowBlockDevices = 0 ,
2021-01-12 06:53:20 +08:00
forceStdin = 0 ,
2018-02-14 06:56:35 +08:00
forceStdout = 0 ,
2020-10-08 01:47:38 +08:00
hasStdout = 0 ,
2018-02-10 07:53:27 +08:00
ldmFlag = 0 ,
2018-02-14 06:56:35 +08:00
main_pause = 0 ,
2018-08-12 11:48:06 +08:00
adapt = 0 ,
2020-11-03 09:52:29 +08:00
useRowMatchFinder = 0 ,
2018-09-25 09:16:08 +08:00
adaptMin = MINCLEVEL ,
adaptMax = MAXCLEVEL ,
2018-11-13 11:59:42 +08:00
rsyncable = 0 ,
2018-02-14 06:56:35 +08:00
nextArgumentsAreFiles = 0 ,
operationResult = 0 ,
2017-11-17 16:22:55 +08:00
separateFiles = 0 ,
2018-02-10 07:53:27 +08:00
setRealTimePrio = 0 ,
2018-02-14 06:56:35 +08:00
singleThread = 0 ,
2021-06-16 21:38:43 +08:00
defaultLogicalCores = 0 ,
2020-01-31 06:12:03 +08:00
showDefaultCParams = 0 ,
2020-03-10 02:07:29 +08:00
ultra = 0 ,
2020-03-10 03:12:52 +08:00
contentSize = 1 ;
fileio_types.h : avoid dependency on mem.h
fileio_types.h cannot be parsed by itself
because it relies on basic types defined in `lib/common/mem.h`.
As for #3231, it likely wasn't detected because `mem.h` was probably included before within target files.
But this is not proper.
A "easy" solution would be to add the missing include,
but each dependency should be considered "bad" by default,
and only allowed if it brings some tangible value.
In this case, since these types are only used to declare internal structure variables
which are effectively only flags,
I believe it's really not valuable to add a dependency on `mem.h` for this purpose
while the standard `int` type can do the same job.
I was expecting some compiler warnings following this change,
but it turns out we don't use `-Wconversion` by default on `zstd` source code,
so there is none.
Nevertheless, I enabled `-Wconversion` locally and proceeded to fix a few conversion warnings in the process.
Adding `-Wconversion` to the list of flags used for `zstd` is something I would be favorable over the long term,
but it cannot be done overnight,
because the nb of places where this warning is triggered is daunting.
Better progressively reduce the nb of triggered `-Wconversion` warnings before enabling this flag by default.
2022-08-04 03:39:35 +08:00
unsigned nbWorkers = 0 ;
2018-06-20 01:58:22 +08:00
double compressibility = 0.5 ;
2017-01-20 08:59:56 +08:00
unsigned bench_nbSeconds = 3 ; /* would be better if this value was synchronized from bench */
size_t blockSize = 0 ;
2019-01-23 09:31:13 +08:00
FIO_prefs_t * const prefs = FIO_createPreferences ( ) ;
2020-08-28 23:01:04 +08:00
FIO_ctx_t * const fCtx = FIO_createContext ( ) ;
2016-10-29 04:58:31 +08:00
zstd_operation_mode operation = zom_compress ;
2016-12-13 19:18:07 +08:00
ZSTD_compressionParameters compressionParams ;
2019-12-03 08:08:08 +08:00
int cLevel = init_cLevel ( ) ;
2019-12-03 06:28:18 +08:00
int cLevelLast = MINCLEVEL - 1 ; /* lower than minimum */
2016-05-29 11:16:05 +08:00
unsigned recursive = 0 ;
2016-10-15 04:13:13 +08:00
unsigned memLimit = 0 ;
2019-11-06 09:02:43 +08:00
FileNamesTable * filenames = UTIL_allocateFileNamesTable ( ( size_t ) argCount ) ; /* argCount >= 1 */
FileNamesTable * file_of_names = UTIL_allocateFileNamesTable ( ( size_t ) argCount ) ; /* argCount >= 1 */
2016-05-29 11:16:05 +08:00
const char * programName = argv [ 0 ] ;
const char * outFileName = NULL ;
2019-09-06 07:03:35 +08:00
const char * outDirName = NULL ;
2020-06-20 10:35:51 +08:00
const char * outMirroredDirName = NULL ;
2016-05-29 11:16:05 +08:00
const char * dictFileName = NULL ;
2020-01-11 06:25:24 +08:00
const char * patchFromDictFileName = NULL ;
2017-02-09 00:37:14 +08:00
const char * suffix = ZSTD_EXTENSION ;
2016-05-29 11:16:05 +08:00
unsigned maxDictSize = g_defaultMaxDictSize ;
2016-05-31 03:18:52 +08:00
unsigned dictID = 0 ;
2019-08-16 14:57:55 +08:00
size_t streamSrcSize = 0 ;
2019-06-25 04:40:52 +08:00
size_t targetCBlockSize = 0 ;
2019-08-19 23:52:08 +08:00
size_t srcSizeHint = 0 ;
2022-01-25 09:42:21 +08:00
size_t nbInputFileNames = 0 ;
2016-07-27 21:09:11 +08:00
int dictCLevel = g_defaultDictCLevel ;
2016-05-29 11:16:05 +08:00
unsigned dictSelect = g_defaultSelectivityLevel ;
2017-01-01 13:08:24 +08:00
# ifndef ZSTD_NODICT
2017-06-27 12:07:14 +08:00
ZDICT_cover_params_t coverParams = defaultCoverParams ( ) ;
2018-08-24 03:06:20 +08:00
ZDICT_fastCover_params_t fastCoverParams = defaultFastCoverParams ( ) ;
dictType dict = fastCover ;
2017-01-01 13:08:24 +08:00
# endif
2018-08-03 22:54:29 +08:00
# ifndef ZSTD_NOBENCH
BMK_advancedParams_t benchParams = BMK_initAdvancedParams ( ) ;
# endif
2021-09-20 21:04:07 +08:00
ZSTD_paramSwitch_e literalCompressionMode = ZSTD_ps_auto ;
2016-05-29 11:16:05 +08:00
2017-10-02 03:10:26 +08:00
2016-05-29 11:16:05 +08:00
/* init */
2021-01-08 02:37:27 +08:00
checkLibVersion ( ) ;
2016-06-05 07:05:01 +08:00
( void ) recursive ; ( void ) cLevelLast ; /* not used when ZSTD_NOBENCH set */
2020-01-11 06:25:24 +08:00
( void ) memLimit ;
2019-12-03 06:28:18 +08:00
assert ( argCount > = 1 ) ;
2019-11-06 09:02:43 +08:00
if ( ( filenames = = NULL ) | | ( file_of_names = = NULL ) ) { DISPLAY ( " zstd: allocation error \n " ) ; exit ( 1 ) ; }
2017-03-25 08:06:09 +08:00
programName = lastNameFromPath ( programName ) ;
2018-02-14 06:56:35 +08:00
# ifdef ZSTD_MULTITHREAD
2020-09-10 00:35:40 +08:00
nbWorkers = init_nbThreads ( ) ;
2018-02-14 06:56:35 +08:00
# endif
2016-05-29 11:16:05 +08:00
/* preset behaviors */
2018-06-30 08:10:56 +08:00
if ( exeNameMatch ( programName , ZSTD_ZSTDMT ) ) nbWorkers = 0 , singleThread = 0 ;
2017-03-25 08:06:09 +08:00
if ( exeNameMatch ( programName , ZSTD_UNZSTD ) ) operation = zom_decompress ;
2022-08-05 08:15:59 +08:00
if ( exeNameMatch ( programName , ZSTD_CAT ) ) { operation = zom_decompress ; FIO_overwriteMode ( prefs ) ; forceStdout = 1 ; followLinks = 1 ; FIO_setPassThroughFlag ( prefs , 1 ) ; outFileName = stdoutmark ; g_displayLevel = 1 ; } /* supports multiple formats */
if ( exeNameMatch ( programName , ZSTD_ZCAT ) ) { operation = zom_decompress ; FIO_overwriteMode ( prefs ) ; forceStdout = 1 ; followLinks = 1 ; FIO_setPassThroughFlag ( prefs , 1 ) ; outFileName = stdoutmark ; g_displayLevel = 1 ; } /* behave like zcat, also supports multiple formats */
2022-02-09 04:32:42 +08:00
if ( exeNameMatch ( programName , ZSTD_GZ ) ) { /* behave like gzip */
suffix = GZ_EXTENSION ; FIO_setCompressionType ( prefs , FIO_gzipCompression ) ; FIO_setRemoveSrcFile ( prefs , 1 ) ;
dictCLevel = cLevel = 6 ; /* gzip default is -6 */
}
2019-01-23 09:31:13 +08:00
if ( exeNameMatch ( programName , ZSTD_GUNZIP ) ) { operation = zom_decompress ; FIO_setRemoveSrcFile ( prefs , 1 ) ; } /* behave like gunzip, also supports multiple formats */
2022-08-05 08:15:59 +08:00
if ( exeNameMatch ( programName , ZSTD_GZCAT ) ) { operation = zom_decompress ; FIO_overwriteMode ( prefs ) ; forceStdout = 1 ; followLinks = 1 ; FIO_setPassThroughFlag ( prefs , 1 ) ; outFileName = stdoutmark ; g_displayLevel = 1 ; } /* behave like gzcat, also supports multiple formats */
2019-01-23 09:31:13 +08:00
if ( exeNameMatch ( programName , ZSTD_LZMA ) ) { suffix = LZMA_EXTENSION ; FIO_setCompressionType ( prefs , FIO_lzmaCompression ) ; FIO_setRemoveSrcFile ( prefs , 1 ) ; } /* behave like lzma */
if ( exeNameMatch ( programName , ZSTD_UNLZMA ) ) { operation = zom_decompress ; FIO_setCompressionType ( prefs , FIO_lzmaCompression ) ; FIO_setRemoveSrcFile ( prefs , 1 ) ; } /* behave like unlzma, also supports multiple formats */
if ( exeNameMatch ( programName , ZSTD_XZ ) ) { suffix = XZ_EXTENSION ; FIO_setCompressionType ( prefs , FIO_xzCompression ) ; FIO_setRemoveSrcFile ( prefs , 1 ) ; } /* behave like xz */
if ( exeNameMatch ( programName , ZSTD_UNXZ ) ) { operation = zom_decompress ; FIO_setCompressionType ( prefs , FIO_xzCompression ) ; FIO_setRemoveSrcFile ( prefs , 1 ) ; } /* behave like unxz, also supports multiple formats */
if ( exeNameMatch ( programName , ZSTD_LZ4 ) ) { suffix = LZ4_EXTENSION ; FIO_setCompressionType ( prefs , FIO_lz4Compression ) ; } /* behave like lz4 */
if ( exeNameMatch ( programName , ZSTD_UNLZ4 ) ) { operation = zom_decompress ; FIO_setCompressionType ( prefs , FIO_lz4Compression ) ; } /* behave like unlz4, also supports multiple formats */
2016-12-13 19:18:07 +08:00
memset ( & compressionParams , 0 , sizeof ( compressionParams ) ) ;
2016-05-29 11:16:05 +08:00
2018-09-07 09:46:52 +08:00
/* init crash handler */
2018-09-12 02:56:50 +08:00
FIO_addAbortHandler ( ) ;
2018-09-07 09:46:52 +08:00
2016-05-29 11:16:05 +08:00
/* command switches */
2016-10-15 05:07:11 +08:00
for ( argNb = 1 ; argNb < argCount ; argNb + + ) {
2016-05-29 11:16:05 +08:00
const char * argument = argv [ argNb ] ;
2019-11-06 09:02:43 +08:00
if ( ! argument ) continue ; /* Protection if argument empty */
if ( nextArgumentsAreFiles ) {
UTIL_refFilename ( filenames , argument ) ;
continue ;
}
/* "-" means stdin/stdout */
if ( ! strcmp ( argument , " - " ) ) {
UTIL_refFilename ( filenames , stdinmark ) ;
continue ;
}
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
/* Decode commands (note : aggregated commands are allowed) */
if ( argument [ 0 ] = = ' - ' ) {
if ( argument [ 1 ] = = ' - ' ) {
/* long commands (--long-word) */
if ( ! strcmp ( argument , " -- " ) ) { nextArgumentsAreFiles = 1 ; continue ; } /* only file names allowed from now on */
if ( ! strcmp ( argument , " --list " ) ) { operation = zom_list ; continue ; }
if ( ! strcmp ( argument , " --compress " ) ) { operation = zom_compress ; continue ; }
if ( ! strcmp ( argument , " --decompress " ) ) { operation = zom_decompress ; continue ; }
if ( ! strcmp ( argument , " --uncompress " ) ) { operation = zom_decompress ; continue ; }
2021-05-05 04:24:46 +08:00
if ( ! strcmp ( argument , " --force " ) ) { FIO_overwriteMode ( prefs ) ; forceStdin = 1 ; forceStdout = 1 ; followLinks = 1 ; allowBlockDevices = 1 ; continue ; }
2019-12-03 08:08:08 +08:00
if ( ! strcmp ( argument , " --version " ) ) { printVersion ( ) ; CLEAN_RETURN ( 0 ) ; }
if ( ! strcmp ( argument , " --help " ) ) { usage_advanced ( programName ) ; CLEAN_RETURN ( 0 ) ; }
2019-11-06 09:02:43 +08:00
if ( ! strcmp ( argument , " --verbose " ) ) { g_displayLevel + + ; continue ; }
if ( ! strcmp ( argument , " --quiet " ) ) { g_displayLevel - - ; continue ; }
2022-02-08 03:39:15 +08:00
if ( ! strcmp ( argument , " --stdout " ) ) { forceStdout = 1 ; outFileName = stdoutmark ; FIO_setRemoveSrcFile ( prefs , 0 ) ; g_displayLevel - = ( g_displayLevel = = 2 ) ; continue ; }
2019-11-06 09:02:43 +08:00
if ( ! strcmp ( argument , " --ultra " ) ) { ultra = 1 ; continue ; }
if ( ! strcmp ( argument , " --check " ) ) { FIO_setChecksumFlag ( prefs , 2 ) ; continue ; }
if ( ! strcmp ( argument , " --no-check " ) ) { FIO_setChecksumFlag ( prefs , 0 ) ; continue ; }
if ( ! strcmp ( argument , " --sparse " ) ) { FIO_setSparseWrite ( prefs , 2 ) ; continue ; }
if ( ! strcmp ( argument , " --no-sparse " ) ) { FIO_setSparseWrite ( prefs , 0 ) ; continue ; }
2022-08-05 08:15:59 +08:00
if ( ! strcmp ( argument , " --pass-through " ) ) { FIO_setPassThroughFlag ( prefs , 1 ) ; continue ; }
if ( ! strcmp ( argument , " --no-pass-through " ) ) { FIO_setPassThroughFlag ( prefs , 0 ) ; continue ; }
2019-11-06 09:02:43 +08:00
if ( ! strcmp ( argument , " --test " ) ) { operation = zom_test ; continue ; }
2022-01-22 05:55:41 +08:00
if ( ! strcmp ( argument , " --asyncio " ) ) { FIO_setAsyncIOFlag ( prefs , 1 ) ; continue ; }
if ( ! strcmp ( argument , " --no-asyncio " ) ) { FIO_setAsyncIOFlag ( prefs , 0 ) ; continue ; }
2019-11-06 09:02:43 +08:00
if ( ! strcmp ( argument , " --train " ) ) { operation = zom_train ; if ( outFileName = = NULL ) outFileName = g_defaultDictName ; continue ; }
if ( ! strcmp ( argument , " --no-dictID " ) ) { FIO_setDictIDFlag ( prefs , 0 ) ; continue ; }
if ( ! strcmp ( argument , " --keep " ) ) { FIO_setRemoveSrcFile ( prefs , 0 ) ; continue ; }
if ( ! strcmp ( argument , " --rm " ) ) { FIO_setRemoveSrcFile ( prefs , 1 ) ; continue ; }
if ( ! strcmp ( argument , " --priority=rt " ) ) { setRealTimePrio = 1 ; continue ; }
2020-01-31 06:12:03 +08:00
if ( ! strcmp ( argument , " --show-default-cparams " ) ) { showDefaultCParams = 1 ; continue ; }
2020-03-10 03:19:05 +08:00
if ( ! strcmp ( argument , " --content-size " ) ) { contentSize = 1 ; continue ; }
2020-03-10 03:12:52 +08:00
if ( ! strcmp ( argument , " --no-content-size " ) ) { contentSize = 0 ; continue ; }
2019-11-06 09:02:43 +08:00
if ( ! strcmp ( argument , " --adapt " ) ) { adapt = 1 ; continue ; }
2020-11-03 09:52:29 +08:00
if ( ! strcmp ( argument , " --no-row-match-finder " ) ) { useRowMatchFinder = 1 ; continue ; }
if ( ! strcmp ( argument , " --row-match-finder " ) ) { useRowMatchFinder = 2 ; continue ; }
2019-12-03 08:08:08 +08:00
if ( longCommandWArg ( & argument , " --adapt= " ) ) { adapt = 1 ; if ( ! parseAdaptParameters ( argument , & adaptMin , & adaptMax ) ) { badusage ( programName ) ; CLEAN_RETURN ( 1 ) ; } continue ; }
2019-11-06 09:02:43 +08:00
if ( ! strcmp ( argument , " --single-thread " ) ) { nbWorkers = 0 ; singleThread = 1 ; continue ; }
if ( ! strcmp ( argument , " --format=zstd " ) ) { suffix = ZSTD_EXTENSION ; FIO_setCompressionType ( prefs , FIO_zstdCompression ) ; continue ; }
2017-03-02 08:49:20 +08:00
# ifdef ZSTD_GZCOMPRESS
2019-11-06 09:02:43 +08:00
if ( ! strcmp ( argument , " --format=gzip " ) ) { suffix = GZ_EXTENSION ; FIO_setCompressionType ( prefs , FIO_gzipCompression ) ; continue ; }
2022-02-09 04:32:42 +08:00
if ( exeNameMatch ( programName , ZSTD_GZ ) ) { /* behave like gzip */
if ( ! strcmp ( argument , " --best " ) ) { dictCLevel = cLevel = 9 ; continue ; }
if ( ! strcmp ( argument , " --no-name " ) ) { /* ignore for now */ ; continue ; }
}
2017-03-02 08:49:20 +08:00
# endif
2017-03-14 09:11:07 +08:00
# ifdef ZSTD_LZMACOMPRESS
2019-11-06 09:02:43 +08:00
if ( ! strcmp ( argument , " --format=lzma " ) ) { suffix = LZMA_EXTENSION ; FIO_setCompressionType ( prefs , FIO_lzmaCompression ) ; continue ; }
if ( ! strcmp ( argument , " --format=xz " ) ) { suffix = XZ_EXTENSION ; FIO_setCompressionType ( prefs , FIO_xzCompression ) ; continue ; }
2017-03-14 09:11:07 +08:00
# endif
2017-04-25 07:48:25 +08:00
# ifdef ZSTD_LZ4COMPRESS
2019-11-06 09:02:43 +08:00
if ( ! strcmp ( argument , " --format=lz4 " ) ) { suffix = LZ4_EXTENSION ; FIO_setCompressionType ( prefs , FIO_lz4Compression ) ; continue ; }
2017-04-25 07:48:25 +08:00
# endif
2019-11-06 09:02:43 +08:00
if ( ! strcmp ( argument , " --rsyncable " ) ) { rsyncable = 1 ; continue ; }
2021-09-20 21:04:07 +08:00
if ( ! strcmp ( argument , " --compress-literals " ) ) { literalCompressionMode = ZSTD_ps_enable ; continue ; }
if ( ! strcmp ( argument , " --no-compress-literals " ) ) { literalCompressionMode = ZSTD_ps_disable ; continue ; }
2021-05-07 02:50:28 +08:00
if ( ! strcmp ( argument , " --no-progress " ) ) { FIO_setProgressSetting ( FIO_ps_never ) ; continue ; }
if ( ! strcmp ( argument , " --progress " ) ) { FIO_setProgressSetting ( FIO_ps_always ) ; continue ; }
2019-11-06 09:02:43 +08:00
if ( ! strcmp ( argument , " --exclude-compressed " ) ) { FIO_setExcludeCompressedFile ( prefs , 1 ) ; continue ; }
2022-01-15 04:37:32 +08:00
if ( ! strcmp ( argument , " --fake-stdin-is-console " ) ) { UTIL_fakeStdinIsConsole ( ) ; continue ; }
if ( ! strcmp ( argument , " --fake-stdout-is-console " ) ) { UTIL_fakeStdoutIsConsole ( ) ; continue ; }
if ( ! strcmp ( argument , " --fake-stderr-is-console " ) ) { UTIL_fakeStderrIsConsole ( ) ; continue ; }
2020-07-21 08:41:32 +08:00
2019-11-06 09:02:43 +08:00
/* long commands with arguments */
2017-03-02 08:49:20 +08:00
# ifndef ZSTD_NODICT
2019-11-06 09:02:43 +08:00
if ( longCommandWArg ( & argument , " --train-cover " ) ) {
operation = zom_train ;
if ( outFileName = = NULL )
outFileName = g_defaultDictName ;
dict = cover ;
/* Allow optional arguments following an = */
if ( * argument = = 0 ) { memset ( & coverParams , 0 , sizeof ( coverParams ) ) ; }
2019-12-03 08:08:08 +08:00
else if ( * argument + + ! = ' = ' ) { badusage ( programName ) ; CLEAN_RETURN ( 1 ) ; }
else if ( ! parseCoverParameters ( argument , & coverParams ) ) { badusage ( programName ) ; CLEAN_RETURN ( 1 ) ; }
2019-11-06 09:02:43 +08:00
continue ;
}
if ( longCommandWArg ( & argument , " --train-fastcover " ) ) {
operation = zom_train ;
if ( outFileName = = NULL )
outFileName = g_defaultDictName ;
dict = fastCover ;
/* Allow optional arguments following an = */
if ( * argument = = 0 ) { memset ( & fastCoverParams , 0 , sizeof ( fastCoverParams ) ) ; }
2019-12-03 08:08:08 +08:00
else if ( * argument + + ! = ' = ' ) { badusage ( programName ) ; CLEAN_RETURN ( 1 ) ; }
else if ( ! parseFastCoverParameters ( argument , & fastCoverParams ) ) { badusage ( programName ) ; CLEAN_RETURN ( 1 ) ; }
2019-11-06 09:02:43 +08:00
continue ;
}
if ( longCommandWArg ( & argument , " --train-legacy " ) ) {
operation = zom_train ;
if ( outFileName = = NULL )
outFileName = g_defaultDictName ;
dict = legacy ;
/* Allow optional arguments following an = */
if ( * argument = = 0 ) { continue ; }
2019-12-03 08:08:08 +08:00
else if ( * argument + + ! = ' = ' ) { badusage ( programName ) ; CLEAN_RETURN ( 1 ) ; }
else if ( ! parseLegacyParameters ( argument , & dictSelect ) ) { badusage ( programName ) ; CLEAN_RETURN ( 1 ) ; }
2019-11-06 09:02:43 +08:00
continue ;
}
2017-01-01 13:08:24 +08:00
# endif
2020-07-21 08:41:32 +08:00
if ( longCommandWArg ( & argument , " --threads " ) ) { NEXT_UINT32 ( nbWorkers ) ; continue ; }
if ( longCommandWArg ( & argument , " --memlimit " ) ) { NEXT_UINT32 ( memLimit ) ; continue ; }
if ( longCommandWArg ( & argument , " --memory " ) ) { NEXT_UINT32 ( memLimit ) ; continue ; }
if ( longCommandWArg ( & argument , " --memlimit-decompress " ) ) { NEXT_UINT32 ( memLimit ) ; continue ; }
2022-09-22 04:20:01 +08:00
if ( longCommandWArg ( & argument , " --block-size " ) ) { NEXT_TSIZE ( blockSize ) ; continue ; }
2020-07-21 08:41:32 +08:00
if ( longCommandWArg ( & argument , " --maxdict " ) ) { NEXT_UINT32 ( maxDictSize ) ; continue ; }
if ( longCommandWArg ( & argument , " --dictID " ) ) { NEXT_UINT32 ( dictID ) ; continue ; }
2019-12-03 08:08:08 +08:00
if ( longCommandWArg ( & argument , " --zstd= " ) ) { if ( ! parseCompressionParameters ( argument , & compressionParams ) ) { badusage ( programName ) ; CLEAN_RETURN ( 1 ) ; } continue ; }
2022-09-22 04:20:01 +08:00
if ( longCommandWArg ( & argument , " --stream-size " ) ) { NEXT_TSIZE ( streamSrcSize ) ; continue ; }
if ( longCommandWArg ( & argument , " --target-compressed-block-size " ) ) { NEXT_TSIZE ( targetCBlockSize ) ; continue ; }
if ( longCommandWArg ( & argument , " --size-hint " ) ) { NEXT_TSIZE ( srcSizeHint ) ; continue ; }
2022-07-30 05:44:22 +08:00
if ( longCommandWArg ( & argument , " --output-dir-flat " ) ) {
NEXT_FIELD ( outDirName ) ;
if ( strlen ( outDirName ) = = 0 ) {
DISPLAY ( " error: output dir cannot be empty string (did you mean to pass '.' instead?) \n " ) ;
CLEAN_RETURN ( 1 ) ;
}
continue ;
}
2021-06-16 21:38:43 +08:00
if ( longCommandWArg ( & argument , " --auto-threads " ) ) {
const char * threadDefault = NULL ;
NEXT_FIELD ( threadDefault ) ;
if ( strcmp ( threadDefault , " logical " ) = = 0 )
defaultLogicalCores = 1 ;
continue ;
}
2020-07-21 08:41:32 +08:00
# ifdef UTIL_HAS_MIRRORFILELIST
2022-07-30 05:44:22 +08:00
if ( longCommandWArg ( & argument , " --output-dir-mirror " ) ) {
NEXT_FIELD ( outMirroredDirName ) ;
if ( strlen ( outMirroredDirName ) = = 0 ) {
DISPLAY ( " error: output dir cannot be empty string (did you mean to pass '.' instead?) \n " ) ;
CLEAN_RETURN ( 1 ) ;
}
continue ;
}
2021-02-04 11:53:00 +08:00
# endif
# ifndef ZSTD_NOTRACE
if ( longCommandWArg ( & argument , " --trace " ) ) { char const * traceFile ; NEXT_FIELD ( traceFile ) ; TRACE_enable ( traceFile ) ; continue ; }
2020-07-21 08:41:32 +08:00
# endif
if ( longCommandWArg ( & argument , " --patch-from " ) ) { NEXT_FIELD ( patchFromDictFileName ) ; continue ; }
2021-06-10 03:35:43 +08:00
if ( longCommandWArg ( & argument , " --long " ) ) {
unsigned ldmWindowLog = 0 ;
ldmFlag = 1 ;
/* Parse optional window log */
if ( * argument = = ' = ' ) {
+ + argument ;
ldmWindowLog = readU32FromChar ( & argument ) ;
} else if ( * argument ! = 0 ) {
/* Invalid character following --long */
badusage ( programName ) ;
CLEAN_RETURN ( 1 ) ;
2022-08-05 04:51:14 +08:00
} else {
ldmWindowLog = g_defaultMaxWindowLog ;
2021-06-10 03:35:43 +08:00
}
/* Only set windowLog if not already set by --zstd */
if ( compressionParams . windowLog = = 0 )
compressionParams . windowLog = ldmWindowLog ;
continue ;
}
2018-09-23 08:21:39 +08:00
# ifndef ZSTD_NOCOMPRESS /* linking ZSTD_minCLevel() requires compression support */
2019-11-06 09:02:43 +08:00
if ( longCommandWArg ( & argument , " --fast " ) ) {
/* Parse optional acceleration factor */
if ( * argument = = ' = ' ) {
U32 const maxFast = ( U32 ) - ZSTD_minCLevel ( ) ;
U32 fastLevel ;
+ + argument ;
fastLevel = readU32FromChar ( & argument ) ;
if ( fastLevel > maxFast ) fastLevel = maxFast ;
if ( fastLevel ) {
2019-12-03 08:08:08 +08:00
dictCLevel = cLevel = - ( int ) fastLevel ;
2018-03-12 10:56:48 +08:00
} else {
2019-12-03 08:08:08 +08:00
badusage ( programName ) ;
CLEAN_RETURN ( 1 ) ;
2018-03-12 10:56:48 +08:00
}
2019-11-06 09:02:43 +08:00
} else if ( * argument ! = 0 ) {
/* Invalid character following --fast */
2019-12-03 08:08:08 +08:00
badusage ( programName ) ;
CLEAN_RETURN ( 1 ) ;
2019-11-06 09:02:43 +08:00
} else {
cLevel = - 1 ; /* default for --fast */
2018-03-12 10:56:48 +08:00
}
2019-11-06 09:02:43 +08:00
continue ;
}
2018-09-23 08:21:39 +08:00
# endif
2019-10-15 14:49:13 +08:00
2020-07-21 08:41:32 +08:00
if ( longCommandWArg ( & argument , " --filelist " ) ) {
const char * listName ;
NEXT_FIELD ( listName ) ;
UTIL_refFilename ( file_of_names , listName ) ;
2019-11-06 09:02:43 +08:00
continue ;
}
2019-10-15 14:49:13 +08:00
2019-11-06 09:02:43 +08:00
/* fall-through, will trigger bad_usage() later on */
}
2019-10-15 14:49:13 +08:00
2019-11-06 09:02:43 +08:00
argument + + ;
while ( argument [ 0 ] ! = 0 ) {
2020-07-18 04:09:23 +08:00
2016-09-02 06:05:57 +08:00
# ifndef ZSTD_NOCOMPRESS
2019-11-06 09:02:43 +08:00
/* compression Level */
if ( ( * argument > = ' 0 ' ) & & ( * argument < = ' 9 ' ) ) {
dictCLevel = cLevel = ( int ) readU32FromChar ( & argument ) ;
continue ;
}
2016-09-02 06:05:57 +08:00
# endif
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
switch ( argument [ 0 ] )
{
/* Display help */
2019-12-03 08:08:08 +08:00
case ' V ' : printVersion ( ) ; CLEAN_RETURN ( 0 ) ; /* Version Only */
2022-03-10 08:34:12 +08:00
case ' H ' : usage_advanced ( programName ) ; CLEAN_RETURN ( 0 ) ;
case ' h ' : usage ( stdout , programName ) ; CLEAN_RETURN ( 0 ) ;
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
/* Compress */
case ' z ' : operation = zom_compress ; argument + + ; break ;
2016-10-29 04:58:31 +08:00
2019-11-06 09:02:43 +08:00
/* Decoding */
case ' d ' :
2016-12-07 09:56:20 +08:00
# ifndef ZSTD_NOBENCH
2019-11-06 09:02:43 +08:00
benchParams . mode = BMK_decodeOnly ;
if ( operation = = zom_bench ) { argument + + ; break ; } /* benchmark decode (hidden option) */
2016-12-07 09:56:20 +08:00
# endif
2019-11-06 09:02:43 +08:00
operation = zom_decompress ; argument + + ; break ;
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
/* Force stdout, even if stdout==console */
2022-02-08 03:39:15 +08:00
case ' c ' : forceStdout = 1 ; outFileName = stdoutmark ; FIO_setRemoveSrcFile ( prefs , 0 ) ; argument + + ; break ;
2016-05-29 11:16:05 +08:00
2022-02-09 04:32:42 +08:00
/* do not store filename - gzip compatibility - nothing to do */
case ' n ' : argument + + ; break ;
2019-11-06 09:02:43 +08:00
/* Use file content as dictionary */
2020-07-21 08:41:32 +08:00
case ' D ' : argument + + ; NEXT_FIELD ( dictFileName ) ; break ;
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
/* Overwrite */
2021-05-05 04:24:46 +08:00
case ' f ' : FIO_overwriteMode ( prefs ) ; forceStdin = 1 ; forceStdout = 1 ; followLinks = 1 ; allowBlockDevices = 1 ; argument + + ; break ;
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
/* Verbose mode */
case ' v ' : g_displayLevel + + ; argument + + ; break ;
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
/* Quiet mode */
case ' q ' : g_displayLevel - - ; argument + + ; break ;
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
/* keep source file (default) */
case ' k ' : FIO_setRemoveSrcFile ( prefs , 0 ) ; argument + + ; break ;
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
/* Checksum */
case ' C ' : FIO_setChecksumFlag ( prefs , 2 ) ; argument + + ; break ;
2016-06-02 23:05:50 +08:00
2019-11-06 09:02:43 +08:00
/* test compressed file */
case ' t ' : operation = zom_test ; argument + + ; break ;
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
/* destination file name */
2020-07-21 08:41:32 +08:00
case ' o ' : argument + + ; NEXT_FIELD ( outFileName ) ; break ;
2019-10-18 06:27:25 +08:00
2020-01-11 06:25:24 +08:00
/* limit memory */
2019-11-06 09:02:43 +08:00
case ' M ' :
argument + + ;
memLimit = readU32FromChar ( & argument ) ;
break ;
case ' l ' : operation = zom_list ; argument + + ; break ;
2016-09-13 23:50:08 +08:00
# ifdef UTIL_HAS_CREATEFILELIST
2019-11-06 09:02:43 +08:00
/* recursive */
case ' r ' : recursive = 1 ; argument + + ; break ;
2016-09-13 23:50:08 +08:00
# endif
2016-05-29 11:16:05 +08:00
2016-09-02 06:05:57 +08:00
# ifndef ZSTD_NOBENCH
2019-11-06 09:02:43 +08:00
/* Benchmark */
case ' b ' :
operation = zom_bench ;
argument + + ;
break ;
/* range bench (benchmark only) */
case ' e ' :
/* compression Level */
argument + + ;
cLevelLast = ( int ) readU32FromChar ( & argument ) ;
break ;
/* Modify Nb Iterations (benchmark only) */
case ' i ' :
argument + + ;
bench_nbSeconds = readU32FromChar ( & argument ) ;
break ;
/* cut input into blocks (benchmark only) */
case ' B ' :
argument + + ;
blockSize = readU32FromChar ( & argument ) ;
break ;
/* benchmark files separately (hidden option) */
case ' S ' :
argument + + ;
separateFiles = 1 ;
break ;
2017-11-17 16:22:55 +08:00
2017-01-20 08:59:56 +08:00
# endif /* ZSTD_NOBENCH */
2019-11-06 09:02:43 +08:00
/* nb of threads (hidden option) */
case ' T ' :
argument + + ;
fileio_types.h : avoid dependency on mem.h
fileio_types.h cannot be parsed by itself
because it relies on basic types defined in `lib/common/mem.h`.
As for #3231, it likely wasn't detected because `mem.h` was probably included before within target files.
But this is not proper.
A "easy" solution would be to add the missing include,
but each dependency should be considered "bad" by default,
and only allowed if it brings some tangible value.
In this case, since these types are only used to declare internal structure variables
which are effectively only flags,
I believe it's really not valuable to add a dependency on `mem.h` for this purpose
while the standard `int` type can do the same job.
I was expecting some compiler warnings following this change,
but it turns out we don't use `-Wconversion` by default on `zstd` source code,
so there is none.
Nevertheless, I enabled `-Wconversion` locally and proceeded to fix a few conversion warnings in the process.
Adding `-Wconversion` to the list of flags used for `zstd` is something I would be favorable over the long term,
but it cannot be done overnight,
because the nb of places where this warning is triggered is daunting.
Better progressively reduce the nb of triggered `-Wconversion` warnings before enabling this flag by default.
2022-08-04 03:39:35 +08:00
nbWorkers = readU32FromChar ( & argument ) ;
2019-11-06 09:02:43 +08:00
break ;
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
/* Dictionary Selection level */
case ' s ' :
argument + + ;
dictSelect = readU32FromChar ( & argument ) ;
break ;
2016-05-29 11:16:05 +08:00
2019-11-06 09:02:43 +08:00
/* Pause at the end (-p) or set an additional param (-p#) (hidden option) */
case ' p ' : argument + + ;
2016-09-02 06:05:57 +08:00
# ifndef ZSTD_NOBENCH
2019-11-06 09:02:43 +08:00
if ( ( * argument > = ' 0 ' ) & & ( * argument < = ' 9 ' ) ) {
benchParams . additionalParam = ( int ) readU32FromChar ( & argument ) ;
} else
2016-09-02 06:05:57 +08:00
# endif
2019-11-06 09:02:43 +08:00
main_pause = 1 ;
2018-06-20 01:58:22 +08:00
break ;
2019-11-06 09:02:43 +08:00
/* Select compressibility of synthetic sample */
case ' P ' :
2020-07-21 08:41:32 +08:00
argument + + ;
2019-11-06 09:02:43 +08:00
compressibility = ( double ) readU32FromChar ( & argument ) / 100 ;
2020-07-21 08:41:32 +08:00
break ;
2016-07-05 00:16:16 +08:00
2019-11-06 09:02:43 +08:00
/* unknown command */
2019-12-03 08:08:08 +08:00
default : badusage ( programName ) ; CLEAN_RETURN ( 1 ) ;
2019-11-06 09:02:43 +08:00
}
2016-07-05 00:16:16 +08:00
}
2019-11-06 09:02:43 +08:00
continue ;
} /* if (argument[0]=='-') */
/* none of the above : add filename to list */
UTIL_refFilename ( filenames , argument ) ;
2016-05-29 11:16:05 +08:00
}
/* Welcome message (if verbose) */
DISPLAYLEVEL ( 3 , WELCOME_MESSAGE ) ;
2018-02-14 06:56:35 +08:00
# ifdef ZSTD_MULTITHREAD
2022-07-30 03:51:58 +08:00
if ( ( operation = = zom_decompress ) & & ( ! singleThread ) & & ( nbWorkers > 1 ) ) {
DISPLAYLEVEL ( 2 , " Warning : decompression does not support multi-threading \n " ) ;
}
2018-02-14 06:56:35 +08:00
if ( ( nbWorkers = = 0 ) & & ( ! singleThread ) ) {
2018-02-02 11:29:30 +08:00
/* automatically set # workers based on # of reported cpus */
2021-06-16 21:38:43 +08:00
if ( defaultLogicalCores ) {
fileio_types.h : avoid dependency on mem.h
fileio_types.h cannot be parsed by itself
because it relies on basic types defined in `lib/common/mem.h`.
As for #3231, it likely wasn't detected because `mem.h` was probably included before within target files.
But this is not proper.
A "easy" solution would be to add the missing include,
but each dependency should be considered "bad" by default,
and only allowed if it brings some tangible value.
In this case, since these types are only used to declare internal structure variables
which are effectively only flags,
I believe it's really not valuable to add a dependency on `mem.h` for this purpose
while the standard `int` type can do the same job.
I was expecting some compiler warnings following this change,
but it turns out we don't use `-Wconversion` by default on `zstd` source code,
so there is none.
Nevertheless, I enabled `-Wconversion` locally and proceeded to fix a few conversion warnings in the process.
Adding `-Wconversion` to the list of flags used for `zstd` is something I would be favorable over the long term,
but it cannot be done overnight,
because the nb of places where this warning is triggered is daunting.
Better progressively reduce the nb of triggered `-Wconversion` warnings before enabling this flag by default.
2022-08-04 03:39:35 +08:00
nbWorkers = ( unsigned ) UTIL_countLogicalCores ( ) ;
2021-06-16 21:38:43 +08:00
DISPLAYLEVEL ( 3 , " Note: %d logical core(s) detected \n " , nbWorkers ) ;
} else {
fileio_types.h : avoid dependency on mem.h
fileio_types.h cannot be parsed by itself
because it relies on basic types defined in `lib/common/mem.h`.
As for #3231, it likely wasn't detected because `mem.h` was probably included before within target files.
But this is not proper.
A "easy" solution would be to add the missing include,
but each dependency should be considered "bad" by default,
and only allowed if it brings some tangible value.
In this case, since these types are only used to declare internal structure variables
which are effectively only flags,
I believe it's really not valuable to add a dependency on `mem.h` for this purpose
while the standard `int` type can do the same job.
I was expecting some compiler warnings following this change,
but it turns out we don't use `-Wconversion` by default on `zstd` source code,
so there is none.
Nevertheless, I enabled `-Wconversion` locally and proceeded to fix a few conversion warnings in the process.
Adding `-Wconversion` to the list of flags used for `zstd` is something I would be favorable over the long term,
but it cannot be done overnight,
because the nb of places where this warning is triggered is daunting.
Better progressively reduce the nb of triggered `-Wconversion` warnings before enabling this flag by default.
2022-08-04 03:39:35 +08:00
nbWorkers = ( unsigned ) UTIL_countPhysicalCores ( ) ;
2021-06-16 21:38:43 +08:00
DISPLAYLEVEL ( 3 , " Note: %d physical core(s) detected \n " , nbWorkers ) ;
}
2017-04-14 03:28:28 +08:00
}
2018-06-26 16:22:45 +08:00
# else
2022-01-21 14:45:03 +08:00
( void ) singleThread ; ( void ) nbWorkers ; ( void ) defaultLogicalCores ;
2018-02-14 06:56:35 +08:00
# endif
2017-03-24 02:52:09 +08:00
2017-03-24 03:09:35 +08:00
g_utilDisplayLevel = g_displayLevel ;
2021-06-10 03:22:59 +08:00
# ifdef UTIL_HAS_CREATEFILELIST
2017-03-24 02:52:09 +08:00
if ( ! followLinks ) {
2019-11-06 09:02:43 +08:00
unsigned u , fileNamesNb ;
unsigned const nbFilenames = ( unsigned ) filenames - > tableSize ;
for ( u = 0 , fileNamesNb = 0 ; u < nbFilenames ; u + + ) {
2019-11-27 03:20:26 +08:00
if ( UTIL_isLink ( filenames - > fileNames [ u ] )
& & ! UTIL_isFIFO ( filenames - > fileNames [ u ] )
2019-10-26 05:06:50 +08:00
) {
2019-11-27 03:20:26 +08:00
DISPLAYLEVEL ( 2 , " Warning : %s is a symbolic link, ignoring \n " , filenames - > fileNames [ u ] ) ;
2017-03-24 02:52:09 +08:00
} else {
2019-11-06 09:02:43 +08:00
filenames - > fileNames [ fileNamesNb + + ] = filenames - > fileNames [ u ] ;
2019-10-26 09:16:45 +08:00
} }
2019-11-06 09:02:43 +08:00
if ( fileNamesNb = = 0 & & nbFilenames > 0 ) /* all names are eliminated */
2019-02-12 07:07:32 +08:00
CLEAN_RETURN ( 1 ) ;
2019-11-06 09:02:43 +08:00
filenames - > tableSize = fileNamesNb ;
} /* if (!followLinks) */
/* read names from a file */
if ( file_of_names - > tableSize ) {
size_t const nbFileLists = file_of_names - > tableSize ;
size_t flNb ;
for ( flNb = 0 ; flNb < nbFileLists ; flNb + + ) {
FileNamesTable * const fnt = UTIL_createFileNamesTable_fromFileName ( file_of_names - > fileNames [ flNb ] ) ;
if ( fnt = = NULL ) {
DISPLAYLEVEL ( 1 , " zstd: error reading %s \n " , file_of_names - > fileNames [ flNb ] ) ;
CLEAN_RETURN ( 1 ) ;
}
2019-11-07 01:10:05 +08:00
filenames = UTIL_mergeFileNamesTable ( filenames , fnt ) ;
2019-11-06 09:02:43 +08:00
}
2017-03-24 02:52:09 +08:00
}
2019-11-06 09:02:43 +08:00
2022-01-25 09:42:21 +08:00
nbInputFileNames = filenames - > tableSize ; /* saving number of input files */
2022-02-08 03:39:15 +08:00
2016-09-21 18:24:43 +08:00
if ( recursive ) { /* at this stage, filenameTable is a list of paths, which can contain both files and directories */
2019-11-07 01:10:05 +08:00
UTIL_expandFNT ( & filenames , followLinks ) ;
2019-11-06 09:02:43 +08:00
}
2018-06-26 16:22:45 +08:00
# else
( void ) followLinks ;
2016-05-29 11:16:05 +08:00
# endif
2017-08-19 09:30:41 +08:00
2017-06-16 08:46:49 +08:00
if ( operation = = zom_list ) {
2017-08-19 09:30:41 +08:00
# ifndef ZSTD_NODECOMPRESS
2019-11-06 09:02:43 +08:00
int const ret = FIO_listMultipleFiles ( ( unsigned ) filenames - > tableSize , filenames - > fileNames , g_displayLevel ) ;
2017-06-21 03:43:10 +08:00
CLEAN_RETURN ( ret ) ;
2017-08-19 09:30:41 +08:00
# else
DISPLAY ( " file information is not supported \n " ) ;
CLEAN_RETURN ( 1 ) ;
# endif
2017-06-06 05:45:31 +08:00
}
2017-08-19 09:30:41 +08:00
2016-05-29 11:16:05 +08:00
/* Check if benchmark is selected */
2016-10-29 04:58:31 +08:00
if ( operation = = zom_bench ) {
2016-05-29 11:16:05 +08:00
# ifndef ZSTD_NOBENCH
2018-08-03 22:54:29 +08:00
benchParams . blockSize = blockSize ;
fileio_types.h : avoid dependency on mem.h
fileio_types.h cannot be parsed by itself
because it relies on basic types defined in `lib/common/mem.h`.
As for #3231, it likely wasn't detected because `mem.h` was probably included before within target files.
But this is not proper.
A "easy" solution would be to add the missing include,
but each dependency should be considered "bad" by default,
and only allowed if it brings some tangible value.
In this case, since these types are only used to declare internal structure variables
which are effectively only flags,
I believe it's really not valuable to add a dependency on `mem.h` for this purpose
while the standard `int` type can do the same job.
I was expecting some compiler warnings following this change,
but it turns out we don't use `-Wconversion` by default on `zstd` source code,
so there is none.
Nevertheless, I enabled `-Wconversion` locally and proceeded to fix a few conversion warnings in the process.
Adding `-Wconversion` to the list of flags used for `zstd` is something I would be favorable over the long term,
but it cannot be done overnight,
because the nb of places where this warning is triggered is daunting.
Better progressively reduce the nb of triggered `-Wconversion` warnings before enabling this flag by default.
2022-08-04 03:39:35 +08:00
benchParams . nbWorkers = ( int ) nbWorkers ;
2019-10-18 06:27:25 +08:00
benchParams . realTime = ( unsigned ) setRealTimePrio ;
2018-08-03 22:54:29 +08:00
benchParams . nbSeconds = bench_nbSeconds ;
benchParams . ldmFlag = ldmFlag ;
2019-10-18 06:27:25 +08:00
benchParams . ldmMinMatch = ( int ) g_ldmMinMatch ;
benchParams . ldmHashLog = ( int ) g_ldmHashLog ;
2020-11-03 09:52:29 +08:00
benchParams . useRowMatchFinder = useRowMatchFinder ;
2017-09-03 12:10:36 +08:00
if ( g_ldmBucketSizeLog ! = LDM_PARAM_DEFAULT ) {
2019-10-18 06:27:25 +08:00
benchParams . ldmBucketSizeLog = ( int ) g_ldmBucketSizeLog ;
2017-09-03 12:10:36 +08:00
}
2018-11-22 06:36:57 +08:00
if ( g_ldmHashRateLog ! = LDM_PARAM_DEFAULT ) {
2019-10-18 06:27:25 +08:00
benchParams . ldmHashRateLog = ( int ) g_ldmHashRateLog ;
2017-09-03 12:10:36 +08:00
}
2019-02-16 07:24:55 +08:00
benchParams . literalCompressionMode = literalCompressionMode ;
2018-06-16 04:21:08 +08:00
2018-06-19 06:06:31 +08:00
if ( cLevel > ZSTD_maxCLevel ( ) ) cLevel = ZSTD_maxCLevel ( ) ;
if ( cLevelLast > ZSTD_maxCLevel ( ) ) cLevelLast = ZSTD_maxCLevel ( ) ;
if ( cLevelLast < cLevel ) cLevelLast = cLevel ;
2018-07-10 09:24:07 +08:00
if ( cLevelLast > cLevel )
2018-08-29 02:21:09 +08:00
DISPLAYLEVEL ( 3 , " Benchmarking levels from %d to %d \n " , cLevel , cLevelLast ) ;
2019-11-06 09:02:43 +08:00
if ( filenames - > tableSize > 0 ) {
2018-06-20 01:58:22 +08:00
if ( separateFiles ) {
unsigned i ;
2019-11-06 09:02:43 +08:00
for ( i = 0 ; i < filenames - > tableSize ; i + + ) {
2018-06-20 01:58:22 +08:00
int c ;
2019-11-06 09:02:43 +08:00
DISPLAYLEVEL ( 3 , " Benchmarking %s \n " , filenames - > fileNames [ i ] ) ;
2018-06-20 01:58:22 +08:00
for ( c = cLevel ; c < = cLevelLast ; c + + ) {
2019-11-06 09:02:43 +08:00
BMK_benchFilesAdvanced ( & filenames - > fileNames [ i ] , 1 , dictFileName , c , & compressionParams , g_displayLevel , & benchParams ) ;
2019-10-26 07:36:59 +08:00
} }
2018-06-20 01:58:22 +08:00
} else {
for ( ; cLevel < = cLevelLast ; cLevel + + ) {
2019-11-06 09:02:43 +08:00
BMK_benchFilesAdvanced ( filenames - > fileNames , ( unsigned ) filenames - > tableSize , dictFileName , cLevel , & compressionParams , g_displayLevel , & benchParams ) ;
2019-10-26 07:36:59 +08:00
} }
2018-06-19 06:06:31 +08:00
} else {
for ( ; cLevel < = cLevelLast ; cLevel + + ) {
2018-08-03 22:54:29 +08:00
BMK_syntheticTest ( cLevel , compressibility , & compressionParams , g_displayLevel , & benchParams ) ;
2019-10-26 07:36:59 +08:00
} }
2018-06-16 04:21:08 +08:00
2017-12-02 09:42:46 +08:00
# else
2018-08-03 23:30:01 +08:00
( void ) bench_nbSeconds ; ( void ) blockSize ; ( void ) setRealTimePrio ; ( void ) separateFiles ; ( void ) compressibility ;
2016-05-29 11:16:05 +08:00
# endif
goto _end ;
}
/* Check if dictionary builder is selected */
2016-10-29 04:58:31 +08:00
if ( operation = = zom_train ) {
2016-05-29 11:16:05 +08:00
# ifndef ZSTD_NODICT
2017-06-27 12:07:14 +08:00
ZDICT_params_t zParams ;
zParams . compressionLevel = dictCLevel ;
2019-10-18 06:27:25 +08:00
zParams . notificationLevel = ( unsigned ) g_displayLevel ;
2017-06-27 12:07:14 +08:00
zParams . dictID = dictID ;
2018-08-24 03:06:20 +08:00
if ( dict = = cover ) {
2017-05-02 14:40:20 +08:00
int const optimize = ! coverParams . k | | ! coverParams . d ;
2019-10-18 06:27:25 +08:00
coverParams . nbThreads = ( unsigned ) nbWorkers ;
2017-06-27 12:07:14 +08:00
coverParams . zParams = zParams ;
2021-12-11 05:19:40 +08:00
operationResult = DiB_trainFromFiles ( outFileName , maxDictSize , filenames - > fileNames , ( int ) filenames - > tableSize , blockSize , NULL , & coverParams , NULL , optimize , memLimit ) ;
2018-08-24 03:06:20 +08:00
} else if ( dict = = fastCover ) {
int const optimize = ! fastCoverParams . k | | ! fastCoverParams . d ;
2019-10-18 06:27:25 +08:00
fastCoverParams . nbThreads = ( unsigned ) nbWorkers ;
2018-08-24 03:06:20 +08:00
fastCoverParams . zParams = zParams ;
2021-12-11 05:19:40 +08:00
operationResult = DiB_trainFromFiles ( outFileName , maxDictSize , filenames - > fileNames , ( int ) filenames - > tableSize , blockSize , NULL , NULL , & fastCoverParams , optimize , memLimit ) ;
2017-01-01 13:08:24 +08:00
} else {
2017-06-27 12:07:14 +08:00
ZDICT_legacy_params_t dictParams ;
2017-01-01 13:08:24 +08:00
memset ( & dictParams , 0 , sizeof ( dictParams ) ) ;
dictParams . selectivityLevel = dictSelect ;
2017-06-27 12:07:14 +08:00
dictParams . zParams = zParams ;
2021-12-11 05:19:40 +08:00
operationResult = DiB_trainFromFiles ( outFileName , maxDictSize , filenames - > fileNames , ( int ) filenames - > tableSize , blockSize , & dictParams , NULL , NULL , 0 , memLimit ) ;
2017-01-01 13:08:24 +08:00
}
2018-09-24 15:52:19 +08:00
# else
( void ) dictCLevel ; ( void ) dictSelect ; ( void ) dictID ; ( void ) maxDictSize ; /* not used when ZSTD_NODICT set */
DISPLAYLEVEL ( 1 , " training mode not available \n " ) ;
operationResult = 1 ;
2016-05-29 11:16:05 +08:00
# endif
goto _end ;
}
2017-07-19 05:45:49 +08:00
# ifndef ZSTD_NODECOMPRESS
2019-10-18 07:09:53 +08:00
if ( operation = = zom_test ) { FIO_setTestMode ( prefs , 1 ) ; outFileName = nulmark ; FIO_setRemoveSrcFile ( prefs , 0 ) ; } /* test mode */
2017-07-19 05:45:49 +08:00
# endif
2016-05-29 11:16:05 +08:00
/* No input filename ==> use stdin and stdout */
2022-01-25 09:42:21 +08:00
if ( filenames - > tableSize = = 0 ) {
/* It is possible that the input
was a number of empty directories . In this case
stdin and stdout should not be used */
if ( nbInputFileNames > 0 ) {
2022-01-27 08:51:18 +08:00
DISPLAYLEVEL ( 1 , " please provide correct input file(s) or non-empty directories -- ignored \n " ) ;
CLEAN_RETURN ( 0 ) ;
2022-01-25 09:42:21 +08:00
}
UTIL_refFilename ( filenames , stdinmark ) ;
}
2022-02-08 03:39:15 +08:00
2022-07-30 07:13:07 +08:00
if ( filenames - > tableSize = = 1 & & ! strcmp ( filenames - > fileNames [ 0 ] , stdinmark ) & & ! outFileName )
2018-03-12 10:56:48 +08:00
outFileName = stdoutmark ; /* when input is stdin, default output is stdout */
2016-05-29 11:16:05 +08:00
/* Check if input/output defined as console; trigger an error in this case */
2021-01-12 06:53:20 +08:00
if ( ! forceStdin
2022-07-30 07:13:07 +08:00
& & ( UTIL_searchFileNamesTable ( filenames , stdinmark ) ! = - 1 )
2022-01-15 04:37:32 +08:00
& & UTIL_isConsole ( stdin ) ) {
2020-09-29 00:15:18 +08:00
DISPLAYLEVEL ( 1 , " stdin is a console, aborting \n " ) ;
2019-12-03 08:08:08 +08:00
CLEAN_RETURN ( 1 ) ;
}
2022-07-30 07:13:07 +08:00
if ( ( ! outFileName | | ! strcmp ( outFileName , stdoutmark ) )
2022-01-15 04:37:32 +08:00
& & UTIL_isConsole ( stdout )
2022-07-30 07:13:07 +08:00
& & ( UTIL_searchFileNamesTable ( filenames , stdinmark ) ! = - 1 )
2018-03-12 10:56:48 +08:00
& & ! forceStdout
2019-12-03 08:08:08 +08:00
& & operation ! = zom_decompress ) {
2020-09-29 00:15:18 +08:00
DISPLAYLEVEL ( 1 , " stdout is a console, aborting \n " ) ;
2019-12-03 08:08:08 +08:00
CLEAN_RETURN ( 1 ) ;
}
2016-05-29 11:16:05 +08:00
2016-10-05 17:56:22 +08:00
# ifndef ZSTD_NOCOMPRESS
2016-08-13 00:04:15 +08:00
/* check compression level limits */
{ int const maxCLevel = ultra ? ZSTD_maxCLevel ( ) : ZSTDCLI_CLEVEL_MAX ;
if ( cLevel > maxCLevel ) {
DISPLAYLEVEL ( 2 , " Warning : compression level higher than max, reduced to %i \n " , maxCLevel ) ;
cLevel = maxCLevel ;
} }
2016-10-05 17:56:22 +08:00
# endif
2016-08-13 00:04:15 +08:00
2020-01-31 06:12:03 +08:00
if ( showDefaultCParams ) {
if ( operation = = zom_decompress ) {
2022-03-12 15:52:40 +08:00
DISPLAY ( " error : can't use --show-default-cparams in decompression mode \n " ) ;
2020-01-31 06:12:03 +08:00
CLEAN_RETURN ( 1 ) ;
}
}
2020-01-11 06:25:24 +08:00
if ( dictFileName ! = NULL & & patchFromDictFileName ! = NULL ) {
DISPLAY ( " error : can't use -D and --patch-from=# at the same time \n " ) ;
CLEAN_RETURN ( 1 ) ;
}
2020-04-18 04:58:53 +08:00
if ( patchFromDictFileName ! = NULL & & filenames - > tableSize > 1 ) {
DISPLAY ( " error : can't use --patch-from=# on multiple files \n " ) ;
CLEAN_RETURN ( 1 ) ;
}
2021-01-08 02:37:27 +08:00
/* No status message in pipe mode (stdin - stdout) */
2020-10-08 01:47:38 +08:00
hasStdout = outFileName & & ! strcmp ( outFileName , stdoutmark ) ;
2022-01-15 04:37:32 +08:00
if ( ( hasStdout | | ! UTIL_isConsole ( stderr ) ) & & ( g_displayLevel = = 2 ) ) g_displayLevel = 1 ;
2016-05-29 11:16:05 +08:00
/* IO Stream/File */
2020-10-08 01:47:38 +08:00
FIO_setHasStdoutOutput ( fCtx , hasStdout ) ;
2021-01-08 02:37:27 +08:00
FIO_setNbFilesTotal ( fCtx , ( int ) filenames - > tableSize ) ;
2020-09-25 03:55:30 +08:00
FIO_determineHasStdinInput ( fCtx , filenames ) ;
2017-03-24 02:13:52 +08:00
FIO_setNotificationLevel ( g_displayLevel ) ;
2021-05-05 04:24:46 +08:00
FIO_setAllowBlockDevices ( prefs , allowBlockDevices ) ;
2020-01-11 06:25:24 +08:00
FIO_setPatchFromMode ( prefs , patchFromDictFileName ! = NULL ) ;
if ( memLimit = = 0 ) {
if ( compressionParams . windowLog = = 0 ) {
memLimit = ( U32 ) 1 < < g_defaultMaxWindowLog ;
} else {
memLimit = ( U32 ) 1 < < ( compressionParams . windowLog & 31 ) ;
} }
2020-04-18 04:58:53 +08:00
if ( patchFromDictFileName ! = NULL )
dictFileName = patchFromDictFileName ;
2020-01-11 06:25:24 +08:00
FIO_setMemLimit ( prefs , memLimit ) ;
2016-10-29 04:58:31 +08:00
if ( operation = = zom_compress ) {
2016-09-21 18:24:43 +08:00
# ifndef ZSTD_NOCOMPRESS
2020-03-10 03:12:52 +08:00
FIO_setContentSize ( prefs , contentSize ) ;
fileio_types.h : avoid dependency on mem.h
fileio_types.h cannot be parsed by itself
because it relies on basic types defined in `lib/common/mem.h`.
As for #3231, it likely wasn't detected because `mem.h` was probably included before within target files.
But this is not proper.
A "easy" solution would be to add the missing include,
but each dependency should be considered "bad" by default,
and only allowed if it brings some tangible value.
In this case, since these types are only used to declare internal structure variables
which are effectively only flags,
I believe it's really not valuable to add a dependency on `mem.h` for this purpose
while the standard `int` type can do the same job.
I was expecting some compiler warnings following this change,
but it turns out we don't use `-Wconversion` by default on `zstd` source code,
so there is none.
Nevertheless, I enabled `-Wconversion` locally and proceeded to fix a few conversion warnings in the process.
Adding `-Wconversion` to the list of flags used for `zstd` is something I would be favorable over the long term,
but it cannot be done overnight,
because the nb of places where this warning is triggered is daunting.
Better progressively reduce the nb of triggered `-Wconversion` warnings before enabling this flag by default.
2022-08-04 03:39:35 +08:00
FIO_setNbWorkers ( prefs , ( int ) nbWorkers ) ;
2019-10-18 06:27:25 +08:00
FIO_setBlockSize ( prefs , ( int ) blockSize ) ;
if ( g_overlapLog ! = OVERLAP_LOG_DEFAULT ) FIO_setOverlapLog ( prefs , ( int ) g_overlapLog ) ;
FIO_setLdmFlag ( prefs , ( unsigned ) ldmFlag ) ;
FIO_setLdmHashLog ( prefs , ( int ) g_ldmHashLog ) ;
FIO_setLdmMinMatch ( prefs , ( int ) g_ldmMinMatch ) ;
if ( g_ldmBucketSizeLog ! = LDM_PARAM_DEFAULT ) FIO_setLdmBucketSizeLog ( prefs , ( int ) g_ldmBucketSizeLog ) ;
if ( g_ldmHashRateLog ! = LDM_PARAM_DEFAULT ) FIO_setLdmHashRateLog ( prefs , ( int ) g_ldmHashRateLog ) ;
fileio_types.h : avoid dependency on mem.h
fileio_types.h cannot be parsed by itself
because it relies on basic types defined in `lib/common/mem.h`.
As for #3231, it likely wasn't detected because `mem.h` was probably included before within target files.
But this is not proper.
A "easy" solution would be to add the missing include,
but each dependency should be considered "bad" by default,
and only allowed if it brings some tangible value.
In this case, since these types are only used to declare internal structure variables
which are effectively only flags,
I believe it's really not valuable to add a dependency on `mem.h` for this purpose
while the standard `int` type can do the same job.
I was expecting some compiler warnings following this change,
but it turns out we don't use `-Wconversion` by default on `zstd` source code,
so there is none.
Nevertheless, I enabled `-Wconversion` locally and proceeded to fix a few conversion warnings in the process.
Adding `-Wconversion` to the list of flags used for `zstd` is something I would be favorable over the long term,
but it cannot be done overnight,
because the nb of places where this warning is triggered is daunting.
Better progressively reduce the nb of triggered `-Wconversion` warnings before enabling this flag by default.
2022-08-04 03:39:35 +08:00
FIO_setAdaptiveMode ( prefs , adapt ) ;
2020-11-03 09:52:29 +08:00
FIO_setUseRowMatchFinder ( prefs , useRowMatchFinder ) ;
2019-01-23 09:31:13 +08:00
FIO_setAdaptMin ( prefs , adaptMin ) ;
FIO_setAdaptMax ( prefs , adaptMax ) ;
FIO_setRsyncable ( prefs , rsyncable ) ;
2019-08-16 14:57:55 +08:00
FIO_setStreamSrcSize ( prefs , streamSrcSize ) ;
2019-06-25 04:40:52 +08:00
FIO_setTargetCBlockSize ( prefs , targetCBlockSize ) ;
2019-08-19 23:52:08 +08:00
FIO_setSrcSizeHint ( prefs , srcSizeHint ) ;
2019-02-16 07:24:55 +08:00
FIO_setLiteralCompressionMode ( prefs , literalCompressionMode ) ;
2022-02-01 07:43:41 +08:00
FIO_setSparseWrite ( prefs , 0 ) ;
2018-09-25 09:16:08 +08:00
if ( adaptMin > cLevel ) cLevel = adaptMin ;
if ( adaptMax < cLevel ) cLevel = adaptMax ;
2017-09-02 05:52:51 +08:00
2020-02-04 01:52:39 +08:00
/* Compare strategies constant with the ground truth */
{ ZSTD_bounds strategyBounds = ZSTD_cParam_getBounds ( ZSTD_c_strategy ) ;
2020-05-09 04:42:15 +08:00
assert ( ZSTD_NB_STRATEGIES = = strategyBounds . upperBound ) ;
( void ) strategyBounds ; }
2020-02-01 02:47:17 +08:00
2021-11-06 03:48:13 +08:00
if ( showDefaultCParams | | g_displayLevel > = 4 ) {
2020-01-31 06:12:03 +08:00
size_t fileNb ;
for ( fileNb = 0 ; fileNb < ( size_t ) filenames - > tableSize ; fileNb + + ) {
2021-11-06 03:48:13 +08:00
if ( showDefaultCParams )
printDefaultCParams ( filenames - > fileNames [ fileNb ] , dictFileName , cLevel ) ;
if ( g_displayLevel > = 4 )
printActualCParams ( filenames - > fileNames [ fileNb ] , dictFileName , cLevel , & compressionParams ) ;
2020-01-31 06:12:03 +08:00
}
}
2021-11-06 03:01:20 +08:00
if ( g_displayLevel > = 4 )
FIO_displayCompressionParameters ( prefs ) ;
2019-11-06 09:02:43 +08:00
if ( ( filenames - > tableSize = = 1 ) & & outFileName )
2020-09-08 01:13:05 +08:00
operationResult = FIO_compressFilename ( fCtx , prefs , outFileName , filenames - > fileNames [ 0 ] , dictFileName , cLevel , compressionParams ) ;
2016-05-29 11:16:05 +08:00
else
2020-09-08 01:13:05 +08:00
operationResult = FIO_compressMultipleFilenames ( fCtx , prefs , filenames - > fileNames , outMirroredDirName , outDirName , outFileName , suffix , dictFileName , cLevel , compressionParams ) ;
2016-09-21 18:24:43 +08:00
# else
2020-11-03 09:52:29 +08:00
( void ) contentSize ; ( void ) suffix ; ( void ) adapt ; ( void ) rsyncable ; ( void ) ultra ; ( void ) cLevel ; ( void ) ldmFlag ; ( void ) literalCompressionMode ; ( void ) targetCBlockSize ; ( void ) streamSrcSize ; ( void ) srcSizeHint ; ( void ) ZSTD_strategyMap ; ( void ) useRowMatchFinder ; /* not used when ZSTD_NOCOMPRESS set */
2018-09-25 07:56:45 +08:00
DISPLAY ( " Compression not supported \n " ) ;
2016-05-29 11:16:05 +08:00
# endif
2016-10-29 04:58:31 +08:00
} else { /* decompression or test */
2016-05-29 11:16:05 +08:00
# ifndef ZSTD_NODECOMPRESS
2019-11-06 09:02:43 +08:00
if ( filenames - > tableSize = = 1 & & outFileName ) {
2020-09-08 01:13:05 +08:00
operationResult = FIO_decompressFilename ( fCtx , prefs , outFileName , filenames - > fileNames [ 0 ] , dictFileName ) ;
2019-10-26 07:36:59 +08:00
} else {
2020-09-08 01:13:05 +08:00
operationResult = FIO_decompressMultipleFilenames ( fCtx , prefs , filenames - > fileNames , outMirroredDirName , outDirName , outFileName , dictFileName ) ;
2019-10-26 07:36:59 +08:00
}
2016-05-29 11:16:05 +08:00
# else
2018-09-25 07:56:45 +08:00
DISPLAY ( " Decompression not supported \n " ) ;
2016-05-29 11:16:05 +08:00
# endif
}
_end :
2019-01-23 09:31:13 +08:00
FIO_freePreferences ( prefs ) ;
2020-09-03 22:14:04 +08:00
FIO_freeContext ( fCtx ) ;
2016-05-29 11:16:05 +08:00
if ( main_pause ) waitEnter ( ) ;
2019-11-06 09:02:43 +08:00
UTIL_freeFileNamesTable ( filenames ) ;
UTIL_freeFileNamesTable ( file_of_names ) ;
2021-02-04 11:53:00 +08:00
# ifndef ZSTD_NOTRACE
TRACE_finish ( ) ;
# endif
2019-10-26 09:16:45 +08:00
2016-05-29 11:16:05 +08:00
return operationResult ;
}