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