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