xref: /freebsd/sys/contrib/zstd/programs/util.h (revision 4e99f45480598189d49d45a825533a6c9e12f02c)
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 /*-****************************************
98 *  File functions
99 ******************************************/
100 #if defined(_MSC_VER)
101     typedef struct __stat64 stat_t;
102     typedef int mode_t;
103 #else
104     typedef struct stat stat_t;
105 #endif
106 
107 
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);
115 
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);
124 
125 
126 /*-****************************************
127  *  Lists of Filenames
128  ******************************************/
129 
130 typedef struct
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;
135 } FileNamesTable;
136 
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
141  */
142 FileNamesTable*
143 UTIL_createFileNamesTable_fromFileName(const char* inputFileName);
144 
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.
151  */
152 FileNamesTable*
153 UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf);
154 
155 /*! UTIL_freeFileNamesTable() :
156  *  This function is compatible with NULL argument and never fails.
157  */
158 void UTIL_freeFileNamesTable(FileNamesTable* table);
159 
160 /*! UTIL_mergeFileNamesTable():
161  * @return : FileNamesTable*, concatenation of @table1 and @table2
162  *  note: @table1 and @table2 are consumed (freed) by this operation
163  */
164 FileNamesTable*
165 UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2);
166 
167 
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
173  */
174 void UTIL_expandFNT(FileNamesTable** fnt, int followLinks);
175 
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
181  */
182 FileNamesTable*
183 UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames);
184 
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.
189  */
190 FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize);
191 
192 
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()`).
198  */
199 void UTIL_refFilename(FileNamesTable* fnt, const char* filename);
200 
201 
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.
205  */
206 #ifdef _WIN32
207 #  define UTIL_HAS_CREATEFILELIST
208 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
209 #  define UTIL_HAS_CREATEFILELIST
210 #else
211    /* do not define UTIL_HAS_CREATEFILELIST */
212 #endif
213 
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
219  */
220 FileNamesTable*
221 UTIL_createExpandedFNT(const char** filenames, size_t nbFilenames, int followLinks);
222 
223 
224 /*-****************************************
225  *  System
226  ******************************************/
227 
228 int UTIL_countPhysicalCores(void);
229 
230 
231 #if defined (__cplusplus)
232 }
233 #endif
234 
235 #endif /* UTIL_H_MODULE */
236