2 * Copyright (c) 2016-2020, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
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.
14 #if defined (__cplusplus)
19 /*-****************************************
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 */
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
39 # define UTIL_fseek fseek
43 /*-*************************************************
44 * Sleep & priority functions: Windows - Posix - others
45 ***************************************************/
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)
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); }
58 # define UTIL_sleepMilli(milli) /* disabled */
60 # if ZSTD_SETPRIORITY_SUPPORT
61 # include <sys/resource.h> /* setpriority */
62 # define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
64 # define SET_REALTIME_PRIORITY /* disabled */
67 #else /* unknown non-unix operating systen */
68 # define UTIL_sleep(s) /* disabled */
69 # define UTIL_sleepMilli(milli) /* disabled */
70 # define SET_REALTIME_PRIORITY /* disabled */
74 /*-****************************************
76 ******************************************/
77 #if defined(__INTEL_COMPILER)
78 # pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
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
87 # define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
91 /*-****************************************
93 ******************************************/
94 extern int g_utilDisplayLevel;
97 /*-****************************************
99 ******************************************/
100 #if defined(_MSC_VER)
101 typedef struct __stat64 stat_t;
104 typedef struct stat stat_t;
108 int UTIL_fileExist(const char* filename);
109 int UTIL_isRegularFile(const char* infilename);
110 int UTIL_isDirectory(const char* infilename);
111 int UTIL_isSameFile(const char* file1, const char* file2);
112 int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]);
113 int UTIL_isLink(const char* infilename);
114 int UTIL_isFIFO(const char* infilename);
116 #define UTIL_FILESIZE_UNKNOWN ((U64)(-1))
117 U64 UTIL_getFileSize(const char* infilename);
118 U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles);
119 int UTIL_getFileStat(const char* infilename, stat_t* statbuf);
120 int UTIL_setFileStat(const char* filename, stat_t* statbuf);
121 int UTIL_chmod(char const* filename, mode_t permissions); /*< like chmod, but avoid changing permission of /dev/null */
122 int UTIL_compareStr(const void *p1, const void *p2);
123 const char* UTIL_getFileExtension(const char* infilename);
126 /*-****************************************
128 ******************************************/
131 { const char** fileNames;
132 char* buf; /* fileNames are stored in this buffer (or are read-only) */
133 size_t tableSize; /* nb of fileNames */
134 size_t tableCapacity;
137 /*! UTIL_createFileNamesTable_fromFileName() :
138 * read filenames from @inputFileName, and store them into returned object.
139 * @return : a FileNamesTable*, or NULL in case of error (ex: @inputFileName doesn't exist).
140 * Note: inputFileSize must be less than 50MB
143 UTIL_createFileNamesTable_fromFileName(const char* inputFileName);
145 /*! UTIL_assembleFileNamesTable() :
146 * This function takes ownership of its arguments, @filenames and @buf,
147 * and store them inside the created object.
148 * note : this function never fails,
149 * it will rather exit() the program if internal allocation fails.
150 * @return : resulting FileNamesTable* object.
153 UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf);
155 /*! UTIL_freeFileNamesTable() :
156 * This function is compatible with NULL argument and never fails.
158 void UTIL_freeFileNamesTable(FileNamesTable* table);
160 /*! UTIL_mergeFileNamesTable():
161 * @return : FileNamesTable*, concatenation of @table1 and @table2
162 * note: @table1 and @table2 are consumed (freed) by this operation
165 UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2);
168 /*! UTIL_expandFNT() :
169 * read names from @fnt, and expand those corresponding to directories
170 * update @fnt, now containing only file names,
171 * @return : 0 in case of success, 1 if error
172 * note : in case of error, @fnt[0] is NULL
174 void UTIL_expandFNT(FileNamesTable** fnt, int followLinks);
176 /*! UTIL_createFNT_fromROTable() :
177 * copy the @filenames pointer table inside the returned object.
178 * The names themselves are still stored in their original buffer, which must outlive the object.
179 * @return : a FileNamesTable* object,
180 * or NULL in case of error
183 UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames);
185 /*! UTIL_allocateFileNamesTable() :
186 * Allocates a table of const char*, to insert read-only names later on.
187 * The created FileNamesTable* doesn't hold a buffer.
188 * @return : FileNamesTable*, or NULL, if allocation fails.
190 FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize);
193 /*! UTIL_refFilename() :
194 * Add a reference to read-only name into @fnt table.
195 * As @filename is only referenced, its lifetime must outlive @fnt.
196 * Internal table must be large enough to reference a new member,
197 * otherwise its UB (protected by an `assert()`).
199 void UTIL_refFilename(FileNamesTable* fnt, const char* filename);
202 /* UTIL_createExpandedFNT() is only active if UTIL_HAS_CREATEFILELIST is defined.
203 * Otherwise, UTIL_createExpandedFNT() is a shell function which does nothing
204 * apart from displaying a warning message.
207 # define UTIL_HAS_CREATEFILELIST
208 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
209 # define UTIL_HAS_CREATEFILELIST
211 /* do not define UTIL_HAS_CREATEFILELIST */
214 /*! UTIL_createExpandedFNT() :
215 * read names from @filenames, and expand those corresponding to directories.
216 * links are followed or not depending on @followLinks directive.
217 * @return : an expanded FileNamesTable*, where each name is a file
218 * or NULL in case of error
221 UTIL_createExpandedFNT(const char** filenames, size_t nbFilenames, int followLinks);
224 /*-****************************************
226 ******************************************/
228 int UTIL_countPhysicalCores(void);
231 #if defined (__cplusplus)
235 #endif /* UTIL_H_MODULE */