Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 1 | /* |
Nick Terrell | ac58c8d | 2020-03-26 15:19:05 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2020, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under both the BSD-style license (found in the |
| 6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 7 | * in the COPYING file in the root directory of this source tree). |
| 8 | * You may select, at your option, one of the above-listed licenses. |
| 9 | */ |
| 10 | |
| 11 | #if defined (__cplusplus) |
| 12 | extern "C" { |
| 13 | #endif |
| 14 | |
| 15 | |
| 16 | /*-**************************************** |
| 17 | * Dependencies |
| 18 | ******************************************/ |
Yann Collet | ffba142 | 2018-12-20 14:30:30 -0800 | [diff] [blame] | 19 | #include "util.h" /* note : ensure that platform.h is included first ! */ |
Yann Collet | a684b82 | 2019-11-26 15:16:53 -0800 | [diff] [blame] | 20 | #include <stdlib.h> /* malloc, realloc, free */ |
Yann Collet | aaab618 | 2019-11-26 15:25:32 -0800 | [diff] [blame] | 21 | #include <stdio.h> /* fprintf */ |
Yann Collet | a684b82 | 2019-11-26 15:16:53 -0800 | [diff] [blame] | 22 | #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */ |
Yann Collet | 173ef9d | 2018-12-19 18:30:57 -0800 | [diff] [blame] | 23 | #include <errno.h> |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 24 | #include <assert.h> |
Yann Collet | 173ef9d | 2018-12-19 18:30:57 -0800 | [diff] [blame] | 25 | |
Yann Collet | a684b82 | 2019-11-26 15:16:53 -0800 | [diff] [blame] | 26 | #if defined(_WIN32) |
| 27 | # include <sys/utime.h> /* utime */ |
| 28 | # include <io.h> /* _chmod */ |
| 29 | #else |
| 30 | # include <unistd.h> /* chown, stat */ |
Fabrice Fontaine | 26d01bd | 2020-07-15 21:19:14 +0200 | [diff] [blame] | 31 | # if PLATFORM_POSIX_VERSION < 200809L || !defined(st_mtime) |
Yann Collet | a684b82 | 2019-11-26 15:16:53 -0800 | [diff] [blame] | 32 | # include <utime.h> /* utime */ |
| 33 | # else |
| 34 | # include <fcntl.h> /* AT_FDCWD */ |
| 35 | # include <sys/stat.h> /* utimensat */ |
| 36 | # endif |
| 37 | #endif |
| 38 | |
Sen Huang | 62616c4 | 2019-09-06 13:20:50 -0700 | [diff] [blame] | 39 | #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__) |
| 40 | #include <direct.h> /* needed for _mkdir in windows */ |
| 41 | #endif |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 42 | |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 43 | #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 Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 48 | /*-**************************************** |
| 49 | * Internal Macros |
| 50 | ******************************************/ |
| 51 | |
Yann Collet | 7543cd0 | 2019-11-26 15:21:58 -0800 | [diff] [blame] | 52 | /* 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 Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 57 | */ |
| 58 | #define CONTROL(c) { \ |
| 59 | if (!(c)) { \ |
| 60 | UTIL_DISPLAYLEVEL(1, "Error : %s, %i : %s", \ |
| 61 | __FILE__, __LINE__, #c); \ |
Yann Collet | 3e5c81e | 2019-10-26 00:01:11 -0700 | [diff] [blame] | 62 | exit(1); \ |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 63 | } } |
| 64 | |
Yann Collet | 7543cd0 | 2019-11-26 15:21:58 -0800 | [diff] [blame] | 65 | /* 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 Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 70 | * If UTIL_realloc() fails the original block is freed. |
| 71 | */ |
| 72 | UTIL_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 Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 80 | #if defined(_MSC_VER) |
| 81 | #define chmod _chmod |
| 82 | #endif |
| 83 | |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 84 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 85 | /*-**************************************** |
| 86 | * Console log |
| 87 | ******************************************/ |
| 88 | int g_utilDisplayLevel; |
| 89 | |
senhuang42 | 7991c55 | 2020-08-26 16:50:20 -0400 | [diff] [blame] | 90 | int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg, |
senhuang42 | 93d63ea | 2020-09-24 15:58:06 -0400 | [diff] [blame] | 91 | const char* acceptableLetters, int hasStdinInput) { |
senhuang42 | 7991c55 | 2020-08-26 16:50:20 -0400 | [diff] [blame] | 92 | int ch, result; |
senhuang42 | 7aa3da1 | 2020-09-22 14:15:52 -0400 | [diff] [blame] | 93 | |
senhuang42 | 88f4410 | 2020-09-24 16:29:12 -0400 | [diff] [blame] | 94 | if (hasStdinInput) { |
senhuang42 | 02422db | 2020-09-25 11:51:35 -0400 | [diff] [blame] | 95 | UTIL_DISPLAY("stdin is an input - not proceeding.\n"); |
senhuang42 | 93d63ea | 2020-09-24 15:58:06 -0400 | [diff] [blame] | 96 | return 1; |
senhuang42 | 88f4410 | 2020-09-24 16:29:12 -0400 | [diff] [blame] | 97 | } |
senhuang42 | 93d63ea | 2020-09-24 15:58:06 -0400 | [diff] [blame] | 98 | |
senhuang42 | aab11ce | 2020-08-25 11:25:49 -0400 | [diff] [blame] | 99 | UTIL_DISPLAY("%s", prompt); |
| 100 | ch = getchar(); |
senhuang42 | 7991c55 | 2020-08-26 16:50:20 -0400 | [diff] [blame] | 101 | result = 0; |
senhuang42 | aab11ce | 2020-08-25 11:25:49 -0400 | [diff] [blame] | 102 | if (strchr(acceptableLetters, ch) == NULL) { |
| 103 | UTIL_DISPLAY("%s", abortMsg); |
senhuang42 | 7991c55 | 2020-08-26 16:50:20 -0400 | [diff] [blame] | 104 | result = 1; |
senhuang42 | aab11ce | 2020-08-25 11:25:49 -0400 | [diff] [blame] | 105 | } |
| 106 | /* flush the rest */ |
| 107 | while ((ch!=EOF) && (ch!='\n')) |
| 108 | ch = getchar(); |
senhuang42 | 7991c55 | 2020-08-26 16:50:20 -0400 | [diff] [blame] | 109 | return result; |
senhuang42 | aab11ce | 2020-08-25 11:25:49 -0400 | [diff] [blame] | 110 | } |
| 111 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 112 | |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 113 | /*-************************************* |
| 114 | * Constants |
| 115 | ***************************************/ |
| 116 | #define LIST_SIZE_INCREASE (8*1024) |
Yann Collet | c71bd45 | 2019-11-26 11:20:26 -0800 | [diff] [blame] | 117 | #define MAX_FILE_OF_FILE_NAMES_SIZE (1<<20)*50 |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 118 | |
| 119 | |
| 120 | /*-************************************* |
| 121 | * Functions |
| 122 | ***************************************/ |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 123 | |
W. Felix Handte | b11bea5 | 2020-08-05 00:09:29 -0400 | [diff] [blame] | 124 | int 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 Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 135 | int UTIL_isRegularFile(const char* infilename) |
| 136 | { |
| 137 | stat_t statbuf; |
W. Felix Handte | 51ac020 | 2020-08-10 15:28:02 -0400 | [diff] [blame] | 138 | return UTIL_stat(infilename, &statbuf) && UTIL_isRegularFileStat(&statbuf); |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 139 | } |
| 140 | |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 141 | int 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 Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 150 | /* like chmod, but avoid changing permission of /dev/null */ |
W. Felix Handte | 0a8aacb | 2020-08-05 12:00:12 -0400 | [diff] [blame] | 151 | int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions) |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 152 | { |
W. Felix Handte | 0a8aacb | 2020-08-05 12:00:12 -0400 | [diff] [blame] | 153 | 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 Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 159 | return chmod(filename, permissions); |
| 160 | } |
| 161 | |
W. Felix Handte | 1a1003f | 2020-08-05 00:35:21 -0400 | [diff] [blame] | 162 | int UTIL_setFileStat(const char *filename, const stat_t *statbuf) |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 163 | { |
| 164 | int res = 0; |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 165 | |
W. Felix Handte | c144914 | 2020-08-05 12:10:42 -0400 | [diff] [blame] | 166 | stat_t curStatBuf; |
| 167 | if (!UTIL_stat(filename, &curStatBuf) || !UTIL_isRegularFileStat(&curStatBuf)) |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 168 | return -1; |
| 169 | |
W. Felix Handte | e1ec800 | 2019-09-12 16:27:05 -0400 | [diff] [blame] | 170 | /* set access and modification times */ |
W. Felix Handte | 5666835 | 2019-12-04 16:59:16 -0500 | [diff] [blame] | 171 | /* 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 Handte | 5af8cb7 | 2019-12-04 10:25:07 -0500 | [diff] [blame] | 175 | #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 Handte | e1ec800 | 2019-09-12 16:27:05 -0400 | [diff] [blame] | 183 | { |
| 184 | struct utimbuf timebuf; |
| 185 | timebuf.actime = time(NULL); |
| 186 | timebuf.modtime = statbuf->st_mtime; |
| 187 | res += utime(filename, &timebuf); |
| 188 | } |
Rosen Penev | 41e9065 | 2019-07-30 17:17:07 -0700 | [diff] [blame] | 189 | #endif |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 190 | |
| 191 | #if !defined(_WIN32) |
| 192 | res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */ |
| 193 | #endif |
| 194 | |
W. Felix Handte | c144914 | 2020-08-05 12:10:42 -0400 | [diff] [blame] | 195 | res += UTIL_chmod(filename, &curStatBuf, statbuf->st_mode & 07777); /* Copy file permissions */ |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 196 | |
| 197 | errno = 0; |
| 198 | return -res; /* number of errors is returned */ |
| 199 | } |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 200 | |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 201 | int UTIL_isDirectory(const char* infilename) |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 202 | { |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 203 | stat_t statbuf; |
W. Felix Handte | 51ac020 | 2020-08-10 15:28:02 -0400 | [diff] [blame] | 204 | return UTIL_stat(infilename, &statbuf) && UTIL_isDirectoryStat(&statbuf); |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 205 | } |
| 206 | |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 207 | int 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 Huang | f80437c | 2019-10-02 11:08:20 -0400 | [diff] [blame] | 216 | int UTIL_compareStr(const void *p1, const void *p2) { |
| 217 | return strcmp(* (char * const *) p1, * (char * const *) p2); |
| 218 | } |
Sen Huang | a9c807a | 2019-09-06 10:17:04 -0700 | [diff] [blame] | 219 | |
Yann Collet | 0004043 | 2019-10-17 10:56:14 -0700 | [diff] [blame] | 220 | int UTIL_isSameFile(const char* fName1, const char* fName2) |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 221 | { |
Yann Collet | 0004043 | 2019-10-17 10:56:14 -0700 | [diff] [blame] | 222 | assert(fName1 != NULL); assert(fName2 != NULL); |
| 223 | #if defined(_MSC_VER) || defined(_WIN32) |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 224 | /* note : Visual does not support file identification by inode. |
Yann Collet | 0004043 | 2019-10-17 10:56:14 -0700 | [diff] [blame] | 225 | * inode does not work on Windows, even with a posix layer, like msys2. |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 226 | * The following work-around is limited to detecting exact name repetition only, |
| 227 | * aka `filename` is considered different from `subdir/../filename` */ |
Yann Collet | 157479a | 2019-10-17 14:31:42 -0700 | [diff] [blame] | 228 | return !strcmp(fName1, fName2); |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 229 | #else |
Yann Collet | 0004043 | 2019-10-17 10:56:14 -0700 | [diff] [blame] | 230 | { stat_t file1Stat; |
| 231 | stat_t file2Stat; |
W. Felix Handte | 5fbc6ad | 2020-08-05 00:31:48 -0400 | [diff] [blame] | 232 | return UTIL_stat(fName1, &file1Stat) |
| 233 | && UTIL_stat(fName2, &file2Stat) |
Yann Collet | 0004043 | 2019-10-17 10:56:14 -0700 | [diff] [blame] | 234 | && (file1Stat.st_dev == file2Stat.st_dev) |
| 235 | && (file1Stat.st_ino == file2Stat.st_ino); |
| 236 | } |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 237 | #endif |
| 238 | } |
| 239 | |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 240 | /* UTIL_isFIFO : distinguish named pipes */ |
| 241 | int UTIL_isFIFO(const char* infilename) |
Bimba Shrestha | 8a39748 | 2019-10-22 15:23:22 -0700 | [diff] [blame] | 242 | { |
| 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 Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 246 | if (UTIL_stat(infilename, &statbuf) && UTIL_isFIFOStat(&statbuf)) return 1; |
Bimba Shrestha | 8a39748 | 2019-10-22 15:23:22 -0700 | [diff] [blame] | 247 | #endif |
| 248 | (void)infilename; |
| 249 | return 0; |
| 250 | } |
Bimba Shrestha | 8a39748 | 2019-10-22 15:23:22 -0700 | [diff] [blame] | 251 | |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 252 | /* UTIL_isFIFO : distinguish named pipes */ |
| 253 | int 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 Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 263 | int UTIL_isLink(const char* infilename) |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 264 | { |
| 265 | /* macro guards, as defined in : https://linux.die.net/man/2/lstat */ |
W. Felix Handte | d2c4804 | 2019-06-07 15:31:33 -0400 | [diff] [blame] | 266 | #if PLATFORM_POSIX_VERSION >= 200112L |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 267 | stat_t statbuf; |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 268 | int const r = lstat(infilename, &statbuf); |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 269 | if (!r && S_ISLNK(statbuf.st_mode)) return 1; |
| 270 | #endif |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 271 | (void)infilename; |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | U64 UTIL_getFileSize(const char* infilename) |
| 276 | { |
W. Felix Handte | 69cb9e7 | 2020-08-05 00:21:41 -0400 | [diff] [blame] | 277 | stat_t statbuf; |
| 278 | if (!UTIL_stat(infilename, &statbuf)) return UTIL_FILESIZE_UNKNOWN; |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 279 | return UTIL_getFileSizeStat(&statbuf); |
| 280 | } |
| 281 | |
| 282 | U64 UTIL_getFileSizeStat(const stat_t* statbuf) |
| 283 | { |
| 284 | if (!UTIL_isRegularFileStat(statbuf)) return UTIL_FILESIZE_UNKNOWN; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 285 | #if defined(_MSC_VER) |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 286 | if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 287 | #elif defined(__MINGW32__) && defined (__MSVCRT__) |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 288 | if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 289 | #else |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 290 | if (!S_ISREG(statbuf->st_mode)) return UTIL_FILESIZE_UNKNOWN; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 291 | #endif |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 292 | return (U64)statbuf->st_size; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
Yann Collet | 5fb84ca | 2019-10-25 17:34:29 -0700 | [diff] [blame] | 296 | U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles) |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 297 | { |
| 298 | U64 total = 0; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 299 | unsigned n; |
| 300 | for (n=0; n<nbFiles; n++) { |
| 301 | U64 const size = UTIL_getFileSize(fileNamesTable[n]); |
Yann Collet | 5fb84ca | 2019-10-25 17:34:29 -0700 | [diff] [blame] | 302 | if (size == UTIL_FILESIZE_UNKNOWN) return UTIL_FILESIZE_UNKNOWN; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 303 | total += size; |
| 304 | } |
Yann Collet | 5fb84ca | 2019-10-25 17:34:29 -0700 | [diff] [blame] | 305 | return total; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 308 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 309 | /* condition : @file must be valid, and not have reached its end. |
Yann Collet | d9c634e | 2019-10-28 15:03:32 -0700 | [diff] [blame] | 310 | * @return : length of line written into @buf, ended with `\0` instead of '\n', |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 311 | * or 0, if there is no new line */ |
| 312 | static size_t readLineFromFile(char* buf, size_t len, FILE* file) |
| 313 | { |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 314 | assert(!feof(file)); |
Nick Terrell | f6d00c0 | 2020-01-13 14:22:46 -0800 | [diff] [blame] | 315 | /* Work around Cygwin problem when len == 1 it returns NULL. */ |
| 316 | if (len <= 1) return 0; |
| 317 | CONTROL( fgets(buf, (int) len, file) ); |
Yann Collet | d9c634e | 2019-10-28 15:03:32 -0700 | [diff] [blame] | 318 | { 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 Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 326 | /* 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 | */ |
| 332 | static int |
| 333 | readLinesFromFile(void* dst, size_t dstCapacity, |
| 334 | const char* inputFileName) |
| 335 | { |
| 336 | int nbFiles = 0; |
Yann Collet | 3e5c81e | 2019-10-26 00:01:11 -0700 | [diff] [blame] | 337 | size_t pos = 0; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 338 | char* const buf = (char*)dst; |
| 339 | FILE* const inputFile = fopen(inputFileName, "r"); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 340 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 341 | assert(dst != NULL); |
Ahmed Abdellah | aefa18e | 2019-10-24 10:12:51 +0100 | [diff] [blame] | 342 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 343 | if(!inputFile) { |
| 344 | if (g_utilDisplayLevel >= 1) perror("zstd:util:readLinesFromFile"); |
| 345 | return -1; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 346 | } |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 347 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 348 | 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 Collet | d9c634e | 2019-10-28 15:03:32 -0700 | [diff] [blame] | 352 | pos += lineLength; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 353 | ++nbFiles; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 354 | } |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 355 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 356 | CONTROL( fclose(inputFile) == 0 ); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 357 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 358 | return nbFiles; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /*Note: buf is not freed in case function successfully created table because filesTable->fileNames[0] = buf*/ |
| 362 | FileNamesTable* |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 363 | UTIL_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 Handte | 7238cca | 2020-08-05 01:08:34 -0400 | [diff] [blame] | 369 | stat_t statbuf; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 370 | |
W. Felix Handte | 7238cca | 2020-08-05 01:08:34 -0400 | [diff] [blame] | 371 | if (!UTIL_stat(inputFileName, &statbuf) || !UTIL_isRegularFileStat(&statbuf)) |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 372 | return NULL; |
Ahmed Abdellah | 47712c9 | 2019-10-24 10:30:05 +0100 | [diff] [blame] | 373 | |
W. Felix Handte | 7238cca | 2020-08-05 01:08:34 -0400 | [diff] [blame] | 374 | { U64 const inputFileSize = UTIL_getFileSizeStat(&statbuf); |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 375 | if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE) |
| 376 | return NULL; |
Yann Collet | 12efa1e | 2019-10-26 00:27:32 -0700 | [diff] [blame] | 377 | bufSize = (size_t)(inputFileSize + 1); /* (+1) to add '\0' at the end of last filename */ |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 380 | buf = (char*) malloc(bufSize); |
| 381 | CONTROL( buf != NULL ); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 382 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 383 | { int const ret_nbFiles = readLinesFromFile(buf, bufSize, inputFileName); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 384 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 385 | if (ret_nbFiles <= 0) { |
| 386 | free(buf); |
| 387 | return NULL; |
| 388 | } |
| 389 | nbFiles = (size_t)ret_nbFiles; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 390 | } |
| 391 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 392 | { const char** filenamesTable = (const char**) malloc(nbFiles * sizeof(*filenamesTable)); |
| 393 | CONTROL(filenamesTable != NULL); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 394 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 395 | { 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 Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 401 | |
Yann Collet | 9a3de0a | 2019-11-25 15:34:55 -0800 | [diff] [blame] | 402 | return UTIL_assembleFileNamesTable(filenamesTable, nbFiles, buf); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 403 | } |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 406 | static FileNamesTable* |
| 407 | UTIL_assembleFileNamesTable2(const char** filenames, size_t tableSize, size_t tableCapacity, char* buf) |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 408 | { |
| 409 | FileNamesTable* const table = (FileNamesTable*) malloc(sizeof(*table)); |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 410 | CONTROL(table != NULL); |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 411 | table->fileNames = filenames; |
| 412 | table->buf = buf; |
| 413 | table->tableSize = tableSize; |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 414 | table->tableCapacity = tableCapacity; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 415 | return table; |
Ahmed Abdellah | cddb05e | 2019-10-24 14:42:37 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 418 | FileNamesTable* |
| 419 | UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf) |
| 420 | { |
| 421 | return UTIL_assembleFileNamesTable2(filenames, tableSize, tableSize, buf); |
| 422 | } |
| 423 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 424 | void UTIL_freeFileNamesTable(FileNamesTable* table) |
| 425 | { |
| 426 | if (table==NULL) return; |
| 427 | free((void*)table->fileNames); |
| 428 | free(table->buf); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 429 | free(table); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 430 | } |
| 431 | |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 432 | FileNamesTable* 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 Collet | 9a3de0a | 2019-11-25 15:34:55 -0800 | [diff] [blame] | 437 | fnt = UTIL_assembleFileNamesTable(fnTable, tableSize, NULL); |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 438 | fnt->tableSize = 0; /* the table is empty */ |
| 439 | return fnt; |
| 440 | } |
| 441 | |
| 442 | void UTIL_refFilename(FileNamesTable* fnt, const char* filename) |
| 443 | { |
Yann Collet | f622c0a | 2019-11-26 14:48:23 -0800 | [diff] [blame] | 444 | assert(fnt->tableSize < fnt->tableCapacity); |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 445 | fnt->fileNames[fnt->tableSize] = filename; |
| 446 | fnt->tableSize++; |
| 447 | } |
| 448 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 449 | static 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 Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | FileNamesTable* |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 459 | UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2) |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 460 | { |
| 461 | unsigned newTableIdx = 0; |
| 462 | size_t pos = 0; |
| 463 | size_t newTotalTableSize; |
| 464 | char* buf; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 465 | |
Yann Collet | 9a3de0a | 2019-11-25 15:34:55 -0800 | [diff] [blame] | 466 | FileNamesTable* const newTable = UTIL_assembleFileNamesTable(NULL, 0, NULL); |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 467 | CONTROL( newTable != NULL ); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 468 | |
Ahmed Abdellah | aefa18e | 2019-10-24 10:12:51 +0100 | [diff] [blame] | 469 | newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 470 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 471 | buf = (char*) calloc(newTotalTableSize, sizeof(*buf)); |
| 472 | CONTROL ( buf != NULL ); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 473 | |
Ahmed Abdellah | aefa18e | 2019-10-24 10:12:51 +0100 | [diff] [blame] | 474 | newTable->buf = buf; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 475 | 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 Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 497 | newTable->tableSize = newTableIdx; |
Ahmed Abdellah | aefa18e | 2019-10-24 10:12:51 +0100 | [diff] [blame] | 498 | |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 499 | UTIL_freeFileNamesTable(table1); |
| 500 | UTIL_freeFileNamesTable(table2); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 501 | |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 502 | return newTable; |
| 503 | } |
| 504 | |
Rohit Jain | c7251e5 | 2018-10-11 18:05:15 -0700 | [diff] [blame] | 505 | #ifdef _WIN32 |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 506 | static int UTIL_prepareFileList(const char* dirName, |
| 507 | char** bufStart, size_t* pos, |
| 508 | char** bufEnd, int followLinks) |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 509 | { |
| 510 | char* path; |
Yann Collet | 74d872e | 2019-10-25 18:26:30 -0700 | [diff] [blame] | 511 | size_t dirLength, pathLength; |
| 512 | int nbFiles = 0; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 513 | WIN32_FIND_DATAA cFile; |
| 514 | HANDLE hFile; |
| 515 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 516 | dirLength = strlen(dirName); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 517 | 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 Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 533 | size_t const fnameLength = strlen(cFile.cFileName); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 534 | 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 Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 542 | 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 Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 547 | if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 548 | } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) |
| 549 | || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) |
| 550 | || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) { |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 551 | if (*bufStart + *pos + pathLength >= *bufEnd) { |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 552 | ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 553 | *bufStart = (char*)UTIL_realloc(*bufStart, newListSize); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 554 | if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 555 | *bufEnd = *bufStart + newListSize; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 556 | } |
| 557 | if (*bufStart + *pos + pathLength < *bufEnd) { |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 558 | memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 559 | *pos += pathLength + 1; |
| 560 | nbFiles++; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 561 | } } |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 562 | 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 Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 571 | static int UTIL_prepareFileList(const char *dirName, |
| 572 | char** bufStart, size_t* pos, |
| 573 | char** bufEnd, int followLinks) |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 574 | { |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 575 | DIR* dir; |
| 576 | struct dirent * entry; |
| 577 | size_t dirLength; |
Yann Collet | a71256a | 2019-10-17 11:01:20 -0700 | [diff] [blame] | 578 | int nbFiles = 0; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 579 | |
| 580 | if (!(dir = opendir(dirName))) { |
| 581 | UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno)); |
| 582 | return 0; |
| 583 | } |
| 584 | |
Yann Collet | a71256a | 2019-10-17 11:01:20 -0700 | [diff] [blame] | 585 | dirLength = strlen(dirName); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 586 | errno = 0; |
| 587 | while ((entry = readdir(dir)) != NULL) { |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 588 | char* path; |
| 589 | size_t fnameLength, pathLength; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 590 | if (strcmp (entry->d_name, "..") == 0 || |
| 591 | strcmp (entry->d_name, ".") == 0) continue; |
Yann Collet | a71256a | 2019-10-17 11:01:20 -0700 | [diff] [blame] | 592 | fnameLength = strlen(entry->d_name); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 593 | 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); |
LeeYoung624 | 793b94b | 2019-07-25 21:07:57 +0800 | [diff] [blame] | 604 | free(path); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 605 | 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 Collet | a71256a | 2019-10-17 11:01:20 -0700 | [diff] [blame] | 614 | assert(newListSize >= 0); |
| 615 | *bufStart = (char*)UTIL_realloc(*bufStart, (size_t)newListSize); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 616 | *bufEnd = *bufStart + newListSize; |
| 617 | if (*bufStart == NULL) { free(path); closedir(dir); return 0; } |
| 618 | } |
| 619 | if (*bufStart + *pos + pathLength < *bufEnd) { |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 620 | memcpy(*bufStart + *pos, path, pathLength + 1); /* with final \0 */ |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 621 | *pos += pathLength + 1; |
| 622 | nbFiles++; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 623 | } } |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 624 | free(path); |
| 625 | errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */ |
| 626 | } |
| 627 | |
| 628 | if (errno != 0) { |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 629 | UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s \n", dirName, strerror(errno)); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 630 | free(*bufStart); |
| 631 | *bufStart = NULL; |
| 632 | } |
| 633 | closedir(dir); |
| 634 | return nbFiles; |
| 635 | } |
| 636 | |
| 637 | #else |
| 638 | |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 639 | static int UTIL_prepareFileList(const char *dirName, |
| 640 | char** bufStart, size_t* pos, |
| 641 | char** bufEnd, int followLinks) |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 642 | { |
| 643 | (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks; |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 644 | UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE) \n", dirName); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | #endif /* #ifdef _WIN32 */ |
| 649 | |
Shashank Tavildar | 0f2bff2 | 2019-10-28 18:21:47 -0700 | [diff] [blame] | 650 | int UTIL_isCompressedFile(const char *inputName, const char *extensionList[]) |
Shashank Tavildar | 48f8566 | 2019-10-25 15:49:11 -0700 | [diff] [blame] | 651 | { |
Shashank Tavildar | 9ab6a74 | 2019-10-29 12:27:54 -0700 | [diff] [blame] | 652 | const char* ext = UTIL_getFileExtension(inputName); |
Shashank Tavildar | 3c1649f | 2019-10-29 15:59:20 -0700 | [diff] [blame] | 653 | while(*extensionList!=NULL) |
Shashank Tavildar | c506099 | 2019-10-29 12:56:04 -0700 | [diff] [blame] | 654 | { |
Shashank Tavildar | 3c1649f | 2019-10-29 15:59:20 -0700 | [diff] [blame] | 655 | const int isCompressedExtension = strcmp(ext,*extensionList); |
| 656 | if(isCompressedExtension==0) |
| 657 | return 1; |
| 658 | ++extensionList; |
Shashank Tavildar | c506099 | 2019-10-29 12:56:04 -0700 | [diff] [blame] | 659 | } |
Shashank Tavildar | 02433e0 | 2019-10-28 14:54:54 -0700 | [diff] [blame] | 660 | return 0; |
Shashank Tavildar | 48f8566 | 2019-10-25 15:49:11 -0700 | [diff] [blame] | 661 | } |
Shashank Tavildar | 0f2bff2 | 2019-10-28 18:21:47 -0700 | [diff] [blame] | 662 | |
Shashank Tavildar | 9ab6a74 | 2019-10-29 12:27:54 -0700 | [diff] [blame] | 663 | /*Utility function to get file extension from file */ |
| 664 | const 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 Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 671 | static int pathnameHas2Dots(const char *pathname) |
| 672 | { |
| 673 | return NULL != strstr(pathname, ".."); |
| 674 | } |
| 675 | |
| 676 | static int isFileNameValidForMirroredOutput(const char *filename) |
| 677 | { |
| 678 | return !pathnameHas2Dots(filename); |
| 679 | } |
| 680 | |
| 681 | |
| 682 | #define DIR_DEFAULT_MODE 0755 |
| 683 | static mode_t getDirMode(const char *dirName) |
| 684 | { |
| 685 | stat_t st; |
W. Felix Handte | 51ac020 | 2020-08-10 15:28:02 -0400 | [diff] [blame] | 686 | if (!UTIL_stat(dirName, &st)) { |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 687 | UTIL_DISPLAY("zstd: failed to get DIR stats %s: %s\n", dirName, strerror(errno)); |
| 688 | return DIR_DEFAULT_MODE; |
| 689 | } |
W. Felix Handte | 51ac020 | 2020-08-10 15:28:02 -0400 | [diff] [blame] | 690 | if (!UTIL_isDirectoryStat(&st)) { |
| 691 | UTIL_DISPLAY("zstd: expected directory: %s\n", dirName); |
| 692 | return DIR_DEFAULT_MODE; |
| 693 | } |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 694 | return st.st_mode; |
| 695 | } |
| 696 | |
| 697 | static 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 */ |
| 714 | static 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 */ |
| 745 | static 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 */ |
| 754 | static 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 | |
| 762 | static char* |
| 763 | trimLeadingCurrentDir(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 */ |
| 775 | static const char * trimPath(const char *pathname) |
| 776 | { |
| 777 | return trimLeadingRootChar( |
| 778 | trimLeadingCurrentDirConst(pathname)); |
| 779 | } |
| 780 | |
| 781 | static 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 */ |
| 810 | char* 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 | |
| 822 | static int |
| 823 | mirrorSrcDir(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 | |
| 837 | static int |
| 838 | mirrorSrcDirRecursive(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 | |
| 858 | static void |
| 859 | makeMirroredDestDirsWithSameSrcDirMode(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 | |
| 866 | static int |
| 867 | firstIsParentOrSameDirOfSecond(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 | |
| 876 | static 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 | |
| 883 | static void |
| 884 | makeUniqueMirroredDestDirs(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 | |
| 922 | static void |
| 923 | makeMirroredDestDirs(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 | |
| 931 | void 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 Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 955 | |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 956 | FileNamesTable* |
| 957 | UTIL_createExpandedFNT(const char** inputNames, size_t nbIfns, int followLinks) |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 958 | { |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 959 | unsigned nbFiles; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 960 | char* buf = (char*)malloc(LIST_SIZE_INCREASE); |
| 961 | char* bufend = buf + LIST_SIZE_INCREASE; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 962 | |
| 963 | if (!buf) return NULL; |
| 964 | |
Yann Collet | d5b4a7e | 2019-11-26 17:46:57 -0800 | [diff] [blame] | 965 | { size_t ifnNb, pos; |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 966 | 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 Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 973 | if (!buf) return NULL; |
Yann Collet | d5b4a7e | 2019-11-26 17:46:57 -0800 | [diff] [blame] | 974 | bufend = buf + newListSize; |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 975 | } |
| 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 Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 985 | |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 986 | /* note : even if nbFiles==0, function returns a valid, though empty, FileNamesTable* object */ |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 987 | |
Yann Collet | d5b4a7e | 2019-11-26 17:46:57 -0800 | [diff] [blame] | 988 | { size_t ifnNb, pos; |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 989 | 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 Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 991 | if (!fileNamesTable) { free(buf); return NULL; } |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 992 | |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 993 | 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 Collet | 29e46ed | 2019-10-18 14:28:34 -0700 | [diff] [blame] | 997 | } |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 998 | return UTIL_assembleFileNamesTable2(fileNamesTable, nbFiles, fntCapacity, buf); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 999 | } |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Yann Collet | 59a7116 | 2019-04-10 12:37:03 -0700 | [diff] [blame] | 1002 | |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 1003 | void UTIL_expandFNT(FileNamesTable** fnt, int followLinks) |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 1004 | { |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 1005 | FileNamesTable* const newFNT = UTIL_createExpandedFNT((*fnt)->fileNames, (*fnt)->tableSize, followLinks); |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 1006 | CONTROL(newFNT != NULL); |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 1007 | UTIL_freeFileNamesTable(*fnt); |
| 1008 | *fnt = newFNT; |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 1009 | } |
| 1010 | |
Yann Collet | a7e33e3 | 2019-11-06 14:42:13 -0800 | [diff] [blame] | 1011 | FileNamesTable* 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 Collet | 9df49dc | 2019-11-06 15:23:44 -0800 | [diff] [blame] | 1016 | memcpy((void*)newFNTable, filenames, sizeof_FNTable); /* void* : mitigate a Visual compiler bug or limitation */ |
Yann Collet | d5b4a7e | 2019-11-26 17:46:57 -0800 | [diff] [blame] | 1017 | return UTIL_assembleFileNamesTable(newFNTable, nbFilenames, NULL); |
Yann Collet | a7e33e3 | 2019-11-06 14:42:13 -0800 | [diff] [blame] | 1018 | } |
| 1019 | |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 1020 | |
Rohit Jain | a47f6e6 | 2018-10-11 16:51:29 -0700 | [diff] [blame] | 1021 | /*-**************************************** |
Yann Collet | 59a7116 | 2019-04-10 12:37:03 -0700 | [diff] [blame] | 1022 | * count the number of physical cores |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 1023 | ******************************************/ |
Rohit Jain | c7251e5 | 2018-10-11 18:05:15 -0700 | [diff] [blame] | 1024 | |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1025 | #if defined(_WIN32) || defined(WIN32) |
| 1026 | |
| 1027 | #include <windows.h> |
| 1028 | |
| 1029 | typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD); |
| 1030 | |
| 1031 | int 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 Collet | 0492c57 | 2019-10-18 17:08:52 -0700 | [diff] [blame] | 1043 | #if defined(_MSC_VER) |
Yann Collet | f379637 | 2019-10-18 17:05:42 -0700 | [diff] [blame] | 1044 | /* 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 Collet | 1bd6c15 | 2019-10-18 15:45:31 -0700 | [diff] [blame] | 1048 | glpi = (LPFN_GLPI)(void*)GetProcAddress(GetModuleHandle(TEXT("kernel32")), |
| 1049 | "GetLogicalProcessorInformation"); |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1050 | |
| 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 Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 1073 | } } |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1074 | |
| 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 | |
| 1092 | failed: |
| 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 */ |
| 1108 | int 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 */ |
| 1134 | int 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, ':'); |
LeeYoung624 | c5caaf5 | 2019-07-29 17:05:50 +0800 | [diff] [blame] | 1166 | if (sep == NULL || *sep == '\0') { |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1167 | /* 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, ':'); |
LeeYoung624 | c5caaf5 | 2019-07-29 17:05:50 +0800 | [diff] [blame] | 1175 | if (sep == NULL || *sep == '\0') { |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1176 | /* 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 Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 1185 | } } |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1186 | if (siblings && cpu_cores) { |
| 1187 | ratio = siblings / cpu_cores; |
| 1188 | } |
| 1189 | failed: |
| 1190 | fclose(cpuinfo); |
| 1191 | return numPhysicalCores = numPhysicalCores / ratio; |
| 1192 | } |
| 1193 | } |
| 1194 | |
Conrad Meyer | fe82637 | 2019-01-04 11:57:12 -0800 | [diff] [blame] | 1195 | #elif defined(__FreeBSD__) |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1196 | |
Conrad Meyer | fe82637 | 2019-01-04 11:57:12 -0800 | [diff] [blame] | 1197 | #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 */ |
| 1202 | int 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 Reiter | d0dcaf5 | 2020-01-08 00:48:26 +0100 | [diff] [blame] | 1227 | #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__CYGWIN__) |
Conrad Meyer | fe82637 | 2019-01-04 11:57:12 -0800 | [diff] [blame] | 1228 | |
| 1229 | /* Use POSIX sysconf |
| 1230 | * see: man 3 sysconf */ |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1231 | int 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 | |
| 1247 | int UTIL_countPhysicalCores(void) |
| 1248 | { |
| 1249 | /* assume 1 */ |
| 1250 | return 1; |
| 1251 | } |
| 1252 | |
| 1253 | #endif |
| 1254 | |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 1255 | #if defined (__cplusplus) |
| 1256 | } |
| 1257 | #endif |