Skip to content

Commit f5d4da0

Browse files
authored
Merge pull request #4353 from afq984/sparse
Check regular file for sparse support after opening
2 parents c5926fb + 2a12bba commit f5d4da0

8 files changed

+42
-12
lines changed

programs/fileio.c

+13-12
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 :
@@ -641,6 +630,7 @@ FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs,
641630
}
642631

643632
{
633+
int isDstRegFile;
644634
#if defined(_WIN32)
645635
/* Windows requires opening the file as a "binary" file to avoid
646636
* mangling. This macro doesn't exist on unix. */
@@ -658,6 +648,17 @@ FIO_openDstFile(FIO_ctx_t* fCtx, FIO_prefs_t* const prefs,
658648
f = fdopen(fd, "wb");
659649
}
660650
#endif
651+
652+
/* Check regular file after opening with O_CREAT */
653+
isDstRegFile = UTIL_isFdRegularFile(fd);
654+
if (prefs->sparseFileSupport == 1) {
655+
prefs->sparseFileSupport = ZSTD_SPARSE_DEFAULT;
656+
if (!isDstRegFile) {
657+
prefs->sparseFileSupport = 0;
658+
DISPLAYLEVEL(4, "Sparse File Support is disabled when output is not a file \n");
659+
}
660+
}
661+
661662
if (f == NULL) {
662663
if (UTIL_isFileDescriptorPipe(dstFileName)) {
663664
DISPLAYLEVEL(1, "zstd: error: no output specified (use -o or -c). \n");

programs/util.c

+10
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 = fd >= 0 && 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

+1
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);

tests/cli-tests/file-stat/compress-file-to-dir-without-write-perm.sh.stderr.exact

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Trace:FileStat: > UTIL_isRegularFile(out/file.zst)
2222
Trace:FileStat: > UTIL_stat(-1, out/file.zst)
2323
Trace:FileStat: < 0
2424
Trace:FileStat: < 0
25+
Trace:FileStat: > UTIL_isFdRegularFile(-1)
26+
Trace:FileStat: < 0
2527
Trace:FileStat: > UTIL_isFileDescriptorPipe(out/file.zst)
2628
Trace:FileStat: < 0
2729
zstd: out/file.zst: Permission denied

tests/cli-tests/file-stat/compress-file-to-file.sh.stderr.exact renamed to tests/cli-tests/file-stat/compress-file-to-file.sh.stderr.glob

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Trace:FileStat: > UTIL_isRegularFile(file.zst)
2222
Trace:FileStat: > UTIL_stat(-1, file.zst)
2323
Trace:FileStat: < 0
2424
Trace:FileStat: < 0
25+
Trace:FileStat: > UTIL_isFdRegularFile(*)
26+
Trace:FileStat: > UTIL_stat(*, )
27+
Trace:FileStat: < 1
28+
Trace:FileStat: < 1
2529
Trace:FileStat: > UTIL_isRegularFile(file.zst)
2630
Trace:FileStat: > UTIL_stat(-1, file.zst)
2731
Trace:FileStat: < 1

tests/cli-tests/file-stat/compress-stdin-to-file.sh.stderr.exact renamed to tests/cli-tests/file-stat/compress-stdin-to-file.sh.stderr.glob

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Trace:FileStat: > UTIL_isRegularFile(file.zst)
1414
Trace:FileStat: > UTIL_stat(-1, file.zst)
1515
Trace:FileStat: < 0
1616
Trace:FileStat: < 0
17+
Trace:FileStat: > UTIL_isFdRegularFile(*)
18+
Trace:FileStat: > UTIL_stat(*, )
19+
Trace:FileStat: < 1
20+
Trace:FileStat: < 1
1721
Trace:FileStat: > UTIL_isRegularFile(file.zst)
1822
Trace:FileStat: > UTIL_stat(-1, file.zst)
1923
Trace:FileStat: < 1

tests/cli-tests/file-stat/decompress-file-to-file.sh.stderr.exact renamed to tests/cli-tests/file-stat/decompress-file-to-file.sh.stderr.glob

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Trace:FileStat: > UTIL_isRegularFile(file)
2222
Trace:FileStat: > UTIL_stat(-1, file)
2323
Trace:FileStat: < 0
2424
Trace:FileStat: < 0
25+
Trace:FileStat: > UTIL_isFdRegularFile(*)
26+
Trace:FileStat: > UTIL_stat(*, )
27+
Trace:FileStat: < 1
28+
Trace:FileStat: < 1
2529
Trace:FileStat: > UTIL_isRegularFile(file)
2630
Trace:FileStat: > UTIL_stat(-1, file)
2731
Trace:FileStat: < 1

tests/cli-tests/file-stat/decompress-stdin-to-file.sh.stderr.exact renamed to tests/cli-tests/file-stat/decompress-stdin-to-file.sh.stderr.glob

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Trace:FileStat: > UTIL_isRegularFile(file)
1414
Trace:FileStat: > UTIL_stat(-1, file)
1515
Trace:FileStat: < 0
1616
Trace:FileStat: < 0
17+
Trace:FileStat: > UTIL_isFdRegularFile(*)
18+
Trace:FileStat: > UTIL_stat(*, )
19+
Trace:FileStat: < 1
20+
Trace:FileStat: < 1
1721
Trace:FileStat: > UTIL_isRegularFile(file)
1822
Trace:FileStat: > UTIL_stat(-1, file)
1923
Trace:FileStat: < 1

0 commit comments

Comments
 (0)