blob: 65eb195dc4c2a63a46f45ecaf82eea3f4b35711e [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Yann Collet232d62b2017-08-21 11:24:32 -07002 * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01003 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * 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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01009 */
inikep69fcd7c2016-04-28 12:23:33 +020010
inikep69fcd7c2016-04-28 12:23:33 +020011#ifndef UTIL_H_MODULE
12#define UTIL_H_MODULE
13
14#if defined (__cplusplus)
15extern "C" {
16#endif
17
inikep9c22e572016-05-05 11:53:42 +020018
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010019
inikep69fcd7c2016-04-28 12:23:33 +020020/*-****************************************
21* Dependencies
22******************************************/
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010023#include "platform.h" /* PLATFORM_POSIX_VERSION */
24#include <stdlib.h> /* malloc */
25#include <stddef.h> /* size_t, ptrdiff_t */
26#include <stdio.h> /* fprintf */
Sean Purcellafa48512017-04-13 12:28:28 -070027#include <string.h> /* strncmp */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010028#include <sys/types.h> /* stat, utime */
29#include <sys/stat.h> /* stat */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010030#if defined(_MSC_VER)
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010031# include <sys/utime.h> /* utime */
32# include <io.h> /* _chmod */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010033#else
Yann Colletfda539f2016-12-12 01:03:23 +010034# include <unistd.h> /* chown, stat */
35# include <utime.h> /* utime */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010036#endif
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010037#include <time.h> /* time */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010038#include <errno.h>
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010039#include "mem.h" /* U32, U64 */
inikep69fcd7c2016-04-28 12:23:33 +020040
inikep31634032016-05-05 00:25:38 +020041
Przemyslaw Skibinski6e59b3c2017-02-15 17:03:16 +010042/* ************************************************************
43* Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW
44***************************************************************/
45#if defined(_MSC_VER) && (_MSC_VER >= 1400)
46# define UTIL_fseek _fseeki64
47#elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
48# define UTIL_fseek fseeko
49#elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
50# define UTIL_fseek fseeko64
51#else
52# define UTIL_fseek fseek
53#endif
54
55
inikep9c22e572016-05-05 11:53:42 +020056/*-****************************************
57* Sleep functions: Windows - Posix - others
58******************************************/
inikep31634032016-05-05 00:25:38 +020059#if defined(_WIN32)
inikep83c76b42016-04-28 13:16:01 +020060# include <windows.h>
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010061# define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
inikep83c76b42016-04-28 13:16:01 +020062# define UTIL_sleep(s) Sleep(1000*s)
63# define UTIL_sleepMilli(milli) Sleep(milli)
Przemyslaw Skibinskib0f36632016-12-16 15:41:18 +010064#elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */
inikep31634032016-05-05 00:25:38 +020065# include <unistd.h>
66# include <sys/resource.h> /* setpriority */
67# include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010068# if defined(PRIO_PROCESS)
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010069# define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010070# else
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010071# define SET_REALTIME_PRIORITY /* disabled */
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010072# endif
inikep31634032016-05-05 00:25:38 +020073# define UTIL_sleep(s) sleep(s)
Przemyslaw Skibinskib0f36632016-12-16 15:41:18 +010074# if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L) /* nanosleep requires POSIX.1-2001 */
inikep9c22e572016-05-05 11:53:42 +020075# define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
76# else
77# define UTIL_sleepMilli(milli) /* disabled */
78# endif
inikep83c76b42016-04-28 13:16:01 +020079#else
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010080# define SET_REALTIME_PRIORITY /* disabled */
inikep31634032016-05-05 00:25:38 +020081# define UTIL_sleep(s) /* disabled */
inikep83c76b42016-04-28 13:16:01 +020082# define UTIL_sleepMilli(milli) /* disabled */
inikep83c76b42016-04-28 13:16:01 +020083#endif
84
inikep31634032016-05-05 00:25:38 +020085
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010086/* *************************************
87* Constants
88***************************************/
89#define LIST_SIZE_INCREASE (8*1024)
90
91
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010092/*-****************************************
93* Compiler specifics
94******************************************/
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010095#if defined(__INTEL_COMPILER)
96# pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
97#endif
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010098#if defined(__GNUC__)
99# define UTIL_STATIC static __attribute__((unused))
100#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
101# define UTIL_STATIC static inline
102#elif defined(_MSC_VER)
103# define UTIL_STATIC static __inline
104#else
105# define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
106#endif
107
108
inikep31634032016-05-05 00:25:38 +0200109/*-****************************************
Yann Collet4299c272017-08-31 16:58:47 -0700110* Console log
111******************************************/
112static int g_utilDisplayLevel;
113#define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__)
114#define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
115
116
117/*-****************************************
inikep31634032016-05-05 00:25:38 +0200118* Time functions
119******************************************/
Przemyslaw Skibinski83775d92017-02-20 11:11:50 +0100120#if defined(_WIN32) /* Windows */
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700121 typedef LARGE_INTEGER UTIL_time_t;
Yann Colletc95c0c92017-09-12 18:12:46 -0700122 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
123 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
124 {
125 static LARGE_INTEGER ticksPerSecond = 0;
126 if (!ticksPerSecond) {
127 if (!QueryPerformanceFrequency(&ticksPerSecond))
128 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
129 }
130 return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
131 }
132 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
133 {
134 static LARGE_INTEGER ticksPerSecond = 0;
135 if (!ticksPerSecond) {
136 if (!QueryPerformanceFrequency(&ticksPerSecond))
137 UTIL_DISPLAYLEVEL(1, "ERROR: QueryPerformanceFrequency() failure\n");
138 }
139 return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
140 }
Przemyslaw Skibinskida4a0f32017-02-20 12:18:15 +0100141#elif defined(__APPLE__) && defined(__MACH__)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700142 #include <mach/mach_time.h>
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700143 typedef U64 UTIL_time_t;
Yann Colletc95c0c92017-09-12 18:12:46 -0700144 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
145 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
146 {
147 static mach_timebase_info_data_t rate;
148 static int init = 0;
149 if (!init) {
150 mach_timebase_info(&rate);
151 init = 1;
152 }
153 return (((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom))/1000ULL;
154 }
155 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
156 {
157 static mach_timebase_info_data_t rate;
158 static int init = 0;
159 if (!init) {
160 mach_timebase_info(&rate);
161 init = 1;
162 }
163 return ((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom);
164 }
Przemyslaw Skibinski1b593332017-02-21 07:33:45 +0100165#elif (PLATFORM_POSIX_VERSION >= 200112L)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700166 #include <time.h>
167 typedef struct timespec UTIL_freq_t;
168 typedef struct timespec UTIL_time_t;
Yann Colletc95c0c92017-09-12 18:12:46 -0700169 UTIL_STATIC UTIL_time_t UTIL_getTime(void)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700170 {
Yann Colletc95c0c92017-09-12 18:12:46 -0700171 UTIL_time_t time;
172 if (clock_gettime(CLOCK_MONOTONIC, &time))
173 UTIL_DISPLAYLEVEL(1, "ERROR: Failed to get time\n"); /* we could also exit() */
174 return time;
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700175 }
Yann Colletc95c0c92017-09-12 18:12:46 -0700176 UTIL_STATIC UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700177 {
178 UTIL_time_t diff;
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700179 if (end.tv_nsec < begin.tv_nsec) {
180 diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec;
181 diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec;
182 } else {
183 diff.tv_sec = end.tv_sec - begin.tv_sec;
184 diff.tv_nsec = end.tv_nsec - begin.tv_nsec;
185 }
186 return diff;
187 }
Yann Colletc95c0c92017-09-12 18:12:46 -0700188 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700189 {
Yann Colletc95c0c92017-09-12 18:12:46 -0700190 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700191 U64 micro = 0;
192 micro += 1000000ULL * diff.tv_sec;
193 micro += diff.tv_nsec / 1000ULL;
194 return micro;
195 }
Yann Colletc95c0c92017-09-12 18:12:46 -0700196 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700197 {
Yann Colletc95c0c92017-09-12 18:12:46 -0700198 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700199 U64 nano = 0;
200 nano += 1000000000ULL * diff.tv_sec;
201 nano += diff.tv_nsec;
202 return nano;
203 }
cyan49735fba09f2017-01-20 12:23:30 -0800204#else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
Nick Terrell6ab4d5e2017-09-12 13:09:08 -0700205 typedef clock_t UTIL_time_t;
Yann Colletc95c0c92017-09-12 18:12:46 -0700206 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return clock(); }
207 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
208 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
inikep83c76b42016-04-28 13:16:01 +0200209#endif
210
211
inikep83c76b42016-04-28 13:16:01 +0200212/* returns time span in microseconds */
Yann Colletc95c0c92017-09-12 18:12:46 -0700213UTIL_STATIC U64 UTIL_clockSpanMicro( UTIL_time_t clockStart )
inikep83c76b42016-04-28 13:16:01 +0200214{
Yann Colletc95c0c92017-09-12 18:12:46 -0700215 UTIL_time_t const clockEnd = UTIL_getTime();
216 return UTIL_getSpanTimeMicro(clockStart, clockEnd);
inikep83c76b42016-04-28 13:16:01 +0200217}
218
219
Yann Colletbc41c7f2017-09-12 19:32:26 -0700220UTIL_STATIC void UTIL_waitForNextTick(void)
inikep83c76b42016-04-28 13:16:01 +0200221{
Yann Colletc95c0c92017-09-12 18:12:46 -0700222 UTIL_time_t const clockStart = UTIL_getTime();
223 UTIL_time_t clockEnd;
Yann Collete162ace2016-05-20 11:24:35 +0200224 do {
Yann Colletc95c0c92017-09-12 18:12:46 -0700225 clockEnd = UTIL_getTime();
226 } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
inikep83c76b42016-04-28 13:16:01 +0200227}
228
229
inikep31634032016-05-05 00:25:38 +0200230
231/*-****************************************
232* File functions
233******************************************/
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100234#if defined(_MSC_VER)
Nick Terrell5152fb22017-03-29 18:51:58 -0700235 #define chmod _chmod
236 typedef struct __stat64 stat_t;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100237#else
238 typedef struct stat stat_t;
239#endif
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100240
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100241
242UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
243{
244 int res = 0;
245 struct utimbuf timebuf;
246
Nick Terrell5152fb22017-03-29 18:51:58 -0700247 timebuf.actime = time(NULL);
248 timebuf.modtime = statbuf->st_mtime;
249 res += utime(filename, &timebuf); /* set access and modification times */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +0100250
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100251#if !defined(_WIN32)
252 res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */
253#endif
254
255 res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100256
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100257 errno = 0;
258 return -res; /* number of errors is returned */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100259}
260
261
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100262UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100263{
264 int r;
265#if defined(_MSC_VER)
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100266 r = _stat64(infilename, statbuf);
267 if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100268#else
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100269 r = stat(infilename, statbuf);
270 if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100271#endif
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100272 return 1;
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100273}
274
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100275
Yann Collet6d4fef32017-05-17 18:36:15 -0700276UTIL_STATIC int UTIL_isRegularFile(const char* infilename)
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800277{
278 stat_t statbuf;
279 return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
280}
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100281
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100282
283UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
284{
285 int r;
286 stat_t statbuf;
287#if defined(_MSC_VER)
288 r = _stat64(infilename, &statbuf);
289 if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
290#else
291 r = stat(infilename, &statbuf);
292 if (!r && S_ISDIR(statbuf.st_mode)) return 1;
293#endif
294 return 0;
295}
296
Sean Purcell680e4e02017-03-23 11:52:09 -0700297UTIL_STATIC U32 UTIL_isLink(const char* infilename)
298{
299#if defined(_WIN32)
300 /* no symlinks on windows */
301 (void)infilename;
302#else
303 int r;
304 stat_t statbuf;
305 r = lstat(infilename, &statbuf);
306 if (!r && S_ISLNK(statbuf.st_mode)) return 1;
307#endif
308 return 0;
309}
310
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100311
inikep69fcd7c2016-04-28 12:23:33 +0200312UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
313{
314 int r;
315#if defined(_MSC_VER)
Przemyslaw Skibinski35bf23c2017-02-13 13:57:29 +0100316 struct __stat64 statbuf;
inikep69fcd7c2016-04-28 12:23:33 +0200317 r = _stat64(infilename, &statbuf);
318 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
ds7745f0c202017-02-10 18:37:57 +0100319#elif defined(__MINGW32__) && defined (__MSVCRT__)
320 struct _stati64 statbuf;
321 r = _stati64(infilename, &statbuf);
322 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
inikep69fcd7c2016-04-28 12:23:33 +0200323#else
324 struct stat statbuf;
325 r = stat(infilename, &statbuf);
326 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
327#endif
328 return (U64)statbuf.st_size;
329}
330
331
inikep55d047a2016-04-28 16:50:13 +0200332UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
333{
334 U64 total = 0;
335 unsigned n;
336 for (n=0; n<nbFiles; n++)
337 total += UTIL_getFileSize(fileNamesTable[n]);
338 return total;
339}
340
341
inikep61739312016-09-15 18:58:18 +0200342/*
343 * A modified version of realloc().
344 * If UTIL_realloc() fails the original block is freed.
345*/
346UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
347{
348 void *newptr = realloc(ptr, size);
349 if (newptr) return newptr;
350 free(ptr);
351 return NULL;
352}
353
inikep31634032016-05-05 00:25:38 +0200354#ifdef _WIN32
inikep9c22e572016-05-05 11:53:42 +0200355# define UTIL_HAS_CREATEFILELIST
inikep31634032016-05-05 00:25:38 +0200356
Sean Purcell680e4e02017-03-23 11:52:09 -0700357UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200358{
inikep1c5ba8a2016-09-13 13:13:10 +0200359 char* path;
360 int dirLength, fnameLength, pathLength, nbFiles = 0;
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100361 WIN32_FIND_DATAA cFile;
inikep31634032016-05-05 00:25:38 +0200362 HANDLE hFile;
363
inikep9f25fcf2016-09-13 16:38:54 +0200364 dirLength = (int)strlen(dirName);
inikep1c5ba8a2016-09-13 13:13:10 +0200365 path = (char*) malloc(dirLength + 3);
366 if (!path) return 0;
367
368 memcpy(path, dirName, dirLength);
369 path[dirLength] = '\\';
370 path[dirLength+1] = '*';
371 path[dirLength+2] = 0;
inikep31634032016-05-05 00:25:38 +0200372
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100373 hFile=FindFirstFileA(path, &cFile);
inikep31634032016-05-05 00:25:38 +0200374 if (hFile == INVALID_HANDLE_VALUE) {
Yann Collet4299c272017-08-31 16:58:47 -0700375 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
inikep31634032016-05-05 00:25:38 +0200376 return 0;
377 }
inikep1c5ba8a2016-09-13 13:13:10 +0200378 free(path);
inikep31634032016-05-05 00:25:38 +0200379
inikep4dbf7f42016-05-11 14:11:00 +0200380 do {
inikep9f25fcf2016-09-13 16:38:54 +0200381 fnameLength = (int)strlen(cFile.cFileName);
inikep1c5ba8a2016-09-13 13:13:10 +0200382 path = (char*) malloc(dirLength + fnameLength + 2);
383 if (!path) { FindClose(hFile); return 0; }
384 memcpy(path, dirName, dirLength);
385 path[dirLength] = '\\';
386 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
387 pathLength = dirLength+1+fnameLength;
388 path[pathLength] = 0;
inikep31634032016-05-05 00:25:38 +0200389 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
390 if (strcmp (cFile.cFileName, "..") == 0 ||
inikep4dbf7f42016-05-11 14:11:00 +0200391 strcmp (cFile.cFileName, ".") == 0) continue;
inikepe416e302016-08-24 17:32:09 +0200392
Sean Purcell680e4e02017-03-23 11:52:09 -0700393 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
inikep1c5ba8a2016-09-13 13:13:10 +0200394 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
inikep31634032016-05-05 00:25:38 +0200395 }
396 else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
inikep4dbf7f42016-05-11 14:11:00 +0200397 if (*bufStart + *pos + pathLength >= *bufEnd) {
398 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200399 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200400 *bufEnd = *bufStart + newListSize;
inikep1c5ba8a2016-09-13 13:13:10 +0200401 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
inikep4dbf7f42016-05-11 14:11:00 +0200402 }
403 if (*bufStart + *pos + pathLength < *bufEnd) {
404 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
405 *pos += pathLength + 1;
406 nbFiles++;
407 }
inikep31634032016-05-05 00:25:38 +0200408 }
inikep1c5ba8a2016-09-13 13:13:10 +0200409 free(path);
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100410 } while (FindNextFileA(hFile, &cFile));
inikep31634032016-05-05 00:25:38 +0200411
412 FindClose(hFile);
413 return nbFiles;
414}
415
Przemyslaw Skibinski0b372052016-12-16 17:12:23 +0100416#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
inikep9c22e572016-05-05 11:53:42 +0200417# define UTIL_HAS_CREATEFILELIST
inikep31634032016-05-05 00:25:38 +0200418# include <dirent.h> /* opendir, readdir */
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100419# include <string.h> /* strerror, memcpy */
inikep31634032016-05-05 00:25:38 +0200420
Sean Purcell680e4e02017-03-23 11:52:09 -0700421UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200422{
423 DIR *dir;
424 struct dirent *entry;
inikep1c5ba8a2016-09-13 13:13:10 +0200425 char* path;
426 int dirLength, fnameLength, pathLength, nbFiles = 0;
inikep31634032016-05-05 00:25:38 +0200427
inikep31634032016-05-05 00:25:38 +0200428 if (!(dir = opendir(dirName))) {
Yann Collet4299c272017-08-31 16:58:47 -0700429 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
inikep31634032016-05-05 00:25:38 +0200430 return 0;
431 }
Yann Collete162ace2016-05-20 11:24:35 +0200432
inikep9f25fcf2016-09-13 16:38:54 +0200433 dirLength = (int)strlen(dirName);
inikep7bc5c6b2016-07-26 11:07:37 +0200434 errno = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200435 while ((entry = readdir(dir)) != NULL) {
inikep0bd0fae2016-05-05 13:10:57 +0200436 if (strcmp (entry->d_name, "..") == 0 ||
437 strcmp (entry->d_name, ".") == 0) continue;
inikep9f25fcf2016-09-13 16:38:54 +0200438 fnameLength = (int)strlen(entry->d_name);
inikep1c5ba8a2016-09-13 13:13:10 +0200439 path = (char*) malloc(dirLength + fnameLength + 2);
440 if (!path) { closedir(dir); return 0; }
441 memcpy(path, dirName, dirLength);
Sean Purcell680e4e02017-03-23 11:52:09 -0700442
inikep1c5ba8a2016-09-13 13:13:10 +0200443 path[dirLength] = '/';
444 memcpy(path+dirLength+1, entry->d_name, fnameLength);
445 pathLength = dirLength+1+fnameLength;
446 path[pathLength] = 0;
447
Sean Purcell680e4e02017-03-23 11:52:09 -0700448 if (!followLinks && UTIL_isLink(path)) {
449 UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
450 continue;
451 }
452
inikep0bd0fae2016-05-05 13:10:57 +0200453 if (UTIL_isDirectory(path)) {
Sean Purcell680e4e02017-03-23 11:52:09 -0700454 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
inikep1c5ba8a2016-09-13 13:13:10 +0200455 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
inikep31634032016-05-05 00:25:38 +0200456 } else {
inikep4dbf7f42016-05-11 14:11:00 +0200457 if (*bufStart + *pos + pathLength >= *bufEnd) {
458 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200459 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200460 *bufEnd = *bufStart + newListSize;
inikep1c5ba8a2016-09-13 13:13:10 +0200461 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
inikep4dbf7f42016-05-11 14:11:00 +0200462 }
463 if (*bufStart + *pos + pathLength < *bufEnd) {
464 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
465 *pos += pathLength + 1;
466 nbFiles++;
467 }
inikep31634032016-05-05 00:25:38 +0200468 }
inikep1c5ba8a2016-09-13 13:13:10 +0200469 free(path);
inikepe416e302016-08-24 17:32:09 +0200470 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
inikep31634032016-05-05 00:25:38 +0200471 }
472
inikep7bc5c6b2016-07-26 11:07:37 +0200473 if (errno != 0) {
Yann Collet4299c272017-08-31 16:58:47 -0700474 UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s\n", dirName, strerror(errno));
inikep7bc5c6b2016-07-26 11:07:37 +0200475 free(*bufStart);
476 *bufStart = NULL;
477 }
inikep31634032016-05-05 00:25:38 +0200478 closedir(dir);
479 return nbFiles;
480}
481
482#else
483
Sean Purcell680e4e02017-03-23 11:52:09 -0700484UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200485{
inikep4dbf7f42016-05-11 14:11:00 +0200486 (void)bufStart; (void)bufEnd; (void)pos;
Yann Collet4299c272017-08-31 16:58:47 -0700487 UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
inikep31634032016-05-05 00:25:38 +0200488 return 0;
489}
490
inikepe416e302016-08-24 17:32:09 +0200491#endif /* #ifdef _WIN32 */
inikep31634032016-05-05 00:25:38 +0200492
Yann Collete162ace2016-05-20 11:24:35 +0200493/*
494 * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
inikep0bdb6a82016-05-13 10:52:02 +0200495 * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
496 * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
497 * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
498 */
Sean Purcell680e4e02017-03-23 11:52:09 -0700499UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb, int followLinks)
inikep31634032016-05-05 00:25:38 +0200500{
inikep4dbf7f42016-05-11 14:11:00 +0200501 size_t pos;
inikep0bdb6a82016-05-13 10:52:02 +0200502 unsigned i, nbFiles;
Yann Colletfda539f2016-12-12 01:03:23 +0100503 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
504 char* bufend = buf + LIST_SIZE_INCREASE;
inikep0bdb6a82016-05-13 10:52:02 +0200505 const char** fileTable;
inikep31634032016-05-05 00:25:38 +0200506
inikep0bdb6a82016-05-13 10:52:02 +0200507 if (!buf) return NULL;
inikep9c22e572016-05-05 11:53:42 +0200508
inikep0bdb6a82016-05-13 10:52:02 +0200509 for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
inikep957823f2016-05-25 15:30:55 +0200510 if (!UTIL_isDirectory(inputNames[i])) {
Yann Colletfda539f2016-12-12 01:03:23 +0100511 size_t const len = strlen(inputNames[i]);
inikep4dbf7f42016-05-11 14:11:00 +0200512 if (buf + pos + len >= bufend) {
513 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200514 buf = (char*)UTIL_realloc(buf, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200515 bufend = buf + newListSize;
inikep0bdb6a82016-05-13 10:52:02 +0200516 if (!buf) return NULL;
inikep4dbf7f42016-05-11 14:11:00 +0200517 }
518 if (buf + pos + len < bufend) {
519 strncpy(buf + pos, inputNames[i], bufend - (buf + pos));
520 pos += len + 1;
521 nbFiles++;
522 }
Yann Collet415251c2016-08-01 14:26:49 +0200523 } else {
Sean Purcell680e4e02017-03-23 11:52:09 -0700524 nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks);
inikep0bdb6a82016-05-13 10:52:02 +0200525 if (buf == NULL) return NULL;
Yann Collet415251c2016-08-01 14:26:49 +0200526 } }
inikep31634032016-05-05 00:25:38 +0200527
inikep0bdb6a82016-05-13 10:52:02 +0200528 if (nbFiles == 0) { free(buf); return NULL; }
inikep31634032016-05-05 00:25:38 +0200529
inikep0bdb6a82016-05-13 10:52:02 +0200530 fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
531 if (!fileTable) { free(buf); return NULL; }
inikep31634032016-05-05 00:25:38 +0200532
Yann Colletfda539f2016-12-12 01:03:23 +0100533 for (i=0, pos=0; i<nbFiles; i++) {
inikep0bdb6a82016-05-13 10:52:02 +0200534 fileTable[i] = buf + pos;
535 pos += strlen(fileTable[i]) + 1;
inikep31634032016-05-05 00:25:38 +0200536 }
537
inikep0bdb6a82016-05-13 10:52:02 +0200538 if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
539
540 *allocatedBuffer = buf;
541 *allocatedNamesNb = nbFiles;
542
543 return fileTable;
inikep31634032016-05-05 00:25:38 +0200544}
545
546
inikep0bdb6a82016-05-13 10:52:02 +0200547UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
inikep31634032016-05-05 00:25:38 +0200548{
inikep0bdb6a82016-05-13 10:52:02 +0200549 if (allocatedBuffer) free(allocatedBuffer);
550 if (filenameTable) free((void*)filenameTable);
inikep31634032016-05-05 00:25:38 +0200551}
552
Sean Purcellafa48512017-04-13 12:28:28 -0700553/* count the number of physical cores */
554#if defined(_WIN32) || defined(WIN32)
555
556#include <windows.h>
557
558typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
559
560UTIL_STATIC int UTIL_countPhysicalCores(void)
561{
Sean Purcelle4f32352017-04-13 16:34:28 -0700562 static int numPhysicalCores = 0;
Sean Purcellafa48512017-04-13 12:28:28 -0700563 if (numPhysicalCores != 0) return numPhysicalCores;
564
565 { LPFN_GLPI glpi;
566 BOOL done = FALSE;
567 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
568 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
569 DWORD returnLength = 0;
570 size_t byteOffset = 0;
571
572 glpi = (LPFN_GLPI)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
573 "GetLogicalProcessorInformation");
574
575 if (glpi == NULL) {
576 goto failed;
577 }
578
579 while(!done) {
580 DWORD rc = glpi(buffer, &returnLength);
581 if (FALSE == rc) {
582 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
583 if (buffer)
584 free(buffer);
585 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
586
587 if (buffer == NULL) {
588 perror("zstd");
589 exit(1);
590 }
591 } else {
592 /* some other error */
593 goto failed;
594 }
595 } else {
596 done = TRUE;
597 }
598 }
599
Sean Purcell3b6207d2017-04-13 14:03:56 -0700600 ptr = buffer;
Sean Purcellafa48512017-04-13 12:28:28 -0700601
Sean Purcell3b6207d2017-04-13 14:03:56 -0700602 while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
603
604 if (ptr->Relationship == RelationProcessorCore) {
Sean Purcellafa48512017-04-13 12:28:28 -0700605 numPhysicalCores++;
606 }
Sean Purcell3b6207d2017-04-13 14:03:56 -0700607
608 ptr++;
609 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
Sean Purcellafa48512017-04-13 12:28:28 -0700610 }
611
612 free(buffer);
613
614 return numPhysicalCores;
615 }
616
617failed:
618 /* try to fall back on GetSystemInfo */
Sean Purcell3b6207d2017-04-13 14:03:56 -0700619 { SYSTEM_INFO sysinfo;
620 GetSystemInfo(&sysinfo);
621 numPhysicalCores = sysinfo.dwNumberOfProcessors;
622 if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
623 }
Sean Purcellafa48512017-04-13 12:28:28 -0700624 return numPhysicalCores;
625}
626
627#elif defined(__APPLE__)
628
629#include <sys/sysctl.h>
630
631/* Use apple-provided syscall
632 * see: man 3 sysctl */
633UTIL_STATIC int UTIL_countPhysicalCores(void)
634{
Sean Purcelle4f32352017-04-13 16:34:28 -0700635 static S32 numPhysicalCores = 0; /* apple specifies int32_t */
Sean Purcellafa48512017-04-13 12:28:28 -0700636 if (numPhysicalCores != 0) return numPhysicalCores;
637
Sean Purcellf876f122017-04-13 12:33:45 -0700638 { size_t size = sizeof(S32);
639 int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
Sean Purcellafa48512017-04-13 12:28:28 -0700640 if (ret != 0) {
641 if (errno == ENOENT) {
642 /* entry not present, fall back on 1 */
643 numPhysicalCores = 1;
644 } else {
645 perror("zstd: can't get number of physical cpus");
646 exit(1);
647 }
648 }
649
650 return numPhysicalCores;
651 }
652}
653
654#elif defined(__linux__)
655
656/* parse /proc/cpuinfo
657 * siblings / cpu cores should give hyperthreading ratio
658 * otherwise fall back on sysconf */
659UTIL_STATIC int UTIL_countPhysicalCores(void)
660{
Sean Purcelle4f32352017-04-13 16:34:28 -0700661 static int numPhysicalCores = 0;
Sean Purcellafa48512017-04-13 12:28:28 -0700662
663 if (numPhysicalCores != 0) return numPhysicalCores;
664
Sean Purcell9227aae2017-04-13 14:06:40 -0700665 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
Sean Purcellafa48512017-04-13 12:28:28 -0700666 if (numPhysicalCores == -1) {
667 /* value not queryable, fall back on 1 */
668 return numPhysicalCores = 1;
669 }
670
671 /* try to determine if there's hyperthreading */
672 { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
Yann Collet46ac9ad2017-05-15 18:15:08 -0700673#define BUF_SIZE 80
Sean Purcellafa48512017-04-13 12:28:28 -0700674 char buff[BUF_SIZE];
675
676 int siblings = 0;
677 int cpu_cores = 0;
678 int ratio = 1;
679
680 if (cpuinfo == NULL) {
681 /* fall back on the sysconf value */
682 return numPhysicalCores;
683 }
684
685 /* assume the cpu cores/siblings values will be constant across all
686 * present processors */
687 while (!feof(cpuinfo)) {
688 if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
689 if (strncmp(buff, "siblings", 8) == 0) {
690 const char* const sep = strchr(buff, ':');
691 if (*sep == '\0') {
692 /* formatting was broken? */
693 goto failed;
694 }
695
696 siblings = atoi(sep + 1);
697 }
698 if (strncmp(buff, "cpu cores", 9) == 0) {
699 const char* const sep = strchr(buff, ':');
700 if (*sep == '\0') {
701 /* formatting was broken? */
702 goto failed;
703 }
704
705 cpu_cores = atoi(sep + 1);
706 }
707 } else if (ferror(cpuinfo)) {
708 /* fall back on the sysconf value */
709 goto failed;
710 }
711 }
712 if (siblings && cpu_cores) {
713 ratio = siblings / cpu_cores;
714 }
715failed:
716 fclose(cpuinfo);
717 return numPhysicalCores = numPhysicalCores / ratio;
718 }
719}
720
Baptiste Daroussin7dd14d02017-04-15 16:25:08 +0200721#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
722
723/* Use apple-provided syscall
724 * see: man 3 sysctl */
725UTIL_STATIC int UTIL_countPhysicalCores(void)
726{
727 static int numPhysicalCores = 0;
728
729 if (numPhysicalCores != 0) return numPhysicalCores;
730
731 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
732 if (numPhysicalCores == -1) {
733 /* value not queryable, fall back on 1 */
734 return numPhysicalCores = 1;
735 }
736 return numPhysicalCores;
737}
738
Sean Purcellafa48512017-04-13 12:28:28 -0700739#else
740
741UTIL_STATIC int UTIL_countPhysicalCores(void)
742{
743 /* assume 1 */
744 return 1;
745}
746
747#endif
inikep31634032016-05-05 00:25:38 +0200748
inikep69fcd7c2016-04-28 12:23:33 +0200749#if defined (__cplusplus)
750}
751#endif
752
753#endif /* UTIL_H_MODULE */