diff options
author | gabrielstedman <gabriwinter@gmail.com> | 2019-04-22 08:00:20 (GMT) |
---|---|---|
committer | gabrielstedman <gabriwinter@gmail.com> | 2019-04-22 08:00:20 (GMT) |
commit | 2133366da080aaa2dbec2a33b52ad236cb493b01 (patch) | |
tree | bb0a06413ad3dbf72d16b93fbf78641ecba92b53 /programs | |
parent | 5fdc6d201ee4f46a1909a2b948e47d2bf8e4c1b3 (diff) | |
download | lz4-2133366da080aaa2dbec2a33b52ad236cb493b01.zip lz4-2133366da080aaa2dbec2a33b52ad236cb493b01.tar.gz lz4-2133366da080aaa2dbec2a33b52ad236cb493b01.tar.bz2 |
FR #598 - Make LZ4IO_getCompressedFileInfo internal and reword func
Diffstat (limited to 'programs')
-rw-r--r-- | programs/lz4cli.c | 3 | ||||
-rw-r--r-- | programs/lz4io.c | 132 | ||||
-rw-r--r-- | programs/lz4io.h | 9 |
3 files changed, 68 insertions, 76 deletions
diff --git a/programs/lz4cli.c b/programs/lz4cli.c index 23c4be7..9a74286 100644 --- a/programs/lz4cli.c +++ b/programs/lz4cli.c @@ -40,7 +40,6 @@ #include <stdlib.h> /* exit, calloc, free */ #include <string.h> /* strcmp, strlen */ #include "bench.h" /* BMK_benchFile, BMK_SetNbIterations, BMK_SetBlocksize, BMK_SetPause */ -#include "lz4frame.h" #include "lz4io.h" /* LZ4IO_compressFilename, LZ4IO_decompressFilename, LZ4IO_compressMultipleFilenames */ #include "lz4hc.h" /* LZ4HC_CLEVEL_MAX */ #include "lz4.h" /* LZ4_VERSION_STRING */ @@ -712,7 +711,7 @@ int main(int argc, const char** argv) if(!multiple_inputs){ inFileNames[ifnIdx++] = input_filename; } - operationResult = LZ4IO_getCompressedFilesInfo(inFileNames, ifnIdx); + operationResult = LZ4IO_displayCompressedFilesInfo(inFileNames, ifnIdx); inFileNames=NULL; } else { /* compression is default action */ diff --git a/programs/lz4io.c b/programs/lz4io.c index 2e38d2f..61fa13f 100644 --- a/programs/lz4io.c +++ b/programs/lz4io.c @@ -1214,6 +1214,60 @@ static int LZ4IO_decompressDstFile(LZ4IO_prefs_t* const prefs, dRess_t ress, con } +static int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo){ + const char *b, + *e; + char *t; + size_t readSize = LZ4F_HEADER_SIZE_MAX; + LZ4F_errorCode_t errorCode; + dRess_t ress; + // LZ4F_compFileInfo_t cfinfo = (LZ4F_compFileInfo_t) LZ4F_INIT_FILEINFO; + /* Open file */ + FILE* const finput = LZ4IO_openSrcFile(input_filename); + if (finput==NULL) return 1; + + /* Get file size */ + if (!UTIL_getFileStat(input_filename, &cfinfo->fileStat)){ + EXM_THROW(60, "Can't stat file : %s", input_filename); + } + + /* Get basename without extension */ + b = strrchr(input_filename, '/'); + if (!b){ + b = strrchr(input_filename, '\\'); + } + if (b && b != input_filename){ + b++; + } else{ + b=input_filename; + } + e = strrchr(b, '.'); + + /* Allocate Memory */ + t = malloc( (e-b+1) * sizeof(char)); + ress.srcBuffer = malloc(LZ4IO_dBufferSize); + if (!t || !ress.srcBuffer) + EXM_THROW(21, "Allocation error : not enough memory"); + strncpy(t, b, (e-b)); + t[e-b] = '\0'; + cfinfo->fileName = t; + + /* init */ + errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); + if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); + + if (!fread(ress.srcBuffer, readSize, 1, finput)){ + EXM_THROW(30, "Error reading %s ", input_filename); + } + LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); + + /* Close input/free resources */ + fclose(finput); + free(ress.srcBuffer); + return 0; +} + + int LZ4IO_decompressFilename(LZ4IO_prefs_t* const prefs, const char* input_filename, const char* output_filename) { dRess_t const ress = LZ4IO_createDResources(prefs); @@ -1266,86 +1320,28 @@ int LZ4IO_decompressMultipleFilenames(LZ4IO_prefs_t* const prefs, const char** i return missingFiles + skippedFiles; } -int LZ4IO_getCompressedFilesInfo(const char** inFileNames, const size_t ifnIdx){ + +int LZ4IO_displayCompressedFilesInfo(const char** inFileNames, const size_t ifnIdx){ size_t idx; int op_result=0; - LZ4F_compFileInfo_t cfinfo = (LZ4F_compFileInfo_t) LZ4F_INIT_FILEINFO; + double ratio; + LZ4F_compFileInfo_t cfinfo; DISPLAY("%16s\t%-20s\t%-20s\t%-10s\t%s\n","BlockChecksumFlag","Compressed", "Uncompressed", "Ratio", "Filename"); for(idx=0; idx<ifnIdx; idx++){ /* Get file info */ - op_result&=LZ4IO_getCompressedFileInfo(inFileNames[idx], &cfinfo); + op_result=LZ4IO_getCompressedFileInfo(inFileNames[idx], &cfinfo); if (op_result != 0){ DISPLAYLEVEL(1, "Failed to get frame info for file %s\n", inFileNames[idx]); - if (ifnIdx < 2){ - return 1; - } - continue; + /* Don't bother trying to process any other file */ + break; } if(cfinfo.frameInfo.contentSize){ - DISPLAY("%-16d\t%-20llu\t%-20llu\t%-8.4f\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize,cfinfo.frameInfo.contentSize, cfinfo.ratio, cfinfo.fileName); + ratio = (double)cfinfo.fileStat.st_size / cfinfo.frameInfo.contentSize; + DISPLAY("%-16d\t%-20lu\t%-20llu\t%-8.4f\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileStat.st_size,cfinfo.frameInfo.contentSize, ratio, cfinfo.fileName); } else{ - DISPLAY("%-16d\t%-20llu\t%-20s\t%-10s\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileSize, "-", "-", cfinfo.fileName); + DISPLAY("%-16d\t%-20lu\t%-20s\t%-10s\t%s\n",cfinfo.frameInfo.blockChecksumFlag,cfinfo.fileStat.st_size, "-", "-", cfinfo.fileName); } } return op_result; } - -int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo){ - const char *b, - *e; - char *t; - stat_t statbuf; - size_t readSize = LZ4F_HEADER_SIZE_MAX; - LZ4F_errorCode_t errorCode; - dRess_t ress; - /* Open file */ - FILE* const finput = LZ4IO_openSrcFile(input_filename); - if (finput==NULL) return 0; - *cfinfo = (LZ4F_compFileInfo_t) LZ4F_INIT_FILEINFO; - /* Get file size */ - if (!UTIL_getFileStat(input_filename, &statbuf)){ - EXM_THROW(60, "Can't stat file : %s", input_filename); - } - cfinfo->fileSize = statbuf.st_size; - - /* Get basename without extension */ - b = strrchr(input_filename, '/'); - if (!b){ - b = strrchr(input_filename, '\\'); - } - if (b && b != input_filename){ - b++; - } else{ - b=input_filename; - } - e = strrchr(b, '.'); - /* Allocate Memory */ - t = malloc( (e-b+1) * sizeof(char)); - ress.srcBuffer = malloc(LZ4IO_dBufferSize); - if (!t || !ress.srcBuffer) - EXM_THROW(21, "Allocation error : not enough memory"); - strncpy(t, b, (e-b)); - t[e-b] = '\0'; - cfinfo->fileName = t; - - /* init */ - errorCode = LZ4F_createDecompressionContext(&ress.dCtx, LZ4F_VERSION); - if (LZ4F_isError(errorCode)) EXM_THROW(60, "Can't create LZ4F context : %s", LZ4F_getErrorName(errorCode)); - - if (!fread(ress.srcBuffer, readSize, 1, finput)){ - EXM_THROW(30, "Error reading %s ", input_filename); - } - // cfinfo->frameInfo = (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO; - LZ4F_getFrameInfo(ress.dCtx, &cfinfo->frameInfo, ress.srcBuffer, &readSize); - if(cfinfo->frameInfo.contentSize){ - cfinfo->ratio = (double)cfinfo->fileSize / cfinfo->frameInfo.contentSize; - } else { - cfinfo->ratio = -1; - } - - /* Close input/free resources */ - fclose(finput); - free(ress.srcBuffer); - return 0; -} diff --git a/programs/lz4io.h b/programs/lz4io.h index 707f233..3d37bd0 100644 --- a/programs/lz4io.h +++ b/programs/lz4io.h @@ -57,11 +57,10 @@ typedef struct LZ4IO_prefs_s LZ4IO_prefs_t; typedef struct { LZ4F_frameInfo_t frameInfo; const char* fileName; - unsigned long long fileSize; - double ratio; + stat_t fileStat; } LZ4F_compFileInfo_t; -#define LZ4F_INIT_FILEINFO { (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO, NULL, 0ULL, -1.f } +#define LZ4F_INIT_FILEINFO { (LZ4F_frameInfo_t) LZ4F_INIT_FRAMEINFO, NULL, stat_t() } LZ4IO_prefs_t* LZ4IO_defaultPreferences(void); void LZ4IO_freePreferences(LZ4IO_prefs_t* const prefs); @@ -125,9 +124,7 @@ int LZ4IO_setSparseFile(LZ4IO_prefs_t* const prefs, int enable); /* Default setting : 0 == no content size present in frame header */ int LZ4IO_setContentSize(LZ4IO_prefs_t* const prefs, int enable); -int LZ4IO_getCompressedFilesInfo(const char** inFileNames,const size_t ifnIdx); - -int LZ4IO_getCompressedFileInfo(const char* input_filename, LZ4F_compFileInfo_t* cfinfo); +int LZ4IO_displayCompressedFilesInfo(const char** inFileNames,const size_t ifnIdx); /* Default setting : 0 == src file preserved */ void LZ4IO_setRemoveSrcFile(LZ4IO_prefs_t* const prefs, unsigned flag); |