blob: c9031e91d35a9cba72e9a3b5e2d6ecc3f84a04ad [file] [log] [blame]
Rohit Jainf881ee82018-10-11 12:52:19 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Rohit Jainf881ee82018-10-11 12:52:19 -07003 * All rights reserved.
4 *
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
9 */
10
11#if defined (__cplusplus)
12extern "C" {
13#endif
14
15
16/*-****************************************
17* Dependencies
18******************************************/
Yann Colletffba1422018-12-20 14:30:30 -080019#include "util.h" /* note : ensure that platform.h is included first ! */
Yann Colleta684b822019-11-26 15:16:53 -080020#include <stdlib.h> /* malloc, realloc, free */
Yann Colletaaab6182019-11-26 15:25:32 -080021#include <stdio.h> /* fprintf */
Yann Colleta684b822019-11-26 15:16:53 -080022#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */
Yann Collet173ef9d2018-12-19 18:30:57 -080023#include <errno.h>
Yann Collet72dbf1b2018-12-20 12:27:12 -080024#include <assert.h>
Yann Collet173ef9d2018-12-19 18:30:57 -080025
Yann Colleta684b822019-11-26 15:16:53 -080026#if defined(_WIN32)
27# include <sys/utime.h> /* utime */
28# include <io.h> /* _chmod */
29#else
30# include <unistd.h> /* chown, stat */
Fabrice Fontaine26d01bd2020-07-15 21:19:14 +020031# if PLATFORM_POSIX_VERSION < 200809L || !defined(st_mtime)
Yann Colleta684b822019-11-26 15:16:53 -080032# include <utime.h> /* utime */
33# else
34# include <fcntl.h> /* AT_FDCWD */
35# include <sys/stat.h> /* utimensat */
36# endif
37#endif
38
Sen Huang62616c42019-09-06 13:20:50 -070039#if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__)
40#include <direct.h> /* needed for _mkdir in windows */
41#endif
Rohit Jainf881ee82018-10-11 12:52:19 -070042
Yann Collet76b9e422019-11-05 14:59:45 -080043#if defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
44# include <dirent.h> /* opendir, readdir */
45# include <string.h> /* strerror, memcpy */
46#endif /* #ifdef _WIN32 */
47
Yann Collet1ead0c52019-10-25 16:36:59 -070048/*-****************************************
49* Internal Macros
50******************************************/
51
Yann Collet7543cd02019-11-26 15:21:58 -080052/* CONTROL is almost like an assert(), but is never disabled.
53 * It's designed for failures that may happen rarely,
54 * but we don't want to maintain a specific error code path for them,
55 * such as a malloc() returning NULL for example.
56 * Since it's always active, this macro can trigger side effects.
Yann Collet1ead0c52019-10-25 16:36:59 -070057 */
58#define CONTROL(c) { \
59 if (!(c)) { \
60 UTIL_DISPLAYLEVEL(1, "Error : %s, %i : %s", \
61 __FILE__, __LINE__, #c); \
Yann Collet3e5c81e2019-10-26 00:01:11 -070062 exit(1); \
Yann Collet1ead0c52019-10-25 16:36:59 -070063} }
64
Yann Collet7543cd02019-11-26 15:21:58 -080065/* console log */
66#define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__)
67#define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
68
Elliott Hughes44aba642023-09-12 20:18:59 +000069static int g_traceDepth = 0;
70int g_traceFileStat = 0;
71
72#define UTIL_TRACE_CALL(...) \
73 { \
74 if (g_traceFileStat) { \
75 UTIL_DISPLAY("Trace:FileStat: %*s> ", g_traceDepth, ""); \
76 UTIL_DISPLAY(__VA_ARGS__); \
77 UTIL_DISPLAY("\n"); \
78 ++g_traceDepth; \
79 } \
80 }
81
82#define UTIL_TRACE_RET(ret) \
83 { \
84 if (g_traceFileStat) { \
85 --g_traceDepth; \
86 UTIL_DISPLAY("Trace:FileStat: %*s< %d\n", g_traceDepth, "", (ret)); \
87 } \
88 }
89
Yann Collet7543cd02019-11-26 15:21:58 -080090/* A modified version of realloc().
Yann Collet76b9e422019-11-05 14:59:45 -080091 * If UTIL_realloc() fails the original block is freed.
92 */
93UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size)
94{
95 void *newptr = realloc(ptr, size);
96 if (newptr) return newptr;
97 free(ptr);
98 return NULL;
99}
100
Yann Collet9a221402019-11-25 13:45:22 -0800101#if defined(_MSC_VER)
102 #define chmod _chmod
103#endif
104
Elliott Hughes44aba642023-09-12 20:18:59 +0000105#ifndef ZSTD_HAVE_FCHMOD
106#if PLATFORM_POSIX_VERSION >= 199309L
107#define ZSTD_HAVE_FCHMOD
108#endif
109#endif
110
111#ifndef ZSTD_HAVE_FCHOWN
112#if PLATFORM_POSIX_VERSION >= 200809L
113#define ZSTD_HAVE_FCHOWN
114#endif
115#endif
Yann Collet76b9e422019-11-05 14:59:45 -0800116
Yann Collet1ead0c52019-10-25 16:36:59 -0700117/*-****************************************
118* Console log
119******************************************/
120int g_utilDisplayLevel;
121
senhuang427991c552020-08-26 16:50:20 -0400122int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg,
senhuang4293d63ea2020-09-24 15:58:06 -0400123 const char* acceptableLetters, int hasStdinInput) {
senhuang427991c552020-08-26 16:50:20 -0400124 int ch, result;
senhuang427aa3da12020-09-22 14:15:52 -0400125
senhuang4288f44102020-09-24 16:29:12 -0400126 if (hasStdinInput) {
senhuang4202422db2020-09-25 11:51:35 -0400127 UTIL_DISPLAY("stdin is an input - not proceeding.\n");
senhuang4293d63ea2020-09-24 15:58:06 -0400128 return 1;
senhuang4288f44102020-09-24 16:29:12 -0400129 }
senhuang4293d63ea2020-09-24 15:58:06 -0400130
senhuang42aab11ce2020-08-25 11:25:49 -0400131 UTIL_DISPLAY("%s", prompt);
132 ch = getchar();
senhuang427991c552020-08-26 16:50:20 -0400133 result = 0;
senhuang42aab11ce2020-08-25 11:25:49 -0400134 if (strchr(acceptableLetters, ch) == NULL) {
Elliott Hughes44aba642023-09-12 20:18:59 +0000135 UTIL_DISPLAY("%s \n", abortMsg);
senhuang427991c552020-08-26 16:50:20 -0400136 result = 1;
senhuang42aab11ce2020-08-25 11:25:49 -0400137 }
138 /* flush the rest */
139 while ((ch!=EOF) && (ch!='\n'))
140 ch = getchar();
senhuang427991c552020-08-26 16:50:20 -0400141 return result;
senhuang42aab11ce2020-08-25 11:25:49 -0400142}
143
Yann Collet1ead0c52019-10-25 16:36:59 -0700144
Yann Collet9a221402019-11-25 13:45:22 -0800145/*-*************************************
146* Constants
147***************************************/
148#define LIST_SIZE_INCREASE (8*1024)
Yann Colletc71bd452019-11-26 11:20:26 -0800149#define MAX_FILE_OF_FILE_NAMES_SIZE (1<<20)*50
Yann Collet9a221402019-11-25 13:45:22 -0800150
151
152/*-*************************************
153* Functions
154***************************************/
Yann Collet1ead0c52019-10-25 16:36:59 -0700155
Elliott Hughes44aba642023-09-12 20:18:59 +0000156void UTIL_traceFileStat(void)
157{
158 g_traceFileStat = 1;
159}
160
161int UTIL_fstat(const int fd, const char* filename, stat_t* statbuf)
162{
163 int ret;
164 UTIL_TRACE_CALL("UTIL_stat(%d, %s)", fd, filename);
165#if defined(_MSC_VER)
166 if (fd >= 0) {
167 ret = !_fstat64(fd, statbuf);
168 } else {
169 ret = !_stat64(filename, statbuf);
170 }
171#elif defined(__MINGW32__) && defined (__MSVCRT__)
172 if (fd >= 0) {
173 ret = !_fstati64(fd, statbuf);
174 } else {
175 ret = !_stati64(filename, statbuf);
176 }
177#else
178 if (fd >= 0) {
179 ret = !fstat(fd, statbuf);
180 } else {
181 ret = !stat(filename, statbuf);
182 }
183#endif
184 UTIL_TRACE_RET(ret);
185 return ret;
186}
187
W. Felix Handtea5a24182023-02-03 13:48:34 -0800188int UTIL_stat(const char* filename, stat_t* statbuf)
189{
Elliott Hughes44aba642023-09-12 20:18:59 +0000190 return UTIL_fstat(-1, filename, statbuf);
W. Felix Handtea5a24182023-02-03 13:48:34 -0800191}
192
Rohit Jaind6d240f2018-10-11 15:07:12 -0700193int UTIL_isRegularFile(const char* infilename)
194{
195 stat_t statbuf;
Elliott Hughes44aba642023-09-12 20:18:59 +0000196 int ret;
197 UTIL_TRACE_CALL("UTIL_isRegularFile(%s)", infilename);
198 ret = UTIL_stat(infilename, &statbuf) && UTIL_isRegularFileStat(&statbuf);
199 UTIL_TRACE_RET(ret);
200 return ret;
Rohit Jaind6d240f2018-10-11 15:07:12 -0700201}
202
W. Felix Handte44fa0522020-08-05 01:00:06 -0400203int UTIL_isRegularFileStat(const stat_t* statbuf)
204{
205#if defined(_MSC_VER)
206 return (statbuf->st_mode & S_IFREG) != 0;
207#else
208 return S_ISREG(statbuf->st_mode) != 0;
209#endif
210}
211
Yann Collet9a221402019-11-25 13:45:22 -0800212/* like chmod, but avoid changing permission of /dev/null */
W. Felix Handte0a8aacb2020-08-05 12:00:12 -0400213int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions)
Yann Collet9a221402019-11-25 13:45:22 -0800214{
Elliott Hughes44aba642023-09-12 20:18:59 +0000215 return UTIL_fchmod(-1, filename, statbuf, permissions);
216}
217
218int UTIL_fchmod(const int fd, char const* filename, const stat_t* statbuf, mode_t permissions)
219{
W. Felix Handte0a8aacb2020-08-05 12:00:12 -0400220 stat_t localStatBuf;
Elliott Hughes44aba642023-09-12 20:18:59 +0000221 UTIL_TRACE_CALL("UTIL_chmod(%s, %#4o)", filename, (unsigned)permissions);
W. Felix Handte0a8aacb2020-08-05 12:00:12 -0400222 if (statbuf == NULL) {
Elliott Hughes44aba642023-09-12 20:18:59 +0000223 if (!UTIL_fstat(fd, filename, &localStatBuf)) {
224 UTIL_TRACE_RET(0);
225 return 0;
226 }
W. Felix Handte0a8aacb2020-08-05 12:00:12 -0400227 statbuf = &localStatBuf;
228 }
Elliott Hughes44aba642023-09-12 20:18:59 +0000229 if (!UTIL_isRegularFileStat(statbuf)) {
230 UTIL_TRACE_RET(0);
231 return 0; /* pretend success, but don't change anything */
232 }
233#ifdef ZSTD_HAVE_FCHMOD
234 if (fd >= 0) {
235 int ret;
236 UTIL_TRACE_CALL("fchmod");
237 ret = fchmod(fd, permissions);
238 UTIL_TRACE_RET(ret);
239 UTIL_TRACE_RET(ret);
240 return ret;
241 } else
242#endif
243 {
244 int ret;
245 UTIL_TRACE_CALL("chmod");
246 ret = chmod(filename, permissions);
247 UTIL_TRACE_RET(ret);
248 UTIL_TRACE_RET(ret);
249 return ret;
250 }
Yann Collet9a221402019-11-25 13:45:22 -0800251}
252
W. Felix Handtea719edb2021-08-04 14:49:00 -0400253/* set access and modification times */
254int UTIL_utime(const char* filename, const stat_t *statbuf)
255{
256 int ret;
Elliott Hughes44aba642023-09-12 20:18:59 +0000257 UTIL_TRACE_CALL("UTIL_utime(%s)", filename);
W. Felix Handtea719edb2021-08-04 14:49:00 -0400258 /* We check that st_mtime is a macro here in order to give us confidence
259 * that struct stat has a struct timespec st_mtim member. We need this
260 * check because there are some platforms that claim to be POSIX 2008
261 * compliant but which do not have st_mtim... */
262#if (PLATFORM_POSIX_VERSION >= 200809L) && defined(st_mtime)
Elliott Hughes44aba642023-09-12 20:18:59 +0000263 {
264 /* (atime, mtime) */
265 struct timespec timebuf[2] = { {0, UTIME_NOW} };
266 timebuf[1] = statbuf->st_mtim;
267 ret = utimensat(AT_FDCWD, filename, timebuf, 0);
268 }
W. Felix Handtea719edb2021-08-04 14:49:00 -0400269#else
Elliott Hughes44aba642023-09-12 20:18:59 +0000270 {
271 struct utimbuf timebuf;
272 timebuf.actime = time(NULL);
273 timebuf.modtime = statbuf->st_mtime;
274 ret = utime(filename, &timebuf);
275 }
W. Felix Handtea719edb2021-08-04 14:49:00 -0400276#endif
277 errno = 0;
Elliott Hughes44aba642023-09-12 20:18:59 +0000278 UTIL_TRACE_RET(ret);
W. Felix Handtea719edb2021-08-04 14:49:00 -0400279 return ret;
280}
281
W. Felix Handte1a1003f2020-08-05 00:35:21 -0400282int UTIL_setFileStat(const char *filename, const stat_t *statbuf)
Rohit Jaind6d240f2018-10-11 15:07:12 -0700283{
Elliott Hughes44aba642023-09-12 20:18:59 +0000284 return UTIL_setFDStat(-1, filename, statbuf);
285}
286
287int UTIL_setFDStat(const int fd, const char *filename, const stat_t *statbuf)
288{
Rohit Jaind6d240f2018-10-11 15:07:12 -0700289 int res = 0;
W. Felix Handtec1449142020-08-05 12:10:42 -0400290 stat_t curStatBuf;
Elliott Hughes44aba642023-09-12 20:18:59 +0000291 UTIL_TRACE_CALL("UTIL_setFileStat(%d, %s)", fd, filename);
Rohit Jaind6d240f2018-10-11 15:07:12 -0700292
Elliott Hughes44aba642023-09-12 20:18:59 +0000293 if (!UTIL_fstat(fd, filename, &curStatBuf) || !UTIL_isRegularFileStat(&curStatBuf)) {
294 UTIL_TRACE_RET(-1);
295 return -1;
296 }
297
298 /* Mimic gzip's behavior:
299 *
300 * "Change the group first, then the permissions, then the owner.
301 * That way, the permissions will be correct on systems that allow
302 * users to give away files, without introducing a security hole.
303 * Security depends on permissions not containing the setuid or
304 * setgid bits." */
W. Felix Handte0d2d4602023-01-17 16:37:30 -0800305
Rohit Jaind6d240f2018-10-11 15:07:12 -0700306#if !defined(_WIN32)
Elliott Hughes44aba642023-09-12 20:18:59 +0000307#ifdef ZSTD_HAVE_FCHOWN
308 if (fd >= 0) {
309 res += fchown(fd, -1, statbuf->st_gid); /* Apply group ownership */
310 } else
311#endif
312 {
313 res += chown(filename, -1, statbuf->st_gid); /* Apply group ownership */
314 }
Rohit Jaind6d240f2018-10-11 15:07:12 -0700315#endif
316
Elliott Hughes44aba642023-09-12 20:18:59 +0000317 res += UTIL_fchmod(fd, filename, &curStatBuf, statbuf->st_mode & 0777); /* Copy file permissions */
318
319#if !defined(_WIN32)
320#ifdef ZSTD_HAVE_FCHOWN
321 if (fd >= 0) {
322 res += fchown(fd, statbuf->st_uid, -1); /* Apply user ownership */
323 } else
324#endif
325 {
326 res += chown(filename, statbuf->st_uid, -1); /* Apply user ownership */
327 }
328#endif
Rohit Jaind6d240f2018-10-11 15:07:12 -0700329
330 errno = 0;
Elliott Hughes44aba642023-09-12 20:18:59 +0000331 UTIL_TRACE_RET(-res);
Rohit Jaind6d240f2018-10-11 15:07:12 -0700332 return -res; /* number of errors is returned */
333}
Rohit Jainf881ee82018-10-11 12:52:19 -0700334
Yann Collet9a221402019-11-25 13:45:22 -0800335int UTIL_isDirectory(const char* infilename)
Rohit Jainf881ee82018-10-11 12:52:19 -0700336{
Rohit Jainf881ee82018-10-11 12:52:19 -0700337 stat_t statbuf;
Elliott Hughes44aba642023-09-12 20:18:59 +0000338 int ret;
339 UTIL_TRACE_CALL("UTIL_isDirectory(%s)", infilename);
340 ret = UTIL_stat(infilename, &statbuf) && UTIL_isDirectoryStat(&statbuf);
341 UTIL_TRACE_RET(ret);
342 return ret;
Rohit Jainf881ee82018-10-11 12:52:19 -0700343}
344
W. Felix Handte44fa0522020-08-05 01:00:06 -0400345int UTIL_isDirectoryStat(const stat_t* statbuf)
346{
Elliott Hughes44aba642023-09-12 20:18:59 +0000347 int ret;
348 UTIL_TRACE_CALL("UTIL_isDirectoryStat()");
W. Felix Handte44fa0522020-08-05 01:00:06 -0400349#if defined(_MSC_VER)
Elliott Hughes44aba642023-09-12 20:18:59 +0000350 ret = (statbuf->st_mode & _S_IFDIR) != 0;
W. Felix Handte44fa0522020-08-05 01:00:06 -0400351#else
Elliott Hughes44aba642023-09-12 20:18:59 +0000352 ret = S_ISDIR(statbuf->st_mode) != 0;
W. Felix Handte44fa0522020-08-05 01:00:06 -0400353#endif
Elliott Hughes44aba642023-09-12 20:18:59 +0000354 UTIL_TRACE_RET(ret);
355 return ret;
W. Felix Handte44fa0522020-08-05 01:00:06 -0400356}
357
Sen Huangf80437c2019-10-02 11:08:20 -0400358int UTIL_compareStr(const void *p1, const void *p2) {
359 return strcmp(* (char * const *) p1, * (char * const *) p2);
360}
Sen Huanga9c807a2019-09-06 10:17:04 -0700361
Yann Collet00040432019-10-17 10:56:14 -0700362int UTIL_isSameFile(const char* fName1, const char* fName2)
shakeelraoe5811e52019-03-23 19:04:56 -0700363{
Elliott Hughes44aba642023-09-12 20:18:59 +0000364 int ret;
Yann Collet00040432019-10-17 10:56:14 -0700365 assert(fName1 != NULL); assert(fName2 != NULL);
Elliott Hughes44aba642023-09-12 20:18:59 +0000366 UTIL_TRACE_CALL("UTIL_isSameFile(%s, %s)", fName1, fName2);
Yann Collet00040432019-10-17 10:56:14 -0700367#if defined(_MSC_VER) || defined(_WIN32)
shakeelraoe5811e52019-03-23 19:04:56 -0700368 /* note : Visual does not support file identification by inode.
Yann Collet00040432019-10-17 10:56:14 -0700369 * inode does not work on Windows, even with a posix layer, like msys2.
shakeelraoe5811e52019-03-23 19:04:56 -0700370 * The following work-around is limited to detecting exact name repetition only,
371 * aka `filename` is considered different from `subdir/../filename` */
Elliott Hughes44aba642023-09-12 20:18:59 +0000372 ret = !strcmp(fName1, fName2);
shakeelraoe5811e52019-03-23 19:04:56 -0700373#else
Yann Collet00040432019-10-17 10:56:14 -0700374 { stat_t file1Stat;
375 stat_t file2Stat;
Elliott Hughes44aba642023-09-12 20:18:59 +0000376 ret = UTIL_stat(fName1, &file1Stat)
W. Felix Handte5fbc6ad2020-08-05 00:31:48 -0400377 && UTIL_stat(fName2, &file2Stat)
Elliott Hughes44aba642023-09-12 20:18:59 +0000378 && UTIL_isSameFileStat(fName1, fName2, &file1Stat, &file2Stat);
W. Felix Handte03820762023-01-17 14:50:31 -0800379 }
380#endif
Elliott Hughes44aba642023-09-12 20:18:59 +0000381 UTIL_TRACE_RET(ret);
382 return ret;
383}
384
385int UTIL_isSameFileStat(
386 const char* fName1, const char* fName2,
387 const stat_t* file1Stat, const stat_t* file2Stat)
388{
389 int ret;
390 assert(fName1 != NULL); assert(fName2 != NULL);
391 UTIL_TRACE_CALL("UTIL_isSameFileStat(%s, %s)", fName1, fName2);
392#if defined(_MSC_VER) || defined(_WIN32)
393 /* note : Visual does not support file identification by inode.
394 * inode does not work on Windows, even with a posix layer, like msys2.
395 * The following work-around is limited to detecting exact name repetition only,
396 * aka `filename` is considered different from `subdir/../filename` */
397 (void)file1Stat;
398 (void)file2Stat;
399 ret = !strcmp(fName1, fName2);
400#else
401 {
402 ret = (file1Stat->st_dev == file2Stat->st_dev)
403 && (file1Stat->st_ino == file2Stat->st_ino);
404 }
405#endif
406 UTIL_TRACE_RET(ret);
407 return ret;
shakeelraoe5811e52019-03-23 19:04:56 -0700408}
409
Yann Collet9a221402019-11-25 13:45:22 -0800410/* UTIL_isFIFO : distinguish named pipes */
411int UTIL_isFIFO(const char* infilename)
Bimba Shrestha8a397482019-10-22 15:23:22 -0700412{
Elliott Hughes44aba642023-09-12 20:18:59 +0000413 UTIL_TRACE_CALL("UTIL_isFIFO(%s)", infilename);
Bimba Shrestha8a397482019-10-22 15:23:22 -0700414/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
415#if PLATFORM_POSIX_VERSION >= 200112L
Elliott Hughes44aba642023-09-12 20:18:59 +0000416 {
417 stat_t statbuf;
418 if (UTIL_stat(infilename, &statbuf) && UTIL_isFIFOStat(&statbuf)) {
419 UTIL_TRACE_RET(1);
420 return 1;
421 }
422 }
Bimba Shrestha8a397482019-10-22 15:23:22 -0700423#endif
424 (void)infilename;
Elliott Hughes44aba642023-09-12 20:18:59 +0000425 UTIL_TRACE_RET(0);
Bimba Shrestha8a397482019-10-22 15:23:22 -0700426 return 0;
427}
Bimba Shrestha8a397482019-10-22 15:23:22 -0700428
W. Felix Handte44fa0522020-08-05 01:00:06 -0400429/* UTIL_isFIFO : distinguish named pipes */
430int UTIL_isFIFOStat(const stat_t* statbuf)
431{
432/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
433#if PLATFORM_POSIX_VERSION >= 200112L
434 if (S_ISFIFO(statbuf->st_mode)) return 1;
435#endif
436 (void)statbuf;
437 return 0;
438}
439
W. Felix Handte33f3e292021-05-04 16:24:46 -0400440/* UTIL_isBlockDevStat : distinguish named pipes */
441int UTIL_isBlockDevStat(const stat_t* statbuf)
442{
443/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
444#if PLATFORM_POSIX_VERSION >= 200112L
445 if (S_ISBLK(statbuf->st_mode)) return 1;
446#endif
447 (void)statbuf;
448 return 0;
449}
450
Yann Collet9a221402019-11-25 13:45:22 -0800451int UTIL_isLink(const char* infilename)
Rohit Jainf881ee82018-10-11 12:52:19 -0700452{
Elliott Hughes44aba642023-09-12 20:18:59 +0000453 UTIL_TRACE_CALL("UTIL_isLink(%s)", infilename);
Rohit Jainf881ee82018-10-11 12:52:19 -0700454/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
W. Felix Handted2c48042019-06-07 15:31:33 -0400455#if PLATFORM_POSIX_VERSION >= 200112L
Elliott Hughes44aba642023-09-12 20:18:59 +0000456 {
457 stat_t statbuf;
458 int const r = lstat(infilename, &statbuf);
459 if (!r && S_ISLNK(statbuf.st_mode)) {
460 UTIL_TRACE_RET(1);
461 return 1;
462 }
463 }
Rohit Jainf881ee82018-10-11 12:52:19 -0700464#endif
Rohit Jainf881ee82018-10-11 12:52:19 -0700465 (void)infilename;
Elliott Hughes44aba642023-09-12 20:18:59 +0000466 UTIL_TRACE_RET(0);
Rohit Jainf881ee82018-10-11 12:52:19 -0700467 return 0;
468}
469
Elliott Hughes44aba642023-09-12 20:18:59 +0000470static int g_fakeStdinIsConsole = 0;
471static int g_fakeStderrIsConsole = 0;
472static int g_fakeStdoutIsConsole = 0;
473
474int UTIL_isConsole(FILE* file)
475{
476 int ret;
477 UTIL_TRACE_CALL("UTIL_isConsole(%d)", fileno(file));
478 if (file == stdin && g_fakeStdinIsConsole)
479 ret = 1;
480 else if (file == stderr && g_fakeStderrIsConsole)
481 ret = 1;
482 else if (file == stdout && g_fakeStdoutIsConsole)
483 ret = 1;
484 else
485 ret = IS_CONSOLE(file);
486 UTIL_TRACE_RET(ret);
487 return ret;
488}
489
490void UTIL_fakeStdinIsConsole(void)
491{
492 g_fakeStdinIsConsole = 1;
493}
494void UTIL_fakeStdoutIsConsole(void)
495{
496 g_fakeStdoutIsConsole = 1;
497}
498void UTIL_fakeStderrIsConsole(void)
499{
500 g_fakeStderrIsConsole = 1;
501}
502
Rohit Jainf881ee82018-10-11 12:52:19 -0700503U64 UTIL_getFileSize(const char* infilename)
504{
W. Felix Handte69cb9e72020-08-05 00:21:41 -0400505 stat_t statbuf;
Elliott Hughes44aba642023-09-12 20:18:59 +0000506 UTIL_TRACE_CALL("UTIL_getFileSize(%s)", infilename);
507 if (!UTIL_stat(infilename, &statbuf)) {
508 UTIL_TRACE_RET(-1);
509 return UTIL_FILESIZE_UNKNOWN;
510 }
511 {
512 U64 const size = UTIL_getFileSizeStat(&statbuf);
513 UTIL_TRACE_RET((int)size);
514 return size;
515 }
W. Felix Handte44fa0522020-08-05 01:00:06 -0400516}
517
518U64 UTIL_getFileSizeStat(const stat_t* statbuf)
519{
520 if (!UTIL_isRegularFileStat(statbuf)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700521#if defined(_MSC_VER)
W. Felix Handte44fa0522020-08-05 01:00:06 -0400522 if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700523#elif defined(__MINGW32__) && defined (__MSVCRT__)
W. Felix Handte44fa0522020-08-05 01:00:06 -0400524 if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700525#else
W. Felix Handte44fa0522020-08-05 01:00:06 -0400526 if (!S_ISREG(statbuf->st_mode)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700527#endif
W. Felix Handte44fa0522020-08-05 01:00:06 -0400528 return (U64)statbuf->st_size;
Rohit Jainf881ee82018-10-11 12:52:19 -0700529}
530
Yann Colleteab69222021-09-03 12:51:02 -0700531UTIL_HumanReadableSize_t UTIL_makeHumanReadableSize(U64 size)
532{
W. Felix Handtebbb81c82021-06-09 13:05:44 -0400533 UTIL_HumanReadableSize_t hrs;
W. Felix Handtebbb81c82021-06-09 13:05:44 -0400534
W. Felix Handte9c340ce2021-06-09 16:13:00 -0400535 if (g_utilDisplayLevel > 3) {
W. Felix Handte464bfb02021-06-09 15:22:59 -0400536 /* In verbose mode, do not scale sizes down, except in the case of
537 * values that exceed the integral precision of a double. */
538 if (size >= (1ull << 53)) {
539 hrs.value = (double)size / (1ull << 20);
W. Felix Handte2af36872021-06-10 12:06:51 -0400540 hrs.suffix = " MiB";
W. Felix Handte464bfb02021-06-09 15:22:59 -0400541 /* At worst, a double representation of a maximal size will be
542 * accurate to better than tens of kilobytes. */
543 hrs.precision = 2;
544 } else {
545 hrs.value = (double)size;
W. Felix Handte93bb3682021-06-09 15:26:16 -0400546 hrs.suffix = " B";
W. Felix Handte464bfb02021-06-09 15:22:59 -0400547 hrs.precision = 0;
548 }
W. Felix Handtebbb81c82021-06-09 13:05:44 -0400549 } else {
W. Felix Handte464bfb02021-06-09 15:22:59 -0400550 /* In regular mode, scale sizes down and use suffixes. */
551 if (size >= (1ull << 60)) {
552 hrs.value = (double)size / (1ull << 60);
W. Felix Handte2af36872021-06-10 12:06:51 -0400553 hrs.suffix = " EiB";
W. Felix Handte464bfb02021-06-09 15:22:59 -0400554 } else if (size >= (1ull << 50)) {
555 hrs.value = (double)size / (1ull << 50);
W. Felix Handte2af36872021-06-10 12:06:51 -0400556 hrs.suffix = " PiB";
W. Felix Handte464bfb02021-06-09 15:22:59 -0400557 } else if (size >= (1ull << 40)) {
558 hrs.value = (double)size / (1ull << 40);
W. Felix Handte2af36872021-06-10 12:06:51 -0400559 hrs.suffix = " TiB";
W. Felix Handte464bfb02021-06-09 15:22:59 -0400560 } else if (size >= (1ull << 30)) {
561 hrs.value = (double)size / (1ull << 30);
W. Felix Handte2af36872021-06-10 12:06:51 -0400562 hrs.suffix = " GiB";
W. Felix Handte464bfb02021-06-09 15:22:59 -0400563 } else if (size >= (1ull << 20)) {
564 hrs.value = (double)size / (1ull << 20);
W. Felix Handte2af36872021-06-10 12:06:51 -0400565 hrs.suffix = " MiB";
W. Felix Handte464bfb02021-06-09 15:22:59 -0400566 } else if (size >= (1ull << 10)) {
567 hrs.value = (double)size / (1ull << 10);
W. Felix Handte2af36872021-06-10 12:06:51 -0400568 hrs.suffix = " KiB";
W. Felix Handte464bfb02021-06-09 15:22:59 -0400569 } else {
570 hrs.value = (double)size;
W. Felix Handte93bb3682021-06-09 15:26:16 -0400571 hrs.suffix = " B";
W. Felix Handte464bfb02021-06-09 15:22:59 -0400572 }
573
574 if (hrs.value >= 100 || (U64)hrs.value == size) {
575 hrs.precision = 0;
576 } else if (hrs.value >= 10) {
577 hrs.precision = 1;
578 } else if (hrs.value > 1) {
579 hrs.precision = 2;
580 } else {
581 hrs.precision = 3;
582 }
W. Felix Handtebbb81c82021-06-09 13:05:44 -0400583 }
584
585 return hrs;
586}
Rohit Jainf881ee82018-10-11 12:52:19 -0700587
Yann Collet5fb84ca2019-10-25 17:34:29 -0700588U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles)
Rohit Jainf881ee82018-10-11 12:52:19 -0700589{
590 U64 total = 0;
Rohit Jainf881ee82018-10-11 12:52:19 -0700591 unsigned n;
Elliott Hughes44aba642023-09-12 20:18:59 +0000592 UTIL_TRACE_CALL("UTIL_getTotalFileSize(%u)", nbFiles);
Rohit Jainf881ee82018-10-11 12:52:19 -0700593 for (n=0; n<nbFiles; n++) {
594 U64 const size = UTIL_getFileSize(fileNamesTable[n]);
Elliott Hughes44aba642023-09-12 20:18:59 +0000595 if (size == UTIL_FILESIZE_UNKNOWN) {
596 UTIL_TRACE_RET(-1);
597 return UTIL_FILESIZE_UNKNOWN;
598 }
Rohit Jainf881ee82018-10-11 12:52:19 -0700599 total += size;
600 }
Elliott Hughes44aba642023-09-12 20:18:59 +0000601 UTIL_TRACE_RET((int)total);
Yann Collet5fb84ca2019-10-25 17:34:29 -0700602 return total;
Rohit Jainf881ee82018-10-11 12:52:19 -0700603}
604
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100605
Yann Collet1ead0c52019-10-25 16:36:59 -0700606/* condition : @file must be valid, and not have reached its end.
Yann Colletd9c634e2019-10-28 15:03:32 -0700607 * @return : length of line written into @buf, ended with `\0` instead of '\n',
Yann Collet1ead0c52019-10-25 16:36:59 -0700608 * or 0, if there is no new line */
609static size_t readLineFromFile(char* buf, size_t len, FILE* file)
610{
Yann Collet1ead0c52019-10-25 16:36:59 -0700611 assert(!feof(file));
Yann Colletdf05b2b2021-05-05 18:01:55 -0700612 if ( fgets(buf, (int) len, file) == NULL ) return 0;
Yann Colletd9c634e2019-10-28 15:03:32 -0700613 { size_t linelen = strlen(buf);
614 if (strlen(buf)==0) return 0;
615 if (buf[linelen-1] == '\n') linelen--;
616 buf[linelen] = '\0';
617 return linelen+1;
618 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100619}
620
Yann Collet1ead0c52019-10-25 16:36:59 -0700621/* Conditions :
622 * size of @inputFileName file must be < @dstCapacity
623 * @dst must be initialized
624 * @return : nb of lines
625 * or -1 if there's an error
626 */
627static int
628readLinesFromFile(void* dst, size_t dstCapacity,
629 const char* inputFileName)
630{
631 int nbFiles = 0;
Yann Collet3e5c81e2019-10-26 00:01:11 -0700632 size_t pos = 0;
Yann Collet1ead0c52019-10-25 16:36:59 -0700633 char* const buf = (char*)dst;
634 FILE* const inputFile = fopen(inputFileName, "r");
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100635
Yann Collet1ead0c52019-10-25 16:36:59 -0700636 assert(dst != NULL);
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100637
Yann Collet1ead0c52019-10-25 16:36:59 -0700638 if(!inputFile) {
639 if (g_utilDisplayLevel >= 1) perror("zstd:util:readLinesFromFile");
640 return -1;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100641 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100642
Yann Collet1ead0c52019-10-25 16:36:59 -0700643 while ( !feof(inputFile) ) {
644 size_t const lineLength = readLineFromFile(buf+pos, dstCapacity-pos, inputFile);
645 if (lineLength == 0) break;
Elliott Hughes44aba642023-09-12 20:18:59 +0000646 assert(pos + lineLength <= dstCapacity); /* '=' for inputFile not terminated with '\n' */
Yann Colletd9c634e2019-10-28 15:03:32 -0700647 pos += lineLength;
Yann Collet1ead0c52019-10-25 16:36:59 -0700648 ++nbFiles;
Yann Collet1ead0c52019-10-25 16:36:59 -0700649 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100650
Yann Collet1ead0c52019-10-25 16:36:59 -0700651 CONTROL( fclose(inputFile) == 0 );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100652
Yann Collet1ead0c52019-10-25 16:36:59 -0700653 return nbFiles;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100654}
655
656/*Note: buf is not freed in case function successfully created table because filesTable->fileNames[0] = buf*/
657FileNamesTable*
Yann Collet1ead0c52019-10-25 16:36:59 -0700658UTIL_createFileNamesTable_fromFileName(const char* inputFileName)
659{
660 size_t nbFiles = 0;
661 char* buf;
662 size_t bufSize;
663 size_t pos = 0;
W. Felix Handte7238cca2020-08-05 01:08:34 -0400664 stat_t statbuf;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100665
W. Felix Handte7238cca2020-08-05 01:08:34 -0400666 if (!UTIL_stat(inputFileName, &statbuf) || !UTIL_isRegularFileStat(&statbuf))
Yann Collet1ead0c52019-10-25 16:36:59 -0700667 return NULL;
Ahmed Abdellah47712c92019-10-24 10:30:05 +0100668
W. Felix Handte7238cca2020-08-05 01:08:34 -0400669 { U64 const inputFileSize = UTIL_getFileSizeStat(&statbuf);
Yann Collet1ead0c52019-10-25 16:36:59 -0700670 if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE)
671 return NULL;
Yann Collet12efa1e2019-10-26 00:27:32 -0700672 bufSize = (size_t)(inputFileSize + 1); /* (+1) to add '\0' at the end of last filename */
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100673 }
674
Yann Collet1ead0c52019-10-25 16:36:59 -0700675 buf = (char*) malloc(bufSize);
676 CONTROL( buf != NULL );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100677
Yann Collet1ead0c52019-10-25 16:36:59 -0700678 { int const ret_nbFiles = readLinesFromFile(buf, bufSize, inputFileName);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100679
Yann Collet1ead0c52019-10-25 16:36:59 -0700680 if (ret_nbFiles <= 0) {
681 free(buf);
682 return NULL;
683 }
684 nbFiles = (size_t)ret_nbFiles;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100685 }
686
Yann Collet1ead0c52019-10-25 16:36:59 -0700687 { const char** filenamesTable = (const char**) malloc(nbFiles * sizeof(*filenamesTable));
688 CONTROL(filenamesTable != NULL);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100689
Yann Collet1ead0c52019-10-25 16:36:59 -0700690 { size_t fnb;
691 for (fnb = 0, pos = 0; fnb < nbFiles; fnb++) {
692 filenamesTable[fnb] = buf+pos;
693 pos += strlen(buf+pos)+1; /* +1 for the finishing `\0` */
694 } }
695 assert(pos <= bufSize);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100696
Yann Collet9a3de0a2019-11-25 15:34:55 -0800697 return UTIL_assembleFileNamesTable(filenamesTable, nbFiles, buf);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100698 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100699}
700
Yann Colleta49417b2019-12-02 14:28:18 -0800701static FileNamesTable*
702UTIL_assembleFileNamesTable2(const char** filenames, size_t tableSize, size_t tableCapacity, char* buf)
Yann Collet1ead0c52019-10-25 16:36:59 -0700703{
704 FileNamesTable* const table = (FileNamesTable*) malloc(sizeof(*table));
Yann Collet96ee2072019-11-26 15:44:33 -0800705 CONTROL(table != NULL);
Yann Collet1ead0c52019-10-25 16:36:59 -0700706 table->fileNames = filenames;
707 table->buf = buf;
708 table->tableSize = tableSize;
Yann Colleta49417b2019-12-02 14:28:18 -0800709 table->tableCapacity = tableCapacity;
Yann Collet1ead0c52019-10-25 16:36:59 -0700710 return table;
Ahmed Abdellahcddb05e2019-10-24 14:42:37 +0100711}
712
Yann Colleta49417b2019-12-02 14:28:18 -0800713FileNamesTable*
714UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf)
715{
716 return UTIL_assembleFileNamesTable2(filenames, tableSize, tableSize, buf);
717}
718
Yann Collet1ead0c52019-10-25 16:36:59 -0700719void UTIL_freeFileNamesTable(FileNamesTable* table)
720{
721 if (table==NULL) return;
722 free((void*)table->fileNames);
723 free(table->buf);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100724 free(table);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100725}
726
Yann Colletb09f5932019-11-05 17:02:43 -0800727FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize)
728{
729 const char** const fnTable = (const char**)malloc(tableSize * sizeof(*fnTable));
730 FileNamesTable* fnt;
731 if (fnTable==NULL) return NULL;
Yann Collet9a3de0a2019-11-25 15:34:55 -0800732 fnt = UTIL_assembleFileNamesTable(fnTable, tableSize, NULL);
Yann Colletb09f5932019-11-05 17:02:43 -0800733 fnt->tableSize = 0; /* the table is empty */
734 return fnt;
735}
736
Elliott Hughes44aba642023-09-12 20:18:59 +0000737int UTIL_searchFileNamesTable(FileNamesTable* table, char const* name) {
738 size_t i;
739 for(i=0 ;i < table->tableSize; i++) {
740 if(!strcmp(table->fileNames[i], name)) {
741 return (int)i;
742 }
743 }
744 return -1;
745}
746
Yann Colletb09f5932019-11-05 17:02:43 -0800747void UTIL_refFilename(FileNamesTable* fnt, const char* filename)
748{
Yann Colletf622c0a2019-11-26 14:48:23 -0800749 assert(fnt->tableSize < fnt->tableCapacity);
Yann Colletb09f5932019-11-05 17:02:43 -0800750 fnt->fileNames[fnt->tableSize] = filename;
751 fnt->tableSize++;
752}
753
Yann Collet1ead0c52019-10-25 16:36:59 -0700754static size_t getTotalTableSize(FileNamesTable* table)
755{
756 size_t fnb = 0, totalSize = 0;
757 for(fnb = 0 ; fnb < table->tableSize && table->fileNames[fnb] ; ++fnb) {
758 totalSize += strlen(table->fileNames[fnb]) + 1; /* +1 to add '\0' at the end of each fileName */
759 }
760 return totalSize;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100761}
762
763FileNamesTable*
Yann Collet31a0abb2019-11-06 09:10:05 -0800764UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2)
Yann Collet1ead0c52019-10-25 16:36:59 -0700765{
766 unsigned newTableIdx = 0;
767 size_t pos = 0;
768 size_t newTotalTableSize;
769 char* buf;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100770
Yann Collet9a3de0a2019-11-25 15:34:55 -0800771 FileNamesTable* const newTable = UTIL_assembleFileNamesTable(NULL, 0, NULL);
Yann Collet1ead0c52019-10-25 16:36:59 -0700772 CONTROL( newTable != NULL );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100773
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100774 newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100775
Yann Collet1ead0c52019-10-25 16:36:59 -0700776 buf = (char*) calloc(newTotalTableSize, sizeof(*buf));
777 CONTROL ( buf != NULL );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100778
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100779 newTable->buf = buf;
Yann Collet1ead0c52019-10-25 16:36:59 -0700780 newTable->tableSize = table1->tableSize + table2->tableSize;
781 newTable->fileNames = (const char **) calloc(newTable->tableSize, sizeof(*(newTable->fileNames)));
782 CONTROL ( newTable->fileNames != NULL );
783
784 { unsigned idx1;
785 for( idx1=0 ; (idx1 < table1->tableSize) && table1->fileNames[idx1] && (pos < newTotalTableSize); ++idx1, ++newTableIdx) {
786 size_t const curLen = strlen(table1->fileNames[idx1]);
787 memcpy(buf+pos, table1->fileNames[idx1], curLen);
788 assert(newTableIdx <= newTable->tableSize);
789 newTable->fileNames[newTableIdx] = buf+pos;
790 pos += curLen+1;
791 } }
792
793 { unsigned idx2;
794 for( idx2=0 ; (idx2 < table2->tableSize) && table2->fileNames[idx2] && (pos < newTotalTableSize) ; ++idx2, ++newTableIdx) {
795 size_t const curLen = strlen(table2->fileNames[idx2]);
796 memcpy(buf+pos, table2->fileNames[idx2], curLen);
Elliott Hughes44aba642023-09-12 20:18:59 +0000797 assert(newTableIdx < newTable->tableSize);
Yann Collet1ead0c52019-10-25 16:36:59 -0700798 newTable->fileNames[newTableIdx] = buf+pos;
799 pos += curLen+1;
800 } }
801 assert(pos <= newTotalTableSize);
Yann Collet1ead0c52019-10-25 16:36:59 -0700802 newTable->tableSize = newTableIdx;
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100803
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100804 UTIL_freeFileNamesTable(table1);
805 UTIL_freeFileNamesTable(table2);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100806
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100807 return newTable;
808}
809
Rohit Jainc7251e52018-10-11 18:05:15 -0700810#ifdef _WIN32
Yann Collet76b9e422019-11-05 14:59:45 -0800811static int UTIL_prepareFileList(const char* dirName,
812 char** bufStart, size_t* pos,
813 char** bufEnd, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -0700814{
815 char* path;
Yann Collet74d872e2019-10-25 18:26:30 -0700816 size_t dirLength, pathLength;
817 int nbFiles = 0;
Rohit Jain705e0b12018-10-11 15:51:57 -0700818 WIN32_FIND_DATAA cFile;
819 HANDLE hFile;
820
Yann Collet1ead0c52019-10-25 16:36:59 -0700821 dirLength = strlen(dirName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700822 path = (char*) malloc(dirLength + 3);
823 if (!path) return 0;
824
825 memcpy(path, dirName, dirLength);
826 path[dirLength] = '\\';
827 path[dirLength+1] = '*';
828 path[dirLength+2] = 0;
829
830 hFile=FindFirstFileA(path, &cFile);
831 if (hFile == INVALID_HANDLE_VALUE) {
832 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
833 return 0;
834 }
835 free(path);
836
837 do {
Yann Collet1ead0c52019-10-25 16:36:59 -0700838 size_t const fnameLength = strlen(cFile.cFileName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700839 path = (char*) malloc(dirLength + fnameLength + 2);
840 if (!path) { FindClose(hFile); return 0; }
841 memcpy(path, dirName, dirLength);
842 path[dirLength] = '\\';
843 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
844 pathLength = dirLength+1+fnameLength;
845 path[pathLength] = 0;
846 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800847 if ( strcmp (cFile.cFileName, "..") == 0
848 || strcmp (cFile.cFileName, ".") == 0 )
849 continue;
850 /* Recursively call "UTIL_prepareFileList" with the new path. */
851 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);
Rohit Jain705e0b12018-10-11 15:51:57 -0700852 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
Yann Collet72dbf1b2018-12-20 12:27:12 -0800853 } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
854 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
855 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) {
Rohit Jain705e0b12018-10-11 15:51:57 -0700856 if (*bufStart + *pos + pathLength >= *bufEnd) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800857 ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
Rohit Jain705e0b12018-10-11 15:51:57 -0700858 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
Rohit Jain705e0b12018-10-11 15:51:57 -0700859 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
Yann Collet72dbf1b2018-12-20 12:27:12 -0800860 *bufEnd = *bufStart + newListSize;
Rohit Jain705e0b12018-10-11 15:51:57 -0700861 }
862 if (*bufStart + *pos + pathLength < *bufEnd) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800863 memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */);
Rohit Jain705e0b12018-10-11 15:51:57 -0700864 *pos += pathLength + 1;
865 nbFiles++;
Yann Collet1ead0c52019-10-25 16:36:59 -0700866 } }
Rohit Jain705e0b12018-10-11 15:51:57 -0700867 free(path);
868 } while (FindNextFileA(hFile, &cFile));
869
870 FindClose(hFile);
871 return nbFiles;
872}
873
874#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
875
Yann Collet76b9e422019-11-05 14:59:45 -0800876static int UTIL_prepareFileList(const char *dirName,
877 char** bufStart, size_t* pos,
878 char** bufEnd, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -0700879{
Yann Collet1ead0c52019-10-25 16:36:59 -0700880 DIR* dir;
881 struct dirent * entry;
882 size_t dirLength;
Yann Colleta71256a2019-10-17 11:01:20 -0700883 int nbFiles = 0;
Rohit Jain705e0b12018-10-11 15:51:57 -0700884
885 if (!(dir = opendir(dirName))) {
886 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
887 return 0;
888 }
889
Yann Colleta71256a2019-10-17 11:01:20 -0700890 dirLength = strlen(dirName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700891 errno = 0;
892 while ((entry = readdir(dir)) != NULL) {
Yann Collet1ead0c52019-10-25 16:36:59 -0700893 char* path;
894 size_t fnameLength, pathLength;
Rohit Jain705e0b12018-10-11 15:51:57 -0700895 if (strcmp (entry->d_name, "..") == 0 ||
896 strcmp (entry->d_name, ".") == 0) continue;
Yann Colleta71256a2019-10-17 11:01:20 -0700897 fnameLength = strlen(entry->d_name);
Rohit Jain705e0b12018-10-11 15:51:57 -0700898 path = (char*) malloc(dirLength + fnameLength + 2);
899 if (!path) { closedir(dir); return 0; }
900 memcpy(path, dirName, dirLength);
901
902 path[dirLength] = '/';
903 memcpy(path+dirLength+1, entry->d_name, fnameLength);
904 pathLength = dirLength+1+fnameLength;
905 path[pathLength] = 0;
906
907 if (!followLinks && UTIL_isLink(path)) {
908 UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
LeeYoung624793b94b2019-07-25 21:07:57 +0800909 free(path);
Rohit Jain705e0b12018-10-11 15:51:57 -0700910 continue;
911 }
912
913 if (UTIL_isDirectory(path)) {
914 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
915 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
916 } else {
917 if (*bufStart + *pos + pathLength >= *bufEnd) {
918 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
Yann Colleta71256a2019-10-17 11:01:20 -0700919 assert(newListSize >= 0);
920 *bufStart = (char*)UTIL_realloc(*bufStart, (size_t)newListSize);
Elliott Hughes44aba642023-09-12 20:18:59 +0000921 if (*bufStart != NULL) {
922 *bufEnd = *bufStart + newListSize;
923 } else {
924 free(path); closedir(dir); return 0;
925 }
Rohit Jain705e0b12018-10-11 15:51:57 -0700926 }
927 if (*bufStart + *pos + pathLength < *bufEnd) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800928 memcpy(*bufStart + *pos, path, pathLength + 1); /* with final \0 */
Rohit Jain705e0b12018-10-11 15:51:57 -0700929 *pos += pathLength + 1;
930 nbFiles++;
Yann Collet1ead0c52019-10-25 16:36:59 -0700931 } }
Rohit Jain705e0b12018-10-11 15:51:57 -0700932 free(path);
933 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
934 }
935
936 if (errno != 0) {
Yann Collet96ee2072019-11-26 15:44:33 -0800937 UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s \n", dirName, strerror(errno));
Rohit Jain705e0b12018-10-11 15:51:57 -0700938 free(*bufStart);
939 *bufStart = NULL;
940 }
941 closedir(dir);
942 return nbFiles;
943}
944
945#else
946
Yann Collet76b9e422019-11-05 14:59:45 -0800947static int UTIL_prepareFileList(const char *dirName,
948 char** bufStart, size_t* pos,
949 char** bufEnd, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -0700950{
951 (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks;
Yann Collet96ee2072019-11-26 15:44:33 -0800952 UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE) \n", dirName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700953 return 0;
954}
955
956#endif /* #ifdef _WIN32 */
957
Shashank Tavildar0f2bff22019-10-28 18:21:47 -0700958int UTIL_isCompressedFile(const char *inputName, const char *extensionList[])
Shashank Tavildar48f85662019-10-25 15:49:11 -0700959{
Shashank Tavildar9ab6a742019-10-29 12:27:54 -0700960 const char* ext = UTIL_getFileExtension(inputName);
Shashank Tavildar3c1649f2019-10-29 15:59:20 -0700961 while(*extensionList!=NULL)
Shashank Tavildarc5060992019-10-29 12:56:04 -0700962 {
Shashank Tavildar3c1649f2019-10-29 15:59:20 -0700963 const int isCompressedExtension = strcmp(ext,*extensionList);
964 if(isCompressedExtension==0)
965 return 1;
966 ++extensionList;
Shashank Tavildarc5060992019-10-29 12:56:04 -0700967 }
Shashank Tavildar02433e02019-10-28 14:54:54 -0700968 return 0;
Shashank Tavildar48f85662019-10-25 15:49:11 -0700969}
Shashank Tavildar0f2bff22019-10-28 18:21:47 -0700970
Shashank Tavildar9ab6a742019-10-29 12:27:54 -0700971/*Utility function to get file extension from file */
972const char* UTIL_getFileExtension(const char* infilename)
973{
974 const char* extension = strrchr(infilename, '.');
975 if(!extension || extension==infilename) return "";
976 return extension;
977}
978
Xin Xie9a8ccd42020-06-19 19:35:51 -0700979static int pathnameHas2Dots(const char *pathname)
980{
W. Felix Handte61db5902021-02-26 12:29:42 -0500981 /* We need to figure out whether any ".." present in the path is a whole
982 * path token, which is the case if it is bordered on both sides by either
983 * the beginning/end of the path or by a directory separator.
984 */
985 const char *needle = pathname;
986 while (1) {
987 needle = strstr(needle, "..");
988
989 if (needle == NULL) {
990 return 0;
991 }
992
993 if ((needle == pathname || needle[-1] == PATH_SEP)
994 && (needle[2] == '\0' || needle[2] == PATH_SEP)) {
995 return 1;
996 }
997
998 /* increment so we search for the next match */
999 needle++;
1000 };
1001 return 0;
Xin Xie9a8ccd42020-06-19 19:35:51 -07001002}
1003
1004static int isFileNameValidForMirroredOutput(const char *filename)
1005{
1006 return !pathnameHas2Dots(filename);
1007}
1008
1009
1010#define DIR_DEFAULT_MODE 0755
1011static mode_t getDirMode(const char *dirName)
1012{
1013 stat_t st;
W. Felix Handte51ac0202020-08-10 15:28:02 -04001014 if (!UTIL_stat(dirName, &st)) {
Xin Xie9a8ccd42020-06-19 19:35:51 -07001015 UTIL_DISPLAY("zstd: failed to get DIR stats %s: %s\n", dirName, strerror(errno));
1016 return DIR_DEFAULT_MODE;
1017 }
W. Felix Handte51ac0202020-08-10 15:28:02 -04001018 if (!UTIL_isDirectoryStat(&st)) {
1019 UTIL_DISPLAY("zstd: expected directory: %s\n", dirName);
1020 return DIR_DEFAULT_MODE;
1021 }
Xin Xie9a8ccd42020-06-19 19:35:51 -07001022 return st.st_mode;
1023}
1024
1025static int makeDir(const char *dir, mode_t mode)
1026{
1027#if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__)
1028 int ret = _mkdir(dir);
1029 (void) mode;
1030#else
1031 int ret = mkdir(dir, mode);
1032#endif
1033 if (ret != 0) {
1034 if (errno == EEXIST)
1035 return 0;
1036 UTIL_DISPLAY("zstd: failed to create DIR %s: %s\n", dir, strerror(errno));
1037 }
1038 return ret;
1039}
1040
1041/* this function requires a mutable input string */
1042static void convertPathnameToDirName(char *pathname)
1043{
1044 size_t len = 0;
1045 char* pos = NULL;
1046 /* get dir name from pathname similar to 'dirname()' */
1047 assert(pathname != NULL);
1048
1049 /* remove trailing '/' chars */
1050 len = strlen(pathname);
1051 assert(len > 0);
1052 while (pathname[len] == PATH_SEP) {
1053 pathname[len] = '\0';
1054 len--;
1055 }
1056 if (len == 0) return;
1057
1058 /* if input is a single file, return '.' instead. i.e.
1059 * "xyz/abc/file.txt" => "xyz/abc"
1060 "./file.txt" => "."
1061 "file.txt" => "."
1062 */
1063 pos = strrchr(pathname, PATH_SEP);
1064 if (pos == NULL) {
1065 pathname[0] = '.';
1066 pathname[1] = '\0';
1067 } else {
1068 *pos = '\0';
1069 }
1070}
1071
1072/* pathname must be valid */
1073static const char* trimLeadingRootChar(const char *pathname)
1074{
1075 assert(pathname != NULL);
1076 if (pathname[0] == PATH_SEP)
1077 return pathname + 1;
1078 return pathname;
1079}
1080
1081/* pathname must be valid */
1082static const char* trimLeadingCurrentDirConst(const char *pathname)
1083{
1084 assert(pathname != NULL);
1085 if ((pathname[0] == '.') && (pathname[1] == PATH_SEP))
1086 return pathname + 2;
1087 return pathname;
1088}
1089
1090static char*
1091trimLeadingCurrentDir(char *pathname)
1092{
1093 /* 'union charunion' can do const-cast without compiler warning */
1094 union charunion {
1095 char *chr;
1096 const char* cchr;
1097 } ptr;
1098 ptr.cchr = trimLeadingCurrentDirConst(pathname);
1099 return ptr.chr;
1100}
1101
1102/* remove leading './' or '/' chars here */
1103static const char * trimPath(const char *pathname)
1104{
1105 return trimLeadingRootChar(
1106 trimLeadingCurrentDirConst(pathname));
1107}
1108
1109static char* mallocAndJoin2Dir(const char *dir1, const char *dir2)
1110{
Xin Xie9a8ccd42020-06-19 19:35:51 -07001111 assert(dir1 != NULL && dir2 != NULL);
Elliott Hughes44aba642023-09-12 20:18:59 +00001112 { const size_t dir1Size = strlen(dir1);
1113 const size_t dir2Size = strlen(dir2);
1114 char *outDirBuffer, *buffer;
Xin Xie9a8ccd42020-06-19 19:35:51 -07001115
Elliott Hughes44aba642023-09-12 20:18:59 +00001116 outDirBuffer = (char *) malloc(dir1Size + dir2Size + 2);
1117 CONTROL(outDirBuffer != NULL);
Xin Xie9a8ccd42020-06-19 19:35:51 -07001118
Elliott Hughes44aba642023-09-12 20:18:59 +00001119 memcpy(outDirBuffer, dir1, dir1Size);
1120 outDirBuffer[dir1Size] = '\0';
1121
1122 if (dir2[0] == '.')
1123 return outDirBuffer;
1124
1125 buffer = outDirBuffer + dir1Size;
1126 if (dir1Size > 0 && *(buffer - 1) != PATH_SEP) {
1127 *buffer = PATH_SEP;
1128 buffer++;
1129 }
1130 memcpy(buffer, dir2, dir2Size);
1131 buffer[dir2Size] = '\0';
1132
Xin Xie9a8ccd42020-06-19 19:35:51 -07001133 return outDirBuffer;
Xin Xie9a8ccd42020-06-19 19:35:51 -07001134 }
Xin Xie9a8ccd42020-06-19 19:35:51 -07001135}
1136
1137/* this function will return NULL if input srcFileName is not valid name for mirrored output path */
1138char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName)
1139{
1140 char* pathname = NULL;
1141 if (!isFileNameValidForMirroredOutput(srcFileName))
1142 return NULL;
1143
1144 pathname = mallocAndJoin2Dir(outDirRootName, trimPath(srcFileName));
1145
1146 convertPathnameToDirName(pathname);
1147 return pathname;
1148}
1149
1150static int
1151mirrorSrcDir(char* srcDirName, const char* outDirName)
1152{
1153 mode_t srcMode;
1154 int status = 0;
1155 char* newDir = mallocAndJoin2Dir(outDirName, trimPath(srcDirName));
1156 if (!newDir)
1157 return -ENOMEM;
1158
1159 srcMode = getDirMode(srcDirName);
1160 status = makeDir(newDir, srcMode);
1161 free(newDir);
1162 return status;
1163}
1164
1165static int
1166mirrorSrcDirRecursive(char* srcDirName, const char* outDirName)
1167{
1168 int status = 0;
1169 char* pp = trimLeadingCurrentDir(srcDirName);
1170 char* sp = NULL;
1171
1172 while ((sp = strchr(pp, PATH_SEP)) != NULL) {
1173 if (sp != pp) {
1174 *sp = '\0';
1175 status = mirrorSrcDir(srcDirName, outDirName);
1176 if (status != 0)
1177 return status;
1178 *sp = PATH_SEP;
1179 }
1180 pp = sp + 1;
1181 }
1182 status = mirrorSrcDir(srcDirName, outDirName);
1183 return status;
1184}
1185
1186static void
1187makeMirroredDestDirsWithSameSrcDirMode(char** srcDirNames, unsigned nbFile, const char* outDirName)
1188{
1189 unsigned int i = 0;
1190 for (i = 0; i < nbFile; i++)
1191 mirrorSrcDirRecursive(srcDirNames[i], outDirName);
1192}
1193
1194static int
1195firstIsParentOrSameDirOfSecond(const char* firstDir, const char* secondDir)
1196{
1197 size_t firstDirLen = strlen(firstDir),
1198 secondDirLen = strlen(secondDir);
1199 return firstDirLen <= secondDirLen &&
1200 (secondDir[firstDirLen] == PATH_SEP || secondDir[firstDirLen] == '\0') &&
1201 0 == strncmp(firstDir, secondDir, firstDirLen);
1202}
1203
1204static int compareDir(const void* pathname1, const void* pathname2) {
1205 /* sort it after remove the leading '/' or './'*/
1206 const char* s1 = trimPath(*(char * const *) pathname1);
1207 const char* s2 = trimPath(*(char * const *) pathname2);
1208 return strcmp(s1, s2);
1209}
1210
1211static void
1212makeUniqueMirroredDestDirs(char** srcDirNames, unsigned nbFile, const char* outDirName)
1213{
1214 unsigned int i = 0, uniqueDirNr = 0;
1215 char** uniqueDirNames = NULL;
1216
1217 if (nbFile == 0)
1218 return;
1219
1220 uniqueDirNames = (char** ) malloc(nbFile * sizeof (char *));
1221 CONTROL(uniqueDirNames != NULL);
1222
1223 /* if dirs is "a/b/c" and "a/b/c/d", we only need call:
1224 * we just need "a/b/c/d" */
1225 qsort((void *)srcDirNames, nbFile, sizeof(char*), compareDir);
1226
1227 uniqueDirNr = 1;
1228 uniqueDirNames[uniqueDirNr - 1] = srcDirNames[0];
1229 for (i = 1; i < nbFile; i++) {
1230 char* prevDirName = srcDirNames[i - 1];
1231 char* currDirName = srcDirNames[i];
1232
Dimitris Apostolouebbd6752021-11-13 10:04:04 +02001233 /* note: we always compare trimmed path, i.e.:
Xin Xie9a8ccd42020-06-19 19:35:51 -07001234 * src dir of "./foo" and "/foo" will be both saved into:
1235 * "outDirName/foo/" */
1236 if (!firstIsParentOrSameDirOfSecond(trimPath(prevDirName),
1237 trimPath(currDirName)))
1238 uniqueDirNr++;
1239
Elliott Hughes44aba642023-09-12 20:18:59 +00001240 /* we need to maintain original src dir name instead of trimmed
Dimitris Apostolouebbd6752021-11-13 10:04:04 +02001241 * dir, so we can retrieve the original src dir's mode_t */
Xin Xie9a8ccd42020-06-19 19:35:51 -07001242 uniqueDirNames[uniqueDirNr - 1] = currDirName;
1243 }
1244
1245 makeMirroredDestDirsWithSameSrcDirMode(uniqueDirNames, uniqueDirNr, outDirName);
1246
1247 free(uniqueDirNames);
1248}
1249
1250static void
1251makeMirroredDestDirs(char** srcFileNames, unsigned nbFile, const char* outDirName)
1252{
1253 unsigned int i = 0;
1254 for (i = 0; i < nbFile; ++i)
1255 convertPathnameToDirName(srcFileNames[i]);
1256 makeUniqueMirroredDestDirs(srcFileNames, nbFile, outDirName);
1257}
1258
1259void UTIL_mirrorSourceFilesDirectories(const char** inFileNames, unsigned int nbFile, const char* outDirName)
1260{
1261 unsigned int i = 0, validFilenamesNr = 0;
1262 char** srcFileNames = (char **) malloc(nbFile * sizeof (char *));
1263 CONTROL(srcFileNames != NULL);
1264
1265 /* check input filenames is valid */
1266 for (i = 0; i < nbFile; ++i) {
1267 if (isFileNameValidForMirroredOutput(inFileNames[i])) {
1268 char* fname = STRDUP(inFileNames[i]);
1269 CONTROL(fname != NULL);
1270 srcFileNames[validFilenamesNr++] = fname;
1271 }
1272 }
1273
1274 if (validFilenamesNr > 0) {
1275 makeDir(outDirName, DIR_DEFAULT_MODE);
1276 makeMirroredDestDirs(srcFileNames, validFilenamesNr, outDirName);
1277 }
1278
1279 for (i = 0; i < validFilenamesNr; i++)
1280 free(srcFileNames[i]);
1281 free(srcFileNames);
1282}
Yann Colletb09f5932019-11-05 17:02:43 -08001283
Yann Collet31a0abb2019-11-06 09:10:05 -08001284FileNamesTable*
Sen Huangf27e3262021-03-25 10:38:56 -07001285UTIL_createExpandedFNT(const char* const* inputNames, size_t nbIfns, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -07001286{
Yann Colletb09f5932019-11-05 17:02:43 -08001287 unsigned nbFiles;
Rohit Jain705e0b12018-10-11 15:51:57 -07001288 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
1289 char* bufend = buf + LIST_SIZE_INCREASE;
Rohit Jain705e0b12018-10-11 15:51:57 -07001290
1291 if (!buf) return NULL;
1292
Yann Colletd5b4a7e2019-11-26 17:46:57 -08001293 { size_t ifnNb, pos;
Yann Colletb09f5932019-11-05 17:02:43 -08001294 for (ifnNb=0, pos=0, nbFiles=0; ifnNb<nbIfns; ifnNb++) {
1295 if (!UTIL_isDirectory(inputNames[ifnNb])) {
1296 size_t const len = strlen(inputNames[ifnNb]);
1297 if (buf + pos + len >= bufend) {
1298 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
1299 assert(newListSize >= 0);
1300 buf = (char*)UTIL_realloc(buf, (size_t)newListSize);
Yann Colletb09f5932019-11-05 17:02:43 -08001301 if (!buf) return NULL;
Yann Colletd5b4a7e2019-11-26 17:46:57 -08001302 bufend = buf + newListSize;
Yann Colletb09f5932019-11-05 17:02:43 -08001303 }
1304 if (buf + pos + len < bufend) {
1305 memcpy(buf+pos, inputNames[ifnNb], len+1); /* including final \0 */
1306 pos += len + 1;
1307 nbFiles++;
1308 }
1309 } else {
1310 nbFiles += (unsigned)UTIL_prepareFileList(inputNames[ifnNb], &buf, &pos, &bufend, followLinks);
1311 if (buf == NULL) return NULL;
1312 } } }
Rohit Jain705e0b12018-10-11 15:51:57 -07001313
Yann Colleta49417b2019-12-02 14:28:18 -08001314 /* note : even if nbFiles==0, function returns a valid, though empty, FileNamesTable* object */
Rohit Jain705e0b12018-10-11 15:51:57 -07001315
Yann Colletd5b4a7e2019-11-26 17:46:57 -08001316 { size_t ifnNb, pos;
Yann Colleta49417b2019-12-02 14:28:18 -08001317 size_t const fntCapacity = nbFiles + 1; /* minimum 1, allows adding one reference, typically stdin */
1318 const char** const fileNamesTable = (const char**)malloc(fntCapacity * sizeof(*fileNamesTable));
Yann Colletb09f5932019-11-05 17:02:43 -08001319 if (!fileNamesTable) { free(buf); return NULL; }
Rohit Jain705e0b12018-10-11 15:51:57 -07001320
Yann Colletb09f5932019-11-05 17:02:43 -08001321 for (ifnNb = 0, pos = 0; ifnNb < nbFiles; ifnNb++) {
1322 fileNamesTable[ifnNb] = buf + pos;
1323 if (buf + pos > bufend) { free(buf); free((void*)fileNamesTable); return NULL; }
1324 pos += strlen(fileNamesTable[ifnNb]) + 1;
Yann Collet29e46ed2019-10-18 14:28:34 -07001325 }
Yann Colleta49417b2019-12-02 14:28:18 -08001326 return UTIL_assembleFileNamesTable2(fileNamesTable, nbFiles, fntCapacity, buf);
Rohit Jain705e0b12018-10-11 15:51:57 -07001327 }
Rohit Jain705e0b12018-10-11 15:51:57 -07001328}
1329
Yann Collet59a71162019-04-10 12:37:03 -07001330
Yann Collet31a0abb2019-11-06 09:10:05 -08001331void UTIL_expandFNT(FileNamesTable** fnt, int followLinks)
Yann Collet76b9e422019-11-05 14:59:45 -08001332{
Yann Collet31a0abb2019-11-06 09:10:05 -08001333 FileNamesTable* const newFNT = UTIL_createExpandedFNT((*fnt)->fileNames, (*fnt)->tableSize, followLinks);
Yann Colleta49417b2019-12-02 14:28:18 -08001334 CONTROL(newFNT != NULL);
Yann Collet31a0abb2019-11-06 09:10:05 -08001335 UTIL_freeFileNamesTable(*fnt);
1336 *fnt = newFNT;
Yann Collet76b9e422019-11-05 14:59:45 -08001337}
1338
Yann Colleta7e33e32019-11-06 14:42:13 -08001339FileNamesTable* UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames)
1340{
1341 size_t const sizeof_FNTable = nbFilenames * sizeof(*filenames);
1342 const char** const newFNTable = (const char**)malloc(sizeof_FNTable);
1343 if (newFNTable==NULL) return NULL;
Yann Collet9df49dc2019-11-06 15:23:44 -08001344 memcpy((void*)newFNTable, filenames, sizeof_FNTable); /* void* : mitigate a Visual compiler bug or limitation */
Yann Colletd5b4a7e2019-11-26 17:46:57 -08001345 return UTIL_assembleFileNamesTable(newFNTable, nbFilenames, NULL);
Yann Colleta7e33e32019-11-06 14:42:13 -08001346}
1347
Yann Collet72dbf1b2018-12-20 12:27:12 -08001348
Rohit Jaina47f6e62018-10-11 16:51:29 -07001349/*-****************************************
Binh Vo6a46e382021-06-16 09:38:43 -04001350* count the number of cores
Rohit Jaind6d240f2018-10-11 15:07:12 -07001351******************************************/
Rohit Jainc7251e52018-10-11 18:05:15 -07001352
Rohit Jain91b2fed2018-10-11 17:34:47 -07001353#if defined(_WIN32) || defined(WIN32)
1354
1355#include <windows.h>
1356
1357typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
1358
Binh Vo6a46e382021-06-16 09:38:43 -04001359DWORD CountSetBits(ULONG_PTR bitMask)
Rohit Jain91b2fed2018-10-11 17:34:47 -07001360{
Binh Vo6a46e382021-06-16 09:38:43 -04001361 DWORD LSHIFT = sizeof(ULONG_PTR)*8 - 1;
1362 DWORD bitSetCount = 0;
Yann Colleteab69222021-09-03 12:51:02 -07001363 ULONG_PTR bitTest = (ULONG_PTR)1 << LSHIFT;
Binh Vo6a46e382021-06-16 09:38:43 -04001364 DWORD i;
Yann Colleteab69222021-09-03 12:51:02 -07001365
Binh Vo6a46e382021-06-16 09:38:43 -04001366 for (i = 0; i <= LSHIFT; ++i)
1367 {
1368 bitSetCount += ((bitMask & bitTest)?1:0);
1369 bitTest/=2;
1370 }
1371
1372 return bitSetCount;
1373}
1374
1375int UTIL_countCores(int logical)
1376{
1377 static int numCores = 0;
1378 if (numCores != 0) return numCores;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001379
1380 { LPFN_GLPI glpi;
1381 BOOL done = FALSE;
1382 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
1383 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
1384 DWORD returnLength = 0;
1385 size_t byteOffset = 0;
1386
Yann Collet0492c572019-10-18 17:08:52 -07001387#if defined(_MSC_VER)
Yann Colletf3796372019-10-18 17:05:42 -07001388/* Visual Studio does not like the following cast */
1389# pragma warning( disable : 4054 ) /* conversion from function ptr to data ptr */
1390# pragma warning( disable : 4055 ) /* conversion from data ptr to function ptr */
1391#endif
Yann Collet1bd6c152019-10-18 15:45:31 -07001392 glpi = (LPFN_GLPI)(void*)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
1393 "GetLogicalProcessorInformation");
Rohit Jain91b2fed2018-10-11 17:34:47 -07001394
1395 if (glpi == NULL) {
1396 goto failed;
1397 }
1398
1399 while(!done) {
1400 DWORD rc = glpi(buffer, &returnLength);
1401 if (FALSE == rc) {
1402 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
1403 if (buffer)
1404 free(buffer);
1405 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
1406
1407 if (buffer == NULL) {
1408 perror("zstd");
1409 exit(1);
1410 }
1411 } else {
1412 /* some other error */
1413 goto failed;
1414 }
1415 } else {
1416 done = TRUE;
Yann Collet96ee2072019-11-26 15:44:33 -08001417 } }
Rohit Jain91b2fed2018-10-11 17:34:47 -07001418
1419 ptr = buffer;
1420
1421 while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
1422
1423 if (ptr->Relationship == RelationProcessorCore) {
Binh Vo6a46e382021-06-16 09:38:43 -04001424 if (logical)
1425 numCores += CountSetBits(ptr->ProcessorMask);
1426 else
1427 numCores++;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001428 }
1429
1430 ptr++;
1431 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
1432 }
1433
1434 free(buffer);
1435
Binh Vo6a46e382021-06-16 09:38:43 -04001436 return numCores;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001437 }
1438
1439failed:
1440 /* try to fall back on GetSystemInfo */
1441 { SYSTEM_INFO sysinfo;
1442 GetSystemInfo(&sysinfo);
Binh Vo6a46e382021-06-16 09:38:43 -04001443 numCores = sysinfo.dwNumberOfProcessors;
1444 if (numCores == 0) numCores = 1; /* just in case */
Rohit Jain91b2fed2018-10-11 17:34:47 -07001445 }
Binh Vo6a46e382021-06-16 09:38:43 -04001446 return numCores;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001447}
1448
1449#elif defined(__APPLE__)
1450
1451#include <sys/sysctl.h>
1452
1453/* Use apple-provided syscall
1454 * see: man 3 sysctl */
Binh Vo6a46e382021-06-16 09:38:43 -04001455int UTIL_countCores(int logical)
Rohit Jain91b2fed2018-10-11 17:34:47 -07001456{
Binh Vo6a46e382021-06-16 09:38:43 -04001457 static S32 numCores = 0; /* apple specifies int32_t */
1458 if (numCores != 0) return numCores;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001459
1460 { size_t size = sizeof(S32);
Binh Vo6a46e382021-06-16 09:38:43 -04001461 int const ret = sysctlbyname(logical ? "hw.logicalcpu" : "hw.physicalcpu", &numCores, &size, NULL, 0);
Rohit Jain91b2fed2018-10-11 17:34:47 -07001462 if (ret != 0) {
1463 if (errno == ENOENT) {
1464 /* entry not present, fall back on 1 */
Binh Vo6a46e382021-06-16 09:38:43 -04001465 numCores = 1;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001466 } else {
Binh Vo6a46e382021-06-16 09:38:43 -04001467 perror("zstd: can't get number of cpus");
Rohit Jain91b2fed2018-10-11 17:34:47 -07001468 exit(1);
1469 }
1470 }
1471
Binh Vo6a46e382021-06-16 09:38:43 -04001472 return numCores;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001473 }
1474}
1475
1476#elif defined(__linux__)
1477
1478/* parse /proc/cpuinfo
1479 * siblings / cpu cores should give hyperthreading ratio
1480 * otherwise fall back on sysconf */
Binh Vo6a46e382021-06-16 09:38:43 -04001481int UTIL_countCores(int logical)
Rohit Jain91b2fed2018-10-11 17:34:47 -07001482{
Binh Vo6a46e382021-06-16 09:38:43 -04001483 static int numCores = 0;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001484
Binh Vo6a46e382021-06-16 09:38:43 -04001485 if (numCores != 0) return numCores;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001486
Binh Vo6a46e382021-06-16 09:38:43 -04001487 numCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1488 if (numCores == -1) {
Rohit Jain91b2fed2018-10-11 17:34:47 -07001489 /* value not queryable, fall back on 1 */
Binh Vo6a46e382021-06-16 09:38:43 -04001490 return numCores = 1;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001491 }
1492
1493 /* try to determine if there's hyperthreading */
1494 { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
1495#define BUF_SIZE 80
1496 char buff[BUF_SIZE];
1497
1498 int siblings = 0;
1499 int cpu_cores = 0;
1500 int ratio = 1;
1501
1502 if (cpuinfo == NULL) {
1503 /* fall back on the sysconf value */
Binh Vo6a46e382021-06-16 09:38:43 -04001504 return numCores;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001505 }
1506
1507 /* assume the cpu cores/siblings values will be constant across all
1508 * present processors */
1509 while (!feof(cpuinfo)) {
1510 if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
1511 if (strncmp(buff, "siblings", 8) == 0) {
1512 const char* const sep = strchr(buff, ':');
LeeYoung624c5caaf52019-07-29 17:05:50 +08001513 if (sep == NULL || *sep == '\0') {
Rohit Jain91b2fed2018-10-11 17:34:47 -07001514 /* formatting was broken? */
1515 goto failed;
1516 }
1517
1518 siblings = atoi(sep + 1);
1519 }
1520 if (strncmp(buff, "cpu cores", 9) == 0) {
1521 const char* const sep = strchr(buff, ':');
LeeYoung624c5caaf52019-07-29 17:05:50 +08001522 if (sep == NULL || *sep == '\0') {
Rohit Jain91b2fed2018-10-11 17:34:47 -07001523 /* formatting was broken? */
1524 goto failed;
1525 }
1526
1527 cpu_cores = atoi(sep + 1);
1528 }
1529 } else if (ferror(cpuinfo)) {
1530 /* fall back on the sysconf value */
1531 goto failed;
Yann Collet96ee2072019-11-26 15:44:33 -08001532 } }
Paul Bone4d6c78f2021-03-02 20:31:23 +11001533 if (siblings && cpu_cores && siblings > cpu_cores) {
Rohit Jain91b2fed2018-10-11 17:34:47 -07001534 ratio = siblings / cpu_cores;
1535 }
Paul Boneeb1a09d2021-03-02 20:13:51 +11001536
Binh Vo6a46e382021-06-16 09:38:43 -04001537 if (ratio && numCores > ratio && !logical) {
1538 numCores = numCores / ratio;
Paul Bone4d6c78f2021-03-02 20:31:23 +11001539 }
Paul Boneeb1a09d2021-03-02 20:13:51 +11001540
Rohit Jain91b2fed2018-10-11 17:34:47 -07001541failed:
1542 fclose(cpuinfo);
Binh Vo6a46e382021-06-16 09:38:43 -04001543 return numCores;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001544 }
1545}
1546
Conrad Meyerfe826372019-01-04 11:57:12 -08001547#elif defined(__FreeBSD__)
Rohit Jain91b2fed2018-10-11 17:34:47 -07001548
Conrad Meyerfe826372019-01-04 11:57:12 -08001549#include <sys/param.h>
1550#include <sys/sysctl.h>
1551
1552/* Use physical core sysctl when available
1553 * see: man 4 smp, man 3 sysctl */
Binh Vo6a46e382021-06-16 09:38:43 -04001554int UTIL_countCores(int logical)
Conrad Meyerfe826372019-01-04 11:57:12 -08001555{
Binh Vo6a46e382021-06-16 09:38:43 -04001556 static int numCores = 0; /* freebsd sysctl is native int sized */
1557#if __FreeBSD_version >= 1300008
1558 static int perCore = 1;
1559#endif
1560 if (numCores != 0) return numCores;
Conrad Meyerfe826372019-01-04 11:57:12 -08001561
1562#if __FreeBSD_version >= 1300008
Binh Vo6a46e382021-06-16 09:38:43 -04001563 { size_t size = sizeof(numCores);
1564 int ret = sysctlbyname("kern.smp.cores", &numCores, &size, NULL, 0);
1565 if (ret == 0) {
1566 if (logical) {
1567 ret = sysctlbyname("kern.smp.threads_per_core", &perCore, &size, NULL, 0);
1568 /* default to physical cores if logical cannot be read */
1569 if (ret == 0)
1570 numCores *= perCore;
1571 }
1572
1573 return numCores;
1574 }
Conrad Meyerfe826372019-01-04 11:57:12 -08001575 if (errno != ENOENT) {
Binh Vo6a46e382021-06-16 09:38:43 -04001576 perror("zstd: can't get number of cpus");
Conrad Meyerfe826372019-01-04 11:57:12 -08001577 exit(1);
1578 }
1579 /* sysctl not present, fall through to older sysconf method */
1580 }
Binh Vo6a46e382021-06-16 09:38:43 -04001581#else
1582 /* suppress unused parameter warning */
1583 (void) logical;
Conrad Meyerfe826372019-01-04 11:57:12 -08001584#endif
1585
Binh Vo6a46e382021-06-16 09:38:43 -04001586 numCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1587 if (numCores == -1) {
Conrad Meyerfe826372019-01-04 11:57:12 -08001588 /* value not queryable, fall back on 1 */
Binh Vo6a46e382021-06-16 09:38:43 -04001589 numCores = 1;
Conrad Meyerfe826372019-01-04 11:57:12 -08001590 }
Binh Vo6a46e382021-06-16 09:38:43 -04001591 return numCores;
Conrad Meyerfe826372019-01-04 11:57:12 -08001592}
1593
Christoph Reiterd0dcaf52020-01-08 00:48:26 +01001594#elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__CYGWIN__)
Conrad Meyerfe826372019-01-04 11:57:12 -08001595
1596/* Use POSIX sysconf
1597 * see: man 3 sysconf */
Binh Vo6a46e382021-06-16 09:38:43 -04001598int UTIL_countCores(int logical)
Rohit Jain91b2fed2018-10-11 17:34:47 -07001599{
Binh Vo6a46e382021-06-16 09:38:43 -04001600 static int numCores = 0;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001601
binhdvod7e17362021-11-29 14:11:39 -05001602 /* suppress unused parameter warning */
1603 (void)logical;
1604
Binh Vo6a46e382021-06-16 09:38:43 -04001605 if (numCores != 0) return numCores;
1606
1607 numCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1608 if (numCores == -1) {
Rohit Jain91b2fed2018-10-11 17:34:47 -07001609 /* value not queryable, fall back on 1 */
Binh Vo6a46e382021-06-16 09:38:43 -04001610 return numCores = 1;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001611 }
Binh Vo6a46e382021-06-16 09:38:43 -04001612 return numCores;
Rohit Jain91b2fed2018-10-11 17:34:47 -07001613}
1614
1615#else
1616
Binh Vo6a46e382021-06-16 09:38:43 -04001617int UTIL_countCores(int logical)
Rohit Jain91b2fed2018-10-11 17:34:47 -07001618{
Elliott Hughes44aba642023-09-12 20:18:59 +00001619 /* suppress unused parameter warning */
1620 (void)logical;
1621
Rohit Jain91b2fed2018-10-11 17:34:47 -07001622 /* assume 1 */
1623 return 1;
1624}
1625
1626#endif
1627
Binh Vo6a46e382021-06-16 09:38:43 -04001628int UTIL_countPhysicalCores(void)
1629{
1630 return UTIL_countCores(0);
1631}
1632
1633int UTIL_countLogicalCores(void)
1634{
1635 return UTIL_countCores(1);
1636}
1637
Rohit Jainf881ee82018-10-11 12:52:19 -07001638#if defined (__cplusplus)
1639}
1640#endif