Skip to content

Commit 3bd5aa3

Browse files
committed
Check regular file for sparse support after opening
A regular file may be created by the open call. Checking after opening allows sparseFileSupport even if dstFileName does not already exist.
1 parent c5926fb commit 3bd5aa3

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

programs/fileio.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,6 @@ FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs,
585585
const char* srcFileName, const char* dstFileName,
586586
const int mode)
587587
{
588-
int isDstRegFile;
589-
590588
if (prefs->testMode) return NULL; /* do not open file in test mode */
591589

592590
assert(dstFileName != NULL);
@@ -606,16 +604,7 @@ FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs,
606604
return NULL;
607605
}
608606

609-
isDstRegFile = UTIL_isRegularFile(dstFileName); /* invoke once */
610-
if (prefs->sparseFileSupport == 1) {
611-
prefs->sparseFileSupport = ZSTD_SPARSE_DEFAULT;
612-
if (!isDstRegFile) {
613-
prefs->sparseFileSupport = 0;
614-
DISPLAYLEVEL(4, "Sparse File Support is disabled when output is not a file \n");
615-
}
616-
}
617-
618-
if (isDstRegFile) {
607+
if (UTIL_isRegularFile(dstFileName)) {
619608
/* Check if destination file already exists */
620609
#if !defined(_WIN32)
621610
/* this test does not work on Windows :
@@ -658,6 +647,16 @@ FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs,
658647
f = fdopen(fd, "wb");
659648
}
660649
#endif
650+
651+
if (prefs->sparseFileSupport == 1) {
652+
prefs->sparseFileSupport = ZSTD_SPARSE_DEFAULT;
653+
/* Check regular file after opening with O_CREAT */
654+
if (!UTIL_isFdRegularFile(fd)) {
655+
prefs->sparseFileSupport = 0;
656+
DISPLAYLEVEL(4, "Sparse File Support is disabled when output is not a file \n");
657+
}
658+
}
659+
661660
if (f == NULL) {
662661
if (UTIL_isFileDescriptorPipe(dstFileName)) {
663662
DISPLAYLEVEL(1, "zstd: error: no output specified (use -o or -c). \n");

programs/util.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ int UTIL_stat(const char* filename, stat_t* statbuf)
197197
return UTIL_fstat(-1, filename, statbuf);
198198
}
199199

200+
int UTIL_isFdRegularFile(int fd)
201+
{
202+
stat_t statbuf;
203+
int ret;
204+
UTIL_TRACE_CALL("UTIL_isFdRegularFile(%d)", fd);
205+
ret = UTIL_fstat(fd, "", &statbuf) && UTIL_isRegularFileStat(&statbuf);
206+
UTIL_TRACE_RET(ret);
207+
return ret;
208+
}
209+
200210
int UTIL_isRegularFile(const char* infilename)
201211
{
202212
stat_t statbuf;

programs/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ int UTIL_fchmod(const int fd, char const* filename, const stat_t* statbuf, mode_
184184
* compute the needed information.
185185
*/
186186

187+
int UTIL_isFdRegularFile(int fd);
187188
int UTIL_isRegularFile(const char* infilename);
188189
int UTIL_isDirectory(const char* infilename);
189190
int UTIL_isSameFile(const char* file1, const char* file2);

0 commit comments

Comments
 (0)