2 * Copyright (c) Meta Platforms, Inc. and affiliates.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 #if defined (__cplusplus)
16 /*-****************************************
18 ******************************************/
19 #include "util.h" /* note : ensure that platform.h is included first ! */
20 #include <stdlib.h> /* malloc, realloc, free */
21 #include <stdio.h> /* fprintf */
22 #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */
26 #if defined(__FreeBSD__)
27 #include <sys/param.h> /* __FreeBSD_version */
28 #endif /* #ifdef __FreeBSD__ */
31 # include <sys/utime.h> /* utime */
32 # include <io.h> /* _chmod */
33 # define ZSTD_USE_UTIMENSAT 0
35 # include <unistd.h> /* chown, stat */
36 # include <sys/stat.h> /* utimensat, st_mtime */
37 # if (PLATFORM_POSIX_VERSION >= 200809L && defined(st_mtime)) \
38 || (defined(__FreeBSD__) && __FreeBSD_version >= 1100056)
39 # define ZSTD_USE_UTIMENSAT 1
41 # define ZSTD_USE_UTIMENSAT 0
43 # if ZSTD_USE_UTIMENSAT
44 # include <fcntl.h> /* AT_FDCWD */
46 # include <utime.h> /* utime */
50 #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__)
51 #include <direct.h> /* needed for _mkdir in windows */
54 #if defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
55 # include <dirent.h> /* opendir, readdir */
56 # include <string.h> /* strerror, memcpy */
57 #endif /* #ifdef _WIN32 */
59 /*-****************************************
61 ******************************************/
63 /* CONTROL is almost like an assert(), but is never disabled.
64 * It's designed for failures that may happen rarely,
65 * but we don't want to maintain a specific error code path for them,
66 * such as a malloc() returning NULL for example.
67 * Since it's always active, this macro can trigger side effects.
69 #define CONTROL(c) { \
71 UTIL_DISPLAYLEVEL(1, "Error : %s, %i : %s", \
72 __FILE__, __LINE__, #c); \
77 #define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__)
78 #define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
80 static int g_traceDepth = 0;
81 int g_traceFileStat = 0;
83 #define UTIL_TRACE_CALL(...) \
85 if (g_traceFileStat) { \
86 UTIL_DISPLAY("Trace:FileStat: %*s> ", g_traceDepth, ""); \
87 UTIL_DISPLAY(__VA_ARGS__); \
93 #define UTIL_TRACE_RET(ret) \
95 if (g_traceFileStat) { \
97 UTIL_DISPLAY("Trace:FileStat: %*s< %d\n", g_traceDepth, "", (ret)); \
101 /* A modified version of realloc().
102 * If UTIL_realloc() fails the original block is freed.
104 UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size)
106 void *newptr = realloc(ptr, size);
107 if (newptr) return newptr;
112 #if defined(_MSC_VER)
116 #ifndef ZSTD_HAVE_FCHMOD
117 #if PLATFORM_POSIX_VERSION >= 199309L
118 #define ZSTD_HAVE_FCHMOD
122 #ifndef ZSTD_HAVE_FCHOWN
123 #if PLATFORM_POSIX_VERSION >= 200809L
124 #define ZSTD_HAVE_FCHOWN
128 /*-****************************************
130 ******************************************/
131 int g_utilDisplayLevel;
133 int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg,
134 const char* acceptableLetters, int hasStdinInput) {
138 UTIL_DISPLAY("stdin is an input - not proceeding.\n");
142 UTIL_DISPLAY("%s", prompt);
145 if (strchr(acceptableLetters, ch) == NULL) {
146 UTIL_DISPLAY("%s \n", abortMsg);
150 while ((ch!=EOF) && (ch!='\n'))
156 /*-*************************************
158 ***************************************/
159 #define LIST_SIZE_INCREASE (8*1024)
160 #define MAX_FILE_OF_FILE_NAMES_SIZE (1<<20)*50
163 /*-*************************************
165 ***************************************/
167 void UTIL_traceFileStat(void)
172 int UTIL_fstat(const int fd, const char* filename, stat_t* statbuf)
175 UTIL_TRACE_CALL("UTIL_stat(%d, %s)", fd, filename);
176 #if defined(_MSC_VER)
178 ret = !_fstat64(fd, statbuf);
180 ret = !_stat64(filename, statbuf);
182 #elif defined(__MINGW32__) && defined (__MSVCRT__)
184 ret = !_fstati64(fd, statbuf);
186 ret = !_stati64(filename, statbuf);
190 ret = !fstat(fd, statbuf);
192 ret = !stat(filename, statbuf);
199 int UTIL_stat(const char* filename, stat_t* statbuf)
201 return UTIL_fstat(-1, filename, statbuf);
204 int UTIL_isRegularFile(const char* infilename)
208 UTIL_TRACE_CALL("UTIL_isRegularFile(%s)", infilename);
209 ret = UTIL_stat(infilename, &statbuf) && UTIL_isRegularFileStat(&statbuf);
214 int UTIL_isRegularFileStat(const stat_t* statbuf)
216 #if defined(_MSC_VER)
217 return (statbuf->st_mode & S_IFREG) != 0;
219 return S_ISREG(statbuf->st_mode) != 0;
223 /* like chmod, but avoid changing permission of /dev/null */
224 int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions)
226 return UTIL_fchmod(-1, filename, statbuf, permissions);
229 int UTIL_fchmod(const int fd, char const* filename, const stat_t* statbuf, mode_t permissions)
232 UTIL_TRACE_CALL("UTIL_chmod(%s, %#4o)", filename, (unsigned)permissions);
233 if (statbuf == NULL) {
234 if (!UTIL_fstat(fd, filename, &localStatBuf)) {
238 statbuf = &localStatBuf;
240 if (!UTIL_isRegularFileStat(statbuf)) {
242 return 0; /* pretend success, but don't change anything */
244 #ifdef ZSTD_HAVE_FCHMOD
247 UTIL_TRACE_CALL("fchmod");
248 ret = fchmod(fd, permissions);
256 UTIL_TRACE_CALL("chmod");
257 ret = chmod(filename, permissions);
264 /* set access and modification times */
265 int UTIL_utime(const char* filename, const stat_t *statbuf)
268 UTIL_TRACE_CALL("UTIL_utime(%s)", filename);
269 /* We check that st_mtime is a macro here in order to give us confidence
270 * that struct stat has a struct timespec st_mtim member. We need this
271 * check because there are some platforms that claim to be POSIX 2008
272 * compliant but which do not have st_mtim... */
273 /* FreeBSD has implemented POSIX 2008 for a long time but still only
274 * advertises support for POSIX 2001. They have a version macro that
275 * lets us safely gate them in.
276 * See https://docs.freebsd.org/en/books/porters-handbook/versions/.
278 #if ZSTD_USE_UTIMENSAT
281 struct timespec timebuf[2] = { {0, UTIME_NOW} };
282 timebuf[1] = statbuf->st_mtim;
283 ret = utimensat(AT_FDCWD, filename, timebuf, 0);
287 struct utimbuf timebuf;
288 timebuf.actime = time(NULL);
289 timebuf.modtime = statbuf->st_mtime;
290 ret = utime(filename, &timebuf);
298 int UTIL_setFileStat(const char *filename, const stat_t *statbuf)
300 return UTIL_setFDStat(-1, filename, statbuf);
303 int UTIL_setFDStat(const int fd, const char *filename, const stat_t *statbuf)
307 UTIL_TRACE_CALL("UTIL_setFileStat(%d, %s)", fd, filename);
309 if (!UTIL_fstat(fd, filename, &curStatBuf) || !UTIL_isRegularFileStat(&curStatBuf)) {
314 /* Mimic gzip's behavior:
316 * "Change the group first, then the permissions, then the owner.
317 * That way, the permissions will be correct on systems that allow
318 * users to give away files, without introducing a security hole.
319 * Security depends on permissions not containing the setuid or
323 #ifdef ZSTD_HAVE_FCHOWN
325 res += fchown(fd, -1, statbuf->st_gid); /* Apply group ownership */
329 res += chown(filename, -1, statbuf->st_gid); /* Apply group ownership */
333 res += UTIL_fchmod(fd, filename, &curStatBuf, statbuf->st_mode & 0777); /* Copy file permissions */
336 #ifdef ZSTD_HAVE_FCHOWN
338 res += fchown(fd, statbuf->st_uid, -1); /* Apply user ownership */
342 res += chown(filename, statbuf->st_uid, -1); /* Apply user ownership */
347 UTIL_TRACE_RET(-res);
348 return -res; /* number of errors is returned */
351 int UTIL_isDirectory(const char* infilename)
355 UTIL_TRACE_CALL("UTIL_isDirectory(%s)", infilename);
356 ret = UTIL_stat(infilename, &statbuf) && UTIL_isDirectoryStat(&statbuf);
361 int UTIL_isDirectoryStat(const stat_t* statbuf)
364 UTIL_TRACE_CALL("UTIL_isDirectoryStat()");
365 #if defined(_MSC_VER)
366 ret = (statbuf->st_mode & _S_IFDIR) != 0;
368 ret = S_ISDIR(statbuf->st_mode) != 0;
374 int UTIL_compareStr(const void *p1, const void *p2) {
375 return strcmp(* (char * const *) p1, * (char * const *) p2);
378 int UTIL_isSameFile(const char* fName1, const char* fName2)
381 assert(fName1 != NULL); assert(fName2 != NULL);
382 UTIL_TRACE_CALL("UTIL_isSameFile(%s, %s)", fName1, fName2);
383 #if defined(_MSC_VER) || defined(_WIN32)
384 /* note : Visual does not support file identification by inode.
385 * inode does not work on Windows, even with a posix layer, like msys2.
386 * The following work-around is limited to detecting exact name repetition only,
387 * aka `filename` is considered different from `subdir/../filename` */
388 ret = !strcmp(fName1, fName2);
392 ret = UTIL_stat(fName1, &file1Stat)
393 && UTIL_stat(fName2, &file2Stat)
394 && UTIL_isSameFileStat(fName1, fName2, &file1Stat, &file2Stat);
401 int UTIL_isSameFileStat(
402 const char* fName1, const char* fName2,
403 const stat_t* file1Stat, const stat_t* file2Stat)
406 assert(fName1 != NULL); assert(fName2 != NULL);
407 UTIL_TRACE_CALL("UTIL_isSameFileStat(%s, %s)", fName1, fName2);
408 #if defined(_MSC_VER) || defined(_WIN32)
409 /* note : Visual does not support file identification by inode.
410 * inode does not work on Windows, even with a posix layer, like msys2.
411 * The following work-around is limited to detecting exact name repetition only,
412 * aka `filename` is considered different from `subdir/../filename` */
415 ret = !strcmp(fName1, fName2);
418 ret = (file1Stat->st_dev == file2Stat->st_dev)
419 && (file1Stat->st_ino == file2Stat->st_ino);
426 /* UTIL_isFIFO : distinguish named pipes */
427 int UTIL_isFIFO(const char* infilename)
429 UTIL_TRACE_CALL("UTIL_isFIFO(%s)", infilename);
430 /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
431 #if PLATFORM_POSIX_VERSION >= 200112L
434 if (UTIL_stat(infilename, &statbuf) && UTIL_isFIFOStat(&statbuf)) {
445 /* UTIL_isFIFO : distinguish named pipes */
446 int UTIL_isFIFOStat(const stat_t* statbuf)
448 /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
449 #if PLATFORM_POSIX_VERSION >= 200112L
450 if (S_ISFIFO(statbuf->st_mode)) return 1;
456 /* UTIL_isBlockDevStat : distinguish named pipes */
457 int UTIL_isBlockDevStat(const stat_t* statbuf)
459 /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
460 #if PLATFORM_POSIX_VERSION >= 200112L
461 if (S_ISBLK(statbuf->st_mode)) return 1;
467 int UTIL_isLink(const char* infilename)
469 UTIL_TRACE_CALL("UTIL_isLink(%s)", infilename);
470 /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
471 #if PLATFORM_POSIX_VERSION >= 200112L
474 int const r = lstat(infilename, &statbuf);
475 if (!r && S_ISLNK(statbuf.st_mode)) {
486 static int g_fakeStdinIsConsole = 0;
487 static int g_fakeStderrIsConsole = 0;
488 static int g_fakeStdoutIsConsole = 0;
490 int UTIL_isConsole(FILE* file)
493 UTIL_TRACE_CALL("UTIL_isConsole(%d)", fileno(file));
494 if (file == stdin && g_fakeStdinIsConsole)
496 else if (file == stderr && g_fakeStderrIsConsole)
498 else if (file == stdout && g_fakeStdoutIsConsole)
501 ret = IS_CONSOLE(file);
506 void UTIL_fakeStdinIsConsole(void)
508 g_fakeStdinIsConsole = 1;
510 void UTIL_fakeStdoutIsConsole(void)
512 g_fakeStdoutIsConsole = 1;
514 void UTIL_fakeStderrIsConsole(void)
516 g_fakeStderrIsConsole = 1;
519 U64 UTIL_getFileSize(const char* infilename)
522 UTIL_TRACE_CALL("UTIL_getFileSize(%s)", infilename);
523 if (!UTIL_stat(infilename, &statbuf)) {
525 return UTIL_FILESIZE_UNKNOWN;
528 U64 const size = UTIL_getFileSizeStat(&statbuf);
529 UTIL_TRACE_RET((int)size);
534 U64 UTIL_getFileSizeStat(const stat_t* statbuf)
536 if (!UTIL_isRegularFileStat(statbuf)) return UTIL_FILESIZE_UNKNOWN;
537 #if defined(_MSC_VER)
538 if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
539 #elif defined(__MINGW32__) && defined (__MSVCRT__)
540 if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
542 if (!S_ISREG(statbuf->st_mode)) return UTIL_FILESIZE_UNKNOWN;
544 return (U64)statbuf->st_size;
547 UTIL_HumanReadableSize_t UTIL_makeHumanReadableSize(U64 size)
549 UTIL_HumanReadableSize_t hrs;
551 if (g_utilDisplayLevel > 3) {
552 /* In verbose mode, do not scale sizes down, except in the case of
553 * values that exceed the integral precision of a double. */
554 if (size >= (1ull << 53)) {
555 hrs.value = (double)size / (1ull << 20);
557 /* At worst, a double representation of a maximal size will be
558 * accurate to better than tens of kilobytes. */
561 hrs.value = (double)size;
566 /* In regular mode, scale sizes down and use suffixes. */
567 if (size >= (1ull << 60)) {
568 hrs.value = (double)size / (1ull << 60);
570 } else if (size >= (1ull << 50)) {
571 hrs.value = (double)size / (1ull << 50);
573 } else if (size >= (1ull << 40)) {
574 hrs.value = (double)size / (1ull << 40);
576 } else if (size >= (1ull << 30)) {
577 hrs.value = (double)size / (1ull << 30);
579 } else if (size >= (1ull << 20)) {
580 hrs.value = (double)size / (1ull << 20);
582 } else if (size >= (1ull << 10)) {
583 hrs.value = (double)size / (1ull << 10);
586 hrs.value = (double)size;
590 if (hrs.value >= 100 || (U64)hrs.value == size) {
592 } else if (hrs.value >= 10) {
594 } else if (hrs.value > 1) {
604 U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles)
608 UTIL_TRACE_CALL("UTIL_getTotalFileSize(%u)", nbFiles);
609 for (n=0; n<nbFiles; n++) {
610 U64 const size = UTIL_getFileSize(fileNamesTable[n]);
611 if (size == UTIL_FILESIZE_UNKNOWN) {
613 return UTIL_FILESIZE_UNKNOWN;
617 UTIL_TRACE_RET((int)total);
622 /* condition : @file must be valid, and not have reached its end.
623 * @return : length of line written into @buf, ended with `\0` instead of '\n',
624 * or 0, if there is no new line */
625 static size_t readLineFromFile(char* buf, size_t len, FILE* file)
628 if ( fgets(buf, (int) len, file) == NULL ) return 0;
629 { size_t linelen = strlen(buf);
630 if (strlen(buf)==0) return 0;
631 if (buf[linelen-1] == '\n') linelen--;
638 * size of @inputFileName file must be < @dstCapacity
639 * @dst must be initialized
640 * @return : nb of lines
641 * or -1 if there's an error
644 readLinesFromFile(void* dst, size_t dstCapacity,
645 const char* inputFileName)
649 char* const buf = (char*)dst;
650 FILE* const inputFile = fopen(inputFileName, "r");
655 if (g_utilDisplayLevel >= 1) perror("zstd:util:readLinesFromFile");
659 while ( !feof(inputFile) ) {
660 size_t const lineLength = readLineFromFile(buf+pos, dstCapacity-pos, inputFile);
661 if (lineLength == 0) break;
662 assert(pos + lineLength <= dstCapacity); /* '=' for inputFile not terminated with '\n' */
667 CONTROL( fclose(inputFile) == 0 );
672 /*Note: buf is not freed in case function successfully created table because filesTable->fileNames[0] = buf*/
674 UTIL_createFileNamesTable_fromFileName(const char* inputFileName)
681 if (!UTIL_stat(inputFileName, &statbuf) || !UTIL_isRegularFileStat(&statbuf))
684 { U64 const inputFileSize = UTIL_getFileSizeStat(&statbuf);
685 if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE)
687 bufSize = (size_t)(inputFileSize + 1); /* (+1) to add '\0' at the end of last filename */
690 buf = (char*) malloc(bufSize);
691 CONTROL( buf != NULL );
693 { int const ret_nbFiles = readLinesFromFile(buf, bufSize, inputFileName);
695 if (ret_nbFiles <= 0) {
699 nbFiles = (size_t)ret_nbFiles;
702 { const char** filenamesTable = (const char**) malloc(nbFiles * sizeof(*filenamesTable));
703 CONTROL(filenamesTable != NULL);
705 { size_t fnb, pos = 0;
706 for (fnb = 0; fnb < nbFiles; fnb++) {
707 filenamesTable[fnb] = buf+pos;
708 pos += strlen(buf+pos)+1; /* +1 for the finishing `\0` */
710 assert(pos <= bufSize);
713 return UTIL_assembleFileNamesTable(filenamesTable, nbFiles, buf);
717 static FileNamesTable*
718 UTIL_assembleFileNamesTable2(const char** filenames, size_t tableSize, size_t tableCapacity, char* buf)
720 FileNamesTable* const table = (FileNamesTable*) malloc(sizeof(*table));
721 CONTROL(table != NULL);
722 table->fileNames = filenames;
724 table->tableSize = tableSize;
725 table->tableCapacity = tableCapacity;
730 UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf)
732 return UTIL_assembleFileNamesTable2(filenames, tableSize, tableSize, buf);
735 void UTIL_freeFileNamesTable(FileNamesTable* table)
737 if (table==NULL) return;
738 free((void*)table->fileNames);
743 FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize)
745 const char** const fnTable = (const char**)malloc(tableSize * sizeof(*fnTable));
747 if (fnTable==NULL) return NULL;
748 fnt = UTIL_assembleFileNamesTable(fnTable, tableSize, NULL);
749 fnt->tableSize = 0; /* the table is empty */
753 int UTIL_searchFileNamesTable(FileNamesTable* table, char const* name) {
755 for(i=0 ;i < table->tableSize; i++) {
756 if(!strcmp(table->fileNames[i], name)) {
763 void UTIL_refFilename(FileNamesTable* fnt, const char* filename)
765 assert(fnt->tableSize < fnt->tableCapacity);
766 fnt->fileNames[fnt->tableSize] = filename;
770 static size_t getTotalTableSize(FileNamesTable* table)
772 size_t fnb, totalSize = 0;
773 for(fnb = 0 ; fnb < table->tableSize && table->fileNames[fnb] ; ++fnb) {
774 totalSize += strlen(table->fileNames[fnb]) + 1; /* +1 to add '\0' at the end of each fileName */
780 UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2)
782 unsigned newTableIdx = 0;
784 size_t newTotalTableSize;
787 FileNamesTable* const newTable = UTIL_assembleFileNamesTable(NULL, 0, NULL);
788 CONTROL( newTable != NULL );
790 newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2);
792 buf = (char*) calloc(newTotalTableSize, sizeof(*buf));
793 CONTROL ( buf != NULL );
796 newTable->tableSize = table1->tableSize + table2->tableSize;
797 newTable->fileNames = (const char **) calloc(newTable->tableSize, sizeof(*(newTable->fileNames)));
798 CONTROL ( newTable->fileNames != NULL );
801 for( idx1=0 ; (idx1 < table1->tableSize) && table1->fileNames[idx1] && (pos < newTotalTableSize); ++idx1, ++newTableIdx) {
802 size_t const curLen = strlen(table1->fileNames[idx1]);
803 memcpy(buf+pos, table1->fileNames[idx1], curLen);
804 assert(newTableIdx <= newTable->tableSize);
805 newTable->fileNames[newTableIdx] = buf+pos;
810 for( idx2=0 ; (idx2 < table2->tableSize) && table2->fileNames[idx2] && (pos < newTotalTableSize) ; ++idx2, ++newTableIdx) {
811 size_t const curLen = strlen(table2->fileNames[idx2]);
812 memcpy(buf+pos, table2->fileNames[idx2], curLen);
813 assert(newTableIdx < newTable->tableSize);
814 newTable->fileNames[newTableIdx] = buf+pos;
817 assert(pos <= newTotalTableSize);
818 newTable->tableSize = newTableIdx;
820 UTIL_freeFileNamesTable(table1);
821 UTIL_freeFileNamesTable(table2);
827 static int UTIL_prepareFileList(const char* dirName,
828 char** bufStart, size_t* pos,
829 char** bufEnd, int followLinks)
832 size_t dirLength, pathLength;
834 WIN32_FIND_DATAA cFile;
837 dirLength = strlen(dirName);
838 path = (char*) malloc(dirLength + 3);
841 memcpy(path, dirName, dirLength);
842 path[dirLength] = '\\';
843 path[dirLength+1] = '*';
844 path[dirLength+2] = 0;
846 hFile=FindFirstFileA(path, &cFile);
847 if (hFile == INVALID_HANDLE_VALUE) {
848 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
854 size_t const fnameLength = strlen(cFile.cFileName);
855 path = (char*) malloc(dirLength + fnameLength + 2);
856 if (!path) { FindClose(hFile); return 0; }
857 memcpy(path, dirName, dirLength);
858 path[dirLength] = '\\';
859 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
860 pathLength = dirLength+1+fnameLength;
861 path[pathLength] = 0;
862 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
863 if ( strcmp (cFile.cFileName, "..") == 0
864 || strcmp (cFile.cFileName, ".") == 0 )
866 /* Recursively call "UTIL_prepareFileList" with the new path. */
867 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);
868 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
869 } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
870 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
871 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) {
872 if (*bufStart + *pos + pathLength >= *bufEnd) {
873 ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
874 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
875 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
876 *bufEnd = *bufStart + newListSize;
878 if (*bufStart + *pos + pathLength < *bufEnd) {
879 memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */);
880 *pos += pathLength + 1;
884 } while (FindNextFileA(hFile, &cFile));
890 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
892 static int UTIL_prepareFileList(const char *dirName,
893 char** bufStart, size_t* pos,
894 char** bufEnd, int followLinks)
897 struct dirent * entry;
901 if (!(dir = opendir(dirName))) {
902 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
906 dirLength = strlen(dirName);
908 while ((entry = readdir(dir)) != NULL) {
910 size_t fnameLength, pathLength;
911 if (strcmp (entry->d_name, "..") == 0 ||
912 strcmp (entry->d_name, ".") == 0) continue;
913 fnameLength = strlen(entry->d_name);
914 path = (char*) malloc(dirLength + fnameLength + 2);
915 if (!path) { closedir(dir); return 0; }
916 memcpy(path, dirName, dirLength);
918 path[dirLength] = '/';
919 memcpy(path+dirLength+1, entry->d_name, fnameLength);
920 pathLength = dirLength+1+fnameLength;
921 path[pathLength] = 0;
923 if (!followLinks && UTIL_isLink(path)) {
924 UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
929 if (UTIL_isDirectory(path)) {
930 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
931 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
933 if (*bufStart + *pos + pathLength >= *bufEnd) {
934 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
935 assert(newListSize >= 0);
936 *bufStart = (char*)UTIL_realloc(*bufStart, (size_t)newListSize);
937 if (*bufStart != NULL) {
938 *bufEnd = *bufStart + newListSize;
940 free(path); closedir(dir); return 0;
943 if (*bufStart + *pos + pathLength < *bufEnd) {
944 memcpy(*bufStart + *pos, path, pathLength + 1); /* with final \0 */
945 *pos += pathLength + 1;
949 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
953 UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s \n", dirName, strerror(errno));
963 static int UTIL_prepareFileList(const char *dirName,
964 char** bufStart, size_t* pos,
965 char** bufEnd, int followLinks)
967 (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks;
968 UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE) \n", dirName);
972 #endif /* #ifdef _WIN32 */
974 int UTIL_isCompressedFile(const char *inputName, const char *extensionList[])
976 const char* ext = UTIL_getFileExtension(inputName);
977 while(*extensionList!=NULL)
979 const int isCompressedExtension = strcmp(ext,*extensionList);
980 if(isCompressedExtension==0)
987 /*Utility function to get file extension from file */
988 const char* UTIL_getFileExtension(const char* infilename)
990 const char* extension = strrchr(infilename, '.');
991 if(!extension || extension==infilename) return "";
995 static int pathnameHas2Dots(const char *pathname)
997 /* We need to figure out whether any ".." present in the path is a whole
998 * path token, which is the case if it is bordered on both sides by either
999 * the beginning/end of the path or by a directory separator.
1001 const char *needle = pathname;
1003 needle = strstr(needle, "..");
1005 if (needle == NULL) {
1009 if ((needle == pathname || needle[-1] == PATH_SEP)
1010 && (needle[2] == '\0' || needle[2] == PATH_SEP)) {
1014 /* increment so we search for the next match */
1020 static int isFileNameValidForMirroredOutput(const char *filename)
1022 return !pathnameHas2Dots(filename);
1026 #define DIR_DEFAULT_MODE 0755
1027 static mode_t getDirMode(const char *dirName)
1030 if (!UTIL_stat(dirName, &st)) {
1031 UTIL_DISPLAY("zstd: failed to get DIR stats %s: %s\n", dirName, strerror(errno));
1032 return DIR_DEFAULT_MODE;
1034 if (!UTIL_isDirectoryStat(&st)) {
1035 UTIL_DISPLAY("zstd: expected directory: %s\n", dirName);
1036 return DIR_DEFAULT_MODE;
1041 static int makeDir(const char *dir, mode_t mode)
1043 #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__)
1044 int ret = _mkdir(dir);
1047 int ret = mkdir(dir, mode);
1050 if (errno == EEXIST)
1052 UTIL_DISPLAY("zstd: failed to create DIR %s: %s\n", dir, strerror(errno));
1057 /* this function requires a mutable input string */
1058 static void convertPathnameToDirName(char *pathname)
1062 /* get dir name from pathname similar to 'dirname()' */
1063 assert(pathname != NULL);
1065 /* remove trailing '/' chars */
1066 len = strlen(pathname);
1068 while (pathname[len] == PATH_SEP) {
1069 pathname[len] = '\0';
1072 if (len == 0) return;
1074 /* if input is a single file, return '.' instead. i.e.
1075 * "xyz/abc/file.txt" => "xyz/abc"
1079 pos = strrchr(pathname, PATH_SEP);
1088 /* pathname must be valid */
1089 static const char* trimLeadingRootChar(const char *pathname)
1091 assert(pathname != NULL);
1092 if (pathname[0] == PATH_SEP)
1093 return pathname + 1;
1097 /* pathname must be valid */
1098 static const char* trimLeadingCurrentDirConst(const char *pathname)
1100 assert(pathname != NULL);
1101 if ((pathname[0] == '.') && (pathname[1] == PATH_SEP))
1102 return pathname + 2;
1107 trimLeadingCurrentDir(char *pathname)
1109 /* 'union charunion' can do const-cast without compiler warning */
1114 ptr.cchr = trimLeadingCurrentDirConst(pathname);
1118 /* remove leading './' or '/' chars here */
1119 static const char * trimPath(const char *pathname)
1121 return trimLeadingRootChar(
1122 trimLeadingCurrentDirConst(pathname));
1125 static char* mallocAndJoin2Dir(const char *dir1, const char *dir2)
1127 assert(dir1 != NULL && dir2 != NULL);
1128 { const size_t dir1Size = strlen(dir1);
1129 const size_t dir2Size = strlen(dir2);
1130 char *outDirBuffer, *buffer;
1132 outDirBuffer = (char *) malloc(dir1Size + dir2Size + 2);
1133 CONTROL(outDirBuffer != NULL);
1135 memcpy(outDirBuffer, dir1, dir1Size);
1136 outDirBuffer[dir1Size] = '\0';
1138 buffer = outDirBuffer + dir1Size;
1139 if (dir1Size > 0 && *(buffer - 1) != PATH_SEP) {
1143 memcpy(buffer, dir2, dir2Size);
1144 buffer[dir2Size] = '\0';
1146 return outDirBuffer;
1150 /* this function will return NULL if input srcFileName is not valid name for mirrored output path */
1151 char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName)
1153 char* pathname = NULL;
1154 if (!isFileNameValidForMirroredOutput(srcFileName))
1157 pathname = mallocAndJoin2Dir(outDirRootName, trimPath(srcFileName));
1159 convertPathnameToDirName(pathname);
1164 mirrorSrcDir(char* srcDirName, const char* outDirName)
1168 char* newDir = mallocAndJoin2Dir(outDirName, trimPath(srcDirName));
1172 srcMode = getDirMode(srcDirName);
1173 status = makeDir(newDir, srcMode);
1179 mirrorSrcDirRecursive(char* srcDirName, const char* outDirName)
1182 char* pp = trimLeadingCurrentDir(srcDirName);
1185 while ((sp = strchr(pp, PATH_SEP)) != NULL) {
1188 status = mirrorSrcDir(srcDirName, outDirName);
1195 status = mirrorSrcDir(srcDirName, outDirName);
1200 makeMirroredDestDirsWithSameSrcDirMode(char** srcDirNames, unsigned nbFile, const char* outDirName)
1203 for (i = 0; i < nbFile; i++)
1204 mirrorSrcDirRecursive(srcDirNames[i], outDirName);
1208 firstIsParentOrSameDirOfSecond(const char* firstDir, const char* secondDir)
1210 size_t firstDirLen = strlen(firstDir),
1211 secondDirLen = strlen(secondDir);
1212 return firstDirLen <= secondDirLen &&
1213 (secondDir[firstDirLen] == PATH_SEP || secondDir[firstDirLen] == '\0') &&
1214 0 == strncmp(firstDir, secondDir, firstDirLen);
1217 static int compareDir(const void* pathname1, const void* pathname2) {
1218 /* sort it after remove the leading '/' or './'*/
1219 const char* s1 = trimPath(*(char * const *) pathname1);
1220 const char* s2 = trimPath(*(char * const *) pathname2);
1221 return strcmp(s1, s2);
1225 makeUniqueMirroredDestDirs(char** srcDirNames, unsigned nbFile, const char* outDirName)
1227 unsigned int i = 0, uniqueDirNr = 0;
1228 char** uniqueDirNames = NULL;
1233 uniqueDirNames = (char** ) malloc(nbFile * sizeof (char *));
1234 CONTROL(uniqueDirNames != NULL);
1236 /* if dirs is "a/b/c" and "a/b/c/d", we only need call:
1237 * we just need "a/b/c/d" */
1238 qsort((void *)srcDirNames, nbFile, sizeof(char*), compareDir);
1241 uniqueDirNames[uniqueDirNr - 1] = srcDirNames[0];
1242 for (i = 1; i < nbFile; i++) {
1243 char* prevDirName = srcDirNames[i - 1];
1244 char* currDirName = srcDirNames[i];
1246 /* note: we always compare trimmed path, i.e.:
1247 * src dir of "./foo" and "/foo" will be both saved into:
1248 * "outDirName/foo/" */
1249 if (!firstIsParentOrSameDirOfSecond(trimPath(prevDirName),
1250 trimPath(currDirName)))
1253 /* we need to maintain original src dir name instead of trimmed
1254 * dir, so we can retrieve the original src dir's mode_t */
1255 uniqueDirNames[uniqueDirNr - 1] = currDirName;
1258 makeMirroredDestDirsWithSameSrcDirMode(uniqueDirNames, uniqueDirNr, outDirName);
1260 free(uniqueDirNames);
1264 makeMirroredDestDirs(char** srcFileNames, unsigned nbFile, const char* outDirName)
1267 for (i = 0; i < nbFile; ++i)
1268 convertPathnameToDirName(srcFileNames[i]);
1269 makeUniqueMirroredDestDirs(srcFileNames, nbFile, outDirName);
1272 void UTIL_mirrorSourceFilesDirectories(const char** inFileNames, unsigned int nbFile, const char* outDirName)
1274 unsigned int i = 0, validFilenamesNr = 0;
1275 char** srcFileNames = (char **) malloc(nbFile * sizeof (char *));
1276 CONTROL(srcFileNames != NULL);
1278 /* check input filenames is valid */
1279 for (i = 0; i < nbFile; ++i) {
1280 if (isFileNameValidForMirroredOutput(inFileNames[i])) {
1281 char* fname = STRDUP(inFileNames[i]);
1282 CONTROL(fname != NULL);
1283 srcFileNames[validFilenamesNr++] = fname;
1287 if (validFilenamesNr > 0) {
1288 makeDir(outDirName, DIR_DEFAULT_MODE);
1289 makeMirroredDestDirs(srcFileNames, validFilenamesNr, outDirName);
1292 for (i = 0; i < validFilenamesNr; i++)
1293 free(srcFileNames[i]);
1298 UTIL_createExpandedFNT(const char* const* inputNames, size_t nbIfns, int followLinks)
1301 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
1302 char* bufend = buf + LIST_SIZE_INCREASE;
1304 if (!buf) return NULL;
1306 { size_t ifnNb, pos;
1307 for (ifnNb=0, pos=0, nbFiles=0; ifnNb<nbIfns; ifnNb++) {
1308 if (!UTIL_isDirectory(inputNames[ifnNb])) {
1309 size_t const len = strlen(inputNames[ifnNb]);
1310 if (buf + pos + len >= bufend) {
1311 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
1312 assert(newListSize >= 0);
1313 buf = (char*)UTIL_realloc(buf, (size_t)newListSize);
1314 if (!buf) return NULL;
1315 bufend = buf + newListSize;
1317 if (buf + pos + len < bufend) {
1318 memcpy(buf+pos, inputNames[ifnNb], len+1); /* including final \0 */
1323 nbFiles += (unsigned)UTIL_prepareFileList(inputNames[ifnNb], &buf, &pos, &bufend, followLinks);
1324 if (buf == NULL) return NULL;
1327 /* note : even if nbFiles==0, function returns a valid, though empty, FileNamesTable* object */
1329 { size_t ifnNb, pos;
1330 size_t const fntCapacity = nbFiles + 1; /* minimum 1, allows adding one reference, typically stdin */
1331 const char** const fileNamesTable = (const char**)malloc(fntCapacity * sizeof(*fileNamesTable));
1332 if (!fileNamesTable) { free(buf); return NULL; }
1334 for (ifnNb = 0, pos = 0; ifnNb < nbFiles; ifnNb++) {
1335 fileNamesTable[ifnNb] = buf + pos;
1336 if (buf + pos > bufend) { free(buf); free((void*)fileNamesTable); return NULL; }
1337 pos += strlen(fileNamesTable[ifnNb]) + 1;
1339 return UTIL_assembleFileNamesTable2(fileNamesTable, nbFiles, fntCapacity, buf);
1344 void UTIL_expandFNT(FileNamesTable** fnt, int followLinks)
1346 FileNamesTable* const newFNT = UTIL_createExpandedFNT((*fnt)->fileNames, (*fnt)->tableSize, followLinks);
1347 CONTROL(newFNT != NULL);
1348 UTIL_freeFileNamesTable(*fnt);
1352 FileNamesTable* UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames)
1354 size_t const sizeof_FNTable = nbFilenames * sizeof(*filenames);
1355 const char** const newFNTable = (const char**)malloc(sizeof_FNTable);
1356 if (newFNTable==NULL) return NULL;
1357 memcpy((void*)newFNTable, filenames, sizeof_FNTable); /* void* : mitigate a Visual compiler bug or limitation */
1358 return UTIL_assembleFileNamesTable(newFNTable, nbFilenames, NULL);
1362 /*-****************************************
1363 * count the number of cores
1364 ******************************************/
1366 #if defined(_WIN32) || defined(WIN32)
1368 #include <windows.h>
1370 typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
1372 DWORD CountSetBits(ULONG_PTR bitMask)
1374 DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1;
1375 DWORD bitSetCount = 0;
1376 ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT;
1379 for (i = 0; i <= LSHIFT; ++i)
1381 bitSetCount += ((bitMask & bitTest)?1:0);
1388 int UTIL_countCores(int logical)
1390 static int numCores = 0;
1391 if (numCores != 0) return numCores;
1395 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
1396 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
1397 DWORD returnLength = 0;
1398 size_t byteOffset = 0;
1400 #if defined(_MSC_VER)
1401 /* Visual Studio does not like the following cast */
1402 # pragma warning( disable : 4054 ) /* conversion from function ptr to data ptr */
1403 # pragma warning( disable : 4055 ) /* conversion from data ptr to function ptr */
1405 glpi = (LPFN_GLPI)(void*)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
1406 "GetLogicalProcessorInformation");
1413 DWORD rc = glpi(buffer, &returnLength);
1415 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
1418 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
1420 if (buffer == NULL) {
1425 /* some other error */
1434 while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
1436 if (ptr->Relationship == RelationProcessorCore) {
1438 numCores += CountSetBits(ptr->ProcessorMask);
1444 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
1453 /* try to fall back on GetSystemInfo */
1454 { SYSTEM_INFO sysinfo;
1455 GetSystemInfo(&sysinfo);
1456 numCores = sysinfo.dwNumberOfProcessors;
1457 if (numCores == 0) numCores = 1; /* just in case */
1462 #elif defined(__APPLE__)
1464 #include <sys/sysctl.h>
1466 /* Use apple-provided syscall
1467 * see: man 3 sysctl */
1468 int UTIL_countCores(int logical)
1470 static S32 numCores = 0; /* apple specifies int32_t */
1471 if (numCores != 0) return numCores;
1473 { size_t size = sizeof(S32);
1474 int const ret = sysctlbyname(logical ? "hw.logicalcpu" : "hw.physicalcpu", &numCores, &size, NULL, 0);
1476 if (errno == ENOENT) {
1477 /* entry not present, fall back on 1 */
1480 perror("zstd: can't get number of cpus");
1489 #elif defined(__linux__)
1491 /* parse /proc/cpuinfo
1492 * siblings / cpu cores should give hyperthreading ratio
1493 * otherwise fall back on sysconf */
1494 int UTIL_countCores(int logical)
1496 static int numCores = 0;
1498 if (numCores != 0) return numCores;
1500 numCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1501 if (numCores == -1) {
1502 /* value not queryable, fall back on 1 */
1503 return numCores = 1;
1506 /* try to determine if there's hyperthreading */
1507 { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
1509 char buff[BUF_SIZE];
1515 if (cpuinfo == NULL) {
1516 /* fall back on the sysconf value */
1520 /* assume the cpu cores/siblings values will be constant across all
1521 * present processors */
1522 while (!feof(cpuinfo)) {
1523 if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
1524 if (strncmp(buff, "siblings", 8) == 0) {
1525 const char* const sep = strchr(buff, ':');
1526 if (sep == NULL || *sep == '\0') {
1527 /* formatting was broken? */
1531 siblings = atoi(sep + 1);
1533 if (strncmp(buff, "cpu cores", 9) == 0) {
1534 const char* const sep = strchr(buff, ':');
1535 if (sep == NULL || *sep == '\0') {
1536 /* formatting was broken? */
1540 cpu_cores = atoi(sep + 1);
1542 } else if (ferror(cpuinfo)) {
1543 /* fall back on the sysconf value */
1546 if (siblings && cpu_cores && siblings > cpu_cores) {
1547 ratio = siblings / cpu_cores;
1550 if (ratio && numCores > ratio && !logical) {
1551 numCores = numCores / ratio;
1560 #elif defined(__FreeBSD__)
1562 #include <sys/sysctl.h>
1564 /* Use physical core sysctl when available
1565 * see: man 4 smp, man 3 sysctl */
1566 int UTIL_countCores(int logical)
1568 static int numCores = 0; /* freebsd sysctl is native int sized */
1569 #if __FreeBSD_version >= 1300008
1570 static int perCore = 1;
1572 if (numCores != 0) return numCores;
1574 #if __FreeBSD_version >= 1300008
1575 { size_t size = sizeof(numCores);
1576 int ret = sysctlbyname("kern.smp.cores", &numCores, &size, NULL, 0);
1579 ret = sysctlbyname("kern.smp.threads_per_core", &perCore, &size, NULL, 0);
1580 /* default to physical cores if logical cannot be read */
1582 numCores *= perCore;
1587 if (errno != ENOENT) {
1588 perror("zstd: can't get number of cpus");
1591 /* sysctl not present, fall through to older sysconf method */
1594 /* suppress unused parameter warning */
1598 numCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1599 if (numCores == -1) {
1600 /* value not queryable, fall back on 1 */
1606 #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__CYGWIN__)
1608 /* Use POSIX sysconf
1609 * see: man 3 sysconf */
1610 int UTIL_countCores(int logical)
1612 static int numCores = 0;
1614 /* suppress unused parameter warning */
1617 if (numCores != 0) return numCores;
1619 numCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1620 if (numCores == -1) {
1621 /* value not queryable, fall back on 1 */
1622 return numCores = 1;
1629 int UTIL_countCores(int logical)
1631 /* suppress unused parameter warning */
1640 int UTIL_countPhysicalCores(void)
1642 return UTIL_countCores(0);
1645 int UTIL_countLogicalCores(void)
1647 return UTIL_countCores(1);
1650 #if defined (__cplusplus)