]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Use stat() to Check that File Should be chmod()-ed
authorW. Felix Handte <w@felixhandte.com>
Wed, 5 Aug 2020 16:00:12 +0000 (12:00 -0400)
committerW. Felix Handte <w@felixhandte.com>
Wed, 5 Aug 2020 16:00:12 +0000 (12:00 -0400)
Rather than special-casing a check for `/dev/null`, this uses `stat()` to
avoid `chmod()`-ing any non-regular file. I believe this is the desirable
behavior. `UTIL_chmod()` is never called on directories at the moment, only
output files.

programs/fileio.c
programs/util.c
programs/util.h

index 0633d39df6a591dc39ced9e4f97a9b20bb130f37..7d58a11e79ed2ed18317858293575b9005641a54 100644 (file)
@@ -509,7 +509,7 @@ static int FIO_remove(const char* path)
 #if defined(_WIN32) || defined(WIN32)
     /* windows doesn't allow remove read-only files,
      * so try to make it writable first */
-    UTIL_chmod(path, _S_IWRITE);
+    UTIL_chmod(path, NULL, _S_IWRITE);
 #endif
     return remove(path);
 }
@@ -619,7 +619,7 @@ FIO_openDstFile(FIO_prefs_t* const prefs,
                && strcmp (srcFileName, stdinmark)
                && strcmp(dstFileName, nulmark) ) {
             /* reduce rights on newly created dst file while compression is ongoing */
-            UTIL_chmod(dstFileName, 00600);
+            UTIL_chmod(dstFileName, NULL, 00600);
         }
         return f;
     }
index 69b93b8d99a738f7b109894f2ee23c2c7281fad2..fd4d7a60420ccbe5be10400fd6466523f8a33e61 100644 (file)
@@ -142,9 +142,14 @@ int UTIL_statDir(const char* infilename, stat_t *statbuf)
 }
 
 /* like chmod, but avoid changing permission of /dev/null */
-int UTIL_chmod(char const* filename, mode_t permissions)
+int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions)
 {
-    if (!strcmp(filename, "/dev/null")) return 0;   /* pretend success, but don't change anything */
+    stat_t localStatBuf;
+    if (statbuf == NULL) {
+        if (!UTIL_stat(filename, &localStatBuf)) return 0;
+        statbuf = &localStatBuf;
+    }
+    if (!UTIL_isRegularFileStat(statbuf)) return 0; /* pretend success, but don't change anything */
     return chmod(filename, permissions);
 }
 
@@ -180,7 +185,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, statbuf->st_mode & 07777);  /* Copy file permissions */
+    res += UTIL_chmod(filename, NULL, statbuf->st_mode & 07777);  /* Copy file permissions */
 
     errno = 0;
     return -res; /* number of errors is returned */
index 0a3be31cb671411534d991b0d8137bfb089c42fd..f75bca04241b17edd0dc154b0b1e6315f2f40ee5 100644 (file)
@@ -140,7 +140,12 @@ 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 */
+/**
+ * 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 that it should be modified.
+ */
+int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions);
 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);