10c16b537SWarner Losh /* 20c16b537SWarner Losh * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. 30c16b537SWarner Losh * All rights reserved. 40c16b537SWarner Losh * 50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the 60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found 70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree). 80c16b537SWarner Losh * You may select, at your option, one of the above-listed licenses. 90c16b537SWarner Losh */ 100c16b537SWarner Losh 110c16b537SWarner Losh #ifndef UTIL_H_MODULE 120c16b537SWarner Losh #define UTIL_H_MODULE 130c16b537SWarner Losh 140c16b537SWarner Losh #if defined (__cplusplus) 150c16b537SWarner Losh extern "C" { 160c16b537SWarner Losh #endif 170c16b537SWarner Losh 180c16b537SWarner Losh 190c16b537SWarner Losh /*-**************************************** 200c16b537SWarner Losh * Dependencies 210c16b537SWarner Losh ******************************************/ 220f743729SConrad Meyer #include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */ 23a0483764SConrad Meyer #include <stdlib.h> /* malloc, realloc, free */ 240c16b537SWarner Losh #include <stddef.h> /* size_t, ptrdiff_t */ 250c16b537SWarner Losh #include <stdio.h> /* fprintf */ 260c16b537SWarner Losh #include <sys/types.h> /* stat, utime */ 270f743729SConrad Meyer #include <sys/stat.h> /* stat, chmod */ 280c16b537SWarner Losh #if defined(_MSC_VER) 290c16b537SWarner Losh # include <sys/utime.h> /* utime */ 300c16b537SWarner Losh # include <io.h> /* _chmod */ 310c16b537SWarner Losh #else 320c16b537SWarner Losh # include <unistd.h> /* chown, stat */ 330c16b537SWarner Losh # include <utime.h> /* utime */ 340c16b537SWarner Losh #endif 35052d3c12SConrad Meyer #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */ 360c16b537SWarner Losh #include "mem.h" /* U32, U64 */ 370c16b537SWarner Losh 380c16b537SWarner Losh 39a0483764SConrad Meyer /*-************************************************************ 400f743729SConrad Meyer * Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW 410c16b537SWarner Losh ***************************************************************/ 420c16b537SWarner Losh #if defined(_MSC_VER) && (_MSC_VER >= 1400) 430c16b537SWarner Losh # define UTIL_fseek _fseeki64 440c16b537SWarner Losh #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */ 450c16b537SWarner Losh # define UTIL_fseek fseeko 460c16b537SWarner Losh #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS) 470c16b537SWarner Losh # define UTIL_fseek fseeko64 480c16b537SWarner Losh #else 490c16b537SWarner Losh # define UTIL_fseek fseek 500c16b537SWarner Losh #endif 510c16b537SWarner Losh 520c16b537SWarner Losh 530f743729SConrad Meyer /*-************************************************* 540f743729SConrad Meyer * Sleep & priority functions: Windows - Posix - others 550f743729SConrad Meyer ***************************************************/ 560c16b537SWarner Losh #if defined(_WIN32) 570c16b537SWarner Losh # include <windows.h> 580c16b537SWarner Losh # define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) 590c16b537SWarner Losh # define UTIL_sleep(s) Sleep(1000*s) 600c16b537SWarner Losh # define UTIL_sleepMilli(milli) Sleep(milli) 610f743729SConrad Meyer 620f743729SConrad Meyer #elif PLATFORM_POSIX_VERSION > 0 /* Unix-like operating system */ 630f743729SConrad Meyer # include <unistd.h> /* sleep */ 640c16b537SWarner Losh # define UTIL_sleep(s) sleep(s) 650f743729SConrad Meyer # if ZSTD_NANOSLEEP_SUPPORT /* necessarily defined in platform.h */ 660c16b537SWarner Losh # define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); } 670c16b537SWarner Losh # else 680c16b537SWarner Losh # define UTIL_sleepMilli(milli) /* disabled */ 690c16b537SWarner Losh # endif 700f743729SConrad Meyer # if ZSTD_SETPRIORITY_SUPPORT 710f743729SConrad Meyer # include <sys/resource.h> /* setpriority */ 720f743729SConrad Meyer # define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20) 730c16b537SWarner Losh # else 740c16b537SWarner Losh # define SET_REALTIME_PRIORITY /* disabled */ 750f743729SConrad Meyer # endif 760f743729SConrad Meyer 770f743729SConrad Meyer #else /* unknown non-unix operating systen */ 780c16b537SWarner Losh # define UTIL_sleep(s) /* disabled */ 790c16b537SWarner Losh # define UTIL_sleepMilli(milli) /* disabled */ 800f743729SConrad Meyer # define SET_REALTIME_PRIORITY /* disabled */ 810c16b537SWarner Losh #endif 820c16b537SWarner Losh 830c16b537SWarner Losh 84a0483764SConrad Meyer /*-************************************* 850c16b537SWarner Losh * Constants 860c16b537SWarner Losh ***************************************/ 870c16b537SWarner Losh #define LIST_SIZE_INCREASE (8*1024) 880c16b537SWarner Losh 890c16b537SWarner Losh 900c16b537SWarner Losh /*-**************************************** 910c16b537SWarner Losh * Compiler specifics 920c16b537SWarner Losh ******************************************/ 930c16b537SWarner Losh #if defined(__INTEL_COMPILER) 940c16b537SWarner Losh # pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */ 950c16b537SWarner Losh #endif 960c16b537SWarner Losh #if defined(__GNUC__) 970c16b537SWarner Losh # define UTIL_STATIC static __attribute__((unused)) 980c16b537SWarner Losh #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) 990c16b537SWarner Losh # define UTIL_STATIC static inline 1000c16b537SWarner Losh #elif defined(_MSC_VER) 1010c16b537SWarner Losh # define UTIL_STATIC static __inline 1020c16b537SWarner Losh #else 1030c16b537SWarner Losh # define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */ 1040c16b537SWarner Losh #endif 1050c16b537SWarner Losh 1060c16b537SWarner Losh 1070c16b537SWarner Losh /*-**************************************** 1080c16b537SWarner Losh * Console log 1090c16b537SWarner Losh ******************************************/ 110a0483764SConrad Meyer extern int g_utilDisplayLevel; 1110c16b537SWarner Losh #define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__) 1120c16b537SWarner Losh #define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } } 1130c16b537SWarner Losh 1140c16b537SWarner Losh 1150c16b537SWarner Losh /*-**************************************** 1160c16b537SWarner Losh * File functions 1170c16b537SWarner Losh ******************************************/ 1180c16b537SWarner Losh #if defined(_MSC_VER) 1190c16b537SWarner Losh #define chmod _chmod 1200c16b537SWarner Losh typedef struct __stat64 stat_t; 1210c16b537SWarner Losh #else 1220c16b537SWarner Losh typedef struct stat stat_t; 1230c16b537SWarner Losh #endif 1240c16b537SWarner Losh 1250c16b537SWarner Losh 126a0483764SConrad Meyer int UTIL_fileExist(const char* filename); 127a0483764SConrad Meyer int UTIL_isRegularFile(const char* infilename); 128a0483764SConrad Meyer int UTIL_setFileStat(const char* filename, stat_t* statbuf); 129a0483764SConrad Meyer U32 UTIL_isDirectory(const char* infilename); 130a0483764SConrad Meyer int UTIL_getFileStat(const char* infilename, stat_t* statbuf); 131*2b9c00cbSConrad Meyer int UTIL_isSameFile(const char* file1, const char* file2); 13219fcbaf1SConrad Meyer 133a0483764SConrad Meyer U32 UTIL_isLink(const char* infilename); 134052d3c12SConrad Meyer #define UTIL_FILESIZE_UNKNOWN ((U64)(-1)) 135a0483764SConrad Meyer U64 UTIL_getFileSize(const char* infilename); 1360c16b537SWarner Losh 137a0483764SConrad Meyer U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles); 1380c16b537SWarner Losh 1390c16b537SWarner Losh /* 1400c16b537SWarner Losh * A modified version of realloc(). 1410c16b537SWarner Losh * If UTIL_realloc() fails the original block is freed. 1420c16b537SWarner Losh */ 1430c16b537SWarner Losh UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size) 1440c16b537SWarner Losh { 1450c16b537SWarner Losh void *newptr = realloc(ptr, size); 1460c16b537SWarner Losh if (newptr) return newptr; 1470c16b537SWarner Losh free(ptr); 1480c16b537SWarner Losh return NULL; 1490c16b537SWarner Losh } 1500c16b537SWarner Losh 151a0483764SConrad Meyer int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks); 1520c16b537SWarner Losh #ifdef _WIN32 1530c16b537SWarner Losh # define UTIL_HAS_CREATEFILELIST 1540c16b537SWarner Losh #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ 1550c16b537SWarner Losh # define UTIL_HAS_CREATEFILELIST 1560c16b537SWarner Losh # include <dirent.h> /* opendir, readdir */ 1570c16b537SWarner Losh # include <string.h> /* strerror, memcpy */ 1580c16b537SWarner Losh #else 1590c16b537SWarner Losh #endif /* #ifdef _WIN32 */ 1600c16b537SWarner Losh 1610c16b537SWarner Losh /* 1620c16b537SWarner Losh * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories, 1630c16b537SWarner Losh * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb). 1640c16b537SWarner Losh * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer) 1650c16b537SWarner Losh * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called. 1660c16b537SWarner Losh */ 167a0483764SConrad Meyer const char** 1680f743729SConrad Meyer UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, 1690f743729SConrad Meyer char** allocatedBuffer, unsigned* allocatedNamesNb, 170a0483764SConrad Meyer int followLinks); 1710c16b537SWarner Losh 1720c16b537SWarner Losh UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer) 1730c16b537SWarner Losh { 1740c16b537SWarner Losh if (allocatedBuffer) free(allocatedBuffer); 1750c16b537SWarner Losh if (filenameTable) free((void*)filenameTable); 1760c16b537SWarner Losh } 1770c16b537SWarner Losh 178a0483764SConrad Meyer int UTIL_countPhysicalCores(void); 1790c16b537SWarner Losh 1800c16b537SWarner Losh #if defined (__cplusplus) 1810c16b537SWarner Losh } 1820c16b537SWarner Losh #endif 1830c16b537SWarner Losh 1840c16b537SWarner Losh #endif /* UTIL_H_MODULE */ 185