blob: 980ab5a4259acb32aa91cfad6e852a1a1ada51f4 [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
senhuang427991c552020-08-26 16:50:20 -040090int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg,
senhuang4293d63ea2020-09-24 15:58:06 -040091 const char* acceptableLetters, int hasStdinInput) {
senhuang427991c552020-08-26 16:50:20 -040092 int ch, result;
senhuang427aa3da12020-09-22 14:15:52 -040093
senhuang4288f44102020-09-24 16:29:12 -040094 if (hasStdinInput) {
senhuang4202422db2020-09-25 11:51:35 -040095 UTIL_DISPLAY("stdin is an input - not proceeding.\n");
senhuang4293d63ea2020-09-24 15:58:06 -040096 return 1;
senhuang4288f44102020-09-24 16:29:12 -040097 }
senhuang4293d63ea2020-09-24 15:58:06 -040098
senhuang42aab11ce2020-08-25 11:25:49 -040099 UTIL_DISPLAY("%s", prompt);
100 ch = getchar();
senhuang427991c552020-08-26 16:50:20 -0400101 result = 0;
senhuang42aab11ce2020-08-25 11:25:49 -0400102 if (strchr(acceptableLetters, ch) == NULL) {
103 UTIL_DISPLAY("%s", abortMsg);
senhuang427991c552020-08-26 16:50:20 -0400104 result = 1;
senhuang42aab11ce2020-08-25 11:25:49 -0400105 }
106 /* flush the rest */
107 while ((ch!=EOF) && (ch!='\n'))
108 ch = getchar();
senhuang427991c552020-08-26 16:50:20 -0400109 return result;
senhuang42aab11ce2020-08-25 11:25:49 -0400110}
111
Yann Collet1ead0c52019-10-25 16:36:59 -0700112
Yann Collet9a221402019-11-25 13:45:22 -0800113/*-*************************************
114* Constants
115***************************************/
116#define LIST_SIZE_INCREASE (8*1024)
Yann Colletc71bd452019-11-26 11:20:26 -0800117#define MAX_FILE_OF_FILE_NAMES_SIZE (1<<20)*50
Yann Collet9a221402019-11-25 13:45:22 -0800118
119
120/*-*************************************
121* Functions
122***************************************/
Yann Collet1ead0c52019-10-25 16:36:59 -0700123
W. Felix Handteb11bea52020-08-05 00:09:29 -0400124int UTIL_stat(const char* filename, stat_t* statbuf)
125{
126#if defined(_MSC_VER)
127 return !_stat64(filename, statbuf);
128#elif defined(__MINGW32__) && defined (__MSVCRT__)
129 return !_stati64(filename, statbuf);
130#else
131 return !stat(filename, statbuf);
132#endif
133}
134
Rohit Jaind6d240f2018-10-11 15:07:12 -0700135int UTIL_isRegularFile(const char* infilename)
136{
137 stat_t statbuf;
W. Felix Handte51ac0202020-08-10 15:28:02 -0400138 return UTIL_stat(infilename, &statbuf) && UTIL_isRegularFileStat(&statbuf);
Rohit Jaind6d240f2018-10-11 15:07:12 -0700139}
140
W. Felix Handte44fa0522020-08-05 01:00:06 -0400141int UTIL_isRegularFileStat(const stat_t* statbuf)
142{
143#if defined(_MSC_VER)
144 return (statbuf->st_mode & S_IFREG) != 0;
145#else
146 return S_ISREG(statbuf->st_mode) != 0;
147#endif
148}
149
Yann Collet9a221402019-11-25 13:45:22 -0800150/* like chmod, but avoid changing permission of /dev/null */
W. Felix Handte0a8aacb2020-08-05 12:00:12 -0400151int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions)
Yann Collet9a221402019-11-25 13:45:22 -0800152{
W. Felix Handte0a8aacb2020-08-05 12:00:12 -0400153 stat_t localStatBuf;
154 if (statbuf == NULL) {
155 if (!UTIL_stat(filename, &localStatBuf)) return 0;
156 statbuf = &localStatBuf;
157 }
158 if (!UTIL_isRegularFileStat(statbuf)) return 0; /* pretend success, but don't change anything */
Yann Collet9a221402019-11-25 13:45:22 -0800159 return chmod(filename, permissions);
160}
161
W. Felix Handte1a1003f2020-08-05 00:35:21 -0400162int UTIL_setFileStat(const char *filename, const stat_t *statbuf)
Rohit Jaind6d240f2018-10-11 15:07:12 -0700163{
164 int res = 0;
Rohit Jaind6d240f2018-10-11 15:07:12 -0700165
W. Felix Handtec1449142020-08-05 12:10:42 -0400166 stat_t curStatBuf;
167 if (!UTIL_stat(filename, &curStatBuf) || !UTIL_isRegularFileStat(&curStatBuf))
Rohit Jaind6d240f2018-10-11 15:07:12 -0700168 return -1;
169
W. Felix Handtee1ec8002019-09-12 16:27:05 -0400170 /* set access and modification times */
W. Felix Handte56668352019-12-04 16:59:16 -0500171 /* We check that st_mtime is a macro here in order to give us confidence
172 * that struct stat has a struct timespec st_mtim member. We need this
173 * check because there are some platforms that claim to be POSIX 2008
174 * compliant but which do not have st_mtim... */
W. Felix Handte5af8cb72019-12-04 10:25:07 -0500175#if (PLATFORM_POSIX_VERSION >= 200809L) && defined(st_mtime)
176 {
177 /* (atime, mtime) */
178 struct timespec timebuf[2] = { {0, UTIME_NOW} };
179 timebuf[1] = statbuf->st_mtim;
180 res += utimensat(AT_FDCWD, filename, timebuf, 0);
181 }
182#else
W. Felix Handtee1ec8002019-09-12 16:27:05 -0400183 {
184 struct utimbuf timebuf;
185 timebuf.actime = time(NULL);
186 timebuf.modtime = statbuf->st_mtime;
187 res += utime(filename, &timebuf);
188 }
Rosen Penev41e90652019-07-30 17:17:07 -0700189#endif
Rohit Jaind6d240f2018-10-11 15:07:12 -0700190
191#if !defined(_WIN32)
192 res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */
193#endif
194
W. Felix Handtec1449142020-08-05 12:10:42 -0400195 res += UTIL_chmod(filename, &curStatBuf, statbuf->st_mode & 07777); /* Copy file permissions */
Rohit Jaind6d240f2018-10-11 15:07:12 -0700196
197 errno = 0;
198 return -res; /* number of errors is returned */
199}
Rohit Jainf881ee82018-10-11 12:52:19 -0700200
Yann Collet9a221402019-11-25 13:45:22 -0800201int UTIL_isDirectory(const char* infilename)
Rohit Jainf881ee82018-10-11 12:52:19 -0700202{
Rohit Jainf881ee82018-10-11 12:52:19 -0700203 stat_t statbuf;
W. Felix Handte51ac0202020-08-10 15:28:02 -0400204 return UTIL_stat(infilename, &statbuf) && UTIL_isDirectoryStat(&statbuf);
Rohit Jainf881ee82018-10-11 12:52:19 -0700205}
206
W. Felix Handte44fa0522020-08-05 01:00:06 -0400207int UTIL_isDirectoryStat(const stat_t* statbuf)
208{
209#if defined(_MSC_VER)
210 return (statbuf->st_mode & _S_IFDIR) != 0;
211#else
212 return S_ISDIR(statbuf->st_mode) != 0;
213#endif
214}
215
Sen Huangf80437c2019-10-02 11:08:20 -0400216int UTIL_compareStr(const void *p1, const void *p2) {
217 return strcmp(* (char * const *) p1, * (char * const *) p2);
218}
Sen Huanga9c807a2019-09-06 10:17:04 -0700219
Yann Collet00040432019-10-17 10:56:14 -0700220int UTIL_isSameFile(const char* fName1, const char* fName2)
shakeelraoe5811e52019-03-23 19:04:56 -0700221{
Yann Collet00040432019-10-17 10:56:14 -0700222 assert(fName1 != NULL); assert(fName2 != NULL);
223#if defined(_MSC_VER) || defined(_WIN32)
shakeelraoe5811e52019-03-23 19:04:56 -0700224 /* note : Visual does not support file identification by inode.
Yann Collet00040432019-10-17 10:56:14 -0700225 * inode does not work on Windows, even with a posix layer, like msys2.
shakeelraoe5811e52019-03-23 19:04:56 -0700226 * The following work-around is limited to detecting exact name repetition only,
227 * aka `filename` is considered different from `subdir/../filename` */
Yann Collet157479a2019-10-17 14:31:42 -0700228 return !strcmp(fName1, fName2);
shakeelraoe5811e52019-03-23 19:04:56 -0700229#else
Yann Collet00040432019-10-17 10:56:14 -0700230 { stat_t file1Stat;
231 stat_t file2Stat;
W. Felix Handte5fbc6ad2020-08-05 00:31:48 -0400232 return UTIL_stat(fName1, &file1Stat)
233 && UTIL_stat(fName2, &file2Stat)
Yann Collet00040432019-10-17 10:56:14 -0700234 && (file1Stat.st_dev == file2Stat.st_dev)
235 && (file1Stat.st_ino == file2Stat.st_ino);
236 }
shakeelraoe5811e52019-03-23 19:04:56 -0700237#endif
238}
239
Yann Collet9a221402019-11-25 13:45:22 -0800240/* UTIL_isFIFO : distinguish named pipes */
241int UTIL_isFIFO(const char* infilename)
Bimba Shrestha8a397482019-10-22 15:23:22 -0700242{
243/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
244#if PLATFORM_POSIX_VERSION >= 200112L
245 stat_t statbuf;
W. Felix Handte44fa0522020-08-05 01:00:06 -0400246 if (UTIL_stat(infilename, &statbuf) && UTIL_isFIFOStat(&statbuf)) return 1;
Bimba Shrestha8a397482019-10-22 15:23:22 -0700247#endif
248 (void)infilename;
249 return 0;
250}
Bimba Shrestha8a397482019-10-22 15:23:22 -0700251
W. Felix Handte44fa0522020-08-05 01:00:06 -0400252/* UTIL_isFIFO : distinguish named pipes */
253int UTIL_isFIFOStat(const stat_t* statbuf)
254{
255/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
256#if PLATFORM_POSIX_VERSION >= 200112L
257 if (S_ISFIFO(statbuf->st_mode)) return 1;
258#endif
259 (void)statbuf;
260 return 0;
261}
262
Yann Collet9a221402019-11-25 13:45:22 -0800263int UTIL_isLink(const char* infilename)
Rohit Jainf881ee82018-10-11 12:52:19 -0700264{
265/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
W. Felix Handted2c48042019-06-07 15:31:33 -0400266#if PLATFORM_POSIX_VERSION >= 200112L
Rohit Jainf881ee82018-10-11 12:52:19 -0700267 stat_t statbuf;
Yann Collet9a221402019-11-25 13:45:22 -0800268 int const r = lstat(infilename, &statbuf);
Rohit Jainf881ee82018-10-11 12:52:19 -0700269 if (!r && S_ISLNK(statbuf.st_mode)) return 1;
270#endif
Rohit Jainf881ee82018-10-11 12:52:19 -0700271 (void)infilename;
272 return 0;
273}
274
275U64 UTIL_getFileSize(const char* infilename)
276{
W. Felix Handte69cb9e72020-08-05 00:21:41 -0400277 stat_t statbuf;
278 if (!UTIL_stat(infilename, &statbuf)) return UTIL_FILESIZE_UNKNOWN;
W. Felix Handte44fa0522020-08-05 01:00:06 -0400279 return UTIL_getFileSizeStat(&statbuf);
280}
281
282U64 UTIL_getFileSizeStat(const stat_t* statbuf)
283{
284 if (!UTIL_isRegularFileStat(statbuf)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700285#if defined(_MSC_VER)
W. Felix Handte44fa0522020-08-05 01:00:06 -0400286 if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700287#elif defined(__MINGW32__) && defined (__MSVCRT__)
W. Felix Handte44fa0522020-08-05 01:00:06 -0400288 if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700289#else
W. Felix Handte44fa0522020-08-05 01:00:06 -0400290 if (!S_ISREG(statbuf->st_mode)) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700291#endif
W. Felix Handte44fa0522020-08-05 01:00:06 -0400292 return (U64)statbuf->st_size;
Rohit Jainf881ee82018-10-11 12:52:19 -0700293}
294
295
Yann Collet5fb84ca2019-10-25 17:34:29 -0700296U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles)
Rohit Jainf881ee82018-10-11 12:52:19 -0700297{
298 U64 total = 0;
Rohit Jainf881ee82018-10-11 12:52:19 -0700299 unsigned n;
300 for (n=0; n<nbFiles; n++) {
301 U64 const size = UTIL_getFileSize(fileNamesTable[n]);
Yann Collet5fb84ca2019-10-25 17:34:29 -0700302 if (size == UTIL_FILESIZE_UNKNOWN) return UTIL_FILESIZE_UNKNOWN;
Rohit Jainf881ee82018-10-11 12:52:19 -0700303 total += size;
304 }
Yann Collet5fb84ca2019-10-25 17:34:29 -0700305 return total;
Rohit Jainf881ee82018-10-11 12:52:19 -0700306}
307
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100308
Yann Collet1ead0c52019-10-25 16:36:59 -0700309/* condition : @file must be valid, and not have reached its end.
Yann Colletd9c634e2019-10-28 15:03:32 -0700310 * @return : length of line written into @buf, ended with `\0` instead of '\n',
Yann Collet1ead0c52019-10-25 16:36:59 -0700311 * or 0, if there is no new line */
312static size_t readLineFromFile(char* buf, size_t len, FILE* file)
313{
Yann Collet1ead0c52019-10-25 16:36:59 -0700314 assert(!feof(file));
Nick Terrellf6d00c02020-01-13 14:22:46 -0800315 /* Work around Cygwin problem when len == 1 it returns NULL. */
316 if (len <= 1) return 0;
317 CONTROL( fgets(buf, (int) len, file) );
Yann Colletd9c634e2019-10-28 15:03:32 -0700318 { size_t linelen = strlen(buf);
319 if (strlen(buf)==0) return 0;
320 if (buf[linelen-1] == '\n') linelen--;
321 buf[linelen] = '\0';
322 return linelen+1;
323 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100324}
325
Yann Collet1ead0c52019-10-25 16:36:59 -0700326/* Conditions :
327 * size of @inputFileName file must be < @dstCapacity
328 * @dst must be initialized
329 * @return : nb of lines
330 * or -1 if there's an error
331 */
332static int
333readLinesFromFile(void* dst, size_t dstCapacity,
334 const char* inputFileName)
335{
336 int nbFiles = 0;
Yann Collet3e5c81e2019-10-26 00:01:11 -0700337 size_t pos = 0;
Yann Collet1ead0c52019-10-25 16:36:59 -0700338 char* const buf = (char*)dst;
339 FILE* const inputFile = fopen(inputFileName, "r");
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100340
Yann Collet1ead0c52019-10-25 16:36:59 -0700341 assert(dst != NULL);
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100342
Yann Collet1ead0c52019-10-25 16:36:59 -0700343 if(!inputFile) {
344 if (g_utilDisplayLevel >= 1) perror("zstd:util:readLinesFromFile");
345 return -1;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100346 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100347
Yann Collet1ead0c52019-10-25 16:36:59 -0700348 while ( !feof(inputFile) ) {
349 size_t const lineLength = readLineFromFile(buf+pos, dstCapacity-pos, inputFile);
350 if (lineLength == 0) break;
351 assert(pos + lineLength < dstCapacity);
Yann Colletd9c634e2019-10-28 15:03:32 -0700352 pos += lineLength;
Yann Collet1ead0c52019-10-25 16:36:59 -0700353 ++nbFiles;
Yann Collet1ead0c52019-10-25 16:36:59 -0700354 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100355
Yann Collet1ead0c52019-10-25 16:36:59 -0700356 CONTROL( fclose(inputFile) == 0 );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100357
Yann Collet1ead0c52019-10-25 16:36:59 -0700358 return nbFiles;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100359}
360
361/*Note: buf is not freed in case function successfully created table because filesTable->fileNames[0] = buf*/
362FileNamesTable*
Yann Collet1ead0c52019-10-25 16:36:59 -0700363UTIL_createFileNamesTable_fromFileName(const char* inputFileName)
364{
365 size_t nbFiles = 0;
366 char* buf;
367 size_t bufSize;
368 size_t pos = 0;
W. Felix Handte7238cca2020-08-05 01:08:34 -0400369 stat_t statbuf;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100370
W. Felix Handte7238cca2020-08-05 01:08:34 -0400371 if (!UTIL_stat(inputFileName, &statbuf) || !UTIL_isRegularFileStat(&statbuf))
Yann Collet1ead0c52019-10-25 16:36:59 -0700372 return NULL;
Ahmed Abdellah47712c92019-10-24 10:30:05 +0100373
W. Felix Handte7238cca2020-08-05 01:08:34 -0400374 { U64 const inputFileSize = UTIL_getFileSizeStat(&statbuf);
Yann Collet1ead0c52019-10-25 16:36:59 -0700375 if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE)
376 return NULL;
Yann Collet12efa1e2019-10-26 00:27:32 -0700377 bufSize = (size_t)(inputFileSize + 1); /* (+1) to add '\0' at the end of last filename */
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100378 }
379
Yann Collet1ead0c52019-10-25 16:36:59 -0700380 buf = (char*) malloc(bufSize);
381 CONTROL( buf != NULL );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100382
Yann Collet1ead0c52019-10-25 16:36:59 -0700383 { int const ret_nbFiles = readLinesFromFile(buf, bufSize, inputFileName);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100384
Yann Collet1ead0c52019-10-25 16:36:59 -0700385 if (ret_nbFiles <= 0) {
386 free(buf);
387 return NULL;
388 }
389 nbFiles = (size_t)ret_nbFiles;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100390 }
391
Yann Collet1ead0c52019-10-25 16:36:59 -0700392 { const char** filenamesTable = (const char**) malloc(nbFiles * sizeof(*filenamesTable));
393 CONTROL(filenamesTable != NULL);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100394
Yann Collet1ead0c52019-10-25 16:36:59 -0700395 { size_t fnb;
396 for (fnb = 0, pos = 0; fnb < nbFiles; fnb++) {
397 filenamesTable[fnb] = buf+pos;
398 pos += strlen(buf+pos)+1; /* +1 for the finishing `\0` */
399 } }
400 assert(pos <= bufSize);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100401
Yann Collet9a3de0a2019-11-25 15:34:55 -0800402 return UTIL_assembleFileNamesTable(filenamesTable, nbFiles, buf);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100403 }
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100404}
405
Yann Colleta49417b2019-12-02 14:28:18 -0800406static FileNamesTable*
407UTIL_assembleFileNamesTable2(const char** filenames, size_t tableSize, size_t tableCapacity, char* buf)
Yann Collet1ead0c52019-10-25 16:36:59 -0700408{
409 FileNamesTable* const table = (FileNamesTable*) malloc(sizeof(*table));
Yann Collet96ee2072019-11-26 15:44:33 -0800410 CONTROL(table != NULL);
Yann Collet1ead0c52019-10-25 16:36:59 -0700411 table->fileNames = filenames;
412 table->buf = buf;
413 table->tableSize = tableSize;
Yann Colleta49417b2019-12-02 14:28:18 -0800414 table->tableCapacity = tableCapacity;
Yann Collet1ead0c52019-10-25 16:36:59 -0700415 return table;
Ahmed Abdellahcddb05e2019-10-24 14:42:37 +0100416}
417
Yann Colleta49417b2019-12-02 14:28:18 -0800418FileNamesTable*
419UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf)
420{
421 return UTIL_assembleFileNamesTable2(filenames, tableSize, tableSize, buf);
422}
423
Yann Collet1ead0c52019-10-25 16:36:59 -0700424void UTIL_freeFileNamesTable(FileNamesTable* table)
425{
426 if (table==NULL) return;
427 free((void*)table->fileNames);
428 free(table->buf);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100429 free(table);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100430}
431
Yann Colletb09f5932019-11-05 17:02:43 -0800432FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize)
433{
434 const char** const fnTable = (const char**)malloc(tableSize * sizeof(*fnTable));
435 FileNamesTable* fnt;
436 if (fnTable==NULL) return NULL;
Yann Collet9a3de0a2019-11-25 15:34:55 -0800437 fnt = UTIL_assembleFileNamesTable(fnTable, tableSize, NULL);
Yann Colletb09f5932019-11-05 17:02:43 -0800438 fnt->tableSize = 0; /* the table is empty */
439 return fnt;
440}
441
442void UTIL_refFilename(FileNamesTable* fnt, const char* filename)
443{
Yann Colletf622c0a2019-11-26 14:48:23 -0800444 assert(fnt->tableSize < fnt->tableCapacity);
Yann Colletb09f5932019-11-05 17:02:43 -0800445 fnt->fileNames[fnt->tableSize] = filename;
446 fnt->tableSize++;
447}
448
Yann Collet1ead0c52019-10-25 16:36:59 -0700449static size_t getTotalTableSize(FileNamesTable* table)
450{
451 size_t fnb = 0, totalSize = 0;
452 for(fnb = 0 ; fnb < table->tableSize && table->fileNames[fnb] ; ++fnb) {
453 totalSize += strlen(table->fileNames[fnb]) + 1; /* +1 to add '\0' at the end of each fileName */
454 }
455 return totalSize;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100456}
457
458FileNamesTable*
Yann Collet31a0abb2019-11-06 09:10:05 -0800459UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2)
Yann Collet1ead0c52019-10-25 16:36:59 -0700460{
461 unsigned newTableIdx = 0;
462 size_t pos = 0;
463 size_t newTotalTableSize;
464 char* buf;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100465
Yann Collet9a3de0a2019-11-25 15:34:55 -0800466 FileNamesTable* const newTable = UTIL_assembleFileNamesTable(NULL, 0, NULL);
Yann Collet1ead0c52019-10-25 16:36:59 -0700467 CONTROL( newTable != NULL );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100468
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100469 newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100470
Yann Collet1ead0c52019-10-25 16:36:59 -0700471 buf = (char*) calloc(newTotalTableSize, sizeof(*buf));
472 CONTROL ( buf != NULL );
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100473
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100474 newTable->buf = buf;
Yann Collet1ead0c52019-10-25 16:36:59 -0700475 newTable->tableSize = table1->tableSize + table2->tableSize;
476 newTable->fileNames = (const char **) calloc(newTable->tableSize, sizeof(*(newTable->fileNames)));
477 CONTROL ( newTable->fileNames != NULL );
478
479 { unsigned idx1;
480 for( idx1=0 ; (idx1 < table1->tableSize) && table1->fileNames[idx1] && (pos < newTotalTableSize); ++idx1, ++newTableIdx) {
481 size_t const curLen = strlen(table1->fileNames[idx1]);
482 memcpy(buf+pos, table1->fileNames[idx1], curLen);
483 assert(newTableIdx <= newTable->tableSize);
484 newTable->fileNames[newTableIdx] = buf+pos;
485 pos += curLen+1;
486 } }
487
488 { unsigned idx2;
489 for( idx2=0 ; (idx2 < table2->tableSize) && table2->fileNames[idx2] && (pos < newTotalTableSize) ; ++idx2, ++newTableIdx) {
490 size_t const curLen = strlen(table2->fileNames[idx2]);
491 memcpy(buf+pos, table2->fileNames[idx2], curLen);
492 assert(newTableIdx <= newTable->tableSize);
493 newTable->fileNames[newTableIdx] = buf+pos;
494 pos += curLen+1;
495 } }
496 assert(pos <= newTotalTableSize);
Yann Collet1ead0c52019-10-25 16:36:59 -0700497 newTable->tableSize = newTableIdx;
Ahmed Abdellahaefa18e2019-10-24 10:12:51 +0100498
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100499 UTIL_freeFileNamesTable(table1);
500 UTIL_freeFileNamesTable(table2);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100501
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100502 return newTable;
503}
504
Rohit Jainc7251e52018-10-11 18:05:15 -0700505#ifdef _WIN32
Yann Collet76b9e422019-11-05 14:59:45 -0800506static int UTIL_prepareFileList(const char* dirName,
507 char** bufStart, size_t* pos,
508 char** bufEnd, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -0700509{
510 char* path;
Yann Collet74d872e2019-10-25 18:26:30 -0700511 size_t dirLength, pathLength;
512 int nbFiles = 0;
Rohit Jain705e0b12018-10-11 15:51:57 -0700513 WIN32_FIND_DATAA cFile;
514 HANDLE hFile;
515
Yann Collet1ead0c52019-10-25 16:36:59 -0700516 dirLength = strlen(dirName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700517 path = (char*) malloc(dirLength + 3);
518 if (!path) return 0;
519
520 memcpy(path, dirName, dirLength);
521 path[dirLength] = '\\';
522 path[dirLength+1] = '*';
523 path[dirLength+2] = 0;
524
525 hFile=FindFirstFileA(path, &cFile);
526 if (hFile == INVALID_HANDLE_VALUE) {
527 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName);
528 return 0;
529 }
530 free(path);
531
532 do {
Yann Collet1ead0c52019-10-25 16:36:59 -0700533 size_t const fnameLength = strlen(cFile.cFileName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700534 path = (char*) malloc(dirLength + fnameLength + 2);
535 if (!path) { FindClose(hFile); return 0; }
536 memcpy(path, dirName, dirLength);
537 path[dirLength] = '\\';
538 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
539 pathLength = dirLength+1+fnameLength;
540 path[pathLength] = 0;
541 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800542 if ( strcmp (cFile.cFileName, "..") == 0
543 || strcmp (cFile.cFileName, ".") == 0 )
544 continue;
545 /* Recursively call "UTIL_prepareFileList" with the new path. */
546 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks);
Rohit Jain705e0b12018-10-11 15:51:57 -0700547 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
Yann Collet72dbf1b2018-12-20 12:27:12 -0800548 } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
549 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
550 || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) {
Rohit Jain705e0b12018-10-11 15:51:57 -0700551 if (*bufStart + *pos + pathLength >= *bufEnd) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800552 ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
Rohit Jain705e0b12018-10-11 15:51:57 -0700553 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
Rohit Jain705e0b12018-10-11 15:51:57 -0700554 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
Yann Collet72dbf1b2018-12-20 12:27:12 -0800555 *bufEnd = *bufStart + newListSize;
Rohit Jain705e0b12018-10-11 15:51:57 -0700556 }
557 if (*bufStart + *pos + pathLength < *bufEnd) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800558 memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */);
Rohit Jain705e0b12018-10-11 15:51:57 -0700559 *pos += pathLength + 1;
560 nbFiles++;
Yann Collet1ead0c52019-10-25 16:36:59 -0700561 } }
Rohit Jain705e0b12018-10-11 15:51:57 -0700562 free(path);
563 } while (FindNextFileA(hFile, &cFile));
564
565 FindClose(hFile);
566 return nbFiles;
567}
568
569#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
570
Yann Collet76b9e422019-11-05 14:59:45 -0800571static int UTIL_prepareFileList(const char *dirName,
572 char** bufStart, size_t* pos,
573 char** bufEnd, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -0700574{
Yann Collet1ead0c52019-10-25 16:36:59 -0700575 DIR* dir;
576 struct dirent * entry;
577 size_t dirLength;
Yann Colleta71256a2019-10-17 11:01:20 -0700578 int nbFiles = 0;
Rohit Jain705e0b12018-10-11 15:51:57 -0700579
580 if (!(dir = opendir(dirName))) {
581 UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
582 return 0;
583 }
584
Yann Colleta71256a2019-10-17 11:01:20 -0700585 dirLength = strlen(dirName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700586 errno = 0;
587 while ((entry = readdir(dir)) != NULL) {
Yann Collet1ead0c52019-10-25 16:36:59 -0700588 char* path;
589 size_t fnameLength, pathLength;
Rohit Jain705e0b12018-10-11 15:51:57 -0700590 if (strcmp (entry->d_name, "..") == 0 ||
591 strcmp (entry->d_name, ".") == 0) continue;
Yann Colleta71256a2019-10-17 11:01:20 -0700592 fnameLength = strlen(entry->d_name);
Rohit Jain705e0b12018-10-11 15:51:57 -0700593 path = (char*) malloc(dirLength + fnameLength + 2);
594 if (!path) { closedir(dir); return 0; }
595 memcpy(path, dirName, dirLength);
596
597 path[dirLength] = '/';
598 memcpy(path+dirLength+1, entry->d_name, fnameLength);
599 pathLength = dirLength+1+fnameLength;
600 path[pathLength] = 0;
601
602 if (!followLinks && UTIL_isLink(path)) {
603 UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
LeeYoung624793b94b2019-07-25 21:07:57 +0800604 free(path);
Rohit Jain705e0b12018-10-11 15:51:57 -0700605 continue;
606 }
607
608 if (UTIL_isDirectory(path)) {
609 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
610 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
611 } else {
612 if (*bufStart + *pos + pathLength >= *bufEnd) {
613 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
Yann Colleta71256a2019-10-17 11:01:20 -0700614 assert(newListSize >= 0);
615 *bufStart = (char*)UTIL_realloc(*bufStart, (size_t)newListSize);
Rohit Jain705e0b12018-10-11 15:51:57 -0700616 *bufEnd = *bufStart + newListSize;
617 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
618 }
619 if (*bufStart + *pos + pathLength < *bufEnd) {
Yann Collet72dbf1b2018-12-20 12:27:12 -0800620 memcpy(*bufStart + *pos, path, pathLength + 1); /* with final \0 */
Rohit Jain705e0b12018-10-11 15:51:57 -0700621 *pos += pathLength + 1;
622 nbFiles++;
Yann Collet1ead0c52019-10-25 16:36:59 -0700623 } }
Rohit Jain705e0b12018-10-11 15:51:57 -0700624 free(path);
625 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
626 }
627
628 if (errno != 0) {
Yann Collet96ee2072019-11-26 15:44:33 -0800629 UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s \n", dirName, strerror(errno));
Rohit Jain705e0b12018-10-11 15:51:57 -0700630 free(*bufStart);
631 *bufStart = NULL;
632 }
633 closedir(dir);
634 return nbFiles;
635}
636
637#else
638
Yann Collet76b9e422019-11-05 14:59:45 -0800639static int UTIL_prepareFileList(const char *dirName,
640 char** bufStart, size_t* pos,
641 char** bufEnd, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -0700642{
643 (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks;
Yann Collet96ee2072019-11-26 15:44:33 -0800644 UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE) \n", dirName);
Rohit Jain705e0b12018-10-11 15:51:57 -0700645 return 0;
646}
647
648#endif /* #ifdef _WIN32 */
649
Shashank Tavildar0f2bff22019-10-28 18:21:47 -0700650int UTIL_isCompressedFile(const char *inputName, const char *extensionList[])
Shashank Tavildar48f85662019-10-25 15:49:11 -0700651{
Shashank Tavildar9ab6a742019-10-29 12:27:54 -0700652 const char* ext = UTIL_getFileExtension(inputName);
Shashank Tavildar3c1649f2019-10-29 15:59:20 -0700653 while(*extensionList!=NULL)
Shashank Tavildarc5060992019-10-29 12:56:04 -0700654 {
Shashank Tavildar3c1649f2019-10-29 15:59:20 -0700655 const int isCompressedExtension = strcmp(ext,*extensionList);
656 if(isCompressedExtension==0)
657 return 1;
658 ++extensionList;
Shashank Tavildarc5060992019-10-29 12:56:04 -0700659 }
Shashank Tavildar02433e02019-10-28 14:54:54 -0700660 return 0;
Shashank Tavildar48f85662019-10-25 15:49:11 -0700661}
Shashank Tavildar0f2bff22019-10-28 18:21:47 -0700662
Shashank Tavildar9ab6a742019-10-29 12:27:54 -0700663/*Utility function to get file extension from file */
664const char* UTIL_getFileExtension(const char* infilename)
665{
666 const char* extension = strrchr(infilename, '.');
667 if(!extension || extension==infilename) return "";
668 return extension;
669}
670
Xin Xie9a8ccd42020-06-19 19:35:51 -0700671static int pathnameHas2Dots(const char *pathname)
672{
673 return NULL != strstr(pathname, "..");
674}
675
676static int isFileNameValidForMirroredOutput(const char *filename)
677{
678 return !pathnameHas2Dots(filename);
679}
680
681
682#define DIR_DEFAULT_MODE 0755
683static mode_t getDirMode(const char *dirName)
684{
685 stat_t st;
W. Felix Handte51ac0202020-08-10 15:28:02 -0400686 if (!UTIL_stat(dirName, &st)) {
Xin Xie9a8ccd42020-06-19 19:35:51 -0700687 UTIL_DISPLAY("zstd: failed to get DIR stats %s: %s\n", dirName, strerror(errno));
688 return DIR_DEFAULT_MODE;
689 }
W. Felix Handte51ac0202020-08-10 15:28:02 -0400690 if (!UTIL_isDirectoryStat(&st)) {
691 UTIL_DISPLAY("zstd: expected directory: %s\n", dirName);
692 return DIR_DEFAULT_MODE;
693 }
Xin Xie9a8ccd42020-06-19 19:35:51 -0700694 return st.st_mode;
695}
696
697static int makeDir(const char *dir, mode_t mode)
698{
699#if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__)
700 int ret = _mkdir(dir);
701 (void) mode;
702#else
703 int ret = mkdir(dir, mode);
704#endif
705 if (ret != 0) {
706 if (errno == EEXIST)
707 return 0;
708 UTIL_DISPLAY("zstd: failed to create DIR %s: %s\n", dir, strerror(errno));
709 }
710 return ret;
711}
712
713/* this function requires a mutable input string */
714static void convertPathnameToDirName(char *pathname)
715{
716 size_t len = 0;
717 char* pos = NULL;
718 /* get dir name from pathname similar to 'dirname()' */
719 assert(pathname != NULL);
720
721 /* remove trailing '/' chars */
722 len = strlen(pathname);
723 assert(len > 0);
724 while (pathname[len] == PATH_SEP) {
725 pathname[len] = '\0';
726 len--;
727 }
728 if (len == 0) return;
729
730 /* if input is a single file, return '.' instead. i.e.
731 * "xyz/abc/file.txt" => "xyz/abc"
732 "./file.txt" => "."
733 "file.txt" => "."
734 */
735 pos = strrchr(pathname, PATH_SEP);
736 if (pos == NULL) {
737 pathname[0] = '.';
738 pathname[1] = '\0';
739 } else {
740 *pos = '\0';
741 }
742}
743
744/* pathname must be valid */
745static const char* trimLeadingRootChar(const char *pathname)
746{
747 assert(pathname != NULL);
748 if (pathname[0] == PATH_SEP)
749 return pathname + 1;
750 return pathname;
751}
752
753/* pathname must be valid */
754static const char* trimLeadingCurrentDirConst(const char *pathname)
755{
756 assert(pathname != NULL);
757 if ((pathname[0] == '.') && (pathname[1] == PATH_SEP))
758 return pathname + 2;
759 return pathname;
760}
761
762static char*
763trimLeadingCurrentDir(char *pathname)
764{
765 /* 'union charunion' can do const-cast without compiler warning */
766 union charunion {
767 char *chr;
768 const char* cchr;
769 } ptr;
770 ptr.cchr = trimLeadingCurrentDirConst(pathname);
771 return ptr.chr;
772}
773
774/* remove leading './' or '/' chars here */
775static const char * trimPath(const char *pathname)
776{
777 return trimLeadingRootChar(
778 trimLeadingCurrentDirConst(pathname));
779}
780
781static char* mallocAndJoin2Dir(const char *dir1, const char *dir2)
782{
783 const size_t dir1Size = strlen(dir1);
784 const size_t dir2Size = strlen(dir2);
785 char *outDirBuffer, *buffer, trailingChar;
786
787 assert(dir1 != NULL && dir2 != NULL);
788 outDirBuffer = (char *) malloc(dir1Size + dir2Size + 2);
789 CONTROL(outDirBuffer != NULL);
790
791 strncpy(outDirBuffer, dir1, dir1Size);
792 outDirBuffer[dir1Size] = '\0';
793
794 if (dir2[0] == '.')
795 return outDirBuffer;
796
797 buffer = outDirBuffer + dir1Size;
798 trailingChar = *(buffer - 1);
799 if (trailingChar != PATH_SEP) {
800 *buffer = PATH_SEP;
801 buffer++;
802 }
803 strncpy(buffer, dir2, dir2Size);
804 buffer[dir2Size] = '\0';
805
806 return outDirBuffer;
807}
808
809/* this function will return NULL if input srcFileName is not valid name for mirrored output path */
810char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName)
811{
812 char* pathname = NULL;
813 if (!isFileNameValidForMirroredOutput(srcFileName))
814 return NULL;
815
816 pathname = mallocAndJoin2Dir(outDirRootName, trimPath(srcFileName));
817
818 convertPathnameToDirName(pathname);
819 return pathname;
820}
821
822static int
823mirrorSrcDir(char* srcDirName, const char* outDirName)
824{
825 mode_t srcMode;
826 int status = 0;
827 char* newDir = mallocAndJoin2Dir(outDirName, trimPath(srcDirName));
828 if (!newDir)
829 return -ENOMEM;
830
831 srcMode = getDirMode(srcDirName);
832 status = makeDir(newDir, srcMode);
833 free(newDir);
834 return status;
835}
836
837static int
838mirrorSrcDirRecursive(char* srcDirName, const char* outDirName)
839{
840 int status = 0;
841 char* pp = trimLeadingCurrentDir(srcDirName);
842 char* sp = NULL;
843
844 while ((sp = strchr(pp, PATH_SEP)) != NULL) {
845 if (sp != pp) {
846 *sp = '\0';
847 status = mirrorSrcDir(srcDirName, outDirName);
848 if (status != 0)
849 return status;
850 *sp = PATH_SEP;
851 }
852 pp = sp + 1;
853 }
854 status = mirrorSrcDir(srcDirName, outDirName);
855 return status;
856}
857
858static void
859makeMirroredDestDirsWithSameSrcDirMode(char** srcDirNames, unsigned nbFile, const char* outDirName)
860{
861 unsigned int i = 0;
862 for (i = 0; i < nbFile; i++)
863 mirrorSrcDirRecursive(srcDirNames[i], outDirName);
864}
865
866static int
867firstIsParentOrSameDirOfSecond(const char* firstDir, const char* secondDir)
868{
869 size_t firstDirLen = strlen(firstDir),
870 secondDirLen = strlen(secondDir);
871 return firstDirLen <= secondDirLen &&
872 (secondDir[firstDirLen] == PATH_SEP || secondDir[firstDirLen] == '\0') &&
873 0 == strncmp(firstDir, secondDir, firstDirLen);
874}
875
876static int compareDir(const void* pathname1, const void* pathname2) {
877 /* sort it after remove the leading '/' or './'*/
878 const char* s1 = trimPath(*(char * const *) pathname1);
879 const char* s2 = trimPath(*(char * const *) pathname2);
880 return strcmp(s1, s2);
881}
882
883static void
884makeUniqueMirroredDestDirs(char** srcDirNames, unsigned nbFile, const char* outDirName)
885{
886 unsigned int i = 0, uniqueDirNr = 0;
887 char** uniqueDirNames = NULL;
888
889 if (nbFile == 0)
890 return;
891
892 uniqueDirNames = (char** ) malloc(nbFile * sizeof (char *));
893 CONTROL(uniqueDirNames != NULL);
894
895 /* if dirs is "a/b/c" and "a/b/c/d", we only need call:
896 * we just need "a/b/c/d" */
897 qsort((void *)srcDirNames, nbFile, sizeof(char*), compareDir);
898
899 uniqueDirNr = 1;
900 uniqueDirNames[uniqueDirNr - 1] = srcDirNames[0];
901 for (i = 1; i < nbFile; i++) {
902 char* prevDirName = srcDirNames[i - 1];
903 char* currDirName = srcDirNames[i];
904
905 /* note: we alwasy compare trimmed path, i.e.:
906 * src dir of "./foo" and "/foo" will be both saved into:
907 * "outDirName/foo/" */
908 if (!firstIsParentOrSameDirOfSecond(trimPath(prevDirName),
909 trimPath(currDirName)))
910 uniqueDirNr++;
911
912 /* we need maintain original src dir name instead of trimmed
913 * dir, so we can retrive the original src dir's mode_t */
914 uniqueDirNames[uniqueDirNr - 1] = currDirName;
915 }
916
917 makeMirroredDestDirsWithSameSrcDirMode(uniqueDirNames, uniqueDirNr, outDirName);
918
919 free(uniqueDirNames);
920}
921
922static void
923makeMirroredDestDirs(char** srcFileNames, unsigned nbFile, const char* outDirName)
924{
925 unsigned int i = 0;
926 for (i = 0; i < nbFile; ++i)
927 convertPathnameToDirName(srcFileNames[i]);
928 makeUniqueMirroredDestDirs(srcFileNames, nbFile, outDirName);
929}
930
931void UTIL_mirrorSourceFilesDirectories(const char** inFileNames, unsigned int nbFile, const char* outDirName)
932{
933 unsigned int i = 0, validFilenamesNr = 0;
934 char** srcFileNames = (char **) malloc(nbFile * sizeof (char *));
935 CONTROL(srcFileNames != NULL);
936
937 /* check input filenames is valid */
938 for (i = 0; i < nbFile; ++i) {
939 if (isFileNameValidForMirroredOutput(inFileNames[i])) {
940 char* fname = STRDUP(inFileNames[i]);
941 CONTROL(fname != NULL);
942 srcFileNames[validFilenamesNr++] = fname;
943 }
944 }
945
946 if (validFilenamesNr > 0) {
947 makeDir(outDirName, DIR_DEFAULT_MODE);
948 makeMirroredDestDirs(srcFileNames, validFilenamesNr, outDirName);
949 }
950
951 for (i = 0; i < validFilenamesNr; i++)
952 free(srcFileNames[i]);
953 free(srcFileNames);
954}
Yann Colletb09f5932019-11-05 17:02:43 -0800955
Yann Collet31a0abb2019-11-06 09:10:05 -0800956FileNamesTable*
957UTIL_createExpandedFNT(const char** inputNames, size_t nbIfns, int followLinks)
Rohit Jain705e0b12018-10-11 15:51:57 -0700958{
Yann Colletb09f5932019-11-05 17:02:43 -0800959 unsigned nbFiles;
Rohit Jain705e0b12018-10-11 15:51:57 -0700960 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
961 char* bufend = buf + LIST_SIZE_INCREASE;
Rohit Jain705e0b12018-10-11 15:51:57 -0700962
963 if (!buf) return NULL;
964
Yann Colletd5b4a7e2019-11-26 17:46:57 -0800965 { size_t ifnNb, pos;
Yann Colletb09f5932019-11-05 17:02:43 -0800966 for (ifnNb=0, pos=0, nbFiles=0; ifnNb<nbIfns; ifnNb++) {
967 if (!UTIL_isDirectory(inputNames[ifnNb])) {
968 size_t const len = strlen(inputNames[ifnNb]);
969 if (buf + pos + len >= bufend) {
970 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
971 assert(newListSize >= 0);
972 buf = (char*)UTIL_realloc(buf, (size_t)newListSize);
Yann Colletb09f5932019-11-05 17:02:43 -0800973 if (!buf) return NULL;
Yann Colletd5b4a7e2019-11-26 17:46:57 -0800974 bufend = buf + newListSize;
Yann Colletb09f5932019-11-05 17:02:43 -0800975 }
976 if (buf + pos + len < bufend) {
977 memcpy(buf+pos, inputNames[ifnNb], len+1); /* including final \0 */
978 pos += len + 1;
979 nbFiles++;
980 }
981 } else {
982 nbFiles += (unsigned)UTIL_prepareFileList(inputNames[ifnNb], &buf, &pos, &bufend, followLinks);
983 if (buf == NULL) return NULL;
984 } } }
Rohit Jain705e0b12018-10-11 15:51:57 -0700985
Yann Colleta49417b2019-12-02 14:28:18 -0800986 /* note : even if nbFiles==0, function returns a valid, though empty, FileNamesTable* object */
Rohit Jain705e0b12018-10-11 15:51:57 -0700987
Yann Colletd5b4a7e2019-11-26 17:46:57 -0800988 { size_t ifnNb, pos;
Yann Colleta49417b2019-12-02 14:28:18 -0800989 size_t const fntCapacity = nbFiles + 1; /* minimum 1, allows adding one reference, typically stdin */
990 const char** const fileNamesTable = (const char**)malloc(fntCapacity * sizeof(*fileNamesTable));
Yann Colletb09f5932019-11-05 17:02:43 -0800991 if (!fileNamesTable) { free(buf); return NULL; }
Rohit Jain705e0b12018-10-11 15:51:57 -0700992
Yann Colletb09f5932019-11-05 17:02:43 -0800993 for (ifnNb = 0, pos = 0; ifnNb < nbFiles; ifnNb++) {
994 fileNamesTable[ifnNb] = buf + pos;
995 if (buf + pos > bufend) { free(buf); free((void*)fileNamesTable); return NULL; }
996 pos += strlen(fileNamesTable[ifnNb]) + 1;
Yann Collet29e46ed2019-10-18 14:28:34 -0700997 }
Yann Colleta49417b2019-12-02 14:28:18 -0800998 return UTIL_assembleFileNamesTable2(fileNamesTable, nbFiles, fntCapacity, buf);
Rohit Jain705e0b12018-10-11 15:51:57 -0700999 }
Rohit Jain705e0b12018-10-11 15:51:57 -07001000}
1001
Yann Collet59a71162019-04-10 12:37:03 -07001002
Yann Collet31a0abb2019-11-06 09:10:05 -08001003void UTIL_expandFNT(FileNamesTable** fnt, int followLinks)
Yann Collet76b9e422019-11-05 14:59:45 -08001004{
Yann Collet31a0abb2019-11-06 09:10:05 -08001005 FileNamesTable* const newFNT = UTIL_createExpandedFNT((*fnt)->fileNames, (*fnt)->tableSize, followLinks);
Yann Colleta49417b2019-12-02 14:28:18 -08001006 CONTROL(newFNT != NULL);
Yann Collet31a0abb2019-11-06 09:10:05 -08001007 UTIL_freeFileNamesTable(*fnt);
1008 *fnt = newFNT;
Yann Collet76b9e422019-11-05 14:59:45 -08001009}
1010
Yann Colleta7e33e32019-11-06 14:42:13 -08001011FileNamesTable* UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames)
1012{
1013 size_t const sizeof_FNTable = nbFilenames * sizeof(*filenames);
1014 const char** const newFNTable = (const char**)malloc(sizeof_FNTable);
1015 if (newFNTable==NULL) return NULL;
Yann Collet9df49dc2019-11-06 15:23:44 -08001016 memcpy((void*)newFNTable, filenames, sizeof_FNTable); /* void* : mitigate a Visual compiler bug or limitation */
Yann Colletd5b4a7e2019-11-26 17:46:57 -08001017 return UTIL_assembleFileNamesTable(newFNTable, nbFilenames, NULL);
Yann Colleta7e33e32019-11-06 14:42:13 -08001018}
1019
Yann Collet72dbf1b2018-12-20 12:27:12 -08001020
Rohit Jaina47f6e62018-10-11 16:51:29 -07001021/*-****************************************
Yann Collet59a71162019-04-10 12:37:03 -07001022* count the number of physical cores
Rohit Jaind6d240f2018-10-11 15:07:12 -07001023******************************************/
Rohit Jainc7251e52018-10-11 18:05:15 -07001024
Rohit Jain91b2fed2018-10-11 17:34:47 -07001025#if defined(_WIN32) || defined(WIN32)
1026
1027#include <windows.h>
1028
1029typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD);
1030
1031int UTIL_countPhysicalCores(void)
1032{
1033 static int numPhysicalCores = 0;
1034 if (numPhysicalCores != 0) return numPhysicalCores;
1035
1036 { LPFN_GLPI glpi;
1037 BOOL done = FALSE;
1038 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL;
1039 PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL;
1040 DWORD returnLength = 0;
1041 size_t byteOffset = 0;
1042
Yann Collet0492c572019-10-18 17:08:52 -07001043#if defined(_MSC_VER)
Yann Colletf3796372019-10-18 17:05:42 -07001044/* Visual Studio does not like the following cast */
1045# pragma warning( disable : 4054 ) /* conversion from function ptr to data ptr */
1046# pragma warning( disable : 4055 ) /* conversion from data ptr to function ptr */
1047#endif
Yann Collet1bd6c152019-10-18 15:45:31 -07001048 glpi = (LPFN_GLPI)(void*)GetProcAddress(GetModuleHandle(TEXT("kernel32")),
1049 "GetLogicalProcessorInformation");
Rohit Jain91b2fed2018-10-11 17:34:47 -07001050
1051 if (glpi == NULL) {
1052 goto failed;
1053 }
1054
1055 while(!done) {
1056 DWORD rc = glpi(buffer, &returnLength);
1057 if (FALSE == rc) {
1058 if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
1059 if (buffer)
1060 free(buffer);
1061 buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength);
1062
1063 if (buffer == NULL) {
1064 perror("zstd");
1065 exit(1);
1066 }
1067 } else {
1068 /* some other error */
1069 goto failed;
1070 }
1071 } else {
1072 done = TRUE;
Yann Collet96ee2072019-11-26 15:44:33 -08001073 } }
Rohit Jain91b2fed2018-10-11 17:34:47 -07001074
1075 ptr = buffer;
1076
1077 while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) {
1078
1079 if (ptr->Relationship == RelationProcessorCore) {
1080 numPhysicalCores++;
1081 }
1082
1083 ptr++;
1084 byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
1085 }
1086
1087 free(buffer);
1088
1089 return numPhysicalCores;
1090 }
1091
1092failed:
1093 /* try to fall back on GetSystemInfo */
1094 { SYSTEM_INFO sysinfo;
1095 GetSystemInfo(&sysinfo);
1096 numPhysicalCores = sysinfo.dwNumberOfProcessors;
1097 if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */
1098 }
1099 return numPhysicalCores;
1100}
1101
1102#elif defined(__APPLE__)
1103
1104#include <sys/sysctl.h>
1105
1106/* Use apple-provided syscall
1107 * see: man 3 sysctl */
1108int UTIL_countPhysicalCores(void)
1109{
1110 static S32 numPhysicalCores = 0; /* apple specifies int32_t */
1111 if (numPhysicalCores != 0) return numPhysicalCores;
1112
1113 { size_t size = sizeof(S32);
1114 int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0);
1115 if (ret != 0) {
1116 if (errno == ENOENT) {
1117 /* entry not present, fall back on 1 */
1118 numPhysicalCores = 1;
1119 } else {
1120 perror("zstd: can't get number of physical cpus");
1121 exit(1);
1122 }
1123 }
1124
1125 return numPhysicalCores;
1126 }
1127}
1128
1129#elif defined(__linux__)
1130
1131/* parse /proc/cpuinfo
1132 * siblings / cpu cores should give hyperthreading ratio
1133 * otherwise fall back on sysconf */
1134int UTIL_countPhysicalCores(void)
1135{
1136 static int numPhysicalCores = 0;
1137
1138 if (numPhysicalCores != 0) return numPhysicalCores;
1139
1140 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1141 if (numPhysicalCores == -1) {
1142 /* value not queryable, fall back on 1 */
1143 return numPhysicalCores = 1;
1144 }
1145
1146 /* try to determine if there's hyperthreading */
1147 { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r");
1148#define BUF_SIZE 80
1149 char buff[BUF_SIZE];
1150
1151 int siblings = 0;
1152 int cpu_cores = 0;
1153 int ratio = 1;
1154
1155 if (cpuinfo == NULL) {
1156 /* fall back on the sysconf value */
1157 return numPhysicalCores;
1158 }
1159
1160 /* assume the cpu cores/siblings values will be constant across all
1161 * present processors */
1162 while (!feof(cpuinfo)) {
1163 if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) {
1164 if (strncmp(buff, "siblings", 8) == 0) {
1165 const char* const sep = strchr(buff, ':');
LeeYoung624c5caaf52019-07-29 17:05:50 +08001166 if (sep == NULL || *sep == '\0') {
Rohit Jain91b2fed2018-10-11 17:34:47 -07001167 /* formatting was broken? */
1168 goto failed;
1169 }
1170
1171 siblings = atoi(sep + 1);
1172 }
1173 if (strncmp(buff, "cpu cores", 9) == 0) {
1174 const char* const sep = strchr(buff, ':');
LeeYoung624c5caaf52019-07-29 17:05:50 +08001175 if (sep == NULL || *sep == '\0') {
Rohit Jain91b2fed2018-10-11 17:34:47 -07001176 /* formatting was broken? */
1177 goto failed;
1178 }
1179
1180 cpu_cores = atoi(sep + 1);
1181 }
1182 } else if (ferror(cpuinfo)) {
1183 /* fall back on the sysconf value */
1184 goto failed;
Yann Collet96ee2072019-11-26 15:44:33 -08001185 } }
Rohit Jain91b2fed2018-10-11 17:34:47 -07001186 if (siblings && cpu_cores) {
1187 ratio = siblings / cpu_cores;
1188 }
1189failed:
1190 fclose(cpuinfo);
1191 return numPhysicalCores = numPhysicalCores / ratio;
1192 }
1193}
1194
Conrad Meyerfe826372019-01-04 11:57:12 -08001195#elif defined(__FreeBSD__)
Rohit Jain91b2fed2018-10-11 17:34:47 -07001196
Conrad Meyerfe826372019-01-04 11:57:12 -08001197#include <sys/param.h>
1198#include <sys/sysctl.h>
1199
1200/* Use physical core sysctl when available
1201 * see: man 4 smp, man 3 sysctl */
1202int UTIL_countPhysicalCores(void)
1203{
1204 static int numPhysicalCores = 0; /* freebsd sysctl is native int sized */
1205 if (numPhysicalCores != 0) return numPhysicalCores;
1206
1207#if __FreeBSD_version >= 1300008
1208 { size_t size = sizeof(numPhysicalCores);
1209 int ret = sysctlbyname("kern.smp.cores", &numPhysicalCores, &size, NULL, 0);
1210 if (ret == 0) return numPhysicalCores;
1211 if (errno != ENOENT) {
1212 perror("zstd: can't get number of physical cpus");
1213 exit(1);
1214 }
1215 /* sysctl not present, fall through to older sysconf method */
1216 }
1217#endif
1218
1219 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1220 if (numPhysicalCores == -1) {
1221 /* value not queryable, fall back on 1 */
1222 numPhysicalCores = 1;
1223 }
1224 return numPhysicalCores;
1225}
1226
Christoph Reiterd0dcaf52020-01-08 00:48:26 +01001227#elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__CYGWIN__)
Conrad Meyerfe826372019-01-04 11:57:12 -08001228
1229/* Use POSIX sysconf
1230 * see: man 3 sysconf */
Rohit Jain91b2fed2018-10-11 17:34:47 -07001231int UTIL_countPhysicalCores(void)
1232{
1233 static int numPhysicalCores = 0;
1234
1235 if (numPhysicalCores != 0) return numPhysicalCores;
1236
1237 numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN);
1238 if (numPhysicalCores == -1) {
1239 /* value not queryable, fall back on 1 */
1240 return numPhysicalCores = 1;
1241 }
1242 return numPhysicalCores;
1243}
1244
1245#else
1246
1247int UTIL_countPhysicalCores(void)
1248{
1249 /* assume 1 */
1250 return 1;
1251}
1252
1253#endif
1254
Rohit Jainf881ee82018-10-11 12:52:19 -07001255#if defined (__cplusplus)
1256}
1257#endif