1 /*
2 * Copyright (c) 2016-present, 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 <stdlib.h> /* malloc, realloc, free */
24 #include <stddef.h> /* size_t, ptrdiff_t */
25 #include <stdio.h> /* fprintf */
26 #include <sys/types.h> /* stat, utime */
27 #include <sys/stat.h> /* stat, chmod */
28 #if defined(_WIN32)
29 # include <sys/utime.h> /* utime */
30 # include <io.h> /* _chmod */
31 #else
32 # include <unistd.h> /* chown, stat */
33 #if PLATFORM_POSIX_VERSION < 200809L
34 # include <utime.h> /* utime */
35 #else
36 # include <fcntl.h> /* AT_FDCWD */
37 # include <sys/stat.h> /* utimensat */
38 #endif
39 #endif
40 #include <time.h> /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */
41 #include "mem.h" /* U32, U64 */
42
43 /*-************************************************************
44 * Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
45 ***************************************************************/
46 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
47 # define UTIL_fseek _fseeki64
48 #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
49 # define UTIL_fseek fseeko
50 #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
51 # define UTIL_fseek fseeko64
52 #else
53 # define UTIL_fseek fseek
54 #endif
55
56
57 /*-*************************************************
58 * Sleep & priority functions: Windows - Posix - others
59 ***************************************************/
60 #if defined(_WIN32)
61 # include <windows.h>
62 # define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
63 # define UTIL_sleep(s) Sleep(1000*s)
64 # define UTIL_sleepMilli(milli) Sleep(milli)
65
66 #elif PLATFORM_POSIX_VERSION > 0 /* Unix-like operating system */
67 # include <unistd.h> /* sleep */
68 # define UTIL_sleep(s) sleep(s)
69 # if ZSTD_NANOSLEEP_SUPPORT /* necessarily defined in platform.h */
70 # define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
71 # else
72 # define UTIL_sleepMilli(milli) /* disabled */
73 # endif
74 # if ZSTD_SETPRIORITY_SUPPORT
75 # include <sys/resource.h> /* setpriority */
76 # define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
77 # else
78 # define SET_REALTIME_PRIORITY /* disabled */
79 # endif
80
81 #else /* unknown non-unix operating systen */
82 # define UTIL_sleep(s) /* disabled */
83 # define UTIL_sleepMilli(milli) /* disabled */
84 # define SET_REALTIME_PRIORITY /* disabled */
85 #endif
86
87
88 /*-*************************************
89 * Constants
90 ***************************************/
91 #define LIST_SIZE_INCREASE (8*1024)
92
93
94 /*-****************************************
95 * Compiler specifics
96 ******************************************/
97 #if defined(__INTEL_COMPILER)
98 # pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
99 #endif
100 #if defined(__GNUC__)
101 # define UTIL_STATIC static __attribute__((unused))
102 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
103 # define UTIL_STATIC static inline
104 #elif defined(_MSC_VER)
105 # define UTIL_STATIC static __inline
106 #else
107 # define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
108 #endif
109
110
111 /*-****************************************
112 * Console log
113 ******************************************/
114 extern int g_utilDisplayLevel;
115 #define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__)
116 #define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
117
118
119 /*-****************************************
120 * File functions
121 ******************************************/
122 #if defined(_MSC_VER)
123 #define chmod _chmod
124 typedef struct __stat64 stat_t;
125 #else
126 typedef struct stat stat_t;
127 #endif
128
129
130 int UTIL_fileExist(const char* filename);
131 int UTIL_isRegularFile(const char* infilename);
132 int UTIL_setFileStat(const char* filename, stat_t* statbuf);
133 U32 UTIL_isDirectory(const char* infilename);
134 int UTIL_getFileStat(const char* infilename, stat_t* statbuf);
135 int UTIL_isSameFile(const char* file1, const char* file2);
136 int UTIL_compareStr(const void *p1, const void *p2);
137 int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]);
138 const char* UTIL_getFileExtension(const char* infilename);
139
140 #ifndef _MSC_VER
141 U32 UTIL_isFIFO(const char* infilename);
142 #endif
143 U32 UTIL_isLink(const char* infilename);
144 #define UTIL_FILESIZE_UNKNOWN ((U64)(-1))
145 U64 UTIL_getFileSize(const char* infilename);
146
147 U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles);
148
149 /*
150 * A modified version of realloc().
151 * If UTIL_realloc() fails the original block is freed.
152 */
UTIL_realloc(void * ptr,size_t size)153 UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size)
154 {
155 void *newptr = realloc(ptr, size);
156 if (newptr) return newptr;
157 free(ptr);
158 return NULL;
159 }
160
161 int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks);
162 #ifdef _WIN32
163 # define UTIL_HAS_CREATEFILELIST
164 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
165 # define UTIL_HAS_CREATEFILELIST
166 # include <dirent.h> /* opendir, readdir */
167 # include <string.h> /* strerror, memcpy */
168 #else
169 #endif /* #ifdef _WIN32 */
170
171 /*
172 * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
173 * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
174 * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
175 * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
176 */
177 const char**
178 UTIL_createFileList(const char **inputNames, unsigned inputNamesNb,
179 char** allocatedBuffer, unsigned* allocatedNamesNb,
180 int followLinks);
181
UTIL_freeFileList(const char ** filenameTable,char * allocatedBuffer)182 UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
183 {
184 if (allocatedBuffer) free(allocatedBuffer);
185 if (filenameTable) free((void*)filenameTable);
186 }
187
188 int UTIL_countPhysicalCores(void);
189
190 #if defined (__cplusplus)
191 }
192 #endif
193
194 #endif /* UTIL_H_MODULE */
195