diff options
author | Yann Collet <cyan@fb.com> | 2016-11-07 18:31:43 (GMT) |
---|---|---|
committer | Yann Collet <cyan@fb.com> | 2016-11-07 18:31:43 (GMT) |
commit | f6a7c6b578ea2bdfbd19e30ac7cadf4b97fcc84c (patch) | |
tree | e11a162b4e0975fab5a0776838e7a2afa03992de /programs | |
parent | 207f478babe676fc000e6e655c150a03a79570b0 (diff) | |
parent | 301c4e94eef421f28265e5cf79c804ab54907967 (diff) | |
download | lz4-f6a7c6b578ea2bdfbd19e30ac7cadf4b97fcc84c.zip lz4-f6a7c6b578ea2bdfbd19e30ac7cadf4b97fcc84c.tar.gz lz4-f6a7c6b578ea2bdfbd19e30ac7cadf4b97fcc84c.tar.bz2 |
Merge branch 'dev' of github.com:Cyan4973/lz4 into dev
Diffstat (limited to 'programs')
-rw-r--r-- | programs/lz4cli.c | 8 | ||||
-rw-r--r-- | programs/lz4io.c | 55 | ||||
-rw-r--r-- | programs/lz4io.h | 2 |
3 files changed, 32 insertions, 33 deletions
diff --git a/programs/lz4cli.c b/programs/lz4cli.c index e49bbcb..f091d3a 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -149,6 +149,7 @@ static int usage(void) DISPLAY( " -d : decompression (default for %s extension)\n", LZ4_EXTENSION); DISPLAY( " -z : force compression\n"); DISPLAY( " -f : overwrite output without prompting \n"); + DISPLAY( "--rm : remove source file(s) after successful de/compression \n"); DISPLAY( " -h/-H : display help/long help and exit\n"); return 0; } @@ -326,7 +327,7 @@ int main(int argc, const char** argv) if ((!strcmp(argument, "--decompress")) || (!strcmp(argument, "--uncompress"))) { decode = 1; continue; } if (!strcmp(argument, "--multiple")) { multiple_inputs = 1; if (inFileNames==NULL) inFileNames = (const char**)malloc(argc * sizeof(char*)); continue; } - if (!strcmp(argument, "--test")) { decode = 1; LZ4IO_setOverwrite(1); output_filename=nulmark; continue; } + if (!strcmp(argument, "--test")) { decode = 1; LZ4IO_setTestMode(1); output_filename=nulmark; continue; } if (!strcmp(argument, "--force")) { LZ4IO_setOverwrite(1); continue; } if (!strcmp(argument, "--no-force")) { LZ4IO_setOverwrite(0); continue; } if ((!strcmp(argument, "--stdout")) @@ -340,7 +341,8 @@ int main(int argc, const char** argv) if (!strcmp(argument, "--verbose")) { displayLevel=4; continue; } if (!strcmp(argument, "--quiet")) { if (displayLevel) displayLevel--; continue; } if (!strcmp(argument, "--version")) { DISPLAY(WELCOME_MESSAGE); return 0; } - if (!strcmp(argument, "--keep")) { continue; } /* keep source file (default anyway; just for xz/lzma compatibility) */ + if (!strcmp(argument, "--keep")) { LZ4IO_setRemoveSrcFile(0); continue; } /* keep source file (default anyway; just for xz/lzma compatibility) */ + if (!strcmp(argument, "--rm")) { LZ4IO_setRemoveSrcFile(1); continue; } /* Short commands (note : aggregated short commands are allowed) */ if (argument[0]=='-') { @@ -406,7 +408,7 @@ int main(int argc, const char** argv) case 'q': if (displayLevel) displayLevel--; break; /* keep source file (default anyway, so useless) (for xz/lzma compatibility) */ - case 'k': break; + case 'k': LZ4IO_setRemoveSrcFile(0); break; /* Modify Block Properties */ case 'B': diff --git a/programs/lz4io.c b/programs/lz4io.c index 2f64561..02f4c2b 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -33,14 +33,7 @@ /************************************** * Compiler Options **************************************/ -#ifdef _MSC_VER /* Visual Studio */ -# define _CRT_SECURE_NO_WARNINGS -# define _CRT_SECURE_NO_DEPRECATE /* VS2005 */ -# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ -#endif - #define _LARGE_FILES /* Large file support on 32-bits AIX */ -#define _FILE_OFFSET_BITS 64 /* Large file support on 32-bits unix */ #if defined(__MINGW32__) && !defined(_POSIX_SOURCE) # define _POSIX_SOURCE 1 /* disable %llu warnings with MinGW on Windows */ @@ -49,6 +42,7 @@ /***************************** * Includes *****************************/ +#include "util.h" /* Compiler options, UTIL_getFileStat */ #include <stdio.h> /* fprintf, fopen, fread, stdin, stdout, fflush, getchar */ #include <stdlib.h> /* malloc, free */ #include <string.h> /* strcmp, strlen */ @@ -84,10 +78,6 @@ # define SET_SPARSE_FILE_MODE(file) #endif -#if !defined(S_ISREG) -# define S_ISREG(x) (((x) & S_IFMT) == S_IFREG) -#endif - /***************************** * Constants @@ -242,19 +232,9 @@ int LZ4IO_setContentSize(int enable) return g_contentSizeFlag; } -static unsigned long long LZ4IO_GetFileSize(const char* infilename) -{ - int r; -#if defined(_MSC_VER) - struct _stat64 statbuf; - r = _stat64(infilename, &statbuf); -#else - struct stat statbuf; - r = stat(infilename, &statbuf); -#endif - if (r || !S_ISREG(statbuf.st_mode)) return 0; /* failure, or is not a regular file */ - return (unsigned long long)statbuf.st_size; -} +static U32 g_removeSrcFile = 0; +void LZ4IO_setRemoveSrcFile(unsigned flag) { g_removeSrcFile = (flag>0); } + /* ************************************************************************ ** @@ -465,7 +445,6 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, LZ4F_compressionContext_t ctx = ress.ctx; /* just a pointer */ LZ4F_preferences_t prefs; - /* Init */ memset(&prefs, 0, sizeof(prefs)); @@ -479,7 +458,7 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, prefs.frameInfo.blockSizeID = (LZ4F_blockSizeID_t)g_blockSizeId; prefs.frameInfo.contentChecksumFlag = (LZ4F_contentChecksum_t)g_streamChecksum; if (g_contentSizeFlag) { - unsigned long long const fileSize = LZ4IO_GetFileSize(srcFileName); + U64 const fileSize = UTIL_getFileSize(srcFileName); prefs.frameInfo.contentSize = fileSize; /* == 0 if input == stdin */ if (fileSize==0) DISPLAYLEVEL(3, "Warning : cannot determine input content size \n"); @@ -547,6 +526,14 @@ static int LZ4IO_compressFilename_extRess(cRess_t ress, const char* srcFileName, fclose (srcFile); fclose (dstFile); + /* Copy owner, file permissions and modification time */ + { stat_t statbuf; + if (strcmp (srcFileName, stdinmark) && strcmp (dstFileName, stdoutmark) && UTIL_getFileStat(srcFileName, &statbuf)) + UTIL_setFileStat(dstFileName, &statbuf); + } + + if (g_removeSrcFile) { if (remove(srcFileName)) EXM_THROW(39, "Remove error : %s: %s", srcFileName, strerror(errno)); } /* remove source file : --rm */ + /* Final Status */ DISPLAYLEVEL(2, "\r%79s\r", ""); DISPLAYLEVEL(2, "Compressed %llu bytes into %llu bytes ==> %.2f%%\n", @@ -946,14 +933,22 @@ static int LZ4IO_decompressFile_extRess(dRess_t ress, const char* input_filename filesize += decodedSize; } while (decodedSize != ENDOFSTREAM); - /* Final Status */ - DISPLAYLEVEL(2, "\r%79s\r", ""); - DISPLAYLEVEL(2, "Successfully decoded %llu bytes \n", filesize); - /* Close */ fclose(finput); fclose(foutput); + /* Copy owner, file permissions and modification time */ + { stat_t statbuf; + if (strcmp (input_filename, stdinmark) && strcmp (output_filename, stdoutmark) && UTIL_getFileStat(input_filename, &statbuf)) + UTIL_setFileStat(output_filename, &statbuf); + } + + if (g_removeSrcFile) { if (remove(input_filename)) EXM_THROW(45, "Remove error : %s: %s", input_filename, strerror(errno)); } /* remove source file : --rm */ + + /* Final Status */ + DISPLAYLEVEL(2, "\r%79s\r", ""); + DISPLAYLEVEL(2, "Successfully decoded %llu bytes \n", filesize); + return 0; } diff --git a/programs/lz4io.h b/programs/lz4io.h index bf076ee..45ae78d 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -89,3 +89,5 @@ int LZ4IO_setSparseFile(int enable); /* Default setting : 0 (disabled) */ int LZ4IO_setContentSize(int enable); + +void LZ4IO_setRemoveSrcFile(unsigned flag); |