Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 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). |
Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 9 | */ |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 10 | |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 11 | #ifndef UTIL_H_MODULE |
| 12 | #define UTIL_H_MODULE |
| 13 | |
| 14 | #if defined (__cplusplus) |
| 15 | extern "C" { |
| 16 | #endif |
| 17 | |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 18 | |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 19 | /*-**************************************** |
| 20 | * Dependencies |
| 21 | ******************************************/ |
Yann Collet | 4a85b12 | 2018-10-03 15:34:41 -0700 | [diff] [blame] | 22 | #include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 23 | #include <stddef.h> /* size_t, ptrdiff_t */ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 24 | #include <sys/types.h> /* stat, utime */ |
W. Felix Handte | b87f97b | 2021-03-08 17:39:14 -0500 | [diff] [blame] | 25 | #include <sys/stat.h> /* stat, chmod */ |
W. Felix Handte | 7dcca6b | 2020-05-01 16:20:40 -0400 | [diff] [blame] | 26 | #include "../lib/common/mem.h" /* U64 */ |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 27 | |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 28 | |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 29 | /*-************************************************************ |
Ryan Schmidt | b567ce9 | 2018-06-09 14:31:17 -0500 | [diff] [blame] | 30 | * Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW |
Przemyslaw Skibinski | 6e59b3c | 2017-02-15 17:03:16 +0100 | [diff] [blame] | 31 | ***************************************************************/ |
| 32 | #if defined(_MSC_VER) && (_MSC_VER >= 1400) |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 33 | # define UTIL_fseek _fseeki64 |
Przemyslaw Skibinski | 6e59b3c | 2017-02-15 17:03:16 +0100 | [diff] [blame] | 34 | #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */ |
| 35 | # define UTIL_fseek fseeko |
| 36 | #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS) |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 37 | # define UTIL_fseek fseeko64 |
Przemyslaw Skibinski | 6e59b3c | 2017-02-15 17:03:16 +0100 | [diff] [blame] | 38 | #else |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 39 | # define UTIL_fseek fseek |
Przemyslaw Skibinski | 6e59b3c | 2017-02-15 17:03:16 +0100 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | |
Yann Collet | 549c19b | 2018-10-03 14:54:33 -0700 | [diff] [blame] | 43 | /*-************************************************* |
| 44 | * Sleep & priority functions: Windows - Posix - others |
| 45 | ***************************************************/ |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 46 | #if defined(_WIN32) |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 47 | # include <windows.h> |
Przemyslaw Skibinski | 94abd6a | 2017-02-07 16:36:19 +0100 | [diff] [blame] | 48 | # define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 49 | # define UTIL_sleep(s) Sleep(1000*s) |
| 50 | # define UTIL_sleepMilli(milli) Sleep(milli) |
Yann Collet | 549c19b | 2018-10-03 14:54:33 -0700 | [diff] [blame] | 51 | |
| 52 | #elif PLATFORM_POSIX_VERSION > 0 /* Unix-like operating system */ |
| 53 | # include <unistd.h> /* sleep */ |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 54 | # define UTIL_sleep(s) sleep(s) |
Yann Collet | 4a85b12 | 2018-10-03 15:34:41 -0700 | [diff] [blame] | 55 | # if ZSTD_NANOSLEEP_SUPPORT /* necessarily defined in platform.h */ |
inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 56 | # define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); } |
| 57 | # else |
| 58 | # define UTIL_sleepMilli(milli) /* disabled */ |
| 59 | # endif |
Yann Collet | 549c19b | 2018-10-03 14:54:33 -0700 | [diff] [blame] | 60 | # if ZSTD_SETPRIORITY_SUPPORT |
| 61 | # include <sys/resource.h> /* setpriority */ |
| 62 | # define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20) |
| 63 | # else |
| 64 | # define SET_REALTIME_PRIORITY /* disabled */ |
| 65 | # endif |
| 66 | |
Dimitris Apostolou | ebbd675 | 2021-11-13 10:04:04 +0200 | [diff] [blame] | 67 | #else /* unknown non-unix operating system */ |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 68 | # define UTIL_sleep(s) /* disabled */ |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 69 | # define UTIL_sleepMilli(milli) /* disabled */ |
Yann Collet | 549c19b | 2018-10-03 14:54:33 -0700 | [diff] [blame] | 70 | # define SET_REALTIME_PRIORITY /* disabled */ |
inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 71 | #endif |
| 72 | |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 73 | |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 74 | /*-**************************************** |
| 75 | * Compiler specifics |
| 76 | ******************************************/ |
Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 77 | #if defined(__INTEL_COMPILER) |
| 78 | # pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */ |
| 79 | #endif |
Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 80 | #if defined(__GNUC__) |
| 81 | # define UTIL_STATIC static __attribute__((unused)) |
| 82 | #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) |
| 83 | # define UTIL_STATIC static inline |
| 84 | #elif defined(_MSC_VER) |
| 85 | # define UTIL_STATIC static __inline |
| 86 | #else |
| 87 | # define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */ |
| 88 | #endif |
| 89 | |
| 90 | |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 91 | /*-**************************************** |
Yann Collet | 4299c27 | 2017-08-31 16:58:47 -0700 | [diff] [blame] | 92 | * Console log |
| 93 | ******************************************/ |
Rohit Jain | a47f6e6 | 2018-10-11 16:51:29 -0700 | [diff] [blame] | 94 | extern int g_utilDisplayLevel; |
Yann Collet | 4299c27 | 2017-08-31 16:58:47 -0700 | [diff] [blame] | 95 | |
senhuang42 | aab11ce | 2020-08-25 11:25:49 -0400 | [diff] [blame] | 96 | /** |
| 97 | * Displays a message prompt and returns success (0) if first character from stdin |
| 98 | * matches any from acceptableLetters. Otherwise, returns failure (1) and displays abortMsg. |
senhuang42 | 93d63ea | 2020-09-24 15:58:06 -0400 | [diff] [blame] | 99 | * If any of the inputs are stdin itself, then automatically return failure (1). |
senhuang42 | aab11ce | 2020-08-25 11:25:49 -0400 | [diff] [blame] | 100 | */ |
senhuang42 | 93d63ea | 2020-09-24 15:58:06 -0400 | [diff] [blame] | 101 | int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg, const char* acceptableLetters, int hasStdinInput); |
senhuang42 | aab11ce | 2020-08-25 11:25:49 -0400 | [diff] [blame] | 102 | |
Yann Collet | 4299c27 | 2017-08-31 16:58:47 -0700 | [diff] [blame] | 103 | |
| 104 | /*-**************************************** |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 105 | * File functions |
| 106 | ******************************************/ |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 107 | #if defined(_MSC_VER) |
Nick Terrell | 5152fb2 | 2017-03-29 18:51:58 -0700 | [diff] [blame] | 108 | typedef struct __stat64 stat_t; |
Yann Collet | b1de3ec | 2019-11-25 13:59:35 -0800 | [diff] [blame] | 109 | typedef int mode_t; |
W. Felix Handte | b11bea5 | 2020-08-05 00:09:29 -0400 | [diff] [blame] | 110 | #elif defined(__MINGW32__) && defined (__MSVCRT__) |
| 111 | typedef struct _stati64 stat_t; |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 112 | #else |
| 113 | typedef struct stat stat_t; |
| 114 | #endif |
Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 115 | |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 116 | #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__) /* windows support */ |
| 117 | #define PATH_SEP '\\' |
| 118 | #define STRDUP(s) _strdup(s) |
| 119 | #else |
| 120 | #define PATH_SEP '/' |
| 121 | #include <libgen.h> |
| 122 | #define STRDUP(s) strdup(s) |
| 123 | #endif |
Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 124 | |
Scott Baker | b70175e | 2021-06-04 20:28:55 -0700 | [diff] [blame] | 125 | |
W. Felix Handte | b11bea5 | 2020-08-05 00:09:29 -0400 | [diff] [blame] | 126 | /** |
| 127 | * Calls platform's equivalent of stat() on filename and writes info to statbuf. |
| 128 | * Returns success (1) or failure (0). |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 129 | * |
| 130 | * UTIL_fstat() is like UTIL_stat() but takes an optional fd that refers to the |
| 131 | * file in question. It turns out that this can be meaningfully faster. If fd is |
| 132 | * -1, behaves just like UTIL_stat() (i.e., falls back to using the filename). |
W. Felix Handte | b11bea5 | 2020-08-05 00:09:29 -0400 | [diff] [blame] | 133 | */ |
| 134 | int UTIL_stat(const char* filename, stat_t* statbuf); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 135 | int UTIL_fstat(const int fd, const char* filename, stat_t* statbuf); |
W. Felix Handte | 7687869 | 2020-08-10 15:16:14 -0400 | [diff] [blame] | 136 | |
| 137 | /** |
| 138 | * Instead of getting a file's stats, this updates them with the info in the |
| 139 | * provided stat_t. Currently sets owner, group, atime, and mtime. Will only |
| 140 | * update this info for regular files. |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 141 | * |
| 142 | * UTIL_setFDStat() also takes an fd, and will preferentially use that to |
| 143 | * indicate which file to modify, If fd is -1, it will fall back to using the |
| 144 | * filename. |
W. Felix Handte | 7687869 | 2020-08-10 15:16:14 -0400 | [diff] [blame] | 145 | */ |
| 146 | int UTIL_setFileStat(const char* filename, const stat_t* statbuf); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 147 | int UTIL_setFDStat(const int fd, const char* filename, const stat_t* statbuf); |
W. Felix Handte | 7687869 | 2020-08-10 15:16:14 -0400 | [diff] [blame] | 148 | |
W. Felix Handte | a719edb | 2021-08-04 14:49:00 -0400 | [diff] [blame] | 149 | /** |
| 150 | * Set atime to now and mtime to the st_mtim in statbuf. |
| 151 | * |
| 152 | * Directly wraps utime() or utimensat(). Returns -1 on error. |
| 153 | * Does not validate filename is valid. |
| 154 | */ |
| 155 | int UTIL_utime(const char* filename, const stat_t *statbuf); |
| 156 | |
W. Felix Handte | 7687869 | 2020-08-10 15:16:14 -0400 | [diff] [blame] | 157 | /* |
| 158 | * These helpers operate on a pre-populated stat_t, i.e., the result of |
| 159 | * calling one of the above functions. |
| 160 | */ |
| 161 | |
| 162 | int UTIL_isRegularFileStat(const stat_t* statbuf); |
| 163 | int UTIL_isDirectoryStat(const stat_t* statbuf); |
| 164 | int UTIL_isFIFOStat(const stat_t* statbuf); |
W. Felix Handte | 33f3e29 | 2021-05-04 16:24:46 -0400 | [diff] [blame] | 165 | int UTIL_isBlockDevStat(const stat_t* statbuf); |
W. Felix Handte | 7687869 | 2020-08-10 15:16:14 -0400 | [diff] [blame] | 166 | U64 UTIL_getFileSizeStat(const stat_t* statbuf); |
| 167 | |
| 168 | /** |
| 169 | * Like chmod(), but only modifies regular files. Provided statbuf may be NULL, |
| 170 | * in which case this function will stat() the file internally, in order to |
| 171 | * check whether it should be modified. |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 172 | * |
| 173 | * If fd is -1, fd is ignored and the filename is used. |
W. Felix Handte | 7687869 | 2020-08-10 15:16:14 -0400 | [diff] [blame] | 174 | */ |
| 175 | int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 176 | int UTIL_fchmod(const int fd, char const* filename, const stat_t* statbuf, mode_t permissions); |
W. Felix Handte | 7687869 | 2020-08-10 15:16:14 -0400 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * In the absence of a pre-existing stat result on the file in question, these |
| 180 | * functions will do a stat() call internally and then use that result to |
| 181 | * compute the needed information. |
| 182 | */ |
| 183 | |
Rohit Jain | d6d240f | 2018-10-11 15:07:12 -0700 | [diff] [blame] | 184 | int UTIL_isRegularFile(const char* infilename); |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 185 | int UTIL_isDirectory(const char* infilename); |
shakeelrao | e5811e5 | 2019-03-23 19:04:56 -0700 | [diff] [blame] | 186 | int UTIL_isSameFile(const char* file1, const char* file2); |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 187 | int UTIL_isSameFileStat(const char* file1, const char* file2, const stat_t* file1Stat, const stat_t* file2Stat); |
Shashank Tavildar | 0f2bff2 | 2019-10-28 18:21:47 -0700 | [diff] [blame] | 188 | int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]); |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 189 | int UTIL_isLink(const char* infilename); |
| 190 | int UTIL_isFIFO(const char* infilename); |
Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 191 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 192 | /** |
| 193 | * Returns with the given file descriptor is a console. |
| 194 | * Allows faking whether stdin/stdout/stderr is a console |
| 195 | * using UTIL_fake*IsConsole(). |
| 196 | */ |
| 197 | int UTIL_isConsole(FILE* file); |
| 198 | |
| 199 | /** |
| 200 | * Pretends that stdin/stdout/stderr is a console for testing. |
| 201 | */ |
| 202 | void UTIL_fakeStdinIsConsole(void); |
| 203 | void UTIL_fakeStdoutIsConsole(void); |
| 204 | void UTIL_fakeStderrIsConsole(void); |
| 205 | |
| 206 | /** |
| 207 | * Emit traces for functions that read, or modify file metadata. |
| 208 | */ |
| 209 | void UTIL_traceFileStat(void); |
| 210 | |
Yann Collet | 18b7953 | 2017-10-17 16:14:25 -0700 | [diff] [blame] | 211 | #define UTIL_FILESIZE_UNKNOWN ((U64)(-1)) |
Rohit Jain | f881ee8 | 2018-10-11 12:52:19 -0700 | [diff] [blame] | 212 | U64 UTIL_getFileSize(const char* infilename); |
Yann Collet | 2d9fad4 | 2019-11-26 14:53:37 -0800 | [diff] [blame] | 213 | U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles); |
W. Felix Handte | 7687869 | 2020-08-10 15:16:14 -0400 | [diff] [blame] | 214 | |
W. Felix Handte | bbb81c8 | 2021-06-09 13:05:44 -0400 | [diff] [blame] | 215 | /** |
Yann Collet | eab6922 | 2021-09-03 12:51:02 -0700 | [diff] [blame] | 216 | * Take @size in bytes, |
| 217 | * prepare the components to pretty-print it in a scaled way. |
| 218 | * The components in the returned struct should be passed in |
W. Felix Handte | bbb81c8 | 2021-06-09 13:05:44 -0400 | [diff] [blame] | 219 | * precision, value, suffix order to a "%.*f%s" format string. |
Yann Collet | eab6922 | 2021-09-03 12:51:02 -0700 | [diff] [blame] | 220 | * Output policy is sensible to @g_utilDisplayLevel, |
| 221 | * for verbose mode (@g_utilDisplayLevel >= 4), |
| 222 | * does not scale down. |
W. Felix Handte | bbb81c8 | 2021-06-09 13:05:44 -0400 | [diff] [blame] | 223 | */ |
| 224 | typedef struct { |
W. Felix Handte | 464bfb0 | 2021-06-09 15:22:59 -0400 | [diff] [blame] | 225 | double value; |
W. Felix Handte | bbb81c8 | 2021-06-09 13:05:44 -0400 | [diff] [blame] | 226 | int precision; |
| 227 | const char* suffix; |
| 228 | } UTIL_HumanReadableSize_t; |
| 229 | |
| 230 | UTIL_HumanReadableSize_t UTIL_makeHumanReadableSize(U64 size); |
| 231 | |
Yann Collet | 9a22140 | 2019-11-25 13:45:22 -0800 | [diff] [blame] | 232 | int UTIL_compareStr(const void *p1, const void *p2); |
| 233 | const char* UTIL_getFileExtension(const char* infilename); |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 234 | void UTIL_mirrorSourceFilesDirectories(const char** fileNamesTable, unsigned int nbFiles, const char *outDirName); |
| 235 | char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName); |
| 236 | |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 237 | |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 238 | |
| 239 | /*-**************************************** |
| 240 | * Lists of Filenames |
| 241 | ******************************************/ |
| 242 | |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 243 | typedef struct |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 244 | { const char** fileNames; |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 245 | char* buf; /* fileNames are stored in this buffer (or are read-only) */ |
| 246 | size_t tableSize; /* nb of fileNames */ |
| 247 | size_t tableCapacity; |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 248 | } FileNamesTable; |
| 249 | |
Yann Collet | 65f2d97 | 2019-10-28 15:20:40 -0700 | [diff] [blame] | 250 | /*! UTIL_createFileNamesTable_fromFileName() : |
| 251 | * read filenames from @inputFileName, and store them into returned object. |
| 252 | * @return : a FileNamesTable*, or NULL in case of error (ex: @inputFileName doesn't exist). |
Yann Collet | 5fb84ca | 2019-10-25 17:34:29 -0700 | [diff] [blame] | 253 | * Note: inputFileSize must be less than 50MB |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 254 | */ |
Yann Collet | 5fb84ca | 2019-10-25 17:34:29 -0700 | [diff] [blame] | 255 | FileNamesTable* |
| 256 | UTIL_createFileNamesTable_fromFileName(const char* inputFileName); |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 257 | |
Yann Collet | 9a3de0a | 2019-11-25 15:34:55 -0800 | [diff] [blame] | 258 | /*! UTIL_assembleFileNamesTable() : |
Yann Collet | 65f2d97 | 2019-10-28 15:20:40 -0700 | [diff] [blame] | 259 | * This function takes ownership of its arguments, @filenames and @buf, |
| 260 | * and store them inside the created object. |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 261 | * note : this function never fails, |
| 262 | * it will rather exit() the program if internal allocation fails. |
| 263 | * @return : resulting FileNamesTable* object. |
Ahmed Abdellah | cddb05e | 2019-10-24 14:42:37 +0100 | [diff] [blame] | 264 | */ |
Ahmed Abdellah | cddb05e | 2019-10-24 14:42:37 +0100 | [diff] [blame] | 265 | FileNamesTable* |
Yann Collet | 9a3de0a | 2019-11-25 15:34:55 -0800 | [diff] [blame] | 266 | UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf); |
Ahmed Abdellah | cddb05e | 2019-10-24 14:42:37 +0100 | [diff] [blame] | 267 | |
Yann Collet | 5fb84ca | 2019-10-25 17:34:29 -0700 | [diff] [blame] | 268 | /*! UTIL_freeFileNamesTable() : |
| 269 | * This function is compatible with NULL argument and never fails. |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 270 | */ |
| 271 | void UTIL_freeFileNamesTable(FileNamesTable* table); |
| 272 | |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 273 | /*! UTIL_mergeFileNamesTable(): |
Yann Collet | 5fb84ca | 2019-10-25 17:34:29 -0700 | [diff] [blame] | 274 | * @return : FileNamesTable*, concatenation of @table1 and @table2 |
| 275 | * note: @table1 and @table2 are consumed (freed) by this operation |
Ahmed Abdellah | 779ea72 | 2019-10-15 07:49:13 +0100 | [diff] [blame] | 276 | */ |
Yann Collet | 5fb84ca | 2019-10-25 17:34:29 -0700 | [diff] [blame] | 277 | FileNamesTable* |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 278 | UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2); |
inikep | 55d047a | 2016-04-28 16:50:13 +0200 | [diff] [blame] | 279 | |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 280 | |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 281 | /*! UTIL_expandFNT() : |
| 282 | * read names from @fnt, and expand those corresponding to directories |
| 283 | * update @fnt, now containing only file names, |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 284 | * note : in case of error, @fnt[0] is NULL |
inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 285 | */ |
Yann Collet | 31a0abb | 2019-11-06 09:10:05 -0800 | [diff] [blame] | 286 | void UTIL_expandFNT(FileNamesTable** fnt, int followLinks); |
| 287 | |
Yann Collet | a7e33e3 | 2019-11-06 14:42:13 -0800 | [diff] [blame] | 288 | /*! UTIL_createFNT_fromROTable() : |
| 289 | * copy the @filenames pointer table inside the returned object. |
| 290 | * The names themselves are still stored in their original buffer, which must outlive the object. |
| 291 | * @return : a FileNamesTable* object, |
| 292 | * or NULL in case of error |
| 293 | */ |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 294 | FileNamesTable* |
| 295 | UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames); |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 296 | |
| 297 | /*! UTIL_allocateFileNamesTable() : |
| 298 | * Allocates a table of const char*, to insert read-only names later on. |
| 299 | * The created FileNamesTable* doesn't hold a buffer. |
| 300 | * @return : FileNamesTable*, or NULL, if allocation fails. |
| 301 | */ |
| 302 | FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize); |
| 303 | |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 304 | /*! UTIL_searchFileNamesTable() : |
| 305 | * Searched through entries in FileNamesTable for a specific name. |
| 306 | * @return : index of entry if found or -1 if not found |
| 307 | */ |
| 308 | int UTIL_searchFileNamesTable(FileNamesTable* table, char const* name); |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 309 | |
| 310 | /*! UTIL_refFilename() : |
Yann Collet | f622c0a | 2019-11-26 14:48:23 -0800 | [diff] [blame] | 311 | * Add a reference to read-only name into @fnt table. |
| 312 | * As @filename is only referenced, its lifetime must outlive @fnt. |
| 313 | * Internal table must be large enough to reference a new member, |
| 314 | * otherwise its UB (protected by an `assert()`). |
Yann Collet | b09f593 | 2019-11-05 17:02:43 -0800 | [diff] [blame] | 315 | */ |
| 316 | void UTIL_refFilename(FileNamesTable* fnt, const char* filename); |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 317 | |
| 318 | |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 319 | /* UTIL_createExpandedFNT() is only active if UTIL_HAS_CREATEFILELIST is defined. |
| 320 | * Otherwise, UTIL_createExpandedFNT() is a shell function which does nothing |
| 321 | * apart from displaying a warning message. |
| 322 | */ |
| 323 | #ifdef _WIN32 |
| 324 | # define UTIL_HAS_CREATEFILELIST |
| 325 | #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ |
| 326 | # define UTIL_HAS_CREATEFILELIST |
Xin Xie | 9a8ccd4 | 2020-06-19 19:35:51 -0700 | [diff] [blame] | 327 | # define UTIL_HAS_MIRRORFILELIST |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 328 | #else |
| 329 | /* do not define UTIL_HAS_CREATEFILELIST */ |
| 330 | #endif |
| 331 | |
| 332 | /*! UTIL_createExpandedFNT() : |
| 333 | * read names from @filenames, and expand those corresponding to directories. |
| 334 | * links are followed or not depending on @followLinks directive. |
| 335 | * @return : an expanded FileNamesTable*, where each name is a file |
| 336 | * or NULL in case of error |
| 337 | */ |
| 338 | FileNamesTable* |
Sen Huang | f27e326 | 2021-03-25 10:38:56 -0700 | [diff] [blame] | 339 | UTIL_createExpandedFNT(const char* const* filenames, size_t nbFilenames, int followLinks); |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 340 | |
Binh Vo | 6a46e38 | 2021-06-16 09:38:43 -0400 | [diff] [blame] | 341 | #if defined(_WIN32) || defined(WIN32) |
| 342 | DWORD CountSetBits(ULONG_PTR bitMask); |
| 343 | #endif |
Yann Collet | 96ee207 | 2019-11-26 15:44:33 -0800 | [diff] [blame] | 344 | |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 345 | /*-**************************************** |
| 346 | * System |
| 347 | ******************************************/ |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 348 | |
Binh Vo | 6a46e38 | 2021-06-16 09:38:43 -0400 | [diff] [blame] | 349 | int UTIL_countCores(int logical); |
| 350 | |
Rohit Jain | 91b2fed | 2018-10-11 17:34:47 -0700 | [diff] [blame] | 351 | int UTIL_countPhysicalCores(void); |
inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 352 | |
Binh Vo | 6a46e38 | 2021-06-16 09:38:43 -0400 | [diff] [blame] | 353 | int UTIL_countLogicalCores(void); |
Yann Collet | 76b9e42 | 2019-11-05 14:59:45 -0800 | [diff] [blame] | 354 | |
inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 355 | #if defined (__cplusplus) |
| 356 | } |
| 357 | #endif |
| 358 | |
| 359 | #endif /* UTIL_H_MODULE */ |