2017-08-18 16:52:05 -07:00
|
|
|
/*
|
2021-03-29 14:23:36 -07:00
|
|
|
* Copyright (c) Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
|
2016-12-21 06:08:44 -08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-08-18 16:52:05 -07:00
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-09-08 00:09:23 -07:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2016-12-21 06:08:44 -08:00
|
|
|
*/
|
2016-05-09 07:19:25 -07:00
|
|
|
|
2016-04-28 03:23:33 -07:00
|
|
|
#ifndef UTIL_H_MODULE
|
|
|
|
#define UTIL_H_MODULE
|
|
|
|
|
|
|
|
#if defined (__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-05-05 02:53:42 -07:00
|
|
|
|
2016-04-28 03:23:33 -07:00
|
|
|
/*-****************************************
|
|
|
|
* Dependencies
|
|
|
|
******************************************/
|
2018-10-03 15:34:41 -07:00
|
|
|
#include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */
|
2016-12-21 04:23:34 -08:00
|
|
|
#include <stddef.h> /* size_t, ptrdiff_t */
|
|
|
|
#include <sys/types.h> /* stat, utime */
|
2021-02-11 12:50:13 -08:00
|
|
|
#include <sys/stat.h> /* stat, chmod, umask */
|
2020-05-01 13:20:40 -07:00
|
|
|
#include "../lib/common/mem.h" /* U64 */
|
2016-04-28 03:23:33 -07:00
|
|
|
|
2019-11-26 15:44:33 -08:00
|
|
|
|
2018-10-11 17:34:47 -07:00
|
|
|
/*-************************************************************
|
2018-06-09 12:31:17 -07:00
|
|
|
* Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW
|
2017-02-15 08:03:16 -08:00
|
|
|
***************************************************************/
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
|
2019-11-26 15:44:33 -08:00
|
|
|
# define UTIL_fseek _fseeki64
|
2017-02-15 08:03:16 -08:00
|
|
|
#elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
|
|
|
|
# define UTIL_fseek fseeko
|
|
|
|
#elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
|
2019-11-26 15:44:33 -08:00
|
|
|
# define UTIL_fseek fseeko64
|
2017-02-15 08:03:16 -08:00
|
|
|
#else
|
2019-11-26 15:44:33 -08:00
|
|
|
# define UTIL_fseek fseek
|
2017-02-15 08:03:16 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2018-10-03 14:54:33 -07:00
|
|
|
/*-*************************************************
|
|
|
|
* Sleep & priority functions: Windows - Posix - others
|
|
|
|
***************************************************/
|
2016-05-04 15:25:38 -07:00
|
|
|
#if defined(_WIN32)
|
2016-04-28 04:16:01 -07:00
|
|
|
# include <windows.h>
|
2017-02-07 07:36:19 -08:00
|
|
|
# define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
|
2016-04-28 04:16:01 -07:00
|
|
|
# define UTIL_sleep(s) Sleep(1000*s)
|
|
|
|
# define UTIL_sleepMilli(milli) Sleep(milli)
|
2018-10-03 14:54:33 -07:00
|
|
|
|
|
|
|
#elif PLATFORM_POSIX_VERSION > 0 /* Unix-like operating system */
|
|
|
|
# include <unistd.h> /* sleep */
|
2016-05-04 15:25:38 -07:00
|
|
|
# define UTIL_sleep(s) sleep(s)
|
2018-10-03 15:34:41 -07:00
|
|
|
# if ZSTD_NANOSLEEP_SUPPORT /* necessarily defined in platform.h */
|
2016-05-05 02:53:42 -07:00
|
|
|
# define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
|
|
|
|
# else
|
|
|
|
# define UTIL_sleepMilli(milli) /* disabled */
|
|
|
|
# endif
|
2018-10-03 14:54:33 -07:00
|
|
|
# if ZSTD_SETPRIORITY_SUPPORT
|
|
|
|
# include <sys/resource.h> /* setpriority */
|
|
|
|
# define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
|
|
|
|
# else
|
|
|
|
# define SET_REALTIME_PRIORITY /* disabled */
|
|
|
|
# endif
|
|
|
|
|
|
|
|
#else /* unknown non-unix operating systen */
|
2016-05-04 15:25:38 -07:00
|
|
|
# define UTIL_sleep(s) /* disabled */
|
2016-04-28 04:16:01 -07:00
|
|
|
# define UTIL_sleepMilli(milli) /* disabled */
|
2018-10-03 14:54:33 -07:00
|
|
|
# define SET_REALTIME_PRIORITY /* disabled */
|
2016-04-28 04:16:01 -07:00
|
|
|
#endif
|
|
|
|
|
2016-05-04 15:25:38 -07:00
|
|
|
|
2016-12-21 00:04:59 -08:00
|
|
|
/*-****************************************
|
|
|
|
* Compiler specifics
|
|
|
|
******************************************/
|
2016-12-21 04:23:34 -08:00
|
|
|
#if defined(__INTEL_COMPILER)
|
|
|
|
# pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
|
|
|
|
#endif
|
2016-12-21 00:04:59 -08:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
# define UTIL_STATIC static __attribute__((unused))
|
|
|
|
#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
|
|
|
|
# define UTIL_STATIC static inline
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
# define UTIL_STATIC static __inline
|
|
|
|
#else
|
|
|
|
# define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-08-31 16:58:47 -07:00
|
|
|
/*-****************************************
|
|
|
|
* Console log
|
|
|
|
******************************************/
|
2018-10-11 16:51:29 -07:00
|
|
|
extern int g_utilDisplayLevel;
|
2017-08-31 16:58:47 -07:00
|
|
|
|
2020-08-25 08:25:49 -07:00
|
|
|
/**
|
|
|
|
* Displays a message prompt and returns success (0) if first character from stdin
|
|
|
|
* matches any from acceptableLetters. Otherwise, returns failure (1) and displays abortMsg.
|
2020-09-24 12:58:06 -07:00
|
|
|
* If any of the inputs are stdin itself, then automatically return failure (1).
|
2020-08-25 08:25:49 -07:00
|
|
|
*/
|
2020-09-24 12:58:06 -07:00
|
|
|
int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg, const char* acceptableLetters, int hasStdinInput);
|
2020-08-25 08:25:49 -07:00
|
|
|
|
2017-08-31 16:58:47 -07:00
|
|
|
|
2016-05-04 15:25:38 -07:00
|
|
|
/*-****************************************
|
|
|
|
* File functions
|
|
|
|
******************************************/
|
2016-11-02 06:08:07 -07:00
|
|
|
#if defined(_MSC_VER)
|
2017-03-29 18:51:58 -07:00
|
|
|
typedef struct __stat64 stat_t;
|
2019-11-25 13:59:35 -08:00
|
|
|
typedef int mode_t;
|
2020-08-04 21:09:29 -07:00
|
|
|
#elif defined(__MINGW32__) && defined (__MSVCRT__)
|
|
|
|
typedef struct _stati64 stat_t;
|
2016-11-02 06:08:07 -07:00
|
|
|
#else
|
|
|
|
typedef struct stat stat_t;
|
|
|
|
#endif
|
|
|
|
|
2020-06-19 19:35:51 -07:00
|
|
|
#if defined(_MSC_VER) || defined(__MINGW32__) || defined (__MSVCRT__) /* windows support */
|
|
|
|
#define PATH_SEP '\\'
|
|
|
|
#define STRDUP(s) _strdup(s)
|
|
|
|
#else
|
|
|
|
#define PATH_SEP '/'
|
|
|
|
#include <libgen.h>
|
|
|
|
#define STRDUP(s) strdup(s)
|
|
|
|
#endif
|
2016-11-02 06:08:07 -07:00
|
|
|
|
2020-08-04 21:09:29 -07:00
|
|
|
/**
|
|
|
|
* Calls platform's equivalent of stat() on filename and writes info to statbuf.
|
|
|
|
* Returns success (1) or failure (0).
|
|
|
|
*/
|
|
|
|
int UTIL_stat(const char* filename, stat_t* statbuf);
|
2020-08-10 12:16:14 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instead of getting a file's stats, this updates them with the info in the
|
|
|
|
* provided stat_t. Currently sets owner, group, atime, and mtime. Will only
|
|
|
|
* update this info for regular files.
|
|
|
|
*/
|
|
|
|
int UTIL_setFileStat(const char* filename, const stat_t* statbuf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These helpers operate on a pre-populated stat_t, i.e., the result of
|
|
|
|
* calling one of the above functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int UTIL_isRegularFileStat(const stat_t* statbuf);
|
|
|
|
int UTIL_isDirectoryStat(const stat_t* statbuf);
|
|
|
|
int UTIL_isFIFOStat(const stat_t* statbuf);
|
|
|
|
U64 UTIL_getFileSizeStat(const stat_t* statbuf);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like chmod(), but only modifies regular files. Provided statbuf may be NULL,
|
|
|
|
* in which case this function will stat() the file internally, in order to
|
|
|
|
* check whether it should be modified.
|
|
|
|
*/
|
|
|
|
int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions);
|
|
|
|
|
2021-02-11 12:50:13 -08:00
|
|
|
/**
|
|
|
|
* Wraps umask(). Does nothing when the platform doesn't have that concept.
|
|
|
|
*/
|
|
|
|
int UTIL_umask(int mode);
|
|
|
|
|
2020-08-10 12:16:14 -07:00
|
|
|
/*
|
|
|
|
* In the absence of a pre-existing stat result on the file in question, these
|
|
|
|
* functions will do a stat() call internally and then use that result to
|
|
|
|
* compute the needed information.
|
|
|
|
*/
|
|
|
|
|
2018-10-11 15:07:12 -07:00
|
|
|
int UTIL_isRegularFile(const char* infilename);
|
2019-11-25 13:45:22 -08:00
|
|
|
int UTIL_isDirectory(const char* infilename);
|
2019-03-23 19:04:56 -07:00
|
|
|
int UTIL_isSameFile(const char* file1, const char* file2);
|
2019-10-28 18:21:47 -07:00
|
|
|
int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]);
|
2019-11-25 13:45:22 -08:00
|
|
|
int UTIL_isLink(const char* infilename);
|
|
|
|
int UTIL_isFIFO(const char* infilename);
|
2018-10-11 12:52:19 -07:00
|
|
|
|
|
|
|
#define UTIL_FILESIZE_UNKNOWN ((U64)(-1))
|
|
|
|
U64 UTIL_getFileSize(const char* infilename);
|
2019-11-26 14:53:37 -08:00
|
|
|
U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles);
|
2020-08-10 12:16:14 -07:00
|
|
|
|
2019-11-25 13:45:22 -08:00
|
|
|
int UTIL_compareStr(const void *p1, const void *p2);
|
|
|
|
const char* UTIL_getFileExtension(const char* infilename);
|
2020-06-19 19:35:51 -07:00
|
|
|
void UTIL_mirrorSourceFilesDirectories(const char** fileNamesTable, unsigned int nbFiles, const char *outDirName);
|
|
|
|
char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName);
|
|
|
|
|
2019-10-14 23:49:13 -07:00
|
|
|
|
2019-11-05 14:59:45 -08:00
|
|
|
|
|
|
|
/*-****************************************
|
|
|
|
* Lists of Filenames
|
|
|
|
******************************************/
|
|
|
|
|
2019-10-14 23:49:13 -07:00
|
|
|
typedef struct
|
2019-11-26 15:44:33 -08:00
|
|
|
{ const char** fileNames;
|
2019-11-05 17:02:43 -08:00
|
|
|
char* buf; /* fileNames are stored in this buffer (or are read-only) */
|
|
|
|
size_t tableSize; /* nb of fileNames */
|
|
|
|
size_t tableCapacity;
|
2019-10-14 23:49:13 -07:00
|
|
|
} FileNamesTable;
|
|
|
|
|
2019-10-28 15:20:40 -07:00
|
|
|
/*! UTIL_createFileNamesTable_fromFileName() :
|
|
|
|
* read filenames from @inputFileName, and store them into returned object.
|
|
|
|
* @return : a FileNamesTable*, or NULL in case of error (ex: @inputFileName doesn't exist).
|
2019-10-25 17:34:29 -07:00
|
|
|
* Note: inputFileSize must be less than 50MB
|
2019-10-14 23:49:13 -07:00
|
|
|
*/
|
2019-10-25 17:34:29 -07:00
|
|
|
FileNamesTable*
|
|
|
|
UTIL_createFileNamesTable_fromFileName(const char* inputFileName);
|
2019-10-14 23:49:13 -07:00
|
|
|
|
2019-11-25 15:34:55 -08:00
|
|
|
/*! UTIL_assembleFileNamesTable() :
|
2019-10-28 15:20:40 -07:00
|
|
|
* This function takes ownership of its arguments, @filenames and @buf,
|
|
|
|
* and store them inside the created object.
|
2019-11-26 15:44:33 -08:00
|
|
|
* note : this function never fails,
|
|
|
|
* it will rather exit() the program if internal allocation fails.
|
|
|
|
* @return : resulting FileNamesTable* object.
|
2019-10-24 06:42:37 -07:00
|
|
|
*/
|
|
|
|
FileNamesTable*
|
2019-11-25 15:34:55 -08:00
|
|
|
UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf);
|
2019-10-24 06:42:37 -07:00
|
|
|
|
2019-10-25 17:34:29 -07:00
|
|
|
/*! UTIL_freeFileNamesTable() :
|
|
|
|
* This function is compatible with NULL argument and never fails.
|
2019-10-14 23:49:13 -07:00
|
|
|
*/
|
|
|
|
void UTIL_freeFileNamesTable(FileNamesTable* table);
|
|
|
|
|
2019-11-06 09:10:05 -08:00
|
|
|
/*! UTIL_mergeFileNamesTable():
|
2019-10-25 17:34:29 -07:00
|
|
|
* @return : FileNamesTable*, concatenation of @table1 and @table2
|
|
|
|
* note: @table1 and @table2 are consumed (freed) by this operation
|
2019-10-14 23:49:13 -07:00
|
|
|
*/
|
2019-10-25 17:34:29 -07:00
|
|
|
FileNamesTable*
|
2019-11-06 09:10:05 -08:00
|
|
|
UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2);
|
2018-10-11 12:52:19 -07:00
|
|
|
|
2016-05-04 15:25:38 -07:00
|
|
|
|
2019-11-06 09:10:05 -08:00
|
|
|
/*! UTIL_expandFNT() :
|
|
|
|
* read names from @fnt, and expand those corresponding to directories
|
|
|
|
* update @fnt, now containing only file names,
|
|
|
|
* @return : 0 in case of success, 1 if error
|
|
|
|
* note : in case of error, @fnt[0] is NULL
|
2019-11-05 17:02:43 -08:00
|
|
|
*/
|
2019-11-06 09:10:05 -08:00
|
|
|
void UTIL_expandFNT(FileNamesTable** fnt, int followLinks);
|
|
|
|
|
2019-11-06 14:42:13 -08:00
|
|
|
/*! UTIL_createFNT_fromROTable() :
|
|
|
|
* copy the @filenames pointer table inside the returned object.
|
|
|
|
* The names themselves are still stored in their original buffer, which must outlive the object.
|
|
|
|
* @return : a FileNamesTable* object,
|
|
|
|
* or NULL in case of error
|
|
|
|
*/
|
2019-11-26 15:44:33 -08:00
|
|
|
FileNamesTable*
|
|
|
|
UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames);
|
2019-11-05 17:02:43 -08:00
|
|
|
|
|
|
|
/*! UTIL_allocateFileNamesTable() :
|
|
|
|
* Allocates a table of const char*, to insert read-only names later on.
|
|
|
|
* The created FileNamesTable* doesn't hold a buffer.
|
|
|
|
* @return : FileNamesTable*, or NULL, if allocation fails.
|
2016-05-13 01:52:02 -07:00
|
|
|
*/
|
2019-11-05 17:02:43 -08:00
|
|
|
FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize);
|
2016-05-04 15:25:38 -07:00
|
|
|
|
2019-11-05 17:02:43 -08:00
|
|
|
|
|
|
|
/*! UTIL_refFilename() :
|
2019-11-26 14:48:23 -08:00
|
|
|
* Add a reference to read-only name into @fnt table.
|
|
|
|
* As @filename is only referenced, its lifetime must outlive @fnt.
|
|
|
|
* Internal table must be large enough to reference a new member,
|
|
|
|
* otherwise its UB (protected by an `assert()`).
|
2019-11-05 17:02:43 -08:00
|
|
|
*/
|
|
|
|
void UTIL_refFilename(FileNamesTable* fnt, const char* filename);
|
2019-11-05 14:59:45 -08:00
|
|
|
|
|
|
|
|
2019-11-26 15:44:33 -08:00
|
|
|
/* UTIL_createExpandedFNT() is only active if UTIL_HAS_CREATEFILELIST is defined.
|
|
|
|
* Otherwise, UTIL_createExpandedFNT() is a shell function which does nothing
|
|
|
|
* apart from displaying a warning message.
|
|
|
|
*/
|
|
|
|
#ifdef _WIN32
|
|
|
|
# define UTIL_HAS_CREATEFILELIST
|
|
|
|
#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
|
|
|
|
# define UTIL_HAS_CREATEFILELIST
|
2020-06-19 19:35:51 -07:00
|
|
|
# define UTIL_HAS_MIRRORFILELIST
|
2019-11-26 15:44:33 -08:00
|
|
|
#else
|
|
|
|
/* do not define UTIL_HAS_CREATEFILELIST */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*! UTIL_createExpandedFNT() :
|
|
|
|
* read names from @filenames, and expand those corresponding to directories.
|
|
|
|
* links are followed or not depending on @followLinks directive.
|
|
|
|
* @return : an expanded FileNamesTable*, where each name is a file
|
|
|
|
* or NULL in case of error
|
|
|
|
*/
|
|
|
|
FileNamesTable*
|
2021-03-25 10:38:56 -07:00
|
|
|
UTIL_createExpandedFNT(const char* const* filenames, size_t nbFilenames, int followLinks);
|
2019-11-26 15:44:33 -08:00
|
|
|
|
|
|
|
|
2019-11-05 14:59:45 -08:00
|
|
|
/*-****************************************
|
|
|
|
* System
|
|
|
|
******************************************/
|
2016-05-04 15:25:38 -07:00
|
|
|
|
2018-10-11 17:34:47 -07:00
|
|
|
int UTIL_countPhysicalCores(void);
|
2016-05-04 15:25:38 -07:00
|
|
|
|
2019-11-05 14:59:45 -08:00
|
|
|
|
2016-04-28 03:23:33 -07:00
|
|
|
#if defined (__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* UTIL_H_MODULE */
|