1a0483764SConrad Meyer /* 2a0483764SConrad Meyer * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. 3a0483764SConrad Meyer * All rights reserved. 4a0483764SConrad Meyer * 5a0483764SConrad Meyer * This source code is licensed under both the BSD-style license (found in the 6a0483764SConrad Meyer * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7a0483764SConrad Meyer * in the COPYING file in the root directory of this source tree). 8a0483764SConrad Meyer * You may select, at your option, one of the above-listed licenses. 9a0483764SConrad Meyer */ 10a0483764SConrad Meyer 11a0483764SConrad Meyer #if defined (__cplusplus) 12a0483764SConrad Meyer extern "C" { 13a0483764SConrad Meyer #endif 14a0483764SConrad Meyer 15a0483764SConrad Meyer 16a0483764SConrad Meyer /*-**************************************** 17a0483764SConrad Meyer * Dependencies 18a0483764SConrad Meyer ******************************************/ 19a0483764SConrad Meyer #include "util.h" /* note : ensure that platform.h is included first ! */ 20a0483764SConrad Meyer #include <errno.h> 21a0483764SConrad Meyer #include <assert.h> 22a0483764SConrad Meyer 23a0483764SConrad Meyer 24a0483764SConrad Meyer int UTIL_fileExist(const char* filename) 25a0483764SConrad Meyer { 26a0483764SConrad Meyer stat_t statbuf; 27a0483764SConrad Meyer #if defined(_MSC_VER) 28a0483764SConrad Meyer int const stat_error = _stat64(filename, &statbuf); 29a0483764SConrad Meyer #else 30a0483764SConrad Meyer int const stat_error = stat(filename, &statbuf); 31a0483764SConrad Meyer #endif 32a0483764SConrad Meyer return !stat_error; 33a0483764SConrad Meyer } 34a0483764SConrad Meyer 35a0483764SConrad Meyer int UTIL_isRegularFile(const char* infilename) 36a0483764SConrad Meyer { 37a0483764SConrad Meyer stat_t statbuf; 38a0483764SConrad Meyer return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */ 39a0483764SConrad Meyer } 40a0483764SConrad Meyer 41a0483764SConrad Meyer int UTIL_getFileStat(const char* infilename, stat_t *statbuf) 42a0483764SConrad Meyer { 43a0483764SConrad Meyer int r; 44a0483764SConrad Meyer #if defined(_MSC_VER) 45a0483764SConrad Meyer r = _stat64(infilename, statbuf); 46a0483764SConrad Meyer if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */ 47a0483764SConrad Meyer #else 48a0483764SConrad Meyer r = stat(infilename, statbuf); 49a0483764SConrad Meyer if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */ 50a0483764SConrad Meyer #endif 51a0483764SConrad Meyer return 1; 52a0483764SConrad Meyer } 53a0483764SConrad Meyer 54a0483764SConrad Meyer int UTIL_setFileStat(const char *filename, stat_t *statbuf) 55a0483764SConrad Meyer { 56a0483764SConrad Meyer int res = 0; 57a0483764SConrad Meyer struct utimbuf timebuf; 58a0483764SConrad Meyer 59a0483764SConrad Meyer if (!UTIL_isRegularFile(filename)) 60a0483764SConrad Meyer return -1; 61a0483764SConrad Meyer 62a0483764SConrad Meyer timebuf.actime = time(NULL); 63a0483764SConrad Meyer timebuf.modtime = statbuf->st_mtime; 64a0483764SConrad Meyer res += utime(filename, &timebuf); /* set access and modification times */ 65a0483764SConrad Meyer 66a0483764SConrad Meyer #if !defined(_WIN32) 67a0483764SConrad Meyer res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */ 68a0483764SConrad Meyer #endif 69a0483764SConrad Meyer 70a0483764SConrad Meyer res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */ 71a0483764SConrad Meyer 72a0483764SConrad Meyer errno = 0; 73a0483764SConrad Meyer return -res; /* number of errors is returned */ 74a0483764SConrad Meyer } 75a0483764SConrad Meyer 76a0483764SConrad Meyer U32 UTIL_isDirectory(const char* infilename) 77a0483764SConrad Meyer { 78a0483764SConrad Meyer int r; 79a0483764SConrad Meyer stat_t statbuf; 80a0483764SConrad Meyer #if defined(_MSC_VER) 81a0483764SConrad Meyer r = _stat64(infilename, &statbuf); 82a0483764SConrad Meyer if (!r && (statbuf.st_mode & _S_IFDIR)) return 1; 83a0483764SConrad Meyer #else 84a0483764SConrad Meyer r = stat(infilename, &statbuf); 85a0483764SConrad Meyer if (!r && S_ISDIR(statbuf.st_mode)) return 1; 86a0483764SConrad Meyer #endif 87a0483764SConrad Meyer return 0; 88a0483764SConrad Meyer } 89a0483764SConrad Meyer 90*2b9c00cbSConrad Meyer int UTIL_isSameFile(const char* file1, const char* file2) 91*2b9c00cbSConrad Meyer { 92*2b9c00cbSConrad Meyer #if defined(_MSC_VER) 93*2b9c00cbSConrad Meyer /* note : Visual does not support file identification by inode. 94*2b9c00cbSConrad Meyer * The following work-around is limited to detecting exact name repetition only, 95*2b9c00cbSConrad Meyer * aka `filename` is considered different from `subdir/../filename` */ 96*2b9c00cbSConrad Meyer return !strcmp(file1, file2); 97*2b9c00cbSConrad Meyer #else 98*2b9c00cbSConrad Meyer stat_t file1Stat; 99*2b9c00cbSConrad Meyer stat_t file2Stat; 100*2b9c00cbSConrad Meyer return UTIL_getFileStat(file1, &file1Stat) 101*2b9c00cbSConrad Meyer && UTIL_getFileStat(file2, &file2Stat) 102*2b9c00cbSConrad Meyer && (file1Stat.st_dev == file2Stat.st_dev) 103*2b9c00cbSConrad Meyer && (file1Stat.st_ino == file2Stat.st_ino); 104*2b9c00cbSConrad Meyer #endif 105*2b9c00cbSConrad Meyer } 106*2b9c00cbSConrad Meyer 107a0483764SConrad Meyer U32 UTIL_isLink(const char* infilename) 108a0483764SConrad Meyer { 109a0483764SConrad Meyer /* macro guards, as defined in : https://linux.die.net/man/2/lstat */ 110a0483764SConrad Meyer #ifndef __STRICT_ANSI__ 111a0483764SConrad Meyer #if defined(_BSD_SOURCE) \ 112a0483764SConrad Meyer || (defined(_XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) \ 113a0483764SConrad Meyer || (defined(_XOPEN_SOURCE) && defined(_XOPEN_SOURCE_EXTENDED)) \ 114a0483764SConrad Meyer || (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) \ 115*2b9c00cbSConrad Meyer || (defined(__APPLE__) && defined(__MACH__)) \ 116*2b9c00cbSConrad Meyer || defined(__OpenBSD__) \ 117*2b9c00cbSConrad Meyer || defined(__FreeBSD__) 118a0483764SConrad Meyer int r; 119a0483764SConrad Meyer stat_t statbuf; 120a0483764SConrad Meyer r = lstat(infilename, &statbuf); 121a0483764SConrad Meyer if (!r && S_ISLNK(statbuf.st_mode)) return 1; 122a0483764SConrad Meyer #endif 123a0483764SConrad Meyer #endif 124a0483764SConrad Meyer (void)infilename; 125a0483764SConrad Meyer return 0; 126a0483764SConrad Meyer } 127a0483764SConrad Meyer 128a0483764SConrad Meyer U64 UTIL_getFileSize(const char* infilename) 129a0483764SConrad Meyer { 130a0483764SConrad Meyer if (!UTIL_isRegularFile(infilename)) return UTIL_FILESIZE_UNKNOWN; 131a0483764SConrad Meyer { int r; 132a0483764SConrad Meyer #if defined(_MSC_VER) 133a0483764SConrad Meyer struct __stat64 statbuf; 134a0483764SConrad Meyer r = _stat64(infilename, &statbuf); 135a0483764SConrad Meyer if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN; 136a0483764SConrad Meyer #elif defined(__MINGW32__) && defined (__MSVCRT__) 137a0483764SConrad Meyer struct _stati64 statbuf; 138a0483764SConrad Meyer r = _stati64(infilename, &statbuf); 139a0483764SConrad Meyer if (r || !(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN; 140a0483764SConrad Meyer #else 141a0483764SConrad Meyer struct stat statbuf; 142a0483764SConrad Meyer r = stat(infilename, &statbuf); 143a0483764SConrad Meyer if (r || !S_ISREG(statbuf.st_mode)) return UTIL_FILESIZE_UNKNOWN; 144a0483764SConrad Meyer #endif 145a0483764SConrad Meyer return (U64)statbuf.st_size; 146a0483764SConrad Meyer } 147a0483764SConrad Meyer } 148a0483764SConrad Meyer 149a0483764SConrad Meyer 150a0483764SConrad Meyer U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles) 151a0483764SConrad Meyer { 152a0483764SConrad Meyer U64 total = 0; 153a0483764SConrad Meyer int error = 0; 154a0483764SConrad Meyer unsigned n; 155a0483764SConrad Meyer for (n=0; n<nbFiles; n++) { 156a0483764SConrad Meyer U64 const size = UTIL_getFileSize(fileNamesTable[n]); 157a0483764SConrad Meyer error |= (size == UTIL_FILESIZE_UNKNOWN); 158a0483764SConrad Meyer total += size; 159a0483764SConrad Meyer } 160a0483764SConrad Meyer return error ? UTIL_FILESIZE_UNKNOWN : total; 161a0483764SConrad Meyer } 162a0483764SConrad Meyer 163a0483764SConrad Meyer #ifdef _WIN32 164a0483764SConrad Meyer int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks) 165a0483764SConrad Meyer { 166a0483764SConrad Meyer char* path; 167a0483764SConrad Meyer int dirLength, fnameLength, pathLength, nbFiles = 0; 168a0483764SConrad Meyer WIN32_FIND_DATAA cFile; 169a0483764SConrad Meyer HANDLE hFile; 170a0483764SConrad Meyer 171a0483764SConrad Meyer dirLength = (int)strlen(dirName); 172a0483764SConrad Meyer path = (char*) malloc(dirLength + 3); 173a0483764SConrad Meyer if (!path) return 0; 174a0483764SConrad Meyer 175a0483764SConrad Meyer memcpy(path, dirName, dirLength); 176a0483764SConrad Meyer path[dirLength] = '\\'; 177a0483764SConrad Meyer path[dirLength+1] = '*'; 178a0483764SConrad Meyer path[dirLength+2] = 0; 179a0483764SConrad Meyer 180a0483764SConrad Meyer hFile=FindFirstFileA(path, &cFile); 181a0483764SConrad Meyer if (hFile == INVALID_HANDLE_VALUE) { 182a0483764SConrad Meyer UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName); 183a0483764SConrad Meyer return 0; 184a0483764SConrad Meyer } 185a0483764SConrad Meyer free(path); 186a0483764SConrad Meyer 187a0483764SConrad Meyer do { 188a0483764SConrad Meyer fnameLength = (int)strlen(cFile.cFileName); 189a0483764SConrad Meyer path = (char*) malloc(dirLength + fnameLength + 2); 190a0483764SConrad Meyer if (!path) { FindClose(hFile); return 0; } 191a0483764SConrad Meyer memcpy(path, dirName, dirLength); 192a0483764SConrad Meyer path[dirLength] = '\\'; 193a0483764SConrad Meyer memcpy(path+dirLength+1, cFile.cFileName, fnameLength); 194a0483764SConrad Meyer pathLength = dirLength+1+fnameLength; 195a0483764SConrad Meyer path[pathLength] = 0; 196a0483764SConrad Meyer if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { 197a0483764SConrad Meyer if ( strcmp (cFile.cFileName, "..") == 0 198a0483764SConrad Meyer || strcmp (cFile.cFileName, ".") == 0 ) 199a0483764SConrad Meyer continue; 200a0483764SConrad Meyer /* Recursively call "UTIL_prepareFileList" with the new path. */ 201a0483764SConrad Meyer nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); 202a0483764SConrad Meyer if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } 203a0483764SConrad Meyer } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) 204a0483764SConrad Meyer || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) 205a0483764SConrad Meyer || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) { 206a0483764SConrad Meyer if (*bufStart + *pos + pathLength >= *bufEnd) { 207a0483764SConrad Meyer ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; 208a0483764SConrad Meyer *bufStart = (char*)UTIL_realloc(*bufStart, newListSize); 209a0483764SConrad Meyer if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } 210a0483764SConrad Meyer *bufEnd = *bufStart + newListSize; 211a0483764SConrad Meyer } 212a0483764SConrad Meyer if (*bufStart + *pos + pathLength < *bufEnd) { 213a0483764SConrad Meyer memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */); 214a0483764SConrad Meyer *pos += pathLength + 1; 215a0483764SConrad Meyer nbFiles++; 216a0483764SConrad Meyer } 217a0483764SConrad Meyer } 218a0483764SConrad Meyer free(path); 219a0483764SConrad Meyer } while (FindNextFileA(hFile, &cFile)); 220a0483764SConrad Meyer 221a0483764SConrad Meyer FindClose(hFile); 222a0483764SConrad Meyer return nbFiles; 223a0483764SConrad Meyer } 224a0483764SConrad Meyer 225a0483764SConrad Meyer #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ 226a0483764SConrad Meyer 227a0483764SConrad Meyer int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks) 228a0483764SConrad Meyer { 229a0483764SConrad Meyer DIR *dir; 230a0483764SConrad Meyer struct dirent *entry; 231a0483764SConrad Meyer char* path; 232a0483764SConrad Meyer int dirLength, fnameLength, pathLength, nbFiles = 0; 233a0483764SConrad Meyer 234a0483764SConrad Meyer if (!(dir = opendir(dirName))) { 235a0483764SConrad Meyer UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno)); 236a0483764SConrad Meyer return 0; 237a0483764SConrad Meyer } 238a0483764SConrad Meyer 239a0483764SConrad Meyer dirLength = (int)strlen(dirName); 240a0483764SConrad Meyer errno = 0; 241a0483764SConrad Meyer while ((entry = readdir(dir)) != NULL) { 242a0483764SConrad Meyer if (strcmp (entry->d_name, "..") == 0 || 243a0483764SConrad Meyer strcmp (entry->d_name, ".") == 0) continue; 244a0483764SConrad Meyer fnameLength = (int)strlen(entry->d_name); 245a0483764SConrad Meyer path = (char*) malloc(dirLength + fnameLength + 2); 246a0483764SConrad Meyer if (!path) { closedir(dir); return 0; } 247a0483764SConrad Meyer memcpy(path, dirName, dirLength); 248a0483764SConrad Meyer 249a0483764SConrad Meyer path[dirLength] = '/'; 250a0483764SConrad Meyer memcpy(path+dirLength+1, entry->d_name, fnameLength); 251a0483764SConrad Meyer pathLength = dirLength+1+fnameLength; 252a0483764SConrad Meyer path[pathLength] = 0; 253a0483764SConrad Meyer 254a0483764SConrad Meyer if (!followLinks && UTIL_isLink(path)) { 255a0483764SConrad Meyer UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path); 256a0483764SConrad Meyer continue; 257a0483764SConrad Meyer } 258a0483764SConrad Meyer 259a0483764SConrad Meyer if (UTIL_isDirectory(path)) { 260a0483764SConrad Meyer nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */ 261a0483764SConrad Meyer if (*bufStart == NULL) { free(path); closedir(dir); return 0; } 262a0483764SConrad Meyer } else { 263a0483764SConrad Meyer if (*bufStart + *pos + pathLength >= *bufEnd) { 264a0483764SConrad Meyer ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; 265a0483764SConrad Meyer *bufStart = (char*)UTIL_realloc(*bufStart, newListSize); 266a0483764SConrad Meyer *bufEnd = *bufStart + newListSize; 267a0483764SConrad Meyer if (*bufStart == NULL) { free(path); closedir(dir); return 0; } 268a0483764SConrad Meyer } 269a0483764SConrad Meyer if (*bufStart + *pos + pathLength < *bufEnd) { 270a0483764SConrad Meyer memcpy(*bufStart + *pos, path, pathLength + 1); /* with final \0 */ 271a0483764SConrad Meyer *pos += pathLength + 1; 272a0483764SConrad Meyer nbFiles++; 273a0483764SConrad Meyer } 274a0483764SConrad Meyer } 275a0483764SConrad Meyer free(path); 276a0483764SConrad Meyer errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */ 277a0483764SConrad Meyer } 278a0483764SConrad Meyer 279a0483764SConrad Meyer if (errno != 0) { 280a0483764SConrad Meyer UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s\n", dirName, strerror(errno)); 281a0483764SConrad Meyer free(*bufStart); 282a0483764SConrad Meyer *bufStart = NULL; 283a0483764SConrad Meyer } 284a0483764SConrad Meyer closedir(dir); 285a0483764SConrad Meyer return nbFiles; 286a0483764SConrad Meyer } 287a0483764SConrad Meyer 288a0483764SConrad Meyer #else 289a0483764SConrad Meyer 290a0483764SConrad Meyer int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks) 291a0483764SConrad Meyer { 292a0483764SConrad Meyer (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks; 293a0483764SConrad Meyer UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName); 294a0483764SConrad Meyer return 0; 295a0483764SConrad Meyer } 296a0483764SConrad Meyer 297a0483764SConrad Meyer #endif /* #ifdef _WIN32 */ 298a0483764SConrad Meyer 299a0483764SConrad Meyer /* 300a0483764SConrad Meyer * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories, 301a0483764SConrad Meyer * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb). 302a0483764SConrad Meyer * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer) 303a0483764SConrad Meyer * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called. 304a0483764SConrad Meyer */ 305a0483764SConrad Meyer const char** 306a0483764SConrad Meyer UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, 307a0483764SConrad Meyer char** allocatedBuffer, unsigned* allocatedNamesNb, 308a0483764SConrad Meyer int followLinks) 309a0483764SConrad Meyer { 310a0483764SConrad Meyer size_t pos; 311a0483764SConrad Meyer unsigned i, nbFiles; 312a0483764SConrad Meyer char* buf = (char*)malloc(LIST_SIZE_INCREASE); 313a0483764SConrad Meyer char* bufend = buf + LIST_SIZE_INCREASE; 314a0483764SConrad Meyer const char** fileTable; 315a0483764SConrad Meyer 316a0483764SConrad Meyer if (!buf) return NULL; 317a0483764SConrad Meyer 318a0483764SConrad Meyer for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) { 319a0483764SConrad Meyer if (!UTIL_isDirectory(inputNames[i])) { 320a0483764SConrad Meyer size_t const len = strlen(inputNames[i]); 321a0483764SConrad Meyer if (buf + pos + len >= bufend) { 322a0483764SConrad Meyer ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE; 323a0483764SConrad Meyer buf = (char*)UTIL_realloc(buf, newListSize); 324a0483764SConrad Meyer bufend = buf + newListSize; 325a0483764SConrad Meyer if (!buf) return NULL; 326a0483764SConrad Meyer } 327a0483764SConrad Meyer if (buf + pos + len < bufend) { 328a0483764SConrad Meyer memcpy(buf+pos, inputNames[i], len+1); /* with final \0 */ 329a0483764SConrad Meyer pos += len + 1; 330a0483764SConrad Meyer nbFiles++; 331a0483764SConrad Meyer } 332a0483764SConrad Meyer } else { 333a0483764SConrad Meyer nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks); 334a0483764SConrad Meyer if (buf == NULL) return NULL; 335a0483764SConrad Meyer } } 336a0483764SConrad Meyer 337a0483764SConrad Meyer if (nbFiles == 0) { free(buf); return NULL; } 338a0483764SConrad Meyer 339a0483764SConrad Meyer fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*)); 340a0483764SConrad Meyer if (!fileTable) { free(buf); return NULL; } 341a0483764SConrad Meyer 342a0483764SConrad Meyer for (i=0, pos=0; i<nbFiles; i++) { 343a0483764SConrad Meyer fileTable[i] = buf + pos; 344a0483764SConrad Meyer pos += strlen(fileTable[i]) + 1; 345a0483764SConrad Meyer } 346a0483764SConrad Meyer 347a0483764SConrad Meyer if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; } 348a0483764SConrad Meyer 349a0483764SConrad Meyer *allocatedBuffer = buf; 350a0483764SConrad Meyer *allocatedNamesNb = nbFiles; 351a0483764SConrad Meyer 352a0483764SConrad Meyer return fileTable; 353a0483764SConrad Meyer } 354a0483764SConrad Meyer 355*2b9c00cbSConrad Meyer 356a0483764SConrad Meyer /*-**************************************** 357a0483764SConrad Meyer * Console log 358a0483764SConrad Meyer ******************************************/ 359a0483764SConrad Meyer int g_utilDisplayLevel; 360a0483764SConrad Meyer 361a0483764SConrad Meyer 362*2b9c00cbSConrad Meyer 363a0483764SConrad Meyer /*-**************************************** 364*2b9c00cbSConrad Meyer * count the number of physical cores 365a0483764SConrad Meyer ******************************************/ 366a0483764SConrad Meyer 367a0483764SConrad Meyer #if defined(_WIN32) || defined(WIN32) 368a0483764SConrad Meyer 369a0483764SConrad Meyer #include <windows.h> 370a0483764SConrad Meyer 371a0483764SConrad Meyer typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD); 372a0483764SConrad Meyer 373a0483764SConrad Meyer int UTIL_countPhysicalCores(void) 374a0483764SConrad Meyer { 375a0483764SConrad Meyer static int numPhysicalCores = 0; 376a0483764SConrad Meyer if (numPhysicalCores != 0) return numPhysicalCores; 377a0483764SConrad Meyer 378a0483764SConrad Meyer { LPFN_GLPI glpi; 379a0483764SConrad Meyer BOOL done = FALSE; 380a0483764SConrad Meyer PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; 381a0483764SConrad Meyer PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL; 382a0483764SConrad Meyer DWORD returnLength = 0; 383a0483764SConrad Meyer size_t byteOffset = 0; 384a0483764SConrad Meyer 385a0483764SConrad Meyer glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")), 386a0483764SConrad Meyer "GetLogicalProcessorInformation"); 387a0483764SConrad Meyer 388a0483764SConrad Meyer if (glpi == NULL) { 389a0483764SConrad Meyer goto failed; 390a0483764SConrad Meyer } 391a0483764SConrad Meyer 392a0483764SConrad Meyer while(!done) { 393a0483764SConrad Meyer DWORD rc = glpi(buffer, &returnLength); 394a0483764SConrad Meyer if (FALSE == rc) { 395a0483764SConrad Meyer if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { 396a0483764SConrad Meyer if (buffer) 397a0483764SConrad Meyer free(buffer); 398a0483764SConrad Meyer buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength); 399a0483764SConrad Meyer 400a0483764SConrad Meyer if (buffer == NULL) { 401a0483764SConrad Meyer perror("zstd"); 402a0483764SConrad Meyer exit(1); 403a0483764SConrad Meyer } 404a0483764SConrad Meyer } else { 405a0483764SConrad Meyer /* some other error */ 406a0483764SConrad Meyer goto failed; 407a0483764SConrad Meyer } 408a0483764SConrad Meyer } else { 409a0483764SConrad Meyer done = TRUE; 410a0483764SConrad Meyer } 411a0483764SConrad Meyer } 412a0483764SConrad Meyer 413a0483764SConrad Meyer ptr = buffer; 414a0483764SConrad Meyer 415a0483764SConrad Meyer while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) { 416a0483764SConrad Meyer 417a0483764SConrad Meyer if (ptr->Relationship == RelationProcessorCore) { 418a0483764SConrad Meyer numPhysicalCores++; 419a0483764SConrad Meyer } 420a0483764SConrad Meyer 421a0483764SConrad Meyer ptr++; 422a0483764SConrad Meyer byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); 423a0483764SConrad Meyer } 424a0483764SConrad Meyer 425a0483764SConrad Meyer free(buffer); 426a0483764SConrad Meyer 427a0483764SConrad Meyer return numPhysicalCores; 428a0483764SConrad Meyer } 429a0483764SConrad Meyer 430a0483764SConrad Meyer failed: 431a0483764SConrad Meyer /* try to fall back on GetSystemInfo */ 432a0483764SConrad Meyer { SYSTEM_INFO sysinfo; 433a0483764SConrad Meyer GetSystemInfo(&sysinfo); 434a0483764SConrad Meyer numPhysicalCores = sysinfo.dwNumberOfProcessors; 435a0483764SConrad Meyer if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */ 436a0483764SConrad Meyer } 437a0483764SConrad Meyer return numPhysicalCores; 438a0483764SConrad Meyer } 439a0483764SConrad Meyer 440a0483764SConrad Meyer #elif defined(__APPLE__) 441a0483764SConrad Meyer 442a0483764SConrad Meyer #include <sys/sysctl.h> 443a0483764SConrad Meyer 444a0483764SConrad Meyer /* Use apple-provided syscall 445a0483764SConrad Meyer * see: man 3 sysctl */ 446a0483764SConrad Meyer int UTIL_countPhysicalCores(void) 447a0483764SConrad Meyer { 448a0483764SConrad Meyer static S32 numPhysicalCores = 0; /* apple specifies int32_t */ 449a0483764SConrad Meyer if (numPhysicalCores != 0) return numPhysicalCores; 450a0483764SConrad Meyer 451a0483764SConrad Meyer { size_t size = sizeof(S32); 452a0483764SConrad Meyer int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0); 453a0483764SConrad Meyer if (ret != 0) { 454a0483764SConrad Meyer if (errno == ENOENT) { 455a0483764SConrad Meyer /* entry not present, fall back on 1 */ 456a0483764SConrad Meyer numPhysicalCores = 1; 457a0483764SConrad Meyer } else { 458a0483764SConrad Meyer perror("zstd: can't get number of physical cpus"); 459a0483764SConrad Meyer exit(1); 460a0483764SConrad Meyer } 461a0483764SConrad Meyer } 462a0483764SConrad Meyer 463a0483764SConrad Meyer return numPhysicalCores; 464a0483764SConrad Meyer } 465a0483764SConrad Meyer } 466a0483764SConrad Meyer 467a0483764SConrad Meyer #elif defined(__linux__) 468a0483764SConrad Meyer 469a0483764SConrad Meyer /* parse /proc/cpuinfo 470a0483764SConrad Meyer * siblings / cpu cores should give hyperthreading ratio 471a0483764SConrad Meyer * otherwise fall back on sysconf */ 472a0483764SConrad Meyer int UTIL_countPhysicalCores(void) 473a0483764SConrad Meyer { 474a0483764SConrad Meyer static int numPhysicalCores = 0; 475a0483764SConrad Meyer 476a0483764SConrad Meyer if (numPhysicalCores != 0) return numPhysicalCores; 477a0483764SConrad Meyer 478a0483764SConrad Meyer numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN); 479a0483764SConrad Meyer if (numPhysicalCores == -1) { 480a0483764SConrad Meyer /* value not queryable, fall back on 1 */ 481a0483764SConrad Meyer return numPhysicalCores = 1; 482a0483764SConrad Meyer } 483a0483764SConrad Meyer 484a0483764SConrad Meyer /* try to determine if there's hyperthreading */ 485a0483764SConrad Meyer { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r"); 486a0483764SConrad Meyer #define BUF_SIZE 80 487a0483764SConrad Meyer char buff[BUF_SIZE]; 488a0483764SConrad Meyer 489a0483764SConrad Meyer int siblings = 0; 490a0483764SConrad Meyer int cpu_cores = 0; 491a0483764SConrad Meyer int ratio = 1; 492a0483764SConrad Meyer 493a0483764SConrad Meyer if (cpuinfo == NULL) { 494a0483764SConrad Meyer /* fall back on the sysconf value */ 495a0483764SConrad Meyer return numPhysicalCores; 496a0483764SConrad Meyer } 497a0483764SConrad Meyer 498a0483764SConrad Meyer /* assume the cpu cores/siblings values will be constant across all 499a0483764SConrad Meyer * present processors */ 500a0483764SConrad Meyer while (!feof(cpuinfo)) { 501a0483764SConrad Meyer if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) { 502a0483764SConrad Meyer if (strncmp(buff, "siblings", 8) == 0) { 503a0483764SConrad Meyer const char* const sep = strchr(buff, ':'); 504a0483764SConrad Meyer if (*sep == '\0') { 505a0483764SConrad Meyer /* formatting was broken? */ 506a0483764SConrad Meyer goto failed; 507a0483764SConrad Meyer } 508a0483764SConrad Meyer 509a0483764SConrad Meyer siblings = atoi(sep + 1); 510a0483764SConrad Meyer } 511a0483764SConrad Meyer if (strncmp(buff, "cpu cores", 9) == 0) { 512a0483764SConrad Meyer const char* const sep = strchr(buff, ':'); 513a0483764SConrad Meyer if (*sep == '\0') { 514a0483764SConrad Meyer /* formatting was broken? */ 515a0483764SConrad Meyer goto failed; 516a0483764SConrad Meyer } 517a0483764SConrad Meyer 518a0483764SConrad Meyer cpu_cores = atoi(sep + 1); 519a0483764SConrad Meyer } 520a0483764SConrad Meyer } else if (ferror(cpuinfo)) { 521a0483764SConrad Meyer /* fall back on the sysconf value */ 522a0483764SConrad Meyer goto failed; 523a0483764SConrad Meyer } 524a0483764SConrad Meyer } 525a0483764SConrad Meyer if (siblings && cpu_cores) { 526a0483764SConrad Meyer ratio = siblings / cpu_cores; 527a0483764SConrad Meyer } 528a0483764SConrad Meyer failed: 529a0483764SConrad Meyer fclose(cpuinfo); 530a0483764SConrad Meyer return numPhysicalCores = numPhysicalCores / ratio; 531a0483764SConrad Meyer } 532a0483764SConrad Meyer } 533a0483764SConrad Meyer 534*2b9c00cbSConrad Meyer #elif defined(__FreeBSD__) 535a0483764SConrad Meyer 536*2b9c00cbSConrad Meyer #include <sys/param.h> 537*2b9c00cbSConrad Meyer #include <sys/sysctl.h> 538*2b9c00cbSConrad Meyer 539*2b9c00cbSConrad Meyer /* Use physical core sysctl when available 540*2b9c00cbSConrad Meyer * see: man 4 smp, man 3 sysctl */ 541*2b9c00cbSConrad Meyer int UTIL_countPhysicalCores(void) 542*2b9c00cbSConrad Meyer { 543*2b9c00cbSConrad Meyer static int numPhysicalCores = 0; /* freebsd sysctl is native int sized */ 544*2b9c00cbSConrad Meyer if (numPhysicalCores != 0) return numPhysicalCores; 545*2b9c00cbSConrad Meyer 546*2b9c00cbSConrad Meyer #if __FreeBSD_version >= 1300008 547*2b9c00cbSConrad Meyer { size_t size = sizeof(numPhysicalCores); 548*2b9c00cbSConrad Meyer int ret = sysctlbyname("kern.smp.cores", &numPhysicalCores, &size, NULL, 0); 549*2b9c00cbSConrad Meyer if (ret == 0) return numPhysicalCores; 550*2b9c00cbSConrad Meyer if (errno != ENOENT) { 551*2b9c00cbSConrad Meyer perror("zstd: can't get number of physical cpus"); 552*2b9c00cbSConrad Meyer exit(1); 553*2b9c00cbSConrad Meyer } 554*2b9c00cbSConrad Meyer /* sysctl not present, fall through to older sysconf method */ 555*2b9c00cbSConrad Meyer } 556*2b9c00cbSConrad Meyer #endif 557*2b9c00cbSConrad Meyer 558*2b9c00cbSConrad Meyer numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN); 559*2b9c00cbSConrad Meyer if (numPhysicalCores == -1) { 560*2b9c00cbSConrad Meyer /* value not queryable, fall back on 1 */ 561*2b9c00cbSConrad Meyer numPhysicalCores = 1; 562*2b9c00cbSConrad Meyer } 563*2b9c00cbSConrad Meyer return numPhysicalCores; 564*2b9c00cbSConrad Meyer } 565*2b9c00cbSConrad Meyer 566*2b9c00cbSConrad Meyer #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) 567*2b9c00cbSConrad Meyer 568*2b9c00cbSConrad Meyer /* Use POSIX sysconf 569*2b9c00cbSConrad Meyer * see: man 3 sysconf */ 570a0483764SConrad Meyer int UTIL_countPhysicalCores(void) 571a0483764SConrad Meyer { 572a0483764SConrad Meyer static int numPhysicalCores = 0; 573a0483764SConrad Meyer 574a0483764SConrad Meyer if (numPhysicalCores != 0) return numPhysicalCores; 575a0483764SConrad Meyer 576a0483764SConrad Meyer numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN); 577a0483764SConrad Meyer if (numPhysicalCores == -1) { 578a0483764SConrad Meyer /* value not queryable, fall back on 1 */ 579a0483764SConrad Meyer return numPhysicalCores = 1; 580a0483764SConrad Meyer } 581a0483764SConrad Meyer return numPhysicalCores; 582a0483764SConrad Meyer } 583a0483764SConrad Meyer 584a0483764SConrad Meyer #else 585a0483764SConrad Meyer 586a0483764SConrad Meyer int UTIL_countPhysicalCores(void) 587a0483764SConrad Meyer { 588a0483764SConrad Meyer /* assume 1 */ 589a0483764SConrad Meyer return 1; 590a0483764SConrad Meyer } 591a0483764SConrad Meyer 592a0483764SConrad Meyer #endif 593a0483764SConrad Meyer 594a0483764SConrad Meyer #if defined (__cplusplus) 595a0483764SConrad Meyer } 596a0483764SConrad Meyer #endif 597