blob: 6b220f39ae8c7e9374768c0ec7d95017663917c1 [file] [log] [blame]
Rohit Jainf881ee82018-10-11 12:52:19 -07001/*
Nick Terrellac58c8d2020-03-26 15:19:05 -07002 * Copyright (c) 2016-2020, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
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
69/* A modified version of realloc().
Yann Collet76b9e422019-11-05 14:59:45 -080070 * If UTIL_realloc() fails the original block is freed.
71 */
72UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size)
73{
74 void *newptr = realloc(ptr, size);
75 if (newptr) return newptr;
76 free(ptr);
77 return NULL;
78}
79
Yann Collet9a221402019-11-25 13:45:22 -080080#if defined(_MSC_VER)
81 #define chmod _chmod
82#endif
83
Yann Collet76b9e422019-11-05 14:59:45 -080084
Yann Collet1ead0c52019-10-25 16:36:59 -070085/*-****************************************
86* Console log
87******************************************/
88int g_utilDisplayLevel;
89
senhuang42aab11ce2020-08-25 11:25:49 -040090int UTIL_requireUserConfirmationToProceed(const char* prompt, const char* abortMsg,
91 const char* acceptableLetters) {
92 int ch;
93 UTIL_DISPLAY("%s", prompt);
94 ch = getchar();
95 if (strchr(acceptableLetters, ch) == NULL) {
96 UTIL_DISPLAY("%s", abortMsg);
97 return 1;
98 }
99 /* flush the rest */
100 while ((ch!=EOF) && (ch!='\n'))
101 ch = getchar();
102
103 return 0;
104}
105
Yann Collet1ead0c52019-10-25 16:36:59 -0700106
Yann Collet9a221402019-11-25 13:45:22 -0800107/*-*************************************
108* Constants
109***************************************/
110#define LIST_SIZE_INCREASE (8*1024)
Yann Colletc71bd452019-11-26 11:20:26 -0800111#define MAX_FILE_OF_FILE_NAMES_SIZE (1<<20)*50
Yann Collet9a221402019-11-25 13:45:22 -0800112
113
114/*-*************************************
115* Functions
116***************************************/
Yann Collet1ead0c52019-10-25 16:36:59 -0700117
W. Felix Handteb11bea52020-08-05 00:09:29 -0400118int UTIL_stat(const char* filename, stat_t* statbuf)
119{
120#if defined(_MSC_VER)
121 return !_stat64(filename, statbuf);
122#elif defined(__MINGW32__) && defined (__MSVCRT__)
123 return !_stati64(filename, statbuf);
124#else
125 return !stat(filename, statbuf);
126#endif
127}
128
Rohit Jaind6d240f2018-10-11 15:07:12 -0700129int UTIL_isRegularFile(const char* infilename)
130{
131 stat_t statbuf;
W. Felix Handte51ac0202020-08-10 15:28:02 -0400132 return UTIL_stat(infilename, &statbuf) && UTIL_isRegularFileStat(&statbuf);
Rohit Jaind6d240f2018-10-11 15:07:12 -0700133}
134
W. Felix Handte44fa0522020-08-05 01:00:06 -0400135int UTIL_isRegularFileStat(const stat_t* statbuf)
136{
137#if defined(_MSC_VER)
138 return (statbuf->st_mode & S_IFREG) != 0;
139#else
140 return S_ISREG(statbuf->st_mode) != 0;
141#endif
142}
143
Yann Collet9a221402019-11-25 13:45:22 -0800144/* like chmod, but avoid changing permission of /dev/null */
W. Felix Handte0a8aacb2020-08-05 12:00:12 -0400145int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions)
Yann Collet9a221402019-11-25 13:45:22 -0800146{
W. Felix Handte0a8aacb2020-08-05 12:00:12 -0400147 stat_t localStatBuf;
148 if (statbuf == NULL) {
149 if (!UTIL_stat(filename, &localStatBuf)) return 0;
150 statbuf = &localStatBuf;
151 }
152 if (!UTIL_isRegularFileStat(statbuf)) return 0; /* pretend success, but don't change anything */
Yann Collet9a221402019-11-25 13:45:22 -0800153 return chmod(filename, permissions);
154}
155
W. Felix Handte1a1003f2020-08-05 00:35:21 -0400156int UTIL_setFileStat(const char *filename, const stat_t *statbuf)
Rohit Jaind6d240f2018-10-11 15:07:12 -0700157{
158 int res = 0;
Rohit Jaind6d240f2018-10-11 15:07:12 -0700159
W. Felix Handtec1449142020-08-05 12:10:42 -0400160 stat_t curStatBuf;
161 if (!UTIL_stat(filename, &curStatBuf) || !UTIL_isRegularFileStat(&curStatBuf))
Rohit Jaind6d240f2018-10-11 15:07:12 -0700162 return -1;
163
W. Felix Handtee1ec8002019-09-12 16:27:05 -0400164 /* set access and modification times */
W. Felix Handte56668352019-12-04 16:59:16 -0500165 /* We check that st_mtime is a macro here in order to give us confidence
166 * that struct stat has a struct timespec st_mtim member. We need this
167 * check because there are some platforms that claim to be POSIX 2008
168 * compliant but which do not have st_mtim... */
W. Felix Handte5af8cb72019-12-04 10:25:07 -0500169#if (PLATFORM_POSIX_VERSION >= 200809L) && defined(st_mtime)
170 {
171 /* (atime, mtime) */
172 struct timespec timebuf[2] = { {0, UTIME_NOW} };
173 timebuf[1] = statbuf->st_mtim;
174 res += utimensat(AT_FDCWD, filename, timebuf, 0);
175 }
176#else
W. Felix Handtee1ec8002019-09-12 16:27:05 -0400177 {
178 struct utimbuf timebuf;
179 timebuf.actime = time(NULL);
180 timebuf.modtime = statbuf->st_mtime;
181 res += utime(filename, &timebuf);
182 }
Rosen Penev41e90652019-07-30 17:17:07 -0700183#endif
Rohit Jaind6d240f2018-10-11 15:07:12 -0700184
185#if !defined(_WIN32)
186 res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */
187#endif
188
W. Felix Handtec1449142020-08-05 12:10:42 -0400189 res += UTIL_chmod(filename, &curStatBuf, statbuf->st_mode & 07777); /* Copy file permissions */
Rohit Jaind6d240f2018-10-11 15:07:12 -0700190
191 errno = 0;
192 return -res; /* number of errors is returned */
193}
Rohit Jainf881ee82018-10-11 12:52:19 -0700194
Yann Collet9a221402019-11-25 13:45:22 -0800195int UTIL_isDirectory(const char* infilename)
Rohit Jainf881ee82018-10-11 12:52:19 -0700196{
Rohit Jainf881ee82018-10-11 12:52:19 -0700197 stat_t statbuf;
W. Felix Handte51ac0202020-08-10 15:28:02 -0400198 return UTIL_stat(infilename, &statbuf) && UTIL_isDirectoryStat(&statbuf);
Rohit Jainf881ee82018-10-11 12:52:19 -0700199}
200
W. Felix Handte44fa0522020-08-05 01:00:06 -0400201int UTIL_isDirectoryStat(const stat_t* statbuf)
202{
203#if defined(_MSC_VER)
204 return (statbuf->st_mode & _S_IFDIR) != 0;
205#else
206 return S_ISDIR(statbuf->st_mode) != 0;
207#endif
208}
209
Sen Huangf80437c2019-10-02 11:08:20 -0400210int UTIL_compareStr(const void *p1, const void *p2) {
211 return strcmp(* (char * const *) p1, * (char * const *) p2);
212}
Sen Huanga9c807a2019-09-06 10:17:04 -0700213
Yann Collet00040432019-10-17 10:56:14 -0700214int UTIL_isSameFile(const char* fName1, const char* fName2)
shakeelraoe5811e52019-03-23 19:04:56 -0700215{
Yann Collet00040432019-10-17 10:56:14 -0700216 assert(fName1 != NULL); assert(fName2 != NULL);
217#if defined(_MSC_VER) || defined(_WIN32)
shakeelraoe5811e52019-03-23 19:04:56 -0700218 /* note : Visual does not support file identification by inode.
Yann Collet00040432019-10-17 10:56:14 -0700219 * inode does not work on Windows, even with a posix layer, like msys2.
shakeelraoe5811e52019-03-23 19:04:56 -0700220 * The following work-around is limited to detecting exact name repetition only,
221 * aka `filename` is considered different from `subdir/../filename` */
Yann Collet157479a2019-10-17 14:31:42 -0700222 return !strcmp(fName1, fName2);
shakeelraoe5811e52019-03-23 19:04:56 -0700223#else
Yann Collet00040432019-10-17 10:56:14 -0700224 { stat_t file1Stat;
225 stat_t file2Stat;
W. Felix Handte5fbc6ad2020-08-05 00:31:48 -0400226 return UTIL_stat(fName1, &file1Stat)
227 && UTIL_stat(fName2, &file2Stat)
Yann Collet00040432019-10-17 10:56:14 -0700228 && (file1Stat.st_dev == file2Stat.st_dev)
229 && (file1Stat.st_ino == file2Stat.st_ino);
230 }
shakeelraoe5811e52019-03-23 19:04:56 -0700231#endif
232}
233
Yann Collet9a221402019-11-25 13:45:22 -0800234/* UTIL_isFIFO : distinguish named pipes */
235int UTIL_isFIFO(const char* infilename)
Bimba Shrestha8a397482019-10-22 15:23:22 -0700236{
237/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
238#if PLATFORM_POSIX_VERSION >= 200112L
239 stat_t statbuf;
W. Felix Handte44fa0522020-08-05 01:00:06 -0400240 if (UTIL_stat(infilename, &statbuf) && UTIL_isFIFOStat(&statbuf)) return 1;
Bimba Shrestha8a397482019-10-22 15:23:22 -0700241#endif
242 (void)infilename;
243 return 0;
244}
Bimba Shrestha8a397482019-10-22 15:23:22 -0700245
W. Felix Handte44fa0522020-08-05 01:00:06 -0400246/* UTIL_isFIFO : distinguish named pipes */
247int UTIL_isFIFOStat(const stat_t* statbuf)
248{
249/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
250#if PLATFORM_POSIX_VERSION >= 200112L
251 if (S_ISFIFO(statbuf->st_mode)) return 1;
252#endif
253 (void)statbuf;
254 return 0;
255}
256
Yann Collet9a221402019-11-25 13:45:22 -0800257int UTIL_isLink(const char* infilename)
Rohit Jainf881ee82018-10-11 12:52:19 -0700258{
259/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
W. Felix Handted2c48042019-06-07 15:31:33 -0400260#if PLATFORM_POSIX_VERSION >= 200112L
Rohit Jainf881ee82018-10-11 12:52:19 -0700261 stat_t statbuf;
Yann Collet9a221402019-11-25 13:45:22 -0800262 int const r = lstat(infilename, &statbuf);
Rohit Jainf881ee82018-10-11 12:52:19 -0700263 if (!r && S_ISLNK(statbuf.st_mode)) return 1;
264#endif
Rohit Jainf881ee82018-10-11 12:52:19 -0700265 (void)infilename;
266 return 0;
267}
268
269U64 UTIL_getFileSize(const char* infilename)
270{
W. Felix Handte69cb9e72020-08-05 00:21:41 -0400271 stat_t statbuf;
272 if (!UTIL_stat(infilename, &statbuf)) return UTIL_FILESIZE_UNKNOWN;
W. Felix Handte44fa0522020-08-05 01:00:06 -0400273 return UTIL_getFileSizeStat(&statbuf);
274}
275
276U64 UTIL_getFileSizeStat(const stat_t* statbuf)
277{
278 if (!UTIL_isRegularFileStat(statbuf)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700279#if defined(_MSC_VER)
W. Felix Handte44fa0522020-08-05 01:00:06 -0400280 if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700281#elif defined(__MINGW32__) && defined (__MSVCRT__)
W. Felix Handte44fa0522020-08-05 01:00:06 -0400282 if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700283#else
W. Felix Handte44fa0522020-08-05 01:00:06 -0400284 if (!S_ISREG(statbuf->st_mode)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700285#endif
W. Felix Handte44fa0522020-08-05 01:00:06 -0400286 return (U64)statbuf->st_size;
Rohit Jainf881ee82018-10-11 12:52:19 -0700287}
288
289
Yann Collet5fb84ca2019-10-25 17:34:29 -0700290U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles)
Rohit Jainf881ee82018-10-11 12:52:19 -0700291{
292 U64 total = 0;
Rohit Jainf881ee82018-10-11 12:52:19 -0700293 unsigned n;
294 for (n=0; n<nbFiles; n++) {
295 U64 const size = UTIL_getFileSize(fileNamesTable[n]);
Yann Collet5fb84ca2019-10-25 17:34:29 -0700296 if (size == UTIL_FILESIZE_UNKNOWN) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700297 total += size;
298 }
Yann Collet5fb84ca2019-10-25 17:34:29 -0700299 return total;
Rohit Jainf881ee82018-10-11 12:52:19 -0700300}
301
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100302
Yann Collet1ead0c52019-10-25 16:36:59 -0700303/* condition : @file must be valid, and not have reached its end.
Yann Colletd9c634e2019-10-28 15:03:32 -0700304 * @return : length of line written into @buf, ended with `\0` instead of '\n',
Yann Collet1ead0c52019-10-25 16:36:59 -0700305 * or 0, if there is no new line */
306static size_t readLineFromFile(char* buf, size_t len, FILE* file)
307{
Yann Collet1ead0c52019-10-25 16:36:59 -0700308 assert(!feof(file));
Nick Terrellf6d00c02020-01-13 14:22:46 -0800309 /* Work around Cygwin problem when len == 1 it returns NULL. */
310 if (len <= 1) return 0;
311 CONTROL( fgets(buf, (int) len, file) );
Yann Colletd9c634e2019-10-28 15:03:32 -0700312 { size_t linelen = strlen(buf);
313 if (strlen(buf)==0) return 0;
314 if (buf[linelen-1] == '\n') linelen--;
315 buf[linelen] = '\0';
316 return linelen+1;
317 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100318}
319
Yann Collet1ead0c52019-10-25 16:36:59 -0700320/* Conditions :
321 * size of @inputFileName file must be < @dstCapacity
322 * @dst must be initialized
323 * @return : nb of lines
324 * or -1 if there's an error
325 */
326static int
327readLinesFromFile(void* dst, size_t dstCapacity,
328 const char* inputFileName)
329{
330 int nbFiles = 0;
Yann Collet3e5c81e2019-10-26 00:01:11 -0700331 size_t pos = 0;
Yann Collet1ead0c52019-10-25 16:36:59 -0700332 char* const buf = (char*)dst;
333 FILE* const inputFile = fopen(inputFileName, "r");
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100334
Yann Collet1ead0c52019-10-25 16:36:59 -0700335 assert(dst != NULL);
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100336
Yann Collet1ead0c52019-10-25 16:36:59 -0700337 if(!inputFile) {
338 if (g_utilDisplayLevel >= 1) perror("zstd:util:readLinesFromFile");
339 return -1;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100340 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100341
Yann Collet1ead0c52019-10-25 16:36:59 -0700342 while ( !feof(inputFile) ) {
343 size_t const lineLength = readLineFromFile(buf+pos, dstCapacity-pos, inputFile);
344 if (lineLength == 0) break;
345 assert(pos + lineLength < dstCapacity);
Yann Colletd9c634e2019-10-28 15:03:32 -0700346 pos += lineLength;
Yann Collet1ead0c52019-10-25 16:36:59 -0700347 ++nbFiles;
Yann Collet1ead0c52019-10-25 16:36:59 -0700348 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100349
Yann Collet1ead0c52019-10-25 16:36:59 -0700350 CONTROL( fclose(inputFile) == 0 );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100351
Yann Collet1ead0c52019-10-25 16:36:59 -0700352 return nbFiles;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100353}
354
355/*Note: buf is not freed in case function successfully created table because filesTable->fileNames[0] = buf*/
356FileNamesTable*
Yann Collet1ead0c52019-10-25 16:36:59 -0700357UTIL_createFileNamesTable_fromFileName(const char* inputFileName)
358{
359 size_t nbFiles = 0;
360 char* buf;
361 size_t bufSize;
362 size_t pos = 0;
W. Felix Handte7238cca2020-08-05 01:08:34 -0400363 stat_t statbuf;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100364
W. Felix Handte7238cca2020-08-05 01:08:34 -0400365 if (!UTIL_stat(inputFileName, &statbuf) || !UTIL_isRegularFileStat(&statbuf))
Yann Collet1ead0c52019-10-25 16:36:59 -0700366 return NULL;
Ahmed Abdellah47712c92019-10-24 10:30:05 +0100367
W. Felix Handte7238cca2020-08-05 01:08:34 -0400368 { U64 const inputFileSize = UTIL_getFileSizeStat(&statbuf);
Yann Collet1ead0c52019-10-25 16:36:59 -0700369 if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE)
370 return NULL;
Yann Collet12efa1e2019-10-26 00:27:32 -0700371 bufSize = (size_t)(inputFileSize + 1); /* (+1) to add '\0' at the end of last filename */
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100372 }
373
Yann Collet1ead0c52019-10-25 16:36:59 -0700374 buf = (char*) malloc(bufSize);
375 CONTROL( buf != NULL );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100376
Yann Collet1ead0c52019-10-25 16:36:59 -0700377 { int const ret_nbFiles = readLinesFromFile(buf, bufSize, inputFileName);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100378
Yann Collet1ead0c52019-10-25 16:36:59 -0700379 if (ret_nbFiles <= 0) {
380 free(buf);
381 return NULL;
382 }
383 nbFiles = (size_t)ret_nbFiles;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100384 }
385
Yann Collet1ead0c52019-10-25 16:36:59 -0700386 { const char** filenamesTable = (const char**) malloc(nbFiles * sizeof(*filenamesTable));
387 CONTROL(filenamesTable != NULL);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100388
Yann Collet1ead0c52019-10-25 16:36:59 -0700389 { size_t fnb;
390 for (fnb = 0, pos = 0; fnb < nbFiles; fnb++) {
391 filenamesTable[fnb] = buf+pos;
392 pos += strlen(buf+pos)+1; /* +1 for the finishing `\0` */
393 } }
394 assert(pos <= bufSize);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100395
Yann Collet9a3de0a2019-11-25 15:34:55 -0800396 return UTIL_assembleFileNamesTable(filenamesTable, nbFiles, buf);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100397 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100398}
399
Yann Colleta49417b2019-12-02 14:28:18 -0800400static FileNamesTable*
401UTIL_assembleFileNamesTable2(const char** filenames, size_t tableSize, size_t tableCapacity, char* buf)
Yann Collet1ead0c52019-10-25 16:36:59 -0700402{
403 FileNamesTable* const table = (FileNamesTable*) malloc(sizeof(*table));
Yann Collet96ee2072019-11-26 15:44:33 -0800404 CONTROL(table != NULL);
Yann Collet1ead0c52019-10-25 16:36:59 -0700405 table->fileNames = filenames;
406 table->buf = buf;
407 table->tableSize = tableSize;
Yann Colleta49417b2019-12-02 14:28:18 -0800408 table->tableCapacity = tableCapacity;
Yann Collet1ead0c52019-10-25 16:36:59 -0700409 return table;
Ahmed Abdellahcddb05e2019-10-24 14:42:37 +0100410}
411
Yann Colleta49417b2019-12-02 14:28:18 -0800412FileNamesTable*
413UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf)
414{
415 return UTIL_assembleFileNamesTable2(filenames, tableSize, tableSize, buf);
416}
417
Yann Collet1ead0c52019-10-25 16:36:59 -0700418void UTIL_freeFileNamesTable(FileNamesTable* table)
419{
420 if (table==NULL) return;
421 free((void*)table->fileNames);
422 free(table->buf);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100423 free(table);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100424}
425
Yann Colletb09f5932019-11-05 17:02:43 -0800426FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize)
427{
428 const char** const fnTable = (const char**)malloc(tableSize * sizeof(*fnTable));
429 FileNamesTable* fnt;
430 if (fnTable==NULL) return NULL;
Yann Collet9a3de0a2019-11-25 15:34:55 -0800431 fnt = UTIL_assembleFileNamesTable(fnTable, tableSize, NULL);
Yann Colletb09f5932019-11-05 17:02:43 -0800432 fnt->tableSize = 0; /* the table is empty */
433 return fnt;
434}
435
436void UTIL_refFilename(FileNamesTable* fnt, const char* filename)
437{
Yann Colletf622c0a2019-11-26 14:48:23 -0800438 assert(fnt->tableSize < fnt->tableCapacity);
Yann Colletb09f5932019-11-05 17:02:43 -0800439 fnt->fileNames[fnt->tableSize] = filename;
440 fnt->tableSize++;
441}
442
Yann Collet1ead0c52019-10-25 16:36:59 -0700443static size_t getTotalTableSize(FileNamesTable* table)
444{
445 size_t fnb = 0, totalSize = 0;
446 for(fnb = 0 ; fnb < table->tableSize && table->fileNames[fnb] ; ++fnb) {
447 totalSize += strlen(table->fileNames[fnb]) + 1; /* +1 to add '\0' at the end of each fileName */
448 }
449 return totalSize;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100450}
451
452FileNamesTable*
Yann Collet31a0abb2019-11-06 09:10:05 -0800453UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2)
Yann Collet1ead0c52019-10-25 16:36:59 -0700454{
455 unsigned newTableIdx = 0;
456 size_t pos = 0;
457 size_t newTotalTableSize;
458 char* buf;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100459
Yann Collet9a3de0a2019-11-25 15:34:55 -0800460 FileNamesTable* const newTable = UTIL_assembleFileNamesTable(NULL, 0, NULL);
Yann Collet1ead0c52019-10-25 16:36:59 -0700461 CONTROL( newTable != NULL );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100462
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100463 newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100464
Yann Collet1ead0c52019-10-25 16:36:59 -0700465 buf = (char*) calloc(newTotalTableSize, sizeof(*buf));
466 CONTROL ( buf != NULL );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100467
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100468 newTable->buf = buf;
Yann Collet1ead0c52019-10-25 16:36:59 -0700469 newTable->tableSize = table1->tableSize + table2->tableSize;
470 newTable->fileNames = (const char **) calloc(newTable->tableSize, sizeof(*(newTable->fileNames)));
471 CONTROL ( newTable->fileNames != NULL );
472
473 { unsigned idx1;
474 for( idx1=0 ; (idx1 < table1->tableSize) && table1->fileNames[idx1] && (pos < newTotalTableSize); ++idx1, ++newTableIdx) {
475 size_t const curLen = strlen(table1->fileNames[idx1]);
476 memcpy(buf+pos, table1->fileNames[idx1], curLen);
477 assert(newTableIdx <= newTable->tableSize);
478 newTable->fileNames[newTableIdx] = buf+pos;
479 pos += curLen+1;
480 } }
481
482 { unsigned idx2;
483 for( idx2=0 ; (idx2 < table2->tableSize) && table2->fileNames[idx2] && (pos < newTotalTableSize) ; ++idx2, ++newTableIdx) {
484 size_t const curLen = strlen(table2->fileNames[idx2]);
485 memcpy(buf+pos, table2->fileNames[idx2], curLen);
486 assert(newTableIdx <= newTable->tableSize);
487 newTable->fileNames[newTableIdx] = buf+pos;
488 pos += curLen+1;
489 } }
490 assert(pos <= newTotalTableSize);
Yann Collet1ead0c52019-10-25 16:36:59 -0700491 newTable->tableSize = newTableIdx;
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100492
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100493 UTIL_freeFileNamesTable(table1);
494 UTIL_freeFileNamesTable(table2);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100495
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100496 return newTable;
497}
498
Rohit Jainc7251e52018-10-11 18:05:15 -0700499#ifdef _WIN32
Yann Collet76b9e422019-11-05 14:59:45 -0800500static int UTIL_prepareFileList(const char* dirName,
501 char** bufStart, size_t* pos,
502 char** bufEnd, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -0700503{
504 char* path;
Yann Collet74d872e2019-10-25 18:26:30 -0700505 size_t dirLength, pathLength;
506 int nbFiles = 0;
Rohit Jain705e0b12018-10-11 15:51:57 -0700507 WIN32_FIND_DATAA cFile;
508 HANDLE hFile;
509
Yann Collet1ead0c52019-10-25 16:36:59 -0700510 dirLength = strlen(dirName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700511 path = (char*) malloc(dirLength + 3);
512 if (!path) return 0;
513
514 memcpy(path, dirName, dirLength);
515 path[dirLength] = '\\';
516 path[dirLength+1] = '*';
517 path[dirLength+2] = 0;
518
519 hFile=FindFirstFileA(path, &cFile);
520 if (hFile == INVALID_HANDLE_VALUE) {
521 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
522 return 0;
523 }
524 free(path);
525
526 do {
Yann Collet1ead0c52019-10-25 16:36:59 -0700527 size_t const fnameLength = strlen(cFile.cFileName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700528 path = (char*) malloc(dirLength + fnameLength + 2);
529 if (!path) { FindClose(hFile); return 0; }
530 memcpy(path, dirName, dirLength);
531 path[dirLength] = '\\';
532 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
533 pathLength = dirLength+1+fnameLength;
534 path[pathLength] = 0;
535 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800536 if ( strcmp (cFile.cFileName, "..") == 0
537 || strcmp (cFile.cFileName, ".") == 0 )
538 continue;
539 /* Recursively call "UTIL_prepareFileList" with the new path. */
540 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);
Rohit Jain705e0b12018-10-11 15:51:57 -0700541 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
Yann Collet72dbf1b2018-12-20 12:27:12 -0800542 } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
543 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
544 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) {
Rohit Jain705e0b12018-10-11 15:51:57 -0700545 if (*bufStart + *pos + pathLength >= *bufEnd) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800546 ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
Rohit Jain705e0b12018-10-11 15:51:57 -0700547 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
Rohit Jain705e0b12018-10-11 15:51:57 -0700548 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
Yann Collet72dbf1b2018-12-20 12:27:12 -0800549 *bufEnd = *bufStart + newListSize;
Rohit Jain705e0b12018-10-11 15:51:57 -0700550 }
551 if (*bufStart + *pos + pathLength < *bufEnd) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800552 memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */);
Rohit Jain705e0b12018-10-11 15:51:57 -0700553 *pos += pathLength + 1;
554 nbFiles++;
Yann Collet1ead0c52019-10-25 16:36:59 -0700555 } }
Rohit Jain705e0b12018-10-11 15:51:57 -0700556 free(path);
557 } while (FindNextFileA(hFile, &cFile));
558
559 FindClose(hFile);
560 return nbFiles;
561}
562
563#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
564
Yann Collet76b9e422019-11-05 14:59:45 -0800565static int UTIL_prepareFileList(const char *dirName,
566 char** bufStart, size_t* pos,
567 char** bufEnd, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -0700568{
Yann Collet1ead0c52019-10-25 16:36:59 -0700569 DIR* dir;
570 struct dirent * entry;
571 size_t dirLength;
Yann Colleta71256a2019-10-17 11:01:20 -0700572 int nbFiles = 0;
Rohit Jain705e0b12018-10-11 15:51:57 -0700573
574 if (!(dir = opendir(dirName))) {
575 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
576 return 0;
577 }
578
Yann Colleta71256a2019-10-17 11:01:20 -0700579 dirLength = strlen(dirName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700580 errno = 0;
581 while ((entry = readdir(dir)) != NULL) {
Yann Collet1ead0c52019-10-25 16:36:59 -0700582 char* path;
583 size_t fnameLength, pathLength;
Rohit Jain705e0b12018-10-11 15:51:57 -0700584 if (strcmp (entry->d_name, "..") == 0 ||
585 strcmp (entry->d_name, ".") == 0) continue;
Yann Colleta71256a2019-10-17 11:01:20 -0700586 fnameLength = strlen(entry->d_name);
Rohit Jain705e0b12018-10-11 15:51:57 -0700587 path = (char*) malloc(dirLength + fnameLength + 2);
588 if (!path) { closedir(dir); return 0; }
589 memcpy(path, dirName, dirLength);
590
591 path[dirLength] = '/';
592 memcpy(path+dirLength+1, entry->d_name, fnameLength);
593 pathLength = dirLength+1+fnameLength;
594 path[pathLength] = 0;
595
596 if (!followLinks && UTIL_isLink(path)) {
597 UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
LeeYoung624793b94b2019-07-25 21:07:57 +0800598 free(path);
Rohit Jain705e0b12018-10-11 15:51:57 -0700599 continue;
600 }
601
602 if (UTIL_isDirectory(path)) {
603 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
604 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
605 } else {
606 if (*bufStart + *pos + pathLength >= *bufEnd) {
607 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
Yann Colleta71256a2019-10-17 11:01:20 -0700608 assert(newListSize >= 0);
609 *bufStart = (char*)UTIL_realloc(*bufStart, (size_t)newListSize);
Rohit Jain705e0b12018-10-11 15:51:57 -0700610 *bufEnd = *bufStart + newListSize;
611 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
612 }
613 if (*bufStart + *pos + pathLength < *bufEnd) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800614 memcpy(*bufStart + *pos, path, pathLength + 1); /* with final \0 */
Rohit Jain705e0b12018-10-11 15:51:57 -0700615 *pos += pathLength + 1;
616 nbFiles++;
Yann Collet1ead0c52019-10-25 16:36:59 -0700617 } }
Rohit Jain705e0b12018-10-11 15:51:57 -0700618 free(path);
619 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
620 }
621
622 if (errno != 0) {
Yann Collet96ee2072019-11-26 15:44:33 -0800623 UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s \n", dirName, strerror(errno));
Rohit Jain705e0b12018-10-11 15:51:57 -0700624 free(*bufStart);
625 *bufStart = NULL;
626 }
627 closedir(dir);
628 return nbFiles;
629}
630
631#else
632
Yann Collet76b9e422019-11-05 14:59:45 -0800633static int UTIL_prepareFileList(const char *dirName,
634 char** bufStart, size_t* pos,
635 char** bufEnd, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -0700636{
637 (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks;
Yann Collet96ee2072019-11-26 15:44:33 -0800638 UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE) \n", dirName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700639 return 0;
640}
641
642#endif /* #ifdef _WIN32 */
643
Shashank Tavildar0f2bff22019-10-28 18:21:47 -0700644int UTIL_isCompressedFile(const char *inputName, const char *extensionList[])
Shashank Tavildar48f85662019-10-25 15:49:11 -0700645{
Shashank Tavildar9ab6a742019-10-29 12:27:54 -0700646 const char* ext = UTIL_getFileExtension(inputName);
Shashank Tavildar3c1649f2019-10-29 15:59:20 -0700647 while(*extensionList!=NULL)
Shashank Tavildarc5060992019-10-29 12:56:04 -0700648 {
Shashank Tavildar3c1649f2019-10-29 15:59:20 -0700649 const int isCompressedExtension = strcmp(ext,*extensionList);
650 if(isCompressedExtension==0)
651 return 1;
652 ++extensionList;
Shashank Tavildarc5060992019-10-29 12:56:04 -0700653 }
Shashank Tavildar02433e02019-10-28 14:54:54 -0700654 return 0;
Shashank Tavildar48f85662019-10-25 15:49:11 -0700655}
Shashank Tavildar0f2bff22019-10-28 18:21:47 -0700656
Shashank Tavildar9ab6a742019-10-29 12:27:54 -0700657/*Utility function to get file extension from file */
658const char* UTIL_getFileExtension(const char* infilename)
659{
660 const char* extension = strrchr(infilename, '.');
661 if(!extension || extension==infilename) return "";
662 return extension;
663}
664
Xin Xie9a8ccd42020-06-19 19:35:51 -0700665static int pathnameHas2Dots(const char *pathname)
666{
667 return NULL != strstr(pathname, "..");
668}
669
670static int isFileNameValidForMirroredOutput(const char *filename)
671{
672 return !pathnameHas2Dots(filename);
673}
674
675
676#define DIR_DEFAULT_MODE 0755
677static mode_t getDirMode(const char *dirName)
678{
679 stat_t st;
W. Felix Handte51ac0202020-08-10 15:28:02 -0400680 if (!UTIL_stat(dirName, &st)) {
Xin Xie9a8ccd42020-06-19 19:35:51 -0700681 UTIL_DISPLAY("zstd: failed to get DIR stats %s: %s\n", dirName, strerror(errno));
682 return DIR_DEFAULT_MODE;
683 }
W. Felix Handte51ac0202020-08-10 15:28:02 -0400684 if (!UTIL_isDirectoryStat(&st)) {
685 UTIL_DISPLAY("zstd: expected directory: %s\n", dirName);
686 return DIR_DEFAULT_MODE;
687 }
Xin Xie9a8ccd42020-06-19 19:35:51 -0700688 return st.st_mode;
689}
690
691static int makeDir(const char *dir, mode_t mode)
692{
693#if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__)
694 int ret = _mkdir(dir);
695 (void) mode;
696#else
697 int ret = mkdir(dir, mode);
698#endif
699 if (ret != 0) {
700 if (errno == EEXIST)
701 return 0;
702 UTIL_DISPLAY("zstd: failed to create DIR %s: %s\n", dir, strerror(errno));
703 }
704 return ret;
705}
706
707/* this function requires a mutable input string */
708static void convertPathnameToDirName(char *pathname)
709{
710 size_t len = 0;
711 char* pos = NULL;
712 /* get dir name from pathname similar to 'dirname()' */
713 assert(pathname != NULL);
714
715 /* remove trailing '/' chars */
716 len = strlen(pathname);
717 assert(len > 0);
718 while (pathname[len] == PATH_SEP) {
719 pathname[len] = '\0';
720 len--;
721 }
722 if (len == 0) return;
723
724 /* if input is a single file, return '.' instead. i.e.
725 * "xyz/abc/file.txt" => "xyz/abc"
726 "./file.txt" => "."
727 "file.txt" => "."
728 */
729 pos = strrchr(pathname, PATH_SEP);
730 if (pos == NULL) {
731 pathname[0] = '.';
732 pathname[1] = '\0';
733 } else {
734 *pos = '\0';
735 }
736}
737
738/* pathname must be valid */
739static const char* trimLeadingRootChar(const char *pathname)
740{
741 assert(pathname != NULL);
742 if (pathname[0] == PATH_SEP)
743 return pathname + 1;
744 return pathname;
745}
746
747/* pathname must be valid */
748static const char* trimLeadingCurrentDirConst(const char *pathname)
749{
750 assert(pathname != NULL);
751 if ((pathname[0] == '.') && (pathname[1] == PATH_SEP))
752 return pathname + 2;
753 return pathname;
754}
755
756static char*
757trimLeadingCurrentDir(char *pathname)
758{
759 /* 'union charunion' can do const-cast without compiler warning */
760 union charunion {
761 char *chr;
762 const char* cchr;
763 } ptr;
764 ptr.cchr = trimLeadingCurrentDirConst(pathname);
765 return ptr.chr;
766}
767
768/* remove leading './' or '/' chars here */
769static const char * trimPath(const char *pathname)
770{
771 return trimLeadingRootChar(
772 trimLeadingCurrentDirConst(pathname));
773}
774
775static char* mallocAndJoin2Dir(const char *dir1, const char *dir2)
776{
777 const size_t dir1Size = strlen(dir1);
778 const size_t dir2Size = strlen(dir2);
779 char *outDirBuffer, *buffer, trailingChar;
780
781 assert(dir1 != NULL && dir2 != NULL);
782 outDirBuffer = (char *) malloc(dir1Size + dir2Size + 2);
783 CONTROL(outDirBuffer != NULL);
784
785 strncpy(outDirBuffer, dir1, dir1Size);
786 outDirBuffer[dir1Size] = '\0';
787
788 if (dir2[0] == '.')
789 return outDirBuffer;
790
791 buffer = outDirBuffer + dir1Size;
792 trailingChar = *(buffer - 1);
793 if (trailingChar != PATH_SEP) {
794 *buffer = PATH_SEP;
795 buffer++;
796 }
797 strncpy(buffer, dir2, dir2Size);
798 buffer[dir2Size] = '\0';
799
800 return outDirBuffer;
801}
802
803/* this function will return NULL if input srcFileName is not valid name for mirrored output path */
804char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName)
805{
806 char* pathname = NULL;
807 if (!isFileNameValidForMirroredOutput(srcFileName))
808 return NULL;
809
810 pathname = mallocAndJoin2Dir(outDirRootName, trimPath(srcFileName));
811
812 convertPathnameToDirName(pathname);
813 return pathname;
814}
815
816static int
817mirrorSrcDir(char* srcDirName, const char* outDirName)
818{
819 mode_t srcMode;
820 int status = 0;
821 char* newDir = mallocAndJoin2Dir(outDirName, trimPath(srcDirName));
822 if (!newDir)
823 return -ENOMEM;
824
825 srcMode = getDirMode(srcDirName);
826 status = makeDir(newDir, srcMode);
827 free(newDir);
828 return status;
829}
830
831static int
832mirrorSrcDirRecursive(char* srcDirName, const char* outDirName)
833{
834 int status = 0;
835 char* pp = trimLeadingCurrentDir(srcDirName);
836 char* sp = NULL;
837
838 while ((sp = strchr(pp, PATH_SEP)) != NULL) {
839 if (sp != pp) {
840 *sp = '\0';
841 status = mirrorSrcDir(srcDirName, outDirName);
842 if (status != 0)
843 return status;
844 *sp = PATH_SEP;
845 }
846 pp = sp + 1;
847 }
848 status = mirrorSrcDir(srcDirName, outDirName);
849 return status;
850}
851
852static void
853makeMirroredDestDirsWithSameSrcDirMode(char** srcDirNames, unsigned nbFile, const char* outDirName)
854{
855 unsigned int i = 0;
856 for (i = 0; i < nbFile; i++)
857 mirrorSrcDirRecursive(srcDirNames[i], outDirName);
858}
859
860static int
861firstIsParentOrSameDirOfSecond(const char* firstDir, const char* secondDir)
862{
863 size_t firstDirLen = strlen(firstDir),
864 secondDirLen = strlen(secondDir);
865 return firstDirLen <= secondDirLen &&
866 (secondDir[firstDirLen] == PATH_SEP || secondDir[firstDirLen] == '\0') &&
867 0 == strncmp(firstDir, secondDir, firstDirLen);
868}
869
870static int compareDir(const void* pathname1, const void* pathname2) {
871 /* sort it after remove the leading '/' or './'*/
872 const char* s1 = trimPath(*(char * const *) pathname1);
873 const char* s2 = trimPath(*(char * const *) pathname2);
874 return strcmp(s1, s2);
875}
876
877static void
878makeUniqueMirroredDestDirs(char** srcDirNames, unsigned nbFile, const char* outDirName)
879{
880 unsigned int i = 0, uniqueDirNr = 0;
881 char** uniqueDirNames = NULL;
882
883 if (nbFile == 0)
884 return;
885
886 uniqueDirNames = (char** ) malloc(nbFile * sizeof (char *));
887 CONTROL(uniqueDirNames != NULL);
888
889 /* if dirs is "a/b/c" and "a/b/c/d", we only need call:
890 * we just need "a/b/c/d" */
891 qsort((void *)srcDirNames, nbFile, sizeof(char*), compareDir);
892
893 uniqueDirNr = 1;
894 uniqueDirNames[uniqueDirNr - 1] = srcDirNames[0];
895 for (i = 1; i < nbFile; i++) {
896 char* prevDirName = srcDirNames[i - 1];
897 char* currDirName = srcDirNames[i];
898
899 /* note: we alwasy compare trimmed path, i.e.:
900 * src dir of "./foo" and "/foo" will be both saved into:
901 * "outDirName/foo/" */
902 if (!firstIsParentOrSameDirOfSecond(trimPath(prevDirName),
903 trimPath(currDirName)))
904 uniqueDirNr++;
905
906 /* we need maintain original src dir name instead of trimmed
907 * dir, so we can retrive the original src dir's mode_t */
908 uniqueDirNames[uniqueDirNr - 1] = currDirName;
909 }
910
911 makeMirroredDestDirsWithSameSrcDirMode(uniqueDirNames, uniqueDirNr, outDirName);
912
913 free(uniqueDirNames);
914}
915
916static void
917makeMirroredDestDirs(char** srcFileNames, unsigned nbFile, const char* outDirName)
918{
919 unsigned int i = 0;
920 for (i = 0; i < nbFile; ++i)
921 convertPathnameToDirName(srcFileNames[i]);
922 makeUniqueMirroredDestDirs(srcFileNames, nbFile, outDirName);
923}
924
925void UTIL_mirrorSourceFilesDirectories(const char** inFileNames, unsigned int nbFile, const char* outDirName)
926{
927 unsigned int i = 0, validFilenamesNr = 0;
928 char** srcFileNames = (char **) malloc(nbFile * sizeof (char *));
929 CONTROL(srcFileNames != NULL);
930
931 /* check input filenames is valid */
932 for (i = 0; i < nbFile; ++i) {
933 if (isFileNameValidForMirroredOutput(inFileNames[i])) {
934 char* fname = STRDUP(inFileNames[i]);
935 CONTROL(fname != NULL);
936 srcFileNames[validFilenamesNr++] = fname;
937 }
938 }
939
940 if (validFilenamesNr > 0) {
941 makeDir(outDirName, DIR_DEFAULT_MODE);
942 makeMirroredDestDirs(srcFileNames, validFilenamesNr, outDirName);
943 }
944
945 for (i = 0; i < validFilenamesNr; i++)
946 free(srcFileNames[i]);
947 free(srcFileNames);
948}
Yann Colletb09f5932019-11-05 17:02:43 -0800949
Yann Collet31a0abb2019-11-06 09:10:05 -0800950FileNamesTable*
951UTIL_createExpandedFNT(const char** inputNames, size_t nbIfns, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -0700952{
Yann Colletb09f5932019-11-05 17:02:43 -0800953 unsigned nbFiles;
Rohit Jain705e0b12018-10-11 15:51:57 -0700954 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
955 char* bufend = buf + LIST_SIZE_INCREASE;
Rohit Jain705e0b12018-10-11 15:51:57 -0700956
957 if (!buf) return NULL;
958
Yann Colletd5b4a7e2019-11-26 17:46:57 -0800959 { size_t ifnNb, pos;
Yann Colletb09f5932019-11-05 17:02:43 -0800960 for (ifnNb=0, pos=0, nbFiles=0; ifnNb<nbIfns; ifnNb++) {
961 if (!UTIL_isDirectory(inputNames[ifnNb])) {
962 size_t const len = strlen(inputNames[ifnNb]);
963 if (buf + pos + len >= bufend) {
964 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
965 assert(newListSize >= 0);
966 buf = (char*)UTIL_realloc(buf, (size_t)newListSize);
Yann Colletb09f5932019-11-05 17:02:43 -0800967 if (!buf) return NULL;
Yann Colletd5b4a7e2019-11-26 17:46:57 -0800968 bufend = buf + newListSize;
Yann Colletb09f5932019-11-05 17:02:43 -0800969 }
970 if (buf + pos + len < bufend) {
971 memcpy(buf+pos, inputNames[ifnNb], len+1); /* including final \0 */
972 pos += len + 1;
973 nbFiles++;
974 }
975 } else {
976 nbFiles += (unsigned)UTIL_prepareFileList(inputNames[ifnNb], &buf, &pos, &bufend, followLinks);
977 if (buf == NULL) return NULL;
978 } } }
Rohit Jain705e0b12018-10-11 15:51:57 -0700979
Yann Colleta49417b2019-12-02 14:28:18 -0800980 /* note : even if nbFiles==0, function returns a valid, though empty, FileNamesTable* object */
Rohit Jain705e0b12018-10-11 15:51:57 -0700981
Yann Colletd5b4a7e2019-11-26 17:46:57 -0800982 { size_t ifnNb, pos;
Yann Colleta49417b2019-12-02 14:28:18 -0800983 size_t const fntCapacity = nbFiles + 1; /* minimum 1, allows adding one reference, typically stdin */
984 const char** const fileNamesTable = (const char**)malloc(fntCapacity * sizeof(*fileNamesTable));
Yann Colletb09f5932019-11-05 17:02:43 -0800985 if (!fileNamesTable) { free(buf); return NULL; }
Rohit Jain705e0b12018-10-11 15:51:57 -0700986
Yann Colletb09f5932019-11-05 17:02:43 -0800987 for (ifnNb = 0, pos = 0; ifnNb < nbFiles; ifnNb++) {
988 fileNamesTable[ifnNb] = buf + pos;
989 if (buf + pos > bufend) { free(buf); free((void*)fileNamesTable); return NULL; }
990 pos += strlen(fileNamesTable[ifnNb]) + 1;
Yann Collet29e46ed2019-10-18 14:28:34 -0700991 }
Yann Colleta49417b2019-12-02 14:28:18 -0800992 return UTIL_assembleFileNamesTable2(fileNamesTable, nbFiles, fntCapacity, buf);
Rohit Jain705e0b12018-10-11 15:51:57 -0700993 }
Rohit Jain705e0b12018-10-11 15:51:57 -0700994}
995
Yann Collet59a71162019-04-10 12:37:03 -0700996
Yann Collet31a0abb2019-11-06 09:10:05 -0800997void UTIL_expandFNT(FileNamesTable** fnt, int followLinks)
Yann Collet76b9e422019-11-05 14:59:45 -0800998{
Yann Collet31a0abb2019-11-06 09:10:05 -0800999 FileNamesTable* const newFNT = UTIL_createExpandedFNT((*fnt)->fileNames, (*fnt)->tableSize, followLinks);
Yann Colleta49417b2019-12-02 14:28:18 -08001000 CONTROL(newFNT != NULL);
Yann Collet31a0abb2019-11-06 09:10:05 -08001001 UTIL_freeFileNamesTable(*fnt);
1002 *fnt = newFNT;
Yann Collet76b9e422019-11-05 14:59:45 -08001003}
1004
Yann Colleta7e33e32019-11-06 14:42:13 -08001005FileNamesTable* UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames)
1006{
1007 size_t const sizeof_FNTable = nbFilenames * sizeof(*filenames);
1008 const char** const newFNTable = (const char**)malloc(sizeof_FNTable);
1009 if (newFNTable==NULL) return NULL;
Yann Collet9df49dc2019-11-06 15:23:44 -08001010 memcpy((void*)newFNTable, filenames, sizeof_FNTable); /* void* : mitigate a Visual compiler bug or limitation */
Yann Colletd5b4a7e2019-11-26 17:46:57 -08001011 return UTIL_assembleFileNamesTable(newFNTable, nbFilenames, NULL);
Yann Colleta7e33e32019-11-06 14:42:13 -08001012}
1013
Yann Collet72dbf1b2018-12-20 12:27:12 -08001014
Rohit Jaina47f6e62018-10-11 16:51:29 -07001015/*-****************************************
Yann Collet59a71162019-04-10 12:37:03 -07001016* count the number of physical cores
Rohit Jaind6d240f2018-10-11 15:07:12 -07001017******************************************/
Rohit Jainc7251e52018-10-11 18:05:15 -07001018
Rohit Jain91b2fed2018-10-11 17:34:47 -07001019#if defined(_WIN32) || defined(WIN32)
1020
1021#include <windows.h>
1022
1023typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
1024
1025int UTIL_countPhysicalCores(void)
1026{
1027 static int numPhysicalCores = 0;
1028 if (numPhysicalCores != 0) return numPhysicalCores;
1029
1030 { LPFN_GLPI glpi;
1031 BOOL done = FALSE;
1032 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
1033 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
1034 DWORD returnLength = 0;
1035 size_t byteOffset = 0;
1036
Yann Collet0492c572019-10-18 17:08:52 -07001037#if defined(_MSC_VER)
Yann Colletf3796372019-10-18 17:05:42 -07001038/* Visual Studio does not like the following cast */
1039# pragma warning( disable : 4054 ) /* conversion from function ptr to data ptr */
1040# pragma warning( disable : 4055 ) /* conversion from data ptr to function ptr */
1041#endif
Yann Collet1bd6c152019-10-18 15:45:31 -07001042 glpi = (LPFN_GLPI)(void*)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
1043 "GetLogicalProcessorInformation");
Rohit Jain91b2fed2018-10-11 17:34:47 -07001044
1045 if (glpi == NULL) {
1046 goto failed;
1047 }
1048
1049 while(!done) {
1050 DWORD rc = glpi(buffer, &returnLength);
1051 if (FALSE == rc) {
1052 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
1053 if (buffer)
1054 free(buffer);
1055 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
1056
1057 if (buffer == NULL) {
1058 perror("zstd");
1059 exit(1);
1060 }
1061 } else {
1062 /* some other error */
1063 goto failed;
1064 }
1065 } else {
1066 done = TRUE;
Yann Collet96ee2072019-11-26 15:44:33 -08001067 } }
Rohit Jain91b2fed2018-10-11 17:34:47 -07001068
1069 ptr = buffer;
1070
1071 while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
1072
1073 if (ptr->Relationship == RelationProcessorCore) {
1074 numPhysicalCores++;
1075 }
1076
1077 ptr++;
1078 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
1079 }
1080
1081 free(buffer);
1082
1083 return numPhysicalCores;
1084 }
1085
1086failed:
1087 /* try to fall back on GetSystemInfo */
1088 { SYSTEM_INFO sysinfo;
1089 GetSystemInfo(&sysinfo);
1090 numPhysicalCores = sysinfo.dwNumberOfProcessors;
1091 if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
1092 }
1093 return numPhysicalCores;
1094}
1095
1096#elif defined(__APPLE__)
1097
1098#include <sys/sysctl.h>
1099
1100/* Use apple-provided syscall
1101 * see: man 3 sysctl */
1102int UTIL_countPhysicalCores(void)
1103{
1104 static S32 numPhysicalCores = 0; /* apple specifies int32_t */
1105 if (numPhysicalCores != 0) return numPhysicalCores;
1106
1107 { size_t size = sizeof(S32);
1108 int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
1109 if (ret != 0) {
1110 if (errno == ENOENT) {
1111 /* entry not present, fall back on 1 */
1112 numPhysicalCores = 1;
1113 } else {
1114 perror("zstd: can't get number of physical cpus");
1115 exit(1);
1116 }
1117 }
1118
1119 return numPhysicalCores;
1120 }
1121}
1122
1123#elif defined(__linux__)
1124
1125/* parse /proc/cpuinfo
1126 * siblings / cpu cores should give hyperthreading ratio
1127 * otherwise fall back on sysconf */
1128int UTIL_countPhysicalCores(void)
1129{
1130 static int numPhysicalCores = 0;
1131
1132 if (numPhysicalCores != 0) return numPhysicalCores;
1133
1134 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1135 if (numPhysicalCores == -1) {
1136 /* value not queryable, fall back on 1 */
1137 return numPhysicalCores = 1;
1138 }
1139
1140 /* try to determine if there's hyperthreading */
1141 { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
1142#define BUF_SIZE 80
1143 char buff[BUF_SIZE];
1144
1145 int siblings = 0;
1146 int cpu_cores = 0;
1147 int ratio = 1;
1148
1149 if (cpuinfo == NULL) {
1150 /* fall back on the sysconf value */
1151 return numPhysicalCores;
1152 }
1153
1154 /* assume the cpu cores/siblings values will be constant across all
1155 * present processors */
1156 while (!feof(cpuinfo)) {
1157 if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
1158 if (strncmp(buff, "siblings", 8) == 0) {
1159 const char* const sep = strchr(buff, ':');
LeeYoung624c5caaf52019-07-29 17:05:50 +08001160 if (sep == NULL || *sep == '\0') {
Rohit Jain91b2fed2018-10-11 17:34:47 -07001161 /* formatting was broken? */
1162 goto failed;
1163 }
1164
1165 siblings = atoi(sep + 1);
1166 }
1167 if (strncmp(buff, "cpu cores", 9) == 0) {
1168 const char* const sep = strchr(buff, ':');
LeeYoung624c5caaf52019-07-29 17:05:50 +08001169 if (sep == NULL || *sep == '\0') {
Rohit Jain91b2fed2018-10-11 17:34:47 -07001170 /* formatting was broken? */
1171 goto failed;
1172 }
1173
1174 cpu_cores = atoi(sep + 1);
1175 }
1176 } else if (ferror(cpuinfo)) {
1177 /* fall back on the sysconf value */
1178 goto failed;
Yann Collet96ee2072019-11-26 15:44:33 -08001179 } }
Rohit Jain91b2fed2018-10-11 17:34:47 -07001180 if (siblings && cpu_cores) {
1181 ratio = siblings / cpu_cores;
1182 }
1183failed:
1184 fclose(cpuinfo);
1185 return numPhysicalCores = numPhysicalCores / ratio;
1186 }
1187}
1188
Conrad Meyerfe826372019-01-04 11:57:12 -08001189#elif defined(__FreeBSD__)
Rohit Jain91b2fed2018-10-11 17:34:47 -07001190
Conrad Meyerfe826372019-01-04 11:57:12 -08001191#include <sys/param.h>
1192#include <sys/sysctl.h>
1193
1194/* Use physical core sysctl when available
1195 * see: man 4 smp, man 3 sysctl */
1196int UTIL_countPhysicalCores(void)
1197{
1198 static int numPhysicalCores = 0; /* freebsd sysctl is native int sized */
1199 if (numPhysicalCores != 0) return numPhysicalCores;
1200
1201#if __FreeBSD_version >= 1300008
1202 { size_t size = sizeof(numPhysicalCores);
1203 int ret = sysctlbyname("kern.smp.cores", &numPhysicalCores, &size, NULL, 0);
1204 if (ret == 0) return numPhysicalCores;
1205 if (errno != ENOENT) {
1206 perror("zstd: can't get number of physical cpus");
1207 exit(1);
1208 }
1209 /* sysctl not present, fall through to older sysconf method */
1210 }
1211#endif
1212
1213 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1214 if (numPhysicalCores == -1) {
1215 /* value not queryable, fall back on 1 */
1216 numPhysicalCores = 1;
1217 }
1218 return numPhysicalCores;
1219}
1220
Christoph Reiterd0dcaf52020-01-08 00:48:26 +01001221#elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__CYGWIN__)
Conrad Meyerfe826372019-01-04 11:57:12 -08001222
1223/* Use POSIX sysconf
1224 * see: man 3 sysconf */
Rohit Jain91b2fed2018-10-11 17:34:47 -07001225int UTIL_countPhysicalCores(void)
1226{
1227 static int numPhysicalCores = 0;
1228
1229 if (numPhysicalCores != 0) return numPhysicalCores;
1230
1231 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1232 if (numPhysicalCores == -1) {
1233 /* value not queryable, fall back on 1 */
1234 return numPhysicalCores = 1;
1235 }
1236 return numPhysicalCores;
1237}
1238
1239#else
1240
1241int UTIL_countPhysicalCores(void)
1242{
1243 /* assume 1 */
1244 return 1;
1245}
1246
1247#endif
1248
Rohit Jainf881ee82018-10-11 12:52:19 -07001249#if defined (__cplusplus)
1250}
1251#endif