First pass at refactoring and creating new FIO_ctx_t */

This commit is contained in:
senhuang42 2020-08-28 11:01:04 -04:00
parent a8c66881e5
commit d54566f334
3 changed files with 72 additions and 17 deletions

View File

@ -319,8 +319,20 @@ struct FIO_prefs_s {
int excludeCompressedFiles;
int patchFromMode;
int contentSize;
};
/*-*************************************
* Parameters: FIO_prefs_t
***************************************/
/* typedef'd to FIO_ctx_t within fileio.h */
struct FIO_ctx_s {
/* multiple file processing info */
int currFileIdx;
int nbFiles;
size_t totalBytesInput;
size_t totalBytesOutput;
};
@ -362,8 +374,18 @@ FIO_prefs_t* FIO_createPreferences(void)
ret->testMode = 0;
ret->literalCompressionMode = ZSTD_lcm_auto;
ret->excludeCompressedFiles = 0;
ret->nbFiles = 1;
return ret;
}
FIO_ctx_t* FIO_createContext(void)
{
FIO_ctx_t* const ret = (FIO_ctx_t*)malloc(sizeof(FIO_ctx_t));
if (!ret) EXM_THROW(21, "Allocation error : not enough memory");
ret->currFileIdx = 0;
ret->nbFiles = 1;
ret->totalBytesInput = 0;
ret->totalBytesOutput = 0;
return ret;
}
@ -372,6 +394,11 @@ void FIO_freePreferences(FIO_prefs_t* const prefs)
free(prefs);
}
void FIO_freeContext(FIO_ctx_t* const fCtx)
{
free(fCtx);
}
/*-*************************************
* Parameters: Display Options
@ -386,6 +413,8 @@ void FIO_setNoProgress(unsigned noProgress) { g_display_prefs.noProgress = noPro
* Parameters: Setters
***************************************/
/* FIO_prefs_t functions */
void FIO_setCompressionType(FIO_prefs_t* const prefs, FIO_compressionType_t compressionType) { prefs->compressionType = compressionType; }
void FIO_overwriteMode(FIO_prefs_t* const prefs) { prefs->overwrite = 1; }
@ -499,14 +528,26 @@ void FIO_setContentSize(FIO_prefs_t* const prefs, int value)
prefs->contentSize = value != 0;
}
void FIO_setNbFiles(FIO_prefs_t* const prefs, int value)
/* FIO_ctx_t functions */
void FIO_setNbFiles(FIO_ctx_t* const fCtx, int value)
{
prefs->nbFiles = value;
fCtx->nbFiles = value;
}
void FIO_setCurrFileIdx(FIO_prefs_t* const prefs, int value)
void FIO_setCurrFileIdx(FIO_ctx_t* const fCtx, int value)
{
prefs->currFileIdx = value;
fCtx->currFileIdx = value;
}
void FIO_setTotalBytesInput(FIO_ctx_t* const fCtx, size_t value)
{
fCtx->totalBytesInput = value;
}
void FIO_setTotalBytesOutput(FIO_ctx_t* const fCtx, size_t value)
{
fCtx->totalBytesOutput = value;
}
/*-*************************************
@ -1685,6 +1726,7 @@ static unsigned long long FIO_getLargestFileSize(const char** inFileNames, unsig
* or into a destination folder (specified with -O)
*/
int FIO_compressMultipleFilenames(FIO_prefs_t* const prefs,
FIO_ctx_t* const fCtx,
const char** inFileNamesTable,
const char* outMirroredRootDirName,
const char* outDirName,
@ -1694,7 +1736,7 @@ int FIO_compressMultipleFilenames(FIO_prefs_t* const prefs,
{
int error = 0;
cRess_t ress = FIO_createCResources(prefs, dictFileName,
FIO_getLargestFileSize(inFileNamesTable, prefs->nbFiles),
FIO_getLargestFileSize(inFileNamesTable, fCtx->nbFiles),
compressionLevel, comprParams);
/* init */
@ -1704,8 +1746,8 @@ int FIO_compressMultipleFilenames(FIO_prefs_t* const prefs,
if (ress.dstFile == NULL) { /* could not open outFileName */
error = 1;
} else {
for (; prefs->currFileIdx < prefs->nbFiles; ++prefs->currFileIdx) {
error |= FIO_compressFilename_srcFile(prefs, ress, outFileName, inFileNamesTable[prefs->currFileIdx], compressionLevel);
for (; fCtx->currFileIdx < fCtx->nbFiles; ++fCtx->currFileIdx) {
error |= FIO_compressFilename_srcFile(prefs, ress, outFileName, inFileNamesTable[fCtx->currFileIdx], compressionLevel);
}
if (fclose(ress.dstFile))
EXM_THROW(29, "Write error (%s) : cannot properly close %s",
@ -1714,10 +1756,10 @@ int FIO_compressMultipleFilenames(FIO_prefs_t* const prefs,
}
} else {
if (outMirroredRootDirName)
UTIL_mirrorSourceFilesDirectories(inFileNamesTable, prefs->nbFiles, outMirroredRootDirName);
UTIL_mirrorSourceFilesDirectories(inFileNamesTable, fCtx->nbFiles, outMirroredRootDirName);
for (; prefs->currFileIdx < prefs->nbFiles; ++prefs->currFileIdx) {
const char* const srcFileName = inFileNamesTable[prefs->currFileIdx];
for (; fCtx->currFileIdx < fCtx->nbFiles; ++fCtx->currFileIdx) {
const char* const srcFileName = inFileNamesTable[fCtx->currFileIdx];
const char* dstFileName = NULL;
if (outMirroredRootDirName) {
char* validMirroredDirName = UTIL_createMirroredDestDirName(srcFileName, outMirroredRootDirName);
@ -1736,7 +1778,7 @@ int FIO_compressMultipleFilenames(FIO_prefs_t* const prefs,
}
if (outDirName)
FIO_checkFilenameCollisions(inFileNamesTable , prefs->nbFiles);
FIO_checkFilenameCollisions(inFileNamesTable , fCtx->nbFiles);
}
FIO_freeCResources(ress);

View File

@ -54,16 +54,24 @@ extern "C" {
***************************************/
typedef enum { FIO_zstdCompression, FIO_gzipCompression, FIO_xzCompression, FIO_lzmaCompression, FIO_lz4Compression } FIO_compressionType_t;
/* Immutable struct containing preferences for file I/O */
typedef struct FIO_prefs_s FIO_prefs_t;
FIO_prefs_t* FIO_createPreferences(void);
void FIO_freePreferences(FIO_prefs_t* const prefs);
/* Mutable struct containing relevant context and state of (de)compression with respect to file I/O */
typedef struct FIO_ctx_s FIO_ctx_t;
FIO_ctx_t* FIO_createContext(void);
void FIO_freeContext(FIO_ctx_t* const fCtx);
typedef struct FIO_display_prefs_s FIO_display_prefs_t;
/*-*************************************
* Parameters
***************************************/
/* FIO_prefs_t functions */
void FIO_setCompressionType(FIO_prefs_t* const prefs, FIO_compressionType_t compressionType);
void FIO_overwriteMode(FIO_prefs_t* const prefs);
void FIO_setAdaptiveMode(FIO_prefs_t* const prefs, unsigned adapt);
@ -96,8 +104,12 @@ void FIO_setNotificationLevel(int level);
void FIO_setExcludeCompressedFile(FIO_prefs_t* const prefs, int excludeCompressedFiles);
void FIO_setPatchFromMode(FIO_prefs_t* const prefs, int value);
void FIO_setContentSize(FIO_prefs_t* const prefs, int value);
void FIO_setNbFiles(FIO_prefs_t* const prefs, int value);
void FIO_setCurrFileIdx(FIO_prefs_t* const prefs, int value);
/* FIO_ctx_t functions */
void FIO_setNbFiles(FIO_ctx_t* const fCtx, int value);
void FIO_setCurrFileIdx(FIO_ctx_t* const fCtx, int value);
void FIO_setTotalBytesInput(FIO_ctx_t* const fCtx, size_t value);
void FIO_setTotalBytesOutput(FIO_ctx_t* const fCtx, size_t value);
/*-*************************************
* Single File functions

View File

@ -693,6 +693,7 @@ int main(int const argCount, const char* argv[])
size_t blockSize = 0;
FIO_prefs_t* const prefs = FIO_createPreferences();
FIO_ctx_t* const fCtx = FIO_createContext();
zstd_operation_mode operation = zom_compress;
ZSTD_compressionParameters compressionParams;
int cLevel = init_cLevel();
@ -1260,7 +1261,7 @@ int main(int const argCount, const char* argv[])
if (!strcmp(filenames->fileNames[0], stdinmark) && outFileName && !strcmp(outFileName,stdoutmark) && (g_displayLevel==2)) g_displayLevel=1;
/* IO Stream/File */
FIO_setNbFiles(prefs, (int)filenames->tableSize);
FIO_setNbFiles(fCtx, (int)filenames->tableSize);
FIO_setNotificationLevel(g_displayLevel);
FIO_setPatchFromMode(prefs, patchFromDictFileName != NULL);
if (memLimit == 0) {
@ -1319,9 +1320,9 @@ int main(int const argCount, const char* argv[])
}
if ((filenames->tableSize==1) && outFileName)
operationResult = FIO_compressFilename(prefs, outFileName, filenames->fileNames[0], dictFileName, cLevel, compressionParams);
operationResult = FIO_compressFilename(prefs, fCtx, outFileName, filenames->fileNames[0], dictFileName, cLevel, compressionParams);
else
operationResult = FIO_compressMultipleFilenames(prefs, filenames->fileNames, outMirroredDirName, outDirName, outFileName, suffix, dictFileName, cLevel, compressionParams);
operationResult = FIO_compressMultipleFilenames(prefs, fCtx, filenames->fileNames, outMirroredDirName, outDirName, outFileName, suffix, dictFileName, cLevel, compressionParams);
#else
(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; /* not used when ZSTD_NOCOMPRESS set */
DISPLAY("Compression not supported \n");