1
0
mirror of https://github.com/facebook/zstd.git synced 2025-07-29 11:21:22 +03:00

Now constructs final destination path without allocating new table

This commit is contained in:
Sen Huang
2019-10-03 13:53:04 -04:00
parent 4dc604cab8
commit 64bc441d7d
5 changed files with 157 additions and 176 deletions

View File

@ -94,99 +94,6 @@ int UTIL_compareStr(const void *p1, const void *p2) {
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
int UTIL_checkFilenameCollisions(char** dstFilenameTable, unsigned nbFiles) {
char** dstFilenameTableSorted;
char* prevElem;
unsigned u;
dstFilenameTableSorted = (char**) malloc(sizeof(char*) * nbFiles);
if (!dstFilenameTableSorted) {
UTIL_DISPLAYLEVEL(1, "Unable to malloc new str array, not checking for name collisions\n");
return 1;
}
for (u = 0; u < nbFiles; ++u) {
dstFilenameTableSorted[u] = dstFilenameTable[u];
}
qsort(dstFilenameTableSorted, nbFiles, sizeof(char*), UTIL_compareStr);
prevElem = dstFilenameTableSorted[0];
for (u = 1; u < nbFiles; ++u) {
if (strcmp(prevElem, dstFilenameTableSorted[u]) == 0) {
UTIL_DISPLAYLEVEL(1, "WARNING: Two files have same filename as source : %s\n", prevElem);
}
prevElem = dstFilenameTableSorted[u];
}
free(dstFilenameTableSorted);
return 0;
}
void UTIL_createDestinationDirTable(char** dstFilenameTable, const char** filenameTable,
const unsigned nbFiles, const char* outDirName)
{
unsigned u;
const char* c;
#if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__) /* windows support */
c = "\\";
#else
c = "/";
#endif
for (u = 0; u < nbFiles; ++u) {
char* filename;
const char* filenameBegin;
size_t finalPathLen;
finalPathLen = strlen(outDirName);
filenameBegin = strrchr(filenameTable[u], c[0]);
if (filenameBegin == NULL) {
filename = (char*) malloc((strlen(filenameTable[u])+1) * sizeof(char));
if (!filename) {
UTIL_DISPLAYLEVEL(1, "Unable to allocate space for filename str\n");
continue;
}
strcpy(filename, filenameTable[u]);
} else {
filename = (char*) malloc((strlen(filenameBegin+1)+1) * sizeof(char));
if (!filename) {
UTIL_DISPLAYLEVEL(1, "Unable to allocate space for filename str\n");
continue;
}
strcpy(filename, filenameBegin+1);
}
finalPathLen += strlen(filename);
dstFilenameTable[u] = (char*) malloc((finalPathLen+3) * sizeof(char));
if (!dstFilenameTable[u]) {
UTIL_DISPLAYLEVEL(1, "Unable to allocate space for file destination str\n");
free(filename);
continue;
}
strcpy(dstFilenameTable[u], outDirName);
if (outDirName[strlen(outDirName)-1] == c[0]) {
strcat(dstFilenameTable[u], filename);
} else {
strcat(dstFilenameTable[u], c);
strcat(dstFilenameTable[u], filename);
}
free(filename);
}
if (UTIL_checkFilenameCollisions(dstFilenameTable, nbFiles)) {
UTIL_DISPLAYLEVEL(1, "Checking for filename collisions failed");
}
}
void UTIL_freeDestinationFilenameTable(char** dstDirTable, unsigned nbFiles) {
unsigned u;
for (u = 0; u < nbFiles; ++u) {
if (dstDirTable[u] != NULL)
free(dstDirTable[u]);
}
if (dstDirTable != NULL) free((void*)dstDirTable);
}
int UTIL_isSameFile(const char* file1, const char* file2)
{
#if defined(_MSC_VER)