1 /*
2  * Copyright (c) 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  */
UTIL_realloc(void * ptr,size_t size)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 
UTIL_requireUserConfirmation(const char * prompt,const char * abortMsg,const char * acceptableLetters,int hasStdinInput)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 
UTIL_stat(const char * filename,stat_t * statbuf)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 
UTIL_isRegularFile(const char * infilename)135 int UTIL_isRegularFile(const char* infilename)
136 {
137     stat_t statbuf;
138     return UTIL_stat(infilename, &statbuf) && UTIL_isRegularFileStat(&statbuf);
139 }
140 
UTIL_isRegularFileStat(const stat_t * statbuf)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 */
UTIL_chmod(char const * filename,const stat_t * statbuf,mode_t permissions)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 
UTIL_setFileStat(const char * filename,const stat_t * statbuf)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 
UTIL_isDirectory(const char * infilename)201 int UTIL_isDirectory(const char* infilename)
202 {
203     stat_t statbuf;
204     return UTIL_stat(infilename, &statbuf) && UTIL_isDirectoryStat(&statbuf);
205 }
206 
UTIL_isDirectoryStat(const stat_t * statbuf)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 
UTIL_compareStr(const void * p1,const void * p2)216 int UTIL_compareStr(const void *p1, const void *p2) {
217     return strcmp(* (char * const *) p1, * (char * const *) p2);
218 }
219 
UTIL_isSameFile(const char * fName1,const char * fName2)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 */
UTIL_isFIFO(const char * infilename)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 */
UTIL_isFIFOStat(const stat_t * statbuf)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 /* UTIL_isBlockDevStat : distinguish named pipes */
UTIL_isBlockDevStat(const stat_t * statbuf)264 int UTIL_isBlockDevStat(const stat_t* statbuf)
265 {
266 /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
267 #if PLATFORM_POSIX_VERSION >= 200112L
268     if (S_ISBLK(statbuf->st_mode)) return 1;
269 #endif
270     (void)statbuf;
271     return 0;
272 }
273 
UTIL_isLink(const char * infilename)274 int UTIL_isLink(const char* infilename)
275 {
276 /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
277 #if PLATFORM_POSIX_VERSION >= 200112L
278     stat_t statbuf;
279     int const r = lstat(infilename, &statbuf);
280     if (!r && S_ISLNK(statbuf.st_mode)) return 1;
281 #endif
282     (void)infilename;
283     return 0;
284 }
285 
UTIL_getFileSize(const char * infilename)286 U64 UTIL_getFileSize(const char* infilename)
287 {
288     stat_t statbuf;
289     if (!UTIL_stat(infilename, &statbuf)) return UTIL_FILESIZE_UNKNOWN;
290     return UTIL_getFileSizeStat(&statbuf);
291 }
292 
UTIL_getFileSizeStat(const stat_t * statbuf)293 U64 UTIL_getFileSizeStat(const stat_t* statbuf)
294 {
295     if (!UTIL_isRegularFileStat(statbuf)) return UTIL_FILESIZE_UNKNOWN;
296 #if defined(_MSC_VER)
297     if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
298 #elif defined(__MINGW32__) && defined (__MSVCRT__)
299     if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
300 #else
301     if (!S_ISREG(statbuf->st_mode)) return UTIL_FILESIZE_UNKNOWN;
302 #endif
303     return (U64)statbuf->st_size;
304 }
305 
306 
UTIL_getTotalFileSize(const char * const * fileNamesTable,unsigned nbFiles)307 U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles)
308 {
309     U64 total = 0;
310     unsigned n;
311     for (n=0; n<nbFiles; n++) {
312         U64 const size = UTIL_getFileSize(fileNamesTable[n]);
313         if (size == UTIL_FILESIZE_UNKNOWN) return UTIL_FILESIZE_UNKNOWN;
314         total += size;
315     }
316     return total;
317 }
318 
319 
320 /* condition : @file must be valid, and not have reached its end.
321  * @return : length of line written into @buf, ended with `\0` instead of '\n',
322  *           or 0, if there is no new line */
readLineFromFile(char * buf,size_t len,FILE * file)323 static size_t readLineFromFile(char* buf, size_t len, FILE* file)
324 {
325     assert(!feof(file));
326     if ( fgets(buf, (int) len, file) == NULL ) return 0;
327     {   size_t linelen = strlen(buf);
328         if (strlen(buf)==0) return 0;
329         if (buf[linelen-1] == '\n') linelen--;
330         buf[linelen] = '\0';
331         return linelen+1;
332     }
333 }
334 
335 /* Conditions :
336  *   size of @inputFileName file must be < @dstCapacity
337  *   @dst must be initialized
338  * @return : nb of lines
339  *       or -1 if there's an error
340  */
341 static int
readLinesFromFile(void * dst,size_t dstCapacity,const char * inputFileName)342 readLinesFromFile(void* dst, size_t dstCapacity,
343             const char* inputFileName)
344 {
345     int nbFiles = 0;
346     size_t pos = 0;
347     char* const buf = (char*)dst;
348     FILE* const inputFile = fopen(inputFileName, "r");
349 
350     assert(dst != NULL);
351 
352     if(!inputFile) {
353         if (g_utilDisplayLevel >= 1) perror("zstd:util:readLinesFromFile");
354         return -1;
355     }
356 
357     while ( !feof(inputFile) ) {
358         size_t const lineLength = readLineFromFile(buf+pos, dstCapacity-pos, inputFile);
359         if (lineLength == 0) break;
360         assert(pos + lineLength < dstCapacity);
361         pos += lineLength;
362         ++nbFiles;
363     }
364 
365     CONTROL( fclose(inputFile) == 0 );
366 
367     return nbFiles;
368 }
369 
370 /*Note: buf is not freed in case function successfully created table because filesTable->fileNames[0] = buf*/
371 FileNamesTable*
UTIL_createFileNamesTable_fromFileName(const char * inputFileName)372 UTIL_createFileNamesTable_fromFileName(const char* inputFileName)
373 {
374     size_t nbFiles = 0;
375     char* buf;
376     size_t bufSize;
377     size_t pos = 0;
378     stat_t statbuf;
379 
380     if (!UTIL_stat(inputFileName, &statbuf) || !UTIL_isRegularFileStat(&statbuf))
381         return NULL;
382 
383     {   U64 const inputFileSize = UTIL_getFileSizeStat(&statbuf);
384         if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE)
385             return NULL;
386         bufSize = (size_t)(inputFileSize + 1); /* (+1) to add '\0' at the end of last filename */
387     }
388 
389     buf = (char*) malloc(bufSize);
390     CONTROL( buf != NULL );
391 
392     {   int const ret_nbFiles = readLinesFromFile(buf, bufSize, inputFileName);
393 
394         if (ret_nbFiles <= 0) {
395           free(buf);
396           return NULL;
397         }
398         nbFiles = (size_t)ret_nbFiles;
399     }
400 
401     {   const char** filenamesTable = (const char**) malloc(nbFiles * sizeof(*filenamesTable));
402         CONTROL(filenamesTable != NULL);
403 
404         {   size_t fnb;
405             for (fnb = 0, pos = 0; fnb < nbFiles; fnb++) {
406                 filenamesTable[fnb] = buf+pos;
407                 pos += strlen(buf+pos)+1;  /* +1 for the finishing `\0` */
408         }   }
409         assert(pos <= bufSize);
410 
411         return UTIL_assembleFileNamesTable(filenamesTable, nbFiles, buf);
412     }
413 }
414 
415 static FileNamesTable*
UTIL_assembleFileNamesTable2(const char ** filenames,size_t tableSize,size_t tableCapacity,char * buf)416 UTIL_assembleFileNamesTable2(const char** filenames, size_t tableSize, size_t tableCapacity, char* buf)
417 {
418     FileNamesTable* const table = (FileNamesTable*) malloc(sizeof(*table));
419     CONTROL(table != NULL);
420     table->fileNames = filenames;
421     table->buf = buf;
422     table->tableSize = tableSize;
423     table->tableCapacity = tableCapacity;
424     return table;
425 }
426 
427 FileNamesTable*
UTIL_assembleFileNamesTable(const char ** filenames,size_t tableSize,char * buf)428 UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf)
429 {
430     return UTIL_assembleFileNamesTable2(filenames, tableSize, tableSize, buf);
431 }
432 
UTIL_freeFileNamesTable(FileNamesTable * table)433 void UTIL_freeFileNamesTable(FileNamesTable* table)
434 {
435     if (table==NULL) return;
436     free((void*)table->fileNames);
437     free(table->buf);
438     free(table);
439 }
440 
UTIL_allocateFileNamesTable(size_t tableSize)441 FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize)
442 {
443     const char** const fnTable = (const char**)malloc(tableSize * sizeof(*fnTable));
444     FileNamesTable* fnt;
445     if (fnTable==NULL) return NULL;
446     fnt = UTIL_assembleFileNamesTable(fnTable, tableSize, NULL);
447     fnt->tableSize = 0;   /* the table is empty */
448     return fnt;
449 }
450 
UTIL_refFilename(FileNamesTable * fnt,const char * filename)451 void UTIL_refFilename(FileNamesTable* fnt, const char* filename)
452 {
453     assert(fnt->tableSize < fnt->tableCapacity);
454     fnt->fileNames[fnt->tableSize] = filename;
455     fnt->tableSize++;
456 }
457 
getTotalTableSize(FileNamesTable * table)458 static size_t getTotalTableSize(FileNamesTable* table)
459 {
460     size_t fnb = 0, totalSize = 0;
461     for(fnb = 0 ; fnb < table->tableSize && table->fileNames[fnb] ; ++fnb) {
462         totalSize += strlen(table->fileNames[fnb]) + 1; /* +1 to add '\0' at the end of each fileName */
463     }
464     return totalSize;
465 }
466 
467 FileNamesTable*
UTIL_mergeFileNamesTable(FileNamesTable * table1,FileNamesTable * table2)468 UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2)
469 {
470     unsigned newTableIdx = 0;
471     size_t pos = 0;
472     size_t newTotalTableSize;
473     char* buf;
474 
475     FileNamesTable* const newTable = UTIL_assembleFileNamesTable(NULL, 0, NULL);
476     CONTROL( newTable != NULL );
477 
478     newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2);
479 
480     buf = (char*) calloc(newTotalTableSize, sizeof(*buf));
481     CONTROL ( buf != NULL );
482 
483     newTable->buf = buf;
484     newTable->tableSize = table1->tableSize + table2->tableSize;
485     newTable->fileNames = (const char **) calloc(newTable->tableSize, sizeof(*(newTable->fileNames)));
486     CONTROL ( newTable->fileNames != NULL );
487 
488     {   unsigned idx1;
489         for( idx1=0 ; (idx1 < table1->tableSize) && table1->fileNames[idx1] && (pos < newTotalTableSize); ++idx1, ++newTableIdx) {
490             size_t const curLen = strlen(table1->fileNames[idx1]);
491             memcpy(buf+pos, table1->fileNames[idx1], curLen);
492             assert(newTableIdx <= newTable->tableSize);
493             newTable->fileNames[newTableIdx] = buf+pos;
494             pos += curLen+1;
495     }   }
496 
497     {   unsigned idx2;
498         for( idx2=0 ; (idx2 < table2->tableSize) && table2->fileNames[idx2] && (pos < newTotalTableSize) ; ++idx2, ++newTableIdx) {
499             size_t const curLen = strlen(table2->fileNames[idx2]);
500             memcpy(buf+pos, table2->fileNames[idx2], curLen);
501             assert(newTableIdx <= newTable->tableSize);
502             newTable->fileNames[newTableIdx] = buf+pos;
503             pos += curLen+1;
504     }   }
505     assert(pos <= newTotalTableSize);
506     newTable->tableSize = newTableIdx;
507 
508     UTIL_freeFileNamesTable(table1);
509     UTIL_freeFileNamesTable(table2);
510 
511     return newTable;
512 }
513 
514 #ifdef _WIN32
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd,int followLinks)515 static int UTIL_prepareFileList(const char* dirName,
516                                 char** bufStart, size_t* pos,
517                                 char** bufEnd, int followLinks)
518 {
519     char* path;
520     size_t dirLength, pathLength;
521     int nbFiles = 0;
522     WIN32_FIND_DATAA cFile;
523     HANDLE hFile;
524 
525     dirLength = strlen(dirName);
526     path = (char*) malloc(dirLength + 3);
527     if (!path) return 0;
528 
529     memcpy(path, dirName, dirLength);
530     path[dirLength] = '\\';
531     path[dirLength+1] = '*';
532     path[dirLength+2] = 0;
533 
534     hFile=FindFirstFileA(path, &cFile);
535     if (hFile == INVALID_HANDLE_VALUE) {
536         UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
537         return 0;
538     }
539     free(path);
540 
541     do {
542         size_t const fnameLength = strlen(cFile.cFileName);
543         path = (char*) malloc(dirLength + fnameLength + 2);
544         if (!path) { FindClose(hFile); return 0; }
545         memcpy(path, dirName, dirLength);
546         path[dirLength] = '\\';
547         memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
548         pathLength = dirLength+1+fnameLength;
549         path[pathLength] = 0;
550         if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
551             if ( strcmp (cFile.cFileName, "..") == 0
552               || strcmp (cFile.cFileName, ".") == 0 )
553                 continue;
554             /* Recursively call "UTIL_prepareFileList" with the new path. */
555             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);
556             if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
557         } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
558                  || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
559                  || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) {
560             if (*bufStart + *pos + pathLength >= *bufEnd) {
561                 ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
562                 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
563                 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
564                 *bufEnd = *bufStart + newListSize;
565             }
566             if (*bufStart + *pos + pathLength < *bufEnd) {
567                 memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */);
568                 *pos += pathLength + 1;
569                 nbFiles++;
570         }   }
571         free(path);
572     } while (FindNextFileA(hFile, &cFile));
573 
574     FindClose(hFile);
575     return nbFiles;
576 }
577 
578 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
579 
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd,int followLinks)580 static int UTIL_prepareFileList(const char *dirName,
581                                 char** bufStart, size_t* pos,
582                                 char** bufEnd, int followLinks)
583 {
584     DIR* dir;
585     struct dirent * entry;
586     size_t dirLength;
587     int nbFiles = 0;
588 
589     if (!(dir = opendir(dirName))) {
590         UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
591         return 0;
592     }
593 
594     dirLength = strlen(dirName);
595     errno = 0;
596     while ((entry = readdir(dir)) != NULL) {
597         char* path;
598         size_t fnameLength, pathLength;
599         if (strcmp (entry->d_name, "..") == 0 ||
600             strcmp (entry->d_name, ".") == 0) continue;
601         fnameLength = strlen(entry->d_name);
602         path = (char*) malloc(dirLength + fnameLength + 2);
603         if (!path) { closedir(dir); return 0; }
604         memcpy(path, dirName, dirLength);
605 
606         path[dirLength] = '/';
607         memcpy(path+dirLength+1, entry->d_name, fnameLength);
608         pathLength = dirLength+1+fnameLength;
609         path[pathLength] = 0;
610 
611         if (!followLinks && UTIL_isLink(path)) {
612             UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
613             free(path);
614             continue;
615         }
616 
617         if (UTIL_isDirectory(path)) {
618             nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);  /* Recursively call "UTIL_prepareFileList" with the new path. */
619             if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
620         } else {
621             if (*bufStart + *pos + pathLength >= *bufEnd) {
622                 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
623                 assert(newListSize >= 0);
624                 *bufStart = (char*)UTIL_realloc(*bufStart, (size_t)newListSize);
625                 *bufEnd = *bufStart + newListSize;
626                 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
627             }
628             if (*bufStart + *pos + pathLength < *bufEnd) {
629                 memcpy(*bufStart + *pos, path, pathLength + 1);  /* with final \0 */
630                 *pos += pathLength + 1;
631                 nbFiles++;
632         }   }
633         free(path);
634         errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
635     }
636 
637     if (errno != 0) {
638         UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s \n", dirName, strerror(errno));
639         free(*bufStart);
640         *bufStart = NULL;
641     }
642     closedir(dir);
643     return nbFiles;
644 }
645 
646 #else
647 
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd,int followLinks)648 static int UTIL_prepareFileList(const char *dirName,
649                                 char** bufStart, size_t* pos,
650                                 char** bufEnd, int followLinks)
651 {
652     (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks;
653     UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE) \n", dirName);
654     return 0;
655 }
656 
657 #endif /* #ifdef _WIN32 */
658 
UTIL_isCompressedFile(const char * inputName,const char * extensionList[])659 int UTIL_isCompressedFile(const char *inputName, const char *extensionList[])
660 {
661   const char* ext = UTIL_getFileExtension(inputName);
662   while(*extensionList!=NULL)
663   {
664     const int isCompressedExtension = strcmp(ext,*extensionList);
665     if(isCompressedExtension==0)
666       return 1;
667     ++extensionList;
668   }
669    return 0;
670 }
671 
672 /*Utility function to get file extension from file */
UTIL_getFileExtension(const char * infilename)673 const char* UTIL_getFileExtension(const char* infilename)
674 {
675    const char* extension = strrchr(infilename, '.');
676    if(!extension || extension==infilename) return "";
677    return extension;
678 }
679 
pathnameHas2Dots(const char * pathname)680 static int pathnameHas2Dots(const char *pathname)
681 {
682     /* We need to figure out whether any ".." present in the path is a whole
683      * path token, which is the case if it is bordered on both sides by either
684      * the beginning/end of the path or by a directory separator.
685      */
686     const char *needle = pathname;
687     while (1) {
688         needle = strstr(needle, "..");
689 
690         if (needle == NULL) {
691             return 0;
692         }
693 
694         if ((needle == pathname || needle[-1] == PATH_SEP)
695          && (needle[2] == '\0' || needle[2] == PATH_SEP)) {
696             return 1;
697         }
698 
699         /* increment so we search for the next match */
700         needle++;
701     };
702     return 0;
703 }
704 
isFileNameValidForMirroredOutput(const char * filename)705 static int isFileNameValidForMirroredOutput(const char *filename)
706 {
707     return !pathnameHas2Dots(filename);
708 }
709 
710 
711 #define DIR_DEFAULT_MODE 0755
getDirMode(const char * dirName)712 static mode_t getDirMode(const char *dirName)
713 {
714     stat_t st;
715     if (!UTIL_stat(dirName, &st)) {
716         UTIL_DISPLAY("zstd: failed to get DIR stats %s: %s\n", dirName, strerror(errno));
717         return DIR_DEFAULT_MODE;
718     }
719     if (!UTIL_isDirectoryStat(&st)) {
720         UTIL_DISPLAY("zstd: expected directory: %s\n", dirName);
721         return DIR_DEFAULT_MODE;
722     }
723     return st.st_mode;
724 }
725 
makeDir(const char * dir,mode_t mode)726 static int makeDir(const char *dir, mode_t mode)
727 {
728 #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__)
729     int ret = _mkdir(dir);
730     (void) mode;
731 #else
732     int ret = mkdir(dir, mode);
733 #endif
734     if (ret != 0) {
735         if (errno == EEXIST)
736             return 0;
737         UTIL_DISPLAY("zstd: failed to create DIR %s: %s\n", dir, strerror(errno));
738     }
739     return ret;
740 }
741 
742 /* this function requires a mutable input string */
convertPathnameToDirName(char * pathname)743 static void convertPathnameToDirName(char *pathname)
744 {
745     size_t len = 0;
746     char* pos = NULL;
747     /* get dir name from pathname similar to 'dirname()' */
748     assert(pathname != NULL);
749 
750     /* remove trailing '/' chars */
751     len = strlen(pathname);
752     assert(len > 0);
753     while (pathname[len] == PATH_SEP) {
754         pathname[len] = '\0';
755         len--;
756     }
757     if (len == 0) return;
758 
759     /* if input is a single file, return '.' instead. i.e.
760      * "xyz/abc/file.txt" => "xyz/abc"
761        "./file.txt"       => "."
762        "file.txt"         => "."
763      */
764     pos = strrchr(pathname, PATH_SEP);
765     if (pos == NULL) {
766         pathname[0] = '.';
767         pathname[1] = '\0';
768     } else {
769         *pos = '\0';
770     }
771 }
772 
773 /* pathname must be valid */
trimLeadingRootChar(const char * pathname)774 static const char* trimLeadingRootChar(const char *pathname)
775 {
776     assert(pathname != NULL);
777     if (pathname[0] == PATH_SEP)
778         return pathname + 1;
779     return pathname;
780 }
781 
782 /* pathname must be valid */
trimLeadingCurrentDirConst(const char * pathname)783 static const char* trimLeadingCurrentDirConst(const char *pathname)
784 {
785     assert(pathname != NULL);
786     if ((pathname[0] == '.') && (pathname[1] == PATH_SEP))
787         return pathname + 2;
788     return pathname;
789 }
790 
791 static char*
trimLeadingCurrentDir(char * pathname)792 trimLeadingCurrentDir(char *pathname)
793 {
794     /* 'union charunion' can do const-cast without compiler warning */
795     union charunion {
796         char *chr;
797         const char* cchr;
798     } ptr;
799     ptr.cchr = trimLeadingCurrentDirConst(pathname);
800     return ptr.chr;
801 }
802 
803 /* remove leading './' or '/' chars here */
trimPath(const char * pathname)804 static const char * trimPath(const char *pathname)
805 {
806     return trimLeadingRootChar(
807             trimLeadingCurrentDirConst(pathname));
808 }
809 
mallocAndJoin2Dir(const char * dir1,const char * dir2)810 static char* mallocAndJoin2Dir(const char *dir1, const char *dir2)
811 {
812     const size_t dir1Size = strlen(dir1);
813     const size_t dir2Size = strlen(dir2);
814     char *outDirBuffer, *buffer, trailingChar;
815 
816     assert(dir1 != NULL && dir2 != NULL);
817     outDirBuffer = (char *) malloc(dir1Size + dir2Size + 2);
818     CONTROL(outDirBuffer != NULL);
819 
820     memcpy(outDirBuffer, dir1, dir1Size);
821     outDirBuffer[dir1Size] = '\0';
822 
823     if (dir2[0] == '.')
824         return outDirBuffer;
825 
826     buffer = outDirBuffer + dir1Size;
827     trailingChar = *(buffer - 1);
828     if (trailingChar != PATH_SEP) {
829         *buffer = PATH_SEP;
830         buffer++;
831     }
832     memcpy(buffer, dir2, dir2Size);
833     buffer[dir2Size] = '\0';
834 
835     return outDirBuffer;
836 }
837 
838 /* this function will return NULL if input srcFileName is not valid name for mirrored output path */
UTIL_createMirroredDestDirName(const char * srcFileName,const char * outDirRootName)839 char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName)
840 {
841     char* pathname = NULL;
842     if (!isFileNameValidForMirroredOutput(srcFileName))
843         return NULL;
844 
845     pathname = mallocAndJoin2Dir(outDirRootName, trimPath(srcFileName));
846 
847     convertPathnameToDirName(pathname);
848     return pathname;
849 }
850 
851 static int
mirrorSrcDir(char * srcDirName,const char * outDirName)852 mirrorSrcDir(char* srcDirName, const char* outDirName)
853 {
854     mode_t srcMode;
855     int status = 0;
856     char* newDir = mallocAndJoin2Dir(outDirName, trimPath(srcDirName));
857     if (!newDir)
858         return -ENOMEM;
859 
860     srcMode = getDirMode(srcDirName);
861     status = makeDir(newDir, srcMode);
862     free(newDir);
863     return status;
864 }
865 
866 static int
mirrorSrcDirRecursive(char * srcDirName,const char * outDirName)867 mirrorSrcDirRecursive(char* srcDirName, const char* outDirName)
868 {
869     int status = 0;
870     char* pp = trimLeadingCurrentDir(srcDirName);
871     char* sp = NULL;
872 
873     while ((sp = strchr(pp, PATH_SEP)) != NULL) {
874         if (sp != pp) {
875             *sp = '\0';
876             status = mirrorSrcDir(srcDirName, outDirName);
877             if (status != 0)
878                 return status;
879             *sp = PATH_SEP;
880         }
881         pp = sp + 1;
882     }
883     status = mirrorSrcDir(srcDirName, outDirName);
884     return status;
885 }
886 
887 static void
makeMirroredDestDirsWithSameSrcDirMode(char ** srcDirNames,unsigned nbFile,const char * outDirName)888 makeMirroredDestDirsWithSameSrcDirMode(char** srcDirNames, unsigned nbFile, const char* outDirName)
889 {
890     unsigned int i = 0;
891     for (i = 0; i < nbFile; i++)
892         mirrorSrcDirRecursive(srcDirNames[i], outDirName);
893 }
894 
895 static int
firstIsParentOrSameDirOfSecond(const char * firstDir,const char * secondDir)896 firstIsParentOrSameDirOfSecond(const char* firstDir, const char* secondDir)
897 {
898     size_t firstDirLen  = strlen(firstDir),
899            secondDirLen = strlen(secondDir);
900     return firstDirLen <= secondDirLen &&
901            (secondDir[firstDirLen] == PATH_SEP || secondDir[firstDirLen] == '\0') &&
902            0 == strncmp(firstDir, secondDir, firstDirLen);
903 }
904 
compareDir(const void * pathname1,const void * pathname2)905 static int compareDir(const void* pathname1, const void* pathname2) {
906     /* sort it after remove the leading '/'  or './'*/
907     const char* s1 = trimPath(*(char * const *) pathname1);
908     const char* s2 = trimPath(*(char * const *) pathname2);
909     return strcmp(s1, s2);
910 }
911 
912 static void
makeUniqueMirroredDestDirs(char ** srcDirNames,unsigned nbFile,const char * outDirName)913 makeUniqueMirroredDestDirs(char** srcDirNames, unsigned nbFile, const char* outDirName)
914 {
915     unsigned int i = 0, uniqueDirNr = 0;
916     char** uniqueDirNames = NULL;
917 
918     if (nbFile == 0)
919         return;
920 
921     uniqueDirNames = (char** ) malloc(nbFile * sizeof (char *));
922     CONTROL(uniqueDirNames != NULL);
923 
924     /* if dirs is "a/b/c" and "a/b/c/d", we only need call:
925      * we just need "a/b/c/d" */
926     qsort((void *)srcDirNames, nbFile, sizeof(char*), compareDir);
927 
928     uniqueDirNr = 1;
929     uniqueDirNames[uniqueDirNr - 1] = srcDirNames[0];
930     for (i = 1; i < nbFile; i++) {
931         char* prevDirName = srcDirNames[i - 1];
932         char* currDirName = srcDirNames[i];
933 
934         /* note: we alwasy compare trimmed path, i.e.:
935          * src dir of "./foo" and "/foo" will be both saved into:
936          * "outDirName/foo/" */
937         if (!firstIsParentOrSameDirOfSecond(trimPath(prevDirName),
938                                             trimPath(currDirName)))
939             uniqueDirNr++;
940 
941         /* we need maintain original src dir name instead of trimmed
942          * dir, so we can retrive the original src dir's mode_t */
943         uniqueDirNames[uniqueDirNr - 1] = currDirName;
944     }
945 
946     makeMirroredDestDirsWithSameSrcDirMode(uniqueDirNames, uniqueDirNr, outDirName);
947 
948     free(uniqueDirNames);
949 }
950 
951 static void
makeMirroredDestDirs(char ** srcFileNames,unsigned nbFile,const char * outDirName)952 makeMirroredDestDirs(char** srcFileNames, unsigned nbFile, const char* outDirName)
953 {
954     unsigned int i = 0;
955     for (i = 0; i < nbFile; ++i)
956         convertPathnameToDirName(srcFileNames[i]);
957     makeUniqueMirroredDestDirs(srcFileNames, nbFile, outDirName);
958 }
959 
UTIL_mirrorSourceFilesDirectories(const char ** inFileNames,unsigned int nbFile,const char * outDirName)960 void UTIL_mirrorSourceFilesDirectories(const char** inFileNames, unsigned int nbFile, const char* outDirName)
961 {
962     unsigned int i = 0, validFilenamesNr = 0;
963     char** srcFileNames = (char **) malloc(nbFile * sizeof (char *));
964     CONTROL(srcFileNames != NULL);
965 
966     /* check input filenames is valid */
967     for (i = 0; i < nbFile; ++i) {
968         if (isFileNameValidForMirroredOutput(inFileNames[i])) {
969             char* fname = STRDUP(inFileNames[i]);
970             CONTROL(fname != NULL);
971             srcFileNames[validFilenamesNr++] = fname;
972         }
973     }
974 
975     if (validFilenamesNr > 0) {
976         makeDir(outDirName, DIR_DEFAULT_MODE);
977         makeMirroredDestDirs(srcFileNames, validFilenamesNr, outDirName);
978     }
979 
980     for (i = 0; i < validFilenamesNr; i++)
981         free(srcFileNames[i]);
982     free(srcFileNames);
983 }
984 
985 FileNamesTable*
UTIL_createExpandedFNT(const char * const * inputNames,size_t nbIfns,int followLinks)986 UTIL_createExpandedFNT(const char* const* inputNames, size_t nbIfns, int followLinks)
987 {
988     unsigned nbFiles;
989     char* buf = (char*)malloc(LIST_SIZE_INCREASE);
990     char* bufend = buf + LIST_SIZE_INCREASE;
991 
992     if (!buf) return NULL;
993 
994     {   size_t ifnNb, pos;
995         for (ifnNb=0, pos=0, nbFiles=0; ifnNb<nbIfns; ifnNb++) {
996             if (!UTIL_isDirectory(inputNames[ifnNb])) {
997                 size_t const len = strlen(inputNames[ifnNb]);
998                 if (buf + pos + len >= bufend) {
999                     ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
1000                     assert(newListSize >= 0);
1001                     buf = (char*)UTIL_realloc(buf, (size_t)newListSize);
1002                     if (!buf) return NULL;
1003                     bufend = buf + newListSize;
1004                 }
1005                 if (buf + pos + len < bufend) {
1006                     memcpy(buf+pos, inputNames[ifnNb], len+1);  /* including final \0 */
1007                     pos += len + 1;
1008                     nbFiles++;
1009                 }
1010             } else {
1011                 nbFiles += (unsigned)UTIL_prepareFileList(inputNames[ifnNb], &buf, &pos, &bufend, followLinks);
1012                 if (buf == NULL) return NULL;
1013     }   }   }
1014 
1015     /* note : even if nbFiles==0, function returns a valid, though empty, FileNamesTable* object */
1016 
1017     {   size_t ifnNb, pos;
1018         size_t const fntCapacity = nbFiles + 1;  /* minimum 1, allows adding one reference, typically stdin */
1019         const char** const fileNamesTable = (const char**)malloc(fntCapacity * sizeof(*fileNamesTable));
1020         if (!fileNamesTable) { free(buf); return NULL; }
1021 
1022         for (ifnNb = 0, pos = 0; ifnNb < nbFiles; ifnNb++) {
1023             fileNamesTable[ifnNb] = buf + pos;
1024             if (buf + pos > bufend) { free(buf); free((void*)fileNamesTable); return NULL; }
1025             pos += strlen(fileNamesTable[ifnNb]) + 1;
1026         }
1027         return UTIL_assembleFileNamesTable2(fileNamesTable, nbFiles, fntCapacity, buf);
1028     }
1029 }
1030 
1031 
UTIL_expandFNT(FileNamesTable ** fnt,int followLinks)1032 void UTIL_expandFNT(FileNamesTable** fnt, int followLinks)
1033 {
1034     FileNamesTable* const newFNT = UTIL_createExpandedFNT((*fnt)->fileNames, (*fnt)->tableSize, followLinks);
1035     CONTROL(newFNT != NULL);
1036     UTIL_freeFileNamesTable(*fnt);
1037     *fnt = newFNT;
1038 }
1039 
UTIL_createFNT_fromROTable(const char ** filenames,size_t nbFilenames)1040 FileNamesTable* UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames)
1041 {
1042     size_t const sizeof_FNTable = nbFilenames * sizeof(*filenames);
1043     const char** const newFNTable = (const char**)malloc(sizeof_FNTable);
1044     if (newFNTable==NULL) return NULL;
1045     memcpy((void*)newFNTable, filenames, sizeof_FNTable);  /* void* : mitigate a Visual compiler bug or limitation */
1046     return UTIL_assembleFileNamesTable(newFNTable, nbFilenames, NULL);
1047 }
1048 
1049 
1050 /*-****************************************
1051 *  count the number of physical cores
1052 ******************************************/
1053 
1054 #if defined(_WIN32) || defined(WIN32)
1055 
1056 #include <windows.h>
1057 
1058 typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
1059 
UTIL_countPhysicalCores(void)1060 int UTIL_countPhysicalCores(void)
1061 {
1062     static int numPhysicalCores = 0;
1063     if (numPhysicalCores != 0) return numPhysicalCores;
1064 
1065     {   LPFN_GLPI glpi;
1066         BOOL done = FALSE;
1067         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
1068         PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
1069         DWORD returnLength = 0;
1070         size_t byteOffset = 0;
1071 
1072 #if defined(_MSC_VER)
1073 /* Visual Studio does not like the following cast */
1074 #   pragma warning( disable : 4054 )  /* conversion from function ptr to data ptr */
1075 #   pragma warning( disable : 4055 )  /* conversion from data ptr to function ptr */
1076 #endif
1077         glpi = (LPFN_GLPI)(void*)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
1078                                                "GetLogicalProcessorInformation");
1079 
1080         if (glpi == NULL) {
1081             goto failed;
1082         }
1083 
1084         while(!done) {
1085             DWORD rc = glpi(buffer, &returnLength);
1086             if (FALSE == rc) {
1087                 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
1088                     if (buffer)
1089                         free(buffer);
1090                     buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
1091 
1092                     if (buffer == NULL) {
1093                         perror("zstd");
1094                         exit(1);
1095                     }
1096                 } else {
1097                     /* some other error */
1098                     goto failed;
1099                 }
1100             } else {
1101                 done = TRUE;
1102         }   }
1103 
1104         ptr = buffer;
1105 
1106         while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
1107 
1108             if (ptr->Relationship == RelationProcessorCore) {
1109                 numPhysicalCores++;
1110             }
1111 
1112             ptr++;
1113             byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
1114         }
1115 
1116         free(buffer);
1117 
1118         return numPhysicalCores;
1119     }
1120 
1121 failed:
1122     /* try to fall back on GetSystemInfo */
1123     {   SYSTEM_INFO sysinfo;
1124         GetSystemInfo(&sysinfo);
1125         numPhysicalCores = sysinfo.dwNumberOfProcessors;
1126         if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
1127     }
1128     return numPhysicalCores;
1129 }
1130 
1131 #elif defined(__APPLE__)
1132 
1133 #include <sys/sysctl.h>
1134 
1135 /* Use apple-provided syscall
1136  * see: man 3 sysctl */
UTIL_countPhysicalCores(void)1137 int UTIL_countPhysicalCores(void)
1138 {
1139     static S32 numPhysicalCores = 0; /* apple specifies int32_t */
1140     if (numPhysicalCores != 0) return numPhysicalCores;
1141 
1142     {   size_t size = sizeof(S32);
1143         int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
1144         if (ret != 0) {
1145             if (errno == ENOENT) {
1146                 /* entry not present, fall back on 1 */
1147                 numPhysicalCores = 1;
1148             } else {
1149                 perror("zstd: can't get number of physical cpus");
1150                 exit(1);
1151             }
1152         }
1153 
1154         return numPhysicalCores;
1155     }
1156 }
1157 
1158 #elif defined(__linux__)
1159 
1160 /* parse /proc/cpuinfo
1161  * siblings / cpu cores should give hyperthreading ratio
1162  * otherwise fall back on sysconf */
UTIL_countPhysicalCores(void)1163 int UTIL_countPhysicalCores(void)
1164 {
1165     static int numPhysicalCores = 0;
1166 
1167     if (numPhysicalCores != 0) return numPhysicalCores;
1168 
1169     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1170     if (numPhysicalCores == -1) {
1171         /* value not queryable, fall back on 1 */
1172         return numPhysicalCores = 1;
1173     }
1174 
1175     /* try to determine if there's hyperthreading */
1176     {   FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
1177 #define BUF_SIZE 80
1178         char buff[BUF_SIZE];
1179 
1180         int siblings = 0;
1181         int cpu_cores = 0;
1182         int ratio = 1;
1183 
1184         if (cpuinfo == NULL) {
1185             /* fall back on the sysconf value */
1186             return numPhysicalCores;
1187         }
1188 
1189         /* assume the cpu cores/siblings values will be constant across all
1190          * present processors */
1191         while (!feof(cpuinfo)) {
1192             if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
1193                 if (strncmp(buff, "siblings", 8) == 0) {
1194                     const char* const sep = strchr(buff, ':');
1195                     if (sep == NULL || *sep == '\0') {
1196                         /* formatting was broken? */
1197                         goto failed;
1198                     }
1199 
1200                     siblings = atoi(sep + 1);
1201                 }
1202                 if (strncmp(buff, "cpu cores", 9) == 0) {
1203                     const char* const sep = strchr(buff, ':');
1204                     if (sep == NULL || *sep == '\0') {
1205                         /* formatting was broken? */
1206                         goto failed;
1207                     }
1208 
1209                     cpu_cores = atoi(sep + 1);
1210                 }
1211             } else if (ferror(cpuinfo)) {
1212                 /* fall back on the sysconf value */
1213                 goto failed;
1214         }   }
1215         if (siblings && cpu_cores && siblings > cpu_cores) {
1216             ratio = siblings / cpu_cores;
1217         }
1218 
1219         if (ratio && numPhysicalCores > ratio) {
1220             numPhysicalCores = numPhysicalCores / ratio;
1221         }
1222 
1223 failed:
1224         fclose(cpuinfo);
1225         return numPhysicalCores;
1226     }
1227 }
1228 
1229 #elif defined(__FreeBSD__)
1230 
1231 #include <sys/param.h>
1232 #include <sys/sysctl.h>
1233 
1234 /* Use physical core sysctl when available
1235  * see: man 4 smp, man 3 sysctl */
UTIL_countPhysicalCores(void)1236 int UTIL_countPhysicalCores(void)
1237 {
1238     static int numPhysicalCores = 0; /* freebsd sysctl is native int sized */
1239     if (numPhysicalCores != 0) return numPhysicalCores;
1240 
1241 #if __FreeBSD_version >= 1300008
1242     {   size_t size = sizeof(numPhysicalCores);
1243         int ret = sysctlbyname("kern.smp.cores", &numPhysicalCores, &size, NULL, 0);
1244         if (ret == 0) return numPhysicalCores;
1245         if (errno != ENOENT) {
1246             perror("zstd: can't get number of physical cpus");
1247             exit(1);
1248         }
1249         /* sysctl not present, fall through to older sysconf method */
1250     }
1251 #endif
1252 
1253     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1254     if (numPhysicalCores == -1) {
1255         /* value not queryable, fall back on 1 */
1256         numPhysicalCores = 1;
1257     }
1258     return numPhysicalCores;
1259 }
1260 
1261 #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__CYGWIN__)
1262 
1263 /* Use POSIX sysconf
1264  * see: man 3 sysconf */
UTIL_countPhysicalCores(void)1265 int UTIL_countPhysicalCores(void)
1266 {
1267     static int numPhysicalCores = 0;
1268 
1269     if (numPhysicalCores != 0) return numPhysicalCores;
1270 
1271     numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1272     if (numPhysicalCores == -1) {
1273         /* value not queryable, fall back on 1 */
1274         return numPhysicalCores = 1;
1275     }
1276     return numPhysicalCores;
1277 }
1278 
1279 #else
1280 
UTIL_countPhysicalCores(void)1281 int UTIL_countPhysicalCores(void)
1282 {
1283     /* assume 1 */
1284     return 1;
1285 }
1286 
1287 #endif
1288 
1289 #if defined (__cplusplus)
1290 }
1291 #endif
1292