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 | aab11ce | 2020-08-25 11:25:49 -0400 | [diff] [blame^] | 90 | int UTIL_requireUserConfirmationToProceed(const char* prompt, const char* abortMsg, |
| 91 | const char* acceptableLetters) { |
| 92 | int ch; |
| 93 | UTIL_DISPLAY("%s", prompt); |
| 94 | ch = getchar(); |
| 95 | if (strchr(acceptableLetters, ch) == NULL) { |
| 96 | UTIL_DISPLAY("%s", abortMsg); |
| 97 | return 1; |
| 98 | } |
| 99 | /* flush the rest */ |
| 100 | while ((ch!=EOF) && (ch!='\n')) |
| 101 | ch = getchar(); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 106 | |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 107 | /*-************************************* |
| 108 | * Constants |
| 109 | ***************************************/ |
| 110 | #define LIST_SIZE_INCREASE (8*1024) |
Yann Collet | c71bd45 | 2019-11-26 11:20:26 -0800 | [diff] [blame] | 111 | #define MAX_FILE_OF_FILE_NAMES_SIZE (1<<20)*50 |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 112 | |
| 113 | |
| 114 | /*-************************************* |
| 115 | * Functions |
| 116 | ***************************************/ |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 117 | |
W. Felix Handte | b11bea5 | 2020-08-05 00:09:29 -0400 | [diff] [blame] | 118 | int UTIL_stat(const char* filename, stat_t* statbuf) |
| 119 | { |
| 120 | #if defined(_MSC_VER) |
| 121 | return !_stat64(filename, statbuf); |
| 122 | #elif defined(__MINGW32__) && defined (__MSVCRT__) |
| 123 | return !_stati64(filename, statbuf); |
| 124 | #else |
| 125 | return !stat(filename, statbuf); |
| 126 | #endif |
| 127 | } |
| 128 | |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 129 | int UTIL_isRegularFile(const char* infilename) |
| 130 | { |
| 131 | stat_t statbuf; |
W. Felix Handte | 51ac020 | 2020-08-10 15:28:02 -0400 | [diff] [blame] | 132 | return UTIL_stat(infilename, &statbuf) && UTIL_isRegularFileStat(&statbuf); |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 133 | } |
| 134 | |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 135 | int UTIL_isRegularFileStat(const stat_t* statbuf) |
| 136 | { |
| 137 | #if defined(_MSC_VER) |
| 138 | return (statbuf->st_mode & S_IFREG) != 0; |
| 139 | #else |
| 140 | return S_ISREG(statbuf->st_mode) != 0; |
| 141 | #endif |
| 142 | } |
| 143 | |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 144 | /* like chmod, but avoid changing permission of /dev/null */ |
W. Felix Handte | 0a8aacb | 2020-08-05 12:00:12 -0400 | [diff] [blame] | 145 | 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] | 146 | { |
W. Felix Handte | 0a8aacb | 2020-08-05 12:00:12 -0400 | [diff] [blame] | 147 | stat_t localStatBuf; |
| 148 | if (statbuf == NULL) { |
| 149 | if (!UTIL_stat(filename, &localStatBuf)) return 0; |
| 150 | statbuf = &localStatBuf; |
| 151 | } |
| 152 | if (!UTIL_isRegularFileStat(statbuf)) return 0; /* pretend success, but don't change anything */ |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 153 | return chmod(filename, permissions); |
| 154 | } |
| 155 | |
W. Felix Handte | 1a1003f | 2020-08-05 00:35:21 -0400 | [diff] [blame] | 156 | int UTIL_setFileStat(const char *filename, const stat_t *statbuf) |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 157 | { |
| 158 | int res = 0; |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 159 | |
W. Felix Handte | c144914 | 2020-08-05 12:10:42 -0400 | [diff] [blame] | 160 | stat_t curStatBuf; |
| 161 | if (!UTIL_stat(filename, &curStatBuf) || !UTIL_isRegularFileStat(&curStatBuf)) |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 162 | return -1; |
| 163 | |
W. Felix Handte | e1ec800 | 2019-09-12 16:27:05 -0400 | [diff] [blame] | 164 | /* set access and modification times */ |
W. Felix Handte | 5666835 | 2019-12-04 16:59:16 -0500 | [diff] [blame] | 165 | /* We check that st_mtime is a macro here in order to give us confidence |
| 166 | * that struct stat has a struct timespec st_mtim member. We need this |
| 167 | * check because there are some platforms that claim to be POSIX 2008 |
| 168 | * compliant but which do not have st_mtim... */ |
W. Felix Handte | 5af8cb7 | 2019-12-04 10:25:07 -0500 | [diff] [blame] | 169 | #if (PLATFORM_POSIX_VERSION >= 200809L) && defined(st_mtime) |
| 170 | { |
| 171 | /* (atime, mtime) */ |
| 172 | struct timespec timebuf[2] = { {0, UTIME_NOW} }; |
| 173 | timebuf[1] = statbuf->st_mtim; |
| 174 | res += utimensat(AT_FDCWD, filename, timebuf, 0); |
| 175 | } |
| 176 | #else |
W. Felix Handte | e1ec800 | 2019-09-12 16:27:05 -0400 | [diff] [blame] | 177 | { |
| 178 | struct utimbuf timebuf; |
| 179 | timebuf.actime = time(NULL); |
| 180 | timebuf.modtime = statbuf->st_mtime; |
| 181 | res += utime(filename, &timebuf); |
| 182 | } |
Rosen Penev | 41e9065 | 2019-07-30 17:17:07 -0700 | [diff] [blame] | 183 | #endif |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 184 | |
| 185 | #if !defined(_WIN32) |
| 186 | res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */ |
| 187 | #endif |
| 188 | |
W. Felix Handte | c144914 | 2020-08-05 12:10:42 -0400 | [diff] [blame] | 189 | res += UTIL_chmod(filename, &curStatBuf, statbuf->st_mode & 07777); /* Copy file permissions */ |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 190 | |
| 191 | errno = 0; |
| 192 | return -res; /* number of errors is returned */ |
| 193 | } |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 194 | |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 195 | int UTIL_isDirectory(const char* infilename) |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 196 | { |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 197 | stat_t statbuf; |
W. Felix Handte | 51ac020 | 2020-08-10 15:28:02 -0400 | [diff] [blame] | 198 | return UTIL_stat(infilename, &statbuf) && UTIL_isDirectoryStat(&statbuf); |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 199 | } |
| 200 | |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 201 | int UTIL_isDirectoryStat(const stat_t* statbuf) |
| 202 | { |
| 203 | #if defined(_MSC_VER) |
| 204 | return (statbuf->st_mode & _S_IFDIR) != 0; |
| 205 | #else |
| 206 | return S_ISDIR(statbuf->st_mode) != 0; |
| 207 | #endif |
| 208 | } |
| 209 | |
Sen Huang | f80437c | 2019-10-02 11:08:20 -0400 | [diff] [blame] | 210 | int UTIL_compareStr(const void *p1, const void *p2) { |
| 211 | return strcmp(* (char * const *) p1, * (char * const *) p2); |
| 212 | } |
Sen Huang | a9c807a | 2019-09-06 10:17:04 -0700 | [diff] [blame] | 213 | |
Yann Collet | 0004043 | 2019-10-17 10:56:14 -0700 | [diff] [blame] | 214 | int UTIL_isSameFile(const char* fName1, const char* fName2) |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 215 | { |
Yann Collet | 0004043 | 2019-10-17 10:56:14 -0700 | [diff] [blame] | 216 | assert(fName1 != NULL); assert(fName2 != NULL); |
| 217 | #if defined(_MSC_VER) || defined(_WIN32) |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 218 | /* note : Visual does not support file identification by inode. |
Yann Collet | 0004043 | 2019-10-17 10:56:14 -0700 | [diff] [blame] | 219 | * inode does not work on Windows, even with a posix layer, like msys2. |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 220 | * The following work-around is limited to detecting exact name repetition only, |
| 221 | * aka `filename` is considered different from `subdir/../filename` */ |
Yann Collet | 157479a | 2019-10-17 14:31:42 -0700 | [diff] [blame] | 222 | return !strcmp(fName1, fName2); |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 223 | #else |
Yann Collet | 0004043 | 2019-10-17 10:56:14 -0700 | [diff] [blame] | 224 | { stat_t file1Stat; |
| 225 | stat_t file2Stat; |
W. Felix Handte | 5fbc6ad | 2020-08-05 00:31:48 -0400 | [diff] [blame] | 226 | return UTIL_stat(fName1, &file1Stat) |
| 227 | && UTIL_stat(fName2, &file2Stat) |
Yann Collet | 0004043 | 2019-10-17 10:56:14 -0700 | [diff] [blame] | 228 | && (file1Stat.st_dev == file2Stat.st_dev) |
| 229 | && (file1Stat.st_ino == file2Stat.st_ino); |
| 230 | } |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 231 | #endif |
| 232 | } |
| 233 | |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 234 | /* UTIL_isFIFO : distinguish named pipes */ |
| 235 | int UTIL_isFIFO(const char* infilename) |
Bimba Shrestha | 8a39748 | 2019-10-22 15:23:22 -0700 | [diff] [blame] | 236 | { |
| 237 | /* macro guards, as defined in : https://linux.die.net/man/2/lstat */ |
| 238 | #if PLATFORM_POSIX_VERSION >= 200112L |
| 239 | stat_t statbuf; |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 240 | if (UTIL_stat(infilename, &statbuf) && UTIL_isFIFOStat(&statbuf)) return 1; |
Bimba Shrestha | 8a39748 | 2019-10-22 15:23:22 -0700 | [diff] [blame] | 241 | #endif |
| 242 | (void)infilename; |
| 243 | return 0; |
| 244 | } |
Bimba Shrestha | 8a39748 | 2019-10-22 15:23:22 -0700 | [diff] [blame] | 245 | |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 246 | /* UTIL_isFIFO : distinguish named pipes */ |
| 247 | int UTIL_isFIFOStat(const stat_t* statbuf) |
| 248 | { |
| 249 | /* macro guards, as defined in : https://linux.die.net/man/2/lstat */ |
| 250 | #if PLATFORM_POSIX_VERSION >= 200112L |
| 251 | if (S_ISFIFO(statbuf->st_mode)) return 1; |
| 252 | #endif |
| 253 | (void)statbuf; |
| 254 | return 0; |
| 255 | } |
| 256 | |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 257 | int UTIL_isLink(const char* infilename) |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 258 | { |
| 259 | /* 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] | 260 | #if PLATFORM_POSIX_VERSION >= 200112L |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 261 | stat_t statbuf; |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 262 | int const r = lstat(infilename, &statbuf); |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 263 | if (!r && S_ISLNK(statbuf.st_mode)) return 1; |
| 264 | #endif |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 265 | (void)infilename; |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | U64 UTIL_getFileSize(const char* infilename) |
| 270 | { |
W. Felix Handte | 69cb9e7 | 2020-08-05 00:21:41 -0400 | [diff] [blame] | 271 | stat_t statbuf; |
| 272 | if (!UTIL_stat(infilename, &statbuf)) return UTIL_FILESIZE_UNKNOWN; |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 273 | return UTIL_getFileSizeStat(&statbuf); |
| 274 | } |
| 275 | |
| 276 | U64 UTIL_getFileSizeStat(const stat_t* statbuf) |
| 277 | { |
| 278 | if (!UTIL_isRegularFileStat(statbuf)) return UTIL_FILESIZE_UNKNOWN; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 279 | #if defined(_MSC_VER) |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 280 | if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 281 | #elif defined(__MINGW32__) && defined (__MSVCRT__) |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 282 | if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 283 | #else |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 284 | if (!S_ISREG(statbuf->st_mode)) return UTIL_FILESIZE_UNKNOWN; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 285 | #endif |
W. Felix Handte | 44fa052 | 2020-08-05 01:00:06 -0400 | [diff] [blame] | 286 | return (U64)statbuf->st_size; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | |
Yann Collet | 5fb84ca | 2019-10-25 17:34:29 -0700 | [diff] [blame] | 290 | U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles) |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 291 | { |
| 292 | U64 total = 0; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 293 | unsigned n; |
| 294 | for (n=0; n<nbFiles; n++) { |
| 295 | U64 const size = UTIL_getFileSize(fileNamesTable[n]); |
Yann Collet | 5fb84ca | 2019-10-25 17:34:29 -0700 | [diff] [blame] | 296 | if (size == UTIL_FILESIZE_UNKNOWN) return UTIL_FILESIZE_UNKNOWN; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 297 | total += size; |
| 298 | } |
Yann Collet | 5fb84ca | 2019-10-25 17:34:29 -0700 | [diff] [blame] | 299 | return total; |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 302 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 303 | /* condition : @file must be valid, and not have reached its end. |
Yann Collet | d9c634e | 2019-10-28 15:03:32 -0700 | [diff] [blame] | 304 | * @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] | 305 | * or 0, if there is no new line */ |
| 306 | static size_t readLineFromFile(char* buf, size_t len, FILE* file) |
| 307 | { |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 308 | assert(!feof(file)); |
Nick Terrell | f6d00c0 | 2020-01-13 14:22:46 -0800 | [diff] [blame] | 309 | /* Work around Cygwin problem when len == 1 it returns NULL. */ |
| 310 | if (len <= 1) return 0; |
| 311 | CONTROL( fgets(buf, (int) len, file) ); |
Yann Collet | d9c634e | 2019-10-28 15:03:32 -0700 | [diff] [blame] | 312 | { size_t linelen = strlen(buf); |
| 313 | if (strlen(buf)==0) return 0; |
| 314 | if (buf[linelen-1] == '\n') linelen--; |
| 315 | buf[linelen] = '\0'; |
| 316 | return linelen+1; |
| 317 | } |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 320 | /* Conditions : |
| 321 | * size of @inputFileName file must be < @dstCapacity |
| 322 | * @dst must be initialized |
| 323 | * @return : nb of lines |
| 324 | * or -1 if there's an error |
| 325 | */ |
| 326 | static int |
| 327 | readLinesFromFile(void* dst, size_t dstCapacity, |
| 328 | const char* inputFileName) |
| 329 | { |
| 330 | int nbFiles = 0; |
Yann Collet | 3e5c81e | 2019-10-26 00:01:11 -0700 | [diff] [blame] | 331 | size_t pos = 0; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 332 | char* const buf = (char*)dst; |
| 333 | FILE* const inputFile = fopen(inputFileName, "r"); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 334 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 335 | assert(dst != NULL); |
Ahmed Abdellah | aefa18e | 2019-10-24 10:12:51 +0100 | [diff] [blame] | 336 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 337 | if(!inputFile) { |
| 338 | if (g_utilDisplayLevel >= 1) perror("zstd:util:readLinesFromFile"); |
| 339 | return -1; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 340 | } |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 341 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 342 | while ( !feof(inputFile) ) { |
| 343 | size_t const lineLength = readLineFromFile(buf+pos, dstCapacity-pos, inputFile); |
| 344 | if (lineLength == 0) break; |
| 345 | assert(pos + lineLength < dstCapacity); |
Yann Collet | d9c634e | 2019-10-28 15:03:32 -0700 | [diff] [blame] | 346 | pos += lineLength; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 347 | ++nbFiles; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 348 | } |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 349 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 350 | CONTROL( fclose(inputFile) == 0 ); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 351 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 352 | return nbFiles; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /*Note: buf is not freed in case function successfully created table because filesTable->fileNames[0] = buf*/ |
| 356 | FileNamesTable* |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 357 | UTIL_createFileNamesTable_fromFileName(const char* inputFileName) |
| 358 | { |
| 359 | size_t nbFiles = 0; |
| 360 | char* buf; |
| 361 | size_t bufSize; |
| 362 | size_t pos = 0; |
W. Felix Handte | 7238cca | 2020-08-05 01:08:34 -0400 | [diff] [blame] | 363 | stat_t statbuf; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 364 | |
W. Felix Handte | 7238cca | 2020-08-05 01:08:34 -0400 | [diff] [blame] | 365 | if (!UTIL_stat(inputFileName, &statbuf) || !UTIL_isRegularFileStat(&statbuf)) |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 366 | return NULL; |
Ahmed Abdellah | 47712c9 | 2019-10-24 10:30:05 +0100 | [diff] [blame] | 367 | |
W. Felix Handte | 7238cca | 2020-08-05 01:08:34 -0400 | [diff] [blame] | 368 | { U64 const inputFileSize = UTIL_getFileSizeStat(&statbuf); |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 369 | if(inputFileSize > MAX_FILE_OF_FILE_NAMES_SIZE) |
| 370 | return NULL; |
Yann Collet | 12efa1e | 2019-10-26 00:27:32 -0700 | [diff] [blame] | 371 | 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] | 372 | } |
| 373 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 374 | buf = (char*) malloc(bufSize); |
| 375 | CONTROL( buf != NULL ); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 376 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 377 | { int const ret_nbFiles = readLinesFromFile(buf, bufSize, inputFileName); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 378 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 379 | if (ret_nbFiles <= 0) { |
| 380 | free(buf); |
| 381 | return NULL; |
| 382 | } |
| 383 | nbFiles = (size_t)ret_nbFiles; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 386 | { const char** filenamesTable = (const char**) malloc(nbFiles * sizeof(*filenamesTable)); |
| 387 | CONTROL(filenamesTable != NULL); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 388 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 389 | { size_t fnb; |
| 390 | for (fnb = 0, pos = 0; fnb < nbFiles; fnb++) { |
| 391 | filenamesTable[fnb] = buf+pos; |
| 392 | pos += strlen(buf+pos)+1; /* +1 for the finishing `\0` */ |
| 393 | } } |
| 394 | assert(pos <= bufSize); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 395 | |
Yann Collet | 9a3de0a | 2019-11-25 15:34:55 -0800 | [diff] [blame] | 396 | return UTIL_assembleFileNamesTable(filenamesTable, nbFiles, buf); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 397 | } |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 400 | static FileNamesTable* |
| 401 | 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] | 402 | { |
| 403 | FileNamesTable* const table = (FileNamesTable*) malloc(sizeof(*table)); |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 404 | CONTROL(table != NULL); |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 405 | table->fileNames = filenames; |
| 406 | table->buf = buf; |
| 407 | table->tableSize = tableSize; |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 408 | table->tableCapacity = tableCapacity; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 409 | return table; |
Ahmed Abdellah | cddb05e | 2019-10-24 14:42:37 +0100 | [diff] [blame] | 410 | } |
| 411 | |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 412 | FileNamesTable* |
| 413 | UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf) |
| 414 | { |
| 415 | return UTIL_assembleFileNamesTable2(filenames, tableSize, tableSize, buf); |
| 416 | } |
| 417 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 418 | void UTIL_freeFileNamesTable(FileNamesTable* table) |
| 419 | { |
| 420 | if (table==NULL) return; |
| 421 | free((void*)table->fileNames); |
| 422 | free(table->buf); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 423 | free(table); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 424 | } |
| 425 | |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 426 | FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize) |
| 427 | { |
| 428 | const char** const fnTable = (const char**)malloc(tableSize * sizeof(*fnTable)); |
| 429 | FileNamesTable* fnt; |
| 430 | if (fnTable==NULL) return NULL; |
Yann Collet | 9a3de0a | 2019-11-25 15:34:55 -0800 | [diff] [blame] | 431 | fnt = UTIL_assembleFileNamesTable(fnTable, tableSize, NULL); |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 432 | fnt->tableSize = 0; /* the table is empty */ |
| 433 | return fnt; |
| 434 | } |
| 435 | |
| 436 | void UTIL_refFilename(FileNamesTable* fnt, const char* filename) |
| 437 | { |
Yann Collet | f622c0a | 2019-11-26 14:48:23 -0800 | [diff] [blame] | 438 | assert(fnt->tableSize < fnt->tableCapacity); |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 439 | fnt->fileNames[fnt->tableSize] = filename; |
| 440 | fnt->tableSize++; |
| 441 | } |
| 442 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 443 | static size_t getTotalTableSize(FileNamesTable* table) |
| 444 | { |
| 445 | size_t fnb = 0, totalSize = 0; |
| 446 | for(fnb = 0 ; fnb < table->tableSize && table->fileNames[fnb] ; ++fnb) { |
| 447 | totalSize += strlen(table->fileNames[fnb]) + 1; /* +1 to add '\0' at the end of each fileName */ |
| 448 | } |
| 449 | return totalSize; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | FileNamesTable* |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 453 | UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2) |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 454 | { |
| 455 | unsigned newTableIdx = 0; |
| 456 | size_t pos = 0; |
| 457 | size_t newTotalTableSize; |
| 458 | char* buf; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 459 | |
Yann Collet | 9a3de0a | 2019-11-25 15:34:55 -0800 | [diff] [blame] | 460 | FileNamesTable* const newTable = UTIL_assembleFileNamesTable(NULL, 0, NULL); |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 461 | CONTROL( newTable != NULL ); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 462 | |
Ahmed Abdellah | aefa18e | 2019-10-24 10:12:51 +0100 | [diff] [blame] | 463 | newTotalTableSize = getTotalTableSize(table1) + getTotalTableSize(table2); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 464 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 465 | buf = (char*) calloc(newTotalTableSize, sizeof(*buf)); |
| 466 | CONTROL ( buf != NULL ); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 467 | |
Ahmed Abdellah | aefa18e | 2019-10-24 10:12:51 +0100 | [diff] [blame] | 468 | newTable->buf = buf; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 469 | newTable->tableSize = table1->tableSize + table2->tableSize; |
| 470 | newTable->fileNames = (const char **) calloc(newTable->tableSize, sizeof(*(newTable->fileNames))); |
| 471 | CONTROL ( newTable->fileNames != NULL ); |
| 472 | |
| 473 | { unsigned idx1; |
| 474 | for( idx1=0 ; (idx1 < table1->tableSize) && table1->fileNames[idx1] && (pos < newTotalTableSize); ++idx1, ++newTableIdx) { |
| 475 | size_t const curLen = strlen(table1->fileNames[idx1]); |
| 476 | memcpy(buf+pos, table1->fileNames[idx1], curLen); |
| 477 | assert(newTableIdx <= newTable->tableSize); |
| 478 | newTable->fileNames[newTableIdx] = buf+pos; |
| 479 | pos += curLen+1; |
| 480 | } } |
| 481 | |
| 482 | { unsigned idx2; |
| 483 | for( idx2=0 ; (idx2 < table2->tableSize) && table2->fileNames[idx2] && (pos < newTotalTableSize) ; ++idx2, ++newTableIdx) { |
| 484 | size_t const curLen = strlen(table2->fileNames[idx2]); |
| 485 | memcpy(buf+pos, table2->fileNames[idx2], curLen); |
| 486 | assert(newTableIdx <= newTable->tableSize); |
| 487 | newTable->fileNames[newTableIdx] = buf+pos; |
| 488 | pos += curLen+1; |
| 489 | } } |
| 490 | assert(pos <= newTotalTableSize); |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 491 | newTable->tableSize = newTableIdx; |
Ahmed Abdellah | aefa18e | 2019-10-24 10:12:51 +0100 | [diff] [blame] | 492 | |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 493 | UTIL_freeFileNamesTable(table1); |
| 494 | UTIL_freeFileNamesTable(table2); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 495 | |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 496 | return newTable; |
| 497 | } |
| 498 | |
Rohit Jain | c7251e5 | 2018-10-11 18:05:15 -0700 | [diff] [blame] | 499 | #ifdef _WIN32 |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 500 | static int UTIL_prepareFileList(const char* dirName, |
| 501 | char** bufStart, size_t* pos, |
| 502 | char** bufEnd, int followLinks) |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 503 | { |
| 504 | char* path; |
Yann Collet | 74d872e | 2019-10-25 18:26:30 -0700 | [diff] [blame] | 505 | size_t dirLength, pathLength; |
| 506 | int nbFiles = 0; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 507 | WIN32_FIND_DATAA cFile; |
| 508 | HANDLE hFile; |
| 509 | |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 510 | dirLength = strlen(dirName); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 511 | path = (char*) malloc(dirLength + 3); |
| 512 | if (!path) return 0; |
| 513 | |
| 514 | memcpy(path, dirName, dirLength); |
| 515 | path[dirLength] = '\\'; |
| 516 | path[dirLength+1] = '*'; |
| 517 | path[dirLength+2] = 0; |
| 518 | |
| 519 | hFile=FindFirstFileA(path, &cFile); |
| 520 | if (hFile == INVALID_HANDLE_VALUE) { |
| 521 | UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s'\n", dirName); |
| 522 | return 0; |
| 523 | } |
| 524 | free(path); |
| 525 | |
| 526 | do { |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 527 | size_t const fnameLength = strlen(cFile.cFileName); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 528 | path = (char*) malloc(dirLength + fnameLength + 2); |
| 529 | if (!path) { FindClose(hFile); return 0; } |
| 530 | memcpy(path, dirName, dirLength); |
| 531 | path[dirLength] = '\\'; |
| 532 | memcpy(path+dirLength+1, cFile.cFileName, fnameLength); |
| 533 | pathLength = dirLength+1+fnameLength; |
| 534 | path[pathLength] = 0; |
| 535 | if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 536 | if ( strcmp (cFile.cFileName, "..") == 0 |
| 537 | || strcmp (cFile.cFileName, ".") == 0 ) |
| 538 | continue; |
| 539 | /* Recursively call "UTIL_prepareFileList" with the new path. */ |
| 540 | nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 541 | if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 542 | } else if ( (cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) |
| 543 | || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) |
| 544 | || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) ) { |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 545 | if (*bufStart + *pos + pathLength >= *bufEnd) { |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 546 | ptrdiff_t const newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 547 | *bufStart = (char*)UTIL_realloc(*bufStart, newListSize); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 548 | if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 549 | *bufEnd = *bufStart + newListSize; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 550 | } |
| 551 | if (*bufStart + *pos + pathLength < *bufEnd) { |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 552 | memcpy(*bufStart + *pos, path, pathLength+1 /* include final \0 */); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 553 | *pos += pathLength + 1; |
| 554 | nbFiles++; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 555 | } } |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 556 | free(path); |
| 557 | } while (FindNextFileA(hFile, &cFile)); |
| 558 | |
| 559 | FindClose(hFile); |
| 560 | return nbFiles; |
| 561 | } |
| 562 | |
| 563 | #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ |
| 564 | |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 565 | static int UTIL_prepareFileList(const char *dirName, |
| 566 | char** bufStart, size_t* pos, |
| 567 | char** bufEnd, int followLinks) |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 568 | { |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 569 | DIR* dir; |
| 570 | struct dirent * entry; |
| 571 | size_t dirLength; |
Yann Collet | a71256a | 2019-10-17 11:01:20 -0700 | [diff] [blame] | 572 | int nbFiles = 0; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 573 | |
| 574 | if (!(dir = opendir(dirName))) { |
| 575 | UTIL_DISPLAYLEVEL(1, "Cannot open directory '%s': %s\n", dirName, strerror(errno)); |
| 576 | return 0; |
| 577 | } |
| 578 | |
Yann Collet | a71256a | 2019-10-17 11:01:20 -0700 | [diff] [blame] | 579 | dirLength = strlen(dirName); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 580 | errno = 0; |
| 581 | while ((entry = readdir(dir)) != NULL) { |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 582 | char* path; |
| 583 | size_t fnameLength, pathLength; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 584 | if (strcmp (entry->d_name, "..") == 0 || |
| 585 | strcmp (entry->d_name, ".") == 0) continue; |
Yann Collet | a71256a | 2019-10-17 11:01:20 -0700 | [diff] [blame] | 586 | fnameLength = strlen(entry->d_name); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 587 | path = (char*) malloc(dirLength + fnameLength + 2); |
| 588 | if (!path) { closedir(dir); return 0; } |
| 589 | memcpy(path, dirName, dirLength); |
| 590 | |
| 591 | path[dirLength] = '/'; |
| 592 | memcpy(path+dirLength+1, entry->d_name, fnameLength); |
| 593 | pathLength = dirLength+1+fnameLength; |
| 594 | path[pathLength] = 0; |
| 595 | |
| 596 | if (!followLinks && UTIL_isLink(path)) { |
| 597 | UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path); |
LeeYoung624 | 793b94b | 2019-07-25 21:07:57 +0800 | [diff] [blame] | 598 | free(path); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 599 | continue; |
| 600 | } |
| 601 | |
| 602 | if (UTIL_isDirectory(path)) { |
| 603 | nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */ |
| 604 | if (*bufStart == NULL) { free(path); closedir(dir); return 0; } |
| 605 | } else { |
| 606 | if (*bufStart + *pos + pathLength >= *bufEnd) { |
| 607 | ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; |
Yann Collet | a71256a | 2019-10-17 11:01:20 -0700 | [diff] [blame] | 608 | assert(newListSize >= 0); |
| 609 | *bufStart = (char*)UTIL_realloc(*bufStart, (size_t)newListSize); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 610 | *bufEnd = *bufStart + newListSize; |
| 611 | if (*bufStart == NULL) { free(path); closedir(dir); return 0; } |
| 612 | } |
| 613 | if (*bufStart + *pos + pathLength < *bufEnd) { |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 614 | memcpy(*bufStart + *pos, path, pathLength + 1); /* with final \0 */ |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 615 | *pos += pathLength + 1; |
| 616 | nbFiles++; |
Yann Collet | 1ead0c5 | 2019-10-25 16:36:59 -0700 | [diff] [blame] | 617 | } } |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 618 | free(path); |
| 619 | errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */ |
| 620 | } |
| 621 | |
| 622 | if (errno != 0) { |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 623 | UTIL_DISPLAYLEVEL(1, "readdir(%s) error: %s \n", dirName, strerror(errno)); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 624 | free(*bufStart); |
| 625 | *bufStart = NULL; |
| 626 | } |
| 627 | closedir(dir); |
| 628 | return nbFiles; |
| 629 | } |
| 630 | |
| 631 | #else |
| 632 | |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 633 | static int UTIL_prepareFileList(const char *dirName, |
| 634 | char** bufStart, size_t* pos, |
| 635 | char** bufEnd, int followLinks) |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 636 | { |
| 637 | (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks; |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 638 | 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] | 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | #endif /* #ifdef _WIN32 */ |
| 643 | |
Shashank Tavildar | 0f2bff2 | 2019-10-28 18:21:47 -0700 | [diff] [blame] | 644 | int UTIL_isCompressedFile(const char *inputName, const char *extensionList[]) |
Shashank Tavildar | 48f8566 | 2019-10-25 15:49:11 -0700 | [diff] [blame] | 645 | { |
Shashank Tavildar | 9ab6a74 | 2019-10-29 12:27:54 -0700 | [diff] [blame] | 646 | const char* ext = UTIL_getFileExtension(inputName); |
Shashank Tavildar | 3c1649f | 2019-10-29 15:59:20 -0700 | [diff] [blame] | 647 | while(*extensionList!=NULL) |
Shashank Tavildar | c506099 | 2019-10-29 12:56:04 -0700 | [diff] [blame] | 648 | { |
Shashank Tavildar | 3c1649f | 2019-10-29 15:59:20 -0700 | [diff] [blame] | 649 | const int isCompressedExtension = strcmp(ext,*extensionList); |
| 650 | if(isCompressedExtension==0) |
| 651 | return 1; |
| 652 | ++extensionList; |
Shashank Tavildar | c506099 | 2019-10-29 12:56:04 -0700 | [diff] [blame] | 653 | } |
Shashank Tavildar | 02433e0 | 2019-10-28 14:54:54 -0700 | [diff] [blame] | 654 | return 0; |
Shashank Tavildar | 48f8566 | 2019-10-25 15:49:11 -0700 | [diff] [blame] | 655 | } |
Shashank Tavildar | 0f2bff2 | 2019-10-28 18:21:47 -0700 | [diff] [blame] | 656 | |
Shashank Tavildar | 9ab6a74 | 2019-10-29 12:27:54 -0700 | [diff] [blame] | 657 | /*Utility function to get file extension from file */ |
| 658 | const char* UTIL_getFileExtension(const char* infilename) |
| 659 | { |
| 660 | const char* extension = strrchr(infilename, '.'); |
| 661 | if(!extension || extension==infilename) return ""; |
| 662 | return extension; |
| 663 | } |
| 664 | |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 665 | static int pathnameHas2Dots(const char *pathname) |
| 666 | { |
| 667 | return NULL != strstr(pathname, ".."); |
| 668 | } |
| 669 | |
| 670 | static int isFileNameValidForMirroredOutput(const char *filename) |
| 671 | { |
| 672 | return !pathnameHas2Dots(filename); |
| 673 | } |
| 674 | |
| 675 | |
| 676 | #define DIR_DEFAULT_MODE 0755 |
| 677 | static mode_t getDirMode(const char *dirName) |
| 678 | { |
| 679 | stat_t st; |
W. Felix Handte | 51ac020 | 2020-08-10 15:28:02 -0400 | [diff] [blame] | 680 | if (!UTIL_stat(dirName, &st)) { |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 681 | UTIL_DISPLAY("zstd: failed to get DIR stats %s: %s\n", dirName, strerror(errno)); |
| 682 | return DIR_DEFAULT_MODE; |
| 683 | } |
W. Felix Handte | 51ac020 | 2020-08-10 15:28:02 -0400 | [diff] [blame] | 684 | if (!UTIL_isDirectoryStat(&st)) { |
| 685 | UTIL_DISPLAY("zstd: expected directory: %s\n", dirName); |
| 686 | return DIR_DEFAULT_MODE; |
| 687 | } |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 688 | return st.st_mode; |
| 689 | } |
| 690 | |
| 691 | static int makeDir(const char *dir, mode_t mode) |
| 692 | { |
| 693 | #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__) |
| 694 | int ret = _mkdir(dir); |
| 695 | (void) mode; |
| 696 | #else |
| 697 | int ret = mkdir(dir, mode); |
| 698 | #endif |
| 699 | if (ret != 0) { |
| 700 | if (errno == EEXIST) |
| 701 | return 0; |
| 702 | UTIL_DISPLAY("zstd: failed to create DIR %s: %s\n", dir, strerror(errno)); |
| 703 | } |
| 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | /* this function requires a mutable input string */ |
| 708 | static void convertPathnameToDirName(char *pathname) |
| 709 | { |
| 710 | size_t len = 0; |
| 711 | char* pos = NULL; |
| 712 | /* get dir name from pathname similar to 'dirname()' */ |
| 713 | assert(pathname != NULL); |
| 714 | |
| 715 | /* remove trailing '/' chars */ |
| 716 | len = strlen(pathname); |
| 717 | assert(len > 0); |
| 718 | while (pathname[len] == PATH_SEP) { |
| 719 | pathname[len] = '\0'; |
| 720 | len--; |
| 721 | } |
| 722 | if (len == 0) return; |
| 723 | |
| 724 | /* if input is a single file, return '.' instead. i.e. |
| 725 | * "xyz/abc/file.txt" => "xyz/abc" |
| 726 | "./file.txt" => "." |
| 727 | "file.txt" => "." |
| 728 | */ |
| 729 | pos = strrchr(pathname, PATH_SEP); |
| 730 | if (pos == NULL) { |
| 731 | pathname[0] = '.'; |
| 732 | pathname[1] = '\0'; |
| 733 | } else { |
| 734 | *pos = '\0'; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | /* pathname must be valid */ |
| 739 | static const char* trimLeadingRootChar(const char *pathname) |
| 740 | { |
| 741 | assert(pathname != NULL); |
| 742 | if (pathname[0] == PATH_SEP) |
| 743 | return pathname + 1; |
| 744 | return pathname; |
| 745 | } |
| 746 | |
| 747 | /* pathname must be valid */ |
| 748 | static const char* trimLeadingCurrentDirConst(const char *pathname) |
| 749 | { |
| 750 | assert(pathname != NULL); |
| 751 | if ((pathname[0] == '.') && (pathname[1] == PATH_SEP)) |
| 752 | return pathname + 2; |
| 753 | return pathname; |
| 754 | } |
| 755 | |
| 756 | static char* |
| 757 | trimLeadingCurrentDir(char *pathname) |
| 758 | { |
| 759 | /* 'union charunion' can do const-cast without compiler warning */ |
| 760 | union charunion { |
| 761 | char *chr; |
| 762 | const char* cchr; |
| 763 | } ptr; |
| 764 | ptr.cchr = trimLeadingCurrentDirConst(pathname); |
| 765 | return ptr.chr; |
| 766 | } |
| 767 | |
| 768 | /* remove leading './' or '/' chars here */ |
| 769 | static const char * trimPath(const char *pathname) |
| 770 | { |
| 771 | return trimLeadingRootChar( |
| 772 | trimLeadingCurrentDirConst(pathname)); |
| 773 | } |
| 774 | |
| 775 | static char* mallocAndJoin2Dir(const char *dir1, const char *dir2) |
| 776 | { |
| 777 | const size_t dir1Size = strlen(dir1); |
| 778 | const size_t dir2Size = strlen(dir2); |
| 779 | char *outDirBuffer, *buffer, trailingChar; |
| 780 | |
| 781 | assert(dir1 != NULL && dir2 != NULL); |
| 782 | outDirBuffer = (char *) malloc(dir1Size + dir2Size + 2); |
| 783 | CONTROL(outDirBuffer != NULL); |
| 784 | |
| 785 | strncpy(outDirBuffer, dir1, dir1Size); |
| 786 | outDirBuffer[dir1Size] = '\0'; |
| 787 | |
| 788 | if (dir2[0] == '.') |
| 789 | return outDirBuffer; |
| 790 | |
| 791 | buffer = outDirBuffer + dir1Size; |
| 792 | trailingChar = *(buffer - 1); |
| 793 | if (trailingChar != PATH_SEP) { |
| 794 | *buffer = PATH_SEP; |
| 795 | buffer++; |
| 796 | } |
| 797 | strncpy(buffer, dir2, dir2Size); |
| 798 | buffer[dir2Size] = '\0'; |
| 799 | |
| 800 | return outDirBuffer; |
| 801 | } |
| 802 | |
| 803 | /* this function will return NULL if input srcFileName is not valid name for mirrored output path */ |
| 804 | char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName) |
| 805 | { |
| 806 | char* pathname = NULL; |
| 807 | if (!isFileNameValidForMirroredOutput(srcFileName)) |
| 808 | return NULL; |
| 809 | |
| 810 | pathname = mallocAndJoin2Dir(outDirRootName, trimPath(srcFileName)); |
| 811 | |
| 812 | convertPathnameToDirName(pathname); |
| 813 | return pathname; |
| 814 | } |
| 815 | |
| 816 | static int |
| 817 | mirrorSrcDir(char* srcDirName, const char* outDirName) |
| 818 | { |
| 819 | mode_t srcMode; |
| 820 | int status = 0; |
| 821 | char* newDir = mallocAndJoin2Dir(outDirName, trimPath(srcDirName)); |
| 822 | if (!newDir) |
| 823 | return -ENOMEM; |
| 824 | |
| 825 | srcMode = getDirMode(srcDirName); |
| 826 | status = makeDir(newDir, srcMode); |
| 827 | free(newDir); |
| 828 | return status; |
| 829 | } |
| 830 | |
| 831 | static int |
| 832 | mirrorSrcDirRecursive(char* srcDirName, const char* outDirName) |
| 833 | { |
| 834 | int status = 0; |
| 835 | char* pp = trimLeadingCurrentDir(srcDirName); |
| 836 | char* sp = NULL; |
| 837 | |
| 838 | while ((sp = strchr(pp, PATH_SEP)) != NULL) { |
| 839 | if (sp != pp) { |
| 840 | *sp = '\0'; |
| 841 | status = mirrorSrcDir(srcDirName, outDirName); |
| 842 | if (status != 0) |
| 843 | return status; |
| 844 | *sp = PATH_SEP; |
| 845 | } |
| 846 | pp = sp + 1; |
| 847 | } |
| 848 | status = mirrorSrcDir(srcDirName, outDirName); |
| 849 | return status; |
| 850 | } |
| 851 | |
| 852 | static void |
| 853 | makeMirroredDestDirsWithSameSrcDirMode(char** srcDirNames, unsigned nbFile, const char* outDirName) |
| 854 | { |
| 855 | unsigned int i = 0; |
| 856 | for (i = 0; i < nbFile; i++) |
| 857 | mirrorSrcDirRecursive(srcDirNames[i], outDirName); |
| 858 | } |
| 859 | |
| 860 | static int |
| 861 | firstIsParentOrSameDirOfSecond(const char* firstDir, const char* secondDir) |
| 862 | { |
| 863 | size_t firstDirLen = strlen(firstDir), |
| 864 | secondDirLen = strlen(secondDir); |
| 865 | return firstDirLen <= secondDirLen && |
| 866 | (secondDir[firstDirLen] == PATH_SEP || secondDir[firstDirLen] == '\0') && |
| 867 | 0 == strncmp(firstDir, secondDir, firstDirLen); |
| 868 | } |
| 869 | |
| 870 | static int compareDir(const void* pathname1, const void* pathname2) { |
| 871 | /* sort it after remove the leading '/' or './'*/ |
| 872 | const char* s1 = trimPath(*(char * const *) pathname1); |
| 873 | const char* s2 = trimPath(*(char * const *) pathname2); |
| 874 | return strcmp(s1, s2); |
| 875 | } |
| 876 | |
| 877 | static void |
| 878 | makeUniqueMirroredDestDirs(char** srcDirNames, unsigned nbFile, const char* outDirName) |
| 879 | { |
| 880 | unsigned int i = 0, uniqueDirNr = 0; |
| 881 | char** uniqueDirNames = NULL; |
| 882 | |
| 883 | if (nbFile == 0) |
| 884 | return; |
| 885 | |
| 886 | uniqueDirNames = (char** ) malloc(nbFile * sizeof (char *)); |
| 887 | CONTROL(uniqueDirNames != NULL); |
| 888 | |
| 889 | /* if dirs is "a/b/c" and "a/b/c/d", we only need call: |
| 890 | * we just need "a/b/c/d" */ |
| 891 | qsort((void *)srcDirNames, nbFile, sizeof(char*), compareDir); |
| 892 | |
| 893 | uniqueDirNr = 1; |
| 894 | uniqueDirNames[uniqueDirNr - 1] = srcDirNames[0]; |
| 895 | for (i = 1; i < nbFile; i++) { |
| 896 | char* prevDirName = srcDirNames[i - 1]; |
| 897 | char* currDirName = srcDirNames[i]; |
| 898 | |
| 899 | /* note: we alwasy compare trimmed path, i.e.: |
| 900 | * src dir of "./foo" and "/foo" will be both saved into: |
| 901 | * "outDirName/foo/" */ |
| 902 | if (!firstIsParentOrSameDirOfSecond(trimPath(prevDirName), |
| 903 | trimPath(currDirName))) |
| 904 | uniqueDirNr++; |
| 905 | |
| 906 | /* we need maintain original src dir name instead of trimmed |
| 907 | * dir, so we can retrive the original src dir's mode_t */ |
| 908 | uniqueDirNames[uniqueDirNr - 1] = currDirName; |
| 909 | } |
| 910 | |
| 911 | makeMirroredDestDirsWithSameSrcDirMode(uniqueDirNames, uniqueDirNr, outDirName); |
| 912 | |
| 913 | free(uniqueDirNames); |
| 914 | } |
| 915 | |
| 916 | static void |
| 917 | makeMirroredDestDirs(char** srcFileNames, unsigned nbFile, const char* outDirName) |
| 918 | { |
| 919 | unsigned int i = 0; |
| 920 | for (i = 0; i < nbFile; ++i) |
| 921 | convertPathnameToDirName(srcFileNames[i]); |
| 922 | makeUniqueMirroredDestDirs(srcFileNames, nbFile, outDirName); |
| 923 | } |
| 924 | |
| 925 | void UTIL_mirrorSourceFilesDirectories(const char** inFileNames, unsigned int nbFile, const char* outDirName) |
| 926 | { |
| 927 | unsigned int i = 0, validFilenamesNr = 0; |
| 928 | char** srcFileNames = (char **) malloc(nbFile * sizeof (char *)); |
| 929 | CONTROL(srcFileNames != NULL); |
| 930 | |
| 931 | /* check input filenames is valid */ |
| 932 | for (i = 0; i < nbFile; ++i) { |
| 933 | if (isFileNameValidForMirroredOutput(inFileNames[i])) { |
| 934 | char* fname = STRDUP(inFileNames[i]); |
| 935 | CONTROL(fname != NULL); |
| 936 | srcFileNames[validFilenamesNr++] = fname; |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | if (validFilenamesNr > 0) { |
| 941 | makeDir(outDirName, DIR_DEFAULT_MODE); |
| 942 | makeMirroredDestDirs(srcFileNames, validFilenamesNr, outDirName); |
| 943 | } |
| 944 | |
| 945 | for (i = 0; i < validFilenamesNr; i++) |
| 946 | free(srcFileNames[i]); |
| 947 | free(srcFileNames); |
| 948 | } |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 949 | |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 950 | FileNamesTable* |
| 951 | UTIL_createExpandedFNT(const char** inputNames, size_t nbIfns, int followLinks) |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 952 | { |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 953 | unsigned nbFiles; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 954 | char* buf = (char*)malloc(LIST_SIZE_INCREASE); |
| 955 | char* bufend = buf + LIST_SIZE_INCREASE; |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 956 | |
| 957 | if (!buf) return NULL; |
| 958 | |
Yann Collet | d5b4a7e | 2019-11-26 17:46:57 -0800 | [diff] [blame] | 959 | { size_t ifnNb, pos; |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 960 | for (ifnNb=0, pos=0, nbFiles=0; ifnNb<nbIfns; ifnNb++) { |
| 961 | if (!UTIL_isDirectory(inputNames[ifnNb])) { |
| 962 | size_t const len = strlen(inputNames[ifnNb]); |
| 963 | if (buf + pos + len >= bufend) { |
| 964 | ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE; |
| 965 | assert(newListSize >= 0); |
| 966 | buf = (char*)UTIL_realloc(buf, (size_t)newListSize); |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 967 | if (!buf) return NULL; |
Yann Collet | d5b4a7e | 2019-11-26 17:46:57 -0800 | [diff] [blame] | 968 | bufend = buf + newListSize; |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 969 | } |
| 970 | if (buf + pos + len < bufend) { |
| 971 | memcpy(buf+pos, inputNames[ifnNb], len+1); /* including final \0 */ |
| 972 | pos += len + 1; |
| 973 | nbFiles++; |
| 974 | } |
| 975 | } else { |
| 976 | nbFiles += (unsigned)UTIL_prepareFileList(inputNames[ifnNb], &buf, &pos, &bufend, followLinks); |
| 977 | if (buf == NULL) return NULL; |
| 978 | } } } |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 979 | |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 980 | /* 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] | 981 | |
Yann Collet | d5b4a7e | 2019-11-26 17:46:57 -0800 | [diff] [blame] | 982 | { size_t ifnNb, pos; |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 983 | size_t const fntCapacity = nbFiles + 1; /* minimum 1, allows adding one reference, typically stdin */ |
| 984 | const char** const fileNamesTable = (const char**)malloc(fntCapacity * sizeof(*fileNamesTable)); |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 985 | if (!fileNamesTable) { free(buf); return NULL; } |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 986 | |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 987 | for (ifnNb = 0, pos = 0; ifnNb < nbFiles; ifnNb++) { |
| 988 | fileNamesTable[ifnNb] = buf + pos; |
| 989 | if (buf + pos > bufend) { free(buf); free((void*)fileNamesTable); return NULL; } |
| 990 | pos += strlen(fileNamesTable[ifnNb]) + 1; |
Yann Collet | 29e46ed | 2019-10-18 14:28:34 -0700 | [diff] [blame] | 991 | } |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 992 | return UTIL_assembleFileNamesTable2(fileNamesTable, nbFiles, fntCapacity, buf); |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 993 | } |
Rohit Jain | 705e0b1 | 2018-10-11 15:51:57 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Yann Collet | 59a7116 | 2019-04-10 12:37:03 -0700 | [diff] [blame] | 996 | |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 997 | void UTIL_expandFNT(FileNamesTable** fnt, int followLinks) |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 998 | { |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 999 | FileNamesTable* const newFNT = UTIL_createExpandedFNT((*fnt)->fileNames, (*fnt)->tableSize, followLinks); |
Yann Collet | a49417b | 2019-12-02 14:28:18 -0800 | [diff] [blame] | 1000 | CONTROL(newFNT != NULL); |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 1001 | UTIL_freeFileNamesTable(*fnt); |
| 1002 | *fnt = newFNT; |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 1003 | } |
| 1004 | |
Yann Collet | a7e33e3 | 2019-11-06 14:42:13 -0800 | [diff] [blame] | 1005 | FileNamesTable* UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames) |
| 1006 | { |
| 1007 | size_t const sizeof_FNTable = nbFilenames * sizeof(*filenames); |
| 1008 | const char** const newFNTable = (const char**)malloc(sizeof_FNTable); |
| 1009 | if (newFNTable==NULL) return NULL; |
Yann Collet | 9df49dc | 2019-11-06 15:23:44 -0800 | [diff] [blame] | 1010 | 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] | 1011 | return UTIL_assembleFileNamesTable(newFNTable, nbFilenames, NULL); |
Yann Collet | a7e33e3 | 2019-11-06 14:42:13 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
Yann Collet | 72dbf1b | 2018-12-20 12:27:12 -0800 | [diff] [blame] | 1014 | |
Rohit Jain | a47f6e6 | 2018-10-11 16:51:29 -0700 | [diff] [blame] | 1015 | /*-**************************************** |
Yann Collet | 59a7116 | 2019-04-10 12:37:03 -0700 | [diff] [blame] | 1016 | * count the number of physical cores |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 1017 | ******************************************/ |
Rohit Jain | c7251e5 | 2018-10-11 18:05:15 -0700 | [diff] [blame] | 1018 | |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1019 | #if defined(_WIN32) || defined(WIN32) |
| 1020 | |
| 1021 | #include <windows.h> |
| 1022 | |
| 1023 | typedef BOOL(WINAPI* LPFN_GLPI)(PSYSTEM_LOGICAL_PROCESSOR_INFORMATION, PDWORD); |
| 1024 | |
| 1025 | int UTIL_countPhysicalCores(void) |
| 1026 | { |
| 1027 | static int numPhysicalCores = 0; |
| 1028 | if (numPhysicalCores != 0) return numPhysicalCores; |
| 1029 | |
| 1030 | { LPFN_GLPI glpi; |
| 1031 | BOOL done = FALSE; |
| 1032 | PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; |
| 1033 | PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr = NULL; |
| 1034 | DWORD returnLength = 0; |
| 1035 | size_t byteOffset = 0; |
| 1036 | |
Yann Collet | 0492c57 | 2019-10-18 17:08:52 -0700 | [diff] [blame] | 1037 | #if defined(_MSC_VER) |
Yann Collet | f379637 | 2019-10-18 17:05:42 -0700 | [diff] [blame] | 1038 | /* Visual Studio does not like the following cast */ |
| 1039 | # pragma warning( disable : 4054 ) /* conversion from function ptr to data ptr */ |
| 1040 | # pragma warning( disable : 4055 ) /* conversion from data ptr to function ptr */ |
| 1041 | #endif |
Yann Collet | 1bd6c15 | 2019-10-18 15:45:31 -0700 | [diff] [blame] | 1042 | glpi = (LPFN_GLPI)(void*)GetProcAddress(GetModuleHandle(TEXT("kernel32")), |
| 1043 | "GetLogicalProcessorInformation"); |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1044 | |
| 1045 | if (glpi == NULL) { |
| 1046 | goto failed; |
| 1047 | } |
| 1048 | |
| 1049 | while(!done) { |
| 1050 | DWORD rc = glpi(buffer, &returnLength); |
| 1051 | if (FALSE == rc) { |
| 1052 | if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
| 1053 | if (buffer) |
| 1054 | free(buffer); |
| 1055 | buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(returnLength); |
| 1056 | |
| 1057 | if (buffer == NULL) { |
| 1058 | perror("zstd"); |
| 1059 | exit(1); |
| 1060 | } |
| 1061 | } else { |
| 1062 | /* some other error */ |
| 1063 | goto failed; |
| 1064 | } |
| 1065 | } else { |
| 1066 | done = TRUE; |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 1067 | } } |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1068 | |
| 1069 | ptr = buffer; |
| 1070 | |
| 1071 | while (byteOffset + sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION) <= returnLength) { |
| 1072 | |
| 1073 | if (ptr->Relationship == RelationProcessorCore) { |
| 1074 | numPhysicalCores++; |
| 1075 | } |
| 1076 | |
| 1077 | ptr++; |
| 1078 | byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); |
| 1079 | } |
| 1080 | |
| 1081 | free(buffer); |
| 1082 | |
| 1083 | return numPhysicalCores; |
| 1084 | } |
| 1085 | |
| 1086 | failed: |
| 1087 | /* try to fall back on GetSystemInfo */ |
| 1088 | { SYSTEM_INFO sysinfo; |
| 1089 | GetSystemInfo(&sysinfo); |
| 1090 | numPhysicalCores = sysinfo.dwNumberOfProcessors; |
| 1091 | if (numPhysicalCores == 0) numPhysicalCores = 1; /* just in case */ |
| 1092 | } |
| 1093 | return numPhysicalCores; |
| 1094 | } |
| 1095 | |
| 1096 | #elif defined(__APPLE__) |
| 1097 | |
| 1098 | #include <sys/sysctl.h> |
| 1099 | |
| 1100 | /* Use apple-provided syscall |
| 1101 | * see: man 3 sysctl */ |
| 1102 | int UTIL_countPhysicalCores(void) |
| 1103 | { |
| 1104 | static S32 numPhysicalCores = 0; /* apple specifies int32_t */ |
| 1105 | if (numPhysicalCores != 0) return numPhysicalCores; |
| 1106 | |
| 1107 | { size_t size = sizeof(S32); |
| 1108 | int const ret = sysctlbyname("hw.physicalcpu", &numPhysicalCores, &size, NULL, 0); |
| 1109 | if (ret != 0) { |
| 1110 | if (errno == ENOENT) { |
| 1111 | /* entry not present, fall back on 1 */ |
| 1112 | numPhysicalCores = 1; |
| 1113 | } else { |
| 1114 | perror("zstd: can't get number of physical cpus"); |
| 1115 | exit(1); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | return numPhysicalCores; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | #elif defined(__linux__) |
| 1124 | |
| 1125 | /* parse /proc/cpuinfo |
| 1126 | * siblings / cpu cores should give hyperthreading ratio |
| 1127 | * otherwise fall back on sysconf */ |
| 1128 | int UTIL_countPhysicalCores(void) |
| 1129 | { |
| 1130 | static int numPhysicalCores = 0; |
| 1131 | |
| 1132 | if (numPhysicalCores != 0) return numPhysicalCores; |
| 1133 | |
| 1134 | numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
| 1135 | if (numPhysicalCores == -1) { |
| 1136 | /* value not queryable, fall back on 1 */ |
| 1137 | return numPhysicalCores = 1; |
| 1138 | } |
| 1139 | |
| 1140 | /* try to determine if there's hyperthreading */ |
| 1141 | { FILE* const cpuinfo = fopen("/proc/cpuinfo", "r"); |
| 1142 | #define BUF_SIZE 80 |
| 1143 | char buff[BUF_SIZE]; |
| 1144 | |
| 1145 | int siblings = 0; |
| 1146 | int cpu_cores = 0; |
| 1147 | int ratio = 1; |
| 1148 | |
| 1149 | if (cpuinfo == NULL) { |
| 1150 | /* fall back on the sysconf value */ |
| 1151 | return numPhysicalCores; |
| 1152 | } |
| 1153 | |
| 1154 | /* assume the cpu cores/siblings values will be constant across all |
| 1155 | * present processors */ |
| 1156 | while (!feof(cpuinfo)) { |
| 1157 | if (fgets(buff, BUF_SIZE, cpuinfo) != NULL) { |
| 1158 | if (strncmp(buff, "siblings", 8) == 0) { |
| 1159 | const char* const sep = strchr(buff, ':'); |
LeeYoung624 | c5caaf5 | 2019-07-29 17:05:50 +0800 | [diff] [blame] | 1160 | if (sep == NULL || *sep == '\0') { |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1161 | /* formatting was broken? */ |
| 1162 | goto failed; |
| 1163 | } |
| 1164 | |
| 1165 | siblings = atoi(sep + 1); |
| 1166 | } |
| 1167 | if (strncmp(buff, "cpu cores", 9) == 0) { |
| 1168 | const char* const sep = strchr(buff, ':'); |
LeeYoung624 | c5caaf5 | 2019-07-29 17:05:50 +0800 | [diff] [blame] | 1169 | if (sep == NULL || *sep == '\0') { |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1170 | /* formatting was broken? */ |
| 1171 | goto failed; |
| 1172 | } |
| 1173 | |
| 1174 | cpu_cores = atoi(sep + 1); |
| 1175 | } |
| 1176 | } else if (ferror(cpuinfo)) { |
| 1177 | /* fall back on the sysconf value */ |
| 1178 | goto failed; |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 1179 | } } |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1180 | if (siblings && cpu_cores) { |
| 1181 | ratio = siblings / cpu_cores; |
| 1182 | } |
| 1183 | failed: |
| 1184 | fclose(cpuinfo); |
| 1185 | return numPhysicalCores = numPhysicalCores / ratio; |
| 1186 | } |
| 1187 | } |
| 1188 | |
Conrad Meyer | fe82637 | 2019-01-04 11:57:12 -0800 | [diff] [blame] | 1189 | #elif defined(__FreeBSD__) |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1190 | |
Conrad Meyer | fe82637 | 2019-01-04 11:57:12 -0800 | [diff] [blame] | 1191 | #include <sys/param.h> |
| 1192 | #include <sys/sysctl.h> |
| 1193 | |
| 1194 | /* Use physical core sysctl when available |
| 1195 | * see: man 4 smp, man 3 sysctl */ |
| 1196 | int UTIL_countPhysicalCores(void) |
| 1197 | { |
| 1198 | static int numPhysicalCores = 0; /* freebsd sysctl is native int sized */ |
| 1199 | if (numPhysicalCores != 0) return numPhysicalCores; |
| 1200 | |
| 1201 | #if __FreeBSD_version >= 1300008 |
| 1202 | { size_t size = sizeof(numPhysicalCores); |
| 1203 | int ret = sysctlbyname("kern.smp.cores", &numPhysicalCores, &size, NULL, 0); |
| 1204 | if (ret == 0) return numPhysicalCores; |
| 1205 | if (errno != ENOENT) { |
| 1206 | perror("zstd: can't get number of physical cpus"); |
| 1207 | exit(1); |
| 1208 | } |
| 1209 | /* sysctl not present, fall through to older sysconf method */ |
| 1210 | } |
| 1211 | #endif |
| 1212 | |
| 1213 | numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
| 1214 | if (numPhysicalCores == -1) { |
| 1215 | /* value not queryable, fall back on 1 */ |
| 1216 | numPhysicalCores = 1; |
| 1217 | } |
| 1218 | return numPhysicalCores; |
| 1219 | } |
| 1220 | |
Christoph Reiter | d0dcaf5 | 2020-01-08 00:48:26 +0100 | [diff] [blame] | 1221 | #elif defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__CYGWIN__) |
Conrad Meyer | fe82637 | 2019-01-04 11:57:12 -0800 | [diff] [blame] | 1222 | |
| 1223 | /* Use POSIX sysconf |
| 1224 | * see: man 3 sysconf */ |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 1225 | int UTIL_countPhysicalCores(void) |
| 1226 | { |
| 1227 | static int numPhysicalCores = 0; |
| 1228 | |
| 1229 | if (numPhysicalCores != 0) return numPhysicalCores; |
| 1230 | |
| 1231 | numPhysicalCores = (int)sysconf(_SC_NPROCESSORS_ONLN); |
| 1232 | if (numPhysicalCores == -1) { |
| 1233 | /* value not queryable, fall back on 1 */ |
| 1234 | return numPhysicalCores = 1; |
| 1235 | } |
| 1236 | return numPhysicalCores; |
| 1237 | } |
| 1238 | |
| 1239 | #else |
| 1240 | |
| 1241 | int UTIL_countPhysicalCores(void) |
| 1242 | { |
| 1243 | /* assume 1 */ |
| 1244 | return 1; |
| 1245 | } |
| 1246 | |
| 1247 | #endif |
| 1248 | |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 1249 | #if defined (__cplusplus) |
| 1250 | } |
| 1251 | #endif |