/src/zstd/programs/util.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
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 | | /*-**************************************** |
12 | | * Dependencies |
13 | | ******************************************/ |
14 | | #include "util.h" /* note : ensure that platform.h is included first ! */ |
15 | | #include <stdlib.h> /* malloc, realloc, free */ |
16 | | #include <stdio.h> /* fprintf */ |
17 | | #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */ |
18 | | #include <errno.h> |
19 | | #include <assert.h> |
20 | | |
21 | | #if defined(__FreeBSD__) |
22 | | #include <sys/param.h> /* __FreeBSD_version */ |
23 | | #endif /* #ifdef __FreeBSD__ */ |
24 | | |
25 | | #if defined(_WIN32) |
26 | | # include <sys/utime.h> /* utime */ |
27 | | # include <io.h> /* _chmod */ |
28 | | # define ZSTD_USE_UTIMENSAT 0 |
29 | | #else |
30 | | # include <unistd.h> /* chown, stat */ |
31 | | # include <sys/stat.h> /* utimensat, st_mtime */ |
32 | | # if (PLATFORM_POSIX_VERSION >= 200809L && defined(st_mtime)) \ |
33 | | || (defined(__FreeBSD__) && __FreeBSD_version >= 1100056) |
34 | | # define ZSTD_USE_UTIMENSAT 1 |
35 | | # else |
36 | | # define ZSTD_USE_UTIMENSAT 0 |
37 | | # endif |
38 | | # if ZSTD_USE_UTIMENSAT |
39 | | # include <fcntl.h> /* AT_FDCWD */ |
40 | | # else |
41 | | # include <utime.h> /* utime */ |
42 | | # endif |
43 | | #endif |
44 | | |
45 | | #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__) |
46 | | #include <direct.h> /* needed for _mkdir in windows */ |
47 | | #endif |
48 | | |
49 | | #if defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ |
50 | | # include <dirent.h> /* opendir, readdir */ |
51 | | # include <string.h> /* strerror, memcpy */ |
52 | | #endif /* #ifdef _WIN32 */ |
53 | | |
54 | | /*-**************************************** |
55 | | * Internal Macros |
56 | | ******************************************/ |
57 | | |
58 | | /* CONTROL is almost like an assert(), but is never disabled. |
59 | | * It's designed for failures that may happen rarely, |
60 | | * but we don't want to maintain a specific error code path for them, |
61 | | * such as a malloc() returning NULL for example. |
62 | | * Since it's always active, this macro can trigger side effects. |
63 | | */ |
64 | 0 | #define CONTROL(c) { \ |
65 | 0 | if (!(c)) { \ |
66 | 0 | UTIL_DISPLAYLEVEL(1, "Error : %s, %i : %s", \ |
67 | 0 | __FILE__, __LINE__, #c); \ |
68 | 0 | exit(1); \ |
69 | 0 | } } |
70 | | |
71 | | /* console log */ |
72 | 0 | #define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__) |
73 | 0 | #define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } } |
74 | | |
75 | | static int g_traceDepth = 0; |
76 | | int g_traceFileStat = 0; |
77 | | |
78 | | #define UTIL_TRACE_CALL(...) \ |
79 | 0 | { \ |
80 | 0 | if (g_traceFileStat) { \ |
81 | 0 | UTIL_DISPLAY("Trace:FileStat: %*s> ", g_traceDepth, ""); \ |
82 | 0 | UTIL_DISPLAY(__VA_ARGS__); \ |
83 | 0 | UTIL_DISPLAY("\n"); \ |
84 | 0 | ++g_traceDepth; \ |
85 | 0 | } \ |
86 | 0 | } |
87 | | |
88 | | #define UTIL_TRACE_RET(ret) \ |
89 | 0 | { \ |
90 | 0 | if (g_traceFileStat) { \ |
91 | 0 | --g_traceDepth; \ |
92 | 0 | UTIL_DISPLAY("Trace:FileStat: %*s< %d\n", g_traceDepth, "", (ret)); \ |
93 | 0 | } \ |
94 | 0 | } |
95 | | |
96 | | /* A modified version of realloc(). |
97 | | * If UTIL_realloc() fails the original block is freed. |
98 | | */ |
99 | | UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size) |
100 | 0 | { |
101 | 0 | void *newptr = realloc(ptr, size); |
102 | 0 | if (newptr) return newptr; |
103 | 0 | free(ptr); |
104 | 0 | return NULL; |
105 | 0 | } |
106 | | |
107 | | #if defined(_MSC_VER) |
108 | | #define chmod _chmod |
109 | | #endif |
110 | | |
111 | | #ifndef ZSTD_HAVE_FCHMOD |
112 | | #if PLATFORM_POSIX_VERSION >= 199309L |
113 | | #define ZSTD_HAVE_FCHMOD |
114 | | #endif |
115 | | #endif |
116 | | |
117 | | #ifndef ZSTD_HAVE_FCHOWN |
118 | | #if PLATFORM_POSIX_VERSION >= 200809L |
119 | | #define ZSTD_HAVE_FCHOWN |
120 | | #endif |
121 | | #endif |
122 | | |
123 | | /*-**************************************** |
124 | | * Console log |
125 | | ******************************************/ |
126 | | int g_utilDisplayLevel; |
127 | | |
128 | | int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg, |
129 | 0 | const char* acceptableLetters, int hasStdinInput) { |
130 | 0 | int ch, result; |
131 | |
|
132 | 0 | if (hasStdinInput) { |
133 | 0 | UTIL_DISPLAY("stdin is an input - not proceeding.\n"); |
134 | 0 | return 1; |
135 | 0 | } |
136 | | |
137 | 0 | UTIL_DISPLAY("%s", prompt); |
138 | 0 | ch = getchar(); |
139 | 0 | result = 0; |
140 | 0 | if (strchr(acceptableLetters, ch) == NULL) { |
141 | 0 | UTIL_DISPLAY("%s \n", abortMsg); |
142 | 0 | result = 1; |
143 | 0 | } |
144 | | /* flush the rest */ |
145 | 0 | while ((ch!=EOF) && (ch!='\n')) |
146 | 0 | ch = getchar(); |
147 | 0 | return result; |
148 | 0 | } |
149 | | |
150 | | |
151 | | /*-************************************* |
152 | | * Constants |
153 | | ***************************************/ |
154 | 0 | #define KB * (1 << 10) |
155 | 0 | #define LIST_SIZE_INCREASE (8 KB) |
156 | 0 | #define MAX_FILE_OF_FILE_NAMES_SIZE (1<<20)*50 |
157 | | |
158 | | |
159 | | /*-************************************* |
160 | | * Functions |
161 | | ***************************************/ |
162 | | |
163 | | void UTIL_traceFileStat(void) |
164 | 0 | { |
165 | 0 | g_traceFileStat = 1; |
166 | 0 | } |
167 | | |
168 | | int UTIL_fstat(const int fd, const char* filename, stat_t* statbuf) |
169 | 0 | { |
170 | 0 | int ret; |
171 | 0 | UTIL_TRACE_CALL("UTIL_stat(%d, %s)", fd, filename); |
172 | | #if defined(_MSC_VER) |
173 | | if (fd >= 0) { |
174 | | ret = !_fstat64(fd, statbuf); |
175 | | } else { |
176 | | ret = !_stat64(filename, statbuf); |
177 | | } |
178 | | #elif defined(__MINGW32__) && defined (__MSVCRT__) |
179 | | if (fd >= 0) { |
180 | | ret = !_fstati64(fd, statbuf); |
181 | | } else { |
182 | | ret = !_stati64(filename, statbuf); |
183 | | } |
184 | | #else |
185 | 0 | if (fd >= 0) { |
186 | 0 | ret = !fstat(fd, statbuf); |
187 | 0 | } else { |
188 | 0 | ret = !stat(filename, statbuf); |
189 | 0 | } |
190 | 0 | #endif |
191 | 0 | UTIL_TRACE_RET(ret); |
192 | 0 | return ret; |
193 | 0 | } |
194 | | |
195 | | int UTIL_stat(const char* filename, stat_t* statbuf) |
196 | 0 | { |
197 | 0 | return UTIL_fstat(-1, filename, statbuf); |
198 | 0 | } |
199 | | |
200 | | int UTIL_isFdRegularFile(int fd) |
201 | 0 | { |
202 | 0 | stat_t statbuf; |
203 | 0 | int ret; |
204 | 0 | UTIL_TRACE_CALL("UTIL_isFdRegularFile(%d)", fd); |
205 | 0 | ret = fd >= 0 && UTIL_fstat(fd, "", &statbuf) && UTIL_isRegularFileStat(&statbuf); |
206 | 0 | UTIL_TRACE_RET(ret); |
207 | 0 | return ret; |
208 | 0 | } |
209 | | |
210 | | int UTIL_isRegularFile(const char* infilename) |
211 | 0 | { |
212 | 0 | stat_t statbuf; |
213 | 0 | int ret; |
214 | 0 | UTIL_TRACE_CALL("UTIL_isRegularFile(%s)", infilename); |
215 | 0 | ret = UTIL_stat(infilename, &statbuf) && UTIL_isRegularFileStat(&statbuf); |
216 | 0 | UTIL_TRACE_RET(ret); |
217 | 0 | return ret; |
218 | 0 | } |
219 | | |
220 | | int UTIL_isRegularFileStat(const stat_t* statbuf) |
221 | 0 | { |
222 | | #if defined(_MSC_VER) |
223 | | return (statbuf->st_mode & S_IFREG) != 0; |
224 | | #else |
225 | 0 | return S_ISREG(statbuf->st_mode) != 0; |
226 | 0 | #endif |
227 | 0 | } |
228 | | |
229 | | /* like chmod, but avoid changing permission of /dev/null */ |
230 | | int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions) |
231 | 0 | { |
232 | 0 | return UTIL_fchmod(-1, filename, statbuf, permissions); |
233 | 0 | } |
234 | | |
235 | | int UTIL_fchmod(const int fd, char const* filename, const stat_t* statbuf, mode_t permissions) |
236 | 0 | { |
237 | 0 | stat_t localStatBuf; |
238 | 0 | UTIL_TRACE_CALL("UTIL_chmod(%s, %#4o)", filename, (unsigned)permissions); |
239 | 0 | if (statbuf == NULL) { |
240 | 0 | if (!UTIL_fstat(fd, filename, &localStatBuf)) { |
241 | 0 | UTIL_TRACE_RET(0); |
242 | 0 | return 0; |
243 | 0 | } |
244 | 0 | statbuf = &localStatBuf; |
245 | 0 | } |
246 | 0 | if (!UTIL_isRegularFileStat(statbuf)) { |
247 | 0 | UTIL_TRACE_RET(0); |
248 | 0 | return 0; /* pretend success, but don't change anything */ |
249 | 0 | } |
250 | 0 | #ifdef ZSTD_HAVE_FCHMOD |
251 | 0 | if (fd >= 0) { |
252 | 0 | int ret; |
253 | 0 | UTIL_TRACE_CALL("fchmod"); |
254 | 0 | ret = fchmod(fd, permissions); |
255 | 0 | UTIL_TRACE_RET(ret); |
256 | 0 | UTIL_TRACE_RET(ret); |
257 | 0 | return ret; |
258 | 0 | } else |
259 | 0 | #endif |
260 | 0 | { |
261 | 0 | int ret; |
262 | 0 | UTIL_TRACE_CALL("chmod"); |
263 | 0 | ret = chmod(filename, permissions); |
264 | 0 | UTIL_TRACE_RET(ret); |
265 | 0 | UTIL_TRACE_RET(ret); |
266 | 0 | return ret; |
267 | 0 | } |
268 | 0 | } |
269 | | |
270 | | /* set access and modification times */ |
271 | | int UTIL_utime(const char* filename, const stat_t *statbuf) |
272 | 0 | { |
273 | 0 | int ret; |
274 | 0 | UTIL_TRACE_CALL("UTIL_utime(%s)", filename); |
275 | | /* We check that st_mtime is a macro here in order to give us confidence |
276 | | * that struct stat has a struct timespec st_mtim member. We need this |
277 | | * check because there are some platforms that claim to be POSIX 2008 |
278 | | * compliant but which do not have st_mtim... */ |
279 | | /* FreeBSD has implemented POSIX 2008 for a long time but still only |
280 | | * advertises support for POSIX 2001. They have a version macro that |
281 | | * lets us safely gate them in. |
282 | | * See https://docs.freebsd.org/en/books/porters-handbook/versions/. |
283 | | */ |
284 | 0 | #if ZSTD_USE_UTIMENSAT |
285 | 0 | { |
286 | | /* (atime, mtime) */ |
287 | 0 | struct timespec timebuf[2] = { {0, UTIME_NOW} }; |
288 | 0 | timebuf[1] = statbuf->st_mtim; |
289 | 0 | ret = utimensat(AT_FDCWD, filename, timebuf, 0); |
290 | 0 | } |
291 | | #else |
292 | | { |
293 | | struct utimbuf timebuf; |
294 | | timebuf.actime = time(NULL); |
295 | | timebuf.modtime = statbuf->st_mtime; |
296 | | ret = utime(filename, &timebuf); |
297 | | } |
298 | | #endif |
299 | 0 | errno = 0; |
300 | 0 | UTIL_TRACE_RET(ret); |
301 | 0 | return ret; |
302 | 0 | } |
303 | | |
304 | | int UTIL_setFileStat(const char *filename, const stat_t *statbuf) |
305 | 0 | { |
306 | 0 | return UTIL_setFDStat(-1, filename, statbuf); |
307 | 0 | } |
308 | | |
309 | | int UTIL_setFDStat(const int fd, const char *filename, const stat_t *statbuf) |
310 | 0 | { |
311 | 0 | int res = 0; |
312 | 0 | stat_t curStatBuf; |
313 | 0 | UTIL_TRACE_CALL("UTIL_setFileStat(%d, %s)", fd, filename); |
314 | |
|
315 | 0 | if (!UTIL_fstat(fd, filename, &curStatBuf) || !UTIL_isRegularFileStat(&curStatBuf)) { |
316 | 0 | UTIL_TRACE_RET(-1); |
317 | 0 | return -1; |
318 | 0 | } |
319 | | |
320 | | /* Mimic gzip's behavior: |
321 | | * |
322 | | * "Change the group first, then the permissions, then the owner. |
323 | | * That way, the permissions will be correct on systems that allow |
324 | | * users to give away files, without introducing a security hole. |
325 | | * Security depends on permissions not containing the setuid or |
326 | | * setgid bits." */ |
327 | | |
328 | 0 | #if !defined(_WIN32) |
329 | 0 | #ifdef ZSTD_HAVE_FCHOWN |
330 | 0 | if (fd >= 0) { |
331 | 0 | res += fchown(fd, -1, statbuf->st_gid); /* Apply group ownership */ |
332 | 0 | } else |
333 | 0 | #endif |
334 | 0 | { |
335 | 0 | res += chown(filename, -1, statbuf->st_gid); /* Apply group ownership */ |
336 | 0 | } |
337 | 0 | #endif |
338 | |
|
339 | 0 | res += UTIL_fchmod(fd, filename, &curStatBuf, statbuf->st_mode & 0777); /* Copy file permissions */ |
340 | |
|
341 | 0 | #if !defined(_WIN32) |
342 | 0 | #ifdef ZSTD_HAVE_FCHOWN |
343 | 0 | if (fd >= 0) { |
344 | 0 | res += fchown(fd, statbuf->st_uid, -1); /* Apply user ownership */ |
345 | 0 | } else |
346 | 0 | #endif |
347 | 0 | { |
348 | 0 | res += chown(filename, statbuf->st_uid, -1); /* Apply user ownership */ |
349 | 0 | } |
350 | 0 | #endif |
351 | |
|
352 | 0 | errno = 0; |
353 | 0 | UTIL_TRACE_RET(-res); |
354 | 0 | return -res; /* number of errors is returned */ |
355 | 0 | } |
356 | | |
357 | | int UTIL_isDirectory(const char* infilename) |
358 | 0 | { |
359 | 0 | stat_t statbuf; |
360 | 0 | int ret; |
361 | 0 | UTIL_TRACE_CALL("UTIL_isDirectory(%s)", infilename); |
362 | 0 | ret = UTIL_stat(infilename, &statbuf) && UTIL_isDirectoryStat(&statbuf); |
363 | 0 | UTIL_TRACE_RET(ret); |
364 | 0 | return ret; |
365 | 0 | } |
366 | | |
367 | | int UTIL_isDirectoryStat(const stat_t* statbuf) |
368 | 0 | { |
369 | 0 | int ret; |
370 | 0 | UTIL_TRACE_CALL("UTIL_isDirectoryStat()"); |
371 | | #if defined(_MSC_VER) |
372 | | ret = (statbuf->st_mode & _S_IFDIR) != 0; |
373 | | #else |
374 | 0 | ret = S_ISDIR(statbuf->st_mode) != 0; |
375 | 0 | #endif |
376 | 0 | UTIL_TRACE_RET(ret); |
377 | 0 | return ret; |
378 | 0 | } |
379 | | |
380 | 0 | int UTIL_compareStr(const void *p1, const void *p2) { |
381 | 0 | return strcmp(* (char * const *) p1, * (char * const *) p2); |
382 | 0 | } |
383 | | |
384 | | int UTIL_isSameFile(const char* fName1, const char* fName2) |
385 | 0 | { |
386 | 0 | int ret; |
387 | 0 | assert(fName1 != NULL); assert(fName2 != NULL); |
388 | 0 | UTIL_TRACE_CALL("UTIL_isSameFile(%s, %s)", fName1, fName2); |
389 | | #if defined(_MSC_VER) || defined(_WIN32) |
390 | | /* note : Visual does not support file identification by inode. |
391 | | * inode does not work on Windows, even with a posix layer, like msys2. |
392 | | * The following work-around is limited to detecting exact name repetition only, |
393 | | * aka `filename` is considered different from `subdir/../filename` */ |
394 | | ret = !strcmp(fName1, fName2); |
395 | | #else |
396 | 0 | { stat_t file1Stat; |
397 | 0 | stat_t file2Stat; |
398 | 0 | ret = UTIL_stat(fName1, &file1Stat) |
399 | 0 | && UTIL_stat(fName2, &file2Stat) |
400 | 0 | && UTIL_isSameFileStat(fName1, fName2, &file1Stat, &file2Stat); |
401 | 0 | } |
402 | 0 | #endif |
403 | 0 | UTIL_TRACE_RET(ret); |
404 | 0 | return ret; |
405 | 0 | } |
406 | | |
407 | | int UTIL_isSameFileStat( |
408 | | const char* fName1, const char* fName2, |
409 | | const stat_t* file1Stat, const stat_t* file2Stat) |
410 | 0 | { |
411 | 0 | int ret; |
412 | 0 | assert(fName1 != NULL); assert(fName2 != NULL); |
413 | 0 | UTIL_TRACE_CALL("UTIL_isSameFileStat(%s, %s)", fName1, fName2); |
414 | | #if defined(_MSC_VER) || defined(_WIN32) |
415 | | /* note : Visual does not support file identification by inode. |
416 | | * inode does not work on Windows, even with a posix layer, like msys2. |
417 | | * The following work-around is limited to detecting exact name repetition only, |
418 | | * aka `filename` is considered different from `subdir/../filename` */ |
419 | | (void)file1Stat; |
420 | | (void)file2Stat; |
421 | | ret = !strcmp(fName1, fName2); |
422 | | #else |
423 | 0 | { |
424 | 0 | ret = (file1Stat->st_dev == file2Stat->st_dev) |
425 | 0 | && (file1Stat->st_ino == file2Stat->st_ino); |
426 | 0 | } |
427 | 0 | #endif |
428 | 0 | UTIL_TRACE_RET(ret); |
429 | 0 | return ret; |
430 | 0 | } |
431 | | |
432 | | /* UTIL_isFIFO : distinguish named pipes */ |
433 | | int UTIL_isFIFO(const char* infilename) |
434 | 0 | { |
435 | 0 | UTIL_TRACE_CALL("UTIL_isFIFO(%s)", infilename); |
436 | | /* macro guards, as defined in : https://linux.die.net/man/2/lstat */ |
437 | 0 | #if PLATFORM_POSIX_VERSION >= 200112L |
438 | 0 | { |
439 | 0 | stat_t statbuf; |
440 | 0 | if (UTIL_stat(infilename, &statbuf) && UTIL_isFIFOStat(&statbuf)) { |
441 | 0 | UTIL_TRACE_RET(1); |
442 | 0 | return 1; |
443 | 0 | } |
444 | 0 | } |
445 | 0 | #endif |
446 | 0 | (void)infilename; |
447 | 0 | UTIL_TRACE_RET(0); |
448 | 0 | return 0; |
449 | 0 | } |
450 | | |
451 | | /* UTIL_isFIFO : distinguish named pipes */ |
452 | | int UTIL_isFIFOStat(const stat_t* statbuf) |
453 | 0 | { |
454 | | /* macro guards, as defined in : https://linux.die.net/man/2/lstat */ |
455 | 0 | #if PLATFORM_POSIX_VERSION >= 200112L |
456 | 0 | if (S_ISFIFO(statbuf->st_mode)) return 1; |
457 | 0 | #endif |
458 | 0 | (void)statbuf; |
459 | 0 | return 0; |
460 | 0 | } |
461 | | |
462 | | /* process substitution */ |
463 | | int UTIL_isFileDescriptorPipe(const char* filename) |
464 | 0 | { |
465 | 0 | UTIL_TRACE_CALL("UTIL_isFileDescriptorPipe(%s)", filename); |
466 | | /* Check if the filename is a /dev/fd/ path which indicates a file descriptor */ |
467 | 0 | if (filename[0] == '/' && strncmp(filename, "/dev/fd/", 8) == 0) { |
468 | 0 | UTIL_TRACE_RET(1); |
469 | 0 | return 1; |
470 | 0 | } |
471 | | |
472 | | /* Check for alternative process substitution formats on different systems */ |
473 | 0 | if (filename[0] == '/' && strncmp(filename, "/proc/self/fd/", 14) == 0) { |
474 | 0 | UTIL_TRACE_RET(1); |
475 | 0 | return 1; |
476 | 0 | } |
477 | | |
478 | 0 | UTIL_TRACE_RET(0); |
479 | 0 | return 0; /* Not recognized as a file descriptor pipe */ |
480 | 0 | } |
481 | | |
482 | | /* UTIL_isBlockDevStat : distinguish named pipes */ |
483 | | int UTIL_isBlockDevStat(const stat_t* statbuf) |
484 | 0 | { |
485 | | /* macro guards, as defined in : https://linux.die.net/man/2/lstat */ |
486 | 0 | #if PLATFORM_POSIX_VERSION >= 200112L |
487 | 0 | if (S_ISBLK(statbuf->st_mode)) return 1; |
488 | 0 | #endif |
489 | 0 | (void)statbuf; |
490 | 0 | return 0; |
491 | 0 | } |
492 | | |
493 | | int UTIL_isLink(const char* infilename) |
494 | 0 | { |
495 | 0 | UTIL_TRACE_CALL("UTIL_isLink(%s)", infilename); |
496 | | /* macro guards, as defined in : https://linux.die.net/man/2/lstat */ |
497 | 0 | #if PLATFORM_POSIX_VERSION >= 200112L |
498 | 0 | { |
499 | 0 | stat_t statbuf; |
500 | 0 | int const r = lstat(infilename, &statbuf); |
501 | 0 | if (!r && S_ISLNK(statbuf.st_mode)) { |
502 | 0 | UTIL_TRACE_RET(1); |
503 | 0 | return 1; |
504 | 0 | } |
505 | 0 | } |
506 | 0 | #endif |
507 | 0 | (void)infilename; |
508 | 0 | UTIL_TRACE_RET(0); |
509 | 0 | return 0; |
510 | 0 | } |
511 | | |
512 | | static int g_fakeStdinIsConsole = 0; |
513 | | static int g_fakeStderrIsConsole = 0; |
514 | | static int g_fakeStdoutIsConsole = 0; |
515 | | |
516 | | int UTIL_isConsole(FILE* file) |
517 | 0 | { |
518 | 0 | int ret; |
519 | 0 | UTIL_TRACE_CALL("UTIL_isConsole(%d)", fileno(file)); |
520 | 0 | if (file == stdin && g_fakeStdinIsConsole) |
521 | 0 | ret = 1; |
522 | 0 | else if (file == stderr && g_fakeStderrIsConsole) |
523 | 0 | ret = 1; |
524 | 0 | else if (file == stdout && g_fakeStdoutIsConsole) |
525 | 0 | ret = 1; |
526 | 0 | else |
527 | 0 | ret = IS_CONSOLE(file); |
528 | 0 | UTIL_TRACE_RET(ret); |
529 | 0 | return ret; |
530 | 0 | } |
531 | | |
532 | | void UTIL_fakeStdinIsConsole(void) |
533 | 0 | { |
534 | 0 | g_fakeStdinIsConsole = 1; |
535 | 0 | } |
536 | | void UTIL_fakeStdoutIsConsole(void) |
537 | 0 | { |
538 | 0 | g_fakeStdoutIsConsole = 1; |
539 | 0 | } |
540 | | void UTIL_fakeStderrIsConsole(void) |
541 | 0 | { |
542 | 0 | g_fakeStderrIsConsole = 1; |
543 | 0 | } |
544 | | |
545 | | U64 UTIL_getFileSize(const char* infilename) |
546 | 0 | { |
547 | 0 | stat_t statbuf; |
548 | 0 | UTIL_TRACE_CALL("UTIL_getFileSize(%s)", infilename); |
549 | 0 | if (!UTIL_stat(infilename, &statbuf)) { |
550 | 0 | UTIL_TRACE_RET(-1); |
551 | 0 | return UTIL_FILESIZE_UNKNOWN; |
552 | 0 | } |
553 | 0 | { |
554 | 0 | U64 const size = UTIL_getFileSizeStat(&statbuf); |
555 | 0 | UTIL_TRACE_RET((int)size); |
556 | 0 | return size; |
557 | 0 | } |
558 | 0 | } |
559 | | |
560 | | U64 UTIL_getFileSizeStat(const stat_t* statbuf) |
561 | 0 | { |
562 | 0 | if (!UTIL_isRegularFileStat(statbuf)) return UTIL_FILESIZE_UNKNOWN; |
563 | | #if defined(_MSC_VER) |
564 | | if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN; |
565 | | #elif defined(__MINGW32__) && defined (__MSVCRT__) |
566 | | if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN; |
567 | | #else |
568 | 0 | if (!S_ISREG(statbuf->st_mode)) return UTIL_FILESIZE_UNKNOWN; |
569 | 0 | #endif |
570 | 0 | return (U64)statbuf->st_size; |
571 | 0 | } |
572 | | |
573 | | UTIL_HumanReadableSize_t UTIL_makeHumanReadableSize(U64 size) |
574 | 0 | { |
575 | 0 | UTIL_HumanReadableSize_t hrs; |
576 | |
|
577 | 0 | if (g_utilDisplayLevel > 3) { |
578 | | /* In verbose mode, do not scale sizes down, except in the case of |
579 | | * values that exceed the integral precision of a double. */ |
580 | 0 | if (size >= (1ull << 53)) { |
581 | 0 | hrs.value = (double)size / (1ull << 20); |
582 | 0 | hrs.suffix = " MiB"; |
583 | | /* At worst, a double representation of a maximal size will be |
584 | | * accurate to better than tens of kilobytes. */ |
585 | 0 | hrs.precision = 2; |
586 | 0 | } else { |
587 | 0 | hrs.value = (double)size; |
588 | 0 | hrs.suffix = " B"; |
589 | 0 | hrs.precision = 0; |
590 | 0 | } |
591 | 0 | } else { |
592 | | /* In regular mode, scale sizes down and use suffixes. */ |
593 | 0 | if (size >= (1ull << 60)) { |
594 | 0 | hrs.value = (double)size / (1ull << 60); |
595 | 0 | hrs.suffix = " EiB"; |
596 | 0 | } else if (size >= (1ull << 50)) { |
597 | 0 | hrs.value = (double)size / (1ull << 50); |
598 | 0 | hrs.suffix = " PiB"; |
599 | 0 | } else if (size >= (1ull << 40)) { |
600 | 0 | hrs.value = (double)size / (1ull << 40); |
601 | 0 | hrs.suffix = " TiB"; |
602 | 0 | } else if (size >= (1ull << 30)) { |
603 | 0 | hrs.value = (double)size / (1ull << 30); |
604 | 0 | hrs.suffix = " GiB"; |
605 | 0 | } else if (size >= (1ull << 20)) { |
606 | 0 | hrs.value = (double)size / (1ull << 20); |
607 | 0 | hrs.suffix = " MiB"; |
608 | 0 | } else if (size >= (1ull << 10)) { |
609 | 0 | hrs.value = (double)size / (1ull << 10); |
610 | 0 | hrs.suffix = " KiB"; |
611 | 0 | } else { |
612 | 0 | hrs.value = (double)size; |
613 | 0 | hrs.suffix = " B"; |
614 | 0 | } |
615 | |
|
616 | 0 | if (hrs.value >= 100 || (U64)hrs.value == size) { |
617 | 0 | hrs.precision = 0; |
618 | 0 | } else if (hrs.value >= 10) { |
619 | 0 | hrs.precision = 1; |
620 | 0 | } else if (hrs.value > 1) { |
621 | 0 | hrs.precision = 2; |
622 | 0 | } else { |
623 | 0 | hrs.precision = 3; |
624 | 0 | } |
625 | 0 | } |
626 | |
|
627 | 0 | return hrs; |
628 | 0 | } |
629 | | |
630 | | U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles) |
631 | 0 | { |
632 | 0 | U64 total = 0; |
633 | 0 | unsigned n; |
634 | 0 | UTIL_TRACE_CALL("UTIL_getTotalFileSize(%u)", nbFiles); |
635 | 0 | for (n=0; n<nbFiles; n++) { |
636 | 0 | U64 const size = UTIL_getFileSize(fileNamesTable[n]); |
637 | 0 | if (size == UTIL_FILESIZE_UNKNOWN) { |
638 | 0 | UTIL_TRACE_RET(-1); |
639 | 0 | return UTIL_FILESIZE_UNKNOWN; |
640 | 0 | } |
641 | 0 | total += size; |
642 | 0 | } |
643 | 0 | UTIL_TRACE_RET((int)total); |
644 | 0 | return total; |
645 | 0 | } |
646 | | |
647 | | |
648 | | /* Read the entire content of a file into a buffer with progressive resizing */ |
649 | | static char* UTIL_readFileContent(FILE* inFile, size_t* totalReadPtr) |
650 | 0 | { |
651 | 0 | size_t bufSize = 64 KB; /* Start with a reasonable buffer size */ |
652 | 0 | size_t totalRead = 0; |
653 | 0 | size_t bytesRead = 0; |
654 | 0 | char* buf = (char*)malloc(bufSize); |
655 | 0 | if (buf == NULL) return NULL; |
656 | | |
657 | | |
658 | | /* Read the file incrementally */ |
659 | 0 | while ((bytesRead = fread(buf + totalRead, 1, bufSize - totalRead - 1, inFile)) > 0) { |
660 | 0 | totalRead += bytesRead; |
661 | | |
662 | | /* If buffer is nearly full, expand it */ |
663 | 0 | if (bufSize - totalRead < 1 KB) { |
664 | 0 | if (bufSize >= MAX_FILE_OF_FILE_NAMES_SIZE) { |
665 | | /* Too large, abort */ |
666 | 0 | free(buf); |
667 | 0 | return NULL; |
668 | 0 | } |
669 | | |
670 | 0 | { size_t newBufSize = bufSize * 2; |
671 | 0 | if (newBufSize > MAX_FILE_OF_FILE_NAMES_SIZE) |
672 | 0 | newBufSize = MAX_FILE_OF_FILE_NAMES_SIZE; |
673 | |
|
674 | 0 | { char* newBuf = (char*)realloc(buf, newBufSize); |
675 | 0 | if (newBuf == NULL) { |
676 | 0 | free(buf); |
677 | 0 | return NULL; |
678 | 0 | } |
679 | | |
680 | 0 | buf = newBuf; |
681 | 0 | bufSize = newBufSize; |
682 | 0 | } } } |
683 | 0 | } |
684 | | |
685 | | /* Add null terminator to the end */ |
686 | 0 | buf[totalRead] = '\0'; |
687 | 0 | *totalReadPtr = totalRead; |
688 | |
|
689 | 0 | return buf; |
690 | 0 | } |
691 | | |
692 | | /* Process a buffer containing multiple lines and count the number of lines */ |
693 | | static size_t UTIL_processLines(char* buffer, size_t bufferSize) |
694 | 0 | { |
695 | 0 | size_t lineCount = 0; |
696 | 0 | size_t i = 0; |
697 | | |
698 | | /* Convert newlines to null terminators and count lines */ |
699 | 0 | while (i < bufferSize) { |
700 | 0 | if (buffer[i] == '\n') { |
701 | 0 | buffer[i] = '\0'; /* Replace newlines with null terminators */ |
702 | 0 | lineCount++; |
703 | 0 | } |
704 | 0 | i++; |
705 | 0 | } |
706 | | |
707 | | /* Count the last line if it doesn't end with a newline */ |
708 | 0 | if (bufferSize > 0 && (i == 0 || buffer[i-1] != '\0')) { |
709 | 0 | lineCount++; |
710 | 0 | } |
711 | |
|
712 | 0 | return lineCount; |
713 | 0 | } |
714 | | |
715 | | /* Create an array of pointers to the lines in a buffer */ |
716 | | static const char** UTIL_createLinePointers(char* buffer, size_t numLines, size_t bufferSize) |
717 | 0 | { |
718 | 0 | size_t lineIndex = 0; |
719 | 0 | size_t pos = 0; |
720 | 0 | void* const bufferPtrs = malloc(numLines * sizeof(const char**)); |
721 | 0 | const char** const linePointers = (const char**)bufferPtrs; |
722 | 0 | if (bufferPtrs == NULL) return NULL; |
723 | | |
724 | 0 | while (lineIndex < numLines && pos < bufferSize) { |
725 | 0 | size_t len = 0; |
726 | 0 | linePointers[lineIndex++] = buffer+pos; |
727 | | |
728 | | /* Find the next null terminator, being careful not to go past the buffer */ |
729 | 0 | while ((pos + len < bufferSize) && buffer[pos + len] != '\0') { |
730 | 0 | len++; |
731 | 0 | } |
732 | | |
733 | | /* Move past this string and its null terminator */ |
734 | 0 | pos += len; |
735 | 0 | if (pos < bufferSize) pos++; /* Skip the null terminator if we're not at buffer end */ |
736 | 0 | } |
737 | | |
738 | | /* Verify we processed the expected number of lines */ |
739 | 0 | if (lineIndex != numLines) { |
740 | | /* Something went wrong - we didn't find as many lines as expected */ |
741 | 0 | free(bufferPtrs); |
742 | 0 | return NULL; |
743 | 0 | } |
744 | | |
745 | 0 | return linePointers; |
746 | 0 | } |
747 | | |
748 | | FileNamesTable* |
749 | | UTIL_createFileNamesTable_fromFileList(const char* fileList) |
750 | 0 | { |
751 | 0 | stat_t statbuf; |
752 | 0 | char* buffer = NULL; |
753 | 0 | size_t numLines = 0; |
754 | 0 | size_t bufferSize = 0; |
755 | | |
756 | | /* Check if the input is a valid file */ |
757 | 0 | if (!UTIL_stat(fileList, &statbuf)) { |
758 | 0 | return NULL; |
759 | 0 | } |
760 | | |
761 | | /* Check if the input is a supported type */ |
762 | 0 | if (!UTIL_isRegularFileStat(&statbuf) && |
763 | 0 | !UTIL_isFIFOStat(&statbuf) && |
764 | 0 | !UTIL_isFileDescriptorPipe(fileList)) { |
765 | 0 | return NULL; |
766 | 0 | } |
767 | | |
768 | | /* Open the input file */ |
769 | 0 | { FILE* const inFile = fopen(fileList, "rb"); |
770 | 0 | if (inFile == NULL) return NULL; |
771 | | |
772 | | /* Read the file content */ |
773 | 0 | buffer = UTIL_readFileContent(inFile, &bufferSize); |
774 | 0 | fclose(inFile); |
775 | 0 | } |
776 | | |
777 | 0 | if (buffer == NULL) return NULL; |
778 | | |
779 | | /* Process lines */ |
780 | 0 | numLines = UTIL_processLines(buffer, bufferSize); |
781 | 0 | if (numLines == 0) { |
782 | 0 | free(buffer); |
783 | 0 | return NULL; |
784 | 0 | } |
785 | | |
786 | | /* Create line pointers */ |
787 | 0 | { const char** linePointers = UTIL_createLinePointers(buffer, numLines, bufferSize); |
788 | 0 | if (linePointers == NULL) { |
789 | 0 | free(buffer); |
790 | 0 | return NULL; |
791 | 0 | } |
792 | | |
793 | | /* Create the final table */ |
794 | 0 | return UTIL_assembleFileNamesTable(linePointers, numLines, buffer); |
795 | 0 | } |
796 | 0 | } |
797 | | |
798 | | |
799 | | static FileNamesTable* |
800 | | UTIL_assembleFileNamesTable2(const char** filenames, size_t tableSize, size_t tableCapacity, char* buf) |
801 | 0 | { |
802 | 0 | FileNamesTable* const table = (FileNamesTable*) malloc(sizeof(*table)); |
803 | 0 | CONTROL(table != NULL); |
804 | 0 | table->fileNames = filenames; |
805 | 0 | table->buf = buf; |
806 | 0 | table->tableSize = tableSize; |
807 | 0 | table->tableCapacity = tableCapacity; |
808 | 0 | return table; |
809 | 0 | } |
810 | | |
811 | | FileNamesTable* |
812 | | UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf) |
813 | 0 | { |
814 | 0 | return UTIL_assembleFileNamesTable2(filenames, tableSize, tableSize, buf); |
815 | 0 | } |
816 | | |
817 | | void UTIL_freeFileNamesTable(FileNamesTable* table) |
818 | 0 | { |
819 | 0 | if (table==NULL) return; |
820 | 0 | free((void*)table->fileNames); |
821 | 0 | free(table->buf); |
822 | 0 | free(table); |
823 | 0 | } |
824 | | |
825 | | FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize) |
826 | 0 | { |
827 | 0 | const char** const fnTable = (const char**)malloc(tableSize * sizeof(*fnTable)); |
828 | 0 | FileNamesTable* fnt; |
829 | 0 | if (fnTable==NULL) return NULL; |
830 | 0 | fnt = UTIL_assembleFileNamesTable(fnTable, tableSize, NULL); |
831 | 0 | fnt->tableSize = 0; /* the table is empty */ |
832 | 0 | return fnt; |
833 | 0 | } |
834 | | |
835 | 0 | int UTIL_searchFileNamesTable(FileNamesTable* table, char const* name) { |
836 | 0 | size_t i; |
837 | 0 | for(i=0 ;i < table->tableSize; i++) { |
838 | 0 | if(!strcmp(table->fileNames[i], name)) { |
839 | 0 | return (int)i; |
840 | 0 | } |
841 | 0 | } |
842 | 0 | return -1; |
843 | 0 | } |
844 | | |
845 | | void UTIL_refFilename(FileNamesTable* fnt, const char* filename) |
846 | 0 | { |
847 | 0 | assert(fnt->tableSize < fnt->tableCapacity); |
848 | 0 | fnt->fileNames[fnt->tableSize] = filename; |
849 | 0 | fnt->tableSize++; |
850 | 0 | } |
851 | | |
852 | | static size_t getTotalTableSize(FileNamesTable* table) |
853 | 0 | { |
854 | 0 | size_t fnb, totalSize = 0; |
855 | 0 | for(fnb = 0 ; fnb < table->tableSize && table->fileNames[fnb] ; ++fnb) { |
856 | 0 | totalSize += strlen(table->fileNames[fnb]) + 1; /* +1 to add '\0' at the end of each fileName */ |
857 | 0 | } |
858 | 0 | return totalSize; |
859 | 0 | } |
860 | | |
861 | | FileNamesTable* |
862 | | UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2) |
863 | 0 | { |
864 | 0 | unsigned newTableIdx = 0; |
865 | 0 | size_t pos = 0; |
866 | 0 | size_t newTotalTableSize; |
867 | 0 | char* buf; |
868 | |
|
869 | 0 | FileNamesTable* const newTable = UTIL_assembleFileNamesTable(NULL, 0, NULL); |
870 | 0 | CONTROL( newTable != NULL ); |
871 | |
|
872 | 0 | newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2); |
873 | |
|
874 | 0 | buf = (char*) calloc(newTotalTableSize, sizeof(*buf)); |
875 | 0 | CONTROL ( buf != NULL ); |
876 | |
|
877 | 0 | newTable->buf = buf; |
878 | 0 | newTable->tableSize = table1->tableSize + table2->tableSize; |
879 | 0 | newTable->fileNames = (const char **) calloc(newTable->tableSize, sizeof(*(newTable->fileNames))); |
880 | 0 | CONTROL ( newTable->fileNames != NULL ); |
881 | |
|
882 | 0 | { unsigned idx1; |
883 | 0 | for( idx1=0 ; (idx1 < table1->tableSize) && table1->fileNames[idx1] && (pos < newTotalTableSize); ++idx1, ++newTableIdx) { |
884 | 0 | size_t const curLen = strlen(table1->fileNames[idx1]); |
885 | 0 | memcpy(buf+pos, table1->fileNames[idx1], curLen); |
886 | 0 | assert(newTableIdx <= newTable->tableSize); |
887 | 0 | newTable->fileNames[newTableIdx] = buf+pos; |
888 | 0 | pos += curLen+1; |
889 | 0 | } } |
890 | | |
891 | 0 | { unsigned idx2; |
892 | 0 | for( idx2=0 ; (idx2 < table2->tableSize) && table2->fileNames[idx2] && (pos < newTotalTableSize) ; ++idx2, ++newTableIdx) { |
893 | 0 | size_t const curLen = strlen(table2->fileNames[idx2]); |
894 | 0 | memcpy(buf+pos, table2->fileNames[idx2], curLen); |
895 | 0 | assert(newTableIdx < newTable->tableSize); |
896 | 0 | newTable->fileNames[newTableIdx] = buf+pos; |
897 | 0 | pos += curLen+1; |
898 | 0 | } } |
899 | 0 | assert(pos <= newTotalTableSize); |
900 | 0 | newTable->tableSize = newTableIdx; |
901 | |
|
902 | 0 | UTIL_freeFileNamesTable(table1); |
903 | 0 | UTIL_freeFileNamesTable(table2); |
904 | |
|
905 | 0 | return newTable; |
906 | 0 | } |
907 | | |
908 | | #ifdef _WIN32 |
909 | | static int UTIL_prepareFileList(const char* dirName, |
910 | | char** bufStart, size_t* pos, |
911 | | char** bufEnd, int followLinks) |
912 | | { |
913 | | char* path; |
914 | | size_t dirLength, pathLength; |
915 | | int nbFiles = 0; |
916 | | WIN32_FIND_DATAA cFile; |
917 | | HANDLE hFile; |
918 | | |
919 | | dirLength = strlen(dirName); |
920 | | path = (char*) malloc(dirLength + 3); |
921 | | if (!path) return 0; |
922 | | |
923 | | memcpy(path, dirName, dirLength); |
924 | | path[dirLength] = '\\'; |
925 | | path[dirLength+1] = '*'; |
926 | | path[dirLength+2] = 0; |
927 | | |
928 | | hFile=FindFirstFileA(path, &cFile); |
929 | | if (hFile == INVALID_HANDLE_VALUE) { |
930 | | UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName); |
931 | | free(path); |
932 | | return 0; |
933 | | } |
934 | | free(path); |
935 | | |
936 | | do { |
937 | | size_t const fnameLength = strlen(cFile.cFileName); |
938 | | path = (char*) malloc(dirLength + fnameLength + 2); |
939 | | if (!path) { FindClose(hFile); return 0; } |
940 | | memcpy(path, dirName, dirLength); |
941 | | path[dirLength] = '\\'; |
942 | | memcpy(path+dirLength+1, cFile.cFileName, fnameLength); |
943 | | pathLength = dirLength+1+fnameLength; |
944 | | path[pathLength] = 0; |
945 | | if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
946 | | if ( strcmp (cFile.cFileName, "..") == 0 |
947 | | || strcmp (cFile.cFileName, ".") == 0 ) |
948 | | continue; |
949 | | /* Recursively call "UTIL_prepareFileList" with the new path. */ |
950 | | nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); |
951 | | if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } |
952 | | } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) |
953 | | || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) |
954 | | || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) { |
955 | | if (*bufStart + *pos + pathLength >= *bufEnd) { |
956 | | ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; |
957 | | *bufStart = (char*)UTIL_realloc(*bufStart, newListSize); |
958 | | if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } |
959 | | *bufEnd = *bufStart + newListSize; |
960 | | } |
961 | | if (*bufStart + *pos + pathLength < *bufEnd) { |
962 | | memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */); |
963 | | *pos += pathLength + 1; |
964 | | nbFiles++; |
965 | | } } |
966 | | free(path); |
967 | | } while (FindNextFileA(hFile, &cFile)); |
968 | | |
969 | | FindClose(hFile); |
970 | | return nbFiles; |
971 | | } |
972 | | |
973 | | #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ |
974 | | |
975 | | static int UTIL_prepareFileList(const char *dirName, |
976 | | char** bufStart, size_t* pos, |
977 | | char** bufEnd, int followLinks) |
978 | 0 | { |
979 | 0 | DIR* dir; |
980 | 0 | struct dirent * entry; |
981 | 0 | size_t dirLength; |
982 | 0 | int nbFiles = 0; |
983 | |
|
984 | 0 | if (!(dir = opendir(dirName))) { |
985 | 0 | UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno)); |
986 | 0 | return 0; |
987 | 0 | } |
988 | | |
989 | 0 | dirLength = strlen(dirName); |
990 | 0 | errno = 0; |
991 | 0 | while ((entry = readdir(dir)) != NULL) { |
992 | 0 | char* path; |
993 | 0 | size_t fnameLength, pathLength; |
994 | 0 | if (strcmp (entry->d_name, "..") == 0 || |
995 | 0 | strcmp (entry->d_name, ".") == 0) continue; |
996 | 0 | fnameLength = strlen(entry->d_name); |
997 | 0 | path = (char*) malloc(dirLength + fnameLength + 2); |
998 | 0 | if (!path) { closedir(dir); return 0; } |
999 | 0 | memcpy(path, dirName, dirLength); |
1000 | |
|
1001 | 0 | path[dirLength] = '/'; |
1002 | 0 | memcpy(path+dirLength+1, entry->d_name, fnameLength); |
1003 | 0 | pathLength = dirLength+1+fnameLength; |
1004 | 0 | path[pathLength] = 0; |
1005 | |
|
1006 | 0 | if (!followLinks && UTIL_isLink(path)) { |
1007 | 0 | UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path); |
1008 | 0 | free(path); |
1009 | 0 | continue; |
1010 | 0 | } |
1011 | | |
1012 | 0 | if (UTIL_isDirectory(path)) { |
1013 | 0 | nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */ |
1014 | 0 | if (*bufStart == NULL) { free(path); closedir(dir); return 0; } |
1015 | 0 | } else { |
1016 | 0 | if (*bufStart + *pos + pathLength >= *bufEnd) { |
1017 | 0 | ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; |
1018 | 0 | assert(newListSize >= 0); |
1019 | 0 | *bufStart = (char*)UTIL_realloc(*bufStart, (size_t)newListSize); |
1020 | 0 | if (*bufStart != NULL) { |
1021 | 0 | *bufEnd = *bufStart + newListSize; |
1022 | 0 | } else { |
1023 | 0 | free(path); closedir(dir); return 0; |
1024 | 0 | } |
1025 | 0 | } |
1026 | 0 | if (*bufStart + *pos + pathLength < *bufEnd) { |
1027 | 0 | memcpy(*bufStart + *pos, path, pathLength + 1); /* with final \0 */ |
1028 | 0 | *pos += pathLength + 1; |
1029 | 0 | nbFiles++; |
1030 | 0 | } } |
1031 | 0 | free(path); |
1032 | 0 | errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */ |
1033 | 0 | } |
1034 | | |
1035 | 0 | if (errno != 0) { |
1036 | 0 | UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s \n", dirName, strerror(errno)); |
1037 | 0 | free(*bufStart); |
1038 | 0 | *bufStart = NULL; |
1039 | 0 | } |
1040 | 0 | closedir(dir); |
1041 | 0 | return nbFiles; |
1042 | 0 | } |
1043 | | |
1044 | | #else |
1045 | | |
1046 | | static int UTIL_prepareFileList(const char *dirName, |
1047 | | char** bufStart, size_t* pos, |
1048 | | char** bufEnd, int followLinks) |
1049 | | { |
1050 | | (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks; |
1051 | | UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE) \n", dirName); |
1052 | | return 0; |
1053 | | } |
1054 | | |
1055 | | #endif /* #ifdef _WIN32 */ |
1056 | | |
1057 | | int UTIL_isCompressedFile(const char *inputName, const char *extensionList[]) |
1058 | 0 | { |
1059 | 0 | const char* ext = UTIL_getFileExtension(inputName); |
1060 | 0 | while(*extensionList!=NULL) |
1061 | 0 | { |
1062 | 0 | const int isCompressedExtension = strcmp(ext,*extensionList); |
1063 | 0 | if(isCompressedExtension==0) |
1064 | 0 | return 1; |
1065 | 0 | ++extensionList; |
1066 | 0 | } |
1067 | 0 | return 0; |
1068 | 0 | } |
1069 | | |
1070 | | /*Utility function to get file extension from file */ |
1071 | | const char* UTIL_getFileExtension(const char* infilename) |
1072 | 0 | { |
1073 | 0 | const char* extension = strrchr(infilename, '.'); |
1074 | 0 | if(!extension || extension==infilename) return ""; |
1075 | 0 | return extension; |
1076 | 0 | } |
1077 | | |
1078 | | static int pathnameHas2Dots(const char *pathname) |
1079 | 0 | { |
1080 | | /* We need to figure out whether any ".." present in the path is a whole |
1081 | | * path token, which is the case if it is bordered on both sides by either |
1082 | | * the beginning/end of the path or by a directory separator. |
1083 | | */ |
1084 | 0 | const char *needle = pathname; |
1085 | 0 | while (1) { |
1086 | 0 | needle = strstr(needle, ".."); |
1087 | |
|
1088 | 0 | if (needle == NULL) { |
1089 | 0 | return 0; |
1090 | 0 | } |
1091 | | |
1092 | 0 | if ((needle == pathname || needle[-1] == PATH_SEP) |
1093 | 0 | && (needle[2] == '\0' || needle[2] == PATH_SEP)) { |
1094 | 0 | return 1; |
1095 | 0 | } |
1096 | | |
1097 | | /* increment so we search for the next match */ |
1098 | 0 | needle++; |
1099 | 0 | }; |
1100 | 0 | return 0; |
1101 | 0 | } |
1102 | | |
1103 | | static int isFileNameValidForMirroredOutput(const char *filename) |
1104 | 0 | { |
1105 | 0 | return !pathnameHas2Dots(filename); |
1106 | 0 | } |
1107 | | |
1108 | | |
1109 | 0 | #define DIR_DEFAULT_MODE 0755 |
1110 | | static mode_t getDirMode(const char *dirName) |
1111 | 0 | { |
1112 | 0 | stat_t st; |
1113 | 0 | if (!UTIL_stat(dirName, &st)) { |
1114 | 0 | UTIL_DISPLAY("zstd: failed to get DIR stats %s: %s\n", dirName, strerror(errno)); |
1115 | 0 | return DIR_DEFAULT_MODE; |
1116 | 0 | } |
1117 | 0 | if (!UTIL_isDirectoryStat(&st)) { |
1118 | 0 | UTIL_DISPLAY("zstd: expected directory: %s\n", dirName); |
1119 | 0 | return DIR_DEFAULT_MODE; |
1120 | 0 | } |
1121 | 0 | return st.st_mode; |
1122 | 0 | } |
1123 | | |
1124 | | static int makeDir(const char *dir, mode_t mode) |
1125 | 0 | { |
1126 | | #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__) |
1127 | | int ret = _mkdir(dir); |
1128 | | (void) mode; |
1129 | | #else |
1130 | 0 | int ret = mkdir(dir, mode); |
1131 | 0 | #endif |
1132 | 0 | if (ret != 0) { |
1133 | 0 | if (errno == EEXIST) |
1134 | 0 | return 0; |
1135 | 0 | UTIL_DISPLAY("zstd: failed to create DIR %s: %s\n", dir, strerror(errno)); |
1136 | 0 | } |
1137 | 0 | return ret; |
1138 | 0 | } |
1139 | | |
1140 | | /* this function requires a mutable input string */ |
1141 | | static void convertPathnameToDirName(char *pathname) |
1142 | 0 | { |
1143 | 0 | size_t len = 0; |
1144 | 0 | char* pos = NULL; |
1145 | | /* get dir name from pathname similar to 'dirname()' */ |
1146 | 0 | assert(pathname != NULL); |
1147 | | |
1148 | | /* remove trailing '/' chars */ |
1149 | 0 | len = strlen(pathname); |
1150 | 0 | assert(len > 0); |
1151 | 0 | while (pathname[len] == PATH_SEP) { |
1152 | 0 | pathname[len] = '\0'; |
1153 | 0 | len--; |
1154 | 0 | } |
1155 | 0 | if (len == 0) return; |
1156 | | |
1157 | | /* if input is a single file, return '.' instead. i.e. |
1158 | | * "xyz/abc/file.txt" => "xyz/abc" |
1159 | | "./file.txt" => "." |
1160 | | "file.txt" => "." |
1161 | | */ |
1162 | 0 | pos = strrchr(pathname, PATH_SEP); |
1163 | 0 | if (pos == NULL) { |
1164 | 0 | pathname[0] = '.'; |
1165 | 0 | pathname[1] = '\0'; |
1166 | 0 | } else { |
1167 | 0 | *pos = '\0'; |
1168 | 0 | } |
1169 | 0 | } |
1170 | | |
1171 | | /* pathname must be valid */ |
1172 | | static const char* trimLeadingRootChar(const char *pathname) |
1173 | 0 | { |
1174 | 0 | assert(pathname != NULL); |
1175 | 0 | if (pathname[0] == PATH_SEP) |
1176 | 0 | return pathname + 1; |
1177 | 0 | return pathname; |
1178 | 0 | } |
1179 | | |
1180 | | /* pathname must be valid */ |
1181 | | static const char* trimLeadingCurrentDirConst(const char *pathname) |
1182 | 0 | { |
1183 | 0 | assert(pathname != NULL); |
1184 | 0 | if ((pathname[0] == '.') && (pathname[1] == PATH_SEP)) |
1185 | 0 | return pathname + 2; |
1186 | 0 | return pathname; |
1187 | 0 | } |
1188 | | |
1189 | | static char* |
1190 | | trimLeadingCurrentDir(char *pathname) |
1191 | 0 | { |
1192 | | /* 'union charunion' can do const-cast without compiler warning */ |
1193 | 0 | union charunion { |
1194 | 0 | char *chr; |
1195 | 0 | const char* cchr; |
1196 | 0 | } ptr; |
1197 | 0 | ptr.cchr = trimLeadingCurrentDirConst(pathname); |
1198 | 0 | return ptr.chr; |
1199 | 0 | } |
1200 | | |
1201 | | /* remove leading './' or '/' chars here */ |
1202 | | static const char * trimPath(const char *pathname) |
1203 | 0 | { |
1204 | 0 | return trimLeadingRootChar( |
1205 | 0 | trimLeadingCurrentDirConst(pathname)); |
1206 | 0 | } |
1207 | | |
1208 | | static char* mallocAndJoin2Dir(const char *dir1, const char *dir2) |
1209 | 0 | { |
1210 | 0 | assert(dir1 != NULL && dir2 != NULL); |
1211 | 0 | { const size_t dir1Size = strlen(dir1); |
1212 | 0 | const size_t dir2Size = strlen(dir2); |
1213 | 0 | char *outDirBuffer, *buffer; |
1214 | |
|
1215 | 0 | outDirBuffer = (char *) malloc(dir1Size + dir2Size + 2); |
1216 | 0 | CONTROL(outDirBuffer != NULL); |
1217 | |
|
1218 | 0 | memcpy(outDirBuffer, dir1, dir1Size); |
1219 | 0 | outDirBuffer[dir1Size] = '\0'; |
1220 | |
|
1221 | 0 | buffer = outDirBuffer + dir1Size; |
1222 | 0 | if (dir1Size > 0 && *(buffer - 1) != PATH_SEP) { |
1223 | 0 | *buffer = PATH_SEP; |
1224 | 0 | buffer++; |
1225 | 0 | } |
1226 | 0 | memcpy(buffer, dir2, dir2Size); |
1227 | 0 | buffer[dir2Size] = '\0'; |
1228 | |
|
1229 | 0 | return outDirBuffer; |
1230 | 0 | } |
1231 | 0 | } |
1232 | | |
1233 | | /* this function will return NULL if input srcFileName is not valid name for mirrored output path */ |
1234 | | char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName) |
1235 | 0 | { |
1236 | 0 | char* pathname = NULL; |
1237 | 0 | if (!isFileNameValidForMirroredOutput(srcFileName)) |
1238 | 0 | return NULL; |
1239 | | |
1240 | 0 | pathname = mallocAndJoin2Dir(outDirRootName, trimPath(srcFileName)); |
1241 | |
|
1242 | 0 | convertPathnameToDirName(pathname); |
1243 | 0 | return pathname; |
1244 | 0 | } |
1245 | | |
1246 | | static int |
1247 | | mirrorSrcDir(char* srcDirName, const char* outDirName) |
1248 | 0 | { |
1249 | 0 | mode_t srcMode; |
1250 | 0 | int status = 0; |
1251 | 0 | char* newDir = mallocAndJoin2Dir(outDirName, trimPath(srcDirName)); |
1252 | 0 | if (!newDir) |
1253 | 0 | return -ENOMEM; |
1254 | | |
1255 | 0 | srcMode = getDirMode(srcDirName); |
1256 | 0 | status = makeDir(newDir, srcMode); |
1257 | 0 | free(newDir); |
1258 | 0 | return status; |
1259 | 0 | } |
1260 | | |
1261 | | static int |
1262 | | mirrorSrcDirRecursive(char* srcDirName, const char* outDirName) |
1263 | 0 | { |
1264 | 0 | int status = 0; |
1265 | 0 | char* pp = trimLeadingCurrentDir(srcDirName); |
1266 | 0 | char* sp = NULL; |
1267 | |
|
1268 | 0 | while ((sp = strchr(pp, PATH_SEP)) != NULL) { |
1269 | 0 | if (sp != pp) { |
1270 | 0 | *sp = '\0'; |
1271 | 0 | status = mirrorSrcDir(srcDirName, outDirName); |
1272 | 0 | if (status != 0) |
1273 | 0 | return status; |
1274 | 0 | *sp = PATH_SEP; |
1275 | 0 | } |
1276 | 0 | pp = sp + 1; |
1277 | 0 | } |
1278 | 0 | status = mirrorSrcDir(srcDirName, outDirName); |
1279 | 0 | return status; |
1280 | 0 | } |
1281 | | |
1282 | | static void |
1283 | | makeMirroredDestDirsWithSameSrcDirMode(char** srcDirNames, unsigned nbFile, const char* outDirName) |
1284 | 0 | { |
1285 | 0 | unsigned int i = 0; |
1286 | 0 | for (i = 0; i < nbFile; i++) |
1287 | 0 | mirrorSrcDirRecursive(srcDirNames[i], outDirName); |
1288 | 0 | } |
1289 | | |
1290 | | static int |
1291 | | firstIsParentOrSameDirOfSecond(const char* firstDir, const char* secondDir) |
1292 | 0 | { |
1293 | 0 | size_t firstDirLen = strlen(firstDir), |
1294 | 0 | secondDirLen = strlen(secondDir); |
1295 | 0 | return firstDirLen <= secondDirLen && |
1296 | 0 | (secondDir[firstDirLen] == PATH_SEP || secondDir[firstDirLen] == '\0') && |
1297 | 0 | 0 == strncmp(firstDir, secondDir, firstDirLen); |
1298 | 0 | } |
1299 | | |
1300 | 0 | static int compareDir(const void* pathname1, const void* pathname2) { |
1301 | | /* sort it after remove the leading '/' or './'*/ |
1302 | 0 | const char* s1 = trimPath(*(char * const *) pathname1); |
1303 | 0 | const char* s2 = trimPath(*(char * const *) pathname2); |
1304 | 0 | return strcmp(s1, s2); |
1305 | 0 | } |
1306 | | |
1307 | | static void |
1308 | | makeUniqueMirroredDestDirs(char** srcDirNames, unsigned nbFile, const char* outDirName) |
1309 | 0 | { |
1310 | 0 | unsigned int i = 0, uniqueDirNr = 0; |
1311 | 0 | char** uniqueDirNames = NULL; |
1312 | |
|
1313 | 0 | if (nbFile == 0) |
1314 | 0 | return; |
1315 | | |
1316 | 0 | uniqueDirNames = (char** ) malloc(nbFile * sizeof (char *)); |
1317 | 0 | CONTROL(uniqueDirNames != NULL); |
1318 | | |
1319 | | /* if dirs is "a/b/c" and "a/b/c/d", we only need call: |
1320 | | * we just need "a/b/c/d" */ |
1321 | 0 | qsort((void *)srcDirNames, nbFile, sizeof(char*), compareDir); |
1322 | |
|
1323 | 0 | uniqueDirNr = 1; |
1324 | 0 | uniqueDirNames[uniqueDirNr - 1] = srcDirNames[0]; |
1325 | 0 | for (i = 1; i < nbFile; i++) { |
1326 | 0 | char* prevDirName = srcDirNames[i - 1]; |
1327 | 0 | char* currDirName = srcDirNames[i]; |
1328 | | |
1329 | | /* note: we always compare trimmed path, i.e.: |
1330 | | * src dir of "./foo" and "/foo" will be both saved into: |
1331 | | * "outDirName/foo/" */ |
1332 | 0 | if (!firstIsParentOrSameDirOfSecond(trimPath(prevDirName), |
1333 | 0 | trimPath(currDirName))) |
1334 | 0 | uniqueDirNr++; |
1335 | | |
1336 | | /* we need to maintain original src dir name instead of trimmed |
1337 | | * dir, so we can retrieve the original src dir's mode_t */ |
1338 | 0 | uniqueDirNames[uniqueDirNr - 1] = currDirName; |
1339 | 0 | } |
1340 | |
|
1341 | 0 | makeMirroredDestDirsWithSameSrcDirMode(uniqueDirNames, uniqueDirNr, outDirName); |
1342 | |
|
1343 | 0 | free(uniqueDirNames); |
1344 | 0 | } |
1345 | | |
1346 | | static void |
1347 | | makeMirroredDestDirs(char** srcFileNames, unsigned nbFile, const char* outDirName) |
1348 | 0 | { |
1349 | 0 | unsigned int i = 0; |
1350 | 0 | for (i = 0; i < nbFile; ++i) |
1351 | 0 | convertPathnameToDirName(srcFileNames[i]); |
1352 | 0 | makeUniqueMirroredDestDirs(srcFileNames, nbFile, outDirName); |
1353 | 0 | } |
1354 | | |
1355 | | void UTIL_mirrorSourceFilesDirectories(const char** inFileNames, unsigned int nbFile, const char* outDirName) |
1356 | 0 | { |
1357 | 0 | unsigned int i = 0, validFilenamesNr = 0; |
1358 | 0 | char** srcFileNames = (char **) malloc(nbFile * sizeof (char *)); |
1359 | 0 | CONTROL(srcFileNames != NULL); |
1360 | | |
1361 | | /* check input filenames is valid */ |
1362 | 0 | for (i = 0; i < nbFile; ++i) { |
1363 | 0 | if (isFileNameValidForMirroredOutput(inFileNames[i])) { |
1364 | 0 | char* fname = STRDUP(inFileNames[i]); |
1365 | 0 | CONTROL(fname != NULL); |
1366 | 0 | srcFileNames[validFilenamesNr++] = fname; |
1367 | 0 | } |
1368 | 0 | } |
1369 | | |
1370 | 0 | if (validFilenamesNr > 0) { |
1371 | 0 | makeDir(outDirName, DIR_DEFAULT_MODE); |
1372 | 0 | makeMirroredDestDirs(srcFileNames, validFilenamesNr, outDirName); |
1373 | 0 | } |
1374 | |
|
1375 | 0 | for (i = 0; i < validFilenamesNr; i++) |
1376 | 0 | free(srcFileNames[i]); |
1377 | 0 | free(srcFileNames); |
1378 | 0 | } |
1379 | | |
1380 | | FileNamesTable* |
1381 | | UTIL_createExpandedFNT(const char* const* inputNames, size_t nbIfns, int followLinks) |
1382 | 0 | { |
1383 | 0 | unsigned nbFiles; |
1384 | 0 | char* buf = (char*)malloc(LIST_SIZE_INCREASE); |
1385 | 0 | char* bufend = buf + LIST_SIZE_INCREASE; |
1386 | |
|
1387 | 0 | if (!buf) return NULL; |
1388 | | |
1389 | 0 | { size_t ifnNb, pos; |
1390 | 0 | for (ifnNb=0, pos=0, nbFiles=0; ifnNb<nbIfns; ifnNb++) { |
1391 | 0 | if (!UTIL_isDirectory(inputNames[ifnNb])) { |
1392 | 0 | size_t const len = strlen(inputNames[ifnNb]); |
1393 | 0 | if (buf + pos + len >= bufend) { |
1394 | 0 | ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE; |
1395 | 0 | assert(newListSize >= 0); |
1396 | 0 | buf = (char*)UTIL_realloc(buf, (size_t)newListSize); |
1397 | 0 | if (!buf) return NULL; |
1398 | 0 | bufend = buf + newListSize; |
1399 | 0 | } |
1400 | 0 | if (buf + pos + len < bufend) { |
1401 | 0 | memcpy(buf+pos, inputNames[ifnNb], len+1); /* including final \0 */ |
1402 | 0 | pos += len + 1; |
1403 | 0 | nbFiles++; |
1404 | 0 | } |
1405 | 0 | } else { |
1406 | 0 | nbFiles += (unsigned)UTIL_prepareFileList(inputNames[ifnNb], &buf, &pos, &bufend, followLinks); |
1407 | 0 | if (buf == NULL) return NULL; |
1408 | 0 | } } } |
1409 | | |
1410 | | /* note : even if nbFiles==0, function returns a valid, though empty, FileNamesTable* object */ |
1411 | | |
1412 | 0 | { size_t ifnNb, pos; |
1413 | 0 | size_t const fntCapacity = nbFiles + 1; /* minimum 1, allows adding one reference, typically stdin */ |
1414 | 0 | const char** const fileNamesTable = (const char**)malloc(fntCapacity * sizeof(*fileNamesTable)); |
1415 | 0 | if (!fileNamesTable) { free(buf); return NULL; } |
1416 | | |
1417 | 0 | for (ifnNb = 0, pos = 0; ifnNb < nbFiles; ifnNb++) { |
1418 | 0 | fileNamesTable[ifnNb] = buf + pos; |
1419 | 0 | if (buf + pos > bufend) { free(buf); free((void*)fileNamesTable); return NULL; } |
1420 | 0 | pos += strlen(fileNamesTable[ifnNb]) + 1; |
1421 | 0 | } |
1422 | 0 | return UTIL_assembleFileNamesTable2(fileNamesTable, nbFiles, fntCapacity, buf); |
1423 | 0 | } |
1424 | 0 | } |
1425 | | |
1426 | | |
1427 | | void UTIL_expandFNT(FileNamesTable** fnt, int followLinks) |
1428 | 0 | { |
1429 | 0 | FileNamesTable* const newFNT = UTIL_createExpandedFNT((*fnt)->fileNames, (*fnt)->tableSize, followLinks); |
1430 | 0 | CONTROL(newFNT != NULL); |
1431 | 0 | UTIL_freeFileNamesTable(*fnt); |
1432 | 0 | *fnt = newFNT; |
1433 | 0 | } |
1434 | | |
1435 | | FileNamesTable* UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames) |
1436 | 0 | { |
1437 | 0 | size_t const sizeof_FNTable = nbFilenames * sizeof(*filenames); |
1438 | 0 | const char** const newFNTable = (const char**)malloc(sizeof_FNTable); |
1439 | 0 | if (newFNTable==NULL) return NULL; |
1440 | 0 | memcpy((void*)newFNTable, filenames, sizeof_FNTable); /* void* : mitigate a Visual compiler bug or limitation */ |
1441 | 0 | return UTIL_assembleFileNamesTable(newFNTable, nbFilenames, NULL); |
1442 | 0 | } |
1443 | | |
1444 | | |
1445 | | /*-**************************************** |
1446 | | * count the number of cores |
1447 | | ******************************************/ |
1448 | | |
1449 | | #if defined(_WIN32) || defined(WIN32) |
1450 | | |
1451 | | #include <windows.h> |
1452 | | |
1453 | | typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD); |
1454 | | |
1455 | | DWORD CountSetBits(ULONG_PTR bitMask) |
1456 | | { |
1457 | | DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1; |
1458 | | DWORD bitSetCount = 0; |
1459 | | ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT; |
1460 | | DWORD i; |
1461 | | |
1462 | | for (i = 0; i <= LSHIFT; ++i) |
1463 | | { |
1464 | | bitSetCount += ((bitMask & bitTest)?1:0); |
1465 | | bitTest/=2; |
1466 | | } |
1467 | | |
1468 | | return bitSetCount; |
1469 | | } |
1470 | | |
1471 | | int UTIL_countCores(int logical) |
1472 | | { |
1473 | | static int numCores = 0; |
1474 | | if (numCores != 0) return numCores; |
1475 | | |
1476 | | { LPFN_GLPI glpi; |
1477 | | BOOL done = FALSE; |
1478 | | PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; |
1479 | | PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL; |
1480 | | DWORD returnLength = 0; |
1481 | | size_t byteOffset = 0; |
1482 | | |
1483 | | #if defined(_MSC_VER) |
1484 | | /* Visual Studio does not like the following cast */ |
1485 | | # pragma warning( disable : 4054 ) /* conversion from function ptr to data ptr */ |
1486 | | # pragma warning( disable : 4055 ) /* conversion from data ptr to function ptr */ |
1487 | | #endif |
1488 | | glpi = (LPFN_GLPI)(void*)GetProcAddress(GetModuleHandle(TEXT("kernel32")), |
1489 | | "GetLogicalProcessorInformation"); |
1490 | | |
1491 | | if (glpi == NULL) { |
1492 | | goto failed; |
1493 | | } |
1494 | | |
1495 | | while(!done) { |
1496 | | DWORD rc = glpi(buffer, &returnLength); |
1497 | | if (FALSE == rc) { |
1498 | | if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
1499 | | if (buffer) |
1500 | | free(buffer); |
1501 | | buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength); |
1502 | | |
1503 | | if (buffer == NULL) { |
1504 | | perror("zstd"); |
1505 | | exit(1); |
1506 | | } |
1507 | | } else { |
1508 | | /* some other error */ |
1509 | | goto failed; |
1510 | | } |
1511 | | } else { |
1512 | | done = TRUE; |
1513 | | } } |
1514 | | |
1515 | | ptr = buffer; |
1516 | | |
1517 | | while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) { |
1518 | | |
1519 | | if (ptr->Relationship == RelationProcessorCore) { |
1520 | | if (logical) |
1521 | | numCores += CountSetBits(ptr->ProcessorMask); |
1522 | | else |
1523 | | numCores++; |
1524 | | } |
1525 | | |
1526 | | ptr++; |
1527 | | byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); |
1528 | | } |
1529 | | |
1530 | | free(buffer); |
1531 | | |
1532 | | return numCores; |
1533 | | } |
1534 | | |
1535 | | failed: |
1536 | | /* try to fall back on GetSystemInfo */ |
1537 | | { SYSTEM_INFO sysinfo; |
1538 | | GetSystemInfo(&sysinfo); |
1539 | | numCores = sysinfo.dwNumberOfProcessors; |
1540 | | if (numCores == 0) numCores = 1; /* just in case */ |
1541 | | } |
1542 | | return numCores; |
1543 | | } |
1544 | | |
1545 | | #elif defined(__APPLE__) |
1546 | | |
1547 | | #include <sys/sysctl.h> |
1548 | | |
1549 | | /* Use apple-provided syscall |
1550 | | * see: man 3 sysctl */ |
1551 | | int UTIL_countCores(int logical) |
1552 | | { |
1553 | | static S32 numCores = 0; /* apple specifies int32_t */ |
1554 | | if (numCores != 0) return numCores; |
1555 | | |
1556 | | { size_t size = sizeof(S32); |
1557 | | int const ret = sysctlbyname(logical ? "hw.logicalcpu" : "hw.physicalcpu", &numCores, &size, NULL, 0); |
1558 | | if (ret != 0) { |
1559 | | if (errno == ENOENT) { |
1560 | | /* entry not present, fall back on 1 */ |
1561 | | numCores = 1; |
1562 | | } else { |
1563 | | perror("zstd: can't get number of cpus"); |
1564 | | exit(1); |
1565 | | } |
1566 | | } |
1567 | | |
1568 | | return numCores; |
1569 | | } |
1570 | | } |
1571 | | |
1572 | | #elif defined(__linux__) |
1573 | | |
1574 | | /* parse /proc/cpuinfo |
1575 | | * siblings / cpu cores should give hyperthreading ratio |
1576 | | * otherwise fall back on sysconf */ |
1577 | | int UTIL_countCores(int logical) |
1578 | 0 | { |
1579 | 0 | static int numCores = 0; |
1580 | |
|
1581 | 0 | if (numCores != 0) return numCores; |
1582 | | |
1583 | 0 | numCores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
1584 | 0 | if (numCores == -1) { |
1585 | | /* value not queryable, fall back on 1 */ |
1586 | 0 | return numCores = 1; |
1587 | 0 | } |
1588 | | |
1589 | | /* try to determine if there's hyperthreading */ |
1590 | 0 | { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r"); |
1591 | 0 | #define BUF_SIZE 80 |
1592 | 0 | char buff[BUF_SIZE]; |
1593 | |
|
1594 | 0 | int siblings = 0; |
1595 | 0 | int cpu_cores = 0; |
1596 | 0 | int ratio = 1; |
1597 | |
|
1598 | 0 | if (cpuinfo == NULL) { |
1599 | | /* fall back on the sysconf value */ |
1600 | 0 | return numCores; |
1601 | 0 | } |
1602 | | |
1603 | | /* assume the cpu cores/siblings values will be constant across all |
1604 | | * present processors */ |
1605 | 0 | while (!feof(cpuinfo)) { |
1606 | 0 | if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) { |
1607 | 0 | if (strncmp(buff, "siblings", 8) == 0) { |
1608 | 0 | const char* const sep = strchr(buff, ':'); |
1609 | 0 | if (sep == NULL || *sep == '\0') { |
1610 | | /* formatting was broken? */ |
1611 | 0 | goto failed; |
1612 | 0 | } |
1613 | | |
1614 | 0 | siblings = atoi(sep + 1); |
1615 | 0 | } |
1616 | 0 | if (strncmp(buff, "cpu cores", 9) == 0) { |
1617 | 0 | const char* const sep = strchr(buff, ':'); |
1618 | 0 | if (sep == NULL || *sep == '\0') { |
1619 | | /* formatting was broken? */ |
1620 | 0 | goto failed; |
1621 | 0 | } |
1622 | | |
1623 | 0 | cpu_cores = atoi(sep + 1); |
1624 | 0 | } |
1625 | 0 | } else if (ferror(cpuinfo)) { |
1626 | | /* fall back on the sysconf value */ |
1627 | 0 | goto failed; |
1628 | 0 | } } |
1629 | 0 | if (siblings && cpu_cores && siblings > cpu_cores) { |
1630 | 0 | ratio = siblings / cpu_cores; |
1631 | 0 | } |
1632 | |
|
1633 | 0 | if (ratio && numCores > ratio && !logical) { |
1634 | 0 | numCores = numCores / ratio; |
1635 | 0 | } |
1636 | |
|
1637 | 0 | failed: |
1638 | 0 | fclose(cpuinfo); |
1639 | 0 | return numCores; |
1640 | 0 | } |
1641 | 0 | } |
1642 | | |
1643 | | #elif defined(__FreeBSD__) |
1644 | | |
1645 | | #include <sys/sysctl.h> |
1646 | | |
1647 | | /* Use physical core sysctl when available |
1648 | | * see: man 4 smp, man 3 sysctl */ |
1649 | | int UTIL_countCores(int logical) |
1650 | | { |
1651 | | static int numCores = 0; /* freebsd sysctl is native int sized */ |
1652 | | #if __FreeBSD_version >= 1300008 |
1653 | | static int perCore = 1; |
1654 | | #endif |
1655 | | if (numCores != 0) return numCores; |
1656 | | |
1657 | | #if __FreeBSD_version >= 1300008 |
1658 | | { size_t size = sizeof(numCores); |
1659 | | int ret = sysctlbyname("kern.smp.cores", &numCores, &size, NULL, 0); |
1660 | | if (ret == 0) { |
1661 | | if (logical) { |
1662 | | ret = sysctlbyname("kern.smp.threads_per_core", &perCore, &size, NULL, 0); |
1663 | | /* default to physical cores if logical cannot be read */ |
1664 | | if (ret == 0) |
1665 | | numCores *= perCore; |
1666 | | } |
1667 | | |
1668 | | return numCores; |
1669 | | } |
1670 | | if (errno != ENOENT) { |
1671 | | perror("zstd: can't get number of cpus"); |
1672 | | exit(1); |
1673 | | } |
1674 | | /* sysctl not present, fall through to older sysconf method */ |
1675 | | } |
1676 | | #else |
1677 | | /* suppress unused parameter warning */ |
1678 | | (void) logical; |
1679 | | #endif |
1680 | | |
1681 | | numCores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
1682 | | if (numCores == -1) { |
1683 | | /* value not queryable, fall back on 1 */ |
1684 | | numCores = 1; |
1685 | | } |
1686 | | return numCores; |
1687 | | } |
1688 | | |
1689 | | #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__CYGWIN__) |
1690 | | |
1691 | | /* Use POSIX sysconf |
1692 | | * see: man 3 sysconf */ |
1693 | | int UTIL_countCores(int logical) |
1694 | | { |
1695 | | static int numCores = 0; |
1696 | | |
1697 | | /* suppress unused parameter warning */ |
1698 | | (void)logical; |
1699 | | |
1700 | | if (numCores != 0) return numCores; |
1701 | | |
1702 | | numCores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
1703 | | if (numCores == -1) { |
1704 | | /* value not queryable, fall back on 1 */ |
1705 | | return numCores = 1; |
1706 | | } |
1707 | | return numCores; |
1708 | | } |
1709 | | |
1710 | | #else |
1711 | | |
1712 | | int UTIL_countCores(int logical) |
1713 | | { |
1714 | | /* suppress unused parameter warning */ |
1715 | | (void)logical; |
1716 | | |
1717 | | /* assume 1 */ |
1718 | | return 1; |
1719 | | } |
1720 | | |
1721 | | #endif |
1722 | | |
1723 | | int UTIL_countPhysicalCores(void) |
1724 | 0 | { |
1725 | 0 | return UTIL_countCores(0); |
1726 | 0 | } |
1727 | | |
1728 | | int UTIL_countLogicalCores(void) |
1729 | 0 | { |
1730 | 0 | return UTIL_countCores(1); |
1731 | 0 | } |