]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Share stat() Calls in Uses of UTIL_chmod()
authorW. Felix Handte <w@felixhandte.com>
Wed, 5 Aug 2020 16:10:42 +0000 (12:10 -0400)
committerW. Felix Handte <w@felixhandte.com>
Wed, 5 Aug 2020 16:10:42 +0000 (12:10 -0400)
programs/fileio.c
programs/util.c

index 7d58a11e79ed2ed18317858293575b9005641a54..e289f9abf662f97bb004968ad4f28644cffe3554 100644 (file)
@@ -502,14 +502,21 @@ void FIO_setContentSize(FIO_prefs_t* const prefs, int value)
  * @result : Unlink `fileName`, even if it's read-only */
 static int FIO_remove(const char* path)
 {
-    if (!UTIL_isRegularFile(path)) {
-        DISPLAYLEVEL(2, "zstd: Refusing to remove non-regular file %s \n", path);
+    stat_t statbuf;
+    if (!UTIL_stat(path, &statbuf)) {
+        DISPLAYLEVEL(2, "zstd: Failed to stat %s while trying to remove it\n", path);
+        return 0;
+    }
+    if (!UTIL_isRegularFileStat(&statbuf)) {
+        DISPLAYLEVEL(2, "zstd: Refusing to remove non-regular file %s\n", path);
         return 0;
     }
 #if defined(_WIN32) || defined(WIN32)
     /* windows doesn't allow remove read-only files,
      * so try to make it writable first */
-    UTIL_chmod(path, NULL, _S_IWRITE);
+    if (!(statbuf.mode & _S_IWRITE)) {
+        UTIL_chmod(path, &statbuf, _S_IWRITE);
+    }
 #endif
     return remove(path);
 }
index fd4d7a60420ccbe5be10400fd6466523f8a33e61..9f9f45d2b146571b653316df0de3ad2fdc2762a6 100644 (file)
@@ -157,7 +157,8 @@ int UTIL_setFileStat(const char *filename, const stat_t *statbuf)
 {
     int res = 0;
 
-    if (!UTIL_isRegularFile(filename))
+    stat_t curStatBuf;
+    if (!UTIL_stat(filename, &curStatBuf) || !UTIL_isRegularFileStat(&curStatBuf))
         return -1;
 
     /* set access and modification times */
@@ -185,7 +186,7 @@ int UTIL_setFileStat(const char *filename, const stat_t *statbuf)
     res += chown(filename, statbuf->st_uid, statbuf->st_gid);  /* Copy ownership */
 #endif
 
-    res += UTIL_chmod(filename, NULL, statbuf->st_mode & 07777);  /* Copy file permissions */
+    res += UTIL_chmod(filename, &curStatBuf, statbuf->st_mode & 07777);  /* Copy file permissions */
 
     errno = 0;
     return -res; /* number of errors is returned */