xref: /freebsd/sys/contrib/zstd/programs/util.h (revision 9cbefe25d46756f342c7dd3d174d2d1103808f21)
10c16b537SWarner Losh /*
20c16b537SWarner Losh  * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
30c16b537SWarner Losh  * All rights reserved.
40c16b537SWarner Losh  *
50c16b537SWarner Losh  * This source code is licensed under both the BSD-style license (found in the
60c16b537SWarner Losh  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
70c16b537SWarner Losh  * in the COPYING file in the root directory of this source tree).
80c16b537SWarner Losh  * You may select, at your option, one of the above-listed licenses.
90c16b537SWarner Losh  */
100c16b537SWarner Losh 
110c16b537SWarner Losh #ifndef UTIL_H_MODULE
120c16b537SWarner Losh #define UTIL_H_MODULE
130c16b537SWarner Losh 
140c16b537SWarner Losh #if defined (__cplusplus)
150c16b537SWarner Losh extern "C" {
160c16b537SWarner Losh #endif
170c16b537SWarner Losh 
180c16b537SWarner Losh 
190c16b537SWarner Losh /*-****************************************
200c16b537SWarner Losh *  Dependencies
210c16b537SWarner Losh ******************************************/
220f743729SConrad Meyer #include "platform.h"     /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */
23a0483764SConrad Meyer #include <stdlib.h>       /* malloc, realloc, free */
240c16b537SWarner Losh #include <stddef.h>       /* size_t, ptrdiff_t */
250c16b537SWarner Losh #include <stdio.h>        /* fprintf */
260c16b537SWarner Losh #include <sys/types.h>    /* stat, utime */
270f743729SConrad Meyer #include <sys/stat.h>     /* stat, chmod */
28*9cbefe25SConrad Meyer #if defined(_WIN32)
290c16b537SWarner Losh #  include <sys/utime.h>  /* utime */
300c16b537SWarner Losh #  include <io.h>         /* _chmod */
310c16b537SWarner Losh #else
320c16b537SWarner Losh #  include <unistd.h>     /* chown, stat */
33*9cbefe25SConrad Meyer #if PLATFORM_POSIX_VERSION < 200809L
340c16b537SWarner Losh #  include <utime.h>      /* utime */
35*9cbefe25SConrad Meyer #else
36*9cbefe25SConrad Meyer #  include <fcntl.h>      /* AT_FDCWD */
37*9cbefe25SConrad Meyer #  include <sys/stat.h>   /* utimensat */
38*9cbefe25SConrad Meyer #endif
390c16b537SWarner Losh #endif
40052d3c12SConrad Meyer #include <time.h>         /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */
410c16b537SWarner Losh #include "mem.h"          /* U32, U64 */
420c16b537SWarner Losh 
43a0483764SConrad Meyer /*-************************************************************
440f743729SConrad Meyer * Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
450c16b537SWarner Losh ***************************************************************/
460c16b537SWarner Losh #if defined(_MSC_VER) && (_MSC_VER >= 1400)
470c16b537SWarner Losh #   define UTIL_fseek _fseeki64
480c16b537SWarner Losh #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
490c16b537SWarner Losh #  define UTIL_fseek fseeko
500c16b537SWarner Losh #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
510c16b537SWarner Losh #   define UTIL_fseek fseeko64
520c16b537SWarner Losh #else
530c16b537SWarner Losh #   define UTIL_fseek fseek
540c16b537SWarner Losh #endif
550c16b537SWarner Losh 
560c16b537SWarner Losh 
570f743729SConrad Meyer /*-*************************************************
580f743729SConrad Meyer *  Sleep & priority functions: Windows - Posix - others
590f743729SConrad Meyer ***************************************************/
600c16b537SWarner Losh #if defined(_WIN32)
610c16b537SWarner Losh #  include <windows.h>
620c16b537SWarner Losh #  define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
630c16b537SWarner Losh #  define UTIL_sleep(s) Sleep(1000*s)
640c16b537SWarner Losh #  define UTIL_sleepMilli(milli) Sleep(milli)
650f743729SConrad Meyer 
660f743729SConrad Meyer #elif PLATFORM_POSIX_VERSION > 0 /* Unix-like operating system */
670f743729SConrad Meyer #  include <unistd.h>   /* sleep */
680c16b537SWarner Losh #  define UTIL_sleep(s) sleep(s)
690f743729SConrad Meyer #  if ZSTD_NANOSLEEP_SUPPORT   /* necessarily defined in platform.h */
700c16b537SWarner Losh #      define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
710c16b537SWarner Losh #  else
720c16b537SWarner Losh #      define UTIL_sleepMilli(milli) /* disabled */
730c16b537SWarner Losh #  endif
740f743729SConrad Meyer #  if ZSTD_SETPRIORITY_SUPPORT
750f743729SConrad Meyer #    include <sys/resource.h> /* setpriority */
760f743729SConrad Meyer #    define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
770c16b537SWarner Losh #  else
780c16b537SWarner Losh #    define SET_REALTIME_PRIORITY /* disabled */
790f743729SConrad Meyer #  endif
800f743729SConrad Meyer 
810f743729SConrad Meyer #else  /* unknown non-unix operating systen */
820c16b537SWarner Losh #  define UTIL_sleep(s)          /* disabled */
830c16b537SWarner Losh #  define UTIL_sleepMilli(milli) /* disabled */
840f743729SConrad Meyer #  define SET_REALTIME_PRIORITY  /* disabled */
850c16b537SWarner Losh #endif
860c16b537SWarner Losh 
870c16b537SWarner Losh 
88a0483764SConrad Meyer /*-*************************************
890c16b537SWarner Losh *  Constants
900c16b537SWarner Losh ***************************************/
910c16b537SWarner Losh #define LIST_SIZE_INCREASE   (8*1024)
920c16b537SWarner Losh 
930c16b537SWarner Losh 
940c16b537SWarner Losh /*-****************************************
950c16b537SWarner Losh *  Compiler specifics
960c16b537SWarner Losh ******************************************/
970c16b537SWarner Losh #if defined(__INTEL_COMPILER)
980c16b537SWarner Losh #  pragma warning(disable : 177)    /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
990c16b537SWarner Losh #endif
1000c16b537SWarner Losh #if defined(__GNUC__)
1010c16b537SWarner Losh #  define UTIL_STATIC static __attribute__((unused))
1020c16b537SWarner Losh #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
1030c16b537SWarner Losh #  define UTIL_STATIC static inline
1040c16b537SWarner Losh #elif defined(_MSC_VER)
1050c16b537SWarner Losh #  define UTIL_STATIC static __inline
1060c16b537SWarner Losh #else
1070c16b537SWarner Losh #  define UTIL_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
1080c16b537SWarner Losh #endif
1090c16b537SWarner Losh 
1100c16b537SWarner Losh 
1110c16b537SWarner Losh /*-****************************************
1120c16b537SWarner Losh *  Console log
1130c16b537SWarner Losh ******************************************/
114a0483764SConrad Meyer extern int g_utilDisplayLevel;
1150c16b537SWarner Losh #define UTIL_DISPLAY(...)         fprintf(stderr, __VA_ARGS__)
1160c16b537SWarner Losh #define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
1170c16b537SWarner Losh 
1180c16b537SWarner Losh 
1190c16b537SWarner Losh /*-****************************************
1200c16b537SWarner Losh *  File functions
1210c16b537SWarner Losh ******************************************/
1220c16b537SWarner Losh #if defined(_MSC_VER)
1230c16b537SWarner Losh     #define chmod _chmod
1240c16b537SWarner Losh     typedef struct __stat64 stat_t;
1250c16b537SWarner Losh #else
1260c16b537SWarner Losh     typedef struct stat stat_t;
1270c16b537SWarner Losh #endif
1280c16b537SWarner Losh 
1290c16b537SWarner Losh 
130a0483764SConrad Meyer int UTIL_fileExist(const char* filename);
131a0483764SConrad Meyer int UTIL_isRegularFile(const char* infilename);
132a0483764SConrad Meyer int UTIL_setFileStat(const char* filename, stat_t* statbuf);
133a0483764SConrad Meyer U32 UTIL_isDirectory(const char* infilename);
134a0483764SConrad Meyer int UTIL_getFileStat(const char* infilename, stat_t* statbuf);
1352b9c00cbSConrad Meyer int UTIL_isSameFile(const char* file1, const char* file2);
136*9cbefe25SConrad Meyer int UTIL_compareStr(const void *p1, const void *p2);
137*9cbefe25SConrad Meyer int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]);
138*9cbefe25SConrad Meyer const char* UTIL_getFileExtension(const char* infilename);
13919fcbaf1SConrad Meyer 
140*9cbefe25SConrad Meyer #ifndef _MSC_VER
141*9cbefe25SConrad Meyer U32 UTIL_isFIFO(const char* infilename);
142*9cbefe25SConrad Meyer #endif
143a0483764SConrad Meyer U32 UTIL_isLink(const char* infilename);
144052d3c12SConrad Meyer #define UTIL_FILESIZE_UNKNOWN  ((U64)(-1))
145a0483764SConrad Meyer U64 UTIL_getFileSize(const char* infilename);
1460c16b537SWarner Losh 
147a0483764SConrad Meyer U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles);
1480c16b537SWarner Losh 
1490c16b537SWarner Losh /*
1500c16b537SWarner Losh  * A modified version of realloc().
1510c16b537SWarner Losh  * If UTIL_realloc() fails the original block is freed.
1520c16b537SWarner Losh */
1530c16b537SWarner Losh UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size)
1540c16b537SWarner Losh {
1550c16b537SWarner Losh     void *newptr = realloc(ptr, size);
1560c16b537SWarner Losh     if (newptr) return newptr;
1570c16b537SWarner Losh     free(ptr);
1580c16b537SWarner Losh     return NULL;
1590c16b537SWarner Losh }
1600c16b537SWarner Losh 
161a0483764SConrad Meyer int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks);
1620c16b537SWarner Losh #ifdef _WIN32
1630c16b537SWarner Losh #  define UTIL_HAS_CREATEFILELIST
1640c16b537SWarner Losh #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
1650c16b537SWarner Losh #  define UTIL_HAS_CREATEFILELIST
1660c16b537SWarner Losh #  include <dirent.h>       /* opendir, readdir */
1670c16b537SWarner Losh #  include <string.h>       /* strerror, memcpy */
1680c16b537SWarner Losh #else
1690c16b537SWarner Losh #endif /* #ifdef _WIN32 */
1700c16b537SWarner Losh 
1710c16b537SWarner Losh /*
1720c16b537SWarner Losh  * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
1730c16b537SWarner Losh  *                       and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
1740c16b537SWarner Losh  * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
1750c16b537SWarner Losh  * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
1760c16b537SWarner Losh  */
177a0483764SConrad Meyer const char**
1780f743729SConrad Meyer UTIL_createFileList(const char **inputNames, unsigned inputNamesNb,
1790f743729SConrad Meyer                     char** allocatedBuffer, unsigned* allocatedNamesNb,
180a0483764SConrad Meyer                     int followLinks);
1810c16b537SWarner Losh 
1820c16b537SWarner Losh UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
1830c16b537SWarner Losh {
1840c16b537SWarner Losh     if (allocatedBuffer) free(allocatedBuffer);
1850c16b537SWarner Losh     if (filenameTable) free((void*)filenameTable);
1860c16b537SWarner Losh }
1870c16b537SWarner Losh 
188a0483764SConrad Meyer int UTIL_countPhysicalCores(void);
1890c16b537SWarner Losh 
1900c16b537SWarner Losh #if defined (__cplusplus)
1910c16b537SWarner Losh }
1920c16b537SWarner Losh #endif
1930c16b537SWarner Losh 
1940c16b537SWarner Losh #endif /* UTIL_H_MODULE */
195