Skip to content

Commit 8a189b1

Browse files
refactor dictionary file stat
1 parent 4373c5a commit 8a189b1

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

programs/fileio.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,23 @@ FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs,
660660
}
661661
}
662662

663-
/*! FIO_createDictBuffer() :
663+
664+
/* FIO_getDictFileStat() :
665+
*/
666+
static void FIO_getDictFileStat(const char* fileName, stat_t* dictFileStat) {
667+
assert(dictFileStat != NULL);
668+
if (fileName == NULL) return;
669+
670+
if (!UTIL_stat(fileName, dictFileStat)) {
671+
EXM_THROW(31, "Stat failed on dictionary file %s: %s", fileName, strerror(errno));
672+
}
673+
674+
if (!UTIL_isRegularFileStat(dictFileStat)) {
675+
EXM_THROW(32, "Dictionary %s must be a regular file.", fileName);
676+
}
677+
}
678+
679+
/* FIO_createDictBuffer() :
664680
* creates a buffer, pointed by `*bufferPtr`,
665681
* loads `filename` content into it, up to DICTSIZE_MAX bytes.
666682
* @return : loaded size
@@ -678,14 +694,6 @@ static size_t FIO_createDictBuffer(void** bufferPtr, const char* fileName, FIO_p
678694

679695
DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName);
680696

681-
if (!UTIL_stat(fileName, dictFileStat)) {
682-
EXM_THROW(31, "Stat failed on dictionary file %s: %s", fileName, strerror(errno));
683-
}
684-
685-
if (!UTIL_isRegularFileStat(dictFileStat)) {
686-
EXM_THROW(32, "Dictionary %s must be a regular file.", fileName);
687-
}
688-
689697
fileHandle = fopen(fileName, "rb");
690698

691699
if (fileHandle == NULL) {
@@ -736,14 +744,6 @@ static size_t FIO_createDictBufferMMap(void** bufferPtr, const char* fileName, F
736744

737745
DISPLAYLEVEL(4,"Loading %s as dictionary \n", fileName);
738746

739-
if (!UTIL_stat(fileName, dictFileStat)) {
740-
EXM_THROW(31, "Stat failed on dictionary file %s: %s", fileName, strerror(errno));
741-
}
742-
743-
if (!UTIL_isRegularFileStat(dictFileStat)) {
744-
EXM_THROW(32, "Dictionary %s must be a regular file.", fileName);
745-
}
746-
747747
fileHandle = open(fileName, O_RDONLY);
748748

749749
if (fileHandle == -1) {
@@ -1038,10 +1038,12 @@ static cRess_t FIO_createCResources(FIO_prefs_t* const prefs,
10381038
EXM_THROW(30, "allocation error (%s): can't create ZSTD_CCtx",
10391039
strerror(errno));
10401040

1041+
FIO_getDictFileStat(dictFileName, &ress.dictFileStat);
1042+
10411043
/* need to update memLimit before calling createDictBuffer
10421044
* because of memLimit check inside it */
10431045
if (prefs->patchFromMode) {
1044-
U64 const dictSize = UTIL_getFileSize(dictFileName);
1046+
U64 const dictSize = UTIL_getFileSizeStat(&ress.dictFileStat);
10451047
unsigned long long const ssSize = (unsigned long long)prefs->streamSrcSize;
10461048
mmapDict = dictSize > prefs->memLimit;
10471049
FIO_adjustParamsForPatchFromMode(prefs, &comprParams, dictSize, ssSize > 0 ? ssSize : maxSrcFileSize, cLevel);
@@ -2135,11 +2137,14 @@ typedef struct {
21352137
static dRess_t FIO_createDResources(FIO_prefs_t* const prefs, const char* dictFileName)
21362138
{
21372139
int mmapDict = 0;
2140+
stat_t statbuf;
21382141
dRess_t ress;
21392142
memset(&ress, 0, sizeof(ress));
21402143

2144+
FIO_getDictFileStat(dictFileName, &statbuf);
2145+
21412146
if (prefs->patchFromMode){
2142-
U64 const dictSize = UTIL_getFileSize(dictFileName);
2147+
U64 const dictSize = UTIL_getFileSizeStat(&statbuf);
21432148
mmapDict = dictSize > prefs->memLimit;
21442149
FIO_adjustMemLimitForPatchFromMode(prefs, dictSize, 0 /* just use the dict size */);
21452150
}
@@ -2153,8 +2158,7 @@ static dRess_t FIO_createDResources(FIO_prefs_t* const prefs, const char* dictFi
21532158
CHECK( ZSTD_DCtx_setParameter(ress.dctx, ZSTD_d_forceIgnoreChecksum, !prefs->checksumFlag));
21542159

21552160
/* dictionary */
2156-
{ stat_t statbuf;
2157-
if (!mmapDict) {
2161+
{ if (!mmapDict) {
21582162
ress.dictBufferSize = FIO_createDictBuffer(&ress.dictBuffer, dictFileName, prefs, &statbuf);
21592163
} else {
21602164
ress.dictBufferSize = FIO_createDictBufferMMap(&ress.dictBuffer, dictFileName, prefs, &statbuf);
@@ -2171,7 +2175,6 @@ static dRess_t FIO_createDResources(FIO_prefs_t* const prefs, const char* dictFi
21712175

21722176
ress.writeCtx = AIO_WritePool_create(prefs, ZSTD_DStreamOutSize());
21732177
ress.readCtx = AIO_ReadPool_create(prefs, ZSTD_DStreamInSize());
2174-
21752178
return ress;
21762179
}
21772180

0 commit comments

Comments
 (0)