xref: /freebsd/sys/contrib/zstd/programs/util.c (revision 19261079b74319502c6ffa1249920079f0f69a72)
1 /*
2  * Copyright (c) 2016-2020, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
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.
9  */
10 
11 #if defined (__cplusplus)
12 extern "C" {
13 #endif
14 
15 
16 /*-****************************************
17 *  Dependencies
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 */
23 #include <errno.h>
24 #include <assert.h>
25 
26 #if defined(_WIN32)
27 #  include <sys/utime.h>  /* utime */
28 #  include <io.h>         /* _chmod */
29 #else
30 #  include <unistd.h>     /* chown, stat */
31 #  if PLATFORM_POSIX_VERSION < 200809L || !defined(st_mtime)
32 #    include <utime.h>    /* utime */
33 #  else
34 #    include <fcntl.h>    /* AT_FDCWD */
35 #    include <sys/stat.h> /* utimensat */
36 #  endif
37 #endif
38 
39 #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__)
40 #include <direct.h>     /* needed for _mkdir in windows */
41 #endif
42 
43 #if defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
44 #  include <dirent.h>       /* opendir, readdir */
45 #  include <string.h>       /* strerror, memcpy */
46 #endif /* #ifdef _WIN32 */
47 
48 /*-****************************************
49 *  Internal Macros
50 ******************************************/
51 
52 /* CONTROL is almost like an assert(), but is never disabled.
53  * It's designed for failures that may happen rarely,
54  * but we don't want to maintain a specific error code path for them,
55  * such as a malloc() returning NULL for example.
56  * Since it's always active, this macro can trigger side effects.
57  */
58 #define CONTROL(c)  {         \
59     if (!(c)) {               \
60         UTIL_DISPLAYLEVEL(1, "Error : %s, %i : %s",  \
61                           __FILE__, __LINE__, #c);   \
62         exit(1);              \
63 }   }
64 
65 /* console log */
66 #define UTIL_DISPLAY(...)         fprintf(stderr, __VA_ARGS__)
67 #define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
68 
69 /* A modified version of realloc().
70  * If UTIL_realloc() fails the original block is freed.
71  */
72 UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size)
73 {
74     void *newptr = realloc(ptr, size);
75     if (newptr) return newptr;
76     free(ptr);
77     return NULL;
78 }
79 
80 #if defined(_MSC_VER)
81     #define chmod _chmod
82 #endif
83 
84 
85 /*-****************************************
86 *  Console log
87 ******************************************/
88 int g_utilDisplayLevel;
89 
90 int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg,
91                                  const char* acceptableLetters, int hasStdinInput) {
92     int ch, result;
93 
94     if (hasStdinInput) {
95         UTIL_DISPLAY("stdin is an input - not proceeding.\n");
96         return 1;
97     }
98 
99     UTIL_DISPLAY("%s", prompt);
100     ch = getchar();
101     result = 0;
102     if (strchr(acceptableLetters, ch) == NULL) {
103         UTIL_DISPLAY("%s", abortMsg);
104         result = 1;
105     }
106     /* flush the rest */
107     while ((ch!=EOF) && (ch!='\n'))
108         ch = getchar();
109     return result;
110 }
111 
112 
113 /*-*************************************
114 *  Constants
115 ***************************************/
116 #define LIST_SIZE_INCREASE   (8*1024)
117 #define MAX_FILE_OF_FILE_NAMES_SIZE (1<<20)*50
118 
119 
120 /*-*************************************
121 *  Functions
122 ***************************************/
123 
124 int UTIL_stat(const char* filename, stat_t* statbuf)
125 {
126 #if defined(_MSC_VER)
127     return !_stat64(filename, statbuf);
128 #elif defined(__MINGW32__) && defined (__MSVCRT__)
129     return !_stati64(filename, statbuf);
130 #else
131     return !stat(filename, statbuf);
132 #endif
133 }
134 
135 int UTIL_isRegularFile(const char* infilename)
136 {
137     stat_t statbuf;
138     return UTIL_stat(infilename, &statbuf) && UTIL_isRegularFileStat(&statbuf);
139 }
140 
141 int UTIL_isRegularFileStat(const stat_t* statbuf)
142 {
143 #if defined(_MSC_VER)
144     return (statbuf->st_mode & S_IFREG) != 0;
145 #else
146     return S_ISREG(statbuf->st_mode) != 0;
147 #endif
148 }
149 
150 /* like chmod, but avoid changing permission of /dev/null */
151 int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions)
152 {
153     stat_t localStatBuf;
154     if (statbuf == NULL) {
155         if (!UTIL_stat(filename, &localStatBuf)) return 0;
156         statbuf = &localStatBuf;
157     }
158     if (!UTIL_isRegularFileStat(statbuf)) return 0; /* pretend success, but don't change anything */
159     return chmod(filename, permissions);
160 }
161 
162 int UTIL_setFileStat(const char *filename, const stat_t *statbuf)
163 {
164     int res = 0;
165 
166     stat_t curStatBuf;
167     if (!UTIL_stat(filename, &curStatBuf) || !UTIL_isRegularFileStat(&curStatBuf))
168         return -1;
169 
170     /* set access and modification times */
171     /* We check that st_mtime is a macro here in order to give us confidence
172      * that struct stat has a struct timespec st_mtim member. We need this
173      * check because there are some platforms that claim to be POSIX 2008
174      * compliant but which do not have st_mtim... */
175 #if (PLATFORM_POSIX_VERSION >= 200809L) && defined(st_mtime)
176     {
177         /* (atime, mtime) */
178         struct timespec timebuf[2] = { {0, UTIME_NOW} };
179         timebuf[1] = statbuf->st_mtim;
180         res += utimensat(AT_FDCWD, filename, timebuf, 0);
181     }
182 #else
183     {
184         struct utimbuf timebuf;
185         timebuf.actime = time(NULL);
186         timebuf.modtime = statbuf->st_mtime;
187         res += utime(filename, &timebuf);
188     }
189 #endif
190 
191 #if !defined(_WIN32)
192     res += chown(filename, statbuf->st_uid, statbuf->st_gid);  /* Copy ownership */
193 #endif
194 
195     res += UTIL_chmod(filename, &curStatBuf, statbuf->st_mode & 07777);  /* Copy file permissions */
196 
197     errno = 0;
198     return -res; /* number of errors is returned */
199 }
200 
201 int UTIL_isDirectory(const char* infilename)
202 {
203     stat_t statbuf;
204     return UTIL_stat(infilename, &statbuf) && UTIL_isDirectoryStat(&statbuf);
205 }
206 
207 int UTIL_isDirectoryStat(const stat_t* statbuf)
208 {
209 #if defined(_MSC_VER)
210     return (statbuf->st_mode & _S_IFDIR) != 0;
211 #else
212     return S_ISDIR(statbuf->st_mode) != 0;
213 #endif
214 }
215 
216 int UTIL_compareStr(const void *p1, const void *p2) {
217     return strcmp(* (char * const *) p1, * (char * const *) p2);
218 }
219 
220 int UTIL_isSameFile(const char* fName1, const char* fName2)
221 {
222     assert(fName1 != NULL); assert(fName2 != NULL);
223 #if defined(_MSC_VER) || defined(_WIN32)
224     /* note : Visual does not support file identification by inode.
225      *        inode does not work on Windows, even with a posix layer, like msys2.
226      *        The following work-around is limited to detecting exact name repetition only,
227      *        aka `filename` is considered different from `subdir/../filename` */
228     return !strcmp(fName1, fName2);
229 #else
230     {   stat_t file1Stat;
231         stat_t file2Stat;
232         return UTIL_stat(fName1, &file1Stat)
233             && UTIL_stat(fName2, &file2Stat)
234             && (file1Stat.st_dev == file2Stat.st_dev)
235             && (file1Stat.st_ino == file2Stat.st_ino);
236     }
237 #endif
238 }
239 
240 /* UTIL_isFIFO : distinguish named pipes */
241 int UTIL_isFIFO(const char* infilename)
242 {
243 /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
244 #if PLATFORM_POSIX_VERSION >= 200112L
245     stat_t statbuf;
246     if (UTIL_stat(infilename, &statbuf) && UTIL_isFIFOStat(&statbuf)) return 1;
247 #endif
248     (void)infilename;
249     return 0;
250 }
251 
252 /* UTIL_isFIFO : distinguish named pipes */
253 int UTIL_isFIFOStat(const stat_t* statbuf)
254 {
255 /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
256 #if PLATFORM_POSIX_VERSION >= 200112L
257     if (S_ISFIFO(statbuf->st_mode)) return 1;
258 #endif
259     (void)statbuf;
260     return 0;
261 }
262 
263 int UTIL_isLink(const char* infilename)
264 {
265 /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
266 #if PLATFORM_POSIX_VERSION >= 200112L
267     stat_t statbuf;
268     int const r = lstat(infilename, &statbuf);
269     if (!r && S_ISLNK(statbuf.st_mode)) return 1;
270 #endif
271     (void)infilename;
272     return 0;
273 }
274 
275 U64 UTIL_getFileSize(const char* infilename)
276 {
277     stat_t statbuf;
278     if (!UTIL_stat(infilename, &statbuf)) return UTIL_FILESIZE_UNKNOWN;
279     return UTIL_getFileSizeStat(&statbuf);
280 }
281 
282 U64 UTIL_getFileSizeStat(const stat_t* statbuf)
283 {
284     if (!UTIL_isRegularFileStat(statbuf)) return UTIL_FILESIZE_UNKNOWN;
285 #if defined(_MSC_VER)
286     if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
287 #elif defined(__MINGW32__) && defined (__MSVCRT__)
288     if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
289 #else
290     if (!S_ISREG(statbuf->st_mode)) return UTIL_FILESIZE_UNKNOWN;
291 #endif
292     return (U64)statbuf->st_size;
293 }
294 
295 
296 U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles)
297 {
298     U64 total = 0;
299     unsigned n;
300     for (n=0; n<nbFiles; n++) {
301         U64 const size = UTIL_getFileSize(fileNamesTable[n]);
302         if (size == UTIL_FILESIZE_UNKNOWN) return UTIL_FILESIZE_UNKNOWN;
303         total += size;
304     }
305     return total;
306 }
307 
308 
309 /* condition : @file must be valid, and not have reached its end.
310  * @return : length of line written into @buf, ended with `\0` instead of '\n',
311  *           or 0, if there is no new line */
312 static size_t readLineFromFile(char* buf, size_t len, FILE* file)
313 {
314     assert(!feof(file));
315     /* Work around Cygwin problem when len == 1 it returns NULL. */
316     if (len <= 1) return 0;
317     CONTROL( fgets(buf, (int) len, file) );
318     {   size_t linelen = strlen(buf);
319         if (strlen(buf)==0) return 0;
320         if (buf[linelen-1] == '\n') linelen--;
321         buf[linelen] = '\0';
322         return linelen+1;
323     }
324 }
325 
326 /* Conditions :
327  *   size of @inputFileName file must be < @dstCapacity
328  *   @dst must be initialized
329  * @return : nb of lines
330  *       or -1 if there's an error
331  */
332 static int
333 readLinesFromFile(void* dst, size_t dstCapacity,
334             const char* inputFileName)
335 {
336     int nbFiles = 0;
337     size_t pos = 0;
338     char* const buf = (char*)dst;
339     FILE* const inputFile = fopen(inputFileName, "r");
340 
341     assert(dst != NULL);
342 
343     if(!inputFile) {
344         if (g_utilDisplayLevel >= 1) perror("zstd:util:readLinesFromFile");
345         return -1;
346     }
347 
348     while ( !feof(inputFile) ) {
349         size_t const lineLength = readLineFromFile(buf+pos, dstCapacity-pos, inputFile);
350         if (lineLength == 0) break;
351         assert(pos + lineLength < dstCapacity);
352         pos += lineLength;
353         ++nbFiles;
354     }
355 
356     CONTROL( fclose(inputFile) == 0 );
357 
358     return nbFiles;
359 }
360 
361 /*Note: buf is not freed in case function successfully created table because filesTable->fileNames[0] = buf*/
362 FileNamesTable*
363 UTIL_createFileNamesTable_fromFileName(const char* inputFileName)
364 {
365     size_t nbFiles = 0;
366     char* buf;
367     size_t bufSize;
368     size_t pos = 0;
369     stat_t statbuf;
370 
371     if (!UTIL_stat(inputFileName, &statbuf) || !UTIL_isRegularFileStat(&statbuf))
372         return NULL;
373 
374     {   U64 const inputFileSize = UTIL_getFileSizeStat(&statbuf);
375         if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE)
376             return NULL;
377         bufSize = (size_t)(inputFileSize + 1); /* (+1) to add '\0' at the end of last filename */
378     }
379 
380     buf = (char*) malloc(bufSize);
381     CONTROL( buf != NULL );
382 
383     {   int const ret_nbFiles = readLinesFromFile(buf, bufSize, inputFileName);
384 
385         if (ret_nbFiles <= 0) {
386           free(buf);
387           return NULL;
388         }
389         nbFiles = (size_t)ret_nbFiles;
390     }
391 
392     {   const char** filenamesTable = (const char**) malloc(nbFiles * sizeof(*filenamesTable));
393         CONTROL(filenamesTable != NULL);
394 
395         {   size_t fnb;
396             for (fnb = 0, pos = 0; fnb < nbFiles; fnb++) {
397                 filenamesTable[fnb] = buf+pos;
398                 pos += strlen(buf+pos)+1;  /* +1 for the finishing `\0` */
399         }   }
400         assert(pos <= bufSize);
401 
402         return UTIL_assembleFileNamesTable(filenamesTable, nbFiles, buf);
403     }
404 }
405 
406 static FileNamesTable*
407 UTIL_assembleFileNamesTable2(const char** filenames, size_t tableSize, size_t tableCapacity, char* buf)
408 {
409     FileNamesTable* const table = (FileNamesTable*) malloc(sizeof(*table));
410     CONTROL(table != NULL);
411     table->fileNames = filenames;
412     table->buf = buf;
413     table->tableSize = tableSize;
414     table->tableCapacity = tableCapacity;
415     return table;
416 }
417 
418 FileNamesTable*
419 UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf)
420 {
421     return UTIL_assembleFileNamesTable2(filenames, tableSize, tableSize, buf);
422 }
423 
424 void UTIL_freeFileNamesTable(FileNamesTable* table)
425 {
426     if (table==NULL) return;
427     free((void*)table->fileNames);
428     free(table->buf);
429     free(table);
430 }
431 
432 FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize)
433 {
434     const char** const fnTable = (const char**)malloc(tableSize * sizeof(*fnTable));
435     FileNamesTable* fnt;
436     if (fnTable==NULL) return NULL;
437     fnt = UTIL_assembleFileNamesTable(fnTable, tableSize, NULL);
438     fnt->tableSize = 0;   /* the table is empty */
439     return fnt;
440 }
441 
442 void UTIL_refFilename(FileNamesTable* fnt, const char* filename)
443 {
444     assert(fnt->tableSize < fnt->tableCapacity);
445     fnt->fileNames[fnt->tableSize] = filename;
446     fnt->tableSize++;
447 }
448 
449 static size_t getTotalTableSize(FileNamesTable* table)
450 {
451     size_t fnb = 0, totalSize = 0;
452     for(fnb = 0 ; fnb < table->tableSize && table->fileNames[fnb] ; ++fnb) {
453         totalSize += strlen(table->fileNames[fnb]) + 1; /* +1 to add '\0' at the end of each fileName */
454     }
455     return totalSize;
456 }
457 
458 FileNamesTable*
459 UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2)
460 {
461     unsigned newTableIdx = 0;
462     size_t pos = 0;
463     size_t newTotalTableSize;
464     char* buf;
465 
466     FileNamesTable* const newTable = UTIL_assembleFileNamesTable(NULL, 0, NULL);
467     CONTROL( newTable != NULL );
468 
469     newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2);
470 
471     buf = (char*) calloc(newTotalTableSize, sizeof(*buf));
472     CONTROL ( buf != NULL );
473 
474     newTable->buf = buf;
475     newTable->tableSize = table1->tableSize + table2->tableSize;
476     newTable->fileNames = (const char **) calloc(newTable->tableSize, sizeof(*(newTable->fileNames)));
477     CONTROL ( newTable->fileNames != NULL );
478 
479     {   unsigned idx1;
480         for( idx1=0 ; (idx1 < table1->tableSize) && table1->fileNames[idx1] && (pos < newTotalTableSize); ++idx1, ++newTableIdx) {
481             size_t const curLen = strlen(table1->fileNames[idx1]);
482             memcpy(buf+pos, table1->fileNames[idx1], curLen);
483             assert(newTableIdx <= newTable->tableSize);
484             newTable->fileNames[newTableIdx] = buf+pos;
485             pos += curLen+1;
486     }   }
487 
488     {   unsigned idx2;
489         for( idx2=0 ; (idx2 < table2->tableSize) && table2->fileNames[idx2] && (pos < newTotalTableSize) ; ++idx2, ++newTableIdx) {
490             size_t const curLen = strlen(table2->fileNames[idx2]);
491             memcpy(buf+pos, table2->fileNames[idx2], curLen);
492             assert(newTableIdx <= newTable->tableSize);
493             newTable->fileNames[newTableIdx] = buf+pos;
494             pos += curLen+1;
495     }   }
496     assert(pos <= newTotalTableSize);
497     newTable->tableSize = newTableIdx;
498 
499     UTIL_freeFileNamesTable(table1);
500     UTIL_freeFileNamesTable(table2);
501 
502     return newTable;
503 }
504 
505 #ifdef _WIN32
506 static int UTIL_prepareFileList(const char* dirName,
507                                 char** bufStart, size_t* pos,
508                                 char** bufEnd, int followLinks)
509 {
510     char* path;
511     size_t dirLength, pathLength;
512     int nbFiles = 0;
513     WIN32_FIND_DATAA cFile;
514     HANDLE hFile;
515 
516     dirLength = strlen(dirName);
517     path = (char*) malloc(dirLength + 3);
518     if (!path) return 0;
519 
520     memcpy(path, dirName, dirLength);
521     path[dirLength] = '\\';
522     path[dirLength+1] = '*';
523     path[dirLength+2] = 0;
524 
525     hFile=FindFirstFileA(path, &cFile);
526     if (hFile == INVALID_HANDLE_VALUE) {
527         UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
528         return 0;
529     }
530     free(path);
531 
532     do {
533         size_t const fnameLength = strlen(cFile.cFileName);
534         path = (char*) malloc(dirLength + fnameLength + 2);
535         if (!path) { FindClose(hFile); return 0; }
536         memcpy(path, dirName, dirLength);
537         path[dirLength] = '\\';
538         memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
539         pathLength = dirLength+1+fnameLength;
540         path[pathLength] = 0;
541         if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
542             if ( strcmp (cFile.cFileName, "..") == 0
543               || strcmp (cFile.cFileName, ".") == 0 )
544                 continue;
545             /* Recursively call "UTIL_prepareFileList" with the new path. */
546             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);
547             if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
548         } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
549                  || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
550                  || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) {
551             if (*bufStart + *pos + pathLength >= *bufEnd) {
552                 ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
553                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
554                 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
555                 *bufEnd = *bufStart + newListSize;
556             }
557             if (*bufStart + *pos + pathLength < *bufEnd) {
558                 memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */);
559                 *pos += pathLength + 1;
560                 nbFiles++;
561         }   }
562         free(path);
563     } while (FindNextFileA(hFile, &cFile));
564 
565     FindClose(hFile);
566     return nbFiles;
567 }
568 
569 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
570 
571 static int UTIL_prepareFileList(const char *dirName,
572                                 char** bufStart, size_t* pos,
573                                 char** bufEnd, int followLinks)
574 {
575     DIR* dir;
576     struct dirent * entry;
577     size_t dirLength;
578     int nbFiles = 0;
579 
580     if (!(dir = opendir(dirName))) {
581         UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
582         return 0;
583     }
584 
585     dirLength = strlen(dirName);
586     errno = 0;
587     while ((entry = readdir(dir)) != NULL) {
588         char* path;
589         size_t fnameLength, pathLength;
590         if (strcmp (entry->d_name, "..") == 0 ||
591             strcmp (entry->d_name, ".") == 0) continue;
592         fnameLength = strlen(entry->d_name);
593         path = (char*) malloc(dirLength + fnameLength + 2);
594         if (!path) { closedir(dir); return 0; }
595         memcpy(path, dirName, dirLength);
596 
597         path[dirLength] = '/';
598         memcpy(path+dirLength+1, entry->d_name, fnameLength);
599         pathLength = dirLength+1+fnameLength;
600         path[pathLength] = 0;
601 
602         if (!followLinks && UTIL_isLink(path)) {
603             UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
604             free(path);
605             continue;
606         }
607 
608         if (UTIL_isDirectory(path)) {
609             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);  /* Recursively call "UTIL_prepareFileList" with the new path. */
610             if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
611         } else {
612             if (*bufStart + *pos + pathLength >= *bufEnd) {
613                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
614                 assert(newListSize >= 0);
615                 *bufStart = (char*)UTIL_realloc(*bufStart, (size_t)newListSize);
616                 *bufEnd = *bufStart + newListSize;
617                 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
618             }
619             if (*bufStart + *pos + pathLength < *bufEnd) {
620                 memcpy(*bufStart + *pos, path, pathLength + 1);  /* with final \0 */
621                 *pos += pathLength + 1;
622                 nbFiles++;
623         }   }
624         free(path);
625         errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
626     }
627 
628     if (errno != 0) {
629         UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s \n", dirName, strerror(errno));
630         free(*bufStart);
631         *bufStart = NULL;
632     }
633     closedir(dir);
634     return nbFiles;
635 }
636 
637 #else
638 
639 static int UTIL_prepareFileList(const char *dirName,
640                                 char** bufStart, size_t* pos,
641                                 char** bufEnd, int followLinks)
642 {
643     (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks;
644     UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE) \n", dirName);
645     return 0;
646 }
647 
648 #endif /* #ifdef _WIN32 */
649 
650 int UTIL_isCompressedFile(const char *inputName, const char *extensionList[])
651 {
652   const char* ext = UTIL_getFileExtension(inputName);
653   while(*extensionList!=NULL)
654   {
655     const int isCompressedExtension = strcmp(ext,*extensionList);
656     if(isCompressedExtension==0)
657       return 1;
658     ++extensionList;
659   }
660    return 0;
661 }
662 
663 /*Utility function to get file extension from file */
664 const char* UTIL_getFileExtension(const char* infilename)
665 {
666    const char* extension = strrchr(infilename, '.');
667    if(!extension || extension==infilename) return "";
668    return extension;
669 }
670 
671 static int pathnameHas2Dots(const char *pathname)
672 {
673     return NULL != strstr(pathname, "..");
674 }
675 
676 static int isFileNameValidForMirroredOutput(const char *filename)
677 {
678     return !pathnameHas2Dots(filename);
679 }
680 
681 
682 #define DIR_DEFAULT_MODE 0755
683 static mode_t getDirMode(const char *dirName)
684 {
685     stat_t st;
686     if (!UTIL_stat(dirName, &st)) {
687         UTIL_DISPLAY("zstd: failed to get DIR stats %s: %s\n", dirName, strerror(errno));
688         return DIR_DEFAULT_MODE;
689     }
690     if (!UTIL_isDirectoryStat(&st)) {
691         UTIL_DISPLAY("zstd: expected directory: %s\n", dirName);
692         return DIR_DEFAULT_MODE;
693     }
694     return st.st_mode;
695 }
696 
697 static int makeDir(const char *dir, mode_t mode)
698 {
699 #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__)
700     int ret = _mkdir(dir);
701     (void) mode;
702 #else
703     int ret = mkdir(dir, mode);
704 #endif
705     if (ret != 0) {
706         if (errno == EEXIST)
707             return 0;
708         UTIL_DISPLAY("zstd: failed to create DIR %s: %s\n", dir, strerror(errno));
709     }
710     return ret;
711 }
712 
713 /* this function requires a mutable input string */
714 static void convertPathnameToDirName(char *pathname)
715 {
716     size_t len = 0;
717     char* pos = NULL;
718     /* get dir name from pathname similar to 'dirname()' */
719     assert(pathname != NULL);
720 
721     /* remove trailing '/' chars */
722     len = strlen(pathname);
723     assert(len > 0);
724     while (pathname[len] == PATH_SEP) {
725         pathname[len] = '\0';
726         len--;
727     }
728     if (len == 0) return;
729 
730     /* if input is a single file, return '.' instead. i.e.
731      * "xyz/abc/file.txt" => "xyz/abc"
732        "./file.txt"       => "."
733        "file.txt"         => "."
734      */
735     pos = strrchr(pathname, PATH_SEP);
736     if (pos == NULL) {
737         pathname[0] = '.';
738         pathname[1] = '\0';
739     } else {
740         *pos = '\0';
741     }
742 }
743 
744 /* pathname must be valid */
745 static const char* trimLeadingRootChar(const char *pathname)
746 {
747     assert(pathname != NULL);
748     if (pathname[0] == PATH_SEP)
749         return pathname + 1;
750     return pathname;
751 }
752 
753 /* pathname must be valid */
754 static const char* trimLeadingCurrentDirConst(const char *pathname)
755 {
756     assert(pathname != NULL);
757     if ((pathname[0] == '.') && (pathname[1] == PATH_SEP))
758         return pathname + 2;
759     return pathname;
760 }
761 
762 static char*
763 trimLeadingCurrentDir(char *pathname)
764 {
765     /* 'union charunion' can do const-cast without compiler warning */
766     union charunion {
767         char *chr;
768         const char* cchr;
769     } ptr;
770     ptr.cchr = trimLeadingCurrentDirConst(pathname);
771     return ptr.chr;
772 }
773 
774 /* remove leading './' or '/' chars here */
775 static const char * trimPath(const char *pathname)
776 {
777     return trimLeadingRootChar(
778             trimLeadingCurrentDirConst(pathname));
779 }
780 
781 static char* mallocAndJoin2Dir(const char *dir1, const char *dir2)
782 {
783     const size_t dir1Size = strlen(dir1);
784     const size_t dir2Size = strlen(dir2);
785     char *outDirBuffer, *buffer, trailingChar;
786 
787     assert(dir1 != NULL && dir2 != NULL);
788     outDirBuffer = (char *) malloc(dir1Size + dir2Size + 2);
789     CONTROL(outDirBuffer != NULL);
790 
791     memcpy(outDirBuffer, dir1, dir1Size);
792     outDirBuffer[dir1Size] = '\0';
793 
794     if (dir2[0] == '.')
795         return outDirBuffer;
796 
797     buffer = outDirBuffer + dir1Size;
798     trailingChar = *(buffer - 1);
799     if (trailingChar != PATH_SEP) {
800         *buffer = PATH_SEP;
801         buffer++;
802     }
803     memcpy(buffer, dir2, dir2Size);
804     buffer[dir2Size] = '\0';
805 
806     return outDirBuffer;
807 }
808 
809 /* this function will return NULL if input srcFileName is not valid name for mirrored output path */
810 char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName)
811 {
812     char* pathname = NULL;
813     if (!isFileNameValidForMirroredOutput(srcFileName))
814         return NULL;
815 
816     pathname = mallocAndJoin2Dir(outDirRootName, trimPath(srcFileName));
817 
818     convertPathnameToDirName(pathname);
819     return pathname;
820 }
821 
822 static int
823 mirrorSrcDir(char* srcDirName, const char* outDirName)
824 {
825     mode_t srcMode;
826     int status = 0;
827     char* newDir = mallocAndJoin2Dir(outDirName, trimPath(srcDirName));
828     if (!newDir)
829         return -ENOMEM;
830 
831     srcMode = getDirMode(srcDirName);
832     status = makeDir(newDir, srcMode);
833     free(newDir);
834     return status;
835 }
836 
837 static int
838 mirrorSrcDirRecursive(char* srcDirName, const char* outDirName)
839 {
840     int status = 0;
841     char* pp = trimLeadingCurrentDir(srcDirName);
842     char* sp = NULL;
843 
844     while ((sp = strchr(pp, PATH_SEP)) != NULL) {
845         if (sp != pp) {
846             *sp = '\0';
847             status = mirrorSrcDir(srcDirName, outDirName);
848             if (status != 0)
849                 return status;
850             *sp = PATH_SEP;
851         }
852         pp = sp + 1;
853     }
854     status = mirrorSrcDir(srcDirName, outDirName);
855     return status;
856 }
857 
858 static void
859 makeMirroredDestDirsWithSameSrcDirMode(char** srcDirNames, unsigned nbFile, const char* outDirName)
860 {
861     unsigned int i = 0;
862     for (i = 0; i < nbFile; i++)
863         mirrorSrcDirRecursive(srcDirNames[i], outDirName);
864 }
865 
866 static int
867 firstIsParentOrSameDirOfSecond(const char* firstDir, const char* secondDir)
868 {
869     size_t firstDirLen  = strlen(firstDir),
870            secondDirLen = strlen(secondDir);
871     return firstDirLen <= secondDirLen &&
872            (secondDir[firstDirLen] == PATH_SEP || secondDir[firstDirLen] == '\0') &&
873            0 == strncmp(firstDir, secondDir, firstDirLen);
874 }
875 
876 static int compareDir(const void* pathname1, const void* pathname2) {
877     /* sort it after remove the leading '/'  or './'*/
878     const char* s1 = trimPath(*(char * const *) pathname1);
879     const char* s2 = trimPath(*(char * const *) pathname2);
880     return strcmp(s1, s2);
881 }
882 
883 static void
884 makeUniqueMirroredDestDirs(char** srcDirNames, unsigned nbFile, const char* outDirName)
885 {
886     unsigned int i = 0, uniqueDirNr = 0;
887     char** uniqueDirNames = NULL;
888 
889     if (nbFile == 0)
890         return;
891 
892     uniqueDirNames = (char** ) malloc(nbFile * sizeof (char *));
893     CONTROL(uniqueDirNames != NULL);
894 
895     /* if dirs is "a/b/c" and "a/b/c/d", we only need call:
896      * we just need "a/b/c/d" */
897     qsort((void *)srcDirNames, nbFile, sizeof(char*), compareDir);
898 
899     uniqueDirNr = 1;
900     uniqueDirNames[uniqueDirNr - 1] = srcDirNames[0];
901     for (i = 1; i < nbFile; i++) {
902         char* prevDirName = srcDirNames[i - 1];
903         char* currDirName = srcDirNames[i];
904 
905         /* note: we alwasy compare trimmed path, i.e.:
906          * src dir of "./foo" and "/foo" will be both saved into:
907          * "outDirName/foo/" */
908         if (!firstIsParentOrSameDirOfSecond(trimPath(prevDirName),
909                                             trimPath(currDirName)))
910             uniqueDirNr++;
911 
912         /* we need maintain original src dir name instead of trimmed
913          * dir, so we can retrive the original src dir's mode_t */
914         uniqueDirNames[uniqueDirNr - 1] = currDirName;
915     }
916 
917     makeMirroredDestDirsWithSameSrcDirMode(uniqueDirNames, uniqueDirNr, outDirName);
918 
919     free(uniqueDirNames);
920 }
921 
922 static void
923 makeMirroredDestDirs(char** srcFileNames, unsigned nbFile, const char* outDirName)
924 {
925     unsigned int i = 0;
926     for (i = 0; i < nbFile; ++i)
927         convertPathnameToDirName(srcFileNames[i]);
928     makeUniqueMirroredDestDirs(srcFileNames, nbFile, outDirName);
929 }
930 
931 void UTIL_mirrorSourceFilesDirectories(const char** inFileNames, unsigned int nbFile, const char* outDirName)
932 {
933     unsigned int i = 0, validFilenamesNr = 0;
934     char** srcFileNames = (char **) malloc(nbFile * sizeof (char *));
935     CONTROL(srcFileNames != NULL);
936 
937     /* check input filenames is valid */
938     for (i = 0; i < nbFile; ++i) {
939         if (isFileNameValidForMirroredOutput(inFileNames[i])) {
940             char* fname = STRDUP(inFileNames[i]);
941             CONTROL(fname != NULL);
942             srcFileNames[validFilenamesNr++] = fname;
943         }
944     }
945 
946     if (validFilenamesNr > 0) {
947         makeDir(outDirName, DIR_DEFAULT_MODE);
948         makeMirroredDestDirs(srcFileNames, validFilenamesNr, outDirName);
949     }
950 
951     for (i = 0; i < validFilenamesNr; i++)
952         free(srcFileNames[i]);
953     free(srcFileNames);
954 }
955 
956 FileNamesTable*
957 UTIL_createExpandedFNT(const char** inputNames, size_t nbIfns, int followLinks)
958 {
959     unsigned nbFiles;
960     char* buf = (char*)malloc(LIST_SIZE_INCREASE);
961     char* bufend = buf + LIST_SIZE_INCREASE;
962 
963     if (!buf) return NULL;
964 
965     {   size_t ifnNb, pos;
966         for (ifnNb=0, pos=0, nbFiles=0; ifnNb<nbIfns; ifnNb++) {
967             if (!UTIL_isDirectory(inputNames[ifnNb])) {
968                 size_t const len = strlen(inputNames[ifnNb]);
969                 if (buf + pos + len >= bufend) {
970                     ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
971                     assert(newListSize >= 0);
972                     buf = (char*)UTIL_realloc(buf, (size_t)newListSize);
973                     if (!buf) return NULL;
974                     bufend = buf + newListSize;
975                 }
976                 if (buf + pos + len < bufend) {
977                     memcpy(buf+pos, inputNames[ifnNb], len+1);  /* including final \0 */
978                     pos += len + 1;
979                     nbFiles++;
980                 }
981             } else {
982                 nbFiles += (unsigned)UTIL_prepareFileList(inputNames[ifnNb], &buf, &pos, &bufend, followLinks);
983                 if (buf == NULL) return NULL;
984     }   }   }
985 
986     /* note : even if nbFiles==0, function returns a valid, though empty, FileNamesTable* object */
987 
988     {   size_t ifnNb, pos;
989         size_t const fntCapacity = nbFiles + 1;  /* minimum 1, allows adding one reference, typically stdin */
990         const char** const fileNamesTable = (const char**)malloc(fntCapacity * sizeof(*fileNamesTable));
991         if (!fileNamesTable) { free(buf); return NULL; }
992 
993         for (ifnNb = 0, pos = 0; ifnNb < nbFiles; ifnNb++) {
994             fileNamesTable[ifnNb] = buf + pos;
995             if (buf + pos > bufend) { free(buf); free((void*)fileNamesTable); return NULL; }
996             pos += strlen(fileNamesTable[ifnNb]) + 1;
997         }
998         return UTIL_assembleFileNamesTable2(fileNamesTable, nbFiles, fntCapacity, buf);
999     }
1000 }
1001 
1002 
1003 void UTIL_expandFNT(FileNamesTable** fnt, int followLinks)
1004 {
1005     FileNamesTable* const newFNT = UTIL_createExpandedFNT((*fnt)->fileNames, (*fnt)->tableSize, followLinks);
1006     CONTROL(newFNT != NULL);
1007     UTIL_freeFileNamesTable(*fnt);
1008     *fnt = newFNT;
1009 }
1010 
1011 FileNamesTable* UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames)
1012 {
1013     size_t const sizeof_FNTable = nbFilenames * sizeof(*filenames);
1014     const char** const newFNTable = (const char**)malloc(sizeof_FNTable);
1015     if (newFNTable==NULL) return NULL;
1016     memcpy((void*)newFNTable, filenames, sizeof_FNTable);  /* void* : mitigate a Visual compiler bug or limitation */
1017     return UTIL_assembleFileNamesTable(newFNTable, nbFilenames, NULL);
1018 }
1019 
1020 
1021 /*-****************************************
1022 *  count the number of physical cores
1023 ******************************************/
1024 
1025 #if defined(_WIN32) || defined(WIN32)
1026 
1027 #include <windows.h>
1028 
1029 typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
1030 
1031 int UTIL_countPhysicalCores(void)
1032 {
1033     static int numPhysicalCores = 0;
1034     if (numPhysicalCores != 0) return numPhysicalCores;
1035 
1036     {   LPFN_GLPI glpi;
1037         BOOL done = FALSE;
1038         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
1039         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
1040         DWORD returnLength = 0;
1041         size_t byteOffset = 0;
1042 
1043 #if defined(_MSC_VER)
1044 /* Visual Studio does not like the following cast */
1045 #   pragma warning( disable : 4054 )  /* conversion from function ptr to data ptr */
1046 #   pragma warning( disable : 4055 )  /* conversion from data ptr to function ptr */
1047 #endif
1048         glpi = (LPFN_GLPI)(void*)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
1049                                                "GetLogicalProcessorInformation");
1050 
1051         if (glpi == NULL) {
1052             goto failed;
1053         }
1054 
1055         while(!done) {
1056             DWORD rc = glpi(buffer, &returnLength);
1057             if (FALSE == rc) {
1058                 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
1059                     if (buffer)
1060                         free(buffer);
1061                     buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
1062 
1063                     if (buffer == NULL) {
1064                         perror("zstd");
1065                         exit(1);
1066                     }
1067                 } else {
1068                     /* some other error */
1069                     goto failed;
1070                 }
1071             } else {
1072                 done = TRUE;
1073         }   }
1074 
1075         ptr = buffer;
1076 
1077         while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
1078 
1079             if (ptr->Relationship == RelationProcessorCore) {
1080                 numPhysicalCores++;
1081             }
1082 
1083             ptr++;
1084             byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
1085         }
1086 
1087         free(buffer);
1088 
1089         return numPhysicalCores;
1090     }
1091 
1092 failed:
1093     /* try to fall back on GetSystemInfo */
1094     {   SYSTEM_INFO sysinfo;
1095         GetSystemInfo(&sysinfo);
1096         numPhysicalCores = sysinfo.dwNumberOfProcessors;
1097         if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
1098     }
1099     return numPhysicalCores;
1100 }
1101 
1102 #elif defined(__APPLE__)
1103 
1104 #include <sys/sysctl.h>
1105 
1106 /* Use apple-provided syscall
1107  * see: man 3 sysctl */
1108 int UTIL_countPhysicalCores(void)
1109 {
1110     static S32 numPhysicalCores = 0; /* apple specifies int32_t */
1111     if (numPhysicalCores != 0) return numPhysicalCores;
1112 
1113     {   size_t size = sizeof(S32);
1114         int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
1115         if (ret != 0) {
1116             if (errno == ENOENT) {
1117                 /* entry not present, fall back on 1 */
1118                 numPhysicalCores = 1;
1119             } else {
1120                 perror("zstd: can't get number of physical cpus");
1121                 exit(1);
1122             }
1123         }
1124 
1125         return numPhysicalCores;
1126     }
1127 }
1128 
1129 #elif defined(__linux__)
1130 
1131 /* parse /proc/cpuinfo
1132  * siblings / cpu cores should give hyperthreading ratio
1133  * otherwise fall back on sysconf */
1134 int UTIL_countPhysicalCores(void)
1135 {
1136     static int numPhysicalCores = 0;
1137 
1138     if (numPhysicalCores != 0) return numPhysicalCores;
1139 
1140     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1141     if (numPhysicalCores == -1) {
1142         /* value not queryable, fall back on 1 */
1143         return numPhysicalCores = 1;
1144     }
1145 
1146     /* try to determine if there's hyperthreading */
1147     {   FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
1148 #define BUF_SIZE 80
1149         char buff[BUF_SIZE];
1150 
1151         int siblings = 0;
1152         int cpu_cores = 0;
1153         int ratio = 1;
1154 
1155         if (cpuinfo == NULL) {
1156             /* fall back on the sysconf value */
1157             return numPhysicalCores;
1158         }
1159 
1160         /* assume the cpu cores/siblings values will be constant across all
1161          * present processors */
1162         while (!feof(cpuinfo)) {
1163             if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
1164                 if (strncmp(buff, "siblings", 8) == 0) {
1165                     const char* const sep = strchr(buff, ':');
1166                     if (sep == NULL || *sep == '\0') {
1167                         /* formatting was broken? */
1168                         goto failed;
1169                     }
1170 
1171                     siblings = atoi(sep + 1);
1172                 }
1173                 if (strncmp(buff, "cpu cores", 9) == 0) {
1174                     const char* const sep = strchr(buff, ':');
1175                     if (sep == NULL || *sep == '\0') {
1176                         /* formatting was broken? */
1177                         goto failed;
1178                     }
1179 
1180                     cpu_cores = atoi(sep + 1);
1181                 }
1182             } else if (ferror(cpuinfo)) {
1183                 /* fall back on the sysconf value */
1184                 goto failed;
1185         }   }
1186         if (siblings && cpu_cores) {
1187             ratio = siblings / cpu_cores;
1188         }
1189 failed:
1190         fclose(cpuinfo);
1191         return numPhysicalCores = numPhysicalCores / ratio;
1192     }
1193 }
1194 
1195 #elif defined(__FreeBSD__)
1196 
1197 #include <sys/param.h>
1198 #include <sys/sysctl.h>
1199 
1200 /* Use physical core sysctl when available
1201  * see: man 4 smp, man 3 sysctl */
1202 int UTIL_countPhysicalCores(void)
1203 {
1204     static int numPhysicalCores = 0; /* freebsd sysctl is native int sized */
1205     if (numPhysicalCores != 0) return numPhysicalCores;
1206 
1207 #if __FreeBSD_version >= 1300008
1208     {   size_t size = sizeof(numPhysicalCores);
1209         int ret = sysctlbyname("kern.smp.cores", &numPhysicalCores, &size, NULL, 0);
1210         if (ret == 0) return numPhysicalCores;
1211         if (errno != ENOENT) {
1212             perror("zstd: can't get number of physical cpus");
1213             exit(1);
1214         }
1215         /* sysctl not present, fall through to older sysconf method */
1216     }
1217 #endif
1218 
1219     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1220     if (numPhysicalCores == -1) {
1221         /* value not queryable, fall back on 1 */
1222         numPhysicalCores = 1;
1223     }
1224     return numPhysicalCores;
1225 }
1226 
1227 #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__CYGWIN__)
1228 
1229 /* Use POSIX sysconf
1230  * see: man 3 sysconf */
1231 int UTIL_countPhysicalCores(void)
1232 {
1233     static int numPhysicalCores = 0;
1234 
1235     if (numPhysicalCores != 0) return numPhysicalCores;
1236 
1237     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1238     if (numPhysicalCores == -1) {
1239         /* value not queryable, fall back on 1 */
1240         return numPhysicalCores = 1;
1241     }
1242     return numPhysicalCores;
1243 }
1244 
1245 #else
1246 
1247 int UTIL_countPhysicalCores(void)
1248 {
1249     /* assume 1 */
1250     return 1;
1251 }
1252 
1253 #endif
1254 
1255 #if defined (__cplusplus)
1256 }
1257 #endif
1258