]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Introduce Variants of Various UTIL Functions that Take Pre-Populated stat_t Structs
authorW. Felix Handte <w@felixhandte.com>
Wed, 5 Aug 2020 05:00:06 +0000 (01:00 -0400)
committerW. Felix Handte <w@felixhandte.com>
Wed, 5 Aug 2020 05:00:06 +0000 (01:00 -0400)
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.

programs/util.c
programs/util.h

index 3f91adeb57213de3e16c0816b81d907041344ee6..c81587b473bbf7d3410883980074619a5b2c608d 100644 (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_statFile(const char* infilename, stat_t *statbuf)
+int UTIL_isRegularFileStat(const stat_t* statbuf)
 {
-    const int r = UTIL_stat(infilename, statbuf);
 #if defined(_MSC_VER)
-    return r && (statbuf->st_mode & S_IFREG);
+    return (statbuf->st_mode & S_IFREG) != 0;
 #else
-    return r && S_ISREG(statbuf->st_mode);
+    return S_ISREG(statbuf->st_mode) != 0;
 #endif
 }
 
+int UTIL_statFile(const char* infilename, stat_t *statbuf)
+{
+    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;
 }
 
 
index ed3bd09ec062819eff5430e7d1a5e139541b4945..0a3be31cb671411534d991b0d8137bfb089c42fd 100644 (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 */