Compress-Stream-Zstd

 view release on metacpan or  search on metacpan

ext/zstd/programs/util.h  view on Meta::CPAN

#else  /* unknown non-unix operating system */
#  define UTIL_sleep(s)          /* disabled */
#  define UTIL_sleepMilli(milli) /* disabled */
#  define SET_REALTIME_PRIORITY  /* disabled */
#endif


/*-****************************************
*  Compiler specifics
******************************************/
#if defined(__INTEL_COMPILER)
#  pragma warning(disable : 177)    /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
#endif
#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


/*-****************************************
*  Console log
******************************************/
extern int g_utilDisplayLevel;

/**
 * Displays a message prompt and returns success (0) if first character from stdin
 * matches any from acceptableLetters. Otherwise, returns failure (1) and displays abortMsg.
 * If any of the inputs are stdin itself, then automatically return failure (1).
 */
int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg, const char* acceptableLetters, int hasStdinInput);


/*-****************************************
*  File functions
******************************************/
#if defined(_MSC_VER)
    typedef struct __stat64 stat_t;
    typedef int mode_t;
#elif defined(__MINGW32__) && defined (__MSVCRT__)
    typedef struct _stati64 stat_t;
#else
    typedef struct stat stat_t;
#endif

#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


/**
 * Calls platform's equivalent of stat() on filename and writes info to statbuf.
 * Returns success (1) or failure (0).
 *
 * UTIL_fstat() is like UTIL_stat() but takes an optional fd that refers to the
 * file in question. It turns out that this can be meaningfully faster. If fd is
 * -1, behaves just like UTIL_stat() (i.e., falls back to using the filename).
 */
int UTIL_stat(const char* filename, stat_t* statbuf);
int UTIL_fstat(const int fd, const char* filename, stat_t* statbuf);

/**
 * 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.
 *
 * UTIL_setFDStat() also takes an fd, and will preferentially use that to
 * indicate which file to modify, If fd is -1, it will fall back to using the
 * filename.
 */
int UTIL_setFileStat(const char* filename, const stat_t* statbuf);
int UTIL_setFDStat(const int fd, const char* filename, const stat_t* statbuf);

/**
 * Set atime to now and mtime to the st_mtim in statbuf.
 *
 * Directly wraps utime() or utimensat(). Returns -1 on error.
 * Does not validate filename is valid.
 */
int UTIL_utime(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);
int UTIL_isBlockDevStat(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.
 *
 * If fd is -1, fd is ignored and the filename is used.
 */
int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions);
int UTIL_fchmod(const int fd, char const* filename, const stat_t* statbuf, mode_t permissions);

/*
 * 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.
 */

int UTIL_isRegularFile(const char* infilename);
int UTIL_isDirectory(const char* infilename);
int UTIL_isSameFile(const char* file1, const char* file2);
int UTIL_isSameFileStat(const char* file1, const char* file2, const stat_t* file1Stat, const stat_t* file2Stat);
int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]);
int UTIL_isLink(const char* infilename);
int UTIL_isFIFO(const char* infilename);

/**
 * Returns with the given file descriptor is a console.
 * Allows faking whether stdin/stdout/stderr is a console
 * using UTIL_fake*IsConsole().
 */
int UTIL_isConsole(FILE* file);

/**
 * Pretends that stdin/stdout/stderr is a console for testing.
 */
void UTIL_fakeStdinIsConsole(void);
void UTIL_fakeStdoutIsConsole(void);
void UTIL_fakeStderrIsConsole(void);

/**
 * Emit traces for functions that read, or modify file metadata.
 */
void UTIL_traceFileStat(void);

#define UTIL_FILESIZE_UNKNOWN  ((U64)(-1))
U64 UTIL_getFileSize(const char* infilename);
U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles);

/**
 * Take @size in bytes,
 * prepare the components to pretty-print it in a scaled way.
 * The components in the returned struct should be passed in
 * precision, value, suffix order to a "%.*f%s" format string.
 * Output policy is sensible to @g_utilDisplayLevel,
 * for verbose mode (@g_utilDisplayLevel >= 4),
 * does not scale down.
 */
typedef struct {
  double value;
  int precision;
  const char* suffix;
} UTIL_HumanReadableSize_t;

UTIL_HumanReadableSize_t UTIL_makeHumanReadableSize(U64 size);

int UTIL_compareStr(const void *p1, const void *p2);
const char* UTIL_getFileExtension(const char* infilename);
void  UTIL_mirrorSourceFilesDirectories(const char** fileNamesTable, unsigned int nbFiles, const char *outDirName);
char* UTIL_createMirroredDestDirName(const char* srcFileName, const char* outDirRootName);



/*-****************************************
 *  Lists of Filenames



( run in 0.588 second using v1.01-cache-2.11-cpan-39bf76dae61 )