xref: /freebsd/sys/contrib/zstd/programs/util.h (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
10c16b537SWarner Losh /*
2*5ff13fbcSAllan Jude  * Copyright (c) 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 */
230c16b537SWarner Losh #include <stddef.h>       /* size_t, ptrdiff_t */
240c16b537SWarner Losh #include <sys/types.h>    /* stat, utime */
250f743729SConrad Meyer #include <sys/stat.h>     /* stat, chmod */
2637f1f268SConrad Meyer #include "../lib/common/mem.h"          /* U64 */
2737f1f268SConrad Meyer 
280c16b537SWarner Losh 
29a0483764SConrad Meyer /*-************************************************************
300f743729SConrad Meyer * Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
310c16b537SWarner Losh ***************************************************************/
320c16b537SWarner Losh #if defined(_MSC_VER) && (_MSC_VER >= 1400)
330c16b537SWarner Losh #  define UTIL_fseek _fseeki64
340c16b537SWarner Losh #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
350c16b537SWarner Losh #  define UTIL_fseek fseeko
360c16b537SWarner Losh #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
370c16b537SWarner Losh #  define UTIL_fseek fseeko64
380c16b537SWarner Losh #else
390c16b537SWarner Losh #  define UTIL_fseek fseek
400c16b537SWarner Losh #endif
410c16b537SWarner Losh 
420c16b537SWarner Losh 
430f743729SConrad Meyer /*-*************************************************
440f743729SConrad Meyer *  Sleep & priority functions: Windows - Posix - others
450f743729SConrad Meyer ***************************************************/
460c16b537SWarner Losh #if defined(_WIN32)
470c16b537SWarner Losh #  include <windows.h>
480c16b537SWarner Losh #  define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
490c16b537SWarner Losh #  define UTIL_sleep(s) Sleep(1000*s)
500c16b537SWarner Losh #  define UTIL_sleepMilli(milli) Sleep(milli)
510f743729SConrad Meyer 
520f743729SConrad Meyer #elif PLATFORM_POSIX_VERSION > 0 /* Unix-like operating system */
530f743729SConrad Meyer #  include <unistd.h>   /* sleep */
540c16b537SWarner Losh #  define UTIL_sleep(s) sleep(s)
550f743729SConrad Meyer #  if ZSTD_NANOSLEEP_SUPPORT   /* necessarily defined in platform.h */
560c16b537SWarner Losh #      define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
570c16b537SWarner Losh #  else
580c16b537SWarner Losh #      define UTIL_sleepMilli(milli) /* disabled */
590c16b537SWarner Losh #  endif
600f743729SConrad Meyer #  if ZSTD_SETPRIORITY_SUPPORT
610f743729SConrad Meyer #    include <sys/resource.h> /* setpriority */
620f743729SConrad Meyer #    define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
630c16b537SWarner Losh #  else
640c16b537SWarner Losh #    define SET_REALTIME_PRIORITY /* disabled */
650f743729SConrad Meyer #  endif
660f743729SConrad Meyer 
67*5ff13fbcSAllan Jude #else  /* unknown non-unix operating system */
680c16b537SWarner Losh #  define UTIL_sleep(s)          /* disabled */
690c16b537SWarner Losh #  define UTIL_sleepMilli(milli) /* disabled */
700f743729SConrad Meyer #  define SET_REALTIME_PRIORITY  /* disabled */
710c16b537SWarner Losh #endif
720c16b537SWarner Losh 
730c16b537SWarner Losh 
740c16b537SWarner Losh /*-****************************************
750c16b537SWarner Losh *  Compiler specifics
760c16b537SWarner Losh ******************************************/
770c16b537SWarner Losh #if defined(__INTEL_COMPILER)
780c16b537SWarner Losh #  pragma warning(disable : 177)    /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
790c16b537SWarner Losh #endif
800c16b537SWarner Losh #if defined(__GNUC__)
810c16b537SWarner Losh #  define UTIL_STATIC static __attribute__((unused))
820c16b537SWarner Losh #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
830c16b537SWarner Losh #  define UTIL_STATIC static inline
840c16b537SWarner Losh #elif defined(_MSC_VER)
850c16b537SWarner Losh #  define UTIL_STATIC static __inline
860c16b537SWarner Losh #else
870c16b537SWarner Losh #  define UTIL_STATIC static  /* this version may generate warnings for unused static functions; disable the relevant warning */
880c16b537SWarner Losh #endif
890c16b537SWarner Losh 
900c16b537SWarner Losh 
910c16b537SWarner Losh /*-****************************************
920c16b537SWarner Losh *  Console log
930c16b537SWarner Losh ******************************************/
94a0483764SConrad Meyer extern int g_utilDisplayLevel;
950c16b537SWarner Losh 
96f7cd7fe5SConrad Meyer /**
97f7cd7fe5SConrad Meyer  * Displays a message prompt and returns success (0) if first character from stdin
98f7cd7fe5SConrad Meyer  * matches any from acceptableLetters. Otherwise, returns failure (1) and displays abortMsg.
99f7cd7fe5SConrad Meyer  * If any of the inputs are stdin itself, then automatically return failure (1).
100f7cd7fe5SConrad Meyer  */
101f7cd7fe5SConrad Meyer int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg, const char* acceptableLetters, int hasStdinInput);
102f7cd7fe5SConrad Meyer 
1030c16b537SWarner Losh 
1040c16b537SWarner Losh /*-****************************************
1050c16b537SWarner Losh *  File functions
1060c16b537SWarner Losh ******************************************/
1070c16b537SWarner Losh #if defined(_MSC_VER)
1080c16b537SWarner Losh     typedef struct __stat64 stat_t;
10937f1f268SConrad Meyer     typedef int mode_t;
110f7cd7fe5SConrad Meyer #elif defined(__MINGW32__) && defined (__MSVCRT__)
111f7cd7fe5SConrad Meyer     typedef struct _stati64 stat_t;
1120c16b537SWarner Losh #else
1130c16b537SWarner Losh     typedef struct stat stat_t;
1140c16b537SWarner Losh #endif
1150c16b537SWarner Losh 
116f7cd7fe5SConrad Meyer #if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__) /* windows support */
117f7cd7fe5SConrad Meyer #define PATH_SEP '\\'
118f7cd7fe5SConrad Meyer #define STRDUP(s) _strdup(s)
119f7cd7fe5SConrad Meyer #else
120f7cd7fe5SConrad Meyer #define PATH_SEP '/'
121f7cd7fe5SConrad Meyer #include <libgen.h>
122f7cd7fe5SConrad Meyer #define STRDUP(s) strdup(s)
123f7cd7fe5SConrad Meyer #endif
1240c16b537SWarner Losh 
125*5ff13fbcSAllan Jude 
126f7cd7fe5SConrad Meyer /**
127f7cd7fe5SConrad Meyer  * Calls platform's equivalent of stat() on filename and writes info to statbuf.
128f7cd7fe5SConrad Meyer  * Returns success (1) or failure (0).
129f7cd7fe5SConrad Meyer  */
130f7cd7fe5SConrad Meyer int UTIL_stat(const char* filename, stat_t* statbuf);
131f7cd7fe5SConrad Meyer 
132f7cd7fe5SConrad Meyer /**
133f7cd7fe5SConrad Meyer  * Instead of getting a file's stats, this updates them with the info in the
134f7cd7fe5SConrad Meyer  * provided stat_t. Currently sets owner, group, atime, and mtime. Will only
135f7cd7fe5SConrad Meyer  * update this info for regular files.
136f7cd7fe5SConrad Meyer  */
137f7cd7fe5SConrad Meyer int UTIL_setFileStat(const char* filename, const stat_t* statbuf);
138f7cd7fe5SConrad Meyer 
139*5ff13fbcSAllan Jude /**
140*5ff13fbcSAllan Jude  * Set atime to now and mtime to the st_mtim in statbuf.
141*5ff13fbcSAllan Jude  *
142*5ff13fbcSAllan Jude  * Directly wraps utime() or utimensat(). Returns -1 on error.
143*5ff13fbcSAllan Jude  * Does not validate filename is valid.
144*5ff13fbcSAllan Jude  */
145*5ff13fbcSAllan Jude int UTIL_utime(const char* filename, const stat_t *statbuf);
146*5ff13fbcSAllan Jude 
147f7cd7fe5SConrad Meyer /*
148f7cd7fe5SConrad Meyer  * These helpers operate on a pre-populated stat_t, i.e., the result of
149f7cd7fe5SConrad Meyer  * calling one of the above functions.
150f7cd7fe5SConrad Meyer  */
151f7cd7fe5SConrad Meyer 
152f7cd7fe5SConrad Meyer int UTIL_isRegularFileStat(const stat_t* statbuf);
153f7cd7fe5SConrad Meyer int UTIL_isDirectoryStat(const stat_t* statbuf);
154f7cd7fe5SConrad Meyer int UTIL_isFIFOStat(const stat_t* statbuf);
155*5ff13fbcSAllan Jude int UTIL_isBlockDevStat(const stat_t* statbuf);
156f7cd7fe5SConrad Meyer U64 UTIL_getFileSizeStat(const stat_t* statbuf);
157f7cd7fe5SConrad Meyer 
158f7cd7fe5SConrad Meyer /**
159f7cd7fe5SConrad Meyer  * Like chmod(), but only modifies regular files. Provided statbuf may be NULL,
160f7cd7fe5SConrad Meyer  * in which case this function will stat() the file internally, in order to
161f7cd7fe5SConrad Meyer  * check whether it should be modified.
162f7cd7fe5SConrad Meyer  */
163f7cd7fe5SConrad Meyer int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions);
164f7cd7fe5SConrad Meyer 
165f7cd7fe5SConrad Meyer /*
166f7cd7fe5SConrad Meyer  * In the absence of a pre-existing stat result on the file in question, these
167f7cd7fe5SConrad Meyer  * functions will do a stat() call internally and then use that result to
168f7cd7fe5SConrad Meyer  * compute the needed information.
169f7cd7fe5SConrad Meyer  */
170f7cd7fe5SConrad Meyer 
171a0483764SConrad Meyer int UTIL_isRegularFile(const char* infilename);
17237f1f268SConrad Meyer int UTIL_isDirectory(const char* infilename);
1732b9c00cbSConrad Meyer int UTIL_isSameFile(const char* file1, const char* file2);
1749cbefe25SConrad Meyer int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]);
17537f1f268SConrad Meyer int UTIL_isLink(const char* infilename);
17637f1f268SConrad Meyer int UTIL_isFIFO(const char* infilename);
17719fcbaf1SConrad Meyer 
178052d3c12SConrad Meyer #define UTIL_FILESIZE_UNKNOWN  ((U64)(-1))
179a0483764SConrad Meyer U64 UTIL_getFileSize(const char* infilename);
18037f1f268SConrad Meyer U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles);
181f7cd7fe5SConrad Meyer 
182*5ff13fbcSAllan Jude /**
183*5ff13fbcSAllan Jude  * Take @size in bytes,
184*5ff13fbcSAllan Jude  * prepare the components to pretty-print it in a scaled way.
185*5ff13fbcSAllan Jude  * The components in the returned struct should be passed in
186*5ff13fbcSAllan Jude  * precision, value, suffix order to a "%.*f%s" format string.
187*5ff13fbcSAllan Jude  * Output policy is sensible to @g_utilDisplayLevel,
188*5ff13fbcSAllan Jude  * for verbose mode (@g_utilDisplayLevel >= 4),
189*5ff13fbcSAllan Jude  * does not scale down.
190*5ff13fbcSAllan Jude  */
191*5ff13fbcSAllan Jude typedef struct {
192*5ff13fbcSAllan Jude   double value;
193*5ff13fbcSAllan Jude   int precision;
194*5ff13fbcSAllan Jude   const char* suffix;
195*5ff13fbcSAllan Jude } UTIL_HumanReadableSize_t;
196*5ff13fbcSAllan Jude 
197*5ff13fbcSAllan Jude UTIL_HumanReadableSize_t UTIL_makeHumanReadableSize(U64 size);
198*5ff13fbcSAllan Jude 
19937f1f268SConrad Meyer int UTIL_compareStr(const void *p1, const void *p2);
20037f1f268SConrad Meyer const char* UTIL_getFileExtension(const char* infilename);
201f7cd7fe5SConrad Meyer void  UTIL_mirrorSourceFilesDirectories(const char** fileNamesTable, unsigned int nbFiles, const char *outDirName);
202f7cd7fe5SConrad Meyer char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName);
203f7cd7fe5SConrad Meyer 
2040c16b537SWarner Losh 
2050c16b537SWarner Losh 
20637f1f268SConrad Meyer /*-****************************************
20737f1f268SConrad Meyer  *  Lists of Filenames
20837f1f268SConrad Meyer  ******************************************/
20937f1f268SConrad Meyer 
21037f1f268SConrad Meyer typedef struct
21137f1f268SConrad Meyer {   const char** fileNames;
21237f1f268SConrad Meyer     char* buf;            /* fileNames are stored in this buffer (or are read-only) */
21337f1f268SConrad Meyer     size_t tableSize;     /* nb of fileNames */
21437f1f268SConrad Meyer     size_t tableCapacity;
21537f1f268SConrad Meyer } FileNamesTable;
21637f1f268SConrad Meyer 
21737f1f268SConrad Meyer /*! UTIL_createFileNamesTable_fromFileName() :
21837f1f268SConrad Meyer  *  read filenames from @inputFileName, and store them into returned object.
21937f1f268SConrad Meyer  * @return : a FileNamesTable*, or NULL in case of error (ex: @inputFileName doesn't exist).
22037f1f268SConrad Meyer  *  Note: inputFileSize must be less than 50MB
2210c16b537SWarner Losh  */
22237f1f268SConrad Meyer FileNamesTable*
22337f1f268SConrad Meyer UTIL_createFileNamesTable_fromFileName(const char* inputFileName);
2240c16b537SWarner Losh 
22537f1f268SConrad Meyer /*! UTIL_assembleFileNamesTable() :
22637f1f268SConrad Meyer  *  This function takes ownership of its arguments, @filenames and @buf,
22737f1f268SConrad Meyer  *  and store them inside the created object.
22837f1f268SConrad Meyer  *  note : this function never fails,
22937f1f268SConrad Meyer  *         it will rather exit() the program if internal allocation fails.
23037f1f268SConrad Meyer  * @return : resulting FileNamesTable* object.
23137f1f268SConrad Meyer  */
23237f1f268SConrad Meyer FileNamesTable*
23337f1f268SConrad Meyer UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf);
23437f1f268SConrad Meyer 
23537f1f268SConrad Meyer /*! UTIL_freeFileNamesTable() :
23637f1f268SConrad Meyer  *  This function is compatible with NULL argument and never fails.
23737f1f268SConrad Meyer  */
23837f1f268SConrad Meyer void UTIL_freeFileNamesTable(FileNamesTable* table);
23937f1f268SConrad Meyer 
24037f1f268SConrad Meyer /*! UTIL_mergeFileNamesTable():
24137f1f268SConrad Meyer  * @return : FileNamesTable*, concatenation of @table1 and @table2
24237f1f268SConrad Meyer  *  note: @table1 and @table2 are consumed (freed) by this operation
24337f1f268SConrad Meyer  */
24437f1f268SConrad Meyer FileNamesTable*
24537f1f268SConrad Meyer UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2);
24637f1f268SConrad Meyer 
24737f1f268SConrad Meyer 
24837f1f268SConrad Meyer /*! UTIL_expandFNT() :
24937f1f268SConrad Meyer  *  read names from @fnt, and expand those corresponding to directories
25037f1f268SConrad Meyer  *  update @fnt, now containing only file names,
25137f1f268SConrad Meyer  * @return : 0 in case of success, 1 if error
25237f1f268SConrad Meyer  *  note : in case of error, @fnt[0] is NULL
25337f1f268SConrad Meyer  */
25437f1f268SConrad Meyer void UTIL_expandFNT(FileNamesTable** fnt, int followLinks);
25537f1f268SConrad Meyer 
25637f1f268SConrad Meyer /*! UTIL_createFNT_fromROTable() :
25737f1f268SConrad Meyer  *  copy the @filenames pointer table inside the returned object.
25837f1f268SConrad Meyer  *  The names themselves are still stored in their original buffer, which must outlive the object.
25937f1f268SConrad Meyer  * @return : a FileNamesTable* object,
26037f1f268SConrad Meyer  *        or NULL in case of error
26137f1f268SConrad Meyer  */
26237f1f268SConrad Meyer FileNamesTable*
26337f1f268SConrad Meyer UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames);
26437f1f268SConrad Meyer 
26537f1f268SConrad Meyer /*! UTIL_allocateFileNamesTable() :
26637f1f268SConrad Meyer  *  Allocates a table of const char*, to insert read-only names later on.
26737f1f268SConrad Meyer  *  The created FileNamesTable* doesn't hold a buffer.
26837f1f268SConrad Meyer  * @return : FileNamesTable*, or NULL, if allocation fails.
26937f1f268SConrad Meyer  */
27037f1f268SConrad Meyer FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize);
27137f1f268SConrad Meyer 
27237f1f268SConrad Meyer 
27337f1f268SConrad Meyer /*! UTIL_refFilename() :
27437f1f268SConrad Meyer  *  Add a reference to read-only name into @fnt table.
27537f1f268SConrad Meyer  *  As @filename is only referenced, its lifetime must outlive @fnt.
27637f1f268SConrad Meyer  *  Internal table must be large enough to reference a new member,
27737f1f268SConrad Meyer  *  otherwise its UB (protected by an `assert()`).
27837f1f268SConrad Meyer  */
27937f1f268SConrad Meyer void UTIL_refFilename(FileNamesTable* fnt, const char* filename);
28037f1f268SConrad Meyer 
28137f1f268SConrad Meyer 
28237f1f268SConrad Meyer /* UTIL_createExpandedFNT() is only active if UTIL_HAS_CREATEFILELIST is defined.
28337f1f268SConrad Meyer  * Otherwise, UTIL_createExpandedFNT() is a shell function which does nothing
28437f1f268SConrad Meyer  * apart from displaying a warning message.
28537f1f268SConrad Meyer  */
2860c16b537SWarner Losh #ifdef _WIN32
2870c16b537SWarner Losh #  define UTIL_HAS_CREATEFILELIST
2880c16b537SWarner Losh #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
2890c16b537SWarner Losh #  define UTIL_HAS_CREATEFILELIST
290f7cd7fe5SConrad Meyer #  define UTIL_HAS_MIRRORFILELIST
2910c16b537SWarner Losh #else
29237f1f268SConrad Meyer    /* do not define UTIL_HAS_CREATEFILELIST */
29337f1f268SConrad Meyer #endif
2940c16b537SWarner Losh 
29537f1f268SConrad Meyer /*! UTIL_createExpandedFNT() :
29637f1f268SConrad Meyer  *  read names from @filenames, and expand those corresponding to directories.
29737f1f268SConrad Meyer  *  links are followed or not depending on @followLinks directive.
29837f1f268SConrad Meyer  * @return : an expanded FileNamesTable*, where each name is a file
29937f1f268SConrad Meyer  *        or NULL in case of error
3000c16b537SWarner Losh  */
30137f1f268SConrad Meyer FileNamesTable*
302*5ff13fbcSAllan Jude UTIL_createExpandedFNT(const char* const* filenames, size_t nbFilenames, int followLinks);
3030c16b537SWarner Losh 
304*5ff13fbcSAllan Jude #if defined(_WIN32) || defined(WIN32)
305*5ff13fbcSAllan Jude DWORD CountSetBits(ULONG_PTR bitMask);
306*5ff13fbcSAllan Jude #endif
30737f1f268SConrad Meyer 
30837f1f268SConrad Meyer /*-****************************************
30937f1f268SConrad Meyer  *  System
31037f1f268SConrad Meyer  ******************************************/
3110c16b537SWarner Losh 
312*5ff13fbcSAllan Jude int UTIL_countCores(int logical);
313*5ff13fbcSAllan Jude 
314a0483764SConrad Meyer int UTIL_countPhysicalCores(void);
3150c16b537SWarner Losh 
316*5ff13fbcSAllan Jude int UTIL_countLogicalCores(void);
31737f1f268SConrad Meyer 
3180c16b537SWarner Losh #if defined (__cplusplus)
3190c16b537SWarner Losh }
3200c16b537SWarner Losh #endif
3210c16b537SWarner Losh 
3220c16b537SWarner Losh #endif /* UTIL_H_MODULE */
323