1/* 2 * Copyright (c) Przemyslaw Skibinski, Yann Collet, Facebook, Inc. 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#ifndef UTIL_H_MODULE 12#define UTIL_H_MODULE 13 14#if defined (__cplusplus) 15extern "C" { 16#endif 17 18 19/*-**************************************** 20* Dependencies 21******************************************/ 22#include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */ 23#include <stddef.h> /* size_t, ptrdiff_t */ 24#include <sys/types.h> /* stat, utime */ 25#include <sys/stat.h> /* stat, chmod */ 26#include "../lib/common/mem.h" /* U64 */ 27 28 29/*-************************************************************ 30* Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW 31***************************************************************/ 32#if defined(_MSC_VER) && (_MSC_VER >= 1400) 33# define UTIL_fseek _fseeki64 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) 37# define UTIL_fseek fseeko64 38#else 39# define UTIL_fseek fseek 40#endif 41 42 43/*-************************************************* 44* Sleep & priority functions: Windows - Posix - others 45***************************************************/ 46#if defined(_WIN32) 47# include <windows.h> 48# define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) 49# define UTIL_sleep(s) Sleep(1000*s) 50# define UTIL_sleepMilli(milli) Sleep(milli) 51 52#elif PLATFORM_POSIX_VERSION > 0 /* Unix-like operating system */ 53# include <unistd.h> /* sleep */ 54# define UTIL_sleep(s) sleep(s) 55# if ZSTD_NANOSLEEP_SUPPORT /* necessarily defined in platform.h */ 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 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 67#else /* unknown non-unix operating system */ 68# define UTIL_sleep(s) /* disabled */ 69# define UTIL_sleepMilli(milli) /* disabled */ 70# define SET_REALTIME_PRIORITY /* disabled */ 71#endif 72 73 74/*-**************************************** 75* Compiler specifics 76******************************************/ 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 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 91/*-**************************************** 92* Console log 93******************************************/ 94extern int g_utilDisplayLevel; 95 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. 99 * If any of the inputs are stdin itself, then automatically return failure (1). 100 */ 101int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg, const char* acceptableLetters, int hasStdinInput); 102 103 104/*-**************************************** 105* File functions 106******************************************/ 107#if defined(_MSC_VER) 108 typedef struct __stat64 stat_t; 109 typedef int mode_t; 110#elif defined(__MINGW32__) && defined (__MSVCRT__) 111 typedef struct _stati64 stat_t; 112#else 113 typedef struct stat stat_t; 114#endif 115 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 124 125 126/** 127 * Calls platform's equivalent of stat() on filename and writes info to statbuf. 128 * Returns success (1) or failure (0). 129 */ 130int UTIL_stat(const char* filename, stat_t* statbuf); 131 132/** 133 * Instead of getting a file's stats, this updates them with the info in the 134 * provided stat_t. Currently sets owner, group, atime, and mtime. Will only 135 * update this info for regular files. 136 */ 137int UTIL_setFileStat(const char* filename, const stat_t* statbuf); 138 139/** 140 * Set atime to now and mtime to the st_mtim in statbuf. 141 * 142 * Directly wraps utime() or utimensat(). Returns -1 on error. 143 * Does not validate filename is valid. 144 */ 145int UTIL_utime(const char* filename, const stat_t *statbuf); 146 147/* 148 * These helpers operate on a pre-populated stat_t, i.e., the result of 149 * calling one of the above functions. 150 */ 151 152int UTIL_isRegularFileStat(const stat_t* statbuf); 153int UTIL_isDirectoryStat(const stat_t* statbuf); 154int UTIL_isFIFOStat(const stat_t* statbuf); 155int UTIL_isBlockDevStat(const stat_t* statbuf); 156U64 UTIL_getFileSizeStat(const stat_t* statbuf); 157 158/** 159 * Like chmod(), but only modifies regular files. Provided statbuf may be NULL, 160 * in which case this function will stat() the file internally, in order to 161 * check whether it should be modified. 162 */ 163int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions); 164 165/* 166 * In the absence of a pre-existing stat result on the file in question, these 167 * functions will do a stat() call internally and then use that result to 168 * compute the needed information. 169 */ 170 171int UTIL_isRegularFile(const char* infilename); 172int UTIL_isDirectory(const char* infilename); 173int UTIL_isSameFile(const char* file1, const char* file2); 174int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]); 175int UTIL_isLink(const char* infilename); 176int UTIL_isFIFO(const char* infilename); 177 178#define UTIL_FILESIZE_UNKNOWN ((U64)(-1)) 179U64 UTIL_getFileSize(const char* infilename); 180U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles); 181 182/** 183 * Take @size in bytes, 184 * prepare the components to pretty-print it in a scaled way. 185 * The components in the returned struct should be passed in 186 * precision, value, suffix order to a "%.*f%s" format string. 187 * Output policy is sensible to @g_utilDisplayLevel, 188 * for verbose mode (@g_utilDisplayLevel >= 4), 189 * does not scale down. 190 */ 191typedef struct { 192 double value; 193 int precision; 194 const char* suffix; 195} UTIL_HumanReadableSize_t; 196 197UTIL_HumanReadableSize_t UTIL_makeHumanReadableSize(U64 size); 198 199int UTIL_compareStr(const void *p1, const void *p2); 200const char* UTIL_getFileExtension(const char* infilename); 201void UTIL_mirrorSourceFilesDirectories(const char** fileNamesTable, unsigned int nbFiles, const char *outDirName); 202char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName); 203 204 205 206/*-**************************************** 207 * Lists of Filenames 208 ******************************************/ 209 210typedef struct 211{ const char** fileNames; 212 char* buf; /* fileNames are stored in this buffer (or are read-only) */ 213 size_t tableSize; /* nb of fileNames */ 214 size_t tableCapacity; 215} FileNamesTable; 216 217/*! UTIL_createFileNamesTable_fromFileName() : 218 * read filenames from @inputFileName, and store them into returned object. 219 * @return : a FileNamesTable*, or NULL in case of error (ex: @inputFileName doesn't exist). 220 * Note: inputFileSize must be less than 50MB 221 */ 222FileNamesTable* 223UTIL_createFileNamesTable_fromFileName(const char* inputFileName); 224 225/*! UTIL_assembleFileNamesTable() : 226 * This function takes ownership of its arguments, @filenames and @buf, 227 * and store them inside the created object. 228 * note : this function never fails, 229 * it will rather exit() the program if internal allocation fails. 230 * @return : resulting FileNamesTable* object. 231 */ 232FileNamesTable* 233UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf); 234 235/*! UTIL_freeFileNamesTable() : 236 * This function is compatible with NULL argument and never fails. 237 */ 238void UTIL_freeFileNamesTable(FileNamesTable* table); 239 240/*! UTIL_mergeFileNamesTable(): 241 * @return : FileNamesTable*, concatenation of @table1 and @table2 242 * note: @table1 and @table2 are consumed (freed) by this operation 243 */ 244FileNamesTable* 245UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2); 246 247 248/*! UTIL_expandFNT() : 249 * read names from @fnt, and expand those corresponding to directories 250 * update @fnt, now containing only file names, 251 * @return : 0 in case of success, 1 if error 252 * note : in case of error, @fnt[0] is NULL 253 */ 254void UTIL_expandFNT(FileNamesTable** fnt, int followLinks); 255 256/*! UTIL_createFNT_fromROTable() : 257 * copy the @filenames pointer table inside the returned object. 258 * The names themselves are still stored in their original buffer, which must outlive the object. 259 * @return : a FileNamesTable* object, 260 * or NULL in case of error 261 */ 262FileNamesTable* 263UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames); 264 265/*! UTIL_allocateFileNamesTable() : 266 * Allocates a table of const char*, to insert read-only names later on. 267 * The created FileNamesTable* doesn't hold a buffer. 268 * @return : FileNamesTable*, or NULL, if allocation fails. 269 */ 270FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize); 271 272 273/*! UTIL_refFilename() : 274 * Add a reference to read-only name into @fnt table. 275 * As @filename is only referenced, its lifetime must outlive @fnt. 276 * Internal table must be large enough to reference a new member, 277 * otherwise its UB (protected by an `assert()`). 278 */ 279void UTIL_refFilename(FileNamesTable* fnt, const char* filename); 280 281 282/* UTIL_createExpandedFNT() is only active if UTIL_HAS_CREATEFILELIST is defined. 283 * Otherwise, UTIL_createExpandedFNT() is a shell function which does nothing 284 * apart from displaying a warning message. 285 */ 286#ifdef _WIN32 287# define UTIL_HAS_CREATEFILELIST 288#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ 289# define UTIL_HAS_CREATEFILELIST 290# define UTIL_HAS_MIRRORFILELIST 291#else 292 /* do not define UTIL_HAS_CREATEFILELIST */ 293#endif 294 295/*! UTIL_createExpandedFNT() : 296 * read names from @filenames, and expand those corresponding to directories. 297 * links are followed or not depending on @followLinks directive. 298 * @return : an expanded FileNamesTable*, where each name is a file 299 * or NULL in case of error 300 */ 301FileNamesTable* 302UTIL_createExpandedFNT(const char* const* filenames, size_t nbFilenames, int followLinks); 303 304#if defined(_WIN32) || defined(WIN32) 305DWORD CountSetBits(ULONG_PTR bitMask); 306#endif 307 308/*-**************************************** 309 * System 310 ******************************************/ 311 312int UTIL_countCores(int logical); 313 314int UTIL_countPhysicalCores(void); 315 316int UTIL_countLogicalCores(void); 317 318#if defined (__cplusplus) 319} 320#endif 321 322#endif /* UTIL_H_MODULE */ 323