diff options
Diffstat (limited to 'programs/util.h')
-rw-r--r-- | programs/util.h | 179 |
1 files changed, 110 insertions, 69 deletions
diff --git a/programs/util.h b/programs/util.h index 1f524f2934ad..8e187e4f2999 100644 --- a/programs/util.h +++ b/programs/util.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. + * Copyright (c) 2016-2020, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. * All rights reserved. * * This source code is licensed under both the BSD-style license (found in the @@ -20,37 +20,23 @@ extern "C" { * Dependencies ******************************************/ #include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */ -#include <stdlib.h> /* malloc, realloc, free */ #include <stddef.h> /* size_t, ptrdiff_t */ -#include <stdio.h> /* fprintf */ #include <sys/types.h> /* stat, utime */ #include <sys/stat.h> /* stat, chmod */ -#if defined(_WIN32) -# include <sys/utime.h> /* utime */ -# include <io.h> /* _chmod */ -#else -# include <unistd.h> /* chown, stat */ -#if PLATFORM_POSIX_VERSION < 200809L -# include <utime.h> /* utime */ -#else -# include <fcntl.h> /* AT_FDCWD */ -# include <sys/stat.h> /* utimensat */ -#endif -#endif -#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC, nanosleep */ -#include "mem.h" /* U32, U64 */ +#include "../lib/common/mem.h" /* U64 */ + /*-************************************************************ * Avoid fseek()'s 2GiB barrier with MSVC, macOS, *BSD, MinGW ***************************************************************/ #if defined(_MSC_VER) && (_MSC_VER >= 1400) -# define UTIL_fseek _fseeki64 +# define UTIL_fseek _fseeki64 #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) -# define UTIL_fseek fseeko64 +# define UTIL_fseek fseeko64 #else -# define UTIL_fseek fseek +# define UTIL_fseek fseek #endif @@ -85,12 +71,6 @@ extern "C" { #endif -/*-************************************* -* Constants -***************************************/ -#define LIST_SIZE_INCREASE (8*1024) - - /*-**************************************** * Compiler specifics ******************************************/ @@ -112,16 +92,14 @@ extern "C" { * Console log ******************************************/ extern int g_utilDisplayLevel; -#define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__) -#define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } } /*-**************************************** * File functions ******************************************/ #if defined(_MSC_VER) - #define chmod _chmod typedef struct __stat64 stat_t; + typedef int mode_t; #else typedef struct stat stat_t; #endif @@ -129,64 +107,127 @@ extern int g_utilDisplayLevel; int UTIL_fileExist(const char* filename); int UTIL_isRegularFile(const char* infilename); -int UTIL_setFileStat(const char* filename, stat_t* statbuf); -U32 UTIL_isDirectory(const char* infilename); -int UTIL_getFileStat(const char* infilename, stat_t* statbuf); +int UTIL_isDirectory(const char* infilename); int UTIL_isSameFile(const char* file1, const char* file2); -int UTIL_compareStr(const void *p1, const void *p2); int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]); -const char* UTIL_getFileExtension(const char* infilename); +int UTIL_isLink(const char* infilename); +int UTIL_isFIFO(const char* infilename); -#ifndef _MSC_VER -U32 UTIL_isFIFO(const char* infilename); -#endif -U32 UTIL_isLink(const char* infilename); #define UTIL_FILESIZE_UNKNOWN ((U64)(-1)) U64 UTIL_getFileSize(const char* infilename); +U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles); +int UTIL_getFileStat(const char* infilename, stat_t* statbuf); +int UTIL_setFileStat(const char* filename, stat_t* statbuf); +int UTIL_chmod(char const* filename, mode_t permissions); /*< like chmod, but avoid changing permission of /dev/null */ +int UTIL_compareStr(const void *p1, const void *p2); +const char* UTIL_getFileExtension(const char* infilename); -U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles); -/* - * A modified version of realloc(). - * If UTIL_realloc() fails the original block is freed. -*/ -UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size) -{ - void *newptr = realloc(ptr, size); - if (newptr) return newptr; - free(ptr); - return NULL; -} +/*-**************************************** + * Lists of Filenames + ******************************************/ + +typedef struct +{ const char** fileNames; + char* buf; /* fileNames are stored in this buffer (or are read-only) */ + size_t tableSize; /* nb of fileNames */ + size_t tableCapacity; +} FileNamesTable; + +/*! 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). + * Note: inputFileSize must be less than 50MB + */ +FileNamesTable* +UTIL_createFileNamesTable_fromFileName(const char* inputFileName); + +/*! UTIL_assembleFileNamesTable() : + * This function takes ownership of its arguments, @filenames and @buf, + * and store them inside the created object. + * note : this function never fails, + * it will rather exit() the program if internal allocation fails. + * @return : resulting FileNamesTable* object. + */ +FileNamesTable* +UTIL_assembleFileNamesTable(const char** filenames, size_t tableSize, char* buf); -int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks); +/*! UTIL_freeFileNamesTable() : + * This function is compatible with NULL argument and never fails. + */ +void UTIL_freeFileNamesTable(FileNamesTable* table); + +/*! UTIL_mergeFileNamesTable(): + * @return : FileNamesTable*, concatenation of @table1 and @table2 + * note: @table1 and @table2 are consumed (freed) by this operation + */ +FileNamesTable* +UTIL_mergeFileNamesTable(FileNamesTable* table1, FileNamesTable* table2); + + +/*! 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 + */ +void UTIL_expandFNT(FileNamesTable** fnt, int followLinks); + +/*! 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 + */ +FileNamesTable* +UTIL_createFNT_fromROTable(const char** filenames, size_t nbFilenames); + +/*! 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. + */ +FileNamesTable* UTIL_allocateFileNamesTable(size_t tableSize); + + +/*! UTIL_refFilename() : + * 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()`). + */ +void UTIL_refFilename(FileNamesTable* fnt, const char* filename); + + +/* 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 -# include <dirent.h> /* opendir, readdir */ -# include <string.h> /* strerror, memcpy */ #else -#endif /* #ifdef _WIN32 */ + /* do not define UTIL_HAS_CREATEFILELIST */ +#endif -/* - * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories, - * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb). - * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer) - * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called. +/*! 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 */ -const char** -UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, - char** allocatedBuffer, unsigned* allocatedNamesNb, - int followLinks); - -UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer) -{ - if (allocatedBuffer) free(allocatedBuffer); - if (filenameTable) free((void*)filenameTable); -} +FileNamesTable* +UTIL_createExpandedFNT(const char** filenames, size_t nbFilenames, int followLinks); + + +/*-**************************************** + * System + ******************************************/ int UTIL_countPhysicalCores(void); + #if defined (__cplusplus) } #endif |