1
0
mirror of https://github.com/facebook/zstd.git synced 2026-01-06 11:21:19 +03:00

Introduce Variants of Various UTIL Functions that Take Pre-Populated stat_t Structs

Instead of calling `stat()`, these functions accept the result of a previous
`stat()` call on the file in question, which will allow us to make multiple
decisions around a file without redundant `stat()` calls.
This commit is contained in:
W. Felix Handte
2020-08-05 01:00:06 -04:00
parent b6e24bc4dc
commit 44fa052599
2 changed files with 50 additions and 21 deletions

View File

@@ -122,24 +122,23 @@ int UTIL_isRegularFile(const char* infilename)
return UTIL_statFile(infilename, &statbuf); /* Only need to know whether it is a regular file */
}
int UTIL_isRegularFileStat(const stat_t* statbuf)
{
#if defined(_MSC_VER)
return (statbuf->st_mode & S_IFREG) != 0;
#else
return S_ISREG(statbuf->st_mode) != 0;
#endif
}
int UTIL_statFile(const char* infilename, stat_t *statbuf)
{
const int r = UTIL_stat(infilename, statbuf);
#if defined(_MSC_VER)
return r && (statbuf->st_mode & S_IFREG);
#else
return r && S_ISREG(statbuf->st_mode);
#endif
return UTIL_stat(infilename, statbuf) && UTIL_isRegularFileStat(statbuf);
}
int UTIL_statDir(const char* infilename, stat_t *statbuf)
{
const int r = UTIL_stat(infilename, statbuf);
#if defined(_MSC_VER)
return r && (statbuf->st_mode & _S_IFDIR);
#else
return r && S_ISDIR(statbuf->st_mode);
#endif
return UTIL_stat(infilename, statbuf) && UTIL_isDirectoryStat(statbuf);
}
/* like chmod, but avoid changing permission of /dev/null */
@@ -193,6 +192,15 @@ int UTIL_isDirectory(const char* infilename)
return UTIL_statDir(infilename, &statbuf);
}
int UTIL_isDirectoryStat(const stat_t* statbuf)
{
#if defined(_MSC_VER)
return (statbuf->st_mode & _S_IFDIR) != 0;
#else
return S_ISDIR(statbuf->st_mode) != 0;
#endif
}
int UTIL_compareStr(const void *p1, const void *p2) {
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
@@ -223,13 +231,23 @@ int UTIL_isFIFO(const char* infilename)
/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
#if PLATFORM_POSIX_VERSION >= 200112L
stat_t statbuf;
int const r = UTIL_stat(infilename, &statbuf);
if (r && S_ISFIFO(statbuf.st_mode)) return 1;
if (UTIL_stat(infilename, &statbuf) && UTIL_isFIFOStat(&statbuf)) return 1;
#endif
(void)infilename;
return 0;
}
/* UTIL_isFIFO : distinguish named pipes */
int UTIL_isFIFOStat(const stat_t* statbuf)
{
/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
#if PLATFORM_POSIX_VERSION >= 200112L
if (S_ISFIFO(statbuf->st_mode)) return 1;
#endif
(void)statbuf;
return 0;
}
int UTIL_isLink(const char* infilename)
{
/* macro guards, as defined in : https://linux.die.net/man/2/lstat */
@@ -246,15 +264,20 @@ U64 UTIL_getFileSize(const char* infilename)
{
stat_t statbuf;
if (!UTIL_stat(infilename, &statbuf)) return UTIL_FILESIZE_UNKNOWN;
if (!UTIL_isRegularFile(infilename)) return UTIL_FILESIZE_UNKNOWN;
return UTIL_getFileSizeStat(&statbuf);
}
U64 UTIL_getFileSizeStat(const stat_t* statbuf)
{
if (!UTIL_isRegularFileStat(statbuf)) return UTIL_FILESIZE_UNKNOWN;
#if defined(_MSC_VER)
if (!(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
#elif defined(__MINGW32__) && defined (__MSVCRT__)
if (!(statbuf.st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
if (!(statbuf->st_mode & S_IFREG)) return UTIL_FILESIZE_UNKNOWN;
#else
if (!S_ISREG(statbuf.st_mode)) return UTIL_FILESIZE_UNKNOWN;
if (!S_ISREG(statbuf->st_mode)) return UTIL_FILESIZE_UNKNOWN;
#endif
return (U64)statbuf.st_size;
return (U64)statbuf->st_size;
}

View File

@@ -120,18 +120,24 @@ extern int g_utilDisplayLevel;
* Returns success (1) or failure (0).
*/
int UTIL_stat(const char* filename, stat_t* statbuf);
int UTIL_statFile(const char* infilename, stat_t* statbuf); /* also check it's a file */
int UTIL_statDir(const char* infilename, stat_t* statbuf); /* also check it's a directory */
/** Also checks that the target is a regular file. */
int UTIL_statFile(const char* infilename, stat_t* statbuf);
/** Also checks that the target is a directory. */
int UTIL_statDir(const char* infilename, stat_t* statbuf);
int UTIL_fileExist(const char* filename);
int UTIL_isRegularFile(const char* infilename);
int UTIL_isRegularFileStat(const stat_t* statbuf); /* same but takes existing statbuf */
int UTIL_isDirectory(const char* infilename);
int UTIL_isDirectoryStat(const stat_t* statbuf); /* same but takes existing statbuf */
int UTIL_isSameFile(const char* file1, const char* file2);
int UTIL_isCompressedFile(const char* infilename, const char *extensionList[]);
int UTIL_isLink(const char* infilename);
int UTIL_isFIFO(const char* infilename);
int UTIL_isFIFOStat(const stat_t* statbuf); /* same but takes existing statbuf */
#define UTIL_FILESIZE_UNKNOWN ((U64)(-1))
U64 UTIL_getFileSize(const char* infilename);
U64 UTIL_getFileSizeStat(const stat_t* statbuf);
U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles);
int UTIL_setFileStat(const char* filename, const stat_t* statbuf);
int UTIL_chmod(char const* filename, mode_t permissions); /*< like chmod, but avoid changing permission of /dev/null */