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