From 779ea729535d1421ac4451db296b0685f8f22f7e Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Tue, 15 Oct 2019 07:49:13 +0100 Subject: [PATCH 01/17] Adding --file=FILE feature --- programs/util.c | 229 +++++++++++++++++++++++++++++++++++++++++++++ programs/util.h | 36 ++++++- programs/zstdcli.c | 72 +++++++++++++- 3 files changed, 335 insertions(+), 2 deletions(-) diff --git a/programs/util.c b/programs/util.c index 3988295d..caadf40e 100644 --- a/programs/util.c +++ b/programs/util.c @@ -163,6 +163,235 @@ U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbF return error ? UTIL_FILESIZE_UNKNOWN : total; } + +int UTIL_readLineFromFile(char* buf, size_t len, FILE* file) { + if (feof(file)) { + UTIL_DISPLAYLEVEL(1, "[ERROR] end of file reached and need to read\n"); + return -1; + } + UTIL_DISPLAY("[TRACE] read line\n"); + + char* fgetsCheck = fgets(buf, len, file); + + if(fgetsCheck == NULL || fgetsCheck != buf) { + UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readLineFromFile] fgets has a problem check: %s buf: %s \n", + fgetsCheck == NULL ? "NULL" : fgetsCheck, buf); + return -1; + } + + UTIL_DISPLAY("[TRACE] length of Line: %d\n" , (int)strlen(buf)); + return (int) strlen(buf)-1; /* -1 to ignore '\n' character */ +} + +/* Warning: inputFileSize should be less than or equal buf capacity and buf should be initialized*/ +static int readFromFile(char* buf, size_t inputFileSize, const char* inputFileName) { + + if(!buf) { + UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create buffer.\n"); + return -1; + } + + UTIL_DISPLAY("[TRACE] open file\n"); + FILE* inputFile = fopen(inputFileName, "r"); + int nbFiles = -1; + + if(!inputFile) { + UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't open file to read input file names.\n"); + return -1; + } + + unsigned pos = 0; + for(nbFiles=0; !feof(inputFile) ; ) { + if(UTIL_readLineFromFile(buf+pos, inputFileSize, inputFile) > 0) { + int len = (int) strlen(buf+pos); + buf[pos+len-1] = '\0'; /* replace '\n' with '\0'*/ + UTIL_DISPLAY("[TRACE] line: %s\n", buf+pos); + pos += len; + ++nbFiles; + } + else + UTIL_DISPLAY("[TRACE] ignored line: %s", buf+pos); + } + + fclose(inputFile); + + if(pos > inputFileSize) return -1; + + return nbFiles; +} + +/*Note: buf is not freed in case function successfully created table because filesTable->fileNames[0] = buf*/ +FileNamesTable* +UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { + + UTIL_DISPLAY("file check\n"); + if(!UTIL_fileExist(inputFileName) || !UTIL_isRegularFile(inputFileName)) + return NULL; + + UTIL_DISPLAY("[TRACE] start function readFileNamesTableFromFile\n"); + U64 inputFileSize = UTIL_getFileSize(inputFileName) + 1; /* (+1) to add '\0' at the end of last filename */ + + if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE) + return NULL; + + char* buf = (char*) malloc(inputFileSize * sizeof(char)); + if(!buf) { + UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create buffer.\n"); + return NULL; + } + + int nbFiles = readFromFile(buf, inputFileSize, inputFileName); + + if(nbFiles <= 0) { + free(buf); + return NULL; + } + + UTIL_DISPLAY("[TRACE] file closed with %d read lines\n", nbFiles); + + FileNamesTable* filesTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + if(!filesTable) { + free(buf); + UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create table for files.\n"); + return NULL; + } + + filesTable->tableSize = nbFiles; + filesTable->fileNames = (const char**) malloc((nbFiles+1) * sizeof(char*)); + + UTIL_DISPLAY("[TRACE] Start migration\n"); + + size_t i = 0, pos = 0; + for(i = 0, pos = 0; i < nbFiles; ++i) { + filesTable->fileNames[i] = buf+pos; + UTIL_DISPLAY("[TRACE] file %zu: %s\n", i, filesTable->fileNames[i]); + pos += strlen(buf+pos)+1; + } + + UTIL_DISPLAY("[TRACE] migration done\n"); + + + UTIL_DISPLAY("[TRACE] pos %zu inputFileSize %lu\n", pos, inputFileSize); + if(pos > inputFileSize){ + UTIL_freeFileNamesTable(filesTable); + if(buf) free(buf); + return NULL; + } + + filesTable->buf = buf; + UTIL_DISPLAY("[TRACE] finished reading\n"); + + return filesTable; +} + +void UTIL_freeFileNamesTable(FileNamesTable* table) { + if(table) { + if(table->fileNames) { + if(table->buf) + free(table->buf); + free(table->fileNames); + } + free(table); + } +} + +static size_t getTotalTableSize(FileNamesTable* table) { + size_t i = 0, totalSize = 0; + for(i = 0 ; i < table->tableSize && table->fileNames[i] ; ++i) { + totalSize += strlen(table->fileNames[i]) + 1; /* +1 to add '\0' at the end of each fileName */ + } + + return totalSize; +} + +FileNamesTable* +UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { + UTIL_DISPLAY("[TRACE] Start concatenation\n"); + unsigned newTableIdx = 0, idx1 = 0, idx2 = 0; + size_t i = 0; + + FileNamesTable* newTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + + if(!newTable) { + UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create new table for concatenation output.\n"); + return NULL; + } + + int newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2); + UTIL_DISPLAY("[TRACE] buf total size is: %d\n", newTotalTableSize); + + char* buf = (char*) malloc(newTotalTableSize * sizeof(char)); + if(!buf) { + UTIL_freeFileNamesTable(newTable); + UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create buf for concatenation output.\n"); + return NULL; + } + + for(i = 0; i < newTotalTableSize ; ++i) buf[i] = '\0'; + + newTable->tableSize = table1->tableSize + table2->tableSize; + newTable->fileNames = (const char **) malloc(newTable->tableSize * sizeof(char*)); + + if(!newTable->fileNames) { + UTIL_freeFileNamesTable(newTable); + if(buf) free(buf); + UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create new table for concatenation output.\n"); + return NULL; + } + + for (i = 0; i < newTable->tableSize; ++i) + newTable->fileNames[i] = NULL; + + UTIL_DISPLAY("[TRACE] add table1 concatenation of size %zu\n", table1->tableSize); + size_t pos = 0; + for( ; idx1 < table1->tableSize && table1->fileNames[idx1] && pos < newTotalTableSize; ++idx1, ++newTableIdx) { + size_t curLen = strlen(table1->fileNames[idx1]); + memcpy(buf+pos, table1->fileNames[idx1], curLen); + newTable->fileNames[newTableIdx] = buf+pos; + pos += curLen+1; + } + + UTIL_DISPLAY("[TRACE] table1 actual size %u\n", idx1); + + UTIL_DISPLAY("[TRACE] add table2 concatenation of size %zu\n", table2->tableSize); + for( ; idx2 < table2->tableSize && table2->fileNames[idx2] && pos < newTotalTableSize ; ++idx2, ++newTableIdx) { + size_t curLen = strlen(table2->fileNames[idx2]); + memcpy(buf+pos, table2->fileNames[idx2], curLen); + newTable->fileNames[newTableIdx] = buf+pos; + pos += curLen+1; + } + + if(pos > newTotalTableSize) { + UTIL_freeFileNamesTable(newTable); + if(buf) free(buf); + return NULL; + } + + UTIL_DISPLAY("[TRACE] table2 actual size %u\n", idx2); + UTIL_DISPLAY("[TRACE] new table actual size %u\n", newTableIdx); + + assert(newTableIdx == newTable->tableSize || newTable->fileNames[newTableIdx] == NULL); + + UTIL_DISPLAY("[TRACE] table1:\n"); + for(idx1 = 0 ; idx1 < table1->tableSize && table1->fileNames[idx1] ; ++idx1) + UTIL_DISPLAY("[TRACE] %u %s\n", idx1, table1->fileNames[idx1]); + + UTIL_DISPLAY("[TRACE] table2:\n"); + for(idx2 = 0 ; idx2 < table2->tableSize && table2->fileNames[idx2] ; ++idx2) + UTIL_DISPLAY("[TRACE] %u %s\n", idx2, table2->fileNames[idx2]); + + UTIL_DISPLAY("[TRACE] new table:\n"); + for(newTableIdx = 0; newTableIdx < newTable->tableSize && newTable->fileNames[newTableIdx] ; ++newTableIdx) + UTIL_DISPLAY("[TRACE] %u %s\n", newTableIdx, newTable->fileNames[newTableIdx]); + + UTIL_freeFileNamesTable(table1); + UTIL_freeFileNamesTable(table2); + UTIL_DISPLAY("[TRACE] concatenation finished\n"); + + newTable->buf = buf; + return newTable; +} + #ifdef _WIN32 int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks) { diff --git a/programs/util.h b/programs/util.h index 0080b63c..33a6d6de 100644 --- a/programs/util.h +++ b/programs/util.h @@ -90,7 +90,7 @@ extern "C" { * Constants ***************************************/ #define LIST_SIZE_INCREASE (8*1024) - +#define MAX_FILE_OF_FILE_NAMES_SIZE (1<<20)*50 /*-**************************************** * Compiler specifics @@ -140,6 +140,40 @@ U32 UTIL_isLink(const char* infilename); U64 UTIL_getFileSize(const char* infilename); U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles); +/*! UTIL_readLineFromFile(char* buf, size_t len, File* file): + * @return : int. size next line in file or -1 in case of file ends + * function reads next line in the file + * Will also modify `*file`, advancing it to position where it stopped reading. + */ +int UTIL_readLineFromFile(char* buf, size_t len, FILE* file); + +/*Note: tableSize is denotes the total capacity of table*/ +typedef struct +{ + const char** fileNames; + char* buf; + size_t tableSize; +} FileNamesTable; + +/*! UTIL_readFileNamesTableFromFile(const char* inputFileName) : + * @return : char** the fileNamesTable or NULL in case of not regular file or file doesn't exist. + * reads fileNamesTable from input fileName. + * Note: inputFileSize should be less than or equal 50MB + */ +FileNamesTable* UTIL_createFileNamesTable_fromFileName(const char* inputFileName); + +/*! UTIL_freeFileNamesTable(FileNamesTable* table) : + * This function takes an buffered based table and frees it. + * @return : void. + */ +void UTIL_freeFileNamesTable(FileNamesTable* table); + +/*! UTIL_concatenateTwoTables(FileNamesTable* table1,FileNamesTable* table2): + * takes table1, its maxSize, table2 and its maxSize, free them and returns its concatenation. + * @return : FileNamesTable* concatenation of two tables + * note table1 and table2 will be freed + */ +FileNamesTable* UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2); /* * A modified version of realloc(). diff --git a/programs/zstdcli.c b/programs/zstdcli.c index 98df728a..e8e17fff 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -565,6 +565,7 @@ int main(int argCount, const char* argv[]) nextArgumentIsMaxDict = 0, nextArgumentIsDictID = 0, nextArgumentsAreFiles = 0, + isTableBufferBased = 0, nextEntryIsDictionary = 0, operationResult = 0, separateFiles = 0, @@ -582,7 +583,9 @@ int main(int argCount, const char* argv[]) int cLevelLast = -1000000000; unsigned recursive = 0; unsigned memLimit = 0; - const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); /* argCount >= 1 */ + unsigned filenameTableSize = argCount; + const char** filenameTable = (const char**)malloc(filenameTableSize * sizeof(const char*)); /* argCount >= 1 */ + char* tableBuf = NULL; unsigned filenameIdx = 0; const char* programName = argv[0]; const char* outFileName = NULL; @@ -791,6 +794,66 @@ int main(int argCount, const char* argv[]) continue; } #endif + + if (longCommandWArg(&argument, "--file=")) { + DISPLAYLEVEL(4, "[TRACE] argument catched\n"); + const char* fileName = argument; + DISPLAYLEVEL(4, "[TRACE] fileName: %s\n", fileName); + if(!UTIL_fileExist(fileName) || !UTIL_isRegularFile(fileName)){ + DISPLAYLEVEL(1, "[ERROR] wrong fileName: %s\n", fileName); + CLEAN_RETURN(badusage(programName)); + } + + DISPLAYLEVEL(4, "[TRACE] call read function\n"); + FileNamesTable* extendedTable = UTIL_createFileNamesTable_fromFileName(fileName); + if(!extendedTable) { + CLEAN_RETURN(badusage(programName)); + } + + DISPLAYLEVEL(4, "[TRACE] call read function is finished\n"); + DISPLAYLEVEL(4, "[TRACE] extendedFileNamesTable:\n"); + size_t extendedTableSize = extendedTable->tableSize; + const char ** extendedFileNamesTable = extendedTable->fileNames; + + int i = 0; + for(i = 0; i < extendedTableSize; ++i) + printf("%s\n",extendedFileNamesTable[i]); + + DISPLAYLEVEL(4, "[TRACE] call concatenation function\n"); + DISPLAYLEVEL(4, "[TRACE] filenameidx: %d\n", filenameIdx); + + for(i = filenameIdx; i < filenameTableSize ; ++i) + filenameTable[i] = NULL; + + FileNamesTable* curTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + if(!curTable) { + UTIL_freeFileNamesTable(extendedTable); + CLEAN_RETURN(badusage(programName)); + } + + curTable->fileNames = filenameTable; + curTable->tableSize = filenameTableSize; + curTable->buf = tableBuf; + + FileNamesTable* concatenatedTables = UTIL_concatenateTwoTables(curTable, extendedTable); + if(!concatenatedTables) { + UTIL_freeFileNamesTable(curTable); + UTIL_freeFileNamesTable(extendedTable); + CLEAN_RETURN(badusage(programName)); + } + + filenameTable = concatenatedTables->fileNames; + filenameTableSize = concatenatedTables->tableSize; + tableBuf = concatenatedTables->buf; + + filenameIdx += extendedTableSize; + free(concatenatedTables); + isTableBufferBased = 1; + + DISPLAYLEVEL(1, "[TRACE] call concatenation function is finished\n"); + + continue; + } /* fall-through, will trigger bad_usage() later on */ } @@ -1193,6 +1256,13 @@ int main(int argCount, const char* argv[]) _end: FIO_freePreferences(prefs); + if(filenameTable) { + if(isTableBufferBased && tableBuf){ + free(tableBuf); + } + } + + if (main_pause) waitEnter(); #ifdef UTIL_HAS_CREATEFILELIST if (extendedFileList) From 9a454e97242a166a519c9501604accd75a764a82 Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Wed, 23 Oct 2019 20:14:48 +0100 Subject: [PATCH 02/17] solving C90 issues in defining local variables in middle of code and comparing uncompatible types --- programs/util.c | 7 ++++--- programs/zstdcli.c | 32 ++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/programs/util.c b/programs/util.c index 34d3acdf..cb2e6410 100644 --- a/programs/util.c +++ b/programs/util.c @@ -251,12 +251,13 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { return NULL; } - int nbFiles = readFromFile(buf, inputFileSize, inputFileName); + int ret_nbFiles = readFromFile(buf, inputFileSize, inputFileName); - if(nbFiles <= 0) { + if(ret_nbFiles <= 0) { free(buf); return NULL; } + unsigned nbFiles = ret_nbFiles; UTIL_DISPLAY("[TRACE] file closed with %d read lines\n", nbFiles); @@ -328,7 +329,7 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { return NULL; } - int newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2); + size_t newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2); UTIL_DISPLAY("[TRACE] buf total size is: %d\n", newTotalTableSize); char* buf = (char*) malloc(newTotalTableSize * sizeof(char)); diff --git a/programs/zstdcli.c b/programs/zstdcli.c index a8cb5cce..ade7ca00 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -587,6 +587,9 @@ int main(int argCount, const char* argv[]) unsigned memLimit = 0; unsigned filenameTableSize = argCount; const char** filenameTable = (const char**)malloc(filenameTableSize * sizeof(const char*)); /* argCount >= 1 */ + FileNamesTable* extendedTable = NULL; + FileNamesTable* concatenatedTables = NULL; + FileNamesTable* curTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); char* tableBuf = NULL; unsigned filenameIdx = 0; const char* programName = argv[0]; @@ -622,6 +625,9 @@ int main(int argCount, const char* argv[]) (void)memLimit; /* not used when ZSTD_NODECOMPRESS set */ if (filenameTable==NULL) { DISPLAY("zstd: %s \n", strerror(errno)); exit(1); } filenameTable[0] = stdinmark; + curTable->fileNames = filenameTable; + curTable->tableSize = filenameTableSize; + curTable->buf = tableBuf; g_displayOut = stderr; cLevel = init_cLevel(); programName = lastNameFromPath(programName); @@ -803,26 +809,24 @@ int main(int argCount, const char* argv[]) if (longCommandWArg(&argument, "--file=")) { DISPLAYLEVEL(4, "[TRACE] argument catched\n"); const char* fileName = argument; - DISPLAYLEVEL(4, "[TRACE] fileName: %s\n", fileName); - if(!UTIL_fileExist(fileName) || !UTIL_isRegularFile(fileName)){ - DISPLAYLEVEL(1, "[ERROR] wrong fileName: %s\n", fileName); + DISPLAYLEVEL(4, "[TRACE] fileName: %s\n", argument); + if(!UTIL_fileExist(fileName) || !UTIL_isRegularFile(argument)){ + DISPLAYLEVEL(1, "[ERROR] wrong fileName: %s\n", argument); CLEAN_RETURN(badusage(programName)); } DISPLAYLEVEL(4, "[TRACE] call read function\n"); - FileNamesTable* extendedTable = UTIL_createFileNamesTable_fromFileName(fileName); + extendedTable = UTIL_createFileNamesTable_fromFileName(argument); if(!extendedTable) { CLEAN_RETURN(badusage(programName)); } DISPLAYLEVEL(4, "[TRACE] call read function is finished\n"); DISPLAYLEVEL(4, "[TRACE] extendedFileNamesTable:\n"); - size_t extendedTableSize = extendedTable->tableSize; - const char ** extendedFileNamesTable = extendedTable->fileNames; - int i = 0; - for(i = 0; i < extendedTableSize; ++i) - printf("%s\n",extendedFileNamesTable[i]); + unsigned i = 0; + for(i = 0; i < extendedTable->tableSize; ++i) + printf("%s\n",extendedTable->fileNames[i]); DISPLAYLEVEL(4, "[TRACE] call concatenation function\n"); DISPLAYLEVEL(4, "[TRACE] filenameidx: %d\n", filenameIdx); @@ -830,7 +834,6 @@ int main(int argCount, const char* argv[]) for(i = filenameIdx; i < filenameTableSize ; ++i) filenameTable[i] = NULL; - FileNamesTable* curTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); if(!curTable) { UTIL_freeFileNamesTable(extendedTable); CLEAN_RETURN(badusage(programName)); @@ -840,7 +843,7 @@ int main(int argCount, const char* argv[]) curTable->tableSize = filenameTableSize; curTable->buf = tableBuf; - FileNamesTable* concatenatedTables = UTIL_concatenateTwoTables(curTable, extendedTable); + concatenatedTables = UTIL_concatenateTwoTables(curTable, extendedTable); if(!concatenatedTables) { UTIL_freeFileNamesTable(curTable); UTIL_freeFileNamesTable(extendedTable); @@ -851,8 +854,7 @@ int main(int argCount, const char* argv[]) filenameTableSize = concatenatedTables->tableSize; tableBuf = concatenatedTables->buf; - filenameIdx += extendedTableSize; - free(concatenatedTables); + filenameIdx += extendedTable->tableSize; isTableBufferBased = 1; DISPLAYLEVEL(1, "[TRACE] call concatenation function is finished\n"); @@ -1273,7 +1275,9 @@ _end: free(tableBuf); } } - + UTIL_freeFileNamesTable(curTable); + UTIL_freeFileNamesTable(extendedTable); + UTIL_freeFileNamesTable(concatenatedTables); if (main_pause) waitEnter(); #ifdef UTIL_HAS_CREATEFILELIST From 8cbe42fcb029e0b8c8e1c169be219f4c19be29d8 Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Wed, 23 Oct 2019 20:22:07 +0100 Subject: [PATCH 03/17] solving the rest of C90 issues in defining local variables in middle of code and comparing uncompatible types --- programs/util.c | 4 ++-- programs/zstdcli.c | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/programs/util.c b/programs/util.c index cb2e6410..e28e4a96 100644 --- a/programs/util.c +++ b/programs/util.c @@ -283,7 +283,7 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { UTIL_DISPLAY("[TRACE] migration done\n"); - UTIL_DISPLAY("[TRACE] pos %zu inputFileSize %lu\n", pos, inputFileSize); + UTIL_DISPLAY("[TRACE] pos %zu inputFileSize %llu\n", pos, inputFileSize); if(pos > inputFileSize){ UTIL_freeFileNamesTable(filesTable); if(buf) free(buf); @@ -330,7 +330,7 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { } size_t newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2); - UTIL_DISPLAY("[TRACE] buf total size is: %d\n", newTotalTableSize); + UTIL_DISPLAY("[TRACE] buf total size is: %zu\n", newTotalTableSize); char* buf = (char*) malloc(newTotalTableSize * sizeof(char)); if(!buf) { diff --git a/programs/zstdcli.c b/programs/zstdcli.c index ade7ca00..9a05cf72 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -808,9 +808,8 @@ int main(int argCount, const char* argv[]) if (longCommandWArg(&argument, "--file=")) { DISPLAYLEVEL(4, "[TRACE] argument catched\n"); - const char* fileName = argument; DISPLAYLEVEL(4, "[TRACE] fileName: %s\n", argument); - if(!UTIL_fileExist(fileName) || !UTIL_isRegularFile(argument)){ + if(!UTIL_fileExist(argument) || !UTIL_isRegularFile(argument)){ DISPLAYLEVEL(1, "[ERROR] wrong fileName: %s\n", argument); CLEAN_RETURN(badusage(programName)); } @@ -824,7 +823,7 @@ int main(int argCount, const char* argv[]) DISPLAYLEVEL(4, "[TRACE] call read function is finished\n"); DISPLAYLEVEL(4, "[TRACE] extendedFileNamesTable:\n"); - unsigned i = 0; + unsigned i; for(i = 0; i < extendedTable->tableSize; ++i) printf("%s\n",extendedTable->fileNames[i]); From f43e45954ff337f5e9e74686b7b2c66581506388 Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 09:39:16 +0100 Subject: [PATCH 04/17] fixing memory leak issue and removing c90 issue --- programs/util.c | 3 +++ programs/zstdcli.c | 16 +++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/programs/util.c b/programs/util.c index e28e4a96..7da11e2f 100644 --- a/programs/util.c +++ b/programs/util.c @@ -308,6 +308,7 @@ void UTIL_freeFileNamesTable(FileNamesTable* table) { } static size_t getTotalTableSize(FileNamesTable* table) { + UTIL_DISPLAY("[TRACE] getTotalTableSize \n"); size_t i = 0, totalSize = 0; for(i = 0 ; i < table->tableSize && table->fileNames[i] ; ++i) { totalSize += strlen(table->fileNames[i]) + 1; /* +1 to add '\0' at the end of each fileName */ @@ -324,6 +325,8 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { FileNamesTable* newTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + UTIL_DISPLAY("[TRACE] newTable created\n"); + if(!newTable) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create new table for concatenation output.\n"); return NULL; diff --git a/programs/zstdcli.c b/programs/zstdcli.c index 9a05cf72..43d496c9 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -589,7 +589,7 @@ int main(int argCount, const char* argv[]) const char** filenameTable = (const char**)malloc(filenameTableSize * sizeof(const char*)); /* argCount >= 1 */ FileNamesTable* extendedTable = NULL; FileNamesTable* concatenatedTables = NULL; - FileNamesTable* curTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + FileNamesTable* curTable = NULL; char* tableBuf = NULL; unsigned filenameIdx = 0; const char* programName = argv[0]; @@ -625,9 +625,6 @@ int main(int argCount, const char* argv[]) (void)memLimit; /* not used when ZSTD_NODECOMPRESS set */ if (filenameTable==NULL) { DISPLAY("zstd: %s \n", strerror(errno)); exit(1); } filenameTable[0] = stdinmark; - curTable->fileNames = filenameTable; - curTable->tableSize = filenameTableSize; - curTable->buf = tableBuf; g_displayOut = stderr; cLevel = init_cLevel(); programName = lastNameFromPath(programName); @@ -823,15 +820,16 @@ int main(int argCount, const char* argv[]) DISPLAYLEVEL(4, "[TRACE] call read function is finished\n"); DISPLAYLEVEL(4, "[TRACE] extendedFileNamesTable:\n"); - unsigned i; - for(i = 0; i < extendedTable->tableSize; ++i) - printf("%s\n",extendedTable->fileNames[i]); DISPLAYLEVEL(4, "[TRACE] call concatenation function\n"); DISPLAYLEVEL(4, "[TRACE] filenameidx: %d\n", filenameIdx); - for(i = filenameIdx; i < filenameTableSize ; ++i) - filenameTable[i] = NULL; + // unsigned i = 0; + // for(i = filenameIdx; i < filenameTableSize ; ++i) + filenameTable[filenameIdx] = NULL; // marking end of table + + + curTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); if(!curTable) { UTIL_freeFileNamesTable(extendedTable); From aefa18ee380d4b71255a7092c7260393c3aeb10a Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 10:12:51 +0100 Subject: [PATCH 05/17] fixing c90 issue in util.c --- programs/util.c | 46 ++++++++++++++++++++++++++++------------------ programs/zstdcli.c | 6 +----- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/programs/util.c b/programs/util.c index 7da11e2f..a7927850 100644 --- a/programs/util.c +++ b/programs/util.c @@ -176,13 +176,15 @@ U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbF int UTIL_readLineFromFile(char* buf, size_t len, FILE* file) { + char* fgetsCheck = NULL; + if (feof(file)) { UTIL_DISPLAYLEVEL(1, "[ERROR] end of file reached and need to read\n"); return -1; } UTIL_DISPLAY("[TRACE] read line\n"); - char* fgetsCheck = fgets(buf, len, file); + fgetsCheck = fgets(buf, len, file); if(fgetsCheck == NULL || fgetsCheck != buf) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readLineFromFile] fgets has a problem check: %s buf: %s \n", @@ -197,21 +199,21 @@ int UTIL_readLineFromFile(char* buf, size_t len, FILE* file) { /* Warning: inputFileSize should be less than or equal buf capacity and buf should be initialized*/ static int readFromFile(char* buf, size_t inputFileSize, const char* inputFileName) { + UTIL_DISPLAY("[TRACE] open file\n"); + FILE* inputFile = fopen(inputFileName, "r"); + int nbFiles = -1; + unsigned pos = 0; + if(!buf) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create buffer.\n"); return -1; } - UTIL_DISPLAY("[TRACE] open file\n"); - FILE* inputFile = fopen(inputFileName, "r"); - int nbFiles = -1; - if(!inputFile) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't open file to read input file names.\n"); return -1; } - unsigned pos = 0; for(nbFiles=0; !feof(inputFile) ; ) { if(UTIL_readLineFromFile(buf+pos, inputFileSize, inputFile) > 0) { int len = (int) strlen(buf+pos); @@ -234,13 +236,20 @@ static int readFromFile(char* buf, size_t inputFileSize, const char* inputFileNa /*Note: buf is not freed in case function successfully created table because filesTable->fileNames[0] = buf*/ FileNamesTable* UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { + U64 inputFileSize = 0; + unsigned nbFiles = 0; + int ret_nbFiles = -1; + + FileNamesTable* filesTable = (FileNamesTable*) malloc(sizeof(FileNamesTable));; + + size_t i = 0, pos = 0; UTIL_DISPLAY("file check\n"); if(!UTIL_fileExist(inputFileName) || !UTIL_isRegularFile(inputFileName)) return NULL; UTIL_DISPLAY("[TRACE] start function readFileNamesTableFromFile\n"); - U64 inputFileSize = UTIL_getFileSize(inputFileName) + 1; /* (+1) to add '\0' at the end of last filename */ + inputFileSize = UTIL_getFileSize(inputFileName) + 1; /* (+1) to add '\0' at the end of last filename */ if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE) return NULL; @@ -251,17 +260,16 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { return NULL; } - int ret_nbFiles = readFromFile(buf, inputFileSize, inputFileName); + ret_nbFiles = readFromFile(buf, inputFileSize, inputFileName); if(ret_nbFiles <= 0) { free(buf); return NULL; } - unsigned nbFiles = ret_nbFiles; + nbFiles = ret_nbFiles; UTIL_DISPLAY("[TRACE] file closed with %d read lines\n", nbFiles); - FileNamesTable* filesTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); if(!filesTable) { free(buf); UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create table for files.\n"); @@ -273,7 +281,7 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { UTIL_DISPLAY("[TRACE] Start migration\n"); - size_t i = 0, pos = 0; + for(i = 0, pos = 0; i < nbFiles; ++i) { filesTable->fileNames[i] = buf+pos; UTIL_DISPLAY("[TRACE] file %zu: %s\n", i, filesTable->fileNames[i]); @@ -283,7 +291,7 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { UTIL_DISPLAY("[TRACE] migration done\n"); - UTIL_DISPLAY("[TRACE] pos %zu inputFileSize %llu\n", pos, inputFileSize); + UTIL_DISPLAY("[TRACE] pos %zu inputFileSize %lu\n", pos, inputFileSize); if(pos > inputFileSize){ UTIL_freeFileNamesTable(filesTable); if(buf) free(buf); @@ -308,7 +316,6 @@ void UTIL_freeFileNamesTable(FileNamesTable* table) { } static size_t getTotalTableSize(FileNamesTable* table) { - UTIL_DISPLAY("[TRACE] getTotalTableSize \n"); size_t i = 0, totalSize = 0; for(i = 0 ; i < table->tableSize && table->fileNames[i] ; ++i) { totalSize += strlen(table->fileNames[i]) + 1; /* +1 to add '\0' at the end of each fileName */ @@ -319,12 +326,15 @@ static size_t getTotalTableSize(FileNamesTable* table) { FileNamesTable* UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { - UTIL_DISPLAY("[TRACE] Start concatenation\n"); unsigned newTableIdx = 0, idx1 = 0, idx2 = 0; - size_t i = 0; + size_t i = 0, pos = 0; + + size_t newTotalTableSize = 0; FileNamesTable* newTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + UTIL_DISPLAY("[TRACE] Start concatenation\n"); + UTIL_DISPLAY("[TRACE] newTable created\n"); if(!newTable) { @@ -332,7 +342,7 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { return NULL; } - size_t newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2); + newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2); UTIL_DISPLAY("[TRACE] buf total size is: %zu\n", newTotalTableSize); char* buf = (char*) malloc(newTotalTableSize * sizeof(char)); @@ -358,7 +368,6 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { newTable->fileNames[i] = NULL; UTIL_DISPLAY("[TRACE] add table1 concatenation of size %zu\n", table1->tableSize); - size_t pos = 0; for( ; idx1 < table1->tableSize && table1->fileNames[idx1] && pos < newTotalTableSize; ++idx1, ++newTableIdx) { size_t curLen = strlen(table1->fileNames[idx1]); memcpy(buf+pos, table1->fileNames[idx1], curLen); @@ -399,11 +408,12 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { for(newTableIdx = 0; newTableIdx < newTable->tableSize && newTable->fileNames[newTableIdx] ; ++newTableIdx) UTIL_DISPLAY("[TRACE] %u %s\n", newTableIdx, newTable->fileNames[newTableIdx]); + newTable->buf = buf; + UTIL_freeFileNamesTable(table1); UTIL_freeFileNamesTable(table2); UTIL_DISPLAY("[TRACE] concatenation finished\n"); - newTable->buf = buf; return newTable; } diff --git a/programs/zstdcli.c b/programs/zstdcli.c index 43d496c9..eaa46178 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -820,14 +820,10 @@ int main(int argCount, const char* argv[]) DISPLAYLEVEL(4, "[TRACE] call read function is finished\n"); DISPLAYLEVEL(4, "[TRACE] extendedFileNamesTable:\n"); - DISPLAYLEVEL(4, "[TRACE] call concatenation function\n"); DISPLAYLEVEL(4, "[TRACE] filenameidx: %d\n", filenameIdx); - // unsigned i = 0; - // for(i = filenameIdx; i < filenameTableSize ; ++i) - filenameTable[filenameIdx] = NULL; // marking end of table - + filenameTable[filenameIdx] = NULL; // marking end of table curTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); From 8a9741b3ee0fbe05e9a6412abd68e76d9ac1041c Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 10:17:31 +0100 Subject: [PATCH 06/17] fixing c90 issue in util.c cont. --- programs/util.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/programs/util.c b/programs/util.c index a7927850..10c4fe4b 100644 --- a/programs/util.c +++ b/programs/util.c @@ -199,11 +199,12 @@ int UTIL_readLineFromFile(char* buf, size_t len, FILE* file) { /* Warning: inputFileSize should be less than or equal buf capacity and buf should be initialized*/ static int readFromFile(char* buf, size_t inputFileSize, const char* inputFileName) { - UTIL_DISPLAY("[TRACE] open file\n"); FILE* inputFile = fopen(inputFileName, "r"); int nbFiles = -1; unsigned pos = 0; + UTIL_DISPLAY("[TRACE] open file\n"); + if(!buf) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create buffer.\n"); return -1; @@ -239,6 +240,7 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { U64 inputFileSize = 0; unsigned nbFiles = 0; int ret_nbFiles = -1; + char* buf = NULL; FileNamesTable* filesTable = (FileNamesTable*) malloc(sizeof(FileNamesTable));; @@ -254,7 +256,7 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE) return NULL; - char* buf = (char*) malloc(inputFileSize * sizeof(char)); + buf = (char*) malloc(inputFileSize * sizeof(char)); if(!buf) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create buffer.\n"); return NULL; From c799f33899c58e2967708720477101548ac925a5 Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 10:23:36 +0100 Subject: [PATCH 07/17] fixing c90 issue in util.c cont. again --- programs/util.c | 1 - 1 file changed, 1 deletion(-) diff --git a/programs/util.c b/programs/util.c index 10c4fe4b..c79fe7d4 100644 --- a/programs/util.c +++ b/programs/util.c @@ -293,7 +293,6 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { UTIL_DISPLAY("[TRACE] migration done\n"); - UTIL_DISPLAY("[TRACE] pos %zu inputFileSize %lu\n", pos, inputFileSize); if(pos > inputFileSize){ UTIL_freeFileNamesTable(filesTable); if(buf) free(buf); From 47712c9b15b4df51e10854cea51357f2c2b015bc Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 10:30:05 +0100 Subject: [PATCH 08/17] fixing c90 issue in util.c cont. --- programs/util.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/programs/util.c b/programs/util.c index c79fe7d4..f3e76bf8 100644 --- a/programs/util.c +++ b/programs/util.c @@ -241,11 +241,10 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { unsigned nbFiles = 0; int ret_nbFiles = -1; char* buf = NULL; - - FileNamesTable* filesTable = (FileNamesTable*) malloc(sizeof(FileNamesTable));; - size_t i = 0, pos = 0; + FileNamesTable* filesTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + UTIL_DISPLAY("file check\n"); if(!UTIL_fileExist(inputFileName) || !UTIL_isRegularFile(inputFileName)) return NULL; @@ -329,11 +328,12 @@ FileNamesTable* UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { unsigned newTableIdx = 0, idx1 = 0, idx2 = 0; size_t i = 0, pos = 0; - size_t newTotalTableSize = 0; FileNamesTable* newTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + char* buf = NULL; + UTIL_DISPLAY("[TRACE] Start concatenation\n"); UTIL_DISPLAY("[TRACE] newTable created\n"); @@ -346,7 +346,7 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2); UTIL_DISPLAY("[TRACE] buf total size is: %zu\n", newTotalTableSize); - char* buf = (char*) malloc(newTotalTableSize * sizeof(char)); + buf = (char*) malloc(newTotalTableSize * sizeof(char)); if(!buf) { UTIL_freeFileNamesTable(newTable); UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create buf for concatenation output.\n"); From 849b8c6de8466558c687baaed9e8d5bf5ff629b8 Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 11:10:13 +0100 Subject: [PATCH 09/17] fixing continuous integeration errors and removing a lot of logs --- programs/util.c | 37 +------------------------------------ programs/zstdcli.c | 13 ++----------- 2 files changed, 3 insertions(+), 47 deletions(-) diff --git a/programs/util.c b/programs/util.c index f3e76bf8..fc90e904 100644 --- a/programs/util.c +++ b/programs/util.c @@ -182,9 +182,8 @@ int UTIL_readLineFromFile(char* buf, size_t len, FILE* file) { UTIL_DISPLAYLEVEL(1, "[ERROR] end of file reached and need to read\n"); return -1; } - UTIL_DISPLAY("[TRACE] read line\n"); - fgetsCheck = fgets(buf, len, file); + fgetsCheck = fgets(buf, (int) len, file); if(fgetsCheck == NULL || fgetsCheck != buf) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readLineFromFile] fgets has a problem check: %s buf: %s \n", @@ -192,7 +191,6 @@ int UTIL_readLineFromFile(char* buf, size_t len, FILE* file) { return -1; } - UTIL_DISPLAY("[TRACE] length of Line: %d\n" , (int)strlen(buf)); return (int) strlen(buf)-1; /* -1 to ignore '\n' character */ } @@ -203,7 +201,6 @@ static int readFromFile(char* buf, size_t inputFileSize, const char* inputFileNa int nbFiles = -1; unsigned pos = 0; - UTIL_DISPLAY("[TRACE] open file\n"); if(!buf) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create buffer.\n"); @@ -219,12 +216,9 @@ static int readFromFile(char* buf, size_t inputFileSize, const char* inputFileNa if(UTIL_readLineFromFile(buf+pos, inputFileSize, inputFile) > 0) { int len = (int) strlen(buf+pos); buf[pos+len-1] = '\0'; /* replace '\n' with '\0'*/ - UTIL_DISPLAY("[TRACE] line: %s\n", buf+pos); pos += len; ++nbFiles; } - else - UTIL_DISPLAY("[TRACE] ignored line: %s", buf+pos); } fclose(inputFile); @@ -249,7 +243,6 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { if(!UTIL_fileExist(inputFileName) || !UTIL_isRegularFile(inputFileName)) return NULL; - UTIL_DISPLAY("[TRACE] start function readFileNamesTableFromFile\n"); inputFileSize = UTIL_getFileSize(inputFileName) + 1; /* (+1) to add '\0' at the end of last filename */ if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE) @@ -269,7 +262,6 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { } nbFiles = ret_nbFiles; - UTIL_DISPLAY("[TRACE] file closed with %d read lines\n", nbFiles); if(!filesTable) { free(buf); @@ -280,7 +272,6 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { filesTable->tableSize = nbFiles; filesTable->fileNames = (const char**) malloc((nbFiles+1) * sizeof(char*)); - UTIL_DISPLAY("[TRACE] Start migration\n"); for(i = 0, pos = 0; i < nbFiles; ++i) { @@ -289,8 +280,6 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { pos += strlen(buf+pos)+1; } - UTIL_DISPLAY("[TRACE] migration done\n"); - if(pos > inputFileSize){ UTIL_freeFileNamesTable(filesTable); @@ -299,7 +288,6 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { } filesTable->buf = buf; - UTIL_DISPLAY("[TRACE] finished reading\n"); return filesTable; } @@ -334,9 +322,6 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { char* buf = NULL; - UTIL_DISPLAY("[TRACE] Start concatenation\n"); - - UTIL_DISPLAY("[TRACE] newTable created\n"); if(!newTable) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create new table for concatenation output.\n"); @@ -344,7 +329,6 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { } newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2); - UTIL_DISPLAY("[TRACE] buf total size is: %zu\n", newTotalTableSize); buf = (char*) malloc(newTotalTableSize * sizeof(char)); if(!buf) { @@ -368,7 +352,6 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { for (i = 0; i < newTable->tableSize; ++i) newTable->fileNames[i] = NULL; - UTIL_DISPLAY("[TRACE] add table1 concatenation of size %zu\n", table1->tableSize); for( ; idx1 < table1->tableSize && table1->fileNames[idx1] && pos < newTotalTableSize; ++idx1, ++newTableIdx) { size_t curLen = strlen(table1->fileNames[idx1]); memcpy(buf+pos, table1->fileNames[idx1], curLen); @@ -376,9 +359,7 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { pos += curLen+1; } - UTIL_DISPLAY("[TRACE] table1 actual size %u\n", idx1); - UTIL_DISPLAY("[TRACE] add table2 concatenation of size %zu\n", table2->tableSize); for( ; idx2 < table2->tableSize && table2->fileNames[idx2] && pos < newTotalTableSize ; ++idx2, ++newTableIdx) { size_t curLen = strlen(table2->fileNames[idx2]); memcpy(buf+pos, table2->fileNames[idx2], curLen); @@ -392,28 +373,12 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { return NULL; } - UTIL_DISPLAY("[TRACE] table2 actual size %u\n", idx2); - UTIL_DISPLAY("[TRACE] new table actual size %u\n", newTableIdx); - assert(newTableIdx == newTable->tableSize || newTable->fileNames[newTableIdx] == NULL); - UTIL_DISPLAY("[TRACE] table1:\n"); - for(idx1 = 0 ; idx1 < table1->tableSize && table1->fileNames[idx1] ; ++idx1) - UTIL_DISPLAY("[TRACE] %u %s\n", idx1, table1->fileNames[idx1]); - - UTIL_DISPLAY("[TRACE] table2:\n"); - for(idx2 = 0 ; idx2 < table2->tableSize && table2->fileNames[idx2] ; ++idx2) - UTIL_DISPLAY("[TRACE] %u %s\n", idx2, table2->fileNames[idx2]); - - UTIL_DISPLAY("[TRACE] new table:\n"); - for(newTableIdx = 0; newTableIdx < newTable->tableSize && newTable->fileNames[newTableIdx] ; ++newTableIdx) - UTIL_DISPLAY("[TRACE] %u %s\n", newTableIdx, newTable->fileNames[newTableIdx]); - newTable->buf = buf; UTIL_freeFileNamesTable(table1); UTIL_freeFileNamesTable(table2); - UTIL_DISPLAY("[TRACE] concatenation finished\n"); return newTable; } diff --git a/programs/zstdcli.c b/programs/zstdcli.c index eaa46178..c6146f5f 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -585,7 +585,7 @@ int main(int argCount, const char* argv[]) int cLevelLast = -1000000000; unsigned recursive = 0; unsigned memLimit = 0; - unsigned filenameTableSize = argCount; + size_t filenameTableSize = argCount; const char** filenameTable = (const char**)malloc(filenameTableSize * sizeof(const char*)); /* argCount >= 1 */ FileNamesTable* extendedTable = NULL; FileNamesTable* concatenatedTables = NULL; @@ -804,24 +804,17 @@ int main(int argCount, const char* argv[]) #endif if (longCommandWArg(&argument, "--file=")) { - DISPLAYLEVEL(4, "[TRACE] argument catched\n"); - DISPLAYLEVEL(4, "[TRACE] fileName: %s\n", argument); + if(!UTIL_fileExist(argument) || !UTIL_isRegularFile(argument)){ DISPLAYLEVEL(1, "[ERROR] wrong fileName: %s\n", argument); CLEAN_RETURN(badusage(programName)); } - DISPLAYLEVEL(4, "[TRACE] call read function\n"); extendedTable = UTIL_createFileNamesTable_fromFileName(argument); if(!extendedTable) { CLEAN_RETURN(badusage(programName)); } - DISPLAYLEVEL(4, "[TRACE] call read function is finished\n"); - DISPLAYLEVEL(4, "[TRACE] extendedFileNamesTable:\n"); - - DISPLAYLEVEL(4, "[TRACE] call concatenation function\n"); - DISPLAYLEVEL(4, "[TRACE] filenameidx: %d\n", filenameIdx); filenameTable[filenameIdx] = NULL; // marking end of table @@ -850,8 +843,6 @@ int main(int argCount, const char* argv[]) filenameIdx += extendedTable->tableSize; isTableBufferBased = 1; - DISPLAYLEVEL(1, "[TRACE] call concatenation function is finished\n"); - continue; } /* fall-through, will trigger bad_usage() later on */ From 639bb46954da5ab87404b093bbdd76a9bbdebdad Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 11:35:26 +0100 Subject: [PATCH 10/17] removing extra logs --- programs/util.c | 1 - 1 file changed, 1 deletion(-) diff --git a/programs/util.c b/programs/util.c index fc90e904..febfb4ad 100644 --- a/programs/util.c +++ b/programs/util.c @@ -276,7 +276,6 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { for(i = 0, pos = 0; i < nbFiles; ++i) { filesTable->fileNames[i] = buf+pos; - UTIL_DISPLAY("[TRACE] file %zu: %s\n", i, filesTable->fileNames[i]); pos += strlen(buf+pos)+1; } From 0e6a73b1485f44e13b495481491a067851653571 Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 13:51:12 +0100 Subject: [PATCH 11/17] fixing newTable issues and some warnings --- programs/util.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/programs/util.c b/programs/util.c index febfb4ad..74b4decc 100644 --- a/programs/util.c +++ b/programs/util.c @@ -237,9 +237,8 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { char* buf = NULL; size_t i = 0, pos = 0; - FileNamesTable* filesTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + FileNamesTable* filesTable = NULL; - UTIL_DISPLAY("file check\n"); if(!UTIL_fileExist(inputFileName) || !UTIL_isRegularFile(inputFileName)) return NULL; @@ -262,7 +261,7 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { } nbFiles = ret_nbFiles; - + filesTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); if(!filesTable) { free(buf); UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create table for files.\n"); @@ -294,10 +293,13 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { void UTIL_freeFileNamesTable(FileNamesTable* table) { if(table) { if(table->fileNames) { - if(table->buf) - free(table->buf); free(table->fileNames); } + + if(table->buf) { + free(table->buf); + } + free(table); } } @@ -317,11 +319,13 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { size_t i = 0, pos = 0; size_t newTotalTableSize = 0; - FileNamesTable* newTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + FileNamesTable* newTable = NULL; char* buf = NULL; + newTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + if(!newTable) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create new table for concatenation output.\n"); return NULL; @@ -372,8 +376,6 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { return NULL; } - assert(newTableIdx == newTable->tableSize || newTable->fileNames[newTableIdx] == NULL); - newTable->buf = buf; UTIL_freeFileNamesTable(table1); From 5e206fdd5370d5ed8c7fcf49ba9abad9d0850ed0 Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 14:21:01 +0100 Subject: [PATCH 12/17] fixing some warning --- programs/util.c | 2 +- programs/util.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/programs/util.c b/programs/util.c index 74b4decc..277b0cd5 100644 --- a/programs/util.c +++ b/programs/util.c @@ -296,7 +296,7 @@ void UTIL_freeFileNamesTable(FileNamesTable* table) { free(table->fileNames); } - if(table->buf) { + if(table && table->buf) { free(table->buf); } diff --git a/programs/util.h b/programs/util.h index f126246b..cb01fcef 100644 --- a/programs/util.h +++ b/programs/util.h @@ -151,8 +151,8 @@ int UTIL_readLineFromFile(char* buf, size_t len, FILE* file); /*Note: tableSize is denotes the total capacity of table*/ typedef struct { - const char** fileNames; - char* buf; + const char** fileNames = NULL; + char* buf = NULL; size_t tableSize; } FileNamesTable; From cddb05ef8c0b3a169456df789d02ce5002b0f03d Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 14:42:37 +0100 Subject: [PATCH 13/17] fixing some warning --- programs/util.c | 16 ++++++++++++++-- programs/util.h | 14 ++++++++++++-- programs/zstdcli.c | 6 +----- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/programs/util.c b/programs/util.c index 277b0cd5..7257c8a5 100644 --- a/programs/util.c +++ b/programs/util.c @@ -261,7 +261,7 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { } nbFiles = ret_nbFiles; - filesTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + filesTable = UTIL_createFileNamesTable(NULL, NULL, 0); if(!filesTable) { free(buf); UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create table for files.\n"); @@ -290,6 +290,18 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { return filesTable; } +FileNamesTable* +UTIL_createFileNamesTable(const char** filenames, char* buf, size_t tableSize){ + FileNamesTable* table = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + if(!table) { + return NULL; + } + table->fileNames = filenames; + table->buf = buf; + table->tableSize = tableSize; + return table; +} + void UTIL_freeFileNamesTable(FileNamesTable* table) { if(table) { if(table->fileNames) { @@ -324,7 +336,7 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2) { char* buf = NULL; - newTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + newTable = UTIL_createFileNamesTable(NULL, NULL, 0); if(!newTable) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_concatenateTwoTables] Can't create new table for concatenation output.\n"); diff --git a/programs/util.h b/programs/util.h index cb01fcef..bd275e2c 100644 --- a/programs/util.h +++ b/programs/util.h @@ -151,8 +151,8 @@ int UTIL_readLineFromFile(char* buf, size_t len, FILE* file); /*Note: tableSize is denotes the total capacity of table*/ typedef struct { - const char** fileNames = NULL; - char* buf = NULL; + const char** fileNames; + char* buf; size_t tableSize; } FileNamesTable; @@ -163,6 +163,16 @@ typedef struct */ FileNamesTable* UTIL_createFileNamesTable_fromFileName(const char* inputFileName); + +/*! UTIL_freeFileNamesTable(const char** filenames, char* buf, size_t tableSize) : + * This function takes an buffered based filename, buf and tableSize to create its object. + * @return : FileNamesTable* + */ + +FileNamesTable* +UTIL_createFileNamesTable(const char** filenames, char* buf, size_t tableSize); + + /*! UTIL_freeFileNamesTable(FileNamesTable* table) : * This function takes an buffered based table and frees it. * @return : void. diff --git a/programs/zstdcli.c b/programs/zstdcli.c index c6146f5f..99f344fc 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -818,17 +818,13 @@ int main(int argCount, const char* argv[]) filenameTable[filenameIdx] = NULL; // marking end of table - curTable = (FileNamesTable*) malloc(sizeof(FileNamesTable)); + curTable = UTIL_createFileNamesTable(filenameTable, tableBuf, filenameTableSize); if(!curTable) { UTIL_freeFileNamesTable(extendedTable); CLEAN_RETURN(badusage(programName)); } - curTable->fileNames = filenameTable; - curTable->tableSize = filenameTableSize; - curTable->buf = tableBuf; - concatenatedTables = UTIL_concatenateTwoTables(curTable, extendedTable); if(!concatenatedTables) { UTIL_freeFileNamesTable(curTable); From 0b3096596aabcd6de0163f7d8f99a26b10f272c1 Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 15:25:48 +0100 Subject: [PATCH 14/17] fixing AppVeyor errors --- programs/util.c | 2 +- programs/zstdcli.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/programs/util.c b/programs/util.c index 7257c8a5..21f1fe7d 100644 --- a/programs/util.c +++ b/programs/util.c @@ -247,7 +247,7 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE) return NULL; - buf = (char*) malloc(inputFileSize * sizeof(char)); + buf = (char*) malloc((size_t) inputFileSize * sizeof(char)); if(!buf) { UTIL_DISPLAYLEVEL(1, "[ERROR][UTIL_readFileNamesTableFromFile] Can't create buffer.\n"); return NULL; diff --git a/programs/zstdcli.c b/programs/zstdcli.c index 99f344fc..9470473d 100644 --- a/programs/zstdcli.c +++ b/programs/zstdcli.c @@ -836,7 +836,7 @@ int main(int argCount, const char* argv[]) filenameTableSize = concatenatedTables->tableSize; tableBuf = concatenatedTables->buf; - filenameIdx += extendedTable->tableSize; + filenameIdx += (unsigned) extendedTable->tableSize; isTableBufferBased = 1; continue; From 5f9e868ee858985445021261ea91ac39603c819e Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 16:20:58 +0100 Subject: [PATCH 15/17] fixing type conversion error --- programs/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/util.c b/programs/util.c index 21f1fe7d..1a202982 100644 --- a/programs/util.c +++ b/programs/util.c @@ -253,7 +253,7 @@ UTIL_createFileNamesTable_fromFileName(const char* inputFileName) { return NULL; } - ret_nbFiles = readFromFile(buf, inputFileSize, inputFileName); + ret_nbFiles = readFromFile(buf, (size_t) inputFileSize, inputFileName); if(ret_nbFiles <= 0) { free(buf); From 5249085e114ddad2c62130bd6c07cd5d2a0115f3 Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Thu, 24 Oct 2019 20:54:40 +0100 Subject: [PATCH 16/17] fixing free const char** filenamesTable --- programs/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/programs/util.c b/programs/util.c index 1a202982..d86b7c22 100644 --- a/programs/util.c +++ b/programs/util.c @@ -305,7 +305,7 @@ UTIL_createFileNamesTable(const char** filenames, char* buf, size_t tableSize){ void UTIL_freeFileNamesTable(FileNamesTable* table) { if(table) { if(table->fileNames) { - free(table->fileNames); + free((void*)table->fileNames); } if(table && table->buf) { From 1faeb222b2dee12fd8eb9fe24a9bb03dff495512 Mon Sep 17 00:00:00 2001 From: Ahmed Abdellah Date: Fri, 25 Oct 2019 15:54:52 +0100 Subject: [PATCH 17/17] adding some functional tests --- tests/playTests.sh | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/playTests.sh b/tests/playTests.sh index 796a5bde..a6998e2a 100755 --- a/tests/playTests.sh +++ b/tests/playTests.sh @@ -293,6 +293,53 @@ test -f tmpOutDirDecomp/tmp2 test -f tmpOutDirDecomp/tmp1 rm -rf tmp* +println "test : compress multiple files reading them from a file, --file=FILE" +mkdir tmpInputTestDir +println "Hello world!, file1" > tmpInputTestDir/file1 +println "Hello world!, file2" > tmpInputTestDir/file2 +println tmpInputTestDir/file1 > tmp +println tmpInputTestDir/file2 >> tmp +$ZSTD -f --file=tmp +test -f tmpInputTestDir/file2.zst +test -f tmpInputTestDir/file1.zst +rm tmpInputTestDir/*.zst + +println "test : compress multiple files reading them from multiple files, --file=FILE" +println "Hello world!, file3" > tmpInputTestDir/file3 +println "Hello world!, file4" > tmpInputTestDir/file4 +println tmpInputTestDir/file3 > tmp1 +println tmpInputTestDir/file4 >> tmp1 +$ZSTD -f --file=tmp --file=tmp1 +test -f tmpInputTestDir/file1.zst +test -f tmpInputTestDir/file2.zst +test -f tmpInputTestDir/file3.zst +test -f tmpInputTestDir/file4.zst + +println "test : decompress multiple files reading them from a file, --file=FILE" +rm tmpInputTestDir/file1 +rm tmpInputTestDir/file2 +println tmpInputTestDir/file1.zst > tmpZst +println tmpInputTestDir/file2.zst >> tmpZst +$ZSTD -d -f --file=tmpZst +test -f tmpInputTestDir/file2 +test -f tmpInputTestDir/file1 + +println "test : decompress multiple files reading them from multiple files, --file=FILE" +rm tmpInputTestDir/file1 +rm tmpInputTestDir/file2 +rm tmpInputTestDir/file3 +rm tmpInputTestDir/file4 +println tmpInputTestDir/file3.zst > tmpZst1 +println tmpInputTestDir/file4.zst >> tmpZst1 +$ZSTD -d -f --file=tmpZst --file=tmpZst1 +test -f tmpInputTestDir/file1 +test -f tmpInputTestDir/file2 +test -f tmpInputTestDir/file3 +test -f tmpInputTestDir/file4 + +rm -rf tmp* + + println "\n===> Advanced compression parameters " println "Hello world!" | $ZSTD --zstd=windowLog=21, - -o tmp.zst && die "wrong parameters not detected!" println "Hello world!" | $ZSTD --zstd=windowLo=21 - -o tmp.zst && die "wrong parameters not detected!"