Create Files with Desired Permissions; Avoid chmod(); Remove UTIL_chmod()

This commit is contained in:
W. Felix Handte
2021-05-05 13:10:34 -04:00
parent 4e10ff15f5
commit b87f97b3ea
3 changed files with 31 additions and 43 deletions
+30 -28
View File
@@ -25,9 +25,10 @@
***************************************/ ***************************************/
#include "platform.h" /* Large Files support, SET_BINARY_MODE */ #include "platform.h" /* Large Files support, SET_BINARY_MODE */
#include "util.h" /* UTIL_getFileSize, UTIL_isRegularFile, UTIL_isSameFile */ #include "util.h" /* UTIL_getFileSize, UTIL_isRegularFile, UTIL_isSameFile */
#include <stdio.h> /* fprintf, fopen, fread, _fileno, stdin, stdout */ #include <stdio.h> /* fprintf, open, fdopen, fread, _fileno, stdin, stdout */
#include <stdlib.h> /* malloc, free */ #include <stdlib.h> /* malloc, free */
#include <string.h> /* strcmp, strlen */ #include <string.h> /* strcmp, strlen */
#include <fcntl.h> /* O_WRONLY */
#include <assert.h> #include <assert.h>
#include <errno.h> /* errno */ #include <errno.h> /* errno */
#include <limits.h> /* INT_MAX */ #include <limits.h> /* INT_MAX */
@@ -73,6 +74,9 @@
#define FNSPACE 30 #define FNSPACE 30
/* Default file permissions 0666 (modulated by umask) */
#define DEFAULT_FILE_PERMISSIONS (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
/*-************************************* /*-*************************************
* Macros * Macros
***************************************/ ***************************************/
@@ -637,7 +641,8 @@ static FILE* FIO_openSrcFile(const FIO_prefs_t* const prefs, const char* srcFile
* @result : FILE* to `dstFileName`, or NULL if it fails */ * @result : FILE* to `dstFileName`, or NULL if it fails */
static FILE* static FILE*
FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs, FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs,
const char* srcFileName, const char* dstFileName) const char* srcFileName, const char* dstFileName,
const int mode)
{ {
if (prefs->testMode) return NULL; /* do not open file in test mode */ if (prefs->testMode) return NULL; /* do not open file in test mode */
@@ -690,9 +695,11 @@ FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs,
FIO_removeFile(dstFileName); FIO_removeFile(dstFileName);
} } } }
{ const int old_umask = UTIL_umask(0177); /* u-x,go-rwx */ { const int fd = open(dstFileName, O_WRONLY|O_CREAT|O_TRUNC, mode);
FILE* const f = fopen( dstFileName, "wb" ); FILE* f = NULL;
UTIL_umask(old_umask); if (fd != -1) {
f = fdopen(fd, "wb");
}
if (f == NULL) { if (f == NULL) {
DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno)); DISPLAYLEVEL(1, "zstd: %s: %s\n", dstFileName, strerror(errno));
} }
@@ -1615,23 +1622,24 @@ static int FIO_compressFilename_dstFile(FIO_ctx_t* const fCtx,
int closeDstFile = 0; int closeDstFile = 0;
int result; int result;
stat_t statbuf; stat_t statbuf;
int transfer_permissions = 0;
assert(ress.srcFile != NULL); assert(ress.srcFile != NULL);
if (ress.dstFile == NULL) { if (ress.dstFile == NULL) {
int dstFilePermissions = DEFAULT_FILE_PERMISSIONS;
if ( strcmp (srcFileName, stdinmark)
&& UTIL_stat(srcFileName, &statbuf)
&& UTIL_isRegularFileStat(&statbuf) ) {
dstFilePermissions = statbuf.st_mode;
}
closeDstFile = 1; closeDstFile = 1;
DISPLAYLEVEL(6, "FIO_compressFilename_dstFile: opening dst: %s \n", dstFileName); DISPLAYLEVEL(6, "FIO_compressFilename_dstFile: opening dst: %s \n", dstFileName);
ress.dstFile = FIO_openDstFile(fCtx, prefs, srcFileName, dstFileName); ress.dstFile = FIO_openDstFile(fCtx, prefs, srcFileName, dstFileName, dstFilePermissions);
if (ress.dstFile==NULL) return 1; /* could not open dstFileName */ if (ress.dstFile==NULL) return 1; /* could not open dstFileName */
/* Must only be added after FIO_openDstFile() succeeds. /* Must only be added after FIO_openDstFile() succeeds.
* Otherwise we may delete the destination file if it already exists, * Otherwise we may delete the destination file if it already exists,
* and the user presses Ctrl-C when asked if they wish to overwrite. * and the user presses Ctrl-C when asked if they wish to overwrite.
*/ */
addHandler(dstFileName); addHandler(dstFileName);
if ( strcmp (srcFileName, stdinmark)
&& UTIL_stat(srcFileName, &statbuf)
&& UTIL_isRegularFileStat(&statbuf) )
transfer_permissions = 1;
} }
result = FIO_compressFilename_internal(fCtx, prefs, ress, dstFileName, srcFileName, compressionLevel); result = FIO_compressFilename_internal(fCtx, prefs, ress, dstFileName, srcFileName, compressionLevel);
@@ -1651,11 +1659,6 @@ static int FIO_compressFilename_dstFile(FIO_ctx_t* const fCtx,
&& strcmp(dstFileName, stdoutmark) /* special case : don't remove() stdout */ && strcmp(dstFileName, stdoutmark) /* special case : don't remove() stdout */
) { ) {
FIO_removeFile(dstFileName); /* remove compression artefact; note don't do anything special if remove() fails */ FIO_removeFile(dstFileName); /* remove compression artefact; note don't do anything special if remove() fails */
} else if (transfer_permissions) {
DISPLAYLEVEL(6, "FIO_compressFilename_dstFile: transferring permissions into dst: %s \n", dstFileName);
UTIL_setFileStat(dstFileName, &statbuf);
} else {
DISPLAYLEVEL(6, "FIO_compressFilename_dstFile: do not transfer permissions into dst: %s \n", dstFileName);
} }
} }
@@ -1827,7 +1830,7 @@ int FIO_compressMultipleFilenames(FIO_ctx_t* const fCtx,
FIO_freeCResources(&ress); FIO_freeCResources(&ress);
return 1; return 1;
} }
ress.dstFile = FIO_openDstFile(fCtx, prefs, NULL, outFileName); ress.dstFile = FIO_openDstFile(fCtx, prefs, NULL, outFileName, DEFAULT_FILE_PERMISSIONS);
if (ress.dstFile == NULL) { /* could not open outFileName */ if (ress.dstFile == NULL) { /* could not open outFileName */
error = 1; error = 1;
} else { } else {
@@ -2517,13 +2520,19 @@ static int FIO_decompressDstFile(FIO_ctx_t* const fCtx,
{ {
int result; int result;
stat_t statbuf; stat_t statbuf;
int transfer_permissions = 0;
int releaseDstFile = 0; int releaseDstFile = 0;
if ((ress.dstFile == NULL) && (prefs->testMode==0)) { if ((ress.dstFile == NULL) && (prefs->testMode==0)) {
int dstFilePermissions = DEFAULT_FILE_PERMISSIONS;
if ( strcmp(srcFileName, stdinmark) /* special case : don't transfer permissions from stdin */
&& UTIL_stat(srcFileName, &statbuf)
&& UTIL_isRegularFileStat(&statbuf) ) {
dstFilePermissions = statbuf.st_mode;
}
releaseDstFile = 1; releaseDstFile = 1;
ress.dstFile = FIO_openDstFile(fCtx, prefs, srcFileName, dstFileName); ress.dstFile = FIO_openDstFile(fCtx, prefs, srcFileName, dstFileName, dstFilePermissions);
if (ress.dstFile==NULL) return 1; if (ress.dstFile==NULL) return 1;
/* Must only be added after FIO_openDstFile() succeeds. /* Must only be added after FIO_openDstFile() succeeds.
@@ -2531,11 +2540,6 @@ static int FIO_decompressDstFile(FIO_ctx_t* const fCtx,
* and the user presses Ctrl-C when asked if they wish to overwrite. * and the user presses Ctrl-C when asked if they wish to overwrite.
*/ */
addHandler(dstFileName); addHandler(dstFileName);
if ( strcmp(srcFileName, stdinmark) /* special case : don't transfer permissions from stdin */
&& UTIL_stat(srcFileName, &statbuf)
&& UTIL_isRegularFileStat(&statbuf) )
transfer_permissions = 1;
} }
result = FIO_decompressFrames(fCtx, ress, srcFile, prefs, dstFileName, srcFileName); result = FIO_decompressFrames(fCtx, ress, srcFile, prefs, dstFileName, srcFileName);
@@ -2553,8 +2557,6 @@ static int FIO_decompressDstFile(FIO_ctx_t* const fCtx,
&& strcmp(dstFileName, stdoutmark) /* special case : don't remove() stdout */ && strcmp(dstFileName, stdoutmark) /* special case : don't remove() stdout */
) { ) {
FIO_removeFile(dstFileName); /* remove decompression artefact; note: don't do anything special if remove() fails */ FIO_removeFile(dstFileName); /* remove decompression artefact; note: don't do anything special if remove() fails */
} else if ( transfer_permissions /* file permissions correctly extracted from src */ ) {
UTIL_setFileStat(dstFileName, &statbuf); /* transfer file permissions from src into dst */
} }
} }
@@ -2756,7 +2758,7 @@ FIO_decompressMultipleFilenames(FIO_ctx_t* const fCtx,
return 1; return 1;
} }
if (!prefs->testMode) { if (!prefs->testMode) {
ress.dstFile = FIO_openDstFile(fCtx, prefs, NULL, outFileName); ress.dstFile = FIO_openDstFile(fCtx, prefs, NULL, outFileName, DEFAULT_FILE_PERMISSIONS);
if (ress.dstFile == 0) EXM_THROW(19, "cannot open %s", outFileName); if (ress.dstFile == 0) EXM_THROW(19, "cannot open %s", outFileName);
} }
for (; fCtx->currFileIdx < fCtx->nbFilesTotal; fCtx->currFileIdx++) { for (; fCtx->currFileIdx < fCtx->nbFilesTotal; fCtx->currFileIdx++) {
-9
View File
@@ -159,15 +159,6 @@ int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions)
return chmod(filename, permissions); return chmod(filename, permissions);
} }
int UTIL_umask(int mode) {
#if PLATFORM_POSIX_VERSION > 0
return umask(mode);
#else
/* do nothing, fake return value */
return mode;
#endif
}
int UTIL_setFileStat(const char *filename, const stat_t *statbuf) int UTIL_setFileStat(const char *filename, const stat_t *statbuf)
{ {
int res = 0; int res = 0;
+1 -6
View File
@@ -22,7 +22,7 @@ extern "C" {
#include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */ #include "platform.h" /* PLATFORM_POSIX_VERSION, ZSTD_NANOSLEEP_SUPPORT, ZSTD_SETPRIORITY_SUPPORT */
#include <stddef.h> /* size_t, ptrdiff_t */ #include <stddef.h> /* size_t, ptrdiff_t */
#include <sys/types.h> /* stat, utime */ #include <sys/types.h> /* stat, utime */
#include <sys/stat.h> /* stat, chmod, umask */ #include <sys/stat.h> /* stat, chmod */
#include "../lib/common/mem.h" /* U64 */ #include "../lib/common/mem.h" /* U64 */
@@ -153,11 +153,6 @@ U64 UTIL_getFileSizeStat(const stat_t* statbuf);
*/ */
int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions); int UTIL_chmod(char const* filename, const stat_t* statbuf, mode_t permissions);
/**
* Wraps umask(). Does nothing when the platform doesn't have that concept.
*/
int UTIL_umask(int mode);
/* /*
* In the absence of a pre-existing stat result on the file in question, these * In the absence of a pre-existing stat result on the file in question, these
* functions will do a stat() call internally and then use that result to * functions will do a stat() call internally and then use that result to