xref: /freebsd/sys/contrib/zstd/programs/util.h (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1 /*
2  * Copyright (c) 2016-2020, 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)
15 extern "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 systen */
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 ******************************************/
94 extern 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  */
101 int 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  * Calls platform's equivalent of stat() on filename and writes info to statbuf.
127  * Returns success (1) or failure (0).
128  */
129 int UTIL_stat(const char* filename, stat_t* statbuf);
130 
131 /**
132  * Instead of getting a file's stats, this updates them with the info in the
133  * provided stat_t. Currently sets owner, group, atime, and mtime. Will only
134  * update this info for regular files.
135  */
136 int UTIL_setFileStat(const char* filename, const stat_t* statbuf);
137 
138 /*
139  * These helpers operate on a pre-populated stat_t, i.e., the result of
140  * calling one of the above functions.
141  */
142 
143 int UTIL_isRegularFileStat(const stat_t* statbuf);
144 int UTIL_isDirectoryStat(const stat_t* statbuf);
145 int UTIL_isFIFOStat(const stat_t* statbuf);
146 U64 UTIL_getFileSizeStat(const stat_t* statbuf);
147 
148 /**
149  * Like chmod(), but only modifies regular files. Provided statbuf may be NULL,
150  * in which case this function will stat() the file internally, in order to
151  * check whether it should be modified.
152  */
153 int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions);
154 
155 /*
156  * In the absence of a pre-existing stat result on the file in question, these
157  * functions will do a stat() call internally and then use that result to
158  * compute the needed information.
159  */
160 
161 int UTIL_isRegularFile(const char* infilename);
162 int UTIL_isDirectory(const char* infilename);
163 int UTIL_isSameFile(const char* file1, const char* file2);
164 int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]);
165 int UTIL_isLink(const char* infilename);
166 int UTIL_isFIFO(const char* infilename);
167 
168 #define UTIL_FILESIZE_UNKNOWN  ((U64)(-1))
169 U64 UTIL_getFileSize(const char* infilename);
170 U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles);
171 
172 int UTIL_compareStr(const void *p1, const void *p2);
173 const char* UTIL_getFileExtension(const char* infilename);
174 void  UTIL_mirrorSourceFilesDirectories(const char** fileNamesTable, unsigned int nbFiles, const char *outDirName);
175 char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName);
176 
177 
178 
179 /*-****************************************
180  *  Lists of Filenames
181  ******************************************/
182 
183 typedef struct
184 {   const char** fileNames;
185     char* buf;            /* fileNames are stored in this buffer (or are read-only) */
186     size_t tableSize;     /* nb of fileNames */
187     size_t tableCapacity;
188 } FileNamesTable;
189 
190 /*! UTIL_createFileNamesTable_fromFileName() :
191  *  read filenames from @inputFileName, and store them into returned object.
192  * @return : a FileNamesTable*, or NULL in case of error (ex: @inputFileName doesn't exist).
193  *  Note: inputFileSize must be less than 50MB
194  */
195 FileNamesTable*
196 UTIL_createFileNamesTable_fromFileName(const char* inputFileName);
197 
198 /*! UTIL_assembleFileNamesTable() :
199  *  This function takes ownership of its arguments, @filenames and @buf,
200  *  and store them inside the created object.
201  *  note : this function never fails,
202  *         it will rather exit() the program if internal allocation fails.
203  * @return : resulting FileNamesTable* object.
204  */
205 FileNamesTable*
206 UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf);
207 
208 /*! UTIL_freeFileNamesTable() :
209  *  This function is compatible with NULL argument and never fails.
210  */
211 void UTIL_freeFileNamesTable(FileNamesTable* table);
212 
213 /*! UTIL_mergeFileNamesTable():
214  * @return : FileNamesTable*, concatenation of @table1 and @table2
215  *  note: @table1 and @table2 are consumed (freed) by this operation
216  */
217 FileNamesTable*
218 UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2);
219 
220 
221 /*! UTIL_expandFNT() :
222  *  read names from @fnt, and expand those corresponding to directories
223  *  update @fnt, now containing only file names,
224  * @return : 0 in case of success, 1 if error
225  *  note : in case of error, @fnt[0] is NULL
226  */
227 void UTIL_expandFNT(FileNamesTable** fnt, int followLinks);
228 
229 /*! UTIL_createFNT_fromROTable() :
230  *  copy the @filenames pointer table inside the returned object.
231  *  The names themselves are still stored in their original buffer, which must outlive the object.
232  * @return : a FileNamesTable* object,
233  *        or NULL in case of error
234  */
235 FileNamesTable*
236 UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames);
237 
238 /*! UTIL_allocateFileNamesTable() :
239  *  Allocates a table of const char*, to insert read-only names later on.
240  *  The created FileNamesTable* doesn't hold a buffer.
241  * @return : FileNamesTable*, or NULL, if allocation fails.
242  */
243 FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize);
244 
245 
246 /*! UTIL_refFilename() :
247  *  Add a reference to read-only name into @fnt table.
248  *  As @filename is only referenced, its lifetime must outlive @fnt.
249  *  Internal table must be large enough to reference a new member,
250  *  otherwise its UB (protected by an `assert()`).
251  */
252 void UTIL_refFilename(FileNamesTable* fnt, const char* filename);
253 
254 
255 /* UTIL_createExpandedFNT() is only active if UTIL_HAS_CREATEFILELIST is defined.
256  * Otherwise, UTIL_createExpandedFNT() is a shell function which does nothing
257  * apart from displaying a warning message.
258  */
259 #ifdef _WIN32
260 #  define UTIL_HAS_CREATEFILELIST
261 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
262 #  define UTIL_HAS_CREATEFILELIST
263 #  define UTIL_HAS_MIRRORFILELIST
264 #else
265    /* do not define UTIL_HAS_CREATEFILELIST */
266 #endif
267 
268 /*! UTIL_createExpandedFNT() :
269  *  read names from @filenames, and expand those corresponding to directories.
270  *  links are followed or not depending on @followLinks directive.
271  * @return : an expanded FileNamesTable*, where each name is a file
272  *        or NULL in case of error
273  */
274 FileNamesTable*
275 UTIL_createExpandedFNT(const char** filenames, size_t nbFilenames, int followLinks);
276 
277 
278 /*-****************************************
279  *  System
280  ******************************************/
281 
282 int UTIL_countPhysicalCores(void);
283 
284 
285 #if defined (__cplusplus)
286 }
287 #endif
288 
289 #endif /* UTIL_H_MODULE */
290