blob: 8234646bf3dc61e482f8331795b8bf54a7aa0746 [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01003 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * 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 Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01009 */
inikep69fcd7c2016-04-28 12:23:33 +020010
inikep69fcd7c2016-04-28 12:23:33 +020011#ifndef UTIL_H_MODULE
12#define UTIL_H_MODULE
13
14#if defined (__cplusplus)
15extern "C" {
16#endif
17
inikep9c22e572016-05-05 11:53:42 +020018
inikep69fcd7c2016-04-28 12:23:33 +020019/*-****************************************
20* Dependencies
21******************************************/
Yann Collet4a85b122018-10-03 15:34:41 -070022#include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010023#include <stddef.h> /* size_t, ptrdiff_t */
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010024#include <sys/types.h> /* stat, utime */
W. Felix Handteb87f97b2021-03-08 17:39:14 -050025#include <sys/stat.h> /* stat, chmod */
W. Felix Handte7dcca6b2020-05-01 16:20:40 -040026#include "../lib/common/mem.h" /* U64 */
inikep69fcd7c2016-04-28 12:23:33 +020027
Yann Collet96ee2072019-11-26 15:44:33 -080028
Rohit Jain91b2fed2018-10-11 17:34:47 -070029/*-************************************************************
Ryan Schmidtb567ce92018-06-09 14:31:17 -050030* Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
Przemyslaw Skibinski6e59b3c2017-02-15 17:03:16 +010031***************************************************************/
32#if defined(_MSC_VER) && (_MSC_VER >= 1400)
Yann Collet96ee2072019-11-26 15:44:33 -080033# define UTIL_fseek _fseeki64
Przemyslaw Skibinski6e59b3c2017-02-15 17:03:16 +010034#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 Collet96ee2072019-11-26 15:44:33 -080037# define UTIL_fseek fseeko64
Przemyslaw Skibinski6e59b3c2017-02-15 17:03:16 +010038#else
Yann Collet96ee2072019-11-26 15:44:33 -080039# define UTIL_fseek fseek
Przemyslaw Skibinski6e59b3c2017-02-15 17:03:16 +010040#endif
41
42
Yann Collet549c19b2018-10-03 14:54:33 -070043/*-*************************************************
44* Sleep & priority functions: Windows - Posix - others
45***************************************************/
inikep31634032016-05-05 00:25:38 +020046#if defined(_WIN32)
inikep83c76b42016-04-28 13:16:01 +020047# include <windows.h>
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010048# define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
inikep83c76b42016-04-28 13:16:01 +020049# define UTIL_sleep(s) Sleep(1000*s)
50# define UTIL_sleepMilli(milli) Sleep(milli)
Yann Collet549c19b2018-10-03 14:54:33 -070051
52#elif PLATFORM_POSIX_VERSION > 0 /* Unix-like operating system */
53# include <unistd.h> /* sleep */
inikep31634032016-05-05 00:25:38 +020054# define UTIL_sleep(s) sleep(s)
Yann Collet4a85b122018-10-03 15:34:41 -070055# if ZSTD_NANOSLEEP_SUPPORT /* necessarily defined in platform.h */
inikep9c22e572016-05-05 11:53:42 +020056# 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 Collet549c19b2018-10-03 14:54:33 -070060# 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 Apostolouebbd6752021-11-13 10:04:04 +020067#else /* unknown non-unix operating system */
inikep31634032016-05-05 00:25:38 +020068# define UTIL_sleep(s) /* disabled */
inikep83c76b42016-04-28 13:16:01 +020069# define UTIL_sleepMilli(milli) /* disabled */
Yann Collet549c19b2018-10-03 14:54:33 -070070# define SET_REALTIME_PRIORITY /* disabled */
inikep83c76b42016-04-28 13:16:01 +020071#endif
72
inikep31634032016-05-05 00:25:38 +020073
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010074/*-****************************************
75* Compiler specifics
76******************************************/
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010077#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 Skibinskiead350b2016-12-21 09:04:59 +010080#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
inikep31634032016-05-05 00:25:38 +020091/*-****************************************
Yann Collet4299c272017-08-31 16:58:47 -070092* Console log
93******************************************/
Rohit Jaina47f6e62018-10-11 16:51:29 -070094extern int g_utilDisplayLevel;
Yann Collet4299c272017-08-31 16:58:47 -070095
senhuang42aab11ce2020-08-25 11:25:49 -040096/**
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.
senhuang4293d63ea2020-09-24 15:58:06 -040099 * If any of the inputs are stdin itself, then automatically return failure (1).
senhuang42aab11ce2020-08-25 11:25:49 -0400100 */
senhuang4293d63ea2020-09-24 15:58:06 -0400101int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg, const char* acceptableLetters, int hasStdinInput);
senhuang42aab11ce2020-08-25 11:25:49 -0400102
Yann Collet4299c272017-08-31 16:58:47 -0700103
104/*-****************************************
inikep31634032016-05-05 00:25:38 +0200105* File functions
106******************************************/
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100107#if defined(_MSC_VER)
Nick Terrell5152fb22017-03-29 18:51:58 -0700108 typedef struct __stat64 stat_t;
Yann Colletb1de3ec2019-11-25 13:59:35 -0800109 typedef int mode_t;
W. Felix Handteb11bea52020-08-05 00:09:29 -0400110#elif defined(__MINGW32__) && defined (__MSVCRT__)
111 typedef struct _stati64 stat_t;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100112#else
113 typedef struct stat stat_t;
114#endif
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100115
Xin Xie9a8ccd42020-06-19 19:35:51 -0700116#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 Skibinskifcf22e32016-11-02 14:08:07 +0100124
Scott Bakerb70175e2021-06-04 20:28:55 -0700125
W. Felix Handteb11bea52020-08-05 00:09:29 -0400126/**
127 * Calls platform's equivalent of stat() on filename and writes info to statbuf.
128 * Returns success (1) or failure (0).
Elliott Hughes44aba642023-09-12 20:18:59 +0000129 *
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 Handteb11bea52020-08-05 00:09:29 -0400133 */
134int UTIL_stat(const char* filename, stat_t* statbuf);
Elliott Hughes44aba642023-09-12 20:18:59 +0000135int UTIL_fstat(const int fd, const char* filename, stat_t* statbuf);
W. Felix Handte76878692020-08-10 15:16:14 -0400136
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 Hughes44aba642023-09-12 20:18:59 +0000141 *
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 Handte76878692020-08-10 15:16:14 -0400145 */
146int UTIL_setFileStat(const char* filename, const stat_t* statbuf);
Elliott Hughes44aba642023-09-12 20:18:59 +0000147int UTIL_setFDStat(const int fd, const char* filename, const stat_t* statbuf);
W. Felix Handte76878692020-08-10 15:16:14 -0400148
W. Felix Handtea719edb2021-08-04 14:49:00 -0400149/**
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 */
155int UTIL_utime(const char* filename, const stat_t *statbuf);
156
W. Felix Handte76878692020-08-10 15:16:14 -0400157/*
158 * These helpers operate on a pre-populated stat_t, i.e., the result of
159 * calling one of the above functions.
160 */
161
162int UTIL_isRegularFileStat(const stat_t* statbuf);
163int UTIL_isDirectoryStat(const stat_t* statbuf);
164int UTIL_isFIFOStat(const stat_t* statbuf);
W. Felix Handte33f3e292021-05-04 16:24:46 -0400165int UTIL_isBlockDevStat(const stat_t* statbuf);
W. Felix Handte76878692020-08-10 15:16:14 -0400166U64 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 Hughes44aba642023-09-12 20:18:59 +0000172 *
173 * If fd is -1, fd is ignored and the filename is used.
W. Felix Handte76878692020-08-10 15:16:14 -0400174 */
175int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions);
Elliott Hughes44aba642023-09-12 20:18:59 +0000176int UTIL_fchmod(const int fd, char const* filename, const stat_t* statbuf, mode_t permissions);
W. Felix Handte76878692020-08-10 15:16:14 -0400177
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 Jaind6d240f2018-10-11 15:07:12 -0700184int UTIL_isRegularFile(const char* infilename);
Yann Collet9a221402019-11-25 13:45:22 -0800185int UTIL_isDirectory(const char* infilename);
shakeelraoe5811e52019-03-23 19:04:56 -0700186int UTIL_isSameFile(const char* file1, const char* file2);
Elliott Hughes44aba642023-09-12 20:18:59 +0000187int UTIL_isSameFileStat(const char* file1, const char* file2, const stat_t* file1Stat, const stat_t* file2Stat);
Shashank Tavildar0f2bff22019-10-28 18:21:47 -0700188int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]);
Yann Collet9a221402019-11-25 13:45:22 -0800189int UTIL_isLink(const char* infilename);
190int UTIL_isFIFO(const char* infilename);
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100191
Elliott Hughes44aba642023-09-12 20:18:59 +0000192/**
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 */
197int UTIL_isConsole(FILE* file);
198
199/**
200 * Pretends that stdin/stdout/stderr is a console for testing.
201 */
202void UTIL_fakeStdinIsConsole(void);
203void UTIL_fakeStdoutIsConsole(void);
204void UTIL_fakeStderrIsConsole(void);
205
206/**
207 * Emit traces for functions that read, or modify file metadata.
208 */
209void UTIL_traceFileStat(void);
210
Yann Collet18b79532017-10-17 16:14:25 -0700211#define UTIL_FILESIZE_UNKNOWN ((U64)(-1))
Rohit Jainf881ee82018-10-11 12:52:19 -0700212U64 UTIL_getFileSize(const char* infilename);
Yann Collet2d9fad42019-11-26 14:53:37 -0800213U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles);
W. Felix Handte76878692020-08-10 15:16:14 -0400214
W. Felix Handtebbb81c82021-06-09 13:05:44 -0400215/**
Yann Colleteab69222021-09-03 12:51:02 -0700216 * 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 Handtebbb81c82021-06-09 13:05:44 -0400219 * precision, value, suffix order to a "%.*f%s" format string.
Yann Colleteab69222021-09-03 12:51:02 -0700220 * Output policy is sensible to @g_utilDisplayLevel,
221 * for verbose mode (@g_utilDisplayLevel >= 4),
222 * does not scale down.
W. Felix Handtebbb81c82021-06-09 13:05:44 -0400223 */
224typedef struct {
W. Felix Handte464bfb02021-06-09 15:22:59 -0400225 double value;
W. Felix Handtebbb81c82021-06-09 13:05:44 -0400226 int precision;
227 const char* suffix;
228} UTIL_HumanReadableSize_t;
229
230UTIL_HumanReadableSize_t UTIL_makeHumanReadableSize(U64 size);
231
Yann Collet9a221402019-11-25 13:45:22 -0800232int UTIL_compareStr(const void *p1, const void *p2);
233const char* UTIL_getFileExtension(const char* infilename);
Xin Xie9a8ccd42020-06-19 19:35:51 -0700234void UTIL_mirrorSourceFilesDirectories(const char** fileNamesTable, unsigned int nbFiles, const char *outDirName);
235char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName);
236
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100237
Yann Collet76b9e422019-11-05 14:59:45 -0800238
239/*-****************************************
240 * Lists of Filenames
241 ******************************************/
242
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100243typedef struct
Yann Collet96ee2072019-11-26 15:44:33 -0800244{ const char** fileNames;
Yann Colletb09f5932019-11-05 17:02:43 -0800245 char* buf; /* fileNames are stored in this buffer (or are read-only) */
246 size_t tableSize; /* nb of fileNames */
247 size_t tableCapacity;
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100248} FileNamesTable;
249
Yann Collet65f2d972019-10-28 15:20:40 -0700250/*! 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 Collet5fb84ca2019-10-25 17:34:29 -0700253 * Note: inputFileSize must be less than 50MB
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100254 */
Yann Collet5fb84ca2019-10-25 17:34:29 -0700255FileNamesTable*
256UTIL_createFileNamesTable_fromFileName(const char* inputFileName);
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100257
Yann Collet9a3de0a2019-11-25 15:34:55 -0800258/*! UTIL_assembleFileNamesTable() :
Yann Collet65f2d972019-10-28 15:20:40 -0700259 * This function takes ownership of its arguments, @filenames and @buf,
260 * and store them inside the created object.
Yann Collet96ee2072019-11-26 15:44:33 -0800261 * note : this function never fails,
262 * it will rather exit() the program if internal allocation fails.
263 * @return : resulting FileNamesTable* object.
Ahmed Abdellahcddb05e2019-10-24 14:42:37 +0100264 */
Ahmed Abdellahcddb05e2019-10-24 14:42:37 +0100265FileNamesTable*
Yann Collet9a3de0a2019-11-25 15:34:55 -0800266UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf);
Ahmed Abdellahcddb05e2019-10-24 14:42:37 +0100267
Yann Collet5fb84ca2019-10-25 17:34:29 -0700268/*! UTIL_freeFileNamesTable() :
269 * This function is compatible with NULL argument and never fails.
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100270 */
271void UTIL_freeFileNamesTable(FileNamesTable* table);
272
Yann Collet31a0abb2019-11-06 09:10:05 -0800273/*! UTIL_mergeFileNamesTable():
Yann Collet5fb84ca2019-10-25 17:34:29 -0700274 * @return : FileNamesTable*, concatenation of @table1 and @table2
275 * note: @table1 and @table2 are consumed (freed) by this operation
Ahmed Abdellah779ea722019-10-15 07:49:13 +0100276 */
Yann Collet5fb84ca2019-10-25 17:34:29 -0700277FileNamesTable*
Yann Collet31a0abb2019-11-06 09:10:05 -0800278UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2);
inikep55d047a2016-04-28 16:50:13 +0200279
inikep31634032016-05-05 00:25:38 +0200280
Yann Collet31a0abb2019-11-06 09:10:05 -0800281/*! UTIL_expandFNT() :
282 * read names from @fnt, and expand those corresponding to directories
283 * update @fnt, now containing only file names,
Yann Collet31a0abb2019-11-06 09:10:05 -0800284 * note : in case of error, @fnt[0] is NULL
inikep0bdb6a82016-05-13 10:52:02 +0200285 */
Yann Collet31a0abb2019-11-06 09:10:05 -0800286void UTIL_expandFNT(FileNamesTable** fnt, int followLinks);
287
Yann Colleta7e33e32019-11-06 14:42:13 -0800288/*! 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 Collet96ee2072019-11-26 15:44:33 -0800294FileNamesTable*
295UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames);
Yann Colletb09f5932019-11-05 17:02:43 -0800296
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 */
302FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize);
303
Elliott Hughes44aba642023-09-12 20:18:59 +0000304/*! 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 */
308int UTIL_searchFileNamesTable(FileNamesTable* table, char const* name);
Yann Colletb09f5932019-11-05 17:02:43 -0800309
310/*! UTIL_refFilename() :
Yann Colletf622c0a2019-11-26 14:48:23 -0800311 * 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 Colletb09f5932019-11-05 17:02:43 -0800315 */
316void UTIL_refFilename(FileNamesTable* fnt, const char* filename);
Yann Collet76b9e422019-11-05 14:59:45 -0800317
318
Yann Collet96ee2072019-11-26 15:44:33 -0800319/* 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 Xie9a8ccd42020-06-19 19:35:51 -0700327# define UTIL_HAS_MIRRORFILELIST
Yann Collet96ee2072019-11-26 15:44:33 -0800328#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 */
338FileNamesTable*
Sen Huangf27e3262021-03-25 10:38:56 -0700339UTIL_createExpandedFNT(const char* const* filenames, size_t nbFilenames, int followLinks);
Yann Collet96ee2072019-11-26 15:44:33 -0800340
Binh Vo6a46e382021-06-16 09:38:43 -0400341#if defined(_WIN32) || defined(WIN32)
342DWORD CountSetBits(ULONG_PTR bitMask);
343#endif
Yann Collet96ee2072019-11-26 15:44:33 -0800344
Yann Collet76b9e422019-11-05 14:59:45 -0800345/*-****************************************
346 * System
347 ******************************************/
inikep31634032016-05-05 00:25:38 +0200348
Binh Vo6a46e382021-06-16 09:38:43 -0400349int UTIL_countCores(int logical);
350
Rohit Jain91b2fed2018-10-11 17:34:47 -0700351int UTIL_countPhysicalCores(void);
inikep31634032016-05-05 00:25:38 +0200352
Binh Vo6a46e382021-06-16 09:38:43 -0400353int UTIL_countLogicalCores(void);
Yann Collet76b9e422019-11-05 14:59:45 -0800354
inikep69fcd7c2016-04-28 12:23:33 +0200355#if defined (__cplusplus)
356}
357#endif
358
359#endif /* UTIL_H_MODULE */