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