Use stat() to Check that File Should be chmod()-ed

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.
This commit is contained in:
W. Felix Handte
2020-08-05 12:00:12 -04:00
parent 7238cca1a1
commit 0a8aacb4db
3 changed files with 16 additions and 6 deletions
+8 -3
View 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 */