2 * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 #if defined (__cplusplus)
16 /*-****************************************
18 ******************************************/
19 #include "util.h" /* note : ensure that platform.h is included first ! */
24 int UTIL_fileExist(const char* filename)
28 int const stat_error = _stat64(filename, &statbuf);
30 int const stat_error = stat(filename, &statbuf);
35 int UTIL_isRegularFile(const char* infilename)
38 return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
41 int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
45 r = _stat64(infilename, statbuf);
46 if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */
48 r = stat(infilename, statbuf);
49 if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */
54 int UTIL_setFileStat(const char *filename, stat_t *statbuf)
57 struct utimbuf timebuf;
59 if (!UTIL_isRegularFile(filename))
62 timebuf.actime = time(NULL);
63 timebuf.modtime = statbuf->st_mtime;
64 res += utime(filename, &timebuf); /* set access and modification times */
67 res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */
70 res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */
73 return -res; /* number of errors is returned */
76 U32 UTIL_isDirectory(const char* infilename)
81 r = _stat64(infilename, &statbuf);
82 if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
84 r = stat(infilename, &statbuf);
85 if (!r && S_ISDIR(statbuf.st_mode)) return 1;
90 int UTIL_isSameFile(const char* file1, const char* file2)
93 /* note : Visual does not support file identification by inode.
94 * The following work-around is limited to detecting exact name repetition only,
95 * aka `filename` is considered different from `subdir/../filename` */
96 return !strcmp(file1, file2);
100 return UTIL_getFileStat(file1, &file1Stat)
101 && UTIL_getFileStat(file2, &file2Stat)
102 && (file1Stat.st_dev == file2Stat.st_dev)
103 && (file1Stat.st_ino == file2Stat.st_ino);
107 U32 UTIL_isLink(const char* infilename)
109 /* macro guards, as defined in : https://linux.die.net/man/2/lstat */
110 #ifndef __STRICT_ANSI__
111 #if defined(_BSD_SOURCE) \
112 || (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) \
113 || (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) \
114 || (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) \
115 || (defined(__APPLE__) && defined(__MACH__)) \
116 || defined(__OpenBSD__) \
117 || defined(__FreeBSD__)
120 r = lstat(infilename, &statbuf);
121 if (!r && S_ISLNK(statbuf.st_mode)) return 1;
128 U64 UTIL_getFileSize(const char* infilename)
130 if (!UTIL_isRegularFile(infilename)) return UTIL_FILESIZE_UNKNOWN;
132 #if defined(_MSC_VER)
133 struct __stat64 statbuf;
134 r = _stat64(infilename, &statbuf);
135 if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
136 #elif defined(__MINGW32__) && defined (__MSVCRT__)
137 struct _stati64 statbuf;
138 r = _stati64(infilename, &statbuf);
139 if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
142 r = stat(infilename, &statbuf);
143 if (r || !S_ISREG(statbuf.st_mode)) return UTIL_FILESIZE_UNKNOWN;
145 return (U64)statbuf.st_size;
150 U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles)
155 for (n=0; n<nbFiles; n++) {
156 U64 const size = UTIL_getFileSize(fileNamesTable[n]);
157 error |= (size == UTIL_FILESIZE_UNKNOWN);
160 return error ? UTIL_FILESIZE_UNKNOWN : total;
164 int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
167 int dirLength, fnameLength, pathLength, nbFiles = 0;
168 WIN32_FIND_DATAA cFile;
171 dirLength = (int)strlen(dirName);
172 path = (char*) malloc(dirLength + 3);
175 memcpy(path, dirName, dirLength);
176 path[dirLength] = '\\';
177 path[dirLength+1] = '*';
178 path[dirLength+2] = 0;
180 hFile=FindFirstFileA(path, &cFile);
181 if (hFile == INVALID_HANDLE_VALUE) {
182 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
188 fnameLength = (int)strlen(cFile.cFileName);
189 path = (char*) malloc(dirLength + fnameLength + 2);
190 if (!path) { FindClose(hFile); return 0; }
191 memcpy(path, dirName, dirLength);
192 path[dirLength] = '\\';
193 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
194 pathLength = dirLength+1+fnameLength;
195 path[pathLength] = 0;
196 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
197 if ( strcmp (cFile.cFileName, "..") == 0
198 || strcmp (cFile.cFileName, ".") == 0 )
200 /* Recursively call "UTIL_prepareFileList" with the new path. */
201 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);
202 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
203 } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
204 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
205 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) {
206 if (*bufStart + *pos + pathLength >= *bufEnd) {
207 ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
208 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
209 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
210 *bufEnd = *bufStart + newListSize;
212 if (*bufStart + *pos + pathLength < *bufEnd) {
213 memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */);
214 *pos += pathLength + 1;
219 } while (FindNextFileA(hFile, &cFile));
225 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
227 int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
230 struct dirent *entry;
232 int dirLength, fnameLength, pathLength, nbFiles = 0;
234 if (!(dir = opendir(dirName))) {
235 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
239 dirLength = (int)strlen(dirName);
241 while ((entry = readdir(dir)) != NULL) {
242 if (strcmp (entry->d_name, "..") == 0 ||
243 strcmp (entry->d_name, ".") == 0) continue;
244 fnameLength = (int)strlen(entry->d_name);
245 path = (char*) malloc(dirLength + fnameLength + 2);
246 if (!path) { closedir(dir); return 0; }
247 memcpy(path, dirName, dirLength);
249 path[dirLength] = '/';
250 memcpy(path+dirLength+1, entry->d_name, fnameLength);
251 pathLength = dirLength+1+fnameLength;
252 path[pathLength] = 0;
254 if (!followLinks && UTIL_isLink(path)) {
255 UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
259 if (UTIL_isDirectory(path)) {
260 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
261 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
263 if (*bufStart + *pos + pathLength >= *bufEnd) {
264 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
265 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
266 *bufEnd = *bufStart + newListSize;
267 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
269 if (*bufStart + *pos + pathLength < *bufEnd) {
270 memcpy(*bufStart + *pos, path, pathLength + 1); /* with final \0 */
271 *pos += pathLength + 1;
276 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
280 UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s\n", dirName, strerror(errno));
290 int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
292 (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks;
293 UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
297 #endif /* #ifdef _WIN32 */
300 * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
301 * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
302 * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
303 * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
306 UTIL_createFileList(const char **inputNames, unsigned inputNamesNb,
307 char** allocatedBuffer, unsigned* allocatedNamesNb,
312 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
313 char* bufend = buf + LIST_SIZE_INCREASE;
314 const char** fileTable;
316 if (!buf) return NULL;
318 for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
319 if (!UTIL_isDirectory(inputNames[i])) {
320 size_t const len = strlen(inputNames[i]);
321 if (buf + pos + len >= bufend) {
322 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
323 buf = (char*)UTIL_realloc(buf, newListSize);
324 bufend = buf + newListSize;
325 if (!buf) return NULL;
327 if (buf + pos + len < bufend) {
328 memcpy(buf+pos, inputNames[i], len+1); /* with final \0 */
333 nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks);
334 if (buf == NULL) return NULL;
337 if (nbFiles == 0) { free(buf); return NULL; }
339 fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
340 if (!fileTable) { free(buf); return NULL; }
342 for (i=0, pos=0; i<nbFiles; i++) {
343 fileTable[i] = buf + pos;
344 pos += strlen(fileTable[i]) + 1;
347 if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
349 *allocatedBuffer = buf;
350 *allocatedNamesNb = nbFiles;
356 /*-****************************************
358 ******************************************/
359 int g_utilDisplayLevel;
363 /*-****************************************
364 * count the number of physical cores
365 ******************************************/
367 #if defined(_WIN32) || defined(WIN32)
371 typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
373 int UTIL_countPhysicalCores(void)
375 static int numPhysicalCores = 0;
376 if (numPhysicalCores != 0) return numPhysicalCores;
380 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
381 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
382 DWORD returnLength = 0;
383 size_t byteOffset = 0;
385 glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
386 "GetLogicalProcessorInformation");
393 DWORD rc = glpi(buffer, &returnLength);
395 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
398 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
400 if (buffer == NULL) {
405 /* some other error */
415 while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
417 if (ptr->Relationship == RelationProcessorCore) {
422 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
427 return numPhysicalCores;
431 /* try to fall back on GetSystemInfo */
432 { SYSTEM_INFO sysinfo;
433 GetSystemInfo(&sysinfo);
434 numPhysicalCores = sysinfo.dwNumberOfProcessors;
435 if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
437 return numPhysicalCores;
440 #elif defined(__APPLE__)
442 #include <sys/sysctl.h>
444 /* Use apple-provided syscall
445 * see: man 3 sysctl */
446 int UTIL_countPhysicalCores(void)
448 static S32 numPhysicalCores = 0; /* apple specifies int32_t */
449 if (numPhysicalCores != 0) return numPhysicalCores;
451 { size_t size = sizeof(S32);
452 int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
454 if (errno == ENOENT) {
455 /* entry not present, fall back on 1 */
456 numPhysicalCores = 1;
458 perror("zstd: can't get number of physical cpus");
463 return numPhysicalCores;
467 #elif defined(__linux__)
469 /* parse /proc/cpuinfo
470 * siblings / cpu cores should give hyperthreading ratio
471 * otherwise fall back on sysconf */
472 int UTIL_countPhysicalCores(void)
474 static int numPhysicalCores = 0;
476 if (numPhysicalCores != 0) return numPhysicalCores;
478 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
479 if (numPhysicalCores == -1) {
480 /* value not queryable, fall back on 1 */
481 return numPhysicalCores = 1;
484 /* try to determine if there's hyperthreading */
485 { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
493 if (cpuinfo == NULL) {
494 /* fall back on the sysconf value */
495 return numPhysicalCores;
498 /* assume the cpu cores/siblings values will be constant across all
499 * present processors */
500 while (!feof(cpuinfo)) {
501 if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
502 if (strncmp(buff, "siblings", 8) == 0) {
503 const char* const sep = strchr(buff, ':');
505 /* formatting was broken? */
509 siblings = atoi(sep + 1);
511 if (strncmp(buff, "cpu cores", 9) == 0) {
512 const char* const sep = strchr(buff, ':');
514 /* formatting was broken? */
518 cpu_cores = atoi(sep + 1);
520 } else if (ferror(cpuinfo)) {
521 /* fall back on the sysconf value */
525 if (siblings && cpu_cores) {
526 ratio = siblings / cpu_cores;
530 return numPhysicalCores = numPhysicalCores / ratio;
534 #elif defined(__FreeBSD__)
536 #include <sys/param.h>
537 #include <sys/sysctl.h>
539 /* Use physical core sysctl when available
540 * see: man 4 smp, man 3 sysctl */
541 int UTIL_countPhysicalCores(void)
543 static int numPhysicalCores = 0; /* freebsd sysctl is native int sized */
544 if (numPhysicalCores != 0) return numPhysicalCores;
546 #if __FreeBSD_version >= 1300008
547 { size_t size = sizeof(numPhysicalCores);
548 int ret = sysctlbyname("kern.smp.cores", &numPhysicalCores, &size, NULL, 0);
549 if (ret == 0) return numPhysicalCores;
550 if (errno != ENOENT) {
551 perror("zstd: can't get number of physical cpus");
554 /* sysctl not present, fall through to older sysconf method */
558 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
559 if (numPhysicalCores == -1) {
560 /* value not queryable, fall back on 1 */
561 numPhysicalCores = 1;
563 return numPhysicalCores;
566 #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
569 * see: man 3 sysconf */
570 int UTIL_countPhysicalCores(void)
572 static int numPhysicalCores = 0;
574 if (numPhysicalCores != 0) return numPhysicalCores;
576 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
577 if (numPhysicalCores == -1) {
578 /* value not queryable, fall back on 1 */
579 return numPhysicalCores = 1;
581 return numPhysicalCores;
586 int UTIL_countPhysicalCores(void)
594 #if defined (__cplusplus)